From ee00e1be0f5bdf1881af91c22d8258bb7046bf96 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 20 Jul 2022 14:48:32 -0500 Subject: [PATCH] Fix race condition with SIGCHLD being delivered before timeout was ready. --- toys/other/timeout.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/toys/other/timeout.c b/toys/other/timeout.c index 221ef2e0..5e3cb204 100644 --- a/toys/other/timeout.c +++ b/toys/other/timeout.c @@ -33,12 +33,12 @@ GLOBALS( char *s, *k; struct pollfd pfd; + sigjmp_buf sj; ) static void handler(int sig) { - close(TT.pfd.fd); - TT.pfd.fd = -1; + siglongjmp(TT.sj, 1); } static long nantomil(struct timespec *ts) @@ -63,15 +63,15 @@ void timeout_main(void) if (!FLAG(foreground)) setpgid(0, 0); toys.exitval = 0; + TT.pfd.events = POLLIN; + if (sigsetjmp(TT.sj, 1)) goto done; + xsignal_flags(SIGCHLD, handler, SA_NOCLDSTOP); pid = xpopen_both(toys.optargs+1, FLAG(i) ? fds : 0); if (!FLAG(i)) xpipe(fds); - TT.pfd.events = POLLIN; TT.pfd.fd = fds[1]; ms = nantomil(&tts); - xsignal_flags(SIGCHLD, handler, SA_NOCLDSTOP); for (;;) { if (1 != xpoll(&TT.pfd, 1, ms)) { - if (-1==TT.pfd.fd) break; if (FLAG(v)) perror_msg("sending signal %s to command %s", num_to_sig(nextsig), toys.optargs[1]); @@ -93,6 +93,7 @@ void timeout_main(void) } if (TT.pfd.revents&POLLHUP) break; } +done: xsignal(SIGCHLD, SIG_DFL); ii = xpclose_both(pid, fds); -- 2.39.2