1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)trap.c 8.5 (Berkeley) 6/5/95 37 * $FreeBSD: src/bin/sh/trap.c,v 1.20.2.2 2002/08/27 01:36:28 tjr Exp $ 38 * $DragonFly: src/bin/sh/trap.c,v 1.2 2003/06/17 04:22:50 dillon Exp $ 39 */ 40 41 #include <signal.h> 42 #include <unistd.h> 43 #include <stdlib.h> 44 45 #include "shell.h" 46 #include "main.h" 47 #include "nodes.h" /* for other headers */ 48 #include "eval.h" 49 #include "jobs.h" 50 #include "show.h" 51 #include "options.h" 52 #include "syntax.h" 53 #include "output.h" 54 #include "memalloc.h" 55 #include "error.h" 56 #include "trap.h" 57 #include "mystring.h" 58 #include "myhistedit.h" 59 60 61 /* 62 * Sigmode records the current value of the signal handlers for the various 63 * modes. A value of zero means that the current handler is not known. 64 * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 65 */ 66 67 #define S_DFL 1 /* default signal handling (SIG_DFL) */ 68 #define S_CATCH 2 /* signal is caught */ 69 #define S_IGN 3 /* signal is ignored (SIG_IGN) */ 70 #define S_HARD_IGN 4 /* signal is ignored permanently */ 71 #define S_RESET 5 /* temporary - to reset a hard ignored sig */ 72 73 74 MKINIT char sigmode[NSIG]; /* current value of signal */ 75 int pendingsigs; /* indicates some signal received */ 76 int in_dotrap; /* do we execute in a trap handler? */ 77 static char *volatile trap[NSIG]; /* trap handler commands */ 78 static volatile sig_atomic_t gotsig[NSIG]; 79 /* indicates specified signal received */ 80 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */ 81 volatile sig_atomic_t gotwinch; 82 83 static int getsigaction(int, sig_t *); 84 85 86 /* 87 * Map a string to a signal number. 88 */ 89 static int 90 sigstring_to_signum(char *sig) 91 { 92 93 if (is_number(sig)) { 94 int signo; 95 96 signo = atoi(sig); 97 return ((signo >= 0 && signo < NSIG) ? signo : (-1)); 98 } else if (strcasecmp(sig, "exit") == 0) { 99 return (0); 100 } else { 101 int n; 102 103 if (strncasecmp(sig, "sig", 3) == 0) 104 sig += 3; 105 for (n = 1; n < NSIG; n++) 106 if (strcasecmp(sys_signame[n], sig) == 0) 107 return (n); 108 } 109 return (-1); 110 } 111 112 113 /* 114 * Print a list of valid signal names. 115 */ 116 static void 117 printsignals(void) 118 { 119 int n; 120 121 for (n = 1; n < NSIG; n++) { 122 out1fmt("%s", sys_signame[n]); 123 if (n == (NSIG / 2) || n == (NSIG - 1)) 124 out1str("\n"); 125 else 126 out1c(' '); 127 } 128 } 129 130 131 /* 132 * The trap builtin. 133 */ 134 int 135 trapcmd(int argc, char **argv) 136 { 137 char *action; 138 int signo; 139 140 if (argc <= 1) { 141 for (signo = 0 ; signo < NSIG ; signo++) { 142 if (trap[signo] != NULL) 143 out1fmt("trap -- '%s' %s\n", trap[signo], 144 (signo) ? sys_signame[signo] : "exit"); 145 } 146 return 0; 147 } 148 action = NULL; 149 if (*++argv && strcmp(*argv, "--") == 0) 150 argv++; 151 if (*argv && sigstring_to_signum(*argv) == -1) { 152 if ((*argv)[0] != '-') { 153 action = *argv; 154 argv++; 155 } else if ((*argv)[1] == '\0') { 156 argv++; 157 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') { 158 printsignals(); 159 return 0; 160 } else { 161 error("bad option %s", *argv); 162 } 163 } 164 while (*argv) { 165 if ((signo = sigstring_to_signum(*argv)) == -1) 166 error("bad signal %s", *argv); 167 INTOFF; 168 if (action) 169 action = savestr(action); 170 if (trap[signo]) 171 ckfree(trap[signo]); 172 trap[signo] = action; 173 if (signo != 0) 174 setsignal(signo); 175 INTON; 176 argv++; 177 } 178 return 0; 179 } 180 181 182 /* 183 * Clear traps on a fork. 184 */ 185 void 186 clear_traps(void) 187 { 188 char *volatile *tp; 189 190 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 191 if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 192 INTOFF; 193 ckfree(*tp); 194 *tp = NULL; 195 if (tp != &trap[0]) 196 setsignal(tp - trap); 197 INTON; 198 } 199 } 200 } 201 202 203 /* 204 * Set the signal handler for the specified signal. The routine figures 205 * out what it should be set to. 206 */ 207 void 208 setsignal(int signo) 209 { 210 int action; 211 sig_t sig, sigact = SIG_DFL; 212 char *t; 213 214 if ((t = trap[signo]) == NULL) 215 action = S_DFL; 216 else if (*t != '\0') 217 action = S_CATCH; 218 else 219 action = S_IGN; 220 if (action == S_DFL) { 221 switch (signo) { 222 case SIGINT: 223 action = S_CATCH; 224 break; 225 case SIGQUIT: 226 #ifdef DEBUG 227 { 228 extern int debug; 229 230 if (debug) 231 break; 232 } 233 #endif 234 action = S_CATCH; 235 break; 236 case SIGTERM: 237 if (rootshell && iflag) 238 action = S_IGN; 239 break; 240 #if JOBS 241 case SIGTSTP: 242 case SIGTTOU: 243 if (rootshell && mflag) 244 action = S_IGN; 245 break; 246 #endif 247 #ifndef NO_HISTORY 248 case SIGWINCH: 249 if (rootshell && iflag) 250 action = S_CATCH; 251 break; 252 #endif 253 } 254 } 255 256 t = &sigmode[signo]; 257 if (*t == 0) { 258 /* 259 * current setting unknown 260 */ 261 if (!getsigaction(signo, &sigact)) { 262 /* 263 * Pretend it worked; maybe we should give a warning 264 * here, but other shells don't. We don't alter 265 * sigmode, so that we retry every time. 266 */ 267 return; 268 } 269 if (sigact == SIG_IGN) { 270 if (mflag && (signo == SIGTSTP || 271 signo == SIGTTIN || signo == SIGTTOU)) { 272 *t = S_IGN; /* don't hard ignore these */ 273 } else 274 *t = S_HARD_IGN; 275 } else { 276 *t = S_RESET; /* force to be set */ 277 } 278 } 279 if (*t == S_HARD_IGN || *t == action) 280 return; 281 switch (action) { 282 case S_DFL: sigact = SIG_DFL; break; 283 case S_CATCH: sigact = onsig; break; 284 case S_IGN: sigact = SIG_IGN; break; 285 } 286 *t = action; 287 sig = signal(signo, sigact); 288 if (sig != SIG_ERR && action == S_CATCH) 289 siginterrupt(signo, 1); 290 } 291 292 293 /* 294 * Return the current setting for sig w/o changing it. 295 */ 296 static int 297 getsigaction(int signo, sig_t *sigact) 298 { 299 struct sigaction sa; 300 301 if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 302 return 0; 303 *sigact = (sig_t) sa.sa_handler; 304 return 1; 305 } 306 307 308 /* 309 * Ignore a signal. 310 */ 311 void 312 ignoresig(int signo) 313 { 314 315 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { 316 signal(signo, SIG_IGN); 317 } 318 sigmode[signo] = S_HARD_IGN; 319 } 320 321 322 #ifdef mkinit 323 INCLUDE <signal.h> 324 INCLUDE "trap.h" 325 326 SHELLPROC { 327 char *sm; 328 329 clear_traps(); 330 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) { 331 if (*sm == S_IGN) 332 *sm = S_HARD_IGN; 333 } 334 } 335 #endif 336 337 338 /* 339 * Signal handler. 340 */ 341 void 342 onsig(int signo) 343 { 344 345 if (signo == SIGINT && trap[SIGINT] == NULL) { 346 onint(); 347 return; 348 } 349 350 if (signo != SIGCHLD || !ignore_sigchld) 351 gotsig[signo] = 1; 352 pendingsigs++; 353 354 /* If we are currently in a wait builtin, prepare to break it */ 355 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0) 356 breakwaitcmd = 1; 357 /* 358 * If a trap is set, not ignored and not the null command, we need 359 * to make sure traps are executed even when a child blocks signals. 360 */ 361 if (Tflag && 362 trap[signo] != NULL && 363 ! trap[signo][0] == '\0' && 364 ! (trap[signo][0] == ':' && trap[signo][1] == '\0')) 365 breakwaitcmd = 1; 366 367 #ifndef NO_HISTORY 368 if (signo == SIGWINCH) 369 gotwinch = 1; 370 #endif 371 } 372 373 374 /* 375 * Called to execute a trap. Perhaps we should avoid entering new trap 376 * handlers while we are executing a trap handler. 377 */ 378 void 379 dotrap(void) 380 { 381 int i; 382 int savestatus; 383 384 in_dotrap++; 385 for (;;) { 386 for (i = 1; i < NSIG; i++) { 387 if (gotsig[i]) { 388 gotsig[i] = 0; 389 if (trap[i]) { 390 /* 391 * Ignore SIGCHLD to avoid infinite recursion 392 * if the trap action does a fork. 393 */ 394 if (i == SIGCHLD) 395 ignore_sigchld++; 396 savestatus = exitstatus; 397 evalstring(trap[i]); 398 exitstatus = savestatus; 399 if (i == SIGCHLD) 400 ignore_sigchld--; 401 } 402 break; 403 } 404 } 405 if (i >= NSIG) 406 break; 407 } 408 in_dotrap--; 409 pendingsigs = 0; 410 } 411 412 413 /* 414 * Controls whether the shell is interactive or not. 415 */ 416 void 417 setinteractive(int on) 418 { 419 static int is_interactive = -1; 420 421 if (on == is_interactive) 422 return; 423 setsignal(SIGINT); 424 setsignal(SIGQUIT); 425 setsignal(SIGTERM); 426 #ifndef NO_HISTORY 427 setsignal(SIGWINCH); 428 #endif 429 is_interactive = on; 430 } 431 432 433 /* 434 * Called to exit the shell. 435 */ 436 void 437 exitshell(int status) 438 { 439 struct jmploc loc1, loc2; 440 char *p; 441 442 TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 443 if (setjmp(loc1.loc)) { 444 goto l1; 445 } 446 if (setjmp(loc2.loc)) { 447 goto l2; 448 } 449 handler = &loc1; 450 if ((p = trap[0]) != NULL && *p != '\0') { 451 trap[0] = NULL; 452 evalstring(p); 453 } 454 l1: handler = &loc2; /* probably unnecessary */ 455 flushall(); 456 #if JOBS 457 setjobctl(0); 458 #endif 459 l2: _exit(status); 460 } 461