trap.c revision 213811
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)trap.c	8.5 (Berkeley) 6/5/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/trap.c 213811 2010-10-13 22:18:03Z obrien $");
401556Srgrimes
4117987Speter#include <signal.h>
4217987Speter#include <unistd.h>
4317987Speter#include <stdlib.h>
4417987Speter
451556Srgrimes#include "shell.h"
461556Srgrimes#include "main.h"
471556Srgrimes#include "nodes.h"	/* for other headers */
481556Srgrimes#include "eval.h"
491556Srgrimes#include "jobs.h"
5017987Speter#include "show.h"
511556Srgrimes#include "options.h"
521556Srgrimes#include "syntax.h"
531556Srgrimes#include "output.h"
541556Srgrimes#include "memalloc.h"
551556Srgrimes#include "error.h"
561556Srgrimes#include "trap.h"
571556Srgrimes#include "mystring.h"
58100578Stjr#include "myhistedit.h"
591556Srgrimes
601556Srgrimes
611556Srgrimes/*
621556Srgrimes * Sigmode records the current value of the signal handlers for the various
631556Srgrimes * modes.  A value of zero means that the current handler is not known.
641556Srgrimes * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
651556Srgrimes */
661556Srgrimes
671556Srgrimes#define S_DFL 1			/* default signal handling (SIG_DFL) */
681556Srgrimes#define S_CATCH 2		/* signal is caught */
691556Srgrimes#define S_IGN 3			/* signal is ignored (SIG_IGN) */
7046684Skris#define S_HARD_IGN 4		/* signal is ignored permanently */
711556Srgrimes#define S_RESET 5		/* temporary - to reset a hard ignored sig */
721556Srgrimes
731556Srgrimes
7417987SpeterMKINIT char sigmode[NSIG];	/* current value of signal */
7538521Scracauerint pendingsigs;		/* indicates some signal received */
7638536Scracauerint in_dotrap;			/* do we execute in a trap handler? */
7738950Scracauerstatic char *volatile trap[NSIG];	/* trap handler commands */
78157811Sschweikhstatic volatile sig_atomic_t gotsig[NSIG];
7938521Scracauer				/* indicates specified signal received */
8020902Sstevestatic int ignore_sigchld;	/* Used while handling SIGCHLD traps. */
81100588Stjrvolatile sig_atomic_t gotwinch;
821556Srgrimes
83213811Sobrienstatic int getsigaction(int, sig_t *);
8417987Speter
8520902Ssteve
861556Srgrimes/*
8720902Ssteve * Map a string to a signal number.
88125727Snjl *
89125727Snjl * Note: the signal number may exceed NSIG.
9020902Ssteve */
91213811Sobrienstatic int
9290111Simpsigstring_to_signum(char *sig)
9320902Ssteve{
9420902Ssteve
9520902Ssteve	if (is_number(sig)) {
9620902Ssteve		int signo;
9720902Ssteve
9820902Ssteve		signo = atoi(sig);
9920902Ssteve		return ((signo >= 0 && signo < NSIG) ? signo : (-1));
10020902Ssteve	} else if (strcasecmp(sig, "exit") == 0) {
10120902Ssteve		return (0);
10220902Ssteve	} else {
10320902Ssteve		int n;
10420902Ssteve
10520902Ssteve		if (strncasecmp(sig, "sig", 3) == 0)
10620902Ssteve			sig += 3;
107125155Snjl		for (n = 1; n < sys_nsig; n++)
108125728Snjl			if (sys_signame[n] &&
109125728Snjl			    strcasecmp(sys_signame[n], sig) == 0)
11020902Ssteve				return (n);
11120902Ssteve	}
11220902Ssteve	return (-1);
11320902Ssteve}
11420902Ssteve
11520902Ssteve
11620902Ssteve/*
11720902Ssteve * Print a list of valid signal names.
11820902Ssteve */
119213811Sobrienstatic void
12090111Simpprintsignals(void)
12120902Ssteve{
122125727Snjl	int n, outlen;
12320902Ssteve
124125727Snjl	outlen = 0;
125125155Snjl	for (n = 1; n < sys_nsig; n++) {
126125727Snjl		if (sys_signame[n]) {
127125727Snjl			out1fmt("%s", sys_signame[n]);
128125727Snjl			outlen += strlen(sys_signame[n]);
129125727Snjl		} else {
130125727Snjl			out1fmt("%d", n);
131125727Snjl			outlen += 3;	/* good enough */
132125727Snjl		}
133125727Snjl		++outlen;
134125727Snjl		if (outlen > 70 || n == sys_nsig - 1) {
13520902Ssteve			out1str("\n");
136125727Snjl			outlen = 0;
137125727Snjl		} else {
13820902Ssteve			out1c(' ');
139125727Snjl		}
14020902Ssteve	}
14120902Ssteve}
14220902Ssteve
14320902Ssteve
14420902Ssteve/*
1451556Srgrimes * The trap builtin.
1461556Srgrimes */
14717987Speterint
14890111Simptrapcmd(int argc, char **argv)
14917987Speter{
1501556Srgrimes	char *action;
1511556Srgrimes	int signo;
152199641Sjilles	int errors = 0;
1531556Srgrimes
1541556Srgrimes	if (argc <= 1) {
155125155Snjl		for (signo = 0 ; signo < sys_nsig ; signo++) {
156125727Snjl			if (signo < NSIG && trap[signo] != NULL) {
157153244Sstefanf				out1str("trap -- ");
158153244Sstefanf				out1qstr(trap[signo]);
159125727Snjl				if (signo == 0) {
160153244Sstefanf					out1str(" exit\n");
161125727Snjl				} else if (sys_signame[signo]) {
162153244Sstefanf					out1fmt(" %s\n", sys_signame[signo]);
163125727Snjl				} else {
164153244Sstefanf					out1fmt(" %d\n", signo);
165125727Snjl				}
166125727Snjl			}
1671556Srgrimes		}
1681556Srgrimes		return 0;
1691556Srgrimes	}
17020902Ssteve	action = NULL;
17120902Ssteve	if (*++argv && strcmp(*argv, "--") == 0)
17220902Ssteve		argv++;
17320902Ssteve	if (*argv && sigstring_to_signum(*argv) == -1) {
17420902Ssteve		if ((*argv)[0] != '-') {
17520902Ssteve			action = *argv;
17620902Ssteve			argv++;
17720902Ssteve		} else if ((*argv)[1] == '\0') {
17820902Ssteve			argv++;
17920902Ssteve		} else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
18020902Ssteve			printsignals();
18120902Ssteve			return 0;
18220902Ssteve		} else {
18320902Ssteve			error("bad option %s", *argv);
18420902Ssteve		}
18520902Ssteve	}
18620902Ssteve	while (*argv) {
187199641Sjilles		if ((signo = sigstring_to_signum(*argv)) == -1) {
188199641Sjilles			out2fmt_flush("trap: bad signal %s\n", *argv);
189199641Sjilles			errors = 1;
190199641Sjilles		}
1911556Srgrimes		INTOFF;
1921556Srgrimes		if (action)
1931556Srgrimes			action = savestr(action);
1941556Srgrimes		if (trap[signo])
1951556Srgrimes			ckfree(trap[signo]);
1961556Srgrimes		trap[signo] = action;
1971556Srgrimes		if (signo != 0)
1981556Srgrimes			setsignal(signo);
1991556Srgrimes		INTON;
20020902Ssteve		argv++;
2011556Srgrimes	}
202199641Sjilles	return errors;
2031556Srgrimes}
2041556Srgrimes
2051556Srgrimes
2061556Srgrimes/*
2071556Srgrimes * Clear traps on a fork.
2081556Srgrimes */
2091556Srgrimesvoid
21090111Simpclear_traps(void)
21120902Ssteve{
21238950Scracauer	char *volatile *tp;
2131556Srgrimes
21420902Ssteve	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
2151556Srgrimes		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
2161556Srgrimes			INTOFF;
2171556Srgrimes			ckfree(*tp);
2181556Srgrimes			*tp = NULL;
2191556Srgrimes			if (tp != &trap[0])
2201556Srgrimes				setsignal(tp - trap);
2211556Srgrimes			INTON;
2221556Srgrimes		}
2231556Srgrimes	}
2241556Srgrimes}
2251556Srgrimes
2261556Srgrimes
2271556Srgrimes/*
228194127Sjilles * Check if we have any traps enabled.
229194127Sjilles */
230194127Sjillesint
231194127Sjilleshave_traps(void)
232194127Sjilles{
233194127Sjilles	char *volatile *tp;
234194127Sjilles
235194127Sjilles	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
236194127Sjilles		if (*tp && **tp)	/* trap not NULL or SIG_IGN */
237194127Sjilles			return 1;
238194127Sjilles	}
239194127Sjilles	return 0;
240194127Sjilles}
241194127Sjilles
242194127Sjilles/*
2431556Srgrimes * Set the signal handler for the specified signal.  The routine figures
2441556Srgrimes * out what it should be set to.
2451556Srgrimes */
24631098Sbdevoid
24790111Simpsetsignal(int signo)
24817987Speter{
2491556Srgrimes	int action;
250199205Sjilles	sig_t sigact = SIG_DFL;
251199205Sjilles	struct sigaction sa;
2521556Srgrimes	char *t;
2531556Srgrimes
2541556Srgrimes	if ((t = trap[signo]) == NULL)
2551556Srgrimes		action = S_DFL;
2561556Srgrimes	else if (*t != '\0')
2571556Srgrimes		action = S_CATCH;
2581556Srgrimes	else
2591556Srgrimes		action = S_IGN;
26038521Scracauer	if (action == S_DFL) {
2611556Srgrimes		switch (signo) {
2621556Srgrimes		case SIGINT:
26338521Scracauer			action = S_CATCH;
2641556Srgrimes			break;
2651556Srgrimes		case SIGQUIT:
2661556Srgrimes#ifdef DEBUG
2671556Srgrimes			{
2681556Srgrimes			extern int debug;
2691556Srgrimes
2701556Srgrimes			if (debug)
2711556Srgrimes				break;
2721556Srgrimes			}
2731556Srgrimes#endif
27438535Scracauer			action = S_CATCH;
27538521Scracauer			break;
2761556Srgrimes		case SIGTERM:
27738521Scracauer			if (rootshell && iflag)
2781556Srgrimes				action = S_IGN;
2791556Srgrimes			break;
2801556Srgrimes#if JOBS
2811556Srgrimes		case SIGTSTP:
2821556Srgrimes		case SIGTTOU:
28338521Scracauer			if (rootshell && mflag)
2841556Srgrimes				action = S_IGN;
2851556Srgrimes			break;
2861556Srgrimes#endif
287100578Stjr#ifndef NO_HISTORY
288100578Stjr		case SIGWINCH:
289100588Stjr			if (rootshell && iflag)
290100578Stjr				action = S_CATCH;
291100578Stjr			break;
292100578Stjr#endif
2931556Srgrimes		}
2941556Srgrimes	}
29517987Speter
29620902Ssteve	t = &sigmode[signo];
2978855Srgrimes	if (*t == 0) {
2988855Srgrimes		/*
2998855Srgrimes		 * current setting unknown
3001556Srgrimes		 */
30117987Speter		if (!getsigaction(signo, &sigact)) {
30217987Speter			/*
30317987Speter			 * Pretend it worked; maybe we should give a warning
30417987Speter			 * here, but other shells don't. We don't alter
30517987Speter			 * sigmode, so that we retry every time.
30617987Speter			 */
30731098Sbde			return;
30817987Speter		}
3091556Srgrimes		if (sigact == SIG_IGN) {
3108855Srgrimes			if (mflag && (signo == SIGTSTP ||
3111556Srgrimes			     signo == SIGTTIN || signo == SIGTTOU)) {
3121556Srgrimes				*t = S_IGN;	/* don't hard ignore these */
3131556Srgrimes			} else
3141556Srgrimes				*t = S_HARD_IGN;
3151556Srgrimes		} else {
3161556Srgrimes			*t = S_RESET;	/* force to be set */
3171556Srgrimes		}
3181556Srgrimes	}
3191556Srgrimes	if (*t == S_HARD_IGN || *t == action)
32031098Sbde		return;
3211556Srgrimes	switch (action) {
3221556Srgrimes		case S_DFL:	sigact = SIG_DFL;	break;
3231556Srgrimes		case S_CATCH:  	sigact = onsig;		break;
3241556Srgrimes		case S_IGN:	sigact = SIG_IGN;	break;
3251556Srgrimes	}
3261556Srgrimes	*t = action;
327199205Sjilles	sa.sa_handler = sigact;
328199205Sjilles	sa.sa_flags = 0;
329199205Sjilles	sigemptyset(&sa.sa_mask);
330199205Sjilles	sigaction(signo, &sa, NULL);
3311556Srgrimes}
3321556Srgrimes
33320902Ssteve
3341556Srgrimes/*
3351556Srgrimes * Return the current setting for sig w/o changing it.
3361556Srgrimes */
337213811Sobrienstatic int
33890111Simpgetsigaction(int signo, sig_t *sigact)
33917987Speter{
3401556Srgrimes	struct sigaction sa;
3411556Srgrimes
3421556Srgrimes	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
34317987Speter		return 0;
34417987Speter	*sigact = (sig_t) sa.sa_handler;
34517987Speter	return 1;
3461556Srgrimes}
3471556Srgrimes
34820902Ssteve
3491556Srgrimes/*
3501556Srgrimes * Ignore a signal.
3511556Srgrimes */
3521556Srgrimesvoid
35390111Simpignoresig(int signo)
35417987Speter{
35520902Ssteve
35620902Ssteve	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
3571556Srgrimes		signal(signo, SIG_IGN);
3581556Srgrimes	}
35920902Ssteve	sigmode[signo] = S_HARD_IGN;
3601556Srgrimes}
3611556Srgrimes
3621556Srgrimes
3631556Srgrimes#ifdef mkinit
36417987SpeterINCLUDE <signal.h>
3651556SrgrimesINCLUDE "trap.h"
3661556Srgrimes
3671556SrgrimesSHELLPROC {
3681556Srgrimes	char *sm;
3691556Srgrimes
3701556Srgrimes	clear_traps();
37117987Speter	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
3721556Srgrimes		if (*sm == S_IGN)
3731556Srgrimes			*sm = S_HARD_IGN;
3741556Srgrimes	}
3751556Srgrimes}
3761556Srgrimes#endif
3771556Srgrimes
3781556Srgrimes
3791556Srgrimes/*
3801556Srgrimes * Signal handler.
3811556Srgrimes */
3821556Srgrimesvoid
38390111Simponsig(int signo)
38417987Speter{
38531098Sbde
3861556Srgrimes	if (signo == SIGINT && trap[SIGINT] == NULL) {
3871556Srgrimes		onint();
3881556Srgrimes		return;
3891556Srgrimes	}
39038950Scracauer
39120902Ssteve	if (signo != SIGCHLD || !ignore_sigchld)
39220902Ssteve		gotsig[signo] = 1;
3931556Srgrimes	pendingsigs++;
39438950Scracauer
39538950Scracauer	/* If we are currently in a wait builtin, prepare to break it */
39638950Scracauer	if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
39738521Scracauer		breakwaitcmd = 1;
398157811Sschweikh	/*
399157811Sschweikh	 * If a trap is set, not ignored and not the null command, we need
40039056Scracauer	 * to make sure traps are executed even when a child blocks signals.
40138950Scracauer	 */
40245221Scracauer	if (Tflag &&
403157811Sschweikh	    trap[signo] != NULL &&
404149825Srse	    ! (trap[signo][0] == '\0') &&
40539049Scracauer	    ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
40639049Scracauer		breakwaitcmd = 1;
407100578Stjr
408100578Stjr#ifndef NO_HISTORY
409100578Stjr	if (signo == SIGWINCH)
410100588Stjr		gotwinch = 1;
411100578Stjr#endif
4121556Srgrimes}
4131556Srgrimes
4141556Srgrimes
4151556Srgrimes/*
4161556Srgrimes * Called to execute a trap.  Perhaps we should avoid entering new trap
4171556Srgrimes * handlers while we are executing a trap handler.
4181556Srgrimes */
4191556Srgrimesvoid
42090111Simpdotrap(void)
42120902Ssteve{
4221556Srgrimes	int i;
4231556Srgrimes	int savestatus;
4241556Srgrimes
42538521Scracauer	in_dotrap++;
4261556Srgrimes	for (;;) {
42720902Ssteve		for (i = 1; i < NSIG; i++) {
42820902Ssteve			if (gotsig[i]) {
42920902Ssteve				gotsig[i] = 0;
43020902Ssteve				if (trap[i]) {
43120902Ssteve					/*
432125728Snjl					 * Ignore SIGCHLD to avoid infinite
433125728Snjl					 * recursion if the trap action does
434125728Snjl					 * a fork.
43520902Ssteve					 */
43620902Ssteve					if (i == SIGCHLD)
43720902Ssteve						ignore_sigchld++;
43820902Ssteve					savestatus = exitstatus;
439193169Sstefanf					evalstring(trap[i], 0);
44020902Ssteve					exitstatus = savestatus;
44120902Ssteve					if (i == SIGCHLD)
44220902Ssteve						ignore_sigchld--;
44320902Ssteve				}
4441556Srgrimes				break;
44520902Ssteve			}
4461556Srgrimes		}
44720902Ssteve		if (i >= NSIG)
44820902Ssteve			break;
4491556Srgrimes	}
45038521Scracauer	in_dotrap--;
4511556Srgrimes	pendingsigs = 0;
4521556Srgrimes}
4531556Srgrimes
4541556Srgrimes
4551556Srgrimes/*
4561556Srgrimes * Controls whether the shell is interactive or not.
4571556Srgrimes */
4581556Srgrimesvoid
45990111Simpsetinteractive(int on)
46017987Speter{
46138521Scracauer	static int is_interactive = -1;
4621556Srgrimes
4631556Srgrimes	if (on == is_interactive)
4641556Srgrimes		return;
4651556Srgrimes	setsignal(SIGINT);
4661556Srgrimes	setsignal(SIGQUIT);
4671556Srgrimes	setsignal(SIGTERM);
468100578Stjr#ifndef NO_HISTORY
469100578Stjr	setsignal(SIGWINCH);
470100578Stjr#endif
4711556Srgrimes	is_interactive = on;
4721556Srgrimes}
4731556Srgrimes
4741556Srgrimes
4751556Srgrimes/*
4761556Srgrimes * Called to exit the shell.
4771556Srgrimes */
4781556Srgrimesvoid
47990111Simpexitshell(int status)
48017987Speter{
4811556Srgrimes	struct jmploc loc1, loc2;
4821556Srgrimes	char *p;
4831556Srgrimes
4841556Srgrimes	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
4851556Srgrimes	if (setjmp(loc1.loc)) {
4861556Srgrimes		goto l1;
4871556Srgrimes	}
4881556Srgrimes	if (setjmp(loc2.loc)) {
4891556Srgrimes		goto l2;
4901556Srgrimes	}
4911556Srgrimes	handler = &loc1;
4921556Srgrimes	if ((p = trap[0]) != NULL && *p != '\0') {
4931556Srgrimes		trap[0] = NULL;
494193169Sstefanf		evalstring(p, 0);
4951556Srgrimes	}
4961556Srgrimesl1:   handler = &loc2;			/* probably unnecessary */
4971556Srgrimes	flushall();
4981556Srgrimes#if JOBS
4991556Srgrimes	setjobctl(0);
5001556Srgrimes#endif
5011556Srgrimesl2:   _exit(status);
5021556Srgrimes}
503