From 5484f308b9aa53ab9c13d670d84a7fc35d447c1a Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Thu, 14 Feb 2019 15:43:34 +0000 Subject: [PATCH 1/2] swaybar: prevent signal handler from firing during termination This prevents a heap-use-after-free crash when sway terminates. --- include/swaybar/bar.h | 2 ++ swaybar/bar.c | 6 +++--- swaybar/main.c | 14 +++++--------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index 2d9ba0d9..2518d5aa 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -46,6 +46,8 @@ struct swaybar { #if HAVE_TRAY struct swaybar_tray *tray; #endif + + bool running; }; struct swaybar_output { diff --git a/swaybar/bar.c b/swaybar/bar.c index a1f7bfdb..db1c1222 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -403,8 +404,7 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) { static void display_in(int fd, short mask, void *data) { struct swaybar *bar = data; if (wl_display_dispatch(bar->display) == -1) { - bar_teardown(bar); - exit(0); + bar->running = false; } } @@ -439,7 +439,7 @@ void bar_run(struct swaybar *bar) { loop_add_fd(bar->eventloop, bar->tray->fd, POLLIN, tray_in, bar->tray->bus); } #endif - while (1) { + while (bar->running) { errno = 0; if (wl_display_flush(bar->display) == -1 && errno != EAGAIN) { break; diff --git a/swaybar/main.c b/swaybar/main.c index 4ef74629..108b16e9 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -11,13 +11,7 @@ static struct swaybar swaybar; void sig_handler(int signal) { - bar_teardown(&swaybar); - exit(0); -} - -void sway_terminate(int code) { - bar_teardown(&swaybar); - exit(code); + swaybar.running = false; } int main(int argc, char **argv) { @@ -93,8 +87,6 @@ int main(int argc, char **argv) { } } - signal(SIGTERM, sig_handler); - if (!bar_setup(&swaybar, socket_path)) { free(socket_path); return 1; @@ -102,6 +94,10 @@ int main(int argc, char **argv) { free(socket_path); + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + + swaybar.running = true; bar_run(&swaybar); bar_teardown(&swaybar); return 0; From d3c527220a445c1f88b892c0e77e801d326541b7 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Fri, 15 Feb 2019 15:15:45 +0000 Subject: [PATCH 2/2] tray: use correct parameter to set bus slot to floating Counter-intuitively, `sd_bus_slot_set_floating` expects 0 to set it to floating. --- swaybar/tray/host.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/swaybar/tray/host.c b/swaybar/tray/host.c index 215e1e72..451b0896 100644 --- a/swaybar/tray/host.c +++ b/swaybar/tray/host.c @@ -189,9 +189,9 @@ bool init_host(struct swaybar_host *host, char *protocol, goto error; } - sd_bus_slot_set_floating(reg_slot, 1); - sd_bus_slot_set_floating(unreg_slot, 1); - sd_bus_slot_set_floating(watcher_slot, 1); + sd_bus_slot_set_floating(reg_slot, 0); + sd_bus_slot_set_floating(unreg_slot, 0); + sd_bus_slot_set_floating(watcher_slot, 0); sway_log(SWAY_DEBUG, "Registered %s", host->service); return true;