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.20.2.2 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/trap.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
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.
90 sigstring_to_signum(char *sig)
97 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
98 } else if (strcasecmp(sig, "exit") == 0) {
103 if (strncasecmp(sig, "sig", 3) == 0)
105 for (n = 1; n < NSIG; n++)
106 if (strcasecmp(sys_signame[n], sig) == 0)
114 * Print a list of valid signal names.
121 for (n = 1; n < NSIG; n++) {
122 out1fmt("%s", sys_signame[n]);
123 if (n == (NSIG / 2) || n == (NSIG - 1))
135 trapcmd(int argc, char **argv)
141 for (signo = 0 ; signo < NSIG ; signo++) {
142 if (trap[signo] != NULL)
143 out1fmt("trap -- '%s' %s\n", trap[signo],
144 (signo) ? sys_signame[signo] : "exit");
149 if (*++argv && strcmp(*argv, "--") == 0)
151 if (*argv && sigstring_to_signum(*argv) == -1) {
152 if ((*argv)[0] != '-') {
155 } else if ((*argv)[1] == '\0') {
157 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
161 error("bad option %s", *argv);
165 if ((signo = sigstring_to_signum(*argv)) == -1)
166 error("bad signal %s", *argv);
169 action = savestr(action);
172 trap[signo] = action;
183 * Clear traps on a fork.
190 for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
191 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
196 setsignal(tp - trap);
204 * Set the signal handler for the specified signal. The routine figures
205 * out what it should be set to.
211 sig_t sig, sigact = SIG_DFL;
214 if ((t = trap[signo]) == NULL)
220 if (action == S_DFL) {
237 if (rootshell && iflag)
243 if (rootshell && mflag)
249 if (rootshell && iflag)
259 * current setting unknown
261 if (!getsigaction(signo, &sigact)) {
263 * Pretend it worked; maybe we should give a warning
264 * here, but other shells don't. We don't alter
265 * sigmode, so that we retry every time.
269 if (sigact == SIG_IGN) {
270 if (mflag && (signo == SIGTSTP ||
271 signo == SIGTTIN || signo == SIGTTOU)) {
272 *t = S_IGN; /* don't hard ignore these */
276 *t = S_RESET; /* force to be set */
279 if (*t == S_HARD_IGN || *t == action)
282 case S_DFL: sigact = SIG_DFL; break;
283 case S_CATCH: sigact = onsig; break;
284 case S_IGN: sigact = SIG_IGN; break;
287 sig = signal(signo, sigact);
288 if (sig != SIG_ERR && action == S_CATCH)
289 siginterrupt(signo, 1);
294 * Return the current setting for sig w/o changing it.
297 getsigaction(int signo, sig_t *sigact)
301 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
303 *sigact = (sig_t) sa.sa_handler;
315 if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
316 signal(signo, SIG_IGN);
318 sigmode[signo] = S_HARD_IGN;
330 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
345 if (signo == SIGINT && trap[SIGINT] == NULL) {
350 if (signo != SIGCHLD || !ignore_sigchld)
354 /* If we are currently in a wait builtin, prepare to break it */
355 if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
358 * If a trap is set, not ignored and not the null command, we need
359 * to make sure traps are executed even when a child blocks signals.
362 trap[signo] != NULL &&
363 ! trap[signo][0] == '\0' &&
364 ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
368 if (signo == SIGWINCH)
375 * Called to execute a trap. Perhaps we should avoid entering new trap
376 * handlers while we are executing a trap handler.
386 for (i = 1; i < NSIG; i++) {
391 * Ignore SIGCHLD to avoid infinite recursion
392 * if the trap action does a fork.
396 savestatus = exitstatus;
398 exitstatus = savestatus;
414 * Controls whether the shell is interactive or not.
417 setinteractive(int on)
419 static int is_interactive = -1;
421 if (on == is_interactive)
434 * Called to exit the shell.
437 exitshell(int status)
439 struct jmploc loc1, loc2;
442 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
443 if (setjmp(loc1.loc)) {
446 if (setjmp(loc2.loc)) {
450 if ((p = trap[0]) != NULL && *p != '\0') {
454 l1: handler = &loc2; /* probably unnecessary */