trap.c revision 153244
1254721Semaste/*- 2254721Semaste * Copyright (c) 1991, 1993 3254721Semaste * The Regents of the University of California. All rights reserved. 4254721Semaste * 5254721Semaste * This code is derived from software contributed to Berkeley by 6254721Semaste * Kenneth Almquist. 7254721Semaste * 8254721Semaste * Redistribution and use in source and binary forms, with or without 9254721Semaste * modification, are permitted provided that the following conditions 10254721Semaste * are met: 11254721Semaste * 1. Redistributions of source code must retain the above copyright 12254721Semaste * notice, this list of conditions and the following disclaimer. 13254721Semaste * 2. Redistributions in binary form must reproduce the above copyright 14254721Semaste * notice, this list of conditions and the following disclaimer in the 15254721Semaste * documentation and/or other materials provided with the distribution. 16254721Semaste * 4. Neither the name of the University nor the names of its contributors 17254721Semaste * may be used to endorse or promote products derived from this software 18254721Semaste * without specific prior written permission. 19254721Semaste * 20254721Semaste * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23254721Semaste * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30254721Semaste * SUCH DAMAGE. 31254721Semaste */ 32254721Semaste 33254721Semaste#ifndef lint 34254721Semaste#if 0 35254721Semastestatic char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95"; 36254721Semaste#endif 37254721Semaste#endif /* not lint */ 38254721Semaste#include <sys/cdefs.h> 39254721Semaste__FBSDID("$FreeBSD: head/bin/sh/trap.c 153244 2005-12-08 20:08:36Z stefanf $"); 40254721Semaste 41254721Semaste#include <signal.h> 42254721Semaste#include <unistd.h> 43254721Semaste#include <stdlib.h> 44254721Semaste 45254721Semaste#include "shell.h" 46254721Semaste#include "main.h" 47254721Semaste#include "nodes.h" /* for other headers */ 48254721Semaste#include "eval.h" 49254721Semaste#include "jobs.h" 50254721Semaste#include "show.h" 51254721Semaste#include "options.h" 52254721Semaste#include "syntax.h" 53254721Semaste#include "output.h" 54254721Semaste#include "memalloc.h" 55254721Semaste#include "error.h" 56254721Semaste#include "trap.h" 57254721Semaste#include "mystring.h" 58254721Semaste#include "myhistedit.h" 59254721Semaste 60254721Semaste 61254721Semaste/* 62254721Semaste * Sigmode records the current value of the signal handlers for the various 63254721Semaste * modes. A value of zero means that the current handler is not known. 64254721Semaste * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 65254721Semaste */ 66254721Semaste 67254721Semaste#define S_DFL 1 /* default signal handling (SIG_DFL) */ 68254721Semaste#define S_CATCH 2 /* signal is caught */ 69254721Semaste#define S_IGN 3 /* signal is ignored (SIG_IGN) */ 70254721Semaste#define S_HARD_IGN 4 /* signal is ignored permanently */ 71254721Semaste#define S_RESET 5 /* temporary - to reset a hard ignored sig */ 72254721Semaste 73254721Semaste 74254721SemasteMKINIT char sigmode[NSIG]; /* current value of signal */ 75254721Semasteint pendingsigs; /* indicates some signal received */ 76254721Semasteint in_dotrap; /* do we execute in a trap handler? */ 77254721Semastestatic char *volatile trap[NSIG]; /* trap handler commands */ 78254721Semastestatic volatile sig_atomic_t gotsig[NSIG]; 79254721Semaste /* indicates specified signal received */ 80254721Semastestatic int ignore_sigchld; /* Used while handling SIGCHLD traps. */ 81254721Semastevolatile sig_atomic_t gotwinch; 82254721Semaste 83254721Semastestatic int getsigaction(int, sig_t *); 84254721Semaste 85254721Semaste 86254721Semaste/* 87254721Semaste * Map a string to a signal number. 88254721Semaste * 89254721Semaste * Note: the signal number may exceed NSIG. 90254721Semaste */ 91254721Semastestatic int 92254721Semastesigstring_to_signum(char *sig) 93254721Semaste{ 94254721Semaste 95254721Semaste if (is_number(sig)) { 96254721Semaste int signo; 97254721Semaste 98254721Semaste signo = atoi(sig); 99254721Semaste return ((signo >= 0 && signo < NSIG) ? signo : (-1)); 100254721Semaste } else if (strcasecmp(sig, "exit") == 0) { 101254721Semaste return (0); 102254721Semaste } else { 103254721Semaste int n; 104254721Semaste 105254721Semaste if (strncasecmp(sig, "sig", 3) == 0) 106254721Semaste sig += 3; 107254721Semaste for (n = 1; n < sys_nsig; n++) 108254721Semaste if (sys_signame[n] && 109254721Semaste strcasecmp(sys_signame[n], sig) == 0) 110254721Semaste return (n); 111254721Semaste } 112254721Semaste return (-1); 113254721Semaste} 114254721Semaste 115254721Semaste 116254721Semaste/* 117254721Semaste * Print a list of valid signal names. 118254721Semaste */ 119254721Semastestatic void 120254721Semasteprintsignals(void) 121254721Semaste{ 122254721Semaste int n, outlen; 123254721Semaste 124254721Semaste outlen = 0; 125254721Semaste for (n = 1; n < sys_nsig; n++) { 126254721Semaste if (sys_signame[n]) { 127254721Semaste out1fmt("%s", sys_signame[n]); 128254721Semaste outlen += strlen(sys_signame[n]); 129254721Semaste } else { 130254721Semaste out1fmt("%d", n); 131254721Semaste outlen += 3; /* good enough */ 132254721Semaste } 133254721Semaste ++outlen; 134254721Semaste if (outlen > 70 || n == sys_nsig - 1) { 135254721Semaste out1str("\n"); 136254721Semaste outlen = 0; 137254721Semaste } else { 138254721Semaste out1c(' '); 139254721Semaste } 140254721Semaste } 141254721Semaste} 142254721Semaste 143254721Semaste 144254721Semaste/* 145254721Semaste * The trap builtin. 146254721Semaste */ 147254721Semasteint 148254721Semastetrapcmd(int argc, char **argv) 149254721Semaste{ 150254721Semaste char *action; 151254721Semaste int signo; 152254721Semaste 153254721Semaste if (argc <= 1) { 154254721Semaste for (signo = 0 ; signo < sys_nsig ; signo++) { 155254721Semaste if (signo < NSIG && trap[signo] != NULL) { 156254721Semaste out1str("trap -- "); 157254721Semaste out1qstr(trap[signo]); 158254721Semaste if (signo == 0) { 159254721Semaste out1str(" exit\n"); 160254721Semaste } else if (sys_signame[signo]) { 161254721Semaste out1fmt(" %s\n", sys_signame[signo]); 162254721Semaste } else { 163254721Semaste out1fmt(" %d\n", signo); 164254721Semaste } 165254721Semaste } 166254721Semaste } 167254721Semaste return 0; 168254721Semaste } 169254721Semaste action = NULL; 170254721Semaste if (*++argv && strcmp(*argv, "--") == 0) 171254721Semaste argv++; 172254721Semaste if (*argv && sigstring_to_signum(*argv) == -1) { 173254721Semaste if ((*argv)[0] != '-') { 174254721Semaste action = *argv; 175254721Semaste argv++; 176254721Semaste } else if ((*argv)[1] == '\0') { 177254721Semaste argv++; 178254721Semaste } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') { 179254721Semaste printsignals(); 180254721Semaste return 0; 181254721Semaste } else { 182254721Semaste error("bad option %s", *argv); 183254721Semaste } 184254721Semaste } 185254721Semaste while (*argv) { 186254721Semaste if ((signo = sigstring_to_signum(*argv)) == -1) 187254721Semaste error("bad signal %s", *argv); 188254721Semaste INTOFF; 189254721Semaste if (action) 190254721Semaste action = savestr(action); 191254721Semaste if (trap[signo]) 192254721Semaste ckfree(trap[signo]); 193254721Semaste trap[signo] = action; 194254721Semaste if (signo != 0) 195254721Semaste setsignal(signo); 196254721Semaste INTON; 197254721Semaste argv++; 198254721Semaste } 199254721Semaste return 0; 200254721Semaste} 201254721Semaste 202254721Semaste 203254721Semaste/* 204254721Semaste * Clear traps on a fork. 205254721Semaste */ 206254721Semastevoid 207254721Semasteclear_traps(void) 208254721Semaste{ 209254721Semaste char *volatile *tp; 210254721Semaste 211254721Semaste for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 212254721Semaste if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 213254721Semaste INTOFF; 214254721Semaste ckfree(*tp); 215254721Semaste *tp = NULL; 216254721Semaste if (tp != &trap[0]) 217254721Semaste setsignal(tp - trap); 218254721Semaste INTON; 219254721Semaste } 220254721Semaste } 221254721Semaste} 222254721Semaste 223254721Semaste 224254721Semaste/* 225254721Semaste * Set the signal handler for the specified signal. The routine figures 226254721Semaste * out what it should be set to. 227254721Semaste */ 228254721Semastevoid 229254721Semastesetsignal(int signo) 230254721Semaste{ 231254721Semaste int action; 232254721Semaste sig_t sig, sigact = SIG_DFL; 233254721Semaste char *t; 234254721Semaste 235254721Semaste if ((t = trap[signo]) == NULL) 236254721Semaste action = S_DFL; 237254721Semaste else if (*t != '\0') 238254721Semaste action = S_CATCH; 239254721Semaste else 240254721Semaste action = S_IGN; 241254721Semaste if (action == S_DFL) { 242254721Semaste switch (signo) { 243254721Semaste case SIGINT: 244254721Semaste action = S_CATCH; 245254721Semaste break; 246254721Semaste case SIGQUIT: 247254721Semaste#ifdef DEBUG 248254721Semaste { 249254721Semaste extern int debug; 250254721Semaste 251254721Semaste if (debug) 252254721Semaste break; 253254721Semaste } 254254721Semaste#endif 255254721Semaste action = S_CATCH; 256254721Semaste break; 257254721Semaste case SIGTERM: 258254721Semaste if (rootshell && iflag) 259254721Semaste action = S_IGN; 260254721Semaste break; 261254721Semaste#if JOBS 262254721Semaste case SIGTSTP: 263254721Semaste case SIGTTOU: 264254721Semaste if (rootshell && mflag) 265254721Semaste action = S_IGN; 266254721Semaste break; 267254721Semaste#endif 268254721Semaste#ifndef NO_HISTORY 269254721Semaste case SIGWINCH: 270254721Semaste if (rootshell && iflag) 271254721Semaste action = S_CATCH; 272254721Semaste break; 273254721Semaste#endif 274254721Semaste } 275254721Semaste } 276 277 t = &sigmode[signo]; 278 if (*t == 0) { 279 /* 280 * current setting unknown 281 */ 282 if (!getsigaction(signo, &sigact)) { 283 /* 284 * Pretend it worked; maybe we should give a warning 285 * here, but other shells don't. We don't alter 286 * sigmode, so that we retry every time. 287 */ 288 return; 289 } 290 if (sigact == SIG_IGN) { 291 if (mflag && (signo == SIGTSTP || 292 signo == SIGTTIN || signo == SIGTTOU)) { 293 *t = S_IGN; /* don't hard ignore these */ 294 } else 295 *t = S_HARD_IGN; 296 } else { 297 *t = S_RESET; /* force to be set */ 298 } 299 } 300 if (*t == S_HARD_IGN || *t == action) 301 return; 302 switch (action) { 303 case S_DFL: sigact = SIG_DFL; break; 304 case S_CATCH: sigact = onsig; break; 305 case S_IGN: sigact = SIG_IGN; break; 306 } 307 *t = action; 308 sig = signal(signo, sigact); 309 if (sig != SIG_ERR && action == S_CATCH) 310 siginterrupt(signo, 1); 311} 312 313 314/* 315 * Return the current setting for sig w/o changing it. 316 */ 317static int 318getsigaction(int signo, sig_t *sigact) 319{ 320 struct sigaction sa; 321 322 if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 323 return 0; 324 *sigact = (sig_t) sa.sa_handler; 325 return 1; 326} 327 328 329/* 330 * Ignore a signal. 331 */ 332void 333ignoresig(int signo) 334{ 335 336 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { 337 signal(signo, SIG_IGN); 338 } 339 sigmode[signo] = S_HARD_IGN; 340} 341 342 343#ifdef mkinit 344INCLUDE <signal.h> 345INCLUDE "trap.h" 346 347SHELLPROC { 348 char *sm; 349 350 clear_traps(); 351 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) { 352 if (*sm == S_IGN) 353 *sm = S_HARD_IGN; 354 } 355} 356#endif 357 358 359/* 360 * Signal handler. 361 */ 362void 363onsig(int signo) 364{ 365 366 if (signo == SIGINT && trap[SIGINT] == NULL) { 367 onint(); 368 return; 369 } 370 371 if (signo != SIGCHLD || !ignore_sigchld) 372 gotsig[signo] = 1; 373 pendingsigs++; 374 375 /* If we are currently in a wait builtin, prepare to break it */ 376 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0) 377 breakwaitcmd = 1; 378 /* 379 * If a trap is set, not ignored and not the null command, we need 380 * to make sure traps are executed even when a child blocks signals. 381 */ 382 if (Tflag && 383 trap[signo] != NULL && 384 ! (trap[signo][0] == '\0') && 385 ! (trap[signo][0] == ':' && trap[signo][1] == '\0')) 386 breakwaitcmd = 1; 387 388#ifndef NO_HISTORY 389 if (signo == SIGWINCH) 390 gotwinch = 1; 391#endif 392} 393 394 395/* 396 * Called to execute a trap. Perhaps we should avoid entering new trap 397 * handlers while we are executing a trap handler. 398 */ 399void 400dotrap(void) 401{ 402 int i; 403 int savestatus; 404 405 in_dotrap++; 406 for (;;) { 407 for (i = 1; i < NSIG; i++) { 408 if (gotsig[i]) { 409 gotsig[i] = 0; 410 if (trap[i]) { 411 /* 412 * Ignore SIGCHLD to avoid infinite 413 * recursion if the trap action does 414 * a fork. 415 */ 416 if (i == SIGCHLD) 417 ignore_sigchld++; 418 savestatus = exitstatus; 419 evalstring(trap[i]); 420 exitstatus = savestatus; 421 if (i == SIGCHLD) 422 ignore_sigchld--; 423 } 424 break; 425 } 426 } 427 if (i >= NSIG) 428 break; 429 } 430 in_dotrap--; 431 pendingsigs = 0; 432} 433 434 435/* 436 * Controls whether the shell is interactive or not. 437 */ 438void 439setinteractive(int on) 440{ 441 static int is_interactive = -1; 442 443 if (on == is_interactive) 444 return; 445 setsignal(SIGINT); 446 setsignal(SIGQUIT); 447 setsignal(SIGTERM); 448#ifndef NO_HISTORY 449 setsignal(SIGWINCH); 450#endif 451 is_interactive = on; 452} 453 454 455/* 456 * Called to exit the shell. 457 */ 458void 459exitshell(int status) 460{ 461 struct jmploc loc1, loc2; 462 char *p; 463 464 TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 465 if (setjmp(loc1.loc)) { 466 goto l1; 467 } 468 if (setjmp(loc2.loc)) { 469 goto l2; 470 } 471 handler = &loc1; 472 if ((p = trap[0]) != NULL && *p != '\0') { 473 trap[0] = NULL; 474 evalstring(p); 475 } 476l1: handler = &loc2; /* probably unnecessary */ 477 flushall(); 478#if JOBS 479 setjobctl(0); 480#endif 481l2: _exit(status); 482} 483