Krolyxon
Commit fddad75

Branches

Refs →

nsxivfddad75

don't spoil errno in sig handler (#411)

reported by thread-sanitizer.

the sighandler's spoiled `errno` was causing xlib to incorrectly assume some
error occurred and thus causing the crash described in #391.

to reproduce:

* Open an nsxiv window
* Open another terminal and run the following:

	var=$(pidof nsxiv); while :; do kill -s SIGCHLD $var; done

putting the `pid` into a variable is actually important because doing
`$(pidof nsxiv)` inside the loop makes it really hard to reproduce the
issue, I presume because of the extra process invocation it was sending
less SIGCHLD and so putting it into a variable avoids that overhead and
is able to generate more signals.

instead of reaping the zombies manually, we now pass the
`SA_NOCLDSTOP|SA_NOCLDWAIT` for SIGCHLD instead so that the zombies are
reaped automatically.
Author
NRK
Date
2023-01-26 16:26 +0000
Parent
75849ad
Commit
fddad757c6d2645ed7b2d8f64931818d5b3b7a00

+5 −10

1 file changed

  1. M main.c +5 −10
M main.c View
@@ -843,19 +843,14 @@ static void run(void)
843843 }
844844 }
845845
846−static void sigchld(int sig)
847−{
848− while (waitpid(-1, NULL, WNOHANG) > 0);
849−}
850−
851−static void setup_signal(int sig, void (*handler)(int sig))
846+static void setup_signal(int sig, void (*handler)(int sig), int flags)
852847 {
853848 struct sigaction sa;
854849
855850 sa.sa_handler = handler;
856851 sigemptyset(&sa.sa_mask);
857− sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
858− if (sigaction(sig, &sa, 0) == -1)
852+ sa.sa_flags = flags;
853+ if (sigaction(sig, &sa, NULL) < 0)
859854 error(EXIT_FAILURE, errno, "signal %d", sig);
860855 }
861856
@@ -865,8 +860,8 @@ int main(int argc, char *argv[])
865860 size_t n;
866861 const char *homedir, *dsuffix = "";
867862
868− setup_signal(SIGCHLD, sigchld);
869− setup_signal(SIGPIPE, SIG_IGN);
863+ setup_signal(SIGCHLD, SIG_DFL, SA_RESTART|SA_NOCLDSTOP|SA_NOCLDWAIT);
864+ setup_signal(SIGPIPE, SIG_IGN, 0);
870865
871866 setlocale(LC_COLLATE, "");
872867