xref: /freebsd/bin/sh/trap.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Kenneth Almquist.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
35aa9caaf6SPeter Wemm #include <signal.h>
36aa9caaf6SPeter Wemm #include <unistd.h>
37aa9caaf6SPeter Wemm #include <stdlib.h>
38aa9caaf6SPeter Wemm 
394b88c807SRodney W. Grimes #include "shell.h"
404b88c807SRodney W. Grimes #include "main.h"
414b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
424b88c807SRodney W. Grimes #include "eval.h"
434b88c807SRodney W. Grimes #include "jobs.h"
44aa9caaf6SPeter Wemm #include "show.h"
454b88c807SRodney W. Grimes #include "options.h"
464b88c807SRodney W. Grimes #include "syntax.h"
474b88c807SRodney W. Grimes #include "output.h"
484b88c807SRodney W. Grimes #include "memalloc.h"
494b88c807SRodney W. Grimes #include "error.h"
504b88c807SRodney W. Grimes #include "trap.h"
514b88c807SRodney W. Grimes #include "mystring.h"
52454a02b3SJilles Tjoelker #include "builtins.h"
53*971677d5SBryan Drewery #ifndef NO_HISTORY
549de7305eSTim J. Robbins #include "myhistedit.h"
55*971677d5SBryan Drewery #endif
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes /*
594b88c807SRodney W. Grimes  * Sigmode records the current value of the signal handlers for the various
604b88c807SRodney W. Grimes  * modes.  A value of zero means that the current handler is not known.
614b88c807SRodney W. Grimes  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
624b88c807SRodney W. Grimes  */
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes #define S_DFL 1			/* default signal handling (SIG_DFL) */
654b88c807SRodney W. Grimes #define S_CATCH 2		/* signal is caught */
664b88c807SRodney W. Grimes #define S_IGN 3			/* signal is ignored (SIG_IGN) */
6746be34b9SKris Kennaway #define S_HARD_IGN 4		/* signal is ignored permanently */
684b88c807SRodney W. Grimes #define S_RESET 5		/* temporary - to reset a hard ignored sig */
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes 
710bdd3871SJilles Tjoelker static char sigmode[NSIG];	/* current value of signal */
7225e0f0f5SJilles Tjoelker volatile sig_atomic_t pendingsig;	/* indicates some signal received */
73edebe564SJilles Tjoelker volatile sig_atomic_t pendingsig_waitcmd;	/* indicates wait builtin should be interrupted */
7485052e7bSJilles Tjoelker static int in_dotrap;			/* do we execute in a trap handler? */
75135421ddSMartin Cracauer static char *volatile trap[NSIG];	/* trap handler commands */
767a8e920bSMartin Cracauer static volatile sig_atomic_t gotsig[NSIG];
777a8e920bSMartin Cracauer 				/* indicates specified signal received */
78f98e1b80SSteve Price static int ignore_sigchld;	/* Used while handling SIGCHLD traps. */
79ebdfd6dcSJilles Tjoelker static int last_trapsig;
804b88c807SRodney W. Grimes 
8170df11eaSJilles Tjoelker static int exiting;		/* exitshell() has been called */
8270df11eaSJilles Tjoelker static int exiting_exitstatus;	/* value passed to exitshell() */
8370df11eaSJilles Tjoelker 
8488328642SDavid E. O'Brien static int getsigaction(int, sig_t *);
85aa9caaf6SPeter Wemm 
86f98e1b80SSteve Price 
87f98e1b80SSteve Price /*
88f98e1b80SSteve Price  * Map a string to a signal number.
8997054a76SNate Lawson  *
9097054a76SNate Lawson  * Note: the signal number may exceed NSIG.
91f98e1b80SSteve Price  */
9288328642SDavid E. O'Brien static int
sigstring_to_signum(char * sig)935134c3f7SWarner Losh sigstring_to_signum(char *sig)
94f98e1b80SSteve Price {
95f98e1b80SSteve Price 
96f98e1b80SSteve Price 	if (is_number(sig)) {
97f98e1b80SSteve Price 		int signo;
98f98e1b80SSteve Price 
99f98e1b80SSteve Price 		signo = atoi(sig);
100f98e1b80SSteve Price 		return ((signo >= 0 && signo < NSIG) ? signo : (-1));
10112dacf62SJilles Tjoelker 	} else if (strcasecmp(sig, "EXIT") == 0) {
102f98e1b80SSteve Price 		return (0);
103f98e1b80SSteve Price 	} else {
104f98e1b80SSteve Price 		int n;
105f98e1b80SSteve Price 
10612dacf62SJilles Tjoelker 		if (strncasecmp(sig, "SIG", 3) == 0)
107f98e1b80SSteve Price 			sig += 3;
1081b13752aSNate Lawson 		for (n = 1; n < sys_nsig; n++)
109aa0b7126SNate Lawson 			if (sys_signame[n] &&
110aa0b7126SNate Lawson 			    strcasecmp(sys_signame[n], sig) == 0)
111f98e1b80SSteve Price 				return (n);
112f98e1b80SSteve Price 	}
113f98e1b80SSteve Price 	return (-1);
114f98e1b80SSteve Price }
115f98e1b80SSteve Price 
116f98e1b80SSteve Price 
117f98e1b80SSteve Price /*
118f98e1b80SSteve Price  * Print a list of valid signal names.
119f98e1b80SSteve Price  */
12088328642SDavid E. O'Brien static void
printsignals(void)1215134c3f7SWarner Losh printsignals(void)
122f98e1b80SSteve Price {
12397054a76SNate Lawson 	int n, outlen;
124f98e1b80SSteve Price 
12597054a76SNate Lawson 	outlen = 0;
1261b13752aSNate Lawson 	for (n = 1; n < sys_nsig; n++) {
12797054a76SNate Lawson 		if (sys_signame[n]) {
128f98e1b80SSteve Price 			out1fmt("%s", sys_signame[n]);
12997054a76SNate Lawson 			outlen += strlen(sys_signame[n]);
13097054a76SNate Lawson 		} else {
13197054a76SNate Lawson 			out1fmt("%d", n);
13297054a76SNate Lawson 			outlen += 3;	/* good enough */
13397054a76SNate Lawson 		}
13497054a76SNate Lawson 		++outlen;
13545b3c176SJilles Tjoelker 		if (outlen > 71 || n == sys_nsig - 1) {
136f98e1b80SSteve Price 			out1str("\n");
13797054a76SNate Lawson 			outlen = 0;
13897054a76SNate Lawson 		} else {
139f98e1b80SSteve Price 			out1c(' ');
140f98e1b80SSteve Price 		}
141f98e1b80SSteve Price 	}
14297054a76SNate Lawson }
143f98e1b80SSteve Price 
144f98e1b80SSteve Price 
1454b88c807SRodney W. Grimes /*
1464b88c807SRodney W. Grimes  * The trap builtin.
1474b88c807SRodney W. Grimes  */
148aa9caaf6SPeter Wemm int
trapcmd(int argc __unused,char ** argv)14946c6b52dSJilles Tjoelker trapcmd(int argc __unused, char **argv)
150aa9caaf6SPeter Wemm {
1514b88c807SRodney W. Grimes 	char *action;
1524b88c807SRodney W. Grimes 	int signo;
153e3c2cd72SJilles Tjoelker 	int errors = 0;
154a043cc4cSJilles Tjoelker 	int i;
1554b88c807SRodney W. Grimes 
156a043cc4cSJilles Tjoelker 	while ((i = nextopt("l")) != '\0') {
157a043cc4cSJilles Tjoelker 		switch (i) {
158a043cc4cSJilles Tjoelker 		case 'l':
159a043cc4cSJilles Tjoelker 			printsignals();
160a043cc4cSJilles Tjoelker 			return (0);
161a043cc4cSJilles Tjoelker 		}
162a043cc4cSJilles Tjoelker 	}
163a043cc4cSJilles Tjoelker 	argv = argptr;
164a043cc4cSJilles Tjoelker 
165a043cc4cSJilles Tjoelker 	if (*argv == NULL) {
1661b13752aSNate Lawson 		for (signo = 0 ; signo < sys_nsig ; signo++) {
16797054a76SNate Lawson 			if (signo < NSIG && trap[signo] != NULL) {
1682a5e306dSStefan Farfeleder 				out1str("trap -- ");
1692a5e306dSStefan Farfeleder 				out1qstr(trap[signo]);
17097054a76SNate Lawson 				if (signo == 0) {
17112dacf62SJilles Tjoelker 					out1str(" EXIT\n");
17297054a76SNate Lawson 				} else if (sys_signame[signo]) {
1732a5e306dSStefan Farfeleder 					out1fmt(" %s\n", sys_signame[signo]);
17497054a76SNate Lawson 				} else {
1752a5e306dSStefan Farfeleder 					out1fmt(" %d\n", signo);
17697054a76SNate Lawson 				}
17797054a76SNate Lawson 			}
1784b88c807SRodney W. Grimes 		}
1794b88c807SRodney W. Grimes 		return 0;
1804b88c807SRodney W. Grimes 	}
1814b88c807SRodney W. Grimes 	action = NULL;
182a59f8174SBryan Drewery 	if (*argv && !is_number(*argv)) {
183a043cc4cSJilles Tjoelker 		if (strcmp(*argv, "-") == 0)
184a043cc4cSJilles Tjoelker 			argv++;
185a043cc4cSJilles Tjoelker 		else {
186f98e1b80SSteve Price 			action = *argv;
187f98e1b80SSteve Price 			argv++;
188f98e1b80SSteve Price 		}
189f98e1b80SSteve Price 	}
19052c45039SJilles Tjoelker 	for (; *argv; argv++) {
191e3c2cd72SJilles Tjoelker 		if ((signo = sigstring_to_signum(*argv)) == -1) {
1925fe9123fSJilles Tjoelker 			warning("bad signal %s", *argv);
193e3c2cd72SJilles Tjoelker 			errors = 1;
19452c45039SJilles Tjoelker 			continue;
195e3c2cd72SJilles Tjoelker 		}
1964b88c807SRodney W. Grimes 		INTOFF;
1974b88c807SRodney W. Grimes 		if (action)
1984b88c807SRodney W. Grimes 			action = savestr(action);
1994b88c807SRodney W. Grimes 		if (trap[signo])
2004b88c807SRodney W. Grimes 			ckfree(trap[signo]);
2014b88c807SRodney W. Grimes 		trap[signo] = action;
2024b88c807SRodney W. Grimes 		if (signo != 0)
2034b88c807SRodney W. Grimes 			setsignal(signo);
2044b88c807SRodney W. Grimes 		INTON;
2054b88c807SRodney W. Grimes 	}
206e3c2cd72SJilles Tjoelker 	return errors;
2074b88c807SRodney W. Grimes }
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes 
2104b88c807SRodney W. Grimes /*
2114b88c807SRodney W. Grimes  * Clear traps on a fork.
2124b88c807SRodney W. Grimes  */
2134b88c807SRodney W. Grimes void
clear_traps(void)2145134c3f7SWarner Losh clear_traps(void)
215f98e1b80SSteve Price {
216135421ddSMartin Cracauer 	char *volatile *tp;
2174b88c807SRodney W. Grimes 
218f98e1b80SSteve Price 	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
2194b88c807SRodney W. Grimes 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
2204b88c807SRodney W. Grimes 			INTOFF;
2214b88c807SRodney W. Grimes 			ckfree(*tp);
2224b88c807SRodney W. Grimes 			*tp = NULL;
2234b88c807SRodney W. Grimes 			if (tp != &trap[0])
2244b88c807SRodney W. Grimes 				setsignal(tp - trap);
2254b88c807SRodney W. Grimes 			INTON;
2264b88c807SRodney W. Grimes 		}
2274b88c807SRodney W. Grimes 	}
2284b88c807SRodney W. Grimes }
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes 
2314b88c807SRodney W. Grimes /*
2326e28dacfSJilles Tjoelker  * Check if we have any traps enabled.
2336e28dacfSJilles Tjoelker  */
2346e28dacfSJilles Tjoelker int
have_traps(void)2356e28dacfSJilles Tjoelker have_traps(void)
2366e28dacfSJilles Tjoelker {
2376e28dacfSJilles Tjoelker 	char *volatile *tp;
2386e28dacfSJilles Tjoelker 
2396e28dacfSJilles Tjoelker 	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
2406e28dacfSJilles Tjoelker 		if (*tp && **tp)	/* trap not NULL or SIG_IGN */
2416e28dacfSJilles Tjoelker 			return 1;
2426e28dacfSJilles Tjoelker 	}
2436e28dacfSJilles Tjoelker 	return 0;
2446e28dacfSJilles Tjoelker }
2456e28dacfSJilles Tjoelker 
2466e28dacfSJilles Tjoelker /*
2474b88c807SRodney W. Grimes  * Set the signal handler for the specified signal.  The routine figures
2484b88c807SRodney W. Grimes  * out what it should be set to.
2494b88c807SRodney W. Grimes  */
25028701136SBruce Evans void
setsignal(int signo)2515134c3f7SWarner Losh setsignal(int signo)
252aa9caaf6SPeter Wemm {
2534b88c807SRodney W. Grimes 	int action;
2543f228d74SJilles Tjoelker 	sig_t sigact = SIG_DFL;
2553f228d74SJilles Tjoelker 	struct sigaction sa;
2564b88c807SRodney W. Grimes 	char *t;
2574b88c807SRodney W. Grimes 
2584b88c807SRodney W. Grimes 	if ((t = trap[signo]) == NULL)
2594b88c807SRodney W. Grimes 		action = S_DFL;
2604b88c807SRodney W. Grimes 	else if (*t != '\0')
2614b88c807SRodney W. Grimes 		action = S_CATCH;
2624b88c807SRodney W. Grimes 	else
2634b88c807SRodney W. Grimes 		action = S_IGN;
2647a8e920bSMartin Cracauer 	if (action == S_DFL) {
2654b88c807SRodney W. Grimes 		switch (signo) {
2664b88c807SRodney W. Grimes 		case SIGINT:
2674b88c807SRodney W. Grimes 			action = S_CATCH;
2684b88c807SRodney W. Grimes 			break;
2694b88c807SRodney W. Grimes 		case SIGQUIT:
2704b88c807SRodney W. Grimes #ifdef DEBUG
2714b88c807SRodney W. Grimes 			if (debug)
2724b88c807SRodney W. Grimes 				break;
2734b88c807SRodney W. Grimes #endif
27440d009a7SMartin Cracauer 			action = S_CATCH;
2757a8e920bSMartin Cracauer 			break;
2764b88c807SRodney W. Grimes 		case SIGTERM:
2777a8e920bSMartin Cracauer 			if (rootshell && iflag)
2784b88c807SRodney W. Grimes 				action = S_IGN;
2794b88c807SRodney W. Grimes 			break;
2804b88c807SRodney W. Grimes #if JOBS
2814b88c807SRodney W. Grimes 		case SIGTSTP:
2824b88c807SRodney W. Grimes 		case SIGTTOU:
2837a8e920bSMartin Cracauer 			if (rootshell && mflag)
2844b88c807SRodney W. Grimes 				action = S_IGN;
2854b88c807SRodney W. Grimes 			break;
2864b88c807SRodney W. Grimes #endif
2874b88c807SRodney W. Grimes 		}
2884b88c807SRodney W. Grimes 	}
289aa9caaf6SPeter Wemm 
290f98e1b80SSteve Price 	t = &sigmode[signo];
2914b88c807SRodney W. Grimes 	if (*t == 0) {
2924b88c807SRodney W. Grimes 		/*
2934b88c807SRodney W. Grimes 		 * current setting unknown
2944b88c807SRodney W. Grimes 		 */
295aa9caaf6SPeter Wemm 		if (!getsigaction(signo, &sigact)) {
296aa9caaf6SPeter Wemm 			/*
297aa9caaf6SPeter Wemm 			 * Pretend it worked; maybe we should give a warning
298aa9caaf6SPeter Wemm 			 * here, but other shells don't. We don't alter
299aa9caaf6SPeter Wemm 			 * sigmode, so that we retry every time.
300aa9caaf6SPeter Wemm 			 */
30128701136SBruce Evans 			return;
302aa9caaf6SPeter Wemm 		}
3034b88c807SRodney W. Grimes 		if (sigact == SIG_IGN) {
3044b88c807SRodney W. Grimes 			if (mflag && (signo == SIGTSTP ||
3054b88c807SRodney W. Grimes 			     signo == SIGTTIN || signo == SIGTTOU)) {
3064b88c807SRodney W. Grimes 				*t = S_IGN;	/* don't hard ignore these */
3074b88c807SRodney W. Grimes 			} else
3084b88c807SRodney W. Grimes 				*t = S_HARD_IGN;
3094b88c807SRodney W. Grimes 		} else {
3104b88c807SRodney W. Grimes 			*t = S_RESET;	/* force to be set */
3114b88c807SRodney W. Grimes 		}
3124b88c807SRodney W. Grimes 	}
3134b88c807SRodney W. Grimes 	if (*t == S_HARD_IGN || *t == action)
31428701136SBruce Evans 		return;
3154b88c807SRodney W. Grimes 	switch (action) {
3164b88c807SRodney W. Grimes 		case S_DFL:	sigact = SIG_DFL;	break;
3174b88c807SRodney W. Grimes 		case S_CATCH:  	sigact = onsig;		break;
3184b88c807SRodney W. Grimes 		case S_IGN:	sigact = SIG_IGN;	break;
3194b88c807SRodney W. Grimes 	}
3204b88c807SRodney W. Grimes 	*t = action;
3213f228d74SJilles Tjoelker 	sa.sa_handler = sigact;
3223f228d74SJilles Tjoelker 	sa.sa_flags = 0;
3233f228d74SJilles Tjoelker 	sigemptyset(&sa.sa_mask);
3243f228d74SJilles Tjoelker 	sigaction(signo, &sa, NULL);
3254b88c807SRodney W. Grimes }
3264b88c807SRodney W. Grimes 
327f98e1b80SSteve Price 
3284b88c807SRodney W. Grimes /*
3294b88c807SRodney W. Grimes  * Return the current setting for sig w/o changing it.
3304b88c807SRodney W. Grimes  */
33188328642SDavid E. O'Brien static int
getsigaction(int signo,sig_t * sigact)3325134c3f7SWarner Losh getsigaction(int signo, sig_t *sigact)
333aa9caaf6SPeter Wemm {
3344b88c807SRodney W. Grimes 	struct sigaction sa;
3354b88c807SRodney W. Grimes 
3364b88c807SRodney W. Grimes 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
337aa9caaf6SPeter Wemm 		return 0;
338aa9caaf6SPeter Wemm 	*sigact = (sig_t) sa.sa_handler;
339aa9caaf6SPeter Wemm 	return 1;
3404b88c807SRodney W. Grimes }
3414b88c807SRodney W. Grimes 
342f98e1b80SSteve Price 
3434b88c807SRodney W. Grimes /*
3444b88c807SRodney W. Grimes  * Ignore a signal.
3454b88c807SRodney W. Grimes  */
3464b88c807SRodney W. Grimes void
ignoresig(int signo)3475134c3f7SWarner Losh ignoresig(int signo)
348aa9caaf6SPeter Wemm {
349f98e1b80SSteve Price 
350efd1946cSJilles Tjoelker 	if (sigmode[signo] == 0)
351efd1946cSJilles Tjoelker 		setsignal(signo);
352f98e1b80SSteve Price 	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
3534b88c807SRodney W. Grimes 		signal(signo, SIG_IGN);
354efd1946cSJilles Tjoelker 		sigmode[signo] = S_IGN;
3554b88c807SRodney W. Grimes 	}
3564b88c807SRodney W. Grimes }
3574b88c807SRodney W. Grimes 
3584b88c807SRodney W. Grimes 
3591794251aSJilles Tjoelker int
issigchldtrapped(void)3601794251aSJilles Tjoelker issigchldtrapped(void)
3611794251aSJilles Tjoelker {
3621794251aSJilles Tjoelker 
3631794251aSJilles Tjoelker 	return (trap[SIGCHLD] != NULL && *trap[SIGCHLD] != '\0');
3641794251aSJilles Tjoelker }
3651794251aSJilles Tjoelker 
3661794251aSJilles Tjoelker 
3674b88c807SRodney W. Grimes /*
3684b88c807SRodney W. Grimes  * Signal handler.
3694b88c807SRodney W. Grimes  */
3704b88c807SRodney W. Grimes void
onsig(int signo)3715134c3f7SWarner Losh onsig(int signo)
372aa9caaf6SPeter Wemm {
37328701136SBruce Evans 
3744b88c807SRodney W. Grimes 	if (signo == SIGINT && trap[SIGINT] == NULL) {
375bd111900SJilles Tjoelker 		if (suppressint)
37685052e7bSJilles Tjoelker 			SET_PENDING_INT;
37785052e7bSJilles Tjoelker 		else
3784b88c807SRodney W. Grimes 			onint();
3794b88c807SRodney W. Grimes 		return;
3804b88c807SRodney W. Grimes 	}
381135421ddSMartin Cracauer 
382135421ddSMartin Cracauer 	/* If we are currently in a wait builtin, prepare to break it */
383b823fb59SJilles Tjoelker 	if (signo == SIGINT || signo == SIGQUIT)
384b823fb59SJilles Tjoelker 		pendingsig_waitcmd = signo;
38525e0f0f5SJilles Tjoelker 
38625e0f0f5SJilles Tjoelker 	if (trap[signo] != NULL && trap[signo][0] != '\0' &&
38725e0f0f5SJilles Tjoelker 	    (signo != SIGCHLD || !ignore_sigchld)) {
38825e0f0f5SJilles Tjoelker 		gotsig[signo] = 1;
38925e0f0f5SJilles Tjoelker 		pendingsig = signo;
390edebe564SJilles Tjoelker 		pendingsig_waitcmd = signo;
39125e0f0f5SJilles Tjoelker 	}
3924b88c807SRodney W. Grimes }
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes 
3954b88c807SRodney W. Grimes /*
3964b88c807SRodney W. Grimes  * Called to execute a trap.  Perhaps we should avoid entering new trap
3974b88c807SRodney W. Grimes  * handlers while we are executing a trap handler.
3984b88c807SRodney W. Grimes  */
3994b88c807SRodney W. Grimes void
dotrap(void)4005134c3f7SWarner Losh dotrap(void)
401f98e1b80SSteve Price {
402f30e8c7eSJilles Tjoelker 	struct stackmark smark;
4034b88c807SRodney W. Grimes 	int i;
40401a43bcfSJean-Sébastien Pédron 	int savestatus, prev_evalskip, prev_skipcount;
4054b88c807SRodney W. Grimes 
4067a8e920bSMartin Cracauer 	in_dotrap++;
4074b88c807SRodney W. Grimes 	for (;;) {
40825e0f0f5SJilles Tjoelker 		pendingsig = 0;
409b823fb59SJilles Tjoelker 		pendingsig_waitcmd = 0;
410f98e1b80SSteve Price 		for (i = 1; i < NSIG; i++) {
411f98e1b80SSteve Price 			if (gotsig[i]) {
412f98e1b80SSteve Price 				gotsig[i] = 0;
413f98e1b80SSteve Price 				if (trap[i]) {
414f98e1b80SSteve Price 					/*
415aa0b7126SNate Lawson 					 * Ignore SIGCHLD to avoid infinite
416aa0b7126SNate Lawson 					 * recursion if the trap action does
417aa0b7126SNate Lawson 					 * a fork.
418f98e1b80SSteve Price 					 */
419f98e1b80SSteve Price 					if (i == SIGCHLD)
420f98e1b80SSteve Price 						ignore_sigchld++;
42101a43bcfSJean-Sébastien Pédron 
42201a43bcfSJean-Sébastien Pédron 					/*
42301a43bcfSJean-Sébastien Pédron 					 * Backup current evalskip
42401a43bcfSJean-Sébastien Pédron 					 * state and reset it before
42501a43bcfSJean-Sébastien Pédron 					 * executing a trap, so that the
42601a43bcfSJean-Sébastien Pédron 					 * trap is not disturbed by an
42701a43bcfSJean-Sébastien Pédron 					 * ongoing break/continue/return
42801a43bcfSJean-Sébastien Pédron 					 * statement.
42901a43bcfSJean-Sébastien Pédron 					 */
43001a43bcfSJean-Sébastien Pédron 					prev_evalskip  = evalskip;
43101a43bcfSJean-Sébastien Pédron 					prev_skipcount = skipcount;
43201a43bcfSJean-Sébastien Pédron 					evalskip = 0;
43301a43bcfSJean-Sébastien Pédron 
434ebdfd6dcSJilles Tjoelker 					last_trapsig = i;
4354b88c807SRodney W. Grimes 					savestatus = exitstatus;
436f30e8c7eSJilles Tjoelker 					setstackmark(&smark);
437f30e8c7eSJilles Tjoelker 					evalstring(stsavestr(trap[i]), 0);
438f30e8c7eSJilles Tjoelker 					popstackmark(&smark);
43901a43bcfSJean-Sébastien Pédron 
44001a43bcfSJean-Sébastien Pédron 					/*
44101a43bcfSJean-Sébastien Pédron 					 * If such a command was not
44201a43bcfSJean-Sébastien Pédron 					 * already in progress, allow a
44301a43bcfSJean-Sébastien Pédron 					 * break/continue/return in the
44401a43bcfSJean-Sébastien Pédron 					 * trap action to have an effect
44501a43bcfSJean-Sébastien Pédron 					 * outside of it.
44601a43bcfSJean-Sébastien Pédron 					 */
4473fe1119fSJilles Tjoelker 					if (evalskip == 0 ||
4483fe1119fSJilles Tjoelker 					    prev_evalskip != 0) {
44901a43bcfSJean-Sébastien Pédron 						evalskip  = prev_evalskip;
45001a43bcfSJean-Sébastien Pédron 						skipcount = prev_skipcount;
4513fe1119fSJilles Tjoelker 						exitstatus = savestatus;
45201a43bcfSJean-Sébastien Pédron 					}
45301a43bcfSJean-Sébastien Pédron 
454f98e1b80SSteve Price 					if (i == SIGCHLD)
455f98e1b80SSteve Price 						ignore_sigchld--;
4564b88c807SRodney W. Grimes 				}
457f98e1b80SSteve Price 				break;
458f98e1b80SSteve Price 			}
459f98e1b80SSteve Price 		}
460f98e1b80SSteve Price 		if (i >= NSIG)
461f98e1b80SSteve Price 			break;
462f98e1b80SSteve Price 	}
4637a8e920bSMartin Cracauer 	in_dotrap--;
4644b88c807SRodney W. Grimes }
4654b88c807SRodney W. Grimes 
4664b88c807SRodney W. Grimes 
4671cffe8b8SJilles Tjoelker void
trap_init(void)4681cffe8b8SJilles Tjoelker trap_init(void)
4691cffe8b8SJilles Tjoelker {
4701cffe8b8SJilles Tjoelker 	setsignal(SIGINT);
4711cffe8b8SJilles Tjoelker 	setsignal(SIGQUIT);
4721cffe8b8SJilles Tjoelker }
4731cffe8b8SJilles Tjoelker 
4741cffe8b8SJilles Tjoelker 
4754b88c807SRodney W. Grimes /*
4763be4e97dSJilles Tjoelker  * Controls whether the shell is interactive or not based on iflag.
4774b88c807SRodney W. Grimes  */
4784b88c807SRodney W. Grimes void
setinteractive(void)4793be4e97dSJilles Tjoelker setinteractive(void)
480aa9caaf6SPeter Wemm {
4814b88c807SRodney W. Grimes 	setsignal(SIGTERM);
4824b88c807SRodney W. Grimes }
4834b88c807SRodney W. Grimes 
4844b88c807SRodney W. Grimes 
4854b88c807SRodney W. Grimes /*
4864b88c807SRodney W. Grimes  * Called to exit the shell.
4874b88c807SRodney W. Grimes  */
4884b88c807SRodney W. Grimes void
exitshell(int status)4895134c3f7SWarner Losh exitshell(int status)
490aa9caaf6SPeter Wemm {
49170df11eaSJilles Tjoelker 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
49270df11eaSJilles Tjoelker 	exiting = 1;
49370df11eaSJilles Tjoelker 	exiting_exitstatus = status;
49470df11eaSJilles Tjoelker 	exitshell_savedstatus();
49570df11eaSJilles Tjoelker }
49670df11eaSJilles Tjoelker 
49770df11eaSJilles Tjoelker void
exitshell_savedstatus(void)49870df11eaSJilles Tjoelker exitshell_savedstatus(void)
49970df11eaSJilles Tjoelker {
5004b88c807SRodney W. Grimes 	struct jmploc loc1, loc2;
5014b88c807SRodney W. Grimes 	char *p;
502ebdfd6dcSJilles Tjoelker 	int sig = 0;
503ebdfd6dcSJilles Tjoelker 	sigset_t sigs;
5044b88c807SRodney W. Grimes 
505ebdfd6dcSJilles Tjoelker 	if (!exiting) {
506ebdfd6dcSJilles Tjoelker 		if (in_dotrap && last_trapsig) {
507ebdfd6dcSJilles Tjoelker 			sig = last_trapsig;
508ebdfd6dcSJilles Tjoelker 			exiting_exitstatus = sig + 128;
509ebdfd6dcSJilles Tjoelker 		} else
51070df11eaSJilles Tjoelker 			exiting_exitstatus = oexitstatus;
511ebdfd6dcSJilles Tjoelker 	}
51270df11eaSJilles Tjoelker 	exitstatus = oexitstatus = exiting_exitstatus;
51333c5acf0SJilles Tjoelker 	if (!setjmp(loc1.loc)) {
5144b88c807SRodney W. Grimes 		handler = &loc1;
5154b88c807SRodney W. Grimes 		if ((p = trap[0]) != NULL && *p != '\0') {
51601a43bcfSJean-Sébastien Pédron 			/*
51701a43bcfSJean-Sébastien Pédron 			 * Reset evalskip, or the trap on EXIT could be
51801a43bcfSJean-Sébastien Pédron 			 * interrupted if the last command was a "return".
51901a43bcfSJean-Sébastien Pédron 			 */
52001a43bcfSJean-Sébastien Pédron 			evalskip = 0;
5214b88c807SRodney W. Grimes 			trap[0] = NULL;
52255c2cd6fSJilles Tjoelker 			FORCEINTON;
523cb806389SStefan Farfeleder 			evalstring(p, 0);
5244b88c807SRodney W. Grimes 		}
52533c5acf0SJilles Tjoelker 	}
52633c5acf0SJilles Tjoelker 	if (!setjmp(loc2.loc)) {
52733c5acf0SJilles Tjoelker 		handler = &loc2;		/* probably unnecessary */
52855c2cd6fSJilles Tjoelker 		FORCEINTON;
5294b88c807SRodney W. Grimes 		flushall();
5304b88c807SRodney W. Grimes #if JOBS
5314b88c807SRodney W. Grimes 		setjobctl(0);
5324b88c807SRodney W. Grimes #endif
533988b1bb0SBaptiste Daroussin #ifndef NO_HISTORY
534988b1bb0SBaptiste Daroussin 		histsave();
535988b1bb0SBaptiste Daroussin #endif
53633c5acf0SJilles Tjoelker 	}
537ebdfd6dcSJilles Tjoelker 	if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN &&
538ebdfd6dcSJilles Tjoelker 	    sig != SIGTTOU) {
539ebdfd6dcSJilles Tjoelker 		signal(sig, SIG_DFL);
540ebdfd6dcSJilles Tjoelker 		sigemptyset(&sigs);
541ebdfd6dcSJilles Tjoelker 		sigaddset(&sigs, sig);
542ebdfd6dcSJilles Tjoelker 		sigprocmask(SIG_UNBLOCK, &sigs, NULL);
543ebdfd6dcSJilles Tjoelker 		kill(getpid(), sig);
544ebdfd6dcSJilles Tjoelker 		/* If the default action is to ignore, fall back to _exit(). */
545ebdfd6dcSJilles Tjoelker 	}
546ebdfd6dcSJilles Tjoelker 	_exit(exiting_exitstatus);
5474b88c807SRodney W. Grimes }
548