xref: /freebsd/bin/sh/trap.c (revision 4b88c807ea9c629dc5691abc7e3cac9ea0d776dd)
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
384b88c807SRodney W. Grimes static char sccsid[] = "@(#)trap.c	8.1 (Berkeley) 5/31/93";
394b88c807SRodney W. Grimes #endif /* not lint */
404b88c807SRodney W. Grimes 
414b88c807SRodney W. Grimes #include "shell.h"
424b88c807SRodney W. Grimes #include "main.h"
434b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
444b88c807SRodney W. Grimes #include "eval.h"
454b88c807SRodney W. Grimes #include "jobs.h"
464b88c807SRodney W. Grimes #include "options.h"
474b88c807SRodney W. Grimes #include "syntax.h"
484b88c807SRodney W. Grimes #include "signames.h"
494b88c807SRodney W. Grimes #include "output.h"
504b88c807SRodney W. Grimes #include "memalloc.h"
514b88c807SRodney W. Grimes #include "error.h"
524b88c807SRodney W. Grimes #include "trap.h"
534b88c807SRodney W. Grimes #include "mystring.h"
544b88c807SRodney W. Grimes #include <signal.h>
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes /*
584b88c807SRodney W. Grimes  * Sigmode records the current value of the signal handlers for the various
594b88c807SRodney W. Grimes  * modes.  A value of zero means that the current handler is not known.
604b88c807SRodney W. Grimes  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
614b88c807SRodney W. Grimes  */
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes #define S_DFL 1			/* default signal handling (SIG_DFL) */
644b88c807SRodney W. Grimes #define S_CATCH 2		/* signal is caught */
654b88c807SRodney W. Grimes #define S_IGN 3			/* signal is ignored (SIG_IGN) */
664b88c807SRodney W. Grimes #define S_HARD_IGN 4		/* signal is ignored permenantly */
674b88c807SRodney W. Grimes #define S_RESET 5		/* temporary - to reset a hard ignored sig */
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes extern char nullstr[1];		/* null string */
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes char *trap[MAXSIG+1];		/* trap handler commands */
734b88c807SRodney W. Grimes MKINIT char sigmode[MAXSIG];	/* current value of signal */
744b88c807SRodney W. Grimes char gotsig[MAXSIG];		/* indicates specified signal received */
754b88c807SRodney W. Grimes int pendingsigs;			/* indicates some signal received */
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes /*
784b88c807SRodney W. Grimes  * The trap builtin.
794b88c807SRodney W. Grimes  */
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes trapcmd(argc, argv)  char **argv; {
824b88c807SRodney W. Grimes 	char *action;
834b88c807SRodney W. Grimes 	char **ap;
844b88c807SRodney W. Grimes 	int signo;
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes 	if (argc <= 1) {
874b88c807SRodney W. Grimes 		for (signo = 0 ; signo <= MAXSIG ; signo++) {
884b88c807SRodney W. Grimes 			if (trap[signo] != NULL)
894b88c807SRodney W. Grimes 				out1fmt("%d: %s\n", signo, trap[signo]);
904b88c807SRodney W. Grimes 		}
914b88c807SRodney W. Grimes 		return 0;
924b88c807SRodney W. Grimes 	}
934b88c807SRodney W. Grimes 	ap = argv + 1;
944b88c807SRodney W. Grimes 	if (is_number(*ap))
954b88c807SRodney W. Grimes 		action = NULL;
964b88c807SRodney W. Grimes 	else
974b88c807SRodney W. Grimes 		action = *ap++;
984b88c807SRodney W. Grimes 	while (*ap) {
994b88c807SRodney W. Grimes 		if ((signo = number(*ap)) < 0 || signo > MAXSIG)
1004b88c807SRodney W. Grimes 			error("%s: bad trap", *ap);
1014b88c807SRodney W. Grimes 		INTOFF;
1024b88c807SRodney W. Grimes 		if (action)
1034b88c807SRodney W. Grimes 			action = savestr(action);
1044b88c807SRodney W. Grimes 		if (trap[signo])
1054b88c807SRodney W. Grimes 			ckfree(trap[signo]);
1064b88c807SRodney W. Grimes 		trap[signo] = action;
1074b88c807SRodney W. Grimes 		if (signo != 0)
1084b88c807SRodney W. Grimes 			setsignal(signo);
1094b88c807SRodney W. Grimes 		INTON;
1104b88c807SRodney W. Grimes 		ap++;
1114b88c807SRodney W. Grimes 	}
1124b88c807SRodney W. Grimes 	return 0;
1134b88c807SRodney W. Grimes }
1144b88c807SRodney W. Grimes 
1154b88c807SRodney W. Grimes 
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes /*
1184b88c807SRodney W. Grimes  * Clear traps on a fork.
1194b88c807SRodney W. Grimes  */
1204b88c807SRodney W. Grimes 
1214b88c807SRodney W. Grimes void
1224b88c807SRodney W. Grimes clear_traps() {
1234b88c807SRodney W. Grimes 	char **tp;
1244b88c807SRodney W. Grimes 
1254b88c807SRodney W. Grimes 	for (tp = trap ; tp <= &trap[MAXSIG] ; tp++) {
1264b88c807SRodney W. Grimes 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
1274b88c807SRodney W. Grimes 			INTOFF;
1284b88c807SRodney W. Grimes 			ckfree(*tp);
1294b88c807SRodney W. Grimes 			*tp = NULL;
1304b88c807SRodney W. Grimes 			if (tp != &trap[0])
1314b88c807SRodney W. Grimes 				setsignal(tp - trap);
1324b88c807SRodney W. Grimes 			INTON;
1334b88c807SRodney W. Grimes 		}
1344b88c807SRodney W. Grimes 	}
1354b88c807SRodney W. Grimes }
1364b88c807SRodney W. Grimes 
1374b88c807SRodney W. Grimes 
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes /*
1404b88c807SRodney W. Grimes  * Set the signal handler for the specified signal.  The routine figures
1414b88c807SRodney W. Grimes  * out what it should be set to.
1424b88c807SRodney W. Grimes  */
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes int
1454b88c807SRodney W. Grimes setsignal(signo) {
1464b88c807SRodney W. Grimes 	int action;
1474b88c807SRodney W. Grimes 	sig_t sigact;
1484b88c807SRodney W. Grimes 	char *t;
1494b88c807SRodney W. Grimes 	extern void onsig();
1504b88c807SRodney W. Grimes 	extern sig_t getsigaction();
1514b88c807SRodney W. Grimes 
1524b88c807SRodney W. Grimes 	if ((t = trap[signo]) == NULL)
1534b88c807SRodney W. Grimes 		action = S_DFL;
1544b88c807SRodney W. Grimes 	else if (*t != '\0')
1554b88c807SRodney W. Grimes 		action = S_CATCH;
1564b88c807SRodney W. Grimes 	else
1574b88c807SRodney W. Grimes 		action = S_IGN;
1584b88c807SRodney W. Grimes 	if (rootshell && action == S_DFL) {
1594b88c807SRodney W. Grimes 		switch (signo) {
1604b88c807SRodney W. Grimes 		case SIGINT:
1614b88c807SRodney W. Grimes 			if (iflag)
1624b88c807SRodney W. Grimes 				action = S_CATCH;
1634b88c807SRodney W. Grimes 			break;
1644b88c807SRodney W. Grimes 		case SIGQUIT:
1654b88c807SRodney W. Grimes #ifdef DEBUG
1664b88c807SRodney W. Grimes 			{
1674b88c807SRodney W. Grimes 			extern int debug;
1684b88c807SRodney W. Grimes 
1694b88c807SRodney W. Grimes 			if (debug)
1704b88c807SRodney W. Grimes 				break;
1714b88c807SRodney W. Grimes 			}
1724b88c807SRodney W. Grimes #endif
1734b88c807SRodney W. Grimes 			/* FALLTHROUGH */
1744b88c807SRodney W. Grimes 		case SIGTERM:
1754b88c807SRodney W. Grimes 			if (iflag)
1764b88c807SRodney W. Grimes 				action = S_IGN;
1774b88c807SRodney W. Grimes 			break;
1784b88c807SRodney W. Grimes #if JOBS
1794b88c807SRodney W. Grimes 		case SIGTSTP:
1804b88c807SRodney W. Grimes 		case SIGTTOU:
1814b88c807SRodney W. Grimes 			if (mflag)
1824b88c807SRodney W. Grimes 				action = S_IGN;
1834b88c807SRodney W. Grimes 			break;
1844b88c807SRodney W. Grimes #endif
1854b88c807SRodney W. Grimes 		}
1864b88c807SRodney W. Grimes 	}
1874b88c807SRodney W. Grimes 	t = &sigmode[signo - 1];
1884b88c807SRodney W. Grimes 	if (*t == 0) {
1894b88c807SRodney W. Grimes 		/*
1904b88c807SRodney W. Grimes 		 * current setting unknown
1914b88c807SRodney W. Grimes 		 */
1924b88c807SRodney W. Grimes 		sigact = getsigaction(signo);
1934b88c807SRodney W. Grimes 		if (sigact == SIG_IGN) {
1944b88c807SRodney W. Grimes 			if (mflag && (signo == SIGTSTP ||
1954b88c807SRodney W. Grimes 			     signo == SIGTTIN || signo == SIGTTOU)) {
1964b88c807SRodney W. Grimes 				*t = S_IGN;	/* don't hard ignore these */
1974b88c807SRodney W. Grimes 			} else
1984b88c807SRodney W. Grimes 				*t = S_HARD_IGN;
1994b88c807SRodney W. Grimes 		} else {
2004b88c807SRodney W. Grimes 			*t = S_RESET;	/* force to be set */
2014b88c807SRodney W. Grimes 		}
2024b88c807SRodney W. Grimes 	}
2034b88c807SRodney W. Grimes 	if (*t == S_HARD_IGN || *t == action)
2044b88c807SRodney W. Grimes 		return 0;
2054b88c807SRodney W. Grimes 	switch (action) {
2064b88c807SRodney W. Grimes 		case S_DFL:	sigact = SIG_DFL;	break;
2074b88c807SRodney W. Grimes 		case S_CATCH:  	sigact = onsig;		break;
2084b88c807SRodney W. Grimes 		case S_IGN:	sigact = SIG_IGN;	break;
2094b88c807SRodney W. Grimes 	}
2104b88c807SRodney W. Grimes 	*t = action;
2114b88c807SRodney W. Grimes 	return (int)signal(signo, sigact);
2124b88c807SRodney W. Grimes }
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes /*
2154b88c807SRodney W. Grimes  * Return the current setting for sig w/o changing it.
2164b88c807SRodney W. Grimes  */
2174b88c807SRodney W. Grimes sig_t
2184b88c807SRodney W. Grimes getsigaction(signo) {
2194b88c807SRodney W. Grimes 	struct sigaction sa;
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
2224b88c807SRodney W. Grimes 		error("Sigaction system call failed");
2234b88c807SRodney W. Grimes 
2244b88c807SRodney W. Grimes 	return sa.sa_handler;
2254b88c807SRodney W. Grimes }
2264b88c807SRodney W. Grimes 
2274b88c807SRodney W. Grimes /*
2284b88c807SRodney W. Grimes  * Ignore a signal.
2294b88c807SRodney W. Grimes  */
2304b88c807SRodney W. Grimes 
2314b88c807SRodney W. Grimes void
2324b88c807SRodney W. Grimes ignoresig(signo) {
2334b88c807SRodney W. Grimes 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
2344b88c807SRodney W. Grimes 		signal(signo, SIG_IGN);
2354b88c807SRodney W. Grimes 	}
2364b88c807SRodney W. Grimes 	sigmode[signo - 1] = S_HARD_IGN;
2374b88c807SRodney W. Grimes }
2384b88c807SRodney W. Grimes 
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes #ifdef mkinit
2414b88c807SRodney W. Grimes INCLUDE "signames.h"
2424b88c807SRodney W. Grimes INCLUDE "trap.h"
2434b88c807SRodney W. Grimes 
2444b88c807SRodney W. Grimes SHELLPROC {
2454b88c807SRodney W. Grimes 	char *sm;
2464b88c807SRodney W. Grimes 
2474b88c807SRodney W. Grimes 	clear_traps();
2484b88c807SRodney W. Grimes 	for (sm = sigmode ; sm < sigmode + MAXSIG ; sm++) {
2494b88c807SRodney W. Grimes 		if (*sm == S_IGN)
2504b88c807SRodney W. Grimes 			*sm = S_HARD_IGN;
2514b88c807SRodney W. Grimes 	}
2524b88c807SRodney W. Grimes }
2534b88c807SRodney W. Grimes #endif
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes 
2564b88c807SRodney W. Grimes 
2574b88c807SRodney W. Grimes /*
2584b88c807SRodney W. Grimes  * Signal handler.
2594b88c807SRodney W. Grimes  */
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes void
2624b88c807SRodney W. Grimes onsig(signo) {
2634b88c807SRodney W. Grimes 	signal(signo, onsig);
2644b88c807SRodney W. Grimes 	if (signo == SIGINT && trap[SIGINT] == NULL) {
2654b88c807SRodney W. Grimes 		onint();
2664b88c807SRodney W. Grimes 		return;
2674b88c807SRodney W. Grimes 	}
2684b88c807SRodney W. Grimes 	gotsig[signo - 1] = 1;
2694b88c807SRodney W. Grimes 	pendingsigs++;
2704b88c807SRodney W. Grimes }
2714b88c807SRodney W. Grimes 
2724b88c807SRodney W. Grimes 
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes /*
2754b88c807SRodney W. Grimes  * Called to execute a trap.  Perhaps we should avoid entering new trap
2764b88c807SRodney W. Grimes  * handlers while we are executing a trap handler.
2774b88c807SRodney W. Grimes  */
2784b88c807SRodney W. Grimes 
2794b88c807SRodney W. Grimes void
2804b88c807SRodney W. Grimes dotrap() {
2814b88c807SRodney W. Grimes 	int i;
2824b88c807SRodney W. Grimes 	int savestatus;
2834b88c807SRodney W. Grimes 
2844b88c807SRodney W. Grimes 	for (;;) {
2854b88c807SRodney W. Grimes 		for (i = 1 ; ; i++) {
2864b88c807SRodney W. Grimes 			if (gotsig[i - 1])
2874b88c807SRodney W. Grimes 				break;
2884b88c807SRodney W. Grimes 			if (i >= MAXSIG)
2894b88c807SRodney W. Grimes 				goto done;
2904b88c807SRodney W. Grimes 		}
2914b88c807SRodney W. Grimes 		gotsig[i - 1] = 0;
2924b88c807SRodney W. Grimes 		savestatus=exitstatus;
2934b88c807SRodney W. Grimes 		evalstring(trap[i]);
2944b88c807SRodney W. Grimes 		exitstatus=savestatus;
2954b88c807SRodney W. Grimes 	}
2964b88c807SRodney W. Grimes done:
2974b88c807SRodney W. Grimes 	pendingsigs = 0;
2984b88c807SRodney W. Grimes }
2994b88c807SRodney W. Grimes 
3004b88c807SRodney W. Grimes 
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes /*
3034b88c807SRodney W. Grimes  * Controls whether the shell is interactive or not.
3044b88c807SRodney W. Grimes  */
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes void
3084b88c807SRodney W. Grimes setinteractive(on) {
3094b88c807SRodney W. Grimes 	static int is_interactive;
3104b88c807SRodney W. Grimes 
3114b88c807SRodney W. Grimes 	if (on == is_interactive)
3124b88c807SRodney W. Grimes 		return;
3134b88c807SRodney W. Grimes 	setsignal(SIGINT);
3144b88c807SRodney W. Grimes 	setsignal(SIGQUIT);
3154b88c807SRodney W. Grimes 	setsignal(SIGTERM);
3164b88c807SRodney W. Grimes 	is_interactive = on;
3174b88c807SRodney W. Grimes }
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes 
3214b88c807SRodney W. Grimes /*
3224b88c807SRodney W. Grimes  * Called to exit the shell.
3234b88c807SRodney W. Grimes  */
3244b88c807SRodney W. Grimes 
3254b88c807SRodney W. Grimes void
3264b88c807SRodney W. Grimes exitshell(status) {
3274b88c807SRodney W. Grimes 	struct jmploc loc1, loc2;
3284b88c807SRodney W. Grimes 	char *p;
3294b88c807SRodney W. Grimes 
3304b88c807SRodney W. Grimes 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
3314b88c807SRodney W. Grimes 	if (setjmp(loc1.loc)) {
3324b88c807SRodney W. Grimes 		goto l1;
3334b88c807SRodney W. Grimes 	}
3344b88c807SRodney W. Grimes 	if (setjmp(loc2.loc)) {
3354b88c807SRodney W. Grimes 		goto l2;
3364b88c807SRodney W. Grimes 	}
3374b88c807SRodney W. Grimes 	handler = &loc1;
3384b88c807SRodney W. Grimes 	if ((p = trap[0]) != NULL && *p != '\0') {
3394b88c807SRodney W. Grimes 		trap[0] = NULL;
3404b88c807SRodney W. Grimes 		evalstring(p);
3414b88c807SRodney W. Grimes 	}
3424b88c807SRodney W. Grimes l1:   handler = &loc2;			/* probably unnecessary */
3434b88c807SRodney W. Grimes 	flushall();
3444b88c807SRodney W. Grimes #if JOBS
3454b88c807SRodney W. Grimes 	setjobctl(0);
3464b88c807SRodney W. Grimes #endif
3474b88c807SRodney W. Grimes l2:   _exit(status);
3484b88c807SRodney W. Grimes }
349