From 5e253fdd9ac5c8733203eec9870aa0ca2cd238fd Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Fri, 26 Feb 2016 09:08:05 +0100 Subject: [PATCH] Correctly exit sway on errors. Calling `exit` in sway_terminate prevents sway from correctly shutting down (freeing data, cleanly terminating the ipc server, etc.). A better way is to exit straight away if the failure occurs before `wlc_run` and use sway_abort as usual if it occur when wlc is running. --- common/log.c | 2 +- include/sway.h | 2 +- sway/commands.c | 2 +- sway/main.c | 18 +++++++++++------- swaybar/main.c | 4 ++-- swaybg/main.c | 4 ++-- swaygrab/main.c | 4 ++-- swaylock/main.c | 4 ++-- swaymsg/main.c | 4 ++-- 9 files changed, 24 insertions(+), 20 deletions(-) diff --git a/common/log.c b/common/log.c index adba00210..6d958db29 100644 --- a/common/log.c +++ b/common/log.c @@ -58,7 +58,7 @@ void sway_abort(const char *format, ...) { vfprintf(stderr, format, args); va_end(args); fprintf(stderr, "\n"); - sway_terminate(); + sway_terminate(EXIT_FAILURE); } #ifndef NDEBUG diff --git a/include/sway.h b/include/sway.h index 6499c81df..b5cfb668e 100644 --- a/include/sway.h +++ b/include/sway.h @@ -1,6 +1,6 @@ #ifndef _SWAY_SWAY_H #define _SWAY_SWAY_H -void sway_terminate(void); +void sway_terminate(int exit_code); #endif diff --git a/sway/commands.c b/sway/commands.c index 055473d5e..c4b7f6ab5 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -450,7 +450,7 @@ static struct cmd_results *cmd_exit(int argc, char **argv) { } // Close all views close_views(&root_container); - sway_terminate(); + sway_terminate(EXIT_SUCCESS); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/main.c b/sway/main.c index 6adbf89d7..7ea392b67 100644 --- a/sway/main.c +++ b/sway/main.c @@ -21,16 +21,17 @@ #include "sway.h" static bool terminate_request = false; +static int exit_value = 0; -void sway_terminate(void) { +void sway_terminate(int exit_code) { terminate_request = true; + exit_value = exit_code; wlc_terminate(); - exit(EXIT_FAILURE); } void sig_handler(int signal) { close_views(&root_container); - sway_terminate(); + sway_terminate(EXIT_SUCCESS); } static void wlc_log_handler(enum wlc_log_type type, const char *str) { @@ -150,16 +151,19 @@ int main(int argc, char **argv) { if (optind < argc) { // Behave as IPC client if(optind != 1) { - sway_abort("Don't use options with the IPC client"); + sway_log(L_ERROR, "Don't use options with the IPC client"); + exit(EXIT_FAILURE); } if (getuid() != geteuid() || getgid() != getegid()) { if (setgid(getgid()) != 0 || setuid(getuid()) != 0) { - sway_abort("Unable to drop root"); + sway_log(L_ERROR, "Unable to drop root"); + exit(EXIT_FAILURE); } } char *socket_path = getenv("SWAYSOCK"); if (!socket_path) { - sway_abort("Unable to retrieve socket path"); + sway_log(L_ERROR, "Unable to retrieve socket path"); + exit(EXIT_FAILURE); } char *command = join_args(argv + optind, argc - optind); run_as_ipc_client(command, socket_path); @@ -224,6 +228,6 @@ int main(int argc, char **argv) { ipc_terminate(); - return 0; + return exit_value; } diff --git a/swaybar/main.c b/swaybar/main.c index 737ee647e..c6bbc7a5e 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -10,9 +10,9 @@ /* global bar state */ struct bar swaybar; -void sway_terminate(void) { +void sway_terminate(int exit_code) { bar_teardown(&swaybar); - exit(EXIT_FAILURE); + exit(exit_code); } void sig_handler(int signal) { diff --git a/swaybg/main.c b/swaybg/main.c index b936be2b8..fbd0d16be 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -21,7 +21,7 @@ enum scaling_mode { SCALING_MODE_TILE, }; -void sway_terminate(void) { +void sway_terminate(int exit_code) { int i; for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; @@ -29,7 +29,7 @@ void sway_terminate(void) { } list_free(surfaces); registry_teardown(registry); - exit(EXIT_FAILURE); + exit(exit_code); } int main(int argc, const char **argv) { diff --git a/swaygrab/main.c b/swaygrab/main.c index 82d623e7a..6ba8fb3e0 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -11,8 +11,8 @@ #include "ipc-client.h" #include "util.h" -void sway_terminate(void) { - exit(EXIT_FAILURE); +void sway_terminate(int exit_code) { + exit(exit_code); } void grab_and_apply_magick(const char *file, const char *output, diff --git a/swaylock/main.c b/swaylock/main.c index 9b14086d7..b20883af0 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -24,7 +24,7 @@ enum scaling_mode { SCALING_MODE_TILE, }; -void sway_terminate(void) { +void sway_terminate(int exit_code) { int i; for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; @@ -32,7 +32,7 @@ void sway_terminate(void) { } list_free(surfaces); registry_teardown(registry); - exit(EXIT_FAILURE); + exit(exit_code); } char *password; diff --git a/swaymsg/main.c b/swaymsg/main.c index 22572b6fc..88a8fab0e 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -11,8 +11,8 @@ #include "readline.h" #include "log.h" -void sway_terminate(void) { - exit(EXIT_FAILURE); +void sway_terminate(int exit_code) { + exit(exit_code); } int main(int argc, char **argv) {