From 569de936abb90f4c7cdca9da111a6ea780b135bf Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 14 May 2024 12:33:03 +0100 Subject: kill: killing a zombie process should fail A process which has exited may still have its process handle held open by its children. Such a process doesn't appear in the process table. It is thus similar to a zombie process in UNIX. Using kill(1) to interact with such a process was seen to succeed, contrary to expectation. The code for "ordinary" signals in kill(2) did check if the process was still active but didn't treat an attempt to kill an inactive process as an error. Furthermore, sending SIGKILL or the fake signal 0 to a process didn't even check if the process was still active. Rearrange the implementation of kill(2) so that an attempt to signal an inactive process is treated as an error. This also consolidates handling of SIGKILL and signal 0 with "ordinary" signals. Saves 96 bytes. (GitHub issue #416) --- coreutils/timeout.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'coreutils/timeout.c') diff --git a/coreutils/timeout.c b/coreutils/timeout.c index ff58a753a..802ddfc07 100644 --- a/coreutils/timeout.c +++ b/coreutils/timeout.c @@ -54,7 +54,9 @@ static HANDLE child = INVALID_HANDLE_VALUE; static void kill_child(void) { if (child != INVALID_HANDLE_VALUE) { - kill_signal_by_handle(child, SIGTERM); + pid_t pid = (pid_t)GetProcessId(child); + if (pid) + kill(pid, SIGTERM); } } @@ -206,13 +208,15 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) status = signo == SIGKILL ? 137 : 124; pid = (pid_t)GetProcessId(child); - kill(pid, signo); + if (pid) { + kill(pid, signo); - if (kill_timeout > 0) { - if (timeout_wait(kill_timeout, child, &status)) - goto finish; - kill(pid, SIGKILL); - status = 137; + if (kill_timeout > 0) { + if (timeout_wait(kill_timeout, child, &status)) + goto finish; + kill(pid, SIGKILL); + status = 137; + } } finish: CloseHandle(child); -- cgit v1.2.3-55-g6feb