From 55ecf3a746db5131ca078875168a59feab8b26f1 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 8 Apr 2024 13:22:30 +0100 Subject: timeout: fix handling of timeouts The 'timeout' applet, by default, accepted fractional seconds but ignored the fraction. Moreover, in busybox-w32, even the integer part was incorrectly handled. Rewrite the (Windows) code to fix both problems. A patch has also been submitted to fix upstream. If that is accepted the Windows port will need to be updated to match. Saves 8-16 bytes. (GitHub issue #400) --- coreutils/timeout.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/coreutils/timeout.c b/coreutils/timeout.c index 0d6a2c612..ff58a753a 100644 --- a/coreutils/timeout.c +++ b/coreutils/timeout.c @@ -59,18 +59,13 @@ static void kill_child(void) } /* Return TRUE if child exits before timeout expires */ +/* NB timeout is in milliseconds */ static NOINLINE int timeout_wait(int timeout, HANDLE proc, DWORD *status) { - /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */ - while (1) { - sleep1(); - if (--timeout <= 0) - break; - if (WaitForSingleObject(proc, 0) == WAIT_OBJECT_0) { - /* process is gone */ - GetExitCodeProcess(proc, status); - return TRUE; - } + if (WaitForSingleObject(proc, timeout) == WAIT_OBJECT_0) { + /* process is gone */ + GetExitCodeProcess(proc, status); + return TRUE; } return FALSE; } @@ -136,11 +131,11 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) kill_timeout = 0; if (opt_k) - kill_timeout = parse_duration_str(opt_k); + kill_timeout = parse_duration_str(opt_k) IF_PLATFORM_MINGW32(* 1000); if (!argv[optind]) bb_show_usage(); - timeout = parse_duration_str(argv[optind++]); + timeout = parse_duration_str(argv[optind++]) IF_PLATFORM_MINGW32(* 1000); if (!argv[optind]) /* no PROG? */ bb_show_usage(); -- cgit v1.2.3-55-g6feb