diff options
author | Ron Yorston <rmy@pobox.com> | 2024-04-08 13:22:30 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-04-08 13:22:30 +0100 |
commit | 55ecf3a746db5131ca078875168a59feab8b26f1 (patch) | |
tree | bc75d34e77344bbf28d9dde9e6d6dafcd615367a | |
parent | 758a72f55360a1f3a5400aeca29bc2ea0647bad1 (diff) | |
download | busybox-w32-55ecf3a746db5131ca078875168a59feab8b26f1.tar.gz busybox-w32-55ecf3a746db5131ca078875168a59feab8b26f1.tar.bz2 busybox-w32-55ecf3a746db5131ca078875168a59feab8b26f1.zip |
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)
-rw-r--r-- | coreutils/timeout.c | 19 |
1 files 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) | |||
59 | } | 59 | } |
60 | 60 | ||
61 | /* Return TRUE if child exits before timeout expires */ | 61 | /* Return TRUE if child exits before timeout expires */ |
62 | /* NB timeout is in milliseconds */ | ||
62 | static NOINLINE int timeout_wait(int timeout, HANDLE proc, DWORD *status) | 63 | static NOINLINE int timeout_wait(int timeout, HANDLE proc, DWORD *status) |
63 | { | 64 | { |
64 | /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */ | 65 | if (WaitForSingleObject(proc, timeout) == WAIT_OBJECT_0) { |
65 | while (1) { | 66 | /* process is gone */ |
66 | sleep1(); | 67 | GetExitCodeProcess(proc, status); |
67 | if (--timeout <= 0) | 68 | return TRUE; |
68 | break; | ||
69 | if (WaitForSingleObject(proc, 0) == WAIT_OBJECT_0) { | ||
70 | /* process is gone */ | ||
71 | GetExitCodeProcess(proc, status); | ||
72 | return TRUE; | ||
73 | } | ||
74 | } | 69 | } |
75 | return FALSE; | 70 | return FALSE; |
76 | } | 71 | } |
@@ -136,11 +131,11 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
136 | 131 | ||
137 | kill_timeout = 0; | 132 | kill_timeout = 0; |
138 | if (opt_k) | 133 | if (opt_k) |
139 | kill_timeout = parse_duration_str(opt_k); | 134 | kill_timeout = parse_duration_str(opt_k) IF_PLATFORM_MINGW32(* 1000); |
140 | 135 | ||
141 | if (!argv[optind]) | 136 | if (!argv[optind]) |
142 | bb_show_usage(); | 137 | bb_show_usage(); |
143 | timeout = parse_duration_str(argv[optind++]); | 138 | timeout = parse_duration_str(argv[optind++]) IF_PLATFORM_MINGW32(* 1000); |
144 | if (!argv[optind]) /* no PROG? */ | 139 | if (!argv[optind]) /* no PROG? */ |
145 | bb_show_usage(); | 140 | bb_show_usage(); |
146 | 141 | ||