From ee37aaec97522b3d375c4a9f70c4020d78cf9833 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 3 Oct 2022 04:20:27 -0500 Subject: [PATCH] Kill process group (child processes too), and ignore SIGTTIN and friends. --- toys/other/timeout.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/toys/other/timeout.c b/toys/other/timeout.c index 6454c66b..d0c93ccd 100644 --- a/toys/other/timeout.c +++ b/toys/other/timeout.c @@ -47,6 +47,11 @@ static long nantomil(struct timespec *ts) return ts->tv_sec*1000+ts->tv_nsec/1000000; } +static void callback(char *argv[]) +{ + if (!FLAG(foreground)) setpgid(0, 0); +} + void timeout_main(void) { int ii, ms, nextsig; @@ -59,14 +64,16 @@ void timeout_main(void) nextsig = SIGTERM; if (TT.s && -1==(nextsig = sig_to_num(TT.s))) error_exit("bad -s: '%s'",TT.s); - if (!FLAG(foreground)) setpgid(0, 0); toys.exitval = 0; TT.pfd.events = POLLIN; TT.fds[1] = -1; if (sigsetjmp(TT.sj, 1)) goto done; xsignal_flags(SIGCHLD, handler, SA_NOCLDSTOP); - TT.pid = xpopen_both(toys.optargs+1, FLAG(i) ? TT.fds : 0); + TT.pid = xpopen_setup(toys.optargs+1, FLAG(i) ? TT.fds : 0, callback); + xsignal(SIGTTIN, SIG_IGN); + xsignal(SIGTTOU, SIG_IGN); + xsignal(SIGTSTP, SIG_IGN); if (!FLAG(i)) xpipe(TT.fds); TT.pfd.fd = TT.fds[1]; ms = nantomil(&tts); @@ -76,7 +83,7 @@ void timeout_main(void) perror_msg("sending signal %s to command %s", num_to_sig(nextsig), toys.optargs[1]); toys.exitval = (nextsig==9) ? 137 : 124; - kill(TT.pid, nextsig); + kill(FLAG(foreground) ? TT.pid : -TT.pid, nextsig); if (!TT.k || nextsig==SIGKILL) break; nextsig = SIGKILL; ms = nantomil(&kts); -- 2.39.2