xref: /freebsd/bin/sh/trap.c (revision 3d7b5b9302e5c79bc5dffd599d6d17dcd9592b76)
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[] =
423d7b5b93SPhilippe Charnier 	"$Id$";
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) */
734b88c807SRodney W. Grimes #define S_HARD_IGN 4		/* signal is ignored permenantly */
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 */
79f98e1b80SSteve Price static char *trap[NSIG];	/* trap handler commands */
80f98e1b80SSteve Price static char gotsig[NSIG];	/* indicates specified signal received */
81f98e1b80SSteve Price static int ignore_sigchld;	/* Used while handling SIGCHLD traps. */
824b88c807SRodney W. Grimes 
83aa9caaf6SPeter Wemm static int getsigaction __P((int, sig_t *));
84aa9caaf6SPeter Wemm 
85f98e1b80SSteve Price 
86f98e1b80SSteve Price /*
87f98e1b80SSteve Price  * Map a string to a signal number.
88f98e1b80SSteve Price  */
89f98e1b80SSteve Price static int
90f98e1b80SSteve Price sigstring_to_signum(sig)
91f98e1b80SSteve Price 	char *sig;
92f98e1b80SSteve Price {
93f98e1b80SSteve Price 
94f98e1b80SSteve Price 	if (is_number(sig)) {
95f98e1b80SSteve Price 		int signo;
96f98e1b80SSteve Price 
97f98e1b80SSteve Price 		signo = atoi(sig);
98f98e1b80SSteve Price 		return ((signo >= 0 && signo < NSIG) ? signo : (-1));
99f98e1b80SSteve Price 	} else if (strcasecmp(sig, "exit") == 0) {
100f98e1b80SSteve Price 		return (0);
101f98e1b80SSteve Price 	} else {
102f98e1b80SSteve Price 		int n;
103f98e1b80SSteve Price 
104f98e1b80SSteve Price 		if (strncasecmp(sig, "sig", 3) == 0)
105f98e1b80SSteve Price 			sig += 3;
106f98e1b80SSteve Price 		for (n = 1; n < NSIG; n++)
107f98e1b80SSteve Price 			if (strcasecmp(sys_signame[n], sig) == 0)
108f98e1b80SSteve Price 				return (n);
109f98e1b80SSteve Price 	}
110f98e1b80SSteve Price 	return (-1);
111f98e1b80SSteve Price }
112f98e1b80SSteve Price 
113f98e1b80SSteve Price 
114f98e1b80SSteve Price /*
115f98e1b80SSteve Price  * Print a list of valid signal names.
116f98e1b80SSteve Price  */
117f98e1b80SSteve Price static void
118f98e1b80SSteve Price printsignals()
119f98e1b80SSteve Price {
120f98e1b80SSteve Price 	int n;
121f98e1b80SSteve Price 
122f98e1b80SSteve Price 	for (n = 1; n < NSIG; n++) {
123f98e1b80SSteve Price 		out1fmt("%s", sys_signame[n]);
124f98e1b80SSteve Price 		if (n == (NSIG / 2) || n == (NSIG - 1))
125f98e1b80SSteve Price 			out1str("\n");
126f98e1b80SSteve Price 		else
127f98e1b80SSteve Price 			out1c(' ');
128f98e1b80SSteve Price 	}
129f98e1b80SSteve Price }
130f98e1b80SSteve Price 
131f98e1b80SSteve Price 
1324b88c807SRodney W. Grimes /*
1334b88c807SRodney W. Grimes  * The trap builtin.
1344b88c807SRodney W. Grimes  */
135aa9caaf6SPeter Wemm int
136aa9caaf6SPeter Wemm trapcmd(argc, argv)
137aa9caaf6SPeter Wemm 	int argc;
138aa9caaf6SPeter Wemm 	char **argv;
139aa9caaf6SPeter Wemm {
1404b88c807SRodney W. Grimes 	char *action;
1414b88c807SRodney W. Grimes 	int signo;
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes 	if (argc <= 1) {
144f98e1b80SSteve Price 		for (signo = 0 ; signo < NSIG ; signo++) {
1454b88c807SRodney W. Grimes 			if (trap[signo] != NULL)
146f98e1b80SSteve Price 				out1fmt("trap -- '%s' %s\n", trap[signo],
147f98e1b80SSteve Price 					(signo) ? sys_signame[signo] : "exit");
1484b88c807SRodney W. Grimes 		}
1494b88c807SRodney W. Grimes 		return 0;
1504b88c807SRodney W. Grimes 	}
1514b88c807SRodney W. Grimes 	action = NULL;
152f98e1b80SSteve Price 	if (*++argv && strcmp(*argv, "--") == 0)
153f98e1b80SSteve Price 		argv++;
154f98e1b80SSteve Price 	if (*argv && sigstring_to_signum(*argv) == -1) {
155f98e1b80SSteve Price 		if ((*argv)[0] != '-') {
156f98e1b80SSteve Price 			action = *argv;
157f98e1b80SSteve Price 			argv++;
158f98e1b80SSteve Price 		} else if ((*argv)[1] == '\0') {
159f98e1b80SSteve Price 			argv++;
160f98e1b80SSteve Price 		} else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
161f98e1b80SSteve Price 			printsignals();
162f98e1b80SSteve Price 			return 0;
163f98e1b80SSteve Price 		} else {
164f98e1b80SSteve Price 			error("bad option %s", *argv);
165f98e1b80SSteve Price 		}
166f98e1b80SSteve Price 	}
167f98e1b80SSteve Price 	while (*argv) {
168f98e1b80SSteve Price 		if ((signo = sigstring_to_signum(*argv)) == -1)
169f98e1b80SSteve Price 			error("bad signal %s", *argv);
1704b88c807SRodney W. Grimes 		INTOFF;
1714b88c807SRodney W. Grimes 		if (action)
1724b88c807SRodney W. Grimes 			action = savestr(action);
1734b88c807SRodney W. Grimes 		if (trap[signo])
1744b88c807SRodney W. Grimes 			ckfree(trap[signo]);
1754b88c807SRodney W. Grimes 		trap[signo] = action;
1764b88c807SRodney W. Grimes 		if (signo != 0)
1774b88c807SRodney W. Grimes 			setsignal(signo);
1784b88c807SRodney W. Grimes 		INTON;
179f98e1b80SSteve Price 		argv++;
1804b88c807SRodney W. Grimes 	}
1814b88c807SRodney W. Grimes 	return 0;
1824b88c807SRodney W. Grimes }
1834b88c807SRodney W. Grimes 
1844b88c807SRodney W. Grimes 
1854b88c807SRodney W. Grimes /*
1864b88c807SRodney W. Grimes  * Clear traps on a fork.
1874b88c807SRodney W. Grimes  */
1884b88c807SRodney W. Grimes void
189f98e1b80SSteve Price clear_traps()
190f98e1b80SSteve Price {
1914b88c807SRodney W. Grimes 	char **tp;
1924b88c807SRodney W. Grimes 
193f98e1b80SSteve Price 	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
1944b88c807SRodney W. Grimes 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
1954b88c807SRodney W. Grimes 			INTOFF;
1964b88c807SRodney W. Grimes 			ckfree(*tp);
1974b88c807SRodney W. Grimes 			*tp = NULL;
1984b88c807SRodney W. Grimes 			if (tp != &trap[0])
1994b88c807SRodney W. Grimes 				setsignal(tp - trap);
2004b88c807SRodney W. Grimes 			INTON;
2014b88c807SRodney W. Grimes 		}
2024b88c807SRodney W. Grimes 	}
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes /*
2074b88c807SRodney W. Grimes  * Set the signal handler for the specified signal.  The routine figures
2084b88c807SRodney W. Grimes  * out what it should be set to.
2094b88c807SRodney W. Grimes  */
21028701136SBruce Evans void
211aa9caaf6SPeter Wemm setsignal(signo)
212aa9caaf6SPeter Wemm 	int signo;
213aa9caaf6SPeter Wemm {
2144b88c807SRodney W. Grimes 	int action;
21528701136SBruce Evans 	sig_t sig, sigact = SIG_DFL;
2164b88c807SRodney W. Grimes 	char *t;
2174b88c807SRodney W. Grimes 
2184b88c807SRodney W. Grimes 	if ((t = trap[signo]) == NULL)
2194b88c807SRodney W. Grimes 		action = S_DFL;
2204b88c807SRodney W. Grimes 	else if (*t != '\0')
2214b88c807SRodney W. Grimes 		action = S_CATCH;
2224b88c807SRodney W. Grimes 	else
2234b88c807SRodney W. Grimes 		action = S_IGN;
2244b88c807SRodney W. Grimes 	if (rootshell && action == S_DFL) {
2254b88c807SRodney W. Grimes 		switch (signo) {
2264b88c807SRodney W. Grimes 		case SIGINT:
2274b88c807SRodney W. Grimes 			if (iflag)
2284b88c807SRodney W. Grimes 				action = S_CATCH;
2294b88c807SRodney W. Grimes 			break;
2304b88c807SRodney W. Grimes 		case SIGQUIT:
2314b88c807SRodney W. Grimes #ifdef DEBUG
2324b88c807SRodney W. Grimes 			{
2334b88c807SRodney W. Grimes 			extern int debug;
2344b88c807SRodney W. Grimes 
2354b88c807SRodney W. Grimes 			if (debug)
2364b88c807SRodney W. Grimes 				break;
2374b88c807SRodney W. Grimes 			}
2384b88c807SRodney W. Grimes #endif
2394b88c807SRodney W. Grimes 			/* FALLTHROUGH */
2404b88c807SRodney W. Grimes 		case SIGTERM:
2414b88c807SRodney W. Grimes 			if (iflag)
2424b88c807SRodney W. Grimes 				action = S_IGN;
2434b88c807SRodney W. Grimes 			break;
2444b88c807SRodney W. Grimes #if JOBS
2454b88c807SRodney W. Grimes 		case SIGTSTP:
2464b88c807SRodney W. Grimes 		case SIGTTOU:
2474b88c807SRodney W. Grimes 			if (mflag)
2484b88c807SRodney W. Grimes 				action = S_IGN;
2494b88c807SRodney W. Grimes 			break;
2504b88c807SRodney W. Grimes #endif
2514b88c807SRodney W. Grimes 		}
2524b88c807SRodney W. Grimes 	}
253aa9caaf6SPeter Wemm 
254f98e1b80SSteve Price 	t = &sigmode[signo];
2554b88c807SRodney W. Grimes 	if (*t == 0) {
2564b88c807SRodney W. Grimes 		/*
2574b88c807SRodney W. Grimes 		 * current setting unknown
2584b88c807SRodney W. Grimes 		 */
259aa9caaf6SPeter Wemm 		if (!getsigaction(signo, &sigact)) {
260aa9caaf6SPeter Wemm 			/*
261aa9caaf6SPeter Wemm 			 * Pretend it worked; maybe we should give a warning
262aa9caaf6SPeter Wemm 			 * here, but other shells don't. We don't alter
263aa9caaf6SPeter Wemm 			 * sigmode, so that we retry every time.
264aa9caaf6SPeter Wemm 			 */
26528701136SBruce Evans 			return;
266aa9caaf6SPeter Wemm 		}
2674b88c807SRodney W. Grimes 		if (sigact == SIG_IGN) {
2684b88c807SRodney W. Grimes 			if (mflag && (signo == SIGTSTP ||
2694b88c807SRodney W. Grimes 			     signo == SIGTTIN || signo == SIGTTOU)) {
2704b88c807SRodney W. Grimes 				*t = S_IGN;	/* don't hard ignore these */
2714b88c807SRodney W. Grimes 			} else
2724b88c807SRodney W. Grimes 				*t = S_HARD_IGN;
2734b88c807SRodney W. Grimes 		} else {
2744b88c807SRodney W. Grimes 			*t = S_RESET;	/* force to be set */
2754b88c807SRodney W. Grimes 		}
2764b88c807SRodney W. Grimes 	}
2774b88c807SRodney W. Grimes 	if (*t == S_HARD_IGN || *t == action)
27828701136SBruce Evans 		return;
2794b88c807SRodney W. Grimes 	switch (action) {
2804b88c807SRodney W. Grimes 		case S_DFL:	sigact = SIG_DFL;	break;
2814b88c807SRodney W. Grimes 		case S_CATCH:  	sigact = onsig;		break;
2824b88c807SRodney W. Grimes 		case S_IGN:	sigact = SIG_IGN;	break;
2834b88c807SRodney W. Grimes 	}
2844b88c807SRodney W. Grimes 	*t = action;
28528701136SBruce Evans 	sig = signal(signo, sigact);
2868dd81503SAndrey A. Chernov #ifdef BSD
28728701136SBruce Evans 	if (sig != SIG_ERR && action == S_CATCH)
28828701136SBruce Evans 		siginterrupt(signo, 1);
2898dd81503SAndrey A. Chernov #endif
2904b88c807SRodney W. Grimes }
2914b88c807SRodney W. Grimes 
292f98e1b80SSteve Price 
2934b88c807SRodney W. Grimes /*
2944b88c807SRodney W. Grimes  * Return the current setting for sig w/o changing it.
2954b88c807SRodney W. Grimes  */
296aa9caaf6SPeter Wemm static int
297aa9caaf6SPeter Wemm getsigaction(signo, sigact)
298aa9caaf6SPeter Wemm 	int signo;
299aa9caaf6SPeter Wemm 	sig_t *sigact;
300aa9caaf6SPeter Wemm {
3014b88c807SRodney W. Grimes 	struct sigaction sa;
3024b88c807SRodney W. Grimes 
3034b88c807SRodney W. Grimes 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
304aa9caaf6SPeter Wemm 		return 0;
305aa9caaf6SPeter Wemm 	*sigact = (sig_t) sa.sa_handler;
306aa9caaf6SPeter Wemm 	return 1;
3074b88c807SRodney W. Grimes }
3084b88c807SRodney W. Grimes 
309f98e1b80SSteve Price 
3104b88c807SRodney W. Grimes /*
3114b88c807SRodney W. Grimes  * Ignore a signal.
3124b88c807SRodney W. Grimes  */
3134b88c807SRodney W. Grimes void
314aa9caaf6SPeter Wemm ignoresig(signo)
315aa9caaf6SPeter Wemm 	int signo;
316aa9caaf6SPeter Wemm {
317f98e1b80SSteve Price 
318f98e1b80SSteve Price 	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
3194b88c807SRodney W. Grimes 		signal(signo, SIG_IGN);
3204b88c807SRodney W. Grimes 	}
321f98e1b80SSteve Price 	sigmode[signo] = S_HARD_IGN;
3224b88c807SRodney W. Grimes }
3234b88c807SRodney W. Grimes 
3244b88c807SRodney W. Grimes 
3254b88c807SRodney W. Grimes #ifdef mkinit
326aa9caaf6SPeter Wemm INCLUDE <signal.h>
3274b88c807SRodney W. Grimes INCLUDE "trap.h"
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes SHELLPROC {
3304b88c807SRodney W. Grimes 	char *sm;
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes 	clear_traps();
333aa9caaf6SPeter Wemm 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
3344b88c807SRodney W. Grimes 		if (*sm == S_IGN)
3354b88c807SRodney W. Grimes 			*sm = S_HARD_IGN;
3364b88c807SRodney W. Grimes 	}
3374b88c807SRodney W. Grimes }
3384b88c807SRodney W. Grimes #endif
3394b88c807SRodney W. Grimes 
3404b88c807SRodney W. Grimes 
3414b88c807SRodney W. Grimes /*
3424b88c807SRodney W. Grimes  * Signal handler.
3434b88c807SRodney W. Grimes  */
3444b88c807SRodney W. Grimes void
345aa9caaf6SPeter Wemm onsig(signo)
346aa9caaf6SPeter Wemm 	int signo;
347aa9caaf6SPeter Wemm {
34828701136SBruce Evans 
3498dd81503SAndrey A. Chernov #ifndef BSD
3504b88c807SRodney W. Grimes 	signal(signo, onsig);
3518dd81503SAndrey A. Chernov #endif
3524b88c807SRodney W. Grimes 	if (signo == SIGINT && trap[SIGINT] == NULL) {
3534b88c807SRodney W. Grimes 		onint();
3544b88c807SRodney W. Grimes 		return;
3554b88c807SRodney W. Grimes 	}
356f98e1b80SSteve Price 	if (signo != SIGCHLD || !ignore_sigchld)
357f98e1b80SSteve Price 		gotsig[signo] = 1;
3584b88c807SRodney W. Grimes 	pendingsigs++;
3594b88c807SRodney W. Grimes }
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes 
3624b88c807SRodney W. Grimes /*
3634b88c807SRodney W. Grimes  * Called to execute a trap.  Perhaps we should avoid entering new trap
3644b88c807SRodney W. Grimes  * handlers while we are executing a trap handler.
3654b88c807SRodney W. Grimes  */
3664b88c807SRodney W. Grimes void
367f98e1b80SSteve Price dotrap()
368f98e1b80SSteve Price {
3694b88c807SRodney W. Grimes 	int i;
3704b88c807SRodney W. Grimes 	int savestatus;
3714b88c807SRodney W. Grimes 
3724b88c807SRodney W. Grimes 	for (;;) {
373f98e1b80SSteve Price 		for (i = 1; i < NSIG; i++) {
374f98e1b80SSteve Price 			if (gotsig[i]) {
375f98e1b80SSteve Price 				gotsig[i] = 0;
376f98e1b80SSteve Price 				if (trap[i]) {
377f98e1b80SSteve Price 					/*
378f98e1b80SSteve Price 					 * Ignore SIGCHLD to avoid infinite recursion
379f98e1b80SSteve Price 					 * if the trap action does a fork.
380f98e1b80SSteve Price 					 */
381f98e1b80SSteve Price 					if (i == SIGCHLD)
382f98e1b80SSteve Price 						ignore_sigchld++;
3834b88c807SRodney W. Grimes 					savestatus = exitstatus;
3844b88c807SRodney W. Grimes 					evalstring(trap[i]);
3854b88c807SRodney W. Grimes 					exitstatus = savestatus;
386f98e1b80SSteve Price 					if (i == SIGCHLD)
387f98e1b80SSteve Price 						ignore_sigchld--;
3884b88c807SRodney W. Grimes 				}
389f98e1b80SSteve Price 				break;
390f98e1b80SSteve Price 			}
391f98e1b80SSteve Price 		}
392f98e1b80SSteve Price 		if (i >= NSIG)
393f98e1b80SSteve Price 			break;
394f98e1b80SSteve Price 	}
3954b88c807SRodney W. Grimes 	pendingsigs = 0;
3964b88c807SRodney W. Grimes }
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes 
3994b88c807SRodney W. Grimes /*
4004b88c807SRodney W. Grimes  * Controls whether the shell is interactive or not.
4014b88c807SRodney W. Grimes  */
4024b88c807SRodney W. Grimes void
403aa9caaf6SPeter Wemm setinteractive(on)
404aa9caaf6SPeter Wemm 	int on;
405aa9caaf6SPeter Wemm {
406ab0a2172SSteve Price 	static int is_interactive = 0;
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 	if (on == is_interactive)
4094b88c807SRodney W. Grimes 		return;
4104b88c807SRodney W. Grimes 	setsignal(SIGINT);
4114b88c807SRodney W. Grimes 	setsignal(SIGQUIT);
4124b88c807SRodney W. Grimes 	setsignal(SIGTERM);
4134b88c807SRodney W. Grimes 	is_interactive = on;
4144b88c807SRodney W. Grimes }
4154b88c807SRodney W. Grimes 
4164b88c807SRodney W. Grimes 
4174b88c807SRodney W. Grimes /*
4184b88c807SRodney W. Grimes  * Called to exit the shell.
4194b88c807SRodney W. Grimes  */
4204b88c807SRodney W. Grimes void
421aa9caaf6SPeter Wemm exitshell(status)
422aa9caaf6SPeter Wemm 	int status;
423aa9caaf6SPeter Wemm {
4244b88c807SRodney W. Grimes 	struct jmploc loc1, loc2;
4254b88c807SRodney W. Grimes 	char *p;
4264b88c807SRodney W. Grimes 
4274b88c807SRodney W. Grimes 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
4284b88c807SRodney W. Grimes 	if (setjmp(loc1.loc)) {
4294b88c807SRodney W. Grimes 		goto l1;
4304b88c807SRodney W. Grimes 	}
4314b88c807SRodney W. Grimes 	if (setjmp(loc2.loc)) {
4324b88c807SRodney W. Grimes 		goto l2;
4334b88c807SRodney W. Grimes 	}
4344b88c807SRodney W. Grimes 	handler = &loc1;
4354b88c807SRodney W. Grimes 	if ((p = trap[0]) != NULL && *p != '\0') {
4364b88c807SRodney W. Grimes 		trap[0] = NULL;
4374b88c807SRodney W. Grimes 		evalstring(p);
4384b88c807SRodney W. Grimes 	}
4394b88c807SRodney W. Grimes l1:   handler = &loc2;			/* probably unnecessary */
4404b88c807SRodney W. Grimes 	flushall();
4414b88c807SRodney W. Grimes #if JOBS
4424b88c807SRodney W. Grimes 	setjobctl(0);
4434b88c807SRodney W. Grimes #endif
4444b88c807SRodney W. Grimes l2:   _exit(status);
4454b88c807SRodney W. Grimes }
446