From 5c8a3afff92f9d5ca157bdfc03483a805d1b52e8 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Mon, 21 Dec 2015 11:46:15 +0100 Subject: [PATCH] swaybar: Correct handling of SIGTERM. Swaybar did not correctly handle the SIGTERM sent from sway when exiting, ultimately leaving the child status_command behind. This should correctly handle the SIGTERM signal and terminate the status_command. Fix #386 --- swaybar/main.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/swaybar/main.c b/swaybar/main.c index 88cd1dbe..4323d370 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -87,7 +88,7 @@ struct colors colors = { }, }; -void sway_terminate(void) { +void swaybar_teardown() { window_teardown(window); if (registry) { registry_teardown(registry); @@ -99,16 +100,31 @@ void sway_terminate(void) { if (pid) { // terminate status_command process - kill(pid, SIGTERM); + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } } if (pipefd[0]) { close(pipefd[0]); } +} + +void sway_terminate(void) { + swaybar_teardown(); exit(EXIT_FAILURE); } +void sig_handler(int signal) { + swaybar_teardown(); + exit(0); +} + void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { cairo_set_source_rgba(cairo, ((color & 0xFF000000) >> 24) / 256.0, @@ -525,14 +541,12 @@ int main(int argc, char **argv) { line[0] = '\0'; } + signal(SIGTERM, sig_handler); + poll_for_update(); - window_teardown(window); - registry_teardown(registry); - fclose(command); - // terminate status_command process - kill(pid, SIGTERM); - close(pipefd[0]); + // gracefully shutdown swaybar and status_command + swaybar_teardown(); return 0; }