summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2025-11-28 17:34:23 +0000
committerPádraig Brady <P@draigBrady.com>2025-11-29 12:17:27 +0000
commitfc5c7b2e47fbe55034f4c8aa2f67caabcc5933dd (patch)
tree963874beede17f3fb4c8e54a2939e15ad51c5bd6 /src
parentd1d311e7a244b908833fdae2f84fb5a613c055c7 (diff)
downloadcoreutils-fc5c7b2e47fbe55034f4c8aa2f67caabcc5933dd.tar.gz
coreutils-fc5c7b2e47fbe55034f4c8aa2f67caabcc5933dd.zip
timeout: ensure we terminate command upon abnormal exit
* src/timeout.c (main): Use PR_SET_PDEATHSIG to ensure the child is terminated even if the parent terminates abnormally. * tests/timeout/timeout-group.sh: Add a case to ensure sending SIGKILL results in the termination of the monitored command. * NEWS: Mention the improvement.
Diffstat (limited to 'src')
-rw-r--r--src/timeout.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/timeout.c b/src/timeout.c
index 68ddfd5d6..7634323d4 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -592,6 +592,14 @@ main (int argc, char **argv)
}
else if (monitored_pid == 0) /* child */
{
+#if HAVE_PRCTL
+ /* Add protection if the parent dies without signalling child. */
+ prctl (PR_SET_PDEATHSIG, term_signal);
+#endif
+ /* If we're already reparented to init, don't proceed. */
+ if (getppid () == 1)
+ return EXIT_CANCELED;
+
/* Restore signal mask for child. */
if (sigprocmask (SIG_SETMASK, &orig_set, nullptr) != 0)
{