diff options
| author | Chris Down <chris@chrisdown.name> | 2023-01-28 12:53:48 +0100 |
|---|---|---|
| committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2023-01-28 13:34:43 +0100 |
| commit | 712d6639ff8e863560328131bbb92b248dc9cde7 (patch) | |
| tree | c563872871c1e3e947d40caae45614498140e8dd /dwm.c | |
| parent | grabkeys: Avoid missing events when a keysym maps to multiple keycodes (diff) | |
| download | dwm-712d6639ff8e863560328131bbb92b248dc9cde7.tar.gz dwm-712d6639ff8e863560328131bbb92b248dc9cde7.zip | |
Use sigaction(SA_NOCLDWAIT) for SIGCHLD handling
signal() semantics are pretty unclearly specified. For example, depending on OS
kernel and libc, the handler may be returned to SIG_DFL (hence the inner call
to read the signal handler). Moving to sigaction() means the behaviour is
consistently defined.
Using SA_NOCLDWAIT also allows us to avoid calling the non-reentrant function
die() in the handler.
Some addditional notes for archival purposes:
* NRK pointed out errno of waitpid could also theoretically get clobbered.
* The original patch was iterated on and modified by NRK and Hiltjo:
* SIG_DFL was changed to SIG_IGN, this is required, atleast on older systems
such as tested on Slackware 11.
* signals are not blocked using sigprocmask, because in theory it would
briefly for example also ignore a SIGTERM signal. It is OK if waitpid() is (in
theory interrupted).
POSIX reference:
"Consequences of Process Termination":
https://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html#tag_16_01_03_01
Diffstat (limited to '')
| -rw-r--r-- | dwm.c | 20 |
1 files changed, 9 insertions, 11 deletions
| @@ -205,7 +205,6 @@ static void setmfact(const Arg *arg); | |||
| 205 | static void setup(void); | 205 | static void setup(void); |
| 206 | static void seturgent(Client *c, int urg); | 206 | static void seturgent(Client *c, int urg); |
| 207 | static void showhide(Client *c); | 207 | static void showhide(Client *c); |
| 208 | static void sigchld(int unused); | ||
| 209 | static void spawn(const Arg *arg); | 208 | static void spawn(const Arg *arg); |
| 210 | static void tag(const Arg *arg); | 209 | static void tag(const Arg *arg); |
| 211 | static void tagmon(const Arg *arg); | 210 | static void tagmon(const Arg *arg); |
| @@ -1543,9 +1542,16 @@ setup(void) | |||
| 1543 | int i; | 1542 | int i; |
| 1544 | XSetWindowAttributes wa; | 1543 | XSetWindowAttributes wa; |
| 1545 | Atom utf8string; | 1544 | Atom utf8string; |
| 1545 | struct sigaction sa; | ||
| 1546 | 1546 | ||
| 1547 | /* clean up any zombies immediately */ | 1547 | /* do not transform children into zombies when they terminate */ |
| 1548 | sigchld(0); | 1548 | sigemptyset(&sa.sa_mask); |
| 1549 | sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART; | ||
| 1550 | sa.sa_handler = SIG_IGN; | ||
| 1551 | sigaction(SIGCHLD, &sa, NULL); | ||
| 1552 | |||
| 1553 | /* clean up any zombies (inherited from .xinitrc etc) immediately */ | ||
| 1554 | while (waitpid(-1, NULL, WNOHANG) > 0); | ||
| 1549 | 1555 | ||
| 1550 | /* init screen */ | 1556 | /* init screen */ |
| 1551 | screen = DefaultScreen(dpy); | 1557 | screen = DefaultScreen(dpy); |
| @@ -1639,14 +1645,6 @@ showhide(Client *c) | |||
| 1639 | } | 1645 | } |
| 1640 | 1646 | ||
| 1641 | void | 1647 | void |
| 1642 | sigchld(int unused) | ||
| 1643 | { | ||
| 1644 | if (signal(SIGCHLD, sigchld) == SIG_ERR) | ||
| 1645 | die("can't install SIGCHLD handler:"); | ||
| 1646 | while (0 < waitpid(-1, NULL, WNOHANG)); | ||
| 1647 | } | ||
| 1648 | |||
| 1649 | void | ||
| 1650 | spawn(const Arg *arg) | 1648 | spawn(const Arg *arg) |
| 1651 | { | 1649 | { |
| 1652 | if (arg->v == dmenucmd) | 1650 | if (arg->v == dmenucmd) |
