From f6c156c3dcd63f0a556f37f0d417b44d7dd45902 Mon Sep 17 00:00:00 2001 From: xorrvin Date: Mon, 15 Jun 2026 17:20:18 +0200 Subject: [PATCH] Gracefully terminate clients --- cage.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/cage.c b/cage.c index 60e78a0..8eee709 100644 --- a/cage.c +++ b/cage.c @@ -198,8 +198,22 @@ cleanup_primary_client(pid_t pid) { int status; + wlr_log(WLR_DEBUG, "Sending SIGTERM to child with pid %d", pid); + kill(pid, SIGTERM); + + /* 10 sec timeout with 100ms intervals */ + for(int i = 0; i < 100; i++) { + if(waitpid(pid, &status, WNOHANG) > 0) { + goto done; + } + usleep(100000); + } + + wlr_log(WLR_INFO, "Child did not exit after 10s, sending SIGKILL"); + kill(pid, SIGKILL); waitpid(pid, &status, 0); +done: if (WIFEXITED(status)) { wlr_log(WLR_DEBUG, "Child exited normally with exit status %d", WEXITSTATUS(status)); return WEXITSTATUS(status); @@ -655,6 +669,14 @@ main(int argc, char *argv[]) seat_center_cursor(server.seat); wl_display_run(server.wl_display); + if (pid != 0) { + app_ret = cleanup_primary_client(pid); + } + + if (!ret && server.return_app_code) { + ret = app_ret; + } + #if CAGE_HAS_XWAYLAND if (xwayland) { wl_list_remove(&server.new_xwayland_surface.link); @@ -680,11 +702,6 @@ main(int argc, char *argv[]) wl_list_remove(&server.output_layout_change.link); end: - if (pid != 0) - app_ret = cleanup_primary_client(pid); - if (!ret && server.return_app_code) - ret = app_ret; - wl_event_source_remove(sigint_source); wl_event_source_remove(sigterm_source); if (sigchld_source) {