blob: 4d598ead2d2ac1bfa45b71e833d8111712f22d28 [file] [log] [blame]
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +00001/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * COPYING NOTES
3 *
4 * timeout.c -- a timeout handler for shell commands
5 *
6 * Copyright (C) 2005-6, Roberto A. Foglietta <me@roberto.foglietta.name>
7 *
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.
11 *
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.
16 *
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.
20 */
21/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
22 * REVISION NOTES:
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
29 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010030//config:config TIMEOUT
31//config: bool "timeout"
32//config: default y
33//config: help
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.
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000036
Pere Orga5bc8c002011-04-11 03:29:49 +020037//usage:#define timeout_trivial_usage
38//usage: "[-t SECS] [-s SIG] PROG ARGS"
39//usage:#define timeout_full_usage "\n\n"
40//usage: "Runs PROG. Sends SIG to it if it is not gone in SECS seconds.\n"
41//usage: "Defaults: SECS: 10, SIG: TERM."
42
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000043#include "libbb.h"
44
45int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
46int timeout_main(int argc UNUSED_PARAM, char **argv)
47{
48 int signo;
49 int status;
50 int parent = 0;
51 int timeout = 10;
52 pid_t pid;
53#if !BB_MMU
54 char *sv1, *sv2;
55#endif
56 const char *opt_s = "TERM";
57
58 /* -p option is not documented, it is needed to support NOMMU. */
59
60 /* -t SECONDS; -p PARENT_PID */
Denis Vlasenko8a2f6bf2008-11-24 22:34:47 +000061 /* '+': stop at first non-option */
Denys Vlasenko237bedd2016-07-06 21:58:02 +020062 getopt32(argv, "+s:t:+" USE_FOR_NOMMU("p:+"), &opt_s, &timeout, &parent);
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000063 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
64 signo = get_signum(opt_s);
65 if (signo < 0)
66 bb_error_msg_and_die("unknown signal '%s'", opt_s);
67
68 /* We want to create a grandchild which will watch
69 * and kill the grandparent. Other methods:
70 * making parent watch child disrupts parent<->child link
71 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
72 * it's better if service_prog is a child of tcpsvd!),
73 * making child watch parent results in programs having
74 * unexpected children. */
75
76 if (parent) /* we were re-execed, already grandchild */
77 goto grandchild;
78 if (!argv[optind]) /* no PROG? */
79 bb_show_usage();
80
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000081#if !BB_MMU
82 sv1 = argv[optind];
83 sv2 = argv[optind + 1];
84#endif
Pascal Bellard926031b2010-07-04 15:32:38 +020085 pid = xvfork();
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000086 if (pid == 0) {
87 /* Child: spawn grandchild and exit */
88 parent = getppid();
89#if !BB_MMU
90 argv[optind] = xasprintf("-p%u", parent);
91 argv[optind + 1] = NULL;
92#endif
93 /* NB: exits with nonzero on error: */
94 bb_daemonize_or_rexec(0, argv);
95 /* Here we are grandchild. Sleep, then kill grandparent */
96 grandchild:
Denys Vlasenko099e5282011-02-02 00:59:35 +010097 /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +000098 while (1) {
99 sleep(1);
100 if (--timeout <= 0)
101 break;
102 if (kill(parent, 0)) {
103 /* process is gone */
104 return EXIT_SUCCESS;
105 }
106 }
107 kill(parent, signo);
108 return EXIT_SUCCESS;
109 }
110
111 /* Parent */
112 wait(&status); /* wait for child to die */
113 /* Did intermediate [v]fork or exec fail? */
114 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
115 return EXIT_FAILURE;
116 /* Ok, exec a program as requested */
117 argv += optind;
118#if !BB_MMU
119 argv[0] = sv1;
120 argv[1] = sv2;
121#endif
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200122 BB_EXECVP_or_die(argv);
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +0000123}