1 /*- 2 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org> 3 * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org> 4 * Copyright (c) 2025 Aaron LI <aly@aaronly.me> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/fcntl.h> 30 #include <sys/procctl.h> 31 #include <sys/resource.h> 32 #include <sys/time.h> 33 #include <sys/wait.h> 34 35 #include <err.h> 36 #include <errno.h> 37 #include <getopt.h> 38 #include <signal.h> 39 #include <stdarg.h> 40 #include <stdbool.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 #define EXIT_TIMEOUT 124 47 #define EXIT_INVALID 125 48 #define EXIT_CMD_ERROR 126 49 #define EXIT_CMD_NOENT 127 50 51 static volatile sig_atomic_t sig_chld = 0; 52 static volatile sig_atomic_t sig_alrm = 0; 53 static volatile sig_atomic_t sig_term = 0; /* signal to terminate children */ 54 static volatile sig_atomic_t sig_other = 0; /* signal to propagate */ 55 static int killsig = SIGTERM; /* signal to kill children */ 56 static const char *command = NULL; 57 static bool verbose = false; 58 59 static void __dead2 60 usage(void) 61 { 62 fprintf(stderr, 63 "Usage: %s [-f | --foreground] [-k time | --kill-after time]" 64 " [-p | --preserve-status] [-s signal | --signal signal] " 65 " [-v | --verbose] <duration> <command> [arg ...]\n", 66 getprogname()); 67 exit(EXIT_FAILURE); 68 } 69 70 static void 71 logv(const char *fmt, ...) 72 { 73 va_list ap; 74 75 if (!verbose) 76 return; 77 78 va_start(ap, fmt); 79 vwarnx(fmt, ap); 80 va_end(ap); 81 } 82 83 static double 84 parse_duration(const char *duration) 85 { 86 double ret; 87 char *suffix; 88 89 ret = strtod(duration, &suffix); 90 if (suffix == duration) 91 errx(EXIT_INVALID, "duration is not a number"); 92 93 if (*suffix == '\0') 94 return (ret); 95 96 if (suffix[1] != '\0') 97 errx(EXIT_INVALID, "duration unit suffix too long"); 98 99 switch (*suffix) { 100 case 's': 101 break; 102 case 'm': 103 ret *= 60; 104 break; 105 case 'h': 106 ret *= 60 * 60; 107 break; 108 case 'd': 109 ret *= 60 * 60 * 24; 110 break; 111 default: 112 errx(EXIT_INVALID, "duration unit suffix invalid"); 113 } 114 115 if (ret < 0 || ret >= 100000000UL) 116 errx(EXIT_INVALID, "duration out of range"); 117 118 return (ret); 119 } 120 121 static int 122 parse_signal(const char *str) 123 { 124 int sig, i; 125 const char *errstr; 126 127 sig = strtonum(str, 1, sys_nsig - 1, &errstr); 128 if (errstr == NULL) 129 return (sig); 130 131 if (strncasecmp(str, "SIG", 3) == 0) 132 str += 3; 133 for (i = 1; i < sys_nsig; i++) { 134 if (strcasecmp(str, sys_signame[i]) == 0) 135 return (i); 136 } 137 138 errx(EXIT_INVALID, "invalid signal"); 139 } 140 141 static void 142 sig_handler(int signo) 143 { 144 if (signo == killsig) { 145 sig_term = signo; 146 return; 147 } 148 149 switch (signo) { 150 case SIGCHLD: 151 sig_chld = 1; 152 break; 153 case SIGALRM: 154 sig_alrm = 1; 155 break; 156 case SIGHUP: 157 case SIGINT: 158 case SIGQUIT: 159 case SIGILL: 160 case SIGTRAP: 161 case SIGABRT: 162 case SIGEMT: 163 case SIGFPE: 164 case SIGBUS: 165 case SIGSEGV: 166 case SIGSYS: 167 case SIGPIPE: 168 case SIGTERM: 169 case SIGXCPU: 170 case SIGXFSZ: 171 case SIGVTALRM: 172 case SIGPROF: 173 case SIGUSR1: 174 case SIGUSR2: 175 /* 176 * Signals with default action to terminate the process. 177 * See the sigaction(2) man page. 178 */ 179 sig_term = signo; 180 break; 181 default: 182 sig_other = signo; 183 break; 184 } 185 } 186 187 static void 188 send_sig(pid_t pid, int signo, bool foreground) 189 { 190 struct procctl_reaper_kill rk; 191 int error; 192 193 logv("sending signal %s(%d) to command '%s'", 194 sys_signame[signo], signo, command); 195 if (foreground) { 196 if (kill(pid, signo) == -1) { 197 if (errno != ESRCH) 198 warn("kill(%d, %s)", (int)pid, 199 sys_signame[signo]); 200 } 201 } else { 202 memset(&rk, 0, sizeof(rk)); 203 rk.rk_sig = signo; 204 error = procctl(P_PID, getpid(), PROC_REAP_KILL, &rk); 205 if (error == 0 || (error == -1 && errno == ESRCH)) 206 ; 207 else if (error == -1) { 208 warn("procctl(PROC_REAP_KILL)"); 209 if (rk.rk_fpid > 0) 210 warnx( 211 "failed to signal some processes: first pid=%d", 212 (int)rk.rk_fpid); 213 } 214 logv("signaled %u processes", rk.rk_killed); 215 } 216 217 /* 218 * If the child process was stopped by a signal, POSIX.1-2024 219 * requires to send a SIGCONT signal. However, the standard also 220 * allows to send a SIGCONT regardless of the stop state, as we 221 * are doing here. 222 */ 223 if (signo != SIGKILL && signo != SIGSTOP && signo != SIGCONT) { 224 logv("sending signal %s(%d) to command '%s'", 225 sys_signame[SIGCONT], SIGCONT, command); 226 if (foreground) { 227 kill(pid, SIGCONT); 228 } else { 229 memset(&rk, 0, sizeof(rk)); 230 rk.rk_sig = SIGCONT; 231 procctl(P_PID, getpid(), PROC_REAP_KILL, &rk); 232 } 233 } 234 } 235 236 static void 237 set_interval(double iv) 238 { 239 struct itimerval tim; 240 241 memset(&tim, 0, sizeof(tim)); 242 if (iv > 0) { 243 tim.it_value.tv_sec = (time_t)iv; 244 iv -= (double)(time_t)iv; 245 tim.it_value.tv_usec = (suseconds_t)(iv * 1000000UL); 246 } 247 248 if (setitimer(ITIMER_REAL, &tim, NULL) == -1) 249 err(EXIT_FAILURE, "setitimer()"); 250 } 251 252 /* 253 * In order to avoid any possible ambiguity that a shell may not set '$?' to 254 * '128+signal_number', POSIX.1-2024 requires that timeout mimic the wait 255 * status of the child process by terminating itself with the same signal, 256 * while disabling core generation. 257 */ 258 static void __dead2 259 kill_self(int signo) 260 { 261 sigset_t mask; 262 struct rlimit rl; 263 264 /* Reset the signal disposition and make sure it's unblocked. */ 265 signal(signo, SIG_DFL); 266 sigfillset(&mask); 267 sigdelset(&mask, signo); 268 sigprocmask(SIG_SETMASK, &mask, NULL); 269 270 /* Disable core generation. */ 271 memset(&rl, 0, sizeof(rl)); 272 setrlimit(RLIMIT_CORE, &rl); 273 274 logv("killing self with signal %s(%d)", sys_signame[signo], signo); 275 kill(getpid(), signo); 276 err(128 + signo, "signal %s(%d) failed to kill self", 277 sys_signame[signo], signo); 278 } 279 280 int 281 main(int argc, char **argv) 282 { 283 int ch, status, sig; 284 int pstat = 0; 285 pid_t pid, cpid; 286 int pp[2], error; 287 char c; 288 double first_kill; 289 double second_kill = 0; 290 bool foreground = false; 291 bool preserve = false; 292 bool timedout = false; 293 bool do_second_kill = false; 294 bool child_done = false; 295 sigset_t zeromask, allmask, oldmask; 296 struct sigaction sa; 297 struct procctl_reaper_status info; 298 299 const char optstr[] = "+fhk:ps:v"; 300 const struct option longopts[] = { 301 { "foreground", no_argument, NULL, 'f' }, 302 { "help", no_argument, NULL, 'h' }, 303 { "kill-after", required_argument, NULL, 'k' }, 304 { "preserve-status", no_argument, NULL, 'p' }, 305 { "signal", required_argument, NULL, 's' }, 306 { "verbose", no_argument, NULL, 'v' }, 307 { NULL, 0, NULL, 0 }, 308 }; 309 310 while ((ch = getopt_long(argc, argv, optstr, longopts, NULL)) != -1) { 311 switch (ch) { 312 case 'f': 313 foreground = true; 314 break; 315 case 'k': 316 do_second_kill = true; 317 second_kill = parse_duration(optarg); 318 break; 319 case 'p': 320 preserve = true; 321 break; 322 case 's': 323 killsig = parse_signal(optarg); 324 break; 325 case 'v': 326 verbose = true; 327 break; 328 case 0: 329 break; 330 default: 331 usage(); 332 } 333 } 334 335 argc -= optind; 336 argv += optind; 337 if (argc < 2) 338 usage(); 339 340 first_kill = parse_duration(argv[0]); 341 argc--; 342 argv++; 343 command = argv[0]; 344 345 if (!foreground) { 346 /* Acquire a reaper */ 347 if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == -1) 348 err(EXIT_FAILURE, "procctl(PROC_REAP_ACQUIRE)"); 349 } 350 351 /* Block all signals to avoid racing against the child. */ 352 sigfillset(&allmask); 353 if (sigprocmask(SIG_BLOCK, &allmask, &oldmask) == -1) 354 err(EXIT_FAILURE, "sigprocmask()"); 355 356 if (pipe2(pp, O_CLOEXEC) == -1) 357 err(EXIT_FAILURE, "pipe2"); 358 359 pid = fork(); 360 if (pid == -1) { 361 err(EXIT_FAILURE, "fork()"); 362 } else if (pid == 0) { 363 /* 364 * child process 365 * 366 * POSIX.1-2024 requires that the child process inherit the 367 * same signal dispositions as the timeout(1) utility 368 * inherited, except for the signal to be sent upon timeout. 369 */ 370 signal(killsig, SIG_DFL); 371 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 372 err(EXIT_FAILURE, "sigprocmask(oldmask)"); 373 374 error = read(pp[0], &c, 1); 375 if (error == -1) 376 err(EXIT_FAILURE, "read from control pipe"); 377 if (error == 0) 378 errx(EXIT_FAILURE, "eof from control pipe"); 379 execvp(argv[0], argv); 380 warn("exec(%s)", argv[0]); 381 _exit(errno == ENOENT ? EXIT_CMD_NOENT : EXIT_CMD_ERROR); 382 } 383 384 /* parent continues here */ 385 386 /* Catch all signals in order to propagate them. */ 387 memset(&sa, 0, sizeof(sa)); 388 sigfillset(&sa.sa_mask); 389 sa.sa_handler = sig_handler; 390 sa.sa_flags = SA_RESTART; 391 for (sig = 1; sig < sys_nsig; sig++) { 392 if (sig == SIGKILL || sig == SIGSTOP || sig == SIGCONT || 393 sig == SIGTTIN || sig == SIGTTOU) 394 continue; 395 if (sigaction(sig, &sa, NULL) == -1) 396 err(EXIT_FAILURE, "sigaction(%d)", sig); 397 } 398 399 /* Don't stop if background child needs TTY */ 400 signal(SIGTTIN, SIG_IGN); 401 signal(SIGTTOU, SIG_IGN); 402 403 set_interval(first_kill); 404 error = write(pp[1], "a", 1); 405 if (error == -1) 406 err(EXIT_FAILURE, "write to control pipe"); 407 if (error == 0) 408 errx(EXIT_FAILURE, "short write to control pipe"); 409 sigemptyset(&zeromask); 410 411 for (;;) { 412 sigsuspend(&zeromask); 413 414 if (sig_chld) { 415 sig_chld = 0; 416 417 while ((cpid = waitpid(-1, &status, WNOHANG)) != 0) { 418 if (cpid < 0) { 419 if (errno != EINTR) 420 break; 421 } else if (cpid == pid) { 422 pstat = status; 423 child_done = true; 424 logv("child terminated: pid=%d, " 425 "exit=%d, signal=%d", 426 (int)pid, WEXITSTATUS(status), 427 WTERMSIG(status)); 428 } else { 429 /* 430 * Collect grandchildren zombies. 431 * Only effective if we're a reaper. 432 */ 433 logv("collected zombie: pid=%d, " 434 "exit=%d, signal=%d", 435 (int)cpid, WEXITSTATUS(status), 436 WTERMSIG(status)); 437 } 438 } 439 if (child_done) { 440 if (foreground) { 441 break; 442 } else { 443 procctl(P_PID, getpid(), 444 PROC_REAP_STATUS, &info); 445 if (info.rs_children == 0) 446 break; 447 } 448 } 449 } else if (sig_alrm || sig_term) { 450 if (sig_alrm) { 451 sig = killsig; 452 sig_alrm = 0; 453 timedout = true; 454 logv("time limit reached or received SIGALRM"); 455 } else { 456 sig = sig_term; 457 sig_term = 0; 458 logv("received terminating signal %s(%d)", 459 sys_signame[sig], sig); 460 } 461 462 send_sig(pid, sig, foreground); 463 464 if (do_second_kill) { 465 set_interval(second_kill); 466 do_second_kill = false; 467 killsig = SIGKILL; 468 } 469 470 } else if (sig_other) { 471 /* Propagate any other signals. */ 472 sig = sig_other; 473 sig_other = 0; 474 logv("received signal %s(%d)", sys_signame[sig], sig); 475 476 send_sig(pid, sig, foreground); 477 } 478 } 479 480 if (!foreground) 481 procctl(P_PID, getpid(), PROC_REAP_RELEASE, NULL); 482 483 if (timedout && !preserve) { 484 pstat = EXIT_TIMEOUT; 485 } else { 486 if (WIFSIGNALED(pstat)) 487 kill_self(WTERMSIG(pstat)); 488 /* NOTREACHED */ 489 490 if (WIFEXITED(pstat)) 491 pstat = WEXITSTATUS(pstat); 492 } 493 494 return (pstat); 495 } 496