xref: /freebsd/bin/sh/trap.c (revision 9de7305e58b975252092081c3b4095df032a2b50)
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
414b88c807SRodney W. Grimes #endif /* not lint */
422749b141SDavid E. O'Brien #include <sys/cdefs.h>
432749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
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"
629de7305eSTim J. Robbins #include "myhistedit.h"
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes /*
664b88c807SRodney W. Grimes  * Sigmode records the current value of the signal handlers for the various
674b88c807SRodney W. Grimes  * modes.  A value of zero means that the current handler is not known.
684b88c807SRodney W. Grimes  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
694b88c807SRodney W. Grimes  */
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes #define S_DFL 1			/* default signal handling (SIG_DFL) */
724b88c807SRodney W. Grimes #define S_CATCH 2		/* signal is caught */
734b88c807SRodney W. Grimes #define S_IGN 3			/* signal is ignored (SIG_IGN) */
7446be34b9SKris Kennaway #define S_HARD_IGN 4		/* signal is ignored permanently */
754b88c807SRodney W. Grimes #define S_RESET 5		/* temporary - to reset a hard ignored sig */
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes 
78aa9caaf6SPeter Wemm MKINIT char sigmode[NSIG];	/* current value of signal */
794b88c807SRodney W. Grimes int pendingsigs;		/* indicates some signal received */
801f40b47bSMartin Cracauer int in_dotrap;			/* do we execute in a trap handler? */
81135421ddSMartin Cracauer static char *volatile trap[NSIG];	/* trap handler commands */
827a8e920bSMartin Cracauer static volatile sig_atomic_t gotsig[NSIG];
837a8e920bSMartin Cracauer 				/* indicates specified signal received */
84f98e1b80SSteve Price static int ignore_sigchld;	/* Used while handling SIGCHLD traps. */
854b88c807SRodney W. Grimes 
865134c3f7SWarner Losh static int getsigaction(int, sig_t *);
87aa9caaf6SPeter Wemm 
88f98e1b80SSteve Price 
89f98e1b80SSteve Price /*
90f98e1b80SSteve Price  * Map a string to a signal number.
91f98e1b80SSteve Price  */
92f98e1b80SSteve Price static int
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));
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
1205134c3f7SWarner Losh printsignals(void)
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
1385134c3f7SWarner Losh trapcmd(int argc, 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
1895134c3f7SWarner Losh clear_traps(void)
190f98e1b80SSteve Price {
191135421ddSMartin Cracauer 	char *volatile *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
2115134c3f7SWarner Losh setsignal(int signo)
212aa9caaf6SPeter Wemm {
2134b88c807SRodney W. Grimes 	int action;
21428701136SBruce Evans 	sig_t sig, sigact = SIG_DFL;
2154b88c807SRodney W. Grimes 	char *t;
2164b88c807SRodney W. Grimes 
2174b88c807SRodney W. Grimes 	if ((t = trap[signo]) == NULL)
2184b88c807SRodney W. Grimes 		action = S_DFL;
2194b88c807SRodney W. Grimes 	else if (*t != '\0')
2204b88c807SRodney W. Grimes 		action = S_CATCH;
2214b88c807SRodney W. Grimes 	else
2224b88c807SRodney W. Grimes 		action = S_IGN;
2237a8e920bSMartin Cracauer 	if (action == S_DFL) {
2244b88c807SRodney W. Grimes 		switch (signo) {
2254b88c807SRodney W. Grimes 		case SIGINT:
2264b88c807SRodney W. Grimes 			action = S_CATCH;
2274b88c807SRodney W. Grimes 			break;
2284b88c807SRodney W. Grimes 		case SIGQUIT:
2294b88c807SRodney W. Grimes #ifdef DEBUG
2304b88c807SRodney W. Grimes 			{
2314b88c807SRodney W. Grimes 			extern int debug;
2324b88c807SRodney W. Grimes 
2334b88c807SRodney W. Grimes 			if (debug)
2344b88c807SRodney W. Grimes 				break;
2354b88c807SRodney W. Grimes 			}
2364b88c807SRodney W. Grimes #endif
23740d009a7SMartin Cracauer 			action = S_CATCH;
2387a8e920bSMartin Cracauer 			break;
2394b88c807SRodney W. Grimes 		case SIGTERM:
2407a8e920bSMartin Cracauer 			if (rootshell && iflag)
2414b88c807SRodney W. Grimes 				action = S_IGN;
2424b88c807SRodney W. Grimes 			break;
2434b88c807SRodney W. Grimes #if JOBS
2444b88c807SRodney W. Grimes 		case SIGTSTP:
2454b88c807SRodney W. Grimes 		case SIGTTOU:
2467a8e920bSMartin Cracauer 			if (rootshell && mflag)
2474b88c807SRodney W. Grimes 				action = S_IGN;
2484b88c807SRodney W. Grimes 			break;
2494b88c807SRodney W. Grimes #endif
2509de7305eSTim J. Robbins #ifndef NO_HISTORY
2519de7305eSTim J. Robbins 		case SIGWINCH:
2529de7305eSTim J. Robbins 			if (rootshell && iflag && el != NULL)
2539de7305eSTim J. Robbins 				action = S_CATCH;
2549de7305eSTim J. Robbins 			break;
2559de7305eSTim J. Robbins #endif
2564b88c807SRodney W. Grimes 		}
2574b88c807SRodney W. Grimes 	}
258aa9caaf6SPeter Wemm 
259f98e1b80SSteve Price 	t = &sigmode[signo];
2604b88c807SRodney W. Grimes 	if (*t == 0) {
2614b88c807SRodney W. Grimes 		/*
2624b88c807SRodney W. Grimes 		 * current setting unknown
2634b88c807SRodney W. Grimes 		 */
264aa9caaf6SPeter Wemm 		if (!getsigaction(signo, &sigact)) {
265aa9caaf6SPeter Wemm 			/*
266aa9caaf6SPeter Wemm 			 * Pretend it worked; maybe we should give a warning
267aa9caaf6SPeter Wemm 			 * here, but other shells don't. We don't alter
268aa9caaf6SPeter Wemm 			 * sigmode, so that we retry every time.
269aa9caaf6SPeter Wemm 			 */
27028701136SBruce Evans 			return;
271aa9caaf6SPeter Wemm 		}
2724b88c807SRodney W. Grimes 		if (sigact == SIG_IGN) {
2734b88c807SRodney W. Grimes 			if (mflag && (signo == SIGTSTP ||
2744b88c807SRodney W. Grimes 			     signo == SIGTTIN || signo == SIGTTOU)) {
2754b88c807SRodney W. Grimes 				*t = S_IGN;	/* don't hard ignore these */
2764b88c807SRodney W. Grimes 			} else
2774b88c807SRodney W. Grimes 				*t = S_HARD_IGN;
2784b88c807SRodney W. Grimes 		} else {
2794b88c807SRodney W. Grimes 			*t = S_RESET;	/* force to be set */
2804b88c807SRodney W. Grimes 		}
2814b88c807SRodney W. Grimes 	}
2824b88c807SRodney W. Grimes 	if (*t == S_HARD_IGN || *t == action)
28328701136SBruce Evans 		return;
2844b88c807SRodney W. Grimes 	switch (action) {
2854b88c807SRodney W. Grimes 		case S_DFL:	sigact = SIG_DFL;	break;
2864b88c807SRodney W. Grimes 		case S_CATCH:  	sigact = onsig;		break;
2874b88c807SRodney W. Grimes 		case S_IGN:	sigact = SIG_IGN;	break;
2884b88c807SRodney W. Grimes 	}
2894b88c807SRodney W. Grimes 	*t = action;
29028701136SBruce Evans 	sig = signal(signo, sigact);
29128701136SBruce Evans 	if (sig != SIG_ERR && action == S_CATCH)
29228701136SBruce Evans 		siginterrupt(signo, 1);
2934b88c807SRodney W. Grimes }
2944b88c807SRodney W. Grimes 
295f98e1b80SSteve Price 
2964b88c807SRodney W. Grimes /*
2974b88c807SRodney W. Grimes  * Return the current setting for sig w/o changing it.
2984b88c807SRodney W. Grimes  */
299aa9caaf6SPeter Wemm static int
3005134c3f7SWarner Losh getsigaction(int signo, sig_t *sigact)
301aa9caaf6SPeter Wemm {
3024b88c807SRodney W. Grimes 	struct sigaction sa;
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
305aa9caaf6SPeter Wemm 		return 0;
306aa9caaf6SPeter Wemm 	*sigact = (sig_t) sa.sa_handler;
307aa9caaf6SPeter Wemm 	return 1;
3084b88c807SRodney W. Grimes }
3094b88c807SRodney W. Grimes 
310f98e1b80SSteve Price 
3114b88c807SRodney W. Grimes /*
3124b88c807SRodney W. Grimes  * Ignore a signal.
3134b88c807SRodney W. Grimes  */
3144b88c807SRodney W. Grimes void
3155134c3f7SWarner Losh ignoresig(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
3455134c3f7SWarner Losh onsig(int signo)
346aa9caaf6SPeter Wemm {
34728701136SBruce Evans 
3484b88c807SRodney W. Grimes 	if (signo == SIGINT && trap[SIGINT] == NULL) {
3494b88c807SRodney W. Grimes 		onint();
3504b88c807SRodney W. Grimes 		return;
3514b88c807SRodney W. Grimes 	}
352135421ddSMartin Cracauer 
353f98e1b80SSteve Price 	if (signo != SIGCHLD || !ignore_sigchld)
354f98e1b80SSteve Price 		gotsig[signo] = 1;
3554b88c807SRodney W. Grimes 	pendingsigs++;
356135421ddSMartin Cracauer 
357135421ddSMartin Cracauer 	/* If we are currently in a wait builtin, prepare to break it */
358135421ddSMartin Cracauer 	if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
3597a8e920bSMartin Cracauer 		breakwaitcmd = 1;
360135421ddSMartin Cracauer 	/*
3619bbfa415SMartin Cracauer 	 * If a trap is set, not ignored and not the null command, we need
3629bbfa415SMartin Cracauer 	 * to make sure traps are executed even when a child blocks signals.
363135421ddSMartin Cracauer 	 */
364c1c72a3cSMartin Cracauer 	if (Tflag &&
365c1c72a3cSMartin Cracauer 	    trap[signo] != NULL &&
3669bbfa415SMartin Cracauer 	    ! trap[signo][0] == '\0' &&
367e6ac45ddSMartin Cracauer 	    ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
368135421ddSMartin Cracauer 		breakwaitcmd = 1;
3699de7305eSTim J. Robbins 
3709de7305eSTim J. Robbins #ifndef NO_HISTORY
3719de7305eSTim J. Robbins 	if (signo == SIGWINCH)
3729de7305eSTim J. Robbins 		el_resize(el);
3739de7305eSTim J. Robbins #endif
3744b88c807SRodney W. Grimes }
3754b88c807SRodney W. Grimes 
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes /*
3784b88c807SRodney W. Grimes  * Called to execute a trap.  Perhaps we should avoid entering new trap
3794b88c807SRodney W. Grimes  * handlers while we are executing a trap handler.
3804b88c807SRodney W. Grimes  */
3814b88c807SRodney W. Grimes void
3825134c3f7SWarner Losh dotrap(void)
383f98e1b80SSteve Price {
3844b88c807SRodney W. Grimes 	int i;
3854b88c807SRodney W. Grimes 	int savestatus;
3864b88c807SRodney W. Grimes 
3877a8e920bSMartin Cracauer 	in_dotrap++;
3884b88c807SRodney W. Grimes 	for (;;) {
389f98e1b80SSteve Price 		for (i = 1; i < NSIG; i++) {
390f98e1b80SSteve Price 			if (gotsig[i]) {
391f98e1b80SSteve Price 				gotsig[i] = 0;
392f98e1b80SSteve Price 				if (trap[i]) {
393f98e1b80SSteve Price 					/*
394f98e1b80SSteve Price 					 * Ignore SIGCHLD to avoid infinite recursion
395f98e1b80SSteve Price 					 * if the trap action does a fork.
396f98e1b80SSteve Price 					 */
397f98e1b80SSteve Price 					if (i == SIGCHLD)
398f98e1b80SSteve Price 						ignore_sigchld++;
3994b88c807SRodney W. Grimes 					savestatus = exitstatus;
4004b88c807SRodney W. Grimes 					evalstring(trap[i]);
4014b88c807SRodney W. Grimes 					exitstatus = savestatus;
402f98e1b80SSteve Price 					if (i == SIGCHLD)
403f98e1b80SSteve Price 						ignore_sigchld--;
4044b88c807SRodney W. Grimes 				}
405f98e1b80SSteve Price 				break;
406f98e1b80SSteve Price 			}
407f98e1b80SSteve Price 		}
408f98e1b80SSteve Price 		if (i >= NSIG)
409f98e1b80SSteve Price 			break;
410f98e1b80SSteve Price 	}
4117a8e920bSMartin Cracauer 	in_dotrap--;
4124b88c807SRodney W. Grimes 	pendingsigs = 0;
4134b88c807SRodney W. Grimes }
4144b88c807SRodney W. Grimes 
4154b88c807SRodney W. Grimes 
4164b88c807SRodney W. Grimes /*
4174b88c807SRodney W. Grimes  * Controls whether the shell is interactive or not.
4184b88c807SRodney W. Grimes  */
4194b88c807SRodney W. Grimes void
4205134c3f7SWarner Losh setinteractive(int on)
421aa9caaf6SPeter Wemm {
4227a8e920bSMartin Cracauer 	static int is_interactive = -1;
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 	if (on == is_interactive)
4254b88c807SRodney W. Grimes 		return;
4264b88c807SRodney W. Grimes 	setsignal(SIGINT);
4274b88c807SRodney W. Grimes 	setsignal(SIGQUIT);
4284b88c807SRodney W. Grimes 	setsignal(SIGTERM);
4299de7305eSTim J. Robbins #ifndef NO_HISTORY
4309de7305eSTim J. Robbins 	setsignal(SIGWINCH);
4319de7305eSTim J. Robbins #endif
4324b88c807SRodney W. Grimes 	is_interactive = on;
4334b88c807SRodney W. Grimes }
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes /*
4374b88c807SRodney W. Grimes  * Called to exit the shell.
4384b88c807SRodney W. Grimes  */
4394b88c807SRodney W. Grimes void
4405134c3f7SWarner Losh exitshell(int status)
441aa9caaf6SPeter Wemm {
4424b88c807SRodney W. Grimes 	struct jmploc loc1, loc2;
4434b88c807SRodney W. Grimes 	char *p;
4444b88c807SRodney W. Grimes 
4454b88c807SRodney W. Grimes 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
4464b88c807SRodney W. Grimes 	if (setjmp(loc1.loc)) {
4474b88c807SRodney W. Grimes 		goto l1;
4484b88c807SRodney W. Grimes 	}
4494b88c807SRodney W. Grimes 	if (setjmp(loc2.loc)) {
4504b88c807SRodney W. Grimes 		goto l2;
4514b88c807SRodney W. Grimes 	}
4524b88c807SRodney W. Grimes 	handler = &loc1;
4534b88c807SRodney W. Grimes 	if ((p = trap[0]) != NULL && *p != '\0') {
4544b88c807SRodney W. Grimes 		trap[0] = NULL;
4554b88c807SRodney W. Grimes 		evalstring(p);
4564b88c807SRodney W. Grimes 	}
4574b88c807SRodney W. Grimes l1:   handler = &loc2;			/* probably unnecessary */
4584b88c807SRodney W. Grimes 	flushall();
4594b88c807SRodney W. Grimes #if JOBS
4604b88c807SRodney W. Grimes 	setjobctl(0);
4614b88c807SRodney W. Grimes #endif
4624b88c807SRodney W. Grimes l2:   _exit(status);
4634b88c807SRodney W. Grimes }
464