From c8d01c9f4b7bdbb88af9f796cf0b90e488812c86 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 27 Mar 2018 14:39:09 +0100 Subject: timeout: futher improvements When signal 0 is sent to the child process both coreutils and BusyBox timeout allow the child to continue running. busybox-w32 was sending the signal and then killing the child: be consistent. When timeout is interrupted by SIGTERM or Ctrl-C from an interactive shell kill the child too. --- coreutils/timeout.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'coreutils/timeout.c') diff --git a/coreutils/timeout.c b/coreutils/timeout.c index 297d08d16..c1be7b198 100644 --- a/coreutils/timeout.c +++ b/coreutils/timeout.c @@ -46,6 +46,17 @@ #include "libbb.h" +#if ENABLE_PLATFORM_MINGW32 +HANDLE child = INVALID_HANDLE_VALUE; + +static void kill_child(void) +{ + if (child != INVALID_HANDLE_VALUE) { + kill_SIGTERM_by_handle(child, 128+SIGTERM); + } +} +#endif + int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int timeout_main(int argc UNUSED_PARAM, char **argv) { @@ -54,7 +65,6 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) int status; #else intptr_t ret; - HANDLE h; DWORD status = EXIT_SUCCESS; #endif int parent = 0; @@ -148,24 +158,26 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) bb_perror_msg_and_die("can't execute '%s'", argv[0]); } - h = (HANDLE)ret; + child = (HANDLE)ret; + atexit(kill_child); while (1) { sleep(1); - if (--timeout <= 0) { + if (signo && --timeout <= 0) { status = signo == SIGKILL ? 137 : 124; break; } - if (WaitForSingleObject(h, 0) == WAIT_OBJECT_0) { + if (WaitForSingleObject(child, 0) == WAIT_OBJECT_0) { /* process is gone */ - GetExitCodeProcess(h, &status); + GetExitCodeProcess(child, &status); goto finish; } } - pid = (pid_t)GetProcessId(h); + pid = (pid_t)GetProcessId(child); kill(pid, signo); finish: - CloseHandle(h); + CloseHandle(child); + child = INVALID_HANDLE_VALUE; return status; #endif } -- cgit v1.2.3-55-g6feb