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>
46 #define EXIT_TIMEOUT 124
48 static sig_atomic_t sig_chld
= 0;
49 static sig_atomic_t sig_term
= 0;
50 static sig_atomic_t sig_alrm
= 0;
51 static sig_atomic_t sig_ign
= 0;
57 fprintf(stderr
, "Usage: %s [--signal sig | -s sig] [--preserve-status]"
58 " [--kill-after time | -k time] [--foreground] <duration> <command>"
59 " <arg ...>\n", getprogname());
65 parse_duration(const char *duration
)
70 ret
= strtod(duration
, &end
);
71 if (ret
== 0 && end
== duration
)
72 errx(125, "invalid duration");
74 if (end
== NULL
|| *end
== '\0')
77 if (end
!= NULL
&& *(end
+ 1) != '\0')
78 errx(EX_USAGE
, "invalid duration");
93 errx(125, "invalid duration");
96 if (ret
< 0 || ret
>= 100000000UL)
97 errx(125, "invalid duration");
103 parse_signal(const char *str
)
108 sig
= strtonum(str
, 1, sys_nsig
- 1, &errstr
);
112 if (strncasecmp(str
, "SIG", 3) == 0)
115 for (i
= 1; i
< sys_nsig
; i
++) {
116 if (strcasecmp(str
, sys_signame
[i
]) == 0)
120 errx(125, "invalid signal");
124 sig_handler(int signo
)
126 if (sig_ign
!= 0 && signo
== sig_ign
) {
149 set_interval(double iv
)
151 struct itimerval tim
;
153 memset(&tim
, 0, sizeof(tim
));
154 tim
.it_value
.tv_sec
= (time_t)iv
;
156 tim
.it_value
.tv_usec
= (suseconds_t
)(iv
* 1000000UL);
158 if (setitimer(ITIMER_REAL
, &tim
, NULL
) == -1)
159 err(EX_OSERR
, "setitimer()");
163 main(int argc
, char **argv
)
167 int foreground
, preserve
;
168 int error
, pstat
, status
;
169 int killsig
= SIGTERM
;
173 bool timedout
= false;
174 bool do_second_kill
= false;
175 bool child_done
= false;
176 struct sigaction signals
;
177 struct procctl_reaper_status info
;
178 struct procctl_reaper_kill killemall
;
189 foreground
= preserve
= 0;
192 const struct option longopts
[] = {
193 { "preserve-status", no_argument
, &preserve
, 1 },
194 { "foreground", no_argument
, &foreground
, 1 },
195 { "kill-after", required_argument
, NULL
, 'k'},
196 { "signal", required_argument
, NULL
, 's'},
197 { "help", no_argument
, NULL
, 'h'},
201 while ((ch
= getopt_long(argc
, argv
, "+k:s:h", longopts
, NULL
)) != -1) {
204 do_second_kill
= true;
205 second_kill
= parse_duration(optarg
);
208 killsig
= parse_signal(optarg
);
225 first_kill
= parse_duration(argv
[0]);
230 /* Acquire a reaper */
231 if (procctl(P_PID
, getpid(), PROC_REAP_ACQUIRE
, NULL
) == -1)
232 err(EX_OSERR
, "Fail to acquire the reaper");
235 memset(&signals
, 0, sizeof(signals
));
236 sigemptyset(&signals
.sa_mask
);
238 if (killsig
!= SIGKILL
&& killsig
!= SIGSTOP
)
239 signums
[0] = killsig
;
241 for (i
= 0; i
< sizeof(signums
) / sizeof(signums
[0]); i
++)
242 sigaddset(&signals
.sa_mask
, signums
[i
]);
244 signals
.sa_handler
= sig_handler
;
245 signals
.sa_flags
= SA_RESTART
;
247 for (i
= 0; i
< sizeof(signums
) / sizeof(signums
[0]); i
++)
248 if (signums
[i
] != -1 && signums
[i
] != 0 &&
249 sigaction(signums
[i
], &signals
, NULL
) == -1)
250 err(EX_OSERR
, "sigaction()");
252 signal(SIGTTIN
, SIG_IGN
);
253 signal(SIGTTOU
, SIG_IGN
);
257 err(EX_OSERR
, "fork()");
260 signal(SIGTTIN
, SIG_DFL
);
261 signal(SIGTTOU
, SIG_DFL
);
263 error
= execvp(argv
[0], argv
);
266 err(127, "exec(%s)", argv
[0]);
268 err(126, "exec(%s)", argv
[0]);
272 if (sigprocmask(SIG_BLOCK
, &signals
.sa_mask
, NULL
) == -1)
273 err(EX_OSERR
, "sigprocmask()");
275 /* parent continues here */
276 set_interval(first_kill
);
279 sigemptyset(&signals
.sa_mask
);
280 sigsuspend(&signals
.sa_mask
);
285 while ((cpid
= waitpid(-1, &status
, WNOHANG
)) != 0) {
291 } else if (cpid
== pid
) {
300 procctl(P_PID
, getpid(),
301 PROC_REAP_STATUS
, &info
);
302 if (info
.rs_children
== 0)
306 } else if (sig_alrm
) {
311 killemall
.rk_sig
= killsig
;
312 killemall
.rk_flags
= 0;
313 procctl(P_PID
, getpid(), PROC_REAP_KILL
,
318 if (do_second_kill
) {
319 set_interval(second_kill
);
326 } else if (sig_term
) {
328 killemall
.rk_sig
= sig_term
;
329 killemall
.rk_flags
= 0;
330 procctl(P_PID
, getpid(), PROC_REAP_KILL
,
335 if (do_second_kill
) {
336 set_interval(second_kill
);
345 while (!child_done
&& wait(&pstat
) == -1) {
347 err(EX_OSERR
, "waitpid()");
351 procctl(P_PID
, getpid(), PROC_REAP_RELEASE
, NULL
);
353 if (WEXITSTATUS(pstat
))
354 pstat
= WEXITSTATUS(pstat
);
355 else if(WIFSIGNALED(pstat
))
356 pstat
= 128 + WTERMSIG(pstat
);
358 if (timedout
&& !preserve
)
359 pstat
= EXIT_TIMEOUT
;