annotate toys/other/timeout.c @ 952:ce0519f6457c

Add timeout, factoring out common code from sleep.
author Rob Landley <rob@landley.net>
date Wed, 17 Jul 2013 17:22:46 -0500
parents
children 85f297591693
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_TIMEOUT(NEWTOY(timeout, "<2^k:s: ", TOYFLAG_BIN))
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.
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 */
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 #define FOR_timeout
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #include "toys.h"
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 GLOBALS(
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 char *s_signal;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 char *k_timeout;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 int nextsig;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 pid_t pid;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 struct timeval ktv;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 struct itimerval itv;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 )
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 static void handler(int i)
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 kill(TT.pid, TT.nextsig);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
42
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 if (TT.k_timeout) {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 TT.k_timeout = 0;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 TT.nextsig = SIGKILL;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 signal(SIGALRM, handler);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 TT.itv.it_value = TT.ktv;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 setitimer(ITIMER_REAL, &TT.itv, (void *)&toybuf);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 }
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 }
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 void timeout_main(void)
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 // 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
55 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
56
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 if (TT.k_timeout)
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 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
59 TT.nextsig = SIGTERM;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 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
61 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
62
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 if (!(TT.pid = fork())) xexec_optargs(1);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 else {
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 int status;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
66
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 signal(SIGALRM, handler);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 setitimer(ITIMER_REAL, &TT.itv, (void *)&toybuf);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 while (-1 == waitpid(TT.pid, &status, 0) && errno == EINTR);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 if (WIFEXITED(status)) toys.exitval = WEXITSTATUS(status);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 else if (WIFSIGNALED(status)) toys.exitval = WTERMSIG(status);
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 else toys.exitval = 1;
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 }
ce0519f6457c Add timeout, factoring out common code from sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }