xref: /freebsd/bin/sh/trap.c (revision 40d009a7171e733336aab9cd444c8469ec795f8c)
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[] =
4240d009a7SMartin Cracauer 	"$Id: trap.c,v 1.12 1998/08/24 10:20:36 cracauer Exp $";
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 */
797a8e920bSMartin Cracauer int in_dotrap = 0;		/* Do we execute in a trap handler? */
80f98e1b80SSteve Price static char *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 
85aa9caaf6SPeter Wemm static int getsigaction __P((int, sig_t *));
86aa9caaf6SPeter Wemm 
87f98e1b80SSteve Price 
88f98e1b80SSteve Price /*
89f98e1b80SSteve Price  * Map a string to a signal number.
90f98e1b80SSteve Price  */
91f98e1b80SSteve Price static int
92f98e1b80SSteve Price sigstring_to_signum(sig)
93f98e1b80SSteve Price 	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));
101f98e1b80SSteve Price 	} else if (strcasecmp(sig, "exit") == 0) {
102f98e1b80SSteve Price 		return (0);
103f98e1b80SSteve Price 	} else {
104f98e1b80SSteve Price 		int n;
105f98e1b80SSteve Price 
106f98e1b80SSteve Price 		if (strncasecmp(sig, "sig", 3) == 0)
107f98e1b80SSteve Price 			sig += 3;
108f98e1b80SSteve Price 		for (n = 1; n < NSIG; n++)
109f98e1b80SSteve Price 			if (strcasecmp(sys_signame[n], sig) == 0)
110f98e1b80SSteve Price 				return (n);
111f98e1b80SSteve Price 	}
112f98e1b80SSteve Price 	return (-1);
113f98e1b80SSteve Price }
114f98e1b80SSteve Price 
115f98e1b80SSteve Price 
116f98e1b80SSteve Price /*
117f98e1b80SSteve Price  * Print a list of valid signal names.
118f98e1b80SSteve Price  */
119f98e1b80SSteve Price static void
120f98e1b80SSteve Price printsignals()
121f98e1b80SSteve Price {
122f98e1b80SSteve Price 	int n;
123f98e1b80SSteve Price 
124f98e1b80SSteve Price 	for (n = 1; n < NSIG; n++) {
125f98e1b80SSteve Price 		out1fmt("%s", sys_signame[n]);
126f98e1b80SSteve Price 		if (n == (NSIG / 2) || n == (NSIG - 1))
127f98e1b80SSteve Price 			out1str("\n");
128f98e1b80SSteve Price 		else
129f98e1b80SSteve Price 			out1c(' ');
130f98e1b80SSteve Price 	}
131f98e1b80SSteve Price }
132f98e1b80SSteve Price 
133f98e1b80SSteve Price 
1344b88c807SRodney W. Grimes /*
1354b88c807SRodney W. Grimes  * The trap builtin.
1364b88c807SRodney W. Grimes  */
137aa9caaf6SPeter Wemm int
138aa9caaf6SPeter Wemm trapcmd(argc, argv)
139aa9caaf6SPeter Wemm 	int argc;
140aa9caaf6SPeter Wemm 	char **argv;
141aa9caaf6SPeter Wemm {
1424b88c807SRodney W. Grimes 	char *action;
1434b88c807SRodney W. Grimes 	int signo;
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 	if (argc <= 1) {
146f98e1b80SSteve Price 		for (signo = 0 ; signo < NSIG ; signo++) {
1474b88c807SRodney W. Grimes 			if (trap[signo] != NULL)
148f98e1b80SSteve Price 				out1fmt("trap -- '%s' %s\n", trap[signo],
149f98e1b80SSteve Price 					(signo) ? sys_signame[signo] : "exit");
1504b88c807SRodney W. Grimes 		}
1514b88c807SRodney W. Grimes 		return 0;
1524b88c807SRodney W. Grimes 	}
1534b88c807SRodney W. Grimes 	action = NULL;
154f98e1b80SSteve Price 	if (*++argv && strcmp(*argv, "--") == 0)
155f98e1b80SSteve Price 		argv++;
156f98e1b80SSteve Price 	if (*argv && sigstring_to_signum(*argv) == -1) {
157f98e1b80SSteve Price 		if ((*argv)[0] != '-') {
158f98e1b80SSteve Price 			action = *argv;
159f98e1b80SSteve Price 			argv++;
160f98e1b80SSteve Price 		} else if ((*argv)[1] == '\0') {
161f98e1b80SSteve Price 			argv++;
162f98e1b80SSteve Price 		} else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
163f98e1b80SSteve Price 			printsignals();
164f98e1b80SSteve Price 			return 0;
165f98e1b80SSteve Price 		} else {
166f98e1b80SSteve Price 			error("bad option %s", *argv);
167f98e1b80SSteve Price 		}
168f98e1b80SSteve Price 	}
169f98e1b80SSteve Price 	while (*argv) {
170f98e1b80SSteve Price 		if ((signo = sigstring_to_signum(*argv)) == -1)
171f98e1b80SSteve Price 			error("bad signal %s", *argv);
1724b88c807SRodney W. Grimes 		INTOFF;
1734b88c807SRodney W. Grimes 		if (action)
1744b88c807SRodney W. Grimes 			action = savestr(action);
1754b88c807SRodney W. Grimes 		if (trap[signo])
1764b88c807SRodney W. Grimes 			ckfree(trap[signo]);
1774b88c807SRodney W. Grimes 		trap[signo] = action;
1784b88c807SRodney W. Grimes 		if (signo != 0)
1794b88c807SRodney W. Grimes 			setsignal(signo);
1804b88c807SRodney W. Grimes 		INTON;
181f98e1b80SSteve Price 		argv++;
1824b88c807SRodney W. Grimes 	}
1834b88c807SRodney W. Grimes 	return 0;
1844b88c807SRodney W. Grimes }
1854b88c807SRodney W. Grimes 
1864b88c807SRodney W. Grimes 
1874b88c807SRodney W. Grimes /*
1884b88c807SRodney W. Grimes  * Clear traps on a fork.
1894b88c807SRodney W. Grimes  */
1904b88c807SRodney W. Grimes void
191f98e1b80SSteve Price clear_traps()
192f98e1b80SSteve Price {
1934b88c807SRodney W. Grimes 	char **tp;
1944b88c807SRodney W. Grimes 
195f98e1b80SSteve Price 	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
1964b88c807SRodney W. Grimes 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
1974b88c807SRodney W. Grimes 			INTOFF;
1984b88c807SRodney W. Grimes 			ckfree(*tp);
1994b88c807SRodney W. Grimes 			*tp = NULL;
2004b88c807SRodney W. Grimes 			if (tp != &trap[0])
2014b88c807SRodney W. Grimes 				setsignal(tp - trap);
2024b88c807SRodney W. Grimes 			INTON;
2034b88c807SRodney W. Grimes 		}
2044b88c807SRodney W. Grimes 	}
2054b88c807SRodney W. Grimes }
2064b88c807SRodney W. Grimes 
2074b88c807SRodney W. Grimes 
2084b88c807SRodney W. Grimes /*
2094b88c807SRodney W. Grimes  * Set the signal handler for the specified signal.  The routine figures
2104b88c807SRodney W. Grimes  * out what it should be set to.
2114b88c807SRodney W. Grimes  */
21228701136SBruce Evans void
213aa9caaf6SPeter Wemm setsignal(signo)
214aa9caaf6SPeter Wemm 	int signo;
215aa9caaf6SPeter Wemm {
2164b88c807SRodney W. Grimes 	int action;
21728701136SBruce Evans 	sig_t sig, sigact = SIG_DFL;
2184b88c807SRodney W. Grimes 	char *t;
2194b88c807SRodney W. Grimes 
2204b88c807SRodney W. Grimes 	if ((t = trap[signo]) == NULL)
2214b88c807SRodney W. Grimes 		action = S_DFL;
2224b88c807SRodney W. Grimes 	else if (*t != '\0')
2234b88c807SRodney W. Grimes 		action = S_CATCH;
2244b88c807SRodney W. Grimes 	else
2254b88c807SRodney W. Grimes 		action = S_IGN;
2267a8e920bSMartin Cracauer 	if (action == S_DFL) {
2274b88c807SRodney W. Grimes 		switch (signo) {
2284b88c807SRodney W. Grimes 		case SIGINT:
2294b88c807SRodney W. Grimes 			action = S_CATCH;
2304b88c807SRodney W. Grimes 			break;
2314b88c807SRodney W. Grimes 		case SIGQUIT:
2324b88c807SRodney W. Grimes #ifdef DEBUG
2334b88c807SRodney W. Grimes 			{
2344b88c807SRodney W. Grimes 			extern int debug;
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes 			if (debug)
2374b88c807SRodney W. Grimes 				break;
2384b88c807SRodney W. Grimes 			}
2394b88c807SRodney W. Grimes #endif
24040d009a7SMartin Cracauer 			action = S_CATCH;
2417a8e920bSMartin Cracauer 			break;
2424b88c807SRodney W. Grimes 		case SIGTERM:
2437a8e920bSMartin Cracauer 			if (rootshell && iflag)
2444b88c807SRodney W. Grimes 				action = S_IGN;
2454b88c807SRodney W. Grimes 			break;
2464b88c807SRodney W. Grimes #if JOBS
2474b88c807SRodney W. Grimes 		case SIGTSTP:
2484b88c807SRodney W. Grimes 		case SIGTTOU:
2497a8e920bSMartin Cracauer 			if (rootshell && mflag)
2504b88c807SRodney W. Grimes 				action = S_IGN;
2514b88c807SRodney W. Grimes 			break;
2524b88c807SRodney W. Grimes #endif
2534b88c807SRodney W. Grimes 		}
2544b88c807SRodney W. Grimes 	}
255aa9caaf6SPeter Wemm 
256f98e1b80SSteve Price 	t = &sigmode[signo];
2574b88c807SRodney W. Grimes 	if (*t == 0) {
2584b88c807SRodney W. Grimes 		/*
2594b88c807SRodney W. Grimes 		 * current setting unknown
2604b88c807SRodney W. Grimes 		 */
261aa9caaf6SPeter Wemm 		if (!getsigaction(signo, &sigact)) {
262aa9caaf6SPeter Wemm 			/*
263aa9caaf6SPeter Wemm 			 * Pretend it worked; maybe we should give a warning
264aa9caaf6SPeter Wemm 			 * here, but other shells don't. We don't alter
265aa9caaf6SPeter Wemm 			 * sigmode, so that we retry every time.
266aa9caaf6SPeter Wemm 			 */
26728701136SBruce Evans 			return;
268aa9caaf6SPeter Wemm 		}
2694b88c807SRodney W. Grimes 		if (sigact == SIG_IGN) {
2704b88c807SRodney W. Grimes 			if (mflag && (signo == SIGTSTP ||
2714b88c807SRodney W. Grimes 			     signo == SIGTTIN || signo == SIGTTOU)) {
2724b88c807SRodney W. Grimes 				*t = S_IGN;	/* don't hard ignore these */
2734b88c807SRodney W. Grimes 			} else
2744b88c807SRodney W. Grimes 				*t = S_HARD_IGN;
2754b88c807SRodney W. Grimes 		} else {
2764b88c807SRodney W. Grimes 			*t = S_RESET;	/* force to be set */
2774b88c807SRodney W. Grimes 		}
2784b88c807SRodney W. Grimes 	}
2794b88c807SRodney W. Grimes 	if (*t == S_HARD_IGN || *t == action)
28028701136SBruce Evans 		return;
2814b88c807SRodney W. Grimes 	switch (action) {
2824b88c807SRodney W. Grimes 		case S_DFL:	sigact = SIG_DFL;	break;
2834b88c807SRodney W. Grimes 		case S_CATCH:  	sigact = onsig;		break;
2844b88c807SRodney W. Grimes 		case S_IGN:	sigact = SIG_IGN;	break;
2854b88c807SRodney W. Grimes 	}
2864b88c807SRodney W. Grimes 	*t = action;
28728701136SBruce Evans 	sig = signal(signo, sigact);
2888dd81503SAndrey A. Chernov #ifdef BSD
28928701136SBruce Evans 	if (sig != SIG_ERR && action == S_CATCH)
29028701136SBruce Evans 		siginterrupt(signo, 1);
2918dd81503SAndrey A. Chernov #endif
2924b88c807SRodney W. Grimes }
2934b88c807SRodney W. Grimes 
294f98e1b80SSteve Price 
2954b88c807SRodney W. Grimes /*
2964b88c807SRodney W. Grimes  * Return the current setting for sig w/o changing it.
2974b88c807SRodney W. Grimes  */
298aa9caaf6SPeter Wemm static int
299aa9caaf6SPeter Wemm getsigaction(signo, sigact)
300aa9caaf6SPeter Wemm 	int signo;
301aa9caaf6SPeter Wemm 	sig_t *sigact;
302aa9caaf6SPeter Wemm {
3034b88c807SRodney W. Grimes 	struct sigaction sa;
3044b88c807SRodney W. Grimes 
3054b88c807SRodney W. Grimes 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
306aa9caaf6SPeter Wemm 		return 0;
307aa9caaf6SPeter Wemm 	*sigact = (sig_t) sa.sa_handler;
308aa9caaf6SPeter Wemm 	return 1;
3094b88c807SRodney W. Grimes }
3104b88c807SRodney W. Grimes 
311f98e1b80SSteve Price 
3124b88c807SRodney W. Grimes /*
3134b88c807SRodney W. Grimes  * Ignore a signal.
3144b88c807SRodney W. Grimes  */
3154b88c807SRodney W. Grimes void
316aa9caaf6SPeter Wemm ignoresig(signo)
317aa9caaf6SPeter Wemm 	int signo;
318aa9caaf6SPeter Wemm {
319f98e1b80SSteve Price 
320f98e1b80SSteve Price 	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
3214b88c807SRodney W. Grimes 		signal(signo, SIG_IGN);
3224b88c807SRodney W. Grimes 	}
323f98e1b80SSteve Price 	sigmode[signo] = S_HARD_IGN;
3244b88c807SRodney W. Grimes }
3254b88c807SRodney W. Grimes 
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes #ifdef mkinit
328aa9caaf6SPeter Wemm INCLUDE <signal.h>
3294b88c807SRodney W. Grimes INCLUDE "trap.h"
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes SHELLPROC {
3324b88c807SRodney W. Grimes 	char *sm;
3334b88c807SRodney W. Grimes 
3344b88c807SRodney W. Grimes 	clear_traps();
335aa9caaf6SPeter Wemm 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
3364b88c807SRodney W. Grimes 		if (*sm == S_IGN)
3374b88c807SRodney W. Grimes 			*sm = S_HARD_IGN;
3384b88c807SRodney W. Grimes 	}
3394b88c807SRodney W. Grimes }
3404b88c807SRodney W. Grimes #endif
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes 
3434b88c807SRodney W. Grimes /*
3444b88c807SRodney W. Grimes  * Signal handler.
3454b88c807SRodney W. Grimes  */
3464b88c807SRodney W. Grimes void
347aa9caaf6SPeter Wemm onsig(signo)
348aa9caaf6SPeter Wemm 	int signo;
349aa9caaf6SPeter Wemm {
35028701136SBruce Evans 
3518dd81503SAndrey A. Chernov #ifndef BSD
3524b88c807SRodney W. Grimes 	signal(signo, onsig);
3538dd81503SAndrey A. Chernov #endif
3544b88c807SRodney W. Grimes 	if (signo == SIGINT && trap[SIGINT] == NULL) {
3554b88c807SRodney W. Grimes 		onint();
3564b88c807SRodney W. Grimes 		return;
3574b88c807SRodney W. Grimes 	}
358f98e1b80SSteve Price 	if (signo != SIGCHLD || !ignore_sigchld)
359f98e1b80SSteve Price 		gotsig[signo] = 1;
3604b88c807SRodney W. Grimes 	pendingsigs++;
3617a8e920bSMartin Cracauer 	if (signo == SIGINT && in_waitcmd != 0) {
3627a8e920bSMartin Cracauer 		dotrap();
3637a8e920bSMartin Cracauer 		breakwaitcmd = 1;
3647a8e920bSMartin Cracauer 	}
3654b88c807SRodney W. Grimes }
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes 
3684b88c807SRodney W. Grimes /*
3694b88c807SRodney W. Grimes  * Called to execute a trap.  Perhaps we should avoid entering new trap
3704b88c807SRodney W. Grimes  * handlers while we are executing a trap handler.
3714b88c807SRodney W. Grimes  */
3724b88c807SRodney W. Grimes void
373f98e1b80SSteve Price dotrap()
374f98e1b80SSteve Price {
3754b88c807SRodney W. Grimes 	int i;
3764b88c807SRodney W. Grimes 	int savestatus;
3774b88c807SRodney W. Grimes 
3787a8e920bSMartin Cracauer 	in_dotrap++;
3794b88c807SRodney W. Grimes 	for (;;) {
380f98e1b80SSteve Price 		for (i = 1; i < NSIG; i++) {
381f98e1b80SSteve Price 			if (gotsig[i]) {
382f98e1b80SSteve Price 				gotsig[i] = 0;
383f98e1b80SSteve Price 				if (trap[i]) {
384f98e1b80SSteve Price 					/*
385f98e1b80SSteve Price 					 * Ignore SIGCHLD to avoid infinite recursion
386f98e1b80SSteve Price 					 * if the trap action does a fork.
387f98e1b80SSteve Price 					 */
388f98e1b80SSteve Price 					if (i == SIGCHLD)
389f98e1b80SSteve Price 						ignore_sigchld++;
3904b88c807SRodney W. Grimes 					savestatus = exitstatus;
3914b88c807SRodney W. Grimes 					evalstring(trap[i]);
3924b88c807SRodney W. Grimes 					exitstatus = savestatus;
393f98e1b80SSteve Price 					if (i == SIGCHLD)
394f98e1b80SSteve Price 						ignore_sigchld--;
3954b88c807SRodney W. Grimes 				}
396f98e1b80SSteve Price 				break;
397f98e1b80SSteve Price 			}
398f98e1b80SSteve Price 		}
399f98e1b80SSteve Price 		if (i >= NSIG)
400f98e1b80SSteve Price 			break;
401f98e1b80SSteve Price 	}
4027a8e920bSMartin Cracauer 	in_dotrap--;
4034b88c807SRodney W. Grimes 	pendingsigs = 0;
4044b88c807SRodney W. Grimes }
4054b88c807SRodney W. Grimes 
4064b88c807SRodney W. Grimes 
4074b88c807SRodney W. Grimes /*
4084b88c807SRodney W. Grimes  * Controls whether the shell is interactive or not.
4094b88c807SRodney W. Grimes  */
4104b88c807SRodney W. Grimes void
411aa9caaf6SPeter Wemm setinteractive(on)
412aa9caaf6SPeter Wemm 	int on;
413aa9caaf6SPeter Wemm {
4147a8e920bSMartin Cracauer 	static int is_interactive = -1;
4154b88c807SRodney W. Grimes 
4164b88c807SRodney W. Grimes 	if (on == is_interactive)
4174b88c807SRodney W. Grimes 		return;
4184b88c807SRodney W. Grimes 	setsignal(SIGINT);
4194b88c807SRodney W. Grimes 	setsignal(SIGQUIT);
4204b88c807SRodney W. Grimes 	setsignal(SIGTERM);
4214b88c807SRodney W. Grimes 	is_interactive = on;
4224b88c807SRodney W. Grimes }
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 
4254b88c807SRodney W. Grimes /*
4264b88c807SRodney W. Grimes  * Called to exit the shell.
4274b88c807SRodney W. Grimes  */
4284b88c807SRodney W. Grimes void
429aa9caaf6SPeter Wemm exitshell(status)
430aa9caaf6SPeter Wemm 	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