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 */
58 #include "myhistedit.h"
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,
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 */
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;
83 static int getsigaction(int, sig_t *);
87 * Map a string to a signal number.
89 * Note: the signal number may exceed NSIG.
92 sigstring_to_signum(char *sig)
99 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
100 } else if (strcasecmp(sig, "exit") == 0) {
105 if (strncasecmp(sig, "sig", 3) == 0)
107 for (n = 1; n < sys_nsig; n++)
108 if (sys_signame[n] &&
109 strcasecmp(sys_signame[n], sig) == 0)
117 * Print a list of valid signal names.
125 for (n = 1; n < sys_nsig; n++) {
126 if (sys_signame[n]) {
127 out1fmt("%s", sys_signame[n]);
128 outlen += strlen(sys_signame[n]);
131 outlen += 3; /* good enough */
134 if (outlen > 70 || n == sys_nsig - 1) {
148 trapcmd(int argc, char **argv)
154 for (signo = 0 ; signo < sys_nsig ; signo++) {
155 if (signo < NSIG && trap[signo] != NULL) {
157 out1qstr(trap[signo]);
160 } else if (sys_signame[signo]) {
161 out1fmt(" %s\n", sys_signame[signo]);
163 out1fmt(" %d\n", signo);
170 if (*++argv && strcmp(*argv, "--") == 0)
172 if (*argv && sigstring_to_signum(*argv) == -1) {
173 if ((*argv)[0] != '-') {
176 } else if ((*argv)[1] == '\0') {
178 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
182 error("bad option %s", *argv);
186 if ((signo = sigstring_to_signum(*argv)) == -1)
187 error("bad signal %s", *argv);
190 action = savestr(action);
193 trap[signo] = action;
204 * Clear traps on a fork.
211 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
212 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
217 setsignal(tp - trap);
225 * Check if we have any traps enabled.
232 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
233 if (*tp && **tp) /* trap not NULL or SIG_IGN */
240 * Set the signal handler for the specified signal. The routine figures
241 * out what it should be set to.
247 sig_t sig, sigact = SIG_DFL;
250 if ((t = trap[signo]) == NULL)
256 if (action == S_DFL) {
273 if (rootshell && iflag)
279 if (rootshell && mflag)
285 if (rootshell && iflag)
295 * current setting unknown
297 if (!getsigaction(signo, &sigact)) {
299 * Pretend it worked; maybe we should give a warning
300 * here, but other shells don't. We don't alter
301 * sigmode, so that we retry every time.
305 if (sigact == SIG_IGN) {
306 if (mflag && (signo == SIGTSTP ||
307 signo == SIGTTIN || signo == SIGTTOU)) {
308 *t = S_IGN; /* don't hard ignore these */
312 *t = S_RESET; /* force to be set */
315 if (*t == S_HARD_IGN || *t == action)
318 case S_DFL: sigact = SIG_DFL; break;
319 case S_CATCH: sigact = onsig; break;
320 case S_IGN: sigact = SIG_IGN; break;
323 sig = signal(signo, sigact);
324 if (sig != SIG_ERR && action == S_CATCH)
325 siginterrupt(signo, 1);
330 * Return the current setting for sig w/o changing it.
333 getsigaction(int signo, sig_t *sigact)
337 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
339 *sigact = (sig_t) sa.sa_handler;
351 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
352 signal(signo, SIG_IGN);
354 sigmode[signo] = S_HARD_IGN;
366 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
381 if (signo == SIGINT && trap[SIGINT] == NULL) {
386 if (signo != SIGCHLD || !ignore_sigchld)
390 /* If we are currently in a wait builtin, prepare to break it */
391 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
394 * If a trap is set, not ignored and not the null command, we need
395 * to make sure traps are executed even when a child blocks signals.
398 trap[signo] != NULL &&
399 ! (trap[signo][0] == '\0') &&
400 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
404 if (signo == SIGWINCH)
411 * Called to execute a trap. Perhaps we should avoid entering new trap
412 * handlers while we are executing a trap handler.
422 for (i = 1; i < NSIG; i++) {
427 * Ignore SIGCHLD to avoid infinite
428 * recursion if the trap action does
433 savestatus = exitstatus;
434 evalstring(trap[i], 0);
435 exitstatus = savestatus;
451 * Controls whether the shell is interactive or not.
454 setinteractive(int on)
456 static int is_interactive = -1;
458 if (on == is_interactive)
471 * Called to exit the shell.
474 exitshell(int status)
476 struct jmploc loc1, loc2;
479 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
480 if (setjmp(loc1.loc)) {
483 if (setjmp(loc2.loc)) {
487 if ((p = trap[0]) != NULL && *p != '\0') {
491 l1: handler = &loc2; /* probably unnecessary */