Mercurial > hg > toybox
annotate toys/other/timeout.c @ 1727:c0ef9b7976f0 draft
Use xsignal() instead of signal().
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 10 Mar 2015 11:07:28 -0500 |
parents | 26170eb7685d |
children |
rev | line source |
---|---|
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* timeout.c - Run command line with a timeout |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * Copyright 2013 Rob Landley <rob@landley.net> |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * No standard |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
1726
26170eb7685d
Fix thinko (don't &toybuf to get scratch space) and add -v option.
Rob Landley <rob@landley.net>
parents:
1676
diff
changeset
|
7 USE_TIMEOUT(NEWTOY(timeout, "<2^vk:s: ", TOYFLAG_BIN)) |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 config TIMEOUT |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 bool "timeout" |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 default y |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 depends on TOYBOX_FLOAT |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 help |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 usage: timeout [-k LENGTH] [-s SIGNAL] LENGTH COMMAND... |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 Run command line as a child process, sending child a signal if the |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 command doesn't exit soon enough. |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 Length can be a decimal fraction. An optional suffix can be "m" |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 (minutes), "h" (hours), "d" (days), or "s" (seconds, the default). |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 -s Send specified signal (default TERM) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 -k Send KILL signal if child still running this long after first signal. |
1726
26170eb7685d
Fix thinko (don't &toybuf to get scratch space) and add -v option.
Rob Landley <rob@landley.net>
parents:
1676
diff
changeset
|
24 -v Verbose |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 */ |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 #define FOR_timeout |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 #include "toys.h" |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 GLOBALS( |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 char *s_signal; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 char *k_timeout; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 int nextsig; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 pid_t pid; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 struct timeval ktv; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 struct itimerval itv; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 ) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 static void handler(int i) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 { |
1726
26170eb7685d
Fix thinko (don't &toybuf to get scratch space) and add -v option.
Rob Landley <rob@landley.net>
parents:
1676
diff
changeset
|
42 fprintf(stderr, "timeout pid %d signal %d\n", TT.pid, TT.nextsig); |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 kill(TT.pid, TT.nextsig); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 if (TT.k_timeout) { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 TT.k_timeout = 0; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 TT.nextsig = SIGKILL; |
1727
c0ef9b7976f0
Use xsignal() instead of signal().
Rob Landley <rob@landley.net>
parents:
1726
diff
changeset
|
48 xsignal(SIGALRM, handler); |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 TT.itv.it_value = TT.ktv; |
1726
26170eb7685d
Fix thinko (don't &toybuf to get scratch space) and add -v option.
Rob Landley <rob@landley.net>
parents:
1676
diff
changeset
|
50 setitimer(ITIMER_REAL, &TT.itv, (void *)toybuf); |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 } |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 } |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 void timeout_main(void) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 // Parse early to get any errors out of the way. |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 TT.itv.it_value.tv_sec = xparsetime(*toys.optargs, 1000000, &TT.itv.it_value.tv_usec); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 if (TT.k_timeout) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 TT.ktv.tv_sec = xparsetime(TT.k_timeout, 1000000, &TT.ktv.tv_usec); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 TT.nextsig = SIGTERM; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 if (TT.s_signal && -1 == (TT.nextsig = sig_to_num(TT.s_signal))) |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 error_exit("bad -s: '%s'", TT.s_signal); |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 |
1676
cbb1aca81eca
Make toy_exec() check if argc is in optargs and deal with it there so we don't need a separate xexec_optargs().
Rob Landley <rob@landley.net>
parents:
1327
diff
changeset
|
65 if (!(TT.pid = xfork())) xexec(toys.optargs+1); |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 else { |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 int status; |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 |
1727
c0ef9b7976f0
Use xsignal() instead of signal().
Rob Landley <rob@landley.net>
parents:
1726
diff
changeset
|
69 xsignal(SIGALRM, handler); |
1726
26170eb7685d
Fix thinko (don't &toybuf to get scratch space) and add -v option.
Rob Landley <rob@landley.net>
parents:
1676
diff
changeset
|
70 setitimer(ITIMER_REAL, &TT.itv, (void *)toybuf); |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 while (-1 == waitpid(TT.pid, &status, 0) && errno == EINTR); |
1327
85f297591693
Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents:
952
diff
changeset
|
72 toys.exitval = WIFEXITED(status) |
85f297591693
Introduce xfork() and make commands use it, and make some WEXITSTATUS() use WIFEXITED() and WTERMSIG()+127.
Rob Landley <rob@landley.net>
parents:
952
diff
changeset
|
73 ? WEXITSTATUS(status) : WTERMSIG(status) + 127; |
952
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 } |
ce0519f6457c
Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 } |