1e7ab1336SMateusz Piotrowski /*-
2e7ab1336SMateusz Piotrowski * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
3e7ab1336SMateusz Piotrowski * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
4e7ab1336SMateusz Piotrowski * All rights reserved.
5e7ab1336SMateusz Piotrowski *
6e7ab1336SMateusz Piotrowski * Redistribution and use in source and binary forms, with or without
7e7ab1336SMateusz Piotrowski * modification, are permitted provided that the following conditions
8e7ab1336SMateusz Piotrowski * are met:
9e7ab1336SMateusz Piotrowski * 1. Redistributions of source code must retain the above copyright
10e7ab1336SMateusz Piotrowski * notice, this list of conditions and the following disclaimer
11e7ab1336SMateusz Piotrowski * in this position and unchanged.
12e7ab1336SMateusz Piotrowski * 2. Redistributions in binary form must reproduce the above copyright
13e7ab1336SMateusz Piotrowski * notice, this list of conditions and the following disclaimer in the
14e7ab1336SMateusz Piotrowski * documentation and/or other materials provided with the distribution.
15e7ab1336SMateusz Piotrowski *
16e7ab1336SMateusz Piotrowski * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17e7ab1336SMateusz Piotrowski * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18e7ab1336SMateusz Piotrowski * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19e7ab1336SMateusz Piotrowski * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20e7ab1336SMateusz Piotrowski * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21e7ab1336SMateusz Piotrowski * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22e7ab1336SMateusz Piotrowski * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23e7ab1336SMateusz Piotrowski * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24e7ab1336SMateusz Piotrowski * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25e7ab1336SMateusz Piotrowski * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26e7ab1336SMateusz Piotrowski */
27e7ab1336SMateusz Piotrowski
28e7ab1336SMateusz Piotrowski #include <sys/cdefs.h>
29e7ab1336SMateusz Piotrowski #include <sys/procctl.h>
30e7ab1336SMateusz Piotrowski #include <sys/time.h>
31e7ab1336SMateusz Piotrowski #include <sys/wait.h>
32e7ab1336SMateusz Piotrowski
33e7ab1336SMateusz Piotrowski #include <err.h>
34e7ab1336SMateusz Piotrowski #include <errno.h>
35e7ab1336SMateusz Piotrowski #include <getopt.h>
36e7ab1336SMateusz Piotrowski #include <signal.h>
37e7ab1336SMateusz Piotrowski #include <stdbool.h>
38e7ab1336SMateusz Piotrowski #include <stdio.h>
39e7ab1336SMateusz Piotrowski #include <stdlib.h>
40e7ab1336SMateusz Piotrowski #include <string.h>
41e7ab1336SMateusz Piotrowski #include <unistd.h>
42e7ab1336SMateusz Piotrowski
43e7ab1336SMateusz Piotrowski #define EXIT_TIMEOUT 124
4406690044SGordon Bergling #define EXIT_INVALID 125
4506690044SGordon Bergling #define EXIT_CMD_ERROR 126
4606690044SGordon Bergling #define EXIT_CMD_NOENT 127
47e7ab1336SMateusz Piotrowski
48e7ab1336SMateusz Piotrowski static sig_atomic_t sig_chld = 0;
49e7ab1336SMateusz Piotrowski static sig_atomic_t sig_term = 0;
50e7ab1336SMateusz Piotrowski static sig_atomic_t sig_alrm = 0;
51e7ab1336SMateusz Piotrowski static sig_atomic_t sig_ign = 0;
52*d633a7d1SGordon Bergling static const char *command = NULL;
53*d633a7d1SGordon Bergling static bool verbose = false;
54e7ab1336SMateusz Piotrowski
55e7ab1336SMateusz Piotrowski static void
usage(void)56e7ab1336SMateusz Piotrowski usage(void)
57e7ab1336SMateusz Piotrowski {
58e7ab1336SMateusz Piotrowski
5906690044SGordon Bergling fprintf(stderr, "Usage: %s [-k time | --kill-after time]"
60*d633a7d1SGordon Bergling " [-s sig | --signal sig] [-v | --verbose] [--foreground]"
61*d633a7d1SGordon Bergling " [--preserve-status] <duration> <command> <arg ...>\n",
62*d633a7d1SGordon Bergling getprogname());
63e7ab1336SMateusz Piotrowski
64e7ab1336SMateusz Piotrowski exit(EXIT_FAILURE);
65e7ab1336SMateusz Piotrowski }
66e7ab1336SMateusz Piotrowski
67e7ab1336SMateusz Piotrowski static double
parse_duration(const char * duration)68e7ab1336SMateusz Piotrowski parse_duration(const char *duration)
69e7ab1336SMateusz Piotrowski {
70e7ab1336SMateusz Piotrowski double ret;
71e7ab1336SMateusz Piotrowski char *end;
72e7ab1336SMateusz Piotrowski
73e7ab1336SMateusz Piotrowski ret = strtod(duration, &end);
74e7ab1336SMateusz Piotrowski if (ret == 0 && end == duration)
7506690044SGordon Bergling errx(EXIT_INVALID, "invalid duration");
76e7ab1336SMateusz Piotrowski
77e7ab1336SMateusz Piotrowski if (end == NULL || *end == '\0')
78e7ab1336SMateusz Piotrowski return (ret);
79e7ab1336SMateusz Piotrowski
80e7ab1336SMateusz Piotrowski if (end != NULL && *(end + 1) != '\0')
8106690044SGordon Bergling errx(EXIT_INVALID, "invalid duration");
82e7ab1336SMateusz Piotrowski
83e7ab1336SMateusz Piotrowski switch (*end) {
84e7ab1336SMateusz Piotrowski case 's':
85e7ab1336SMateusz Piotrowski break;
86e7ab1336SMateusz Piotrowski case 'm':
87e7ab1336SMateusz Piotrowski ret *= 60;
88e7ab1336SMateusz Piotrowski break;
89e7ab1336SMateusz Piotrowski case 'h':
90e7ab1336SMateusz Piotrowski ret *= 60 * 60;
91e7ab1336SMateusz Piotrowski break;
92e7ab1336SMateusz Piotrowski case 'd':
93e7ab1336SMateusz Piotrowski ret *= 60 * 60 * 24;
94e7ab1336SMateusz Piotrowski break;
95e7ab1336SMateusz Piotrowski default:
9606690044SGordon Bergling errx(EXIT_INVALID, "invalid duration");
97e7ab1336SMateusz Piotrowski }
98e7ab1336SMateusz Piotrowski
99e7ab1336SMateusz Piotrowski if (ret < 0 || ret >= 100000000UL)
10006690044SGordon Bergling errx(EXIT_INVALID, "invalid duration");
101e7ab1336SMateusz Piotrowski
102e7ab1336SMateusz Piotrowski return (ret);
103e7ab1336SMateusz Piotrowski }
104e7ab1336SMateusz Piotrowski
105e7ab1336SMateusz Piotrowski static int
parse_signal(const char * str)106e7ab1336SMateusz Piotrowski parse_signal(const char *str)
107e7ab1336SMateusz Piotrowski {
108e7ab1336SMateusz Piotrowski int sig, i;
109e7ab1336SMateusz Piotrowski const char *errstr;
110e7ab1336SMateusz Piotrowski
111e7ab1336SMateusz Piotrowski sig = strtonum(str, 1, sys_nsig - 1, &errstr);
112e7ab1336SMateusz Piotrowski
113e7ab1336SMateusz Piotrowski if (errstr == NULL)
114e7ab1336SMateusz Piotrowski return (sig);
11506690044SGordon Bergling
116e7ab1336SMateusz Piotrowski if (strncasecmp(str, "SIG", 3) == 0)
117e7ab1336SMateusz Piotrowski str += 3;
118e7ab1336SMateusz Piotrowski
119e7ab1336SMateusz Piotrowski for (i = 1; i < sys_nsig; i++) {
120e7ab1336SMateusz Piotrowski if (strcasecmp(str, sys_signame[i]) == 0)
121e7ab1336SMateusz Piotrowski return (i);
122e7ab1336SMateusz Piotrowski }
123e7ab1336SMateusz Piotrowski
12406690044SGordon Bergling errx(EXIT_INVALID, "invalid signal");
125e7ab1336SMateusz Piotrowski }
126e7ab1336SMateusz Piotrowski
127e7ab1336SMateusz Piotrowski static void
sig_handler(int signo)128e7ab1336SMateusz Piotrowski sig_handler(int signo)
129e7ab1336SMateusz Piotrowski {
130e7ab1336SMateusz Piotrowski if (sig_ign != 0 && signo == sig_ign) {
131e7ab1336SMateusz Piotrowski sig_ign = 0;
132e7ab1336SMateusz Piotrowski return;
133e7ab1336SMateusz Piotrowski }
134e7ab1336SMateusz Piotrowski
135e7ab1336SMateusz Piotrowski switch (signo) {
136e7ab1336SMateusz Piotrowski case 0:
137e7ab1336SMateusz Piotrowski case SIGINT:
138e7ab1336SMateusz Piotrowski case SIGHUP:
139e7ab1336SMateusz Piotrowski case SIGQUIT:
140e7ab1336SMateusz Piotrowski case SIGTERM:
141e7ab1336SMateusz Piotrowski sig_term = signo;
142e7ab1336SMateusz Piotrowski break;
143e7ab1336SMateusz Piotrowski case SIGCHLD:
144e7ab1336SMateusz Piotrowski sig_chld = 1;
145e7ab1336SMateusz Piotrowski break;
146e7ab1336SMateusz Piotrowski case SIGALRM:
147e7ab1336SMateusz Piotrowski sig_alrm = 1;
148e7ab1336SMateusz Piotrowski break;
149e7ab1336SMateusz Piotrowski }
150e7ab1336SMateusz Piotrowski }
151e7ab1336SMateusz Piotrowski
152e7ab1336SMateusz Piotrowski static void
send_sig(pid_t pid,int signo)153*d633a7d1SGordon Bergling send_sig(pid_t pid, int signo)
154*d633a7d1SGordon Bergling {
155*d633a7d1SGordon Bergling if (verbose) {
156*d633a7d1SGordon Bergling warnx("sending signal %s(%d) to command '%s'",
157*d633a7d1SGordon Bergling sys_signame[signo], signo, command);
158*d633a7d1SGordon Bergling }
159*d633a7d1SGordon Bergling kill(pid, signo);
160*d633a7d1SGordon Bergling }
161*d633a7d1SGordon Bergling
162*d633a7d1SGordon Bergling static void
set_interval(double iv)163e7ab1336SMateusz Piotrowski set_interval(double iv)
164e7ab1336SMateusz Piotrowski {
165e7ab1336SMateusz Piotrowski struct itimerval tim;
166e7ab1336SMateusz Piotrowski
167e7ab1336SMateusz Piotrowski memset(&tim, 0, sizeof(tim));
168e7ab1336SMateusz Piotrowski tim.it_value.tv_sec = (time_t)iv;
16906690044SGordon Bergling iv -= (double)tim.it_value.tv_sec;
170e7ab1336SMateusz Piotrowski tim.it_value.tv_usec = (suseconds_t)(iv * 1000000UL);
171e7ab1336SMateusz Piotrowski
172e7ab1336SMateusz Piotrowski if (setitimer(ITIMER_REAL, &tim, NULL) == -1)
173e7ab1336SMateusz Piotrowski err(EXIT_FAILURE, "setitimer()");
174e7ab1336SMateusz Piotrowski }
175e7ab1336SMateusz Piotrowski
176e7ab1336SMateusz Piotrowski int
main(int argc,char ** argv)177e7ab1336SMateusz Piotrowski main(int argc, char **argv)
178e7ab1336SMateusz Piotrowski {
179e7ab1336SMateusz Piotrowski int ch;
180e7ab1336SMateusz Piotrowski int foreground, preserve;
181e7ab1336SMateusz Piotrowski int error, pstat, status;
182e7ab1336SMateusz Piotrowski int killsig = SIGTERM;
18306690044SGordon Bergling size_t i;
184e7ab1336SMateusz Piotrowski pid_t pid, cpid;
185e7ab1336SMateusz Piotrowski double first_kill;
186e7ab1336SMateusz Piotrowski double second_kill;
187e7ab1336SMateusz Piotrowski bool timedout = false;
188e7ab1336SMateusz Piotrowski bool do_second_kill = false;
189e7ab1336SMateusz Piotrowski bool child_done = false;
190e7ab1336SMateusz Piotrowski struct sigaction signals;
191e7ab1336SMateusz Piotrowski struct procctl_reaper_status info;
192e7ab1336SMateusz Piotrowski struct procctl_reaper_kill killemall;
193e7ab1336SMateusz Piotrowski int signums[] = {
194e7ab1336SMateusz Piotrowski -1,
195e7ab1336SMateusz Piotrowski SIGTERM,
196e7ab1336SMateusz Piotrowski SIGINT,
197e7ab1336SMateusz Piotrowski SIGHUP,
198e7ab1336SMateusz Piotrowski SIGCHLD,
199e7ab1336SMateusz Piotrowski SIGALRM,
200e7ab1336SMateusz Piotrowski SIGQUIT,
201e7ab1336SMateusz Piotrowski };
202e7ab1336SMateusz Piotrowski
203e7ab1336SMateusz Piotrowski foreground = preserve = 0;
204e7ab1336SMateusz Piotrowski second_kill = 0;
205e7ab1336SMateusz Piotrowski
206e7ab1336SMateusz Piotrowski const struct option longopts[] = {
207e7ab1336SMateusz Piotrowski { "preserve-status", no_argument, &preserve, 1 },
208e7ab1336SMateusz Piotrowski { "foreground", no_argument, &foreground, 1 },
209e7ab1336SMateusz Piotrowski { "kill-after", required_argument, NULL, 'k'},
210e7ab1336SMateusz Piotrowski { "signal", required_argument, NULL, 's'},
211e7ab1336SMateusz Piotrowski { "help", no_argument, NULL, 'h'},
212*d633a7d1SGordon Bergling { "verbose", no_argument, NULL, 'v'},
213e7ab1336SMateusz Piotrowski { NULL, 0, NULL, 0 }
214e7ab1336SMateusz Piotrowski };
215e7ab1336SMateusz Piotrowski
216*d633a7d1SGordon Bergling while ((ch = getopt_long(argc, argv, "+k:s:vh", longopts, NULL)) != -1) {
217e7ab1336SMateusz Piotrowski switch (ch) {
218e7ab1336SMateusz Piotrowski case 'k':
219e7ab1336SMateusz Piotrowski do_second_kill = true;
220e7ab1336SMateusz Piotrowski second_kill = parse_duration(optarg);
221e7ab1336SMateusz Piotrowski break;
222e7ab1336SMateusz Piotrowski case 's':
223e7ab1336SMateusz Piotrowski killsig = parse_signal(optarg);
224e7ab1336SMateusz Piotrowski break;
225*d633a7d1SGordon Bergling case 'v':
226*d633a7d1SGordon Bergling verbose = true;
227*d633a7d1SGordon Bergling break;
228e7ab1336SMateusz Piotrowski case 0:
229e7ab1336SMateusz Piotrowski break;
230e7ab1336SMateusz Piotrowski case 'h':
231e7ab1336SMateusz Piotrowski default:
232e7ab1336SMateusz Piotrowski usage();
233e7ab1336SMateusz Piotrowski }
234e7ab1336SMateusz Piotrowski }
235e7ab1336SMateusz Piotrowski
236e7ab1336SMateusz Piotrowski argc -= optind;
237e7ab1336SMateusz Piotrowski argv += optind;
238e7ab1336SMateusz Piotrowski
239e7ab1336SMateusz Piotrowski if (argc < 2)
240e7ab1336SMateusz Piotrowski usage();
241e7ab1336SMateusz Piotrowski
242e7ab1336SMateusz Piotrowski first_kill = parse_duration(argv[0]);
243e7ab1336SMateusz Piotrowski argc--;
244e7ab1336SMateusz Piotrowski argv++;
245*d633a7d1SGordon Bergling command = argv[0];
246e7ab1336SMateusz Piotrowski
247e7ab1336SMateusz Piotrowski if (!foreground) {
248e7ab1336SMateusz Piotrowski /* Acquire a reaper */
249e7ab1336SMateusz Piotrowski if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == -1)
250e7ab1336SMateusz Piotrowski err(EXIT_FAILURE, "Fail to acquire the reaper");
251e7ab1336SMateusz Piotrowski }
252e7ab1336SMateusz Piotrowski
253e7ab1336SMateusz Piotrowski memset(&signals, 0, sizeof(signals));
254e7ab1336SMateusz Piotrowski sigemptyset(&signals.sa_mask);
255e7ab1336SMateusz Piotrowski
256e7ab1336SMateusz Piotrowski if (killsig != SIGKILL && killsig != SIGSTOP)
257e7ab1336SMateusz Piotrowski signums[0] = killsig;
258e7ab1336SMateusz Piotrowski
259e7ab1336SMateusz Piotrowski for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++)
260e7ab1336SMateusz Piotrowski sigaddset(&signals.sa_mask, signums[i]);
261e7ab1336SMateusz Piotrowski
262e7ab1336SMateusz Piotrowski signals.sa_handler = sig_handler;
263e7ab1336SMateusz Piotrowski signals.sa_flags = SA_RESTART;
264e7ab1336SMateusz Piotrowski
26506690044SGordon Bergling for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++) {
266e7ab1336SMateusz Piotrowski if (signums[i] != -1 && signums[i] != 0 &&
267e7ab1336SMateusz Piotrowski sigaction(signums[i], &signals, NULL) == -1)
268e7ab1336SMateusz Piotrowski err(EXIT_FAILURE, "sigaction()");
26906690044SGordon Bergling }
270e7ab1336SMateusz Piotrowski
27106690044SGordon Bergling /* Don't stop if background child needs TTY */
272e7ab1336SMateusz Piotrowski signal(SIGTTIN, SIG_IGN);
273e7ab1336SMateusz Piotrowski signal(SIGTTOU, SIG_IGN);
274e7ab1336SMateusz Piotrowski
275e7ab1336SMateusz Piotrowski pid = fork();
276e7ab1336SMateusz Piotrowski if (pid == -1)
277e7ab1336SMateusz Piotrowski err(EXIT_FAILURE, "fork()");
278e7ab1336SMateusz Piotrowski else if (pid == 0) {
279e7ab1336SMateusz Piotrowski /* child process */
280e7ab1336SMateusz Piotrowski signal(SIGTTIN, SIG_DFL);
281e7ab1336SMateusz Piotrowski signal(SIGTTOU, SIG_DFL);
282e7ab1336SMateusz Piotrowski
283e7ab1336SMateusz Piotrowski error = execvp(argv[0], argv);
284e7ab1336SMateusz Piotrowski if (error == -1) {
285e7ab1336SMateusz Piotrowski if (errno == ENOENT)
28606690044SGordon Bergling err(EXIT_CMD_NOENT, "exec(%s)", argv[0]);
287e7ab1336SMateusz Piotrowski else
28806690044SGordon Bergling err(EXIT_CMD_ERROR, "exec(%s)", argv[0]);
289e7ab1336SMateusz Piotrowski }
290e7ab1336SMateusz Piotrowski }
291e7ab1336SMateusz Piotrowski
292e7ab1336SMateusz Piotrowski if (sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL) == -1)
293e7ab1336SMateusz Piotrowski err(EXIT_FAILURE, "sigprocmask()");
294e7ab1336SMateusz Piotrowski
295e7ab1336SMateusz Piotrowski /* parent continues here */
296e7ab1336SMateusz Piotrowski set_interval(first_kill);
297e7ab1336SMateusz Piotrowski
298e7ab1336SMateusz Piotrowski for (;;) {
299e7ab1336SMateusz Piotrowski sigemptyset(&signals.sa_mask);
300e7ab1336SMateusz Piotrowski sigsuspend(&signals.sa_mask);
301e7ab1336SMateusz Piotrowski
302e7ab1336SMateusz Piotrowski if (sig_chld) {
303e7ab1336SMateusz Piotrowski sig_chld = 0;
304e7ab1336SMateusz Piotrowski
305e7ab1336SMateusz Piotrowski while ((cpid = waitpid(-1, &status, WNOHANG)) != 0) {
306e7ab1336SMateusz Piotrowski if (cpid < 0) {
307e7ab1336SMateusz Piotrowski if (errno == EINTR)
308e7ab1336SMateusz Piotrowski continue;
309e7ab1336SMateusz Piotrowski else
310e7ab1336SMateusz Piotrowski break;
311e7ab1336SMateusz Piotrowski } else if (cpid == pid) {
312e7ab1336SMateusz Piotrowski pstat = status;
313e7ab1336SMateusz Piotrowski child_done = true;
314e7ab1336SMateusz Piotrowski }
315e7ab1336SMateusz Piotrowski }
316e7ab1336SMateusz Piotrowski if (child_done) {
317e7ab1336SMateusz Piotrowski if (foreground) {
318e7ab1336SMateusz Piotrowski break;
319e7ab1336SMateusz Piotrowski } else {
320e7ab1336SMateusz Piotrowski procctl(P_PID, getpid(),
321e7ab1336SMateusz Piotrowski PROC_REAP_STATUS, &info);
322e7ab1336SMateusz Piotrowski if (info.rs_children == 0)
323e7ab1336SMateusz Piotrowski break;
324e7ab1336SMateusz Piotrowski }
325e7ab1336SMateusz Piotrowski }
326e7ab1336SMateusz Piotrowski } else if (sig_alrm) {
327e7ab1336SMateusz Piotrowski sig_alrm = 0;
328e7ab1336SMateusz Piotrowski
329e7ab1336SMateusz Piotrowski timedout = true;
330e7ab1336SMateusz Piotrowski if (!foreground) {
331e7ab1336SMateusz Piotrowski killemall.rk_sig = killsig;
332e7ab1336SMateusz Piotrowski killemall.rk_flags = 0;
333e7ab1336SMateusz Piotrowski procctl(P_PID, getpid(), PROC_REAP_KILL,
334e7ab1336SMateusz Piotrowski &killemall);
335e7ab1336SMateusz Piotrowski } else
336*d633a7d1SGordon Bergling send_sig(pid, killsig);
337e7ab1336SMateusz Piotrowski
338e7ab1336SMateusz Piotrowski if (do_second_kill) {
339e7ab1336SMateusz Piotrowski set_interval(second_kill);
34006690044SGordon Bergling do_second_kill = false;
341e7ab1336SMateusz Piotrowski sig_ign = killsig;
342e7ab1336SMateusz Piotrowski killsig = SIGKILL;
343e7ab1336SMateusz Piotrowski } else
344e7ab1336SMateusz Piotrowski break;
345e7ab1336SMateusz Piotrowski
346e7ab1336SMateusz Piotrowski } else if (sig_term) {
347e7ab1336SMateusz Piotrowski if (!foreground) {
348e7ab1336SMateusz Piotrowski killemall.rk_sig = sig_term;
349e7ab1336SMateusz Piotrowski killemall.rk_flags = 0;
350e7ab1336SMateusz Piotrowski procctl(P_PID, getpid(), PROC_REAP_KILL,
351e7ab1336SMateusz Piotrowski &killemall);
352e7ab1336SMateusz Piotrowski } else
353*d633a7d1SGordon Bergling send_sig(pid, sig_term);
354e7ab1336SMateusz Piotrowski
355e7ab1336SMateusz Piotrowski if (do_second_kill) {
356e7ab1336SMateusz Piotrowski set_interval(second_kill);
35706690044SGordon Bergling do_second_kill = false;
358e7ab1336SMateusz Piotrowski sig_ign = killsig;
359e7ab1336SMateusz Piotrowski killsig = SIGKILL;
360e7ab1336SMateusz Piotrowski } else
361e7ab1336SMateusz Piotrowski break;
362e7ab1336SMateusz Piotrowski }
363e7ab1336SMateusz Piotrowski }
364e7ab1336SMateusz Piotrowski
365e7ab1336SMateusz Piotrowski while (!child_done && wait(&pstat) == -1) {
366e7ab1336SMateusz Piotrowski if (errno != EINTR)
367e7ab1336SMateusz Piotrowski err(EXIT_FAILURE, "waitpid()");
368e7ab1336SMateusz Piotrowski }
369e7ab1336SMateusz Piotrowski
370e7ab1336SMateusz Piotrowski if (!foreground)
371e7ab1336SMateusz Piotrowski procctl(P_PID, getpid(), PROC_REAP_RELEASE, NULL);
372e7ab1336SMateusz Piotrowski
373e7ab1336SMateusz Piotrowski if (WEXITSTATUS(pstat))
374e7ab1336SMateusz Piotrowski pstat = WEXITSTATUS(pstat);
375e7ab1336SMateusz Piotrowski else if (WIFSIGNALED(pstat))
376e7ab1336SMateusz Piotrowski pstat = 128 + WTERMSIG(pstat);
377e7ab1336SMateusz Piotrowski
378e7ab1336SMateusz Piotrowski if (timedout && !preserve)
379e7ab1336SMateusz Piotrowski pstat = EXIT_TIMEOUT;
380e7ab1336SMateusz Piotrowski
381e7ab1336SMateusz Piotrowski return (pstat);
382e7ab1336SMateusz Piotrowski }
383