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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
47 #include "nodes.h" /* for other headers */
59 #include "myhistedit.h"
63 * Sigmode records the current value of the signal handlers for the various
64 * modes. A value of zero means that the current handler is not known.
65 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
68 #define S_DFL 1 /* default signal handling (SIG_DFL) */
69 #define S_CATCH 2 /* signal is caught */
70 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
71 #define S_HARD_IGN 4 /* signal is ignored permanently */
72 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
75 MKINIT char sigmode[NSIG]; /* current value of signal */
76 int pendingsigs; /* indicates some signal received */
77 int in_dotrap; /* do we execute in a trap handler? */
78 static char *volatile trap[NSIG]; /* trap handler commands */
79 static volatile sig_atomic_t gotsig[NSIG];
80 /* indicates specified signal received */
81 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
82 volatile sig_atomic_t gotwinch;
83 static int last_trapsig;
85 static int exiting; /* exitshell() has been called */
86 static int exiting_exitstatus; /* value passed to exitshell() */
88 static int getsigaction(int, sig_t *);
92 * Map a string to a signal number.
94 * Note: the signal number may exceed NSIG.
97 sigstring_to_signum(char *sig)
100 if (is_number(sig)) {
104 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
105 } else if (strcasecmp(sig, "EXIT") == 0) {
110 if (strncasecmp(sig, "SIG", 3) == 0)
112 for (n = 1; n < sys_nsig; n++)
113 if (sys_signame[n] &&
114 strcasecmp(sys_signame[n], sig) == 0)
122 * Print a list of valid signal names.
130 for (n = 1; n < sys_nsig; n++) {
131 if (sys_signame[n]) {
132 out1fmt("%s", sys_signame[n]);
133 outlen += strlen(sys_signame[n]);
136 outlen += 3; /* good enough */
139 if (outlen > 71 || n == sys_nsig - 1) {
153 trapcmd(int argc, char **argv)
160 while ((i = nextopt("l")) != '\0') {
170 for (signo = 0 ; signo < sys_nsig ; signo++) {
171 if (signo < NSIG && trap[signo] != NULL) {
173 out1qstr(trap[signo]);
176 } else if (sys_signame[signo]) {
177 out1fmt(" %s\n", sys_signame[signo]);
179 out1fmt(" %d\n", signo);
186 if (*argv && sigstring_to_signum(*argv) == -1) {
187 if (strcmp(*argv, "-") == 0)
195 if ((signo = sigstring_to_signum(*argv)) == -1) {
196 warning("bad signal %s", *argv);
201 action = savestr(action);
204 trap[signo] = action;
215 * Clear traps on a fork.
222 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
223 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
228 setsignal(tp - trap);
236 * Check if we have any traps enabled.
243 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
244 if (*tp && **tp) /* trap not NULL or SIG_IGN */
251 * Set the signal handler for the specified signal. The routine figures
252 * out what it should be set to.
258 sig_t sigact = SIG_DFL;
262 if ((t = trap[signo]) == NULL)
268 if (action == S_DFL) {
285 if (rootshell && iflag)
291 if (rootshell && mflag)
297 if (rootshell && iflag)
307 * current setting unknown
309 if (!getsigaction(signo, &sigact)) {
311 * Pretend it worked; maybe we should give a warning
312 * here, but other shells don't. We don't alter
313 * sigmode, so that we retry every time.
317 if (sigact == SIG_IGN) {
318 if (mflag && (signo == SIGTSTP ||
319 signo == SIGTTIN || signo == SIGTTOU)) {
320 *t = S_IGN; /* don't hard ignore these */
324 *t = S_RESET; /* force to be set */
327 if (*t == S_HARD_IGN || *t == action)
330 case S_DFL: sigact = SIG_DFL; break;
331 case S_CATCH: sigact = onsig; break;
332 case S_IGN: sigact = SIG_IGN; break;
335 sa.sa_handler = sigact;
337 sigemptyset(&sa.sa_mask);
338 sigaction(signo, &sa, NULL);
343 * Return the current setting for sig w/o changing it.
346 getsigaction(int signo, sig_t *sigact)
350 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
352 *sigact = (sig_t) sa.sa_handler;
364 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
365 signal(signo, SIG_IGN);
367 sigmode[signo] = S_HARD_IGN;
378 if (signo == SIGINT && trap[SIGINT] == NULL) {
383 if (signo != SIGCHLD || !ignore_sigchld)
387 /* If we are currently in a wait builtin, prepare to break it */
388 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
391 * If a trap is set, not ignored and not the null command, we need
392 * to make sure traps are executed even when a child blocks signals.
395 trap[signo] != NULL &&
396 ! (trap[signo][0] == '\0') &&
397 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
401 if (signo == SIGWINCH)
408 * Called to execute a trap. Perhaps we should avoid entering new trap
409 * handlers while we are executing a trap handler.
419 for (i = 1; i < NSIG; i++) {
424 * Ignore SIGCHLD to avoid infinite
425 * recursion if the trap action does
431 savestatus = exitstatus;
432 evalstring(trap[i], 0);
433 exitstatus = savestatus;
449 * Controls whether the shell is interactive or not.
452 setinteractive(int on)
454 static int is_interactive = -1;
456 if (on == is_interactive)
469 * Called to exit the shell.
472 exitshell(int status)
474 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
476 exiting_exitstatus = status;
477 exitshell_savedstatus();
481 exitshell_savedstatus(void)
483 struct jmploc loc1, loc2;
489 if (in_dotrap && last_trapsig) {
491 exiting_exitstatus = sig + 128;
493 exiting_exitstatus = oexitstatus;
495 exitstatus = oexitstatus = exiting_exitstatus;
496 if (setjmp(loc1.loc)) {
499 if (setjmp(loc2.loc)) {
503 if ((p = trap[0]) != NULL && *p != '\0') {
507 l1: handler = &loc2; /* probably unnecessary */
513 if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&
515 signal(sig, SIG_DFL);
517 sigaddset(&sigs, sig);
518 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
520 /* If the default action is to ignore, fall back to _exit(). */
522 _exit(exiting_exitstatus);