Mercurial > hg > toybox
comparison toys/other/timeout.c @ 1726:26170eb7685d draft
Fix thinko (don't &toybuf to get scratch space) and add -v option.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 09 Mar 2015 15:06:10 -0500 |
parents | cbb1aca81eca |
children | c0ef9b7976f0 |
comparison
equal
deleted
inserted
replaced
1725:b2b2d214727a | 1726:26170eb7685d |
---|---|
2 * | 2 * |
3 * Copyright 2013 Rob Landley <rob@landley.net> | 3 * Copyright 2013 Rob Landley <rob@landley.net> |
4 * | 4 * |
5 * No standard | 5 * No standard |
6 | 6 |
7 USE_TIMEOUT(NEWTOY(timeout, "<2^k:s: ", TOYFLAG_BIN)) | 7 USE_TIMEOUT(NEWTOY(timeout, "<2^vk:s: ", TOYFLAG_BIN)) |
8 | 8 |
9 config TIMEOUT | 9 config TIMEOUT |
10 bool "timeout" | 10 bool "timeout" |
11 default y | 11 default y |
12 depends on TOYBOX_FLOAT | 12 depends on TOYBOX_FLOAT |
19 Length can be a decimal fraction. An optional suffix can be "m" | 19 Length can be a decimal fraction. An optional suffix can be "m" |
20 (minutes), "h" (hours), "d" (days), or "s" (seconds, the default). | 20 (minutes), "h" (hours), "d" (days), or "s" (seconds, the default). |
21 | 21 |
22 -s Send specified signal (default TERM) | 22 -s Send specified signal (default TERM) |
23 -k Send KILL signal if child still running this long after first signal. | 23 -k Send KILL signal if child still running this long after first signal. |
24 -v Verbose | |
24 */ | 25 */ |
25 | 26 |
26 #define FOR_timeout | 27 #define FOR_timeout |
27 #include "toys.h" | 28 #include "toys.h" |
28 | 29 |
36 struct itimerval itv; | 37 struct itimerval itv; |
37 ) | 38 ) |
38 | 39 |
39 static void handler(int i) | 40 static void handler(int i) |
40 { | 41 { |
42 fprintf(stderr, "timeout pid %d signal %d\n", TT.pid, TT.nextsig); | |
41 kill(TT.pid, TT.nextsig); | 43 kill(TT.pid, TT.nextsig); |
42 | 44 |
43 if (TT.k_timeout) { | 45 if (TT.k_timeout) { |
44 TT.k_timeout = 0; | 46 TT.k_timeout = 0; |
45 TT.nextsig = SIGKILL; | 47 TT.nextsig = SIGKILL; |
46 signal(SIGALRM, handler); | 48 signal(SIGALRM, handler); |
47 TT.itv.it_value = TT.ktv; | 49 TT.itv.it_value = TT.ktv; |
48 setitimer(ITIMER_REAL, &TT.itv, (void *)&toybuf); | 50 setitimer(ITIMER_REAL, &TT.itv, (void *)toybuf); |
49 } | 51 } |
50 } | 52 } |
51 | 53 |
52 void timeout_main(void) | 54 void timeout_main(void) |
53 { | 55 { |
63 if (!(TT.pid = xfork())) xexec(toys.optargs+1); | 65 if (!(TT.pid = xfork())) xexec(toys.optargs+1); |
64 else { | 66 else { |
65 int status; | 67 int status; |
66 | 68 |
67 signal(SIGALRM, handler); | 69 signal(SIGALRM, handler); |
68 setitimer(ITIMER_REAL, &TT.itv, (void *)&toybuf); | 70 setitimer(ITIMER_REAL, &TT.itv, (void *)toybuf); |
69 while (-1 == waitpid(TT.pid, &status, 0) && errno == EINTR); | 71 while (-1 == waitpid(TT.pid, &status, 0) && errno == EINTR); |
70 toys.exitval = WIFEXITED(status) | 72 toys.exitval = WIFEXITED(status) |
71 ? WEXITSTATUS(status) : WTERMSIG(status) + 127; | 73 ? WEXITSTATUS(status) : WTERMSIG(status) + 127; |
72 } | 74 } |
73 } | 75 } |