swaybar: ensure correct init order for status_line

`$WAYLAND_SOCKET` is unset by `wl_display_connect` after it has
successfully connected to the wayland socket.

However, subprocesses spawned by swaybar (status-command) don't have
access to waybar's fds as $WAYLAND_SOCKET is O_CLOEXEC. This means any
status command which itself tries to connect to wayland will fail if
this environment variable is set.

Reorder display and status-command initialization so that this variable
is not set and add an assert so we can enforce this invariant in future.
This commit is contained in:
Luke Drummond 2020-06-11 01:29:07 +01:00 committed by Simon Ser
parent bf8d23a836
commit c22caa54a8
2 changed files with 8 additions and 4 deletions

View file

@ -413,10 +413,6 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) {
if (!ipc_initialize(bar)) { if (!ipc_initialize(bar)) {
return false; return false;
} }
if (bar->config->status_command) {
bar->status = status_line_init(bar->config->status_command);
bar->status->bar = bar;
}
bar->display = wl_display_connect(NULL); bar->display = wl_display_connect(NULL);
if (!bar->display) { if (!bar->display) {
@ -445,6 +441,11 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) {
assert(pointer->cursor_surface); assert(pointer->cursor_surface);
} }
if (bar->config->status_command) {
bar->status = status_line_init(bar->config->status_command);
bar->status->bar = bar;
}
#if HAVE_TRAY #if HAVE_TRAY
if (!bar->config->tray_hidden) { if (!bar->config->tray_hidden) {
bar->tray = create_tray(bar); bar->tray = create_tray(bar);

View file

@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <json.h> #include <json.h>
@ -153,6 +154,8 @@ struct status_line *status_line_init(char *cmd) {
exit(1); exit(1);
} }
assert(!getenv("WAYLAND_SOCKET") && "display must be initialized before "
" starting `status-command`; WAYLAND_SOCKET should not be set");
status->pid = fork(); status->pid = fork();
if (status->pid == 0) { if (status->pid == 0) {
dup2(pipe_read_fd[1], STDOUT_FILENO); dup2(pipe_read_fd[1], STDOUT_FILENO);