diff options
author | Ron Yorston <rmy@pobox.com> | 2018-03-27 14:39:09 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-03-27 14:39:09 +0100 |
commit | c8d01c9f4b7bdbb88af9f796cf0b90e488812c86 (patch) | |
tree | dfc1a6a60502fc5fd8812f2e4147fdf564cca0e4 | |
parent | b8d57003cf8d175f7b854240d2bf01fcce8f3ffd (diff) | |
download | busybox-w32-c8d01c9f4b7bdbb88af9f796cf0b90e488812c86.tar.gz busybox-w32-c8d01c9f4b7bdbb88af9f796cf0b90e488812c86.tar.bz2 busybox-w32-c8d01c9f4b7bdbb88af9f796cf0b90e488812c86.zip |
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.
-rw-r--r-- | coreutils/timeout.c | 26 |
1 files changed, 19 insertions, 7 deletions
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 @@ | |||
46 | 46 | ||
47 | #include "libbb.h" | 47 | #include "libbb.h" |
48 | 48 | ||
49 | #if ENABLE_PLATFORM_MINGW32 | ||
50 | HANDLE child = INVALID_HANDLE_VALUE; | ||
51 | |||
52 | static void kill_child(void) | ||
53 | { | ||
54 | if (child != INVALID_HANDLE_VALUE) { | ||
55 | kill_SIGTERM_by_handle(child, 128+SIGTERM); | ||
56 | } | ||
57 | } | ||
58 | #endif | ||
59 | |||
49 | int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 60 | int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
50 | int timeout_main(int argc UNUSED_PARAM, char **argv) | 61 | int timeout_main(int argc UNUSED_PARAM, char **argv) |
51 | { | 62 | { |
@@ -54,7 +65,6 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
54 | int status; | 65 | int status; |
55 | #else | 66 | #else |
56 | intptr_t ret; | 67 | intptr_t ret; |
57 | HANDLE h; | ||
58 | DWORD status = EXIT_SUCCESS; | 68 | DWORD status = EXIT_SUCCESS; |
59 | #endif | 69 | #endif |
60 | int parent = 0; | 70 | int parent = 0; |
@@ -148,24 +158,26 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
148 | bb_perror_msg_and_die("can't execute '%s'", argv[0]); | 158 | bb_perror_msg_and_die("can't execute '%s'", argv[0]); |
149 | } | 159 | } |
150 | 160 | ||
151 | h = (HANDLE)ret; | 161 | child = (HANDLE)ret; |
162 | atexit(kill_child); | ||
152 | while (1) { | 163 | while (1) { |
153 | sleep(1); | 164 | sleep(1); |
154 | if (--timeout <= 0) { | 165 | if (signo && --timeout <= 0) { |
155 | status = signo == SIGKILL ? 137 : 124; | 166 | status = signo == SIGKILL ? 137 : 124; |
156 | break; | 167 | break; |
157 | } | 168 | } |
158 | if (WaitForSingleObject(h, 0) == WAIT_OBJECT_0) { | 169 | if (WaitForSingleObject(child, 0) == WAIT_OBJECT_0) { |
159 | /* process is gone */ | 170 | /* process is gone */ |
160 | GetExitCodeProcess(h, &status); | 171 | GetExitCodeProcess(child, &status); |
161 | goto finish; | 172 | goto finish; |
162 | } | 173 | } |
163 | } | 174 | } |
164 | 175 | ||
165 | pid = (pid_t)GetProcessId(h); | 176 | pid = (pid_t)GetProcessId(child); |
166 | kill(pid, signo); | 177 | kill(pid, signo); |
167 | finish: | 178 | finish: |
168 | CloseHandle(h); | 179 | CloseHandle(child); |
180 | child = INVALID_HANDLE_VALUE; | ||
169 | return status; | 181 | return status; |
170 | #endif | 182 | #endif |
171 | } | 183 | } |