xref: /freebsd/bin/sh/trap.c (revision 5134c3f7990c353d2442ddd8a9a1d30b80099e43)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)trap.c	8.5 (Berkeley) 6/5/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <signal.h>
46aa9caaf6SPeter Wemm #include <unistd.h>
47aa9caaf6SPeter Wemm #include <stdlib.h>
48aa9caaf6SPeter Wemm 
494b88c807SRodney W. Grimes #include "shell.h"
504b88c807SRodney W. Grimes #include "main.h"
514b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
524b88c807SRodney W. Grimes #include "eval.h"
534b88c807SRodney W. Grimes #include "jobs.h"
54aa9caaf6SPeter Wemm #include "show.h"
554b88c807SRodney W. Grimes #include "options.h"
564b88c807SRodney W. Grimes #include "syntax.h"
574b88c807SRodney W. Grimes #include "output.h"
584b88c807SRodney W. Grimes #include "memalloc.h"
594b88c807SRodney W. Grimes #include "error.h"
604b88c807SRodney W. Grimes #include "trap.h"
614b88c807SRodney W. Grimes #include "mystring.h"
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes /*
654b88c807SRodney W. Grimes  * Sigmode records the current value of the signal handlers for the various
664b88c807SRodney W. Grimes  * modes.  A value of zero means that the current handler is not known.
674b88c807SRodney W. Grimes  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
684b88c807SRodney W. Grimes  */
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes #define S_DFL 1			/* default signal handling (SIG_DFL) */
714b88c807SRodney W. Grimes #define S_CATCH 2		/* signal is caught */
724b88c807SRodney W. Grimes #define S_IGN 3			/* signal is ignored (SIG_IGN) */
7346be34b9SKris Kennaway #define S_HARD_IGN 4		/* signal is ignored permanently */
744b88c807SRodney W. Grimes #define S_RESET 5		/* temporary - to reset a hard ignored sig */
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 
77aa9caaf6SPeter Wemm MKINIT char sigmode[NSIG];	/* current value of signal */
784b88c807SRodney W. Grimes int pendingsigs;		/* indicates some signal received */
791f40b47bSMartin Cracauer int in_dotrap;			/* do we execute in a trap handler? */
80135421ddSMartin Cracauer static char *volatile trap[NSIG];	/* trap handler commands */
817a8e920bSMartin Cracauer static volatile sig_atomic_t gotsig[NSIG];
827a8e920bSMartin Cracauer 				/* indicates specified signal received */
83f98e1b80SSteve Price static int ignore_sigchld;	/* Used while handling SIGCHLD traps. */
844b88c807SRodney W. Grimes 
855134c3f7SWarner Losh static int getsigaction(int, sig_t *);
86aa9caaf6SPeter Wemm 
87f98e1b80SSteve Price 
88f98e1b80SSteve Price /*
89f98e1b80SSteve Price  * Map a string to a signal number.
90f98e1b80SSteve Price  */
91f98e1b80SSteve Price static int
925134c3f7SWarner Losh sigstring_to_signum(char *sig)
93f98e1b80SSteve Price {
94f98e1b80SSteve Price 
95f98e1b80SSteve Price 	if (is_number(sig)) {
96f98e1b80SSteve Price 		int signo;
97f98e1b80SSteve Price 
98f98e1b80SSteve Price 		signo = atoi(sig);
99f98e1b80SSteve Price 		return ((signo >= 0 && signo < NSIG) ? signo : (-1));
100f98e1b80SSteve Price 	} else if (strcasecmp(sig, "exit") == 0) {
101f98e1b80SSteve Price 		return (0);
102f98e1b80SSteve Price 	} else {
103f98e1b80SSteve Price 		int n;
104f98e1b80SSteve Price 
105f98e1b80SSteve Price 		if (strncasecmp(sig, "sig", 3) == 0)
106f98e1b80SSteve Price 			sig += 3;
107f98e1b80SSteve Price 		for (n = 1; n < NSIG; n++)
108f98e1b80SSteve Price 			if (strcasecmp(sys_signame[n], sig) == 0)
109f98e1b80SSteve Price 				return (n);
110f98e1b80SSteve Price 	}
111f98e1b80SSteve Price 	return (-1);
112f98e1b80SSteve Price }
113f98e1b80SSteve Price 
114f98e1b80SSteve Price 
115f98e1b80SSteve Price /*
116f98e1b80SSteve Price  * Print a list of valid signal names.
117f98e1b80SSteve Price  */
118f98e1b80SSteve Price static void
1195134c3f7SWarner Losh printsignals(void)
120f98e1b80SSteve Price {
121f98e1b80SSteve Price 	int n;
122f98e1b80SSteve Price 
123f98e1b80SSteve Price 	for (n = 1; n < NSIG; n++) {
124f98e1b80SSteve Price 		out1fmt("%s", sys_signame[n]);
125f98e1b80SSteve Price 		if (n == (NSIG / 2) || n == (NSIG - 1))
126f98e1b80SSteve Price 			out1str("\n");
127f98e1b80SSteve Price 		else
128f98e1b80SSteve Price 			out1c(' ');
129f98e1b80SSteve Price 	}
130f98e1b80SSteve Price }
131f98e1b80SSteve Price 
132f98e1b80SSteve Price 
1334b88c807SRodney W. Grimes /*
1344b88c807SRodney W. Grimes  * The trap builtin.
1354b88c807SRodney W. Grimes  */
136aa9caaf6SPeter Wemm int
1375134c3f7SWarner Losh trapcmd(int argc, char **argv)
138aa9caaf6SPeter Wemm {
1394b88c807SRodney W. Grimes 	char *action;
1404b88c807SRodney W. Grimes 	int signo;
1414b88c807SRodney W. Grimes 
1424b88c807SRodney W. Grimes 	if (argc <= 1) {
143f98e1b80SSteve Price 		for (signo = 0 ; signo < NSIG ; signo++) {
1444b88c807SRodney W. Grimes 			if (trap[signo] != NULL)
145f98e1b80SSteve Price 				out1fmt("trap -- '%s' %s\n", trap[signo],
146f98e1b80SSteve Price 					(signo) ? sys_signame[signo] : "exit");
1474b88c807SRodney W. Grimes 		}
1484b88c807SRodney W. Grimes 		return 0;
1494b88c807SRodney W. Grimes 	}
1504b88c807SRodney W. Grimes 	action = NULL;
151f98e1b80SSteve Price 	if (*++argv && strcmp(*argv, "--") == 0)
152f98e1b80SSteve Price 		argv++;
153f98e1b80SSteve Price 	if (*argv && sigstring_to_signum(*argv) == -1) {
154f98e1b80SSteve Price 		if ((*argv)[0] != '-') {
155f98e1b80SSteve Price 			action = *argv;
156f98e1b80SSteve Price 			argv++;
157f98e1b80SSteve Price 		} else if ((*argv)[1] == '\0') {
158f98e1b80SSteve Price 			argv++;
159f98e1b80SSteve Price 		} else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
160f98e1b80SSteve Price 			printsignals();
161f98e1b80SSteve Price 			return 0;
162f98e1b80SSteve Price 		} else {
163f98e1b80SSteve Price 			error("bad option %s", *argv);
164f98e1b80SSteve Price 		}
165f98e1b80SSteve Price 	}
166f98e1b80SSteve Price 	while (*argv) {
167f98e1b80SSteve Price 		if ((signo = sigstring_to_signum(*argv)) == -1)
168f98e1b80SSteve Price 			error("bad signal %s", *argv);
1694b88c807SRodney W. Grimes 		INTOFF;
1704b88c807SRodney W. Grimes 		if (action)
1714b88c807SRodney W. Grimes 			action = savestr(action);
1724b88c807SRodney W. Grimes 		if (trap[signo])
1734b88c807SRodney W. Grimes 			ckfree(trap[signo]);
1744b88c807SRodney W. Grimes 		trap[signo] = action;
1754b88c807SRodney W. Grimes 		if (signo != 0)
1764b88c807SRodney W. Grimes 			setsignal(signo);
1774b88c807SRodney W. Grimes 		INTON;
178f98e1b80SSteve Price 		argv++;
1794b88c807SRodney W. Grimes 	}
1804b88c807SRodney W. Grimes 	return 0;
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes 
1844b88c807SRodney W. Grimes /*
1854b88c807SRodney W. Grimes  * Clear traps on a fork.
1864b88c807SRodney W. Grimes  */
1874b88c807SRodney W. Grimes void
1885134c3f7SWarner Losh clear_traps(void)
189f98e1b80SSteve Price {
190135421ddSMartin Cracauer 	char *volatile *tp;
1914b88c807SRodney W. Grimes 
192f98e1b80SSteve Price 	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
1934b88c807SRodney W. Grimes 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
1944b88c807SRodney W. Grimes 			INTOFF;
1954b88c807SRodney W. Grimes 			ckfree(*tp);
1964b88c807SRodney W. Grimes 			*tp = NULL;
1974b88c807SRodney W. Grimes 			if (tp != &trap[0])
1984b88c807SRodney W. Grimes 				setsignal(tp - trap);
1994b88c807SRodney W. Grimes 			INTON;
2004b88c807SRodney W. Grimes 		}
2014b88c807SRodney W. Grimes 	}
2024b88c807SRodney W. Grimes }
2034b88c807SRodney W. Grimes 
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes /*
2064b88c807SRodney W. Grimes  * Set the signal handler for the specified signal.  The routine figures
2074b88c807SRodney W. Grimes  * out what it should be set to.
2084b88c807SRodney W. Grimes  */
20928701136SBruce Evans void
2105134c3f7SWarner Losh setsignal(int signo)
211aa9caaf6SPeter Wemm {
2124b88c807SRodney W. Grimes 	int action;
21328701136SBruce Evans 	sig_t sig, sigact = SIG_DFL;
2144b88c807SRodney W. Grimes 	char *t;
2154b88c807SRodney W. Grimes 
2164b88c807SRodney W. Grimes 	if ((t = trap[signo]) == NULL)
2174b88c807SRodney W. Grimes 		action = S_DFL;
2184b88c807SRodney W. Grimes 	else if (*t != '\0')
2194b88c807SRodney W. Grimes 		action = S_CATCH;
2204b88c807SRodney W. Grimes 	else
2214b88c807SRodney W. Grimes 		action = S_IGN;
2227a8e920bSMartin Cracauer 	if (action == S_DFL) {
2234b88c807SRodney W. Grimes 		switch (signo) {
2244b88c807SRodney W. Grimes 		case SIGINT:
2254b88c807SRodney W. Grimes 			action = S_CATCH;
2264b88c807SRodney W. Grimes 			break;
2274b88c807SRodney W. Grimes 		case SIGQUIT:
2284b88c807SRodney W. Grimes #ifdef DEBUG
2294b88c807SRodney W. Grimes 			{
2304b88c807SRodney W. Grimes 			extern int debug;
2314b88c807SRodney W. Grimes 
2324b88c807SRodney W. Grimes 			if (debug)
2334b88c807SRodney W. Grimes 				break;
2344b88c807SRodney W. Grimes 			}
2354b88c807SRodney W. Grimes #endif
23640d009a7SMartin Cracauer 			action = S_CATCH;
2377a8e920bSMartin Cracauer 			break;
2384b88c807SRodney W. Grimes 		case SIGTERM:
2397a8e920bSMartin Cracauer 			if (rootshell && iflag)
2404b88c807SRodney W. Grimes 				action = S_IGN;
2414b88c807SRodney W. Grimes 			break;
2424b88c807SRodney W. Grimes #if JOBS
2434b88c807SRodney W. Grimes 		case SIGTSTP:
2444b88c807SRodney W. Grimes 		case SIGTTOU:
2457a8e920bSMartin Cracauer 			if (rootshell && mflag)
2464b88c807SRodney W. Grimes 				action = S_IGN;
2474b88c807SRodney W. Grimes 			break;
2484b88c807SRodney W. Grimes #endif
2494b88c807SRodney W. Grimes 		}
2504b88c807SRodney W. Grimes 	}
251aa9caaf6SPeter Wemm 
252f98e1b80SSteve Price 	t = &sigmode[signo];
2534b88c807SRodney W. Grimes 	if (*t == 0) {
2544b88c807SRodney W. Grimes 		/*
2554b88c807SRodney W. Grimes 		 * current setting unknown
2564b88c807SRodney W. Grimes 		 */
257aa9caaf6SPeter Wemm 		if (!getsigaction(signo, &sigact)) {
258aa9caaf6SPeter Wemm 			/*
259aa9caaf6SPeter Wemm 			 * Pretend it worked; maybe we should give a warning
260aa9caaf6SPeter Wemm 			 * here, but other shells don't. We don't alter
261aa9caaf6SPeter Wemm 			 * sigmode, so that we retry every time.
262aa9caaf6SPeter Wemm 			 */
26328701136SBruce Evans 			return;
264aa9caaf6SPeter Wemm 		}
2654b88c807SRodney W. Grimes 		if (sigact == SIG_IGN) {
2664b88c807SRodney W. Grimes 			if (mflag && (signo == SIGTSTP ||
2674b88c807SRodney W. Grimes 			     signo == SIGTTIN || signo == SIGTTOU)) {
2684b88c807SRodney W. Grimes 				*t = S_IGN;	/* don't hard ignore these */
2694b88c807SRodney W. Grimes 			} else
2704b88c807SRodney W. Grimes 				*t = S_HARD_IGN;
2714b88c807SRodney W. Grimes 		} else {
2724b88c807SRodney W. Grimes 			*t = S_RESET;	/* force to be set */
2734b88c807SRodney W. Grimes 		}
2744b88c807SRodney W. Grimes 	}
2754b88c807SRodney W. Grimes 	if (*t == S_HARD_IGN || *t == action)
27628701136SBruce Evans 		return;
2774b88c807SRodney W. Grimes 	switch (action) {
2784b88c807SRodney W. Grimes 		case S_DFL:	sigact = SIG_DFL;	break;
2794b88c807SRodney W. Grimes 		case S_CATCH:  	sigact = onsig;		break;
2804b88c807SRodney W. Grimes 		case S_IGN:	sigact = SIG_IGN;	break;
2814b88c807SRodney W. Grimes 	}
2824b88c807SRodney W. Grimes 	*t = action;
28328701136SBruce Evans 	sig = signal(signo, sigact);
2848dd81503SAndrey A. Chernov #ifdef BSD
28528701136SBruce Evans 	if (sig != SIG_ERR && action == S_CATCH)
28628701136SBruce Evans 		siginterrupt(signo, 1);
2878dd81503SAndrey A. Chernov #endif
2884b88c807SRodney W. Grimes }
2894b88c807SRodney W. Grimes 
290f98e1b80SSteve Price 
2914b88c807SRodney W. Grimes /*
2924b88c807SRodney W. Grimes  * Return the current setting for sig w/o changing it.
2934b88c807SRodney W. Grimes  */
294aa9caaf6SPeter Wemm static int
2955134c3f7SWarner Losh getsigaction(int signo, sig_t *sigact)
296aa9caaf6SPeter Wemm {
2974b88c807SRodney W. Grimes 	struct sigaction sa;
2984b88c807SRodney W. Grimes 
2994b88c807SRodney W. Grimes 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
300aa9caaf6SPeter Wemm 		return 0;
301aa9caaf6SPeter Wemm 	*sigact = (sig_t) sa.sa_handler;
302aa9caaf6SPeter Wemm 	return 1;
3034b88c807SRodney W. Grimes }
3044b88c807SRodney W. Grimes 
305f98e1b80SSteve Price 
3064b88c807SRodney W. Grimes /*
3074b88c807SRodney W. Grimes  * Ignore a signal.
3084b88c807SRodney W. Grimes  */
3094b88c807SRodney W. Grimes void
3105134c3f7SWarner Losh ignoresig(int signo)
311aa9caaf6SPeter Wemm {
312f98e1b80SSteve Price 
313f98e1b80SSteve Price 	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
3144b88c807SRodney W. Grimes 		signal(signo, SIG_IGN);
3154b88c807SRodney W. Grimes 	}
316f98e1b80SSteve Price 	sigmode[signo] = S_HARD_IGN;
3174b88c807SRodney W. Grimes }
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes #ifdef mkinit
321aa9caaf6SPeter Wemm INCLUDE <signal.h>
3224b88c807SRodney W. Grimes INCLUDE "trap.h"
3234b88c807SRodney W. Grimes 
3244b88c807SRodney W. Grimes SHELLPROC {
3254b88c807SRodney W. Grimes 	char *sm;
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes 	clear_traps();
328aa9caaf6SPeter Wemm 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
3294b88c807SRodney W. Grimes 		if (*sm == S_IGN)
3304b88c807SRodney W. Grimes 			*sm = S_HARD_IGN;
3314b88c807SRodney W. Grimes 	}
3324b88c807SRodney W. Grimes }
3334b88c807SRodney W. Grimes #endif
3344b88c807SRodney W. Grimes 
3354b88c807SRodney W. Grimes 
3364b88c807SRodney W. Grimes /*
3374b88c807SRodney W. Grimes  * Signal handler.
3384b88c807SRodney W. Grimes  */
3394b88c807SRodney W. Grimes void
3405134c3f7SWarner Losh onsig(int signo)
341aa9caaf6SPeter Wemm {
34228701136SBruce Evans 
3438dd81503SAndrey A. Chernov #ifndef BSD
3444b88c807SRodney W. Grimes 	signal(signo, onsig);
3458dd81503SAndrey A. Chernov #endif
3464b88c807SRodney W. Grimes 	if (signo == SIGINT && trap[SIGINT] == NULL) {
3474b88c807SRodney W. Grimes 		onint();
3484b88c807SRodney W. Grimes 		return;
3494b88c807SRodney W. Grimes 	}
350135421ddSMartin Cracauer 
351f98e1b80SSteve Price 	if (signo != SIGCHLD || !ignore_sigchld)
352f98e1b80SSteve Price 		gotsig[signo] = 1;
3534b88c807SRodney W. Grimes 	pendingsigs++;
354135421ddSMartin Cracauer 
355135421ddSMartin Cracauer 	/* If we are currently in a wait builtin, prepare to break it */
356135421ddSMartin Cracauer 	if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
3577a8e920bSMartin Cracauer 		breakwaitcmd = 1;
358135421ddSMartin Cracauer 	/*
3599bbfa415SMartin Cracauer 	 * If a trap is set, not ignored and not the null command, we need
3609bbfa415SMartin Cracauer 	 * to make sure traps are executed even when a child blocks signals.
361135421ddSMartin Cracauer 	 */
362c1c72a3cSMartin Cracauer 	if (Tflag &&
363c1c72a3cSMartin Cracauer 	    trap[signo] != NULL &&
3649bbfa415SMartin Cracauer 	    ! trap[signo][0] == '\0' &&
365e6ac45ddSMartin Cracauer 	    ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
366135421ddSMartin Cracauer 		breakwaitcmd = 1;
3674b88c807SRodney W. Grimes }
3684b88c807SRodney W. Grimes 
3694b88c807SRodney W. Grimes 
3704b88c807SRodney W. Grimes /*
3714b88c807SRodney W. Grimes  * Called to execute a trap.  Perhaps we should avoid entering new trap
3724b88c807SRodney W. Grimes  * handlers while we are executing a trap handler.
3734b88c807SRodney W. Grimes  */
3744b88c807SRodney W. Grimes void
3755134c3f7SWarner Losh dotrap(void)
376f98e1b80SSteve Price {
3774b88c807SRodney W. Grimes 	int i;
3784b88c807SRodney W. Grimes 	int savestatus;
3794b88c807SRodney W. Grimes 
3807a8e920bSMartin Cracauer 	in_dotrap++;
3814b88c807SRodney W. Grimes 	for (;;) {
382f98e1b80SSteve Price 		for (i = 1; i < NSIG; i++) {
383f98e1b80SSteve Price 			if (gotsig[i]) {
384f98e1b80SSteve Price 				gotsig[i] = 0;
385f98e1b80SSteve Price 				if (trap[i]) {
386f98e1b80SSteve Price 					/*
387f98e1b80SSteve Price 					 * Ignore SIGCHLD to avoid infinite recursion
388f98e1b80SSteve Price 					 * if the trap action does a fork.
389f98e1b80SSteve Price 					 */
390f98e1b80SSteve Price 					if (i == SIGCHLD)
391f98e1b80SSteve Price 						ignore_sigchld++;
3924b88c807SRodney W. Grimes 					savestatus = exitstatus;
3934b88c807SRodney W. Grimes 					evalstring(trap[i]);
3944b88c807SRodney W. Grimes 					exitstatus = savestatus;
395f98e1b80SSteve Price 					if (i == SIGCHLD)
396f98e1b80SSteve Price 						ignore_sigchld--;
3974b88c807SRodney W. Grimes 				}
398f98e1b80SSteve Price 				break;
399f98e1b80SSteve Price 			}
400f98e1b80SSteve Price 		}
401f98e1b80SSteve Price 		if (i >= NSIG)
402f98e1b80SSteve Price 			break;
403f98e1b80SSteve Price 	}
4047a8e920bSMartin Cracauer 	in_dotrap--;
4054b88c807SRodney W. Grimes 	pendingsigs = 0;
4064b88c807SRodney W. Grimes }
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 
4094b88c807SRodney W. Grimes /*
4104b88c807SRodney W. Grimes  * Controls whether the shell is interactive or not.
4114b88c807SRodney W. Grimes  */
4124b88c807SRodney W. Grimes void
4135134c3f7SWarner Losh setinteractive(int on)
414aa9caaf6SPeter Wemm {
4157a8e920bSMartin Cracauer 	static int is_interactive = -1;
4164b88c807SRodney W. Grimes 
4174b88c807SRodney W. Grimes 	if (on == is_interactive)
4184b88c807SRodney W. Grimes 		return;
4194b88c807SRodney W. Grimes 	setsignal(SIGINT);
4204b88c807SRodney W. Grimes 	setsignal(SIGQUIT);
4214b88c807SRodney W. Grimes 	setsignal(SIGTERM);
4224b88c807SRodney W. Grimes 	is_interactive = on;
4234b88c807SRodney W. Grimes }
4244b88c807SRodney W. Grimes 
4254b88c807SRodney W. Grimes 
4264b88c807SRodney W. Grimes /*
4274b88c807SRodney W. Grimes  * Called to exit the shell.
4284b88c807SRodney W. Grimes  */
4294b88c807SRodney W. Grimes void
4305134c3f7SWarner Losh exitshell(int status)
431aa9caaf6SPeter Wemm {
4324b88c807SRodney W. Grimes 	struct jmploc loc1, loc2;
4334b88c807SRodney W. Grimes 	char *p;
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
4364b88c807SRodney W. Grimes 	if (setjmp(loc1.loc)) {
4374b88c807SRodney W. Grimes 		goto l1;
4384b88c807SRodney W. Grimes 	}
4394b88c807SRodney W. Grimes 	if (setjmp(loc2.loc)) {
4404b88c807SRodney W. Grimes 		goto l2;
4414b88c807SRodney W. Grimes 	}
4424b88c807SRodney W. Grimes 	handler = &loc1;
4434b88c807SRodney W. Grimes 	if ((p = trap[0]) != NULL && *p != '\0') {
4444b88c807SRodney W. Grimes 		trap[0] = NULL;
4454b88c807SRodney W. Grimes 		evalstring(p);
4464b88c807SRodney W. Grimes 	}
4474b88c807SRodney W. Grimes l1:   handler = &loc2;			/* probably unnecessary */
4484b88c807SRodney W. Grimes 	flushall();
4494b88c807SRodney W. Grimes #if JOBS
4504b88c807SRodney W. Grimes 	setjobctl(0);
4514b88c807SRodney W. Grimes #endif
4524b88c807SRodney W. Grimes l2:   _exit(status);
4534b88c807SRodney W. Grimes }
454