1 /*- 2 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org> 3 * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 15 * 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. 26 */ 27 28 #include <sys/cdefs.h> 29 #include <sys/procctl.h> 30 #include <sys/time.h> 31 #include <sys/wait.h> 32 33 #include <err.h> 34 #include <errno.h> 35 #include <getopt.h> 36 #include <signal.h> 37 #include <stdbool.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 #define EXIT_TIMEOUT 124 44 #define EXIT_INVALID 125 45 #define EXIT_CMD_ERROR 126 46 #define EXIT_CMD_NOENT 127 47 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; 52 53 static void 54 usage(void) 55 { 56 57 fprintf(stderr, "Usage: %s [-k time | --kill-after time]" 58 " [-s sig | --signal sig] [--foreground] [--preserve-status]" 59 " <duration> <command> <arg ...>\n", getprogname()); 60 61 exit(EXIT_FAILURE); 62 } 63 64 static double 65 parse_duration(const char *duration) 66 { 67 double ret; 68 char *end; 69 70 ret = strtod(duration, &end); 71 if (ret == 0 && end == duration) 72 errx(EXIT_INVALID, "invalid duration"); 73 74 if (end == NULL || *end == '\0') 75 return (ret); 76 77 if (end != NULL && *(end + 1) != '\0') 78 errx(EXIT_INVALID, "invalid duration"); 79 80 switch (*end) { 81 case 's': 82 break; 83 case 'm': 84 ret *= 60; 85 break; 86 case 'h': 87 ret *= 60 * 60; 88 break; 89 case 'd': 90 ret *= 60 * 60 * 24; 91 break; 92 default: 93 errx(EXIT_INVALID, "invalid duration"); 94 } 95 96 if (ret < 0 || ret >= 100000000UL) 97 errx(EXIT_INVALID, "invalid duration"); 98 99 return (ret); 100 } 101 102 static int 103 parse_signal(const char *str) 104 { 105 int sig, i; 106 const char *errstr; 107 108 sig = strtonum(str, 1, sys_nsig - 1, &errstr); 109 110 if (errstr == NULL) 111 return (sig); 112 113 if (strncasecmp(str, "SIG", 3) == 0) 114 str += 3; 115 116 for (i = 1; i < sys_nsig; i++) { 117 if (strcasecmp(str, sys_signame[i]) == 0) 118 return (i); 119 } 120 121 errx(EXIT_INVALID, "invalid signal"); 122 } 123 124 static void 125 sig_handler(int signo) 126 { 127 if (sig_ign != 0 && signo == sig_ign) { 128 sig_ign = 0; 129 return; 130 } 131 132 switch (signo) { 133 case 0: 134 case SIGINT: 135 case SIGHUP: 136 case SIGQUIT: 137 case SIGTERM: 138 sig_term = signo; 139 break; 140 case SIGCHLD: 141 sig_chld = 1; 142 break; 143 case SIGALRM: 144 sig_alrm = 1; 145 break; 146 } 147 } 148 149 static void 150 set_interval(double iv) 151 { 152 struct itimerval tim; 153 154 memset(&tim, 0, sizeof(tim)); 155 tim.it_value.tv_sec = (time_t)iv; 156 iv -= (double)tim.it_value.tv_sec; 157 tim.it_value.tv_usec = (suseconds_t)(iv * 1000000UL); 158 159 if (setitimer(ITIMER_REAL, &tim, NULL) == -1) 160 err(EXIT_FAILURE, "setitimer()"); 161 } 162 163 int 164 main(int argc, char **argv) 165 { 166 int ch; 167 int foreground, preserve; 168 int error, pstat, status; 169 int killsig = SIGTERM; 170 size_t i; 171 pid_t pid, cpid; 172 double first_kill; 173 double second_kill; 174 bool timedout = false; 175 bool do_second_kill = false; 176 bool child_done = false; 177 struct sigaction signals; 178 struct procctl_reaper_status info; 179 struct procctl_reaper_kill killemall; 180 int signums[] = { 181 -1, 182 SIGTERM, 183 SIGINT, 184 SIGHUP, 185 SIGCHLD, 186 SIGALRM, 187 SIGQUIT, 188 }; 189 190 foreground = preserve = 0; 191 second_kill = 0; 192 193 const struct option longopts[] = { 194 { "preserve-status", no_argument, &preserve, 1 }, 195 { "foreground", no_argument, &foreground, 1 }, 196 { "kill-after", required_argument, NULL, 'k'}, 197 { "signal", required_argument, NULL, 's'}, 198 { "help", no_argument, NULL, 'h'}, 199 { NULL, 0, NULL, 0 } 200 }; 201 202 while ((ch = getopt_long(argc, argv, "+k:s:h", longopts, NULL)) != -1) { 203 switch (ch) { 204 case 'k': 205 do_second_kill = true; 206 second_kill = parse_duration(optarg); 207 break; 208 case 's': 209 killsig = parse_signal(optarg); 210 break; 211 case 0: 212 break; 213 case 'h': 214 default: 215 usage(); 216 } 217 } 218 219 argc -= optind; 220 argv += optind; 221 222 if (argc < 2) 223 usage(); 224 225 first_kill = parse_duration(argv[0]); 226 argc--; 227 argv++; 228 229 if (!foreground) { 230 /* Acquire a reaper */ 231 if (procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == -1) 232 err(EXIT_FAILURE, "Fail to acquire the reaper"); 233 } 234 235 memset(&signals, 0, sizeof(signals)); 236 sigemptyset(&signals.sa_mask); 237 238 if (killsig != SIGKILL && killsig != SIGSTOP) 239 signums[0] = killsig; 240 241 for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++) 242 sigaddset(&signals.sa_mask, signums[i]); 243 244 signals.sa_handler = sig_handler; 245 signals.sa_flags = SA_RESTART; 246 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(EXIT_FAILURE, "sigaction()"); 251 } 252 253 /* Don't stop if background child needs TTY */ 254 signal(SIGTTIN, SIG_IGN); 255 signal(SIGTTOU, SIG_IGN); 256 257 pid = fork(); 258 if (pid == -1) 259 err(EXIT_FAILURE, "fork()"); 260 else if (pid == 0) { 261 /* child process */ 262 signal(SIGTTIN, SIG_DFL); 263 signal(SIGTTOU, SIG_DFL); 264 265 error = execvp(argv[0], argv); 266 if (error == -1) { 267 if (errno == ENOENT) 268 err(EXIT_CMD_NOENT, "exec(%s)", argv[0]); 269 else 270 err(EXIT_CMD_ERROR, "exec(%s)", argv[0]); 271 } 272 } 273 274 if (sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL) == -1) 275 err(EXIT_FAILURE, "sigprocmask()"); 276 277 /* parent continues here */ 278 set_interval(first_kill); 279 280 for (;;) { 281 sigemptyset(&signals.sa_mask); 282 sigsuspend(&signals.sa_mask); 283 284 if (sig_chld) { 285 sig_chld = 0; 286 287 while ((cpid = waitpid(-1, &status, WNOHANG)) != 0) { 288 if (cpid < 0) { 289 if (errno == EINTR) 290 continue; 291 else 292 break; 293 } else if (cpid == pid) { 294 pstat = status; 295 child_done = true; 296 } 297 } 298 if (child_done) { 299 if (foreground) { 300 break; 301 } else { 302 procctl(P_PID, getpid(), 303 PROC_REAP_STATUS, &info); 304 if (info.rs_children == 0) 305 break; 306 } 307 } 308 } else if (sig_alrm) { 309 sig_alrm = 0; 310 311 timedout = true; 312 if (!foreground) { 313 killemall.rk_sig = killsig; 314 killemall.rk_flags = 0; 315 procctl(P_PID, getpid(), PROC_REAP_KILL, 316 &killemall); 317 } else 318 kill(pid, killsig); 319 320 if (do_second_kill) { 321 set_interval(second_kill); 322 do_second_kill = false; 323 sig_ign = killsig; 324 killsig = SIGKILL; 325 } else 326 break; 327 328 } else if (sig_term) { 329 if (!foreground) { 330 killemall.rk_sig = sig_term; 331 killemall.rk_flags = 0; 332 procctl(P_PID, getpid(), PROC_REAP_KILL, 333 &killemall); 334 } else 335 kill(pid, sig_term); 336 337 if (do_second_kill) { 338 set_interval(second_kill); 339 do_second_kill = false; 340 sig_ign = killsig; 341 killsig = SIGKILL; 342 } else 343 break; 344 } 345 } 346 347 while (!child_done && wait(&pstat) == -1) { 348 if (errno != EINTR) 349 err(EXIT_FAILURE, "waitpid()"); 350 } 351 352 if (!foreground) 353 procctl(P_PID, getpid(), PROC_REAP_RELEASE, NULL); 354 355 if (WEXITSTATUS(pstat)) 356 pstat = WEXITSTATUS(pstat); 357 else if (WIFSIGNALED(pstat)) 358 pstat = 128 + WTERMSIG(pstat); 359 360 if (timedout && !preserve) 361 pstat = EXIT_TIMEOUT; 362 363 return (pstat); 364 } 365