2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
49 #include "nodes.h" /* for other headers */
61 #include "myhistedit.h"
65 * Sigmode records the current value of the signal handlers for the various
66 * modes. A value of zero means that the current handler is not known.
67 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
70 #define S_DFL 1 /* default signal handling (SIG_DFL) */
71 #define S_CATCH 2 /* signal is caught */
72 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
73 #define S_HARD_IGN 4 /* signal is ignored permanently */
74 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
77 static char sigmode[NSIG]; /* current value of signal */
78 volatile sig_atomic_t pendingsig; /* indicates some signal received */
79 volatile sig_atomic_t pendingsig_waitcmd; /* indicates wait builtin should be interrupted */
80 static int in_dotrap; /* do we execute in a trap handler? */
81 static char *volatile trap[NSIG]; /* trap handler commands */
82 static volatile sig_atomic_t gotsig[NSIG];
83 /* indicates specified signal received */
84 static int ignore_sigchld; /* Used while handling SIGCHLD traps. */
85 static int last_trapsig;
87 static int exiting; /* exitshell() has been called */
88 static int exiting_exitstatus; /* value passed to exitshell() */
90 static int getsigaction(int, sig_t *);
94 * Map a string to a signal number.
96 * Note: the signal number may exceed NSIG.
99 sigstring_to_signum(char *sig)
102 if (is_number(sig)) {
106 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
107 } else if (strcasecmp(sig, "EXIT") == 0) {
112 if (strncasecmp(sig, "SIG", 3) == 0)
114 for (n = 1; n < sys_nsig; n++)
115 if (sys_signame[n] &&
116 strcasecmp(sys_signame[n], sig) == 0)
124 * Print a list of valid signal names.
132 for (n = 1; n < sys_nsig; n++) {
133 if (sys_signame[n]) {
134 out1fmt("%s", sys_signame[n]);
135 outlen += strlen(sys_signame[n]);
138 outlen += 3; /* good enough */
141 if (outlen > 71 || n == sys_nsig - 1) {
155 trapcmd(int argc __unused, char **argv)
162 while ((i = nextopt("l")) != '\0') {
172 for (signo = 0 ; signo < sys_nsig ; signo++) {
173 if (signo < NSIG && trap[signo] != NULL) {
175 out1qstr(trap[signo]);
178 } else if (sys_signame[signo]) {
179 out1fmt(" %s\n", sys_signame[signo]);
181 out1fmt(" %d\n", signo);
188 if (*argv && !is_number(*argv)) {
189 if (strcmp(*argv, "-") == 0)
196 for (; *argv; argv++) {
197 if ((signo = sigstring_to_signum(*argv)) == -1) {
198 warning("bad signal %s", *argv);
204 action = savestr(action);
207 trap[signo] = action;
217 * Clear traps on a fork.
224 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
225 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
230 setsignal(tp - trap);
238 * Check if we have any traps enabled.
245 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
246 if (*tp && **tp) /* trap not NULL or SIG_IGN */
253 * Set the signal handler for the specified signal. The routine figures
254 * out what it should be set to.
260 sig_t sigact = SIG_DFL;
264 if ((t = trap[signo]) == NULL)
270 if (action == S_DFL) {
287 if (rootshell && iflag)
293 if (rootshell && mflag)
303 * current setting unknown
305 if (!getsigaction(signo, &sigact)) {
307 * Pretend it worked; maybe we should give a warning
308 * here, but other shells don't. We don't alter
309 * sigmode, so that we retry every time.
313 if (sigact == SIG_IGN) {
314 if (mflag && (signo == SIGTSTP ||
315 signo == SIGTTIN || signo == SIGTTOU)) {
316 *t = S_IGN; /* don't hard ignore these */
320 *t = S_RESET; /* force to be set */
323 if (*t == S_HARD_IGN || *t == action)
326 case S_DFL: sigact = SIG_DFL; break;
327 case S_CATCH: sigact = onsig; break;
328 case S_IGN: sigact = SIG_IGN; break;
331 sa.sa_handler = sigact;
333 sigemptyset(&sa.sa_mask);
334 sigaction(signo, &sa, NULL);
339 * Return the current setting for sig w/o changing it.
342 getsigaction(int signo, sig_t *sigact)
346 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
348 *sigact = (sig_t) sa.sa_handler;
360 if (sigmode[signo] == 0)
362 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
363 signal(signo, SIG_IGN);
364 sigmode[signo] = S_IGN;
370 issigchldtrapped(void)
373 return (trap[SIGCHLD] != NULL && *trap[SIGCHLD] != '\0');
384 if (signo == SIGINT && trap[SIGINT] == NULL) {
386 * The !in_dotrap here is safe. The only way we can arrive
387 * here with in_dotrap set is that a trap handler set SIGINT to
388 * SIG_DFL and killed itself.
390 if (suppressint && !in_dotrap)
397 /* If we are currently in a wait builtin, prepare to break it */
398 if (signo == SIGINT || signo == SIGQUIT)
399 pendingsig_waitcmd = signo;
401 if (trap[signo] != NULL && trap[signo][0] != '\0' &&
402 (signo != SIGCHLD || !ignore_sigchld)) {
405 pendingsig_waitcmd = signo;
411 * Called to execute a trap. Perhaps we should avoid entering new trap
412 * handlers while we are executing a trap handler.
417 struct stackmark smark;
419 int savestatus, prev_evalskip, prev_skipcount;
424 pendingsig_waitcmd = 0;
425 for (i = 1; i < NSIG; i++) {
430 * Ignore SIGCHLD to avoid infinite
431 * recursion if the trap action does
438 * Backup current evalskip
439 * state and reset it before
440 * executing a trap, so that the
441 * trap is not disturbed by an
442 * ongoing break/continue/return
445 prev_evalskip = evalskip;
446 prev_skipcount = skipcount;
450 savestatus = exitstatus;
451 setstackmark(&smark);
452 evalstring(stsavestr(trap[i]), 0);
453 popstackmark(&smark);
456 * If such a command was not
457 * already in progress, allow a
458 * break/continue/return in the
459 * trap action to have an effect
463 prev_evalskip != 0) {
464 evalskip = prev_evalskip;
465 skipcount = prev_skipcount;
466 exitstatus = savestatus;
483 * Controls whether the shell is interactive or not based on iflag.
495 * Called to exit the shell.
498 exitshell(int status)
500 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
502 exiting_exitstatus = status;
503 exitshell_savedstatus();
507 exitshell_savedstatus(void)
509 struct jmploc loc1, loc2;
515 if (in_dotrap && last_trapsig) {
517 exiting_exitstatus = sig + 128;
519 exiting_exitstatus = oexitstatus;
521 exitstatus = oexitstatus = exiting_exitstatus;
522 if (!setjmp(loc1.loc)) {
524 if ((p = trap[0]) != NULL && *p != '\0') {
526 * Reset evalskip, or the trap on EXIT could be
527 * interrupted if the last command was a "return".
535 if (!setjmp(loc2.loc)) {
536 handler = &loc2; /* probably unnecessary */
543 if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&
545 signal(sig, SIG_DFL);
547 sigaddset(&sigs, sig);
548 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
550 /* If the default action is to ignore, fall back to _exit(). */
552 _exit(exiting_exitstatus);