2 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/procctl.h>
45 #define EXIT_TIMEOUT 124
47 static sig_atomic_t sig_chld = 0;
48 static sig_atomic_t sig_term = 0;
49 static sig_atomic_t sig_alrm = 0;
50 static sig_atomic_t sig_ign = 0;
56 fprintf(stderr, "Usage: %s [--signal sig | -s sig] [--preserve-status]"
57 " [--kill-after time | -k time] [--foreground] <duration> <command>"
58 " <arg ...>\n", getprogname());
64 parse_duration(const char *duration)
69 ret = strtod(duration, &end);
70 if (ret == 0 && end == duration)
71 errx(125, "invalid duration");
73 if (end == NULL || *end == '\0')
76 if (end != NULL && *(end + 1) != '\0')
77 errx(125, "invalid duration");
92 errx(125, "invalid duration");
95 if (ret < 0 || ret >= 100000000UL)
96 errx(125, "invalid duration");
102 parse_signal(const char *str)
107 sig = strtonum(str, 1, sys_nsig - 1, &errstr);
111 if (strncasecmp(str, "SIG", 3) == 0)
114 for (i = 1; i < sys_nsig; i++) {
115 if (strcasecmp(str, sys_signame[i]) == 0)
119 errx(125, "invalid signal");
123 sig_handler(int signo)
125 if (sig_ign != 0 && signo == sig_ign) {
148 set_interval(double iv)
150 struct itimerval tim;
152 memset(&tim, 0, sizeof(tim));
153 tim.it_value.tv_sec = (time_t)iv;
155 tim.it_value.tv_usec = (suseconds_t)(iv * 1000000UL);
157 if (setitimer(ITIMER_REAL, &tim, NULL) == -1)
158 err(EXIT_FAILURE, "setitimer()");
162 main(int argc, char **argv)
166 int foreground, preserve;
167 int error, pstat, status;
168 int killsig = SIGTERM;
172 bool timedout = false;
173 bool do_second_kill = false;
174 bool child_done = false;
175 struct sigaction signals;
176 struct procctl_reaper_status info;
177 struct procctl_reaper_kill killemall;
188 foreground = preserve = 0;
191 const struct option longopts[] = {
192 { "preserve-status", no_argument, &preserve, 1 },
193 { "foreground", no_argument, &foreground, 1 },
194 { "kill-after", required_argument, NULL, 'k'},
195 { "signal", required_argument, NULL, 's'},
196 { "help", no_argument, NULL, 'h'},
200 while ((ch = getopt_long(argc, argv, "+k:s:h", longopts, NULL)) != -1) {
203 do_second_kill = true;
204 second_kill = parse_duration(optarg);
207 killsig = parse_signal(optarg);
224 first_kill = parse_duration(argv[0]);
229 /* Acquire a reaper */
230 if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == -1)
231 err(EXIT_FAILURE, "Fail to acquire the reaper");
234 memset(&signals, 0, sizeof(signals));
235 sigemptyset(&signals.sa_mask);
237 if (killsig != SIGKILL && killsig != SIGSTOP)
238 signums[0] = killsig;
240 for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++)
241 sigaddset(&signals.sa_mask, signums[i]);
243 signals.sa_handler = sig_handler;
244 signals.sa_flags = SA_RESTART;
246 for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++)
247 if (signums[i] != -1 && signums[i] != 0 &&
248 sigaction(signums[i], &signals, NULL) == -1)
249 err(EXIT_FAILURE, "sigaction()");
251 signal(SIGTTIN, SIG_IGN);
252 signal(SIGTTOU, SIG_IGN);
256 err(EXIT_FAILURE, "fork()");
259 signal(SIGTTIN, SIG_DFL);
260 signal(SIGTTOU, SIG_DFL);
262 error = execvp(argv[0], argv);
265 err(127, "exec(%s)", argv[0]);
267 err(126, "exec(%s)", argv[0]);
271 if (sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL) == -1)
272 err(EXIT_FAILURE, "sigprocmask()");
274 /* parent continues here */
275 set_interval(first_kill);
278 sigemptyset(&signals.sa_mask);
279 sigsuspend(&signals.sa_mask);
284 while ((cpid = waitpid(-1, &status, WNOHANG)) != 0) {
290 } else if (cpid == pid) {
299 procctl(P_PID, getpid(),
300 PROC_REAP_STATUS, &info);
301 if (info.rs_children == 0)
305 } else if (sig_alrm) {
310 killemall.rk_sig = killsig;
311 killemall.rk_flags = 0;
312 procctl(P_PID, getpid(), PROC_REAP_KILL,
317 if (do_second_kill) {
318 set_interval(second_kill);
325 } else if (sig_term) {
327 killemall.rk_sig = sig_term;
328 killemall.rk_flags = 0;
329 procctl(P_PID, getpid(), PROC_REAP_KILL,
334 if (do_second_kill) {
335 set_interval(second_kill);
344 while (!child_done && wait(&pstat) == -1) {
346 err(EXIT_FAILURE, "waitpid()");
350 procctl(P_PID, getpid(), PROC_REAP_RELEASE, NULL);
352 if (WEXITSTATUS(pstat))
353 pstat = WEXITSTATUS(pstat);
354 else if (WIFSIGNALED(pstat))
355 pstat = 128 + WTERMSIG(pstat);
357 if (timedout && !preserve)
358 pstat = EXIT_TIMEOUT;