From d8d8298514a571b2d43598f772102bbe1d1dec8b Mon Sep 17 00:00:00 2001 From: James Cook Date: Fri, 12 Jan 2024 01:02:53 +0000 Subject: [PATCH 1/2] Revamp signal handling. - Change from signal to sigaction, which is more portable. - Install a handler for SIGPIPE instead of ignoring it, to avoid passing on the ignored status to other programs launched from bspwm. - Use SA_NOCLDWAIT to avoid the need to call waitpid. (NB if waitpid were called in the handler, it would be a good idea to protect errno.) - Declare the "running" global as volatile sig_atomic_t, as recommended for values modified from signal handlers. --- src/bspwm.c | 29 +++++++++++++++++------------ src/bspwm.h | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/bspwm.c b/src/bspwm.c index 4619a0c2..f1d1e6cf 100644 --- a/src/bspwm.c +++ b/src/bspwm.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -87,7 +86,7 @@ bool auto_raise; bool sticky_still; bool hide_sticky; bool record_history; -bool running; +volatile sig_atomic_t running; bool restart; bool randr; @@ -104,6 +103,7 @@ int main(int argc, char *argv[]) xcb_generic_event_t *event; char *end; int opt; + struct sigaction sigact; while ((opt = getopt(argc, argv, "hvc:s:o:")) != -1) { switch (opt) { @@ -194,11 +194,20 @@ int main(int argc, char *argv[]) fcntl(sock_fd, F_SETFD, FD_CLOEXEC | fcntl(sock_fd, F_GETFD)); - signal(SIGINT, sig_handler); - signal(SIGHUP, sig_handler); - signal(SIGTERM, sig_handler); - signal(SIGCHLD, sig_handler); - signal(SIGPIPE, SIG_IGN); + sigact.sa_handler = sig_handler; + sigemptyset(&sigact.sa_mask); + sigact.sa_flags = SA_RESTART; + sigaction(SIGINT, &sigact, NULL); + sigaction(SIGHUP, &sigact, NULL); + sigaction(SIGTERM, &sigact, NULL); + /* We avoid using SIG_IGN with SIGPIPE because that would be preserved across + exec. */ + sigaction(SIGPIPE, &sigact, NULL); + + sigact.sa_handler = SIG_IGN; + sigact.sa_flags = SA_NOCLDWAIT; + sigaction(SIGCHLD, &sigact, NULL); + run_config(run_level); running = true; @@ -527,11 +536,7 @@ bool check_connection (xcb_connection_t *dpy) void sig_handler(int sig) { - if (sig == SIGCHLD) { - signal(sig, sig_handler); - while (waitpid(-1, 0, WNOHANG) > 0) - ; - } else if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) { + if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) { running = false; } } diff --git a/src/bspwm.h b/src/bspwm.h index 30f972eb..9ee3e52d 100644 --- a/src/bspwm.h +++ b/src/bspwm.h @@ -84,7 +84,7 @@ extern bool auto_raise; extern bool sticky_still; extern bool hide_sticky; extern bool record_history; -extern bool running; +extern volatile sig_atomic_t running; extern bool restart; extern bool randr; From 3914174580dcf5baeea1187be76c1b9c3772a51d Mon Sep 17 00:00:00 2001 From: James Cook Date: Sat, 13 Jan 2024 03:54:12 +0000 Subject: [PATCH 2/2] Add missing #include . --- src/bspwm.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bspwm.h b/src/bspwm.h index 9ee3e52d..bee32d35 100644 --- a/src/bspwm.h +++ b/src/bspwm.h @@ -25,6 +25,7 @@ #ifndef BSPWM_BSPWM_H #define BSPWM_BSPWM_H +#include #include "types.h" #define WM_NAME "bspwm"