1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4 * timeout.c -- a timeout handler for shell commands
6 * Copyright (C) 2005-6, Roberto A. Foglietta <me@roberto.foglietta.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
23 * released 17-11-2005 by Roberto A. Foglietta
24 * talarm 04-12-2005 by Roberto A. Foglietta
25 * modified 05-12-2005 by Roberto A. Foglietta
26 * sizerdct 06-12-2005 by Roberto A. Foglietta
27 * splitszf 12-05-2006 by Roberto A. Foglietta
28 * rewrite 14-11-2008 vda
30 //config:config TIMEOUT
31 //config: bool "timeout (6.5 kb)"
34 //config: Runs a program and watches it. If it does not terminate in
35 //config: specified number of seconds, it is sent a signal.
37 //applet:IF_TIMEOUT(APPLET(timeout, BB_DIR_USR_BIN, BB_SUID_DROP))
39 //kbuild:lib-$(CONFIG_TIMEOUT) += timeout.o
41 //usage:#define timeout_trivial_usage
42 //usage: "[-s SIG] [-k KILL_SECS] SECS PROG ARGS"
43 //usage:#define timeout_full_usage "\n\n"
44 //usage: "Run PROG. Send SIG to it if it is not gone in SECS seconds.\n"
45 //usage: "Default SIG: TERM."
46 //usage: "If it still exists in KILL_SECS seconds, send KILL.\n"
50 static NOINLINE
int timeout_wait(duration_t timeout
, pid_t pid
)
52 /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
54 #if ENABLE_FLOAT_DURATION
56 sleep_for_duration(timeout
);
70 int timeout_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
71 int timeout_main(int argc UNUSED_PARAM
, char **argv
)
77 duration_t kill_timeout
;
82 const char *opt_s
= "TERM";
85 /* -p option is not documented, it is needed to support NOMMU. */
87 /* -t SECONDS; -p PARENT_PID */
88 /* '+': stop at first non-option */
89 getopt32(argv
, "+s:k:" USE_FOR_NOMMU("p:+"), &opt_s
, &opt_k
, &parent
);
90 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
92 signo
= get_signum(opt_s
);
94 bb_error_msg_and_die("unknown signal '%s'", opt_s
);
98 kill_timeout
= parse_duration_str(opt_k
);
102 timeout
= parse_duration_str(argv
[optind
++]);
103 if (!argv
[optind
]) /* no PROG? */
106 /* We want to create a grandchild which will watch
107 * and kill the grandparent. Other methods:
108 * making parent watch child disrupts parent<->child link
109 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
110 * it's better if service_prog is a child of tcpsvd!),
111 * making child watch parent results in programs having
112 * unexpected children. */
114 if (parent
) /* we were re-execed, already grandchild */
119 sv2
= argv
[optind
+ 1];
123 /* Child: spawn grandchild and exit */
126 argv
[optind
] = xasprintf("-p%u", parent
);
127 argv
[optind
+ 1] = NULL
;
129 /* NB: exits with nonzero on error: */
130 bb_daemonize_or_rexec(0, argv
);
131 /* Here we are grandchild. Sleep, then kill grandparent */
133 if (timeout_wait(timeout
, parent
) == EXIT_SUCCESS
)
137 if (kill_timeout
> 0) {
138 if (timeout_wait(kill_timeout
, parent
) == EXIT_SUCCESS
)
140 kill(parent
, SIGKILL
);
147 wait(&status
); /* wait for child to die */
148 /* Did intermediate [v]fork or exec fail? */
149 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
151 /* Ok, exec a program as requested */
157 BB_EXECVP_or_die(argv
);