1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 #if 0 37 static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95"; 38 #endif 39 #endif /* not lint */ 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include <signal.h> 44 #include <unistd.h> 45 #include <stdlib.h> 46 47 #include "shell.h" 48 #include "main.h" 49 #include "nodes.h" /* for other headers */ 50 #include "eval.h" 51 #include "jobs.h" 52 #include "show.h" 53 #include "options.h" 54 #include "syntax.h" 55 #include "output.h" 56 #include "memalloc.h" 57 #include "error.h" 58 #include "trap.h" 59 #include "mystring.h" 60 #include "builtins.h" 61 #include "myhistedit.h" 62 63 64 /* 65 * Sigmode records the current value of the signal handlers for the various 66 * modes. A value of zero means that the current handler is not known. 67 * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 68 */ 69 70 #define S_DFL 1 /* default signal handling (SIG_DFL) */ 71 #define S_CATCH 2 /* signal is caught */ 72 #define S_IGN 3 /* signal is ignored (SIG_IGN) */ 73 #define S_HARD_IGN 4 /* signal is ignored permanently */ 74 #define S_RESET 5 /* temporary - to reset a hard ignored sig */ 75 76 77 static char sigmode[NSIG]; /* current value of signal */ 78 volatile sig_atomic_t pendingsig; /* indicates some signal received */ 79 volatile sig_atomic_t pendingsig_waitcmd; /* indicates wait builtin should be interrupted */ 80 static int in_dotrap; /* do we execute in a trap handler? */ 81 static char *volatile trap[NSIG]; /* trap handler commands */ 82 static volatile sig_atomic_t gotsig[NSIG]; 83 /* indicates specified signal received */ 84 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */ 85 static int last_trapsig; 86 87 static int exiting; /* exitshell() has been called */ 88 static int exiting_exitstatus; /* value passed to exitshell() */ 89 90 static int getsigaction(int, sig_t *); 91 92 93 /* 94 * Map a string to a signal number. 95 * 96 * Note: the signal number may exceed NSIG. 97 */ 98 static int 99 sigstring_to_signum(char *sig) 100 { 101 102 if (is_number(sig)) { 103 int signo; 104 105 signo = atoi(sig); 106 return ((signo >= 0 && signo < NSIG) ? signo : (-1)); 107 } else if (strcasecmp(sig, "EXIT") == 0) { 108 return (0); 109 } else { 110 int n; 111 112 if (strncasecmp(sig, "SIG", 3) == 0) 113 sig += 3; 114 for (n = 1; n < sys_nsig; n++) 115 if (sys_signame[n] && 116 strcasecmp(sys_signame[n], sig) == 0) 117 return (n); 118 } 119 return (-1); 120 } 121 122 123 /* 124 * Print a list of valid signal names. 125 */ 126 static void 127 printsignals(void) 128 { 129 int n, outlen; 130 131 outlen = 0; 132 for (n = 1; n < sys_nsig; n++) { 133 if (sys_signame[n]) { 134 out1fmt("%s", sys_signame[n]); 135 outlen += strlen(sys_signame[n]); 136 } else { 137 out1fmt("%d", n); 138 outlen += 3; /* good enough */ 139 } 140 ++outlen; 141 if (outlen > 71 || n == sys_nsig - 1) { 142 out1str("\n"); 143 outlen = 0; 144 } else { 145 out1c(' '); 146 } 147 } 148 } 149 150 151 /* 152 * The trap builtin. 153 */ 154 int 155 trapcmd(int argc __unused, char **argv) 156 { 157 char *action; 158 int signo; 159 int errors = 0; 160 int i; 161 162 while ((i = nextopt("l")) != '\0') { 163 switch (i) { 164 case 'l': 165 printsignals(); 166 return (0); 167 } 168 } 169 argv = argptr; 170 171 if (*argv == NULL) { 172 for (signo = 0 ; signo < sys_nsig ; signo++) { 173 if (signo < NSIG && trap[signo] != NULL) { 174 out1str("trap -- "); 175 out1qstr(trap[signo]); 176 if (signo == 0) { 177 out1str(" EXIT\n"); 178 } else if (sys_signame[signo]) { 179 out1fmt(" %s\n", sys_signame[signo]); 180 } else { 181 out1fmt(" %d\n", signo); 182 } 183 } 184 } 185 return 0; 186 } 187 action = NULL; 188 if (*argv && !is_number(*argv)) { 189 if (strcmp(*argv, "-") == 0) 190 argv++; 191 else { 192 action = *argv; 193 argv++; 194 } 195 } 196 for (; *argv; argv++) { 197 if ((signo = sigstring_to_signum(*argv)) == -1) { 198 warning("bad signal %s", *argv); 199 errors = 1; 200 continue; 201 } 202 INTOFF; 203 if (action) 204 action = savestr(action); 205 if (trap[signo]) 206 ckfree(trap[signo]); 207 trap[signo] = action; 208 if (signo != 0) 209 setsignal(signo); 210 INTON; 211 } 212 return errors; 213 } 214 215 216 /* 217 * Clear traps on a fork. 218 */ 219 void 220 clear_traps(void) 221 { 222 char *volatile *tp; 223 224 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 225 if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 226 INTOFF; 227 ckfree(*tp); 228 *tp = NULL; 229 if (tp != &trap[0]) 230 setsignal(tp - trap); 231 INTON; 232 } 233 } 234 } 235 236 237 /* 238 * Check if we have any traps enabled. 239 */ 240 int 241 have_traps(void) 242 { 243 char *volatile *tp; 244 245 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 246 if (*tp && **tp) /* trap not NULL or SIG_IGN */ 247 return 1; 248 } 249 return 0; 250 } 251 252 /* 253 * Set the signal handler for the specified signal. The routine figures 254 * out what it should be set to. 255 */ 256 void 257 setsignal(int signo) 258 { 259 int action; 260 sig_t sigact = SIG_DFL; 261 struct sigaction sa; 262 char *t; 263 264 if ((t = trap[signo]) == NULL) 265 action = S_DFL; 266 else if (*t != '\0') 267 action = S_CATCH; 268 else 269 action = S_IGN; 270 if (action == S_DFL) { 271 switch (signo) { 272 case SIGINT: 273 action = S_CATCH; 274 break; 275 case SIGQUIT: 276 #ifdef DEBUG 277 { 278 extern int debug; 279 280 if (debug) 281 break; 282 } 283 #endif 284 action = S_CATCH; 285 break; 286 case SIGTERM: 287 if (rootshell && iflag) 288 action = S_IGN; 289 break; 290 #if JOBS 291 case SIGTSTP: 292 case SIGTTOU: 293 if (rootshell && mflag) 294 action = S_IGN; 295 break; 296 #endif 297 } 298 } 299 300 t = &sigmode[signo]; 301 if (*t == 0) { 302 /* 303 * current setting unknown 304 */ 305 if (!getsigaction(signo, &sigact)) { 306 /* 307 * Pretend it worked; maybe we should give a warning 308 * here, but other shells don't. We don't alter 309 * sigmode, so that we retry every time. 310 */ 311 return; 312 } 313 if (sigact == SIG_IGN) { 314 if (mflag && (signo == SIGTSTP || 315 signo == SIGTTIN || signo == SIGTTOU)) { 316 *t = S_IGN; /* don't hard ignore these */ 317 } else 318 *t = S_HARD_IGN; 319 } else { 320 *t = S_RESET; /* force to be set */ 321 } 322 } 323 if (*t == S_HARD_IGN || *t == action) 324 return; 325 switch (action) { 326 case S_DFL: sigact = SIG_DFL; break; 327 case S_CATCH: sigact = onsig; break; 328 case S_IGN: sigact = SIG_IGN; break; 329 } 330 *t = action; 331 sa.sa_handler = sigact; 332 sa.sa_flags = 0; 333 sigemptyset(&sa.sa_mask); 334 sigaction(signo, &sa, NULL); 335 } 336 337 338 /* 339 * Return the current setting for sig w/o changing it. 340 */ 341 static int 342 getsigaction(int signo, sig_t *sigact) 343 { 344 struct sigaction sa; 345 346 if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 347 return 0; 348 *sigact = (sig_t) sa.sa_handler; 349 return 1; 350 } 351 352 353 /* 354 * Ignore a signal. 355 */ 356 void 357 ignoresig(int signo) 358 { 359 360 if (sigmode[signo] == 0) 361 setsignal(signo); 362 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { 363 signal(signo, SIG_IGN); 364 sigmode[signo] = S_IGN; 365 } 366 } 367 368 369 int 370 issigchldtrapped(void) 371 { 372 373 return (trap[SIGCHLD] != NULL && *trap[SIGCHLD] != '\0'); 374 } 375 376 377 /* 378 * Signal handler. 379 */ 380 void 381 onsig(int signo) 382 { 383 384 if (signo == SIGINT && trap[SIGINT] == NULL) { 385 if (suppressint) 386 SET_PENDING_INT; 387 else 388 onint(); 389 return; 390 } 391 392 /* If we are currently in a wait builtin, prepare to break it */ 393 if (signo == SIGINT || signo == SIGQUIT) 394 pendingsig_waitcmd = signo; 395 396 if (trap[signo] != NULL && trap[signo][0] != '\0' && 397 (signo != SIGCHLD || !ignore_sigchld)) { 398 gotsig[signo] = 1; 399 pendingsig = signo; 400 pendingsig_waitcmd = signo; 401 } 402 } 403 404 405 /* 406 * Called to execute a trap. Perhaps we should avoid entering new trap 407 * handlers while we are executing a trap handler. 408 */ 409 void 410 dotrap(void) 411 { 412 struct stackmark smark; 413 int i; 414 int savestatus, prev_evalskip, prev_skipcount; 415 416 in_dotrap++; 417 for (;;) { 418 pendingsig = 0; 419 pendingsig_waitcmd = 0; 420 for (i = 1; i < NSIG; i++) { 421 if (gotsig[i]) { 422 gotsig[i] = 0; 423 if (trap[i]) { 424 /* 425 * Ignore SIGCHLD to avoid infinite 426 * recursion if the trap action does 427 * a fork. 428 */ 429 if (i == SIGCHLD) 430 ignore_sigchld++; 431 432 /* 433 * Backup current evalskip 434 * state and reset it before 435 * executing a trap, so that the 436 * trap is not disturbed by an 437 * ongoing break/continue/return 438 * statement. 439 */ 440 prev_evalskip = evalskip; 441 prev_skipcount = skipcount; 442 evalskip = 0; 443 444 last_trapsig = i; 445 savestatus = exitstatus; 446 setstackmark(&smark); 447 evalstring(stsavestr(trap[i]), 0); 448 popstackmark(&smark); 449 450 /* 451 * If such a command was not 452 * already in progress, allow a 453 * break/continue/return in the 454 * trap action to have an effect 455 * outside of it. 456 */ 457 if (evalskip == 0 || 458 prev_evalskip != 0) { 459 evalskip = prev_evalskip; 460 skipcount = prev_skipcount; 461 exitstatus = savestatus; 462 } 463 464 if (i == SIGCHLD) 465 ignore_sigchld--; 466 } 467 break; 468 } 469 } 470 if (i >= NSIG) 471 break; 472 } 473 in_dotrap--; 474 } 475 476 477 /* 478 * Controls whether the shell is interactive or not based on iflag. 479 */ 480 void 481 setinteractive(void) 482 { 483 setsignal(SIGINT); 484 setsignal(SIGQUIT); 485 setsignal(SIGTERM); 486 } 487 488 489 /* 490 * Called to exit the shell. 491 */ 492 void 493 exitshell(int status) 494 { 495 TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 496 exiting = 1; 497 exiting_exitstatus = status; 498 exitshell_savedstatus(); 499 } 500 501 void 502 exitshell_savedstatus(void) 503 { 504 struct jmploc loc1, loc2; 505 char *p; 506 int sig = 0; 507 sigset_t sigs; 508 509 if (!exiting) { 510 if (in_dotrap && last_trapsig) { 511 sig = last_trapsig; 512 exiting_exitstatus = sig + 128; 513 } else 514 exiting_exitstatus = oexitstatus; 515 } 516 exitstatus = oexitstatus = exiting_exitstatus; 517 if (!setjmp(loc1.loc)) { 518 handler = &loc1; 519 if ((p = trap[0]) != NULL && *p != '\0') { 520 /* 521 * Reset evalskip, or the trap on EXIT could be 522 * interrupted if the last command was a "return". 523 */ 524 evalskip = 0; 525 trap[0] = NULL; 526 FORCEINTON; 527 evalstring(p, 0); 528 } 529 } 530 if (!setjmp(loc2.loc)) { 531 handler = &loc2; /* probably unnecessary */ 532 FORCEINTON; 533 flushall(); 534 #if JOBS 535 setjobctl(0); 536 #endif 537 } 538 if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN && 539 sig != SIGTTOU) { 540 signal(sig, SIG_DFL); 541 sigemptyset(&sigs); 542 sigaddset(&sigs, sig); 543 sigprocmask(SIG_UNBLOCK, &sigs, NULL); 544 kill(getpid(), sig); 545 /* If the default action is to ignore, fall back to _exit(). */ 546 } 547 _exit(exiting_exitstatus); 548 } 549