summaryrefslogtreecommitdiff
path: root/bspwm/0001-Revamp-signal-handling.patch
blob: 6399b3974352e9b156e3b425ff3e5227d51a8da2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
From 7fbc56a7be07d25d38aab5c300bed433ec6b1f58 Mon Sep 17 00:00:00 2001
From: James Cook <falsifian@falsifian.org>
Date: Fri, 12 Jan 2024 01:02:53 +0000
Subject: [PATCH 1/3] 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 4619a0c..f1d1e6c 100644
--- a/src/bspwm.c
+++ b/src/bspwm.c
@@ -27,7 +27,6 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/time.h>
-#include <sys/wait.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <fcntl.h>
@@ -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 30f972e..9ee3e52 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;
 
-- 
2.47.1