From 638d4fb715555dbb867d982f7c21dfdc995d2fb8 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Fri, 15 May 2020 13:39:12 -0500 Subject: [PATCH] Use sigsetjmp to continue cleanup after SIGTERM/SIGINT This uses the non-local goto sigsetjmp function to continue cleaning up after a SIGINT or SIGTERM has been caught. Normally, SIGINT or SIGTERM signal the end of the program and cannot be ignored. We jump to the cleanup in the signal handler. (cherry picked from commit 70ecdcb02ea073561c831ba23cbb0ad8853cfca9) --- cage.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cage.c b/cage.c index a08fafc..2dcac30 100644 --- a/cage.c +++ b/cage.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -173,6 +174,8 @@ drop_permissions(void) return true; } +sigjmp_buf end_mark; + static int handle_signal(int signal, void *data) { @@ -183,6 +186,7 @@ handle_signal(int signal, void *data) /* Fallthrough */ case SIGTERM: wl_display_terminate(display); + siglongjmp(end_mark, -1); return 0; default: return 0; @@ -539,17 +543,19 @@ main(int argc, char *argv[]) wlr_xwayland_set_seat(xwayland, server.seat->seat); #endif - if (!spawn_primary_client(server.wl_display, argv + optind, &pid, &sigchld_source)) { - ret = 1; - goto end; - } + if (sigsetjmp(end_mark, 0) == 0) { + if (!spawn_primary_client(server.wl_display, argv + optind, &pid, &sigchld_source)) { + ret = 1; + goto end; + } - /* Place the cursor in the center of the output layout. */ - struct wlr_box layout_box; - wlr_output_layout_get_box(server.output_layout, NULL, &layout_box); - wlr_cursor_warp(server.seat->cursor, NULL, layout_box.width / 2, layout_box.height / 2); + /* Place the cursor in the center of the output layout. */ + struct wlr_box layout_box; + wlr_output_layout_get_box(server.output_layout, NULL, &layout_box); + wlr_cursor_warp(server.seat->cursor, NULL, layout_box.width / 2, layout_box.height / 2); - wl_display_run(server.wl_display); + wl_display_run(server.wl_display); + } #if CAGE_HAS_XWAYLAND wlr_xwayland_destroy(xwayland);