hexdump: accept hex numbers in -n, closes 16195
[busybox-git.git] / coreutils / timeout.c
blob84299a0a5537fb53f5b64c6e6a262b6cf7594807
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
30 //config:config TIMEOUT
31 //config: bool "timeout (6.5 kb)"
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.
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"
48 #include "libbb.h"
50 static NOINLINE int timeout_wait(duration_t timeout, pid_t pid)
52 /* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
53 while (1) {
54 #if ENABLE_FLOAT_DURATION
55 if (timeout < 1)
56 sleep_for_duration(timeout);
57 else
58 #endif
59 sleep1();
60 if (--timeout <= 0)
61 break;
62 if (kill(pid, 0)) {
63 /* process is gone */
64 return EXIT_SUCCESS;
67 return EXIT_FAILURE;
70 int timeout_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
71 int timeout_main(int argc UNUSED_PARAM, char **argv)
73 int signo;
74 int status;
75 int parent = 0;
76 duration_t timeout;
77 duration_t kill_timeout;
78 pid_t pid;
79 #if !BB_MMU
80 char *sv1, *sv2;
81 #endif
82 const char *opt_s = "TERM";
83 char *opt_k = NULL;
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);
93 if (signo < 0)
94 bb_error_msg_and_die("unknown signal '%s'", opt_s);
96 kill_timeout = 0;
97 if (opt_k)
98 kill_timeout = parse_duration_str(opt_k);
100 if (!argv[optind])
101 bb_show_usage();
102 timeout = parse_duration_str(argv[optind++]);
103 if (!argv[optind]) /* no PROG? */
104 bb_show_usage();
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 */
115 goto grandchild;
117 #if !BB_MMU
118 sv1 = argv[optind];
119 sv2 = argv[optind + 1];
120 #endif
121 pid = xvfork();
122 if (pid == 0) {
123 /* Child: spawn grandchild and exit */
124 parent = getppid();
125 #if !BB_MMU
126 argv[optind] = xasprintf("-p%u", parent);
127 argv[optind + 1] = NULL;
128 #endif
129 /* NB: exits with nonzero on error: */
130 bb_daemonize_or_rexec(0, argv);
131 /* Here we are grandchild. Sleep, then kill grandparent */
132 grandchild:
133 if (timeout_wait(timeout, parent) == EXIT_SUCCESS)
134 return EXIT_SUCCESS;
135 kill(parent, signo);
137 if (kill_timeout > 0) {
138 if (timeout_wait(kill_timeout, parent) == EXIT_SUCCESS)
139 return EXIT_SUCCESS;
140 kill(parent, SIGKILL);
143 return EXIT_SUCCESS;
146 /* Parent */
147 wait(&status); /* wait for child to die */
148 /* Did intermediate [v]fork or exec fail? */
149 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
150 return EXIT_FAILURE;
151 /* Ok, exec a program as requested */
152 argv += optind;
153 #if !BB_MMU
154 argv[0] = sv1;
155 argv[1] = sv2;
156 #endif
157 BB_EXECVP_or_die(argv);