sway

i3-compatible Wayland compositor
git clone https://git.awy.one/sway
Log | Files | Refs | README | LICENSE

commit 8f089f0229eabbcd3f68d641d5b826220f1edb0c
parent cb246cb9c2c1440e05998d82eada21eaba107d59
Author: Daniel De Graaf <code@danieldg.net>
Date:   Sun,  9 Mar 2025 22:51:48 -0400

Use wl_event_loop_add_signal for exit signals

This avoids calling non-async-signal-safe functions from within a signal
handler.

Diffstat:
Msway/main.c | 32++++++++++++++++++--------------
1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/sway/main.c b/sway/main.c @@ -44,10 +44,6 @@ void sway_terminate(int exit_code) { } } -void sig_handler(int signal) { - sway_terminate(EXIT_SUCCESS); -} - void run_as_ipc_client(char *command, char *socket_path) { int socketfd = ipc_open_socket(socket_path); uint32_t len = strlen(command); @@ -152,6 +148,22 @@ void restore_nofile_limit(void) { } } +static int term_signal(int signal, void *data) { + sway_terminate(EXIT_SUCCESS); + return 0; +} + +static void init_signals(void) { + wl_event_loop_add_signal(server.wl_event_loop, SIGTERM, term_signal, NULL); + wl_event_loop_add_signal(server.wl_event_loop, SIGINT, term_signal, NULL); + + // avoid need to reap children + signal(SIGCHLD, SIG_IGN); + + // prevent ipc write errors from crashing sway + signal(SIGPIPE, SIG_IGN); +} + void restore_signals(void) { sigset_t set; sigemptyset(&set); @@ -330,22 +342,14 @@ int main(int argc, char **argv) { increase_nofile_limit(); - // handle SIGTERM signals - signal(SIGTERM, sig_handler); - signal(SIGINT, sig_handler); - - // avoid need to reap children - signal(SIGCHLD, SIG_IGN); - - // prevent ipc from crashing sway - signal(SIGPIPE, SIG_IGN); - sway_log(SWAY_INFO, "Starting sway version " SWAY_VERSION); if (!server_init(&server)) { return 1; } + init_signals(); + if (server.linux_dmabuf_v1) { wlr_scene_set_linux_dmabuf_v1(root->root_scene, server.linux_dmabuf_v1); }