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 (5.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: "[-t SECS] [-s SIG] PROG ARGS"
43 //usage:#define timeout_full_usage "\n\n"
44 //usage: "Runs PROG. Sends SIG to it if it is not gone in SECS seconds.\n"
45 //usage: "Defaults: SECS: 10, SIG: TERM."
49 int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50 int timeout_main(int argc UNUSED_PARAM, char **argv)
56 const char *timeout_s = "10";
61 const char *opt_s = "TERM";
63 /* -p option is not documented, it is needed to support NOMMU. */
65 /* -t SECONDS; -p PARENT_PID */
66 /* '+': stop at first non-option */
67 getopt32(argv, "+s:t:" USE_FOR_NOMMU("p:+"), &opt_s, &timeout_s, &parent);
68 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
69 signo = get_signum(opt_s);
71 bb_error_msg_and_die("unknown signal '%s'", opt_s);
72 timeout = parse_duration_str((char*)timeout_s);
74 /* We want to create a grandchild which will watch
75 * and kill the grandparent. Other methods:
76 * making parent watch child disrupts parent<->child link
77 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
78 * it's better if service_prog is a child of tcpsvd!),
79 * making child watch parent results in programs having
80 * unexpected children. */
82 if (parent) /* we were re-execed, already grandchild */
84 if (!argv[optind]) /* no PROG? */
89 sv2 = argv[optind + 1];
93 /* Child: spawn grandchild and exit */
96 argv[optind] = xasprintf("-p%u", parent);
97 argv[optind + 1] = NULL;
99 /* NB: exits with nonzero on error: */
100 bb_daemonize_or_rexec(0, argv);
101 /* Here we are grandchild. Sleep, then kill grandparent */
103 /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
108 if (kill(parent, 0)) {
109 /* process is gone */
118 wait(&status); /* wait for child to die */
119 /* Did intermediate [v]fork or exec fail? */
120 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
122 /* Ok, exec a program as requested */
128 BB_EXECVP_or_die(argv);