From 70ecdcb02ea073561c831ba23cbb0ad8853cfca9 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. --- cage.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cage.c b/cage.c index e4eb90f..f3622df 100644 --- a/cage.c +++ b/cage.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -162,6 +163,8 @@ drop_permissions(void) return true; } +sigjmp_buf end_mark; + static int handle_signal(int signal, void *data) { @@ -172,6 +175,7 @@ handle_signal(int signal, void *data) /* Fallthrough */ case SIGTERM: wl_display_terminate(display); + siglongjmp(end_mark, -1); return 0; default: return 0; @@ -474,17 +478,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); + wlr_cursor_warp(server.seat->cursor, NULL, layout_box->width / 2, layout_box->height / 2); + + wl_display_run(server.wl_display); } - /* Place the cursor in the center of the output layout. */ - struct wlr_box *layout_box = wlr_output_layout_get_box(server.output_layout, NULL); - wlr_cursor_warp(server.seat->cursor, NULL, layout_box->width / 2, layout_box->height / 2); - - wl_display_run(server.wl_display); - #if CAGE_HAS_XWAYLAND wlr_xwayland_destroy(xwayland); wlr_xcursor_manager_destroy(xcursor_manager);