Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / miscutils / timeout.c
blob9d56593bae4e456964b66266a96039a0e43cbc57
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * COPYING NOTES
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 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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
31 //usage:#define timeout_trivial_usage
32 //usage: "[-t SECS] [-s SIG] PROG ARGS"
33 //usage:#define timeout_full_usage "\n\n"
34 //usage: "Runs PROG. Sends SIG to it if it is not gone in SECS seconds.\n"
35 //usage: "Defaults: SECS: 10, SIG: TERM."
37 #include "libbb.h"
39 int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40 int timeout_main(int argc UNUSED_PARAM, char **argv)
42 int signo;
43 int status;
44 int parent = 0;
45 int timeout = 10;
46 pid_t pid;
47 #if !BB_MMU
48 char *sv1, *sv2;
49 #endif
50 const char *opt_s = "TERM";
52 /* -p option is not documented, it is needed to support NOMMU. */
54 /* -t SECONDS; -p PARENT_PID */
55 opt_complementary = "t+" USE_FOR_NOMMU(":p+");
56 /* '+': stop at first non-option */
57 getopt32(argv, "+s:t:" USE_FOR_NOMMU("p:"), &opt_s, &timeout, &parent);
58 /*argv += optind; - no, wait for bb_daemonize_or_rexec! */
59 signo = get_signum(opt_s);
60 if (signo < 0)
61 bb_error_msg_and_die("unknown signal '%s'", opt_s);
63 /* We want to create a grandchild which will watch
64 * and kill the grandparent. Other methods:
65 * making parent watch child disrupts parent<->child link
66 * (example: "tcpsvd 0.0.0.0 1234 timeout service_prog" -
67 * it's better if service_prog is a child of tcpsvd!),
68 * making child watch parent results in programs having
69 * unexpected children. */
71 if (parent) /* we were re-execed, already grandchild */
72 goto grandchild;
73 if (!argv[optind]) /* no PROG? */
74 bb_show_usage();
76 #if !BB_MMU
77 sv1 = argv[optind];
78 sv2 = argv[optind + 1];
79 #endif
80 pid = xvfork();
81 if (pid == 0) {
82 /* Child: spawn grandchild and exit */
83 parent = getppid();
84 #if !BB_MMU
85 argv[optind] = xasprintf("-p%u", parent);
86 argv[optind + 1] = NULL;
87 #endif
88 /* NB: exits with nonzero on error: */
89 bb_daemonize_or_rexec(0, argv);
90 /* Here we are grandchild. Sleep, then kill grandparent */
91 grandchild:
92 /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
93 while (1) {
94 sleep(1);
95 if (--timeout <= 0)
96 break;
97 if (kill(parent, 0)) {
98 /* process is gone */
99 return EXIT_SUCCESS;
102 kill(parent, signo);
103 return EXIT_SUCCESS;
106 /* Parent */
107 wait(&status); /* wait for child to die */
108 /* Did intermediate [v]fork or exec fail? */
109 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
110 return EXIT_FAILURE;
111 /* Ok, exec a program as requested */
112 argv += optind;
113 #if !BB_MMU
114 argv[0] = sv1;
115 argv[1] = sv2;
116 #endif
117 BB_EXECVP_or_die(argv);