2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)trap.c 8.5 (Berkeley) 6/5/95
37 * $FreeBSD: src/bin/sh/trap.c,v 1.45 2011/02/04 22:47:55 jilles Exp $
46 #include "nodes.h" /* for other headers */
57 #include "myhistedit.h"
61 * Sigmode records the current value of the signal handlers for the various
62 * modes. A value of zero means that the current handler is not known.
63 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
66 #define S_DFL 1 /* default signal handling (SIG_DFL) */
67 #define S_CATCH 2 /* signal is caught */
68 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
69 #define S_HARD_IGN 4 /* signal is ignored permanently */
70 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
73 MKINIT char sigmode[NSIG]; /* current value of signal */
74 int pendingsigs; /* indicates some signal received */
75 int in_dotrap; /* do we execute in a trap handler? */
76 static char *volatile trap[NSIG]; /* trap handler commands */
77 static volatile sig_atomic_t gotsig[NSIG];
78 /* indicates specified signal received */
79 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
80 volatile sig_atomic_t gotwinch;
81 static int last_trapsig;
83 static int exiting; /* exitshell() has been called */
84 static int exiting_exitstatus; /* value passed to exitshell() */
86 static int getsigaction(int, sig_t *);
90 * Map a string to a signal number.
92 * Note: the signal number may exceed NSIG.
95 sigstring_to_signum(char *sig)
102 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
103 } else if (strcasecmp(sig, "EXIT") == 0) {
108 if (strncasecmp(sig, "SIG", 3) == 0)
110 for (n = 1; n < sys_nsig; n++)
111 if (sys_signame[n] &&
112 strcasecmp(sys_signame[n], sig) == 0)
120 * Print a list of valid signal names.
128 for (n = 1; n < sys_nsig; n++) {
129 if (sys_signame[n]) {
130 out1fmt("%s", sys_signame[n]);
131 outlen += strlen(sys_signame[n]);
134 outlen += 3; /* good enough */
137 if (outlen > 71 || n == sys_nsig - 1) {
151 trapcmd(int argc __unused, char **argv)
158 while ((i = nextopt("l")) != '\0') {
168 for (signo = 0 ; signo < sys_nsig ; signo++) {
169 if (signo < NSIG && trap[signo] != NULL) {
171 out1qstr(trap[signo]);
174 } else if (sys_signame[signo]) {
175 out1fmt(" %s\n", sys_signame[signo]);
177 out1fmt(" %d\n", signo);
184 if (*argv && sigstring_to_signum(*argv) == -1) {
185 if (strcmp(*argv, "-") == 0)
193 if ((signo = sigstring_to_signum(*argv)) == -1) {
194 warning("bad signal %s", *argv);
199 action = savestr(action);
202 trap[signo] = action;
213 * Clear traps on a fork.
220 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
221 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
226 setsignal(tp - trap);
234 * Check if we have any traps enabled.
241 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
242 if (*tp && **tp) /* trap not NULL or SIG_IGN */
249 * Set the signal handler for the specified signal. The routine figures
250 * out what it should be set to.
256 sig_t sigact = SIG_DFL;
260 if ((t = trap[signo]) == NULL)
266 if (action == S_DFL) {
283 if (rootshell && iflag)
289 if (rootshell && mflag)
295 if (rootshell && iflag)
305 * current setting unknown
307 if (!getsigaction(signo, &sigact)) {
309 * Pretend it worked; maybe we should give a warning
310 * here, but other shells don't. We don't alter
311 * sigmode, so that we retry every time.
315 if (sigact == SIG_IGN) {
316 if (mflag && (signo == SIGTSTP ||
317 signo == SIGTTIN || signo == SIGTTOU)) {
318 *t = S_IGN; /* don't hard ignore these */
322 *t = S_RESET; /* force to be set */
325 if (*t == S_HARD_IGN || *t == action)
328 case S_DFL: sigact = SIG_DFL; break;
329 case S_CATCH: sigact = onsig; break;
330 case S_IGN: sigact = SIG_IGN; break;
333 sa.sa_handler = sigact;
335 sigemptyset(&sa.sa_mask);
336 sigaction(signo, &sa, NULL);
341 * Return the current setting for sig w/o changing it.
344 getsigaction(int signo, sig_t *sigact)
348 if (sigaction(signo, NULL, &sa) == -1)
350 *sigact = (sig_t) sa.sa_handler;
362 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
363 signal(signo, SIG_IGN);
365 sigmode[signo] = S_HARD_IGN;
376 if (signo == SIGINT && trap[SIGINT] == NULL) {
381 if (signo != SIGCHLD || !ignore_sigchld)
385 /* If we are currently in a wait builtin, prepare to break it */
386 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
389 * If a trap is set, not ignored and not the null command, we need
390 * to make sure traps are executed even when a child blocks signals.
393 trap[signo] != NULL &&
394 ! (trap[signo][0] == '\0') &&
395 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
399 if (signo == SIGWINCH)
406 * Called to execute a trap. Perhaps we should avoid entering new trap
407 * handlers while we are executing a trap handler.
417 for (i = 1; i < NSIG; i++) {
422 * Ignore SIGCHLD to avoid infinite
423 * recursion if the trap action does
429 savestatus = exitstatus;
430 evalstring(trap[i], 0);
431 exitstatus = savestatus;
447 * Controls whether the shell is interactive or not.
450 setinteractive(int on)
452 static int is_interactive = -1;
454 if (on == is_interactive)
467 * Called to exit the shell.
470 exitshell(int status)
472 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
474 exiting_exitstatus = status;
475 exitshell_savedstatus();
479 exitshell_savedstatus(void)
481 struct jmploc loc1, loc2;
483 volatile int sig = 0;
487 if (in_dotrap && last_trapsig) {
489 exiting_exitstatus = sig + 128;
491 exiting_exitstatus = oexitstatus;
493 exitstatus = oexitstatus = exiting_exitstatus;
494 if (setjmp(loc1.loc)) {
497 if (setjmp(loc2.loc)) {
501 if ((p = trap[0]) != NULL && *p != '\0') {
505 l1: handler = &loc2; /* probably unnecessary */
511 if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&
513 signal(sig, SIG_DFL);
515 sigaddset(&sigs, sig);
516 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
518 /* If the default action is to ignore, fall back to _exit(). */
520 _exit(exiting_exitstatus);