trap.c revision 199641
1145516Sdarrenr/*-
2255332Scy * Copyright (c) 1991, 1993
3145516Sdarrenr *	The Regents of the University of California.  All rights reserved.
4145516Sdarrenr *
5145516Sdarrenr * This code is derived from software contributed to Berkeley by
6145516Sdarrenr * Kenneth Almquist.
7255332Scy *
8145516Sdarrenr * Redistribution and use in source and binary forms, with or without
9145516Sdarrenr * modification, are permitted provided that the following conditions
10145516Sdarrenr * are met:
11145516Sdarrenr * 1. Redistributions of source code must retain the above copyright
12145516Sdarrenr *    notice, this list of conditions and the following disclaimer.
13145516Sdarrenr * 2. Redistributions in binary form must reproduce the above copyright
14145516Sdarrenr *    notice, this list of conditions and the following disclaimer in the
15145516Sdarrenr *    documentation and/or other materials provided with the distribution.
16145516Sdarrenr * 4. Neither the name of the University nor the names of its contributors
17145516Sdarrenr *    may be used to endorse or promote products derived from this software
18145516Sdarrenr *    without specific prior written permission.
19145516Sdarrenr *
20145516Sdarrenr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21145516Sdarrenr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22145516Sdarrenr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23145516Sdarrenr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24145516Sdarrenr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25145516Sdarrenr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26145516Sdarrenr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27145516Sdarrenr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28145516Sdarrenr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29145516Sdarrenr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30145516Sdarrenr * SUCH DAMAGE.
31145516Sdarrenr */
32145516Sdarrenr
33145516Sdarrenr#ifndef lint
34145516Sdarrenr#if 0
35145516Sdarrenrstatic char sccsid[] = "@(#)trap.c	8.5 (Berkeley) 6/5/95";
36145516Sdarrenr#endif
37145516Sdarrenr#endif /* not lint */
38145516Sdarrenr#include <sys/cdefs.h>
39255332Scy__FBSDID("$FreeBSD: head/bin/sh/trap.c 199641 2009-11-21 20:44:34Z jilles $");
40145516Sdarrenr
41145516Sdarrenr#include <signal.h>
42145516Sdarrenr#include <unistd.h>
43145516Sdarrenr#include <stdlib.h>
44145516Sdarrenr
45145516Sdarrenr#include "shell.h"
46145516Sdarrenr#include "main.h"
47145516Sdarrenr#include "nodes.h"	/* for other headers */
48145516Sdarrenr#include "eval.h"
49145516Sdarrenr#include "jobs.h"
50145516Sdarrenr#include "show.h"
51145516Sdarrenr#include "options.h"
52145516Sdarrenr#include "syntax.h"
53145516Sdarrenr#include "output.h"
54145516Sdarrenr#include "memalloc.h"
55145516Sdarrenr#include "error.h"
56145516Sdarrenr#include "trap.h"
57145516Sdarrenr#include "mystring.h"
58145516Sdarrenr#include "myhistedit.h"
59145516Sdarrenr
60145516Sdarrenr
61145516Sdarrenr/*
62145516Sdarrenr * Sigmode records the current value of the signal handlers for the various
63145516Sdarrenr * modes.  A value of zero means that the current handler is not known.
64145516Sdarrenr * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
65145516Sdarrenr */
66145516Sdarrenr
67145516Sdarrenr#define S_DFL 1			/* default signal handling (SIG_DFL) */
68145516Sdarrenr#define S_CATCH 2		/* signal is caught */
69145516Sdarrenr#define S_IGN 3			/* signal is ignored (SIG_IGN) */
70145516Sdarrenr#define S_HARD_IGN 4		/* signal is ignored permanently */
71145516Sdarrenr#define S_RESET 5		/* temporary - to reset a hard ignored sig */
72145516Sdarrenr
73145516Sdarrenr
74145516SdarrenrMKINIT char sigmode[NSIG];	/* current value of signal */
75145516Sdarrenrint pendingsigs;		/* indicates some signal received */
76145516Sdarrenrint in_dotrap;			/* do we execute in a trap handler? */
77145516Sdarrenrstatic char *volatile trap[NSIG];	/* trap handler commands */
78145516Sdarrenrstatic volatile sig_atomic_t gotsig[NSIG];
79145516Sdarrenr				/* indicates specified signal received */
80145516Sdarrenrstatic int ignore_sigchld;	/* Used while handling SIGCHLD traps. */
81145516Sdarrenrvolatile sig_atomic_t gotwinch;
82145516Sdarrenr
83145516Sdarrenrstatic int getsigaction(int, sig_t *);
84145516Sdarrenr
85145516Sdarrenr
86145516Sdarrenr/*
87145516Sdarrenr * Map a string to a signal number.
88145516Sdarrenr *
89145516Sdarrenr * Note: the signal number may exceed NSIG.
90145516Sdarrenr */
91145516Sdarrenrstatic int
92145516Sdarrenrsigstring_to_signum(char *sig)
93145516Sdarrenr{
94145516Sdarrenr
95145516Sdarrenr	if (is_number(sig)) {
96145516Sdarrenr		int signo;
97145516Sdarrenr
98145516Sdarrenr		signo = atoi(sig);
99145516Sdarrenr		return ((signo >= 0 && signo < NSIG) ? signo : (-1));
100145516Sdarrenr	} else if (strcasecmp(sig, "exit") == 0) {
101145516Sdarrenr		return (0);
102145516Sdarrenr	} else {
103255332Scy		int n;
104255332Scy
105255332Scy		if (strncasecmp(sig, "sig", 3) == 0)
106255332Scy			sig += 3;
107255332Scy		for (n = 1; n < sys_nsig; n++)
108255332Scy			if (sys_signame[n] &&
109255332Scy			    strcasecmp(sys_signame[n], sig) == 0)
110255332Scy				return (n);
111255332Scy	}
112255332Scy	return (-1);
113255332Scy}
114255332Scy
115255332Scy
116255332Scy/*
117255332Scy * Print a list of valid signal names.
118255332Scy */
119255332Scystatic void
120145516Sdarrenrprintsignals(void)
121255332Scy{
122	int n, outlen;
123
124	outlen = 0;
125	for (n = 1; n < sys_nsig; n++) {
126		if (sys_signame[n]) {
127			out1fmt("%s", sys_signame[n]);
128			outlen += strlen(sys_signame[n]);
129		} else {
130			out1fmt("%d", n);
131			outlen += 3;	/* good enough */
132		}
133		++outlen;
134		if (outlen > 70 || n == sys_nsig - 1) {
135			out1str("\n");
136			outlen = 0;
137		} else {
138			out1c(' ');
139		}
140	}
141}
142
143
144/*
145 * The trap builtin.
146 */
147int
148trapcmd(int argc, char **argv)
149{
150	char *action;
151	int signo;
152	int errors = 0;
153
154	if (argc <= 1) {
155		for (signo = 0 ; signo < sys_nsig ; signo++) {
156			if (signo < NSIG && trap[signo] != NULL) {
157				out1str("trap -- ");
158				out1qstr(trap[signo]);
159				if (signo == 0) {
160					out1str(" exit\n");
161				} else if (sys_signame[signo]) {
162					out1fmt(" %s\n", sys_signame[signo]);
163				} else {
164					out1fmt(" %d\n", signo);
165				}
166			}
167		}
168		return 0;
169	}
170	action = NULL;
171	if (*++argv && strcmp(*argv, "--") == 0)
172		argv++;
173	if (*argv && sigstring_to_signum(*argv) == -1) {
174		if ((*argv)[0] != '-') {
175			action = *argv;
176			argv++;
177		} else if ((*argv)[1] == '\0') {
178			argv++;
179		} else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
180			printsignals();
181			return 0;
182		} else {
183			error("bad option %s", *argv);
184		}
185	}
186	while (*argv) {
187		if ((signo = sigstring_to_signum(*argv)) == -1) {
188			out2fmt_flush("trap: bad signal %s\n", *argv);
189			errors = 1;
190		}
191		INTOFF;
192		if (action)
193			action = savestr(action);
194		if (trap[signo])
195			ckfree(trap[signo]);
196		trap[signo] = action;
197		if (signo != 0)
198			setsignal(signo);
199		INTON;
200		argv++;
201	}
202	return errors;
203}
204
205
206/*
207 * Clear traps on a fork.
208 */
209void
210clear_traps(void)
211{
212	char *volatile *tp;
213
214	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
215		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
216			INTOFF;
217			ckfree(*tp);
218			*tp = NULL;
219			if (tp != &trap[0])
220				setsignal(tp - trap);
221			INTON;
222		}
223	}
224}
225
226
227/*
228 * Check if we have any traps enabled.
229 */
230int
231have_traps(void)
232{
233	char *volatile *tp;
234
235	for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
236		if (*tp && **tp)	/* trap not NULL or SIG_IGN */
237			return 1;
238	}
239	return 0;
240}
241
242/*
243 * Set the signal handler for the specified signal.  The routine figures
244 * out what it should be set to.
245 */
246void
247setsignal(int signo)
248{
249	int action;
250	sig_t sigact = SIG_DFL;
251	struct sigaction sa;
252	char *t;
253
254	if ((t = trap[signo]) == NULL)
255		action = S_DFL;
256	else if (*t != '\0')
257		action = S_CATCH;
258	else
259		action = S_IGN;
260	if (action == S_DFL) {
261		switch (signo) {
262		case SIGINT:
263			action = S_CATCH;
264			break;
265		case SIGQUIT:
266#ifdef DEBUG
267			{
268			extern int debug;
269
270			if (debug)
271				break;
272			}
273#endif
274			action = S_CATCH;
275			break;
276		case SIGTERM:
277			if (rootshell && iflag)
278				action = S_IGN;
279			break;
280#if JOBS
281		case SIGTSTP:
282		case SIGTTOU:
283			if (rootshell && mflag)
284				action = S_IGN;
285			break;
286#endif
287#ifndef NO_HISTORY
288		case SIGWINCH:
289			if (rootshell && iflag)
290				action = S_CATCH;
291			break;
292#endif
293		}
294	}
295
296	t = &sigmode[signo];
297	if (*t == 0) {
298		/*
299		 * current setting unknown
300		 */
301		if (!getsigaction(signo, &sigact)) {
302			/*
303			 * Pretend it worked; maybe we should give a warning
304			 * here, but other shells don't. We don't alter
305			 * sigmode, so that we retry every time.
306			 */
307			return;
308		}
309		if (sigact == SIG_IGN) {
310			if (mflag && (signo == SIGTSTP ||
311			     signo == SIGTTIN || signo == SIGTTOU)) {
312				*t = S_IGN;	/* don't hard ignore these */
313			} else
314				*t = S_HARD_IGN;
315		} else {
316			*t = S_RESET;	/* force to be set */
317		}
318	}
319	if (*t == S_HARD_IGN || *t == action)
320		return;
321	switch (action) {
322		case S_DFL:	sigact = SIG_DFL;	break;
323		case S_CATCH:  	sigact = onsig;		break;
324		case S_IGN:	sigact = SIG_IGN;	break;
325	}
326	*t = action;
327	sa.sa_handler = sigact;
328	sa.sa_flags = 0;
329	sigemptyset(&sa.sa_mask);
330	sigaction(signo, &sa, NULL);
331}
332
333
334/*
335 * Return the current setting for sig w/o changing it.
336 */
337static int
338getsigaction(int signo, sig_t *sigact)
339{
340	struct sigaction sa;
341
342	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
343		return 0;
344	*sigact = (sig_t) sa.sa_handler;
345	return 1;
346}
347
348
349/*
350 * Ignore a signal.
351 */
352void
353ignoresig(int signo)
354{
355
356	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
357		signal(signo, SIG_IGN);
358	}
359	sigmode[signo] = S_HARD_IGN;
360}
361
362
363#ifdef mkinit
364INCLUDE <signal.h>
365INCLUDE "trap.h"
366
367SHELLPROC {
368	char *sm;
369
370	clear_traps();
371	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
372		if (*sm == S_IGN)
373			*sm = S_HARD_IGN;
374	}
375}
376#endif
377
378
379/*
380 * Signal handler.
381 */
382void
383onsig(int signo)
384{
385
386	if (signo == SIGINT && trap[SIGINT] == NULL) {
387		onint();
388		return;
389	}
390
391	if (signo != SIGCHLD || !ignore_sigchld)
392		gotsig[signo] = 1;
393	pendingsigs++;
394
395	/* If we are currently in a wait builtin, prepare to break it */
396	if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
397		breakwaitcmd = 1;
398	/*
399	 * If a trap is set, not ignored and not the null command, we need
400	 * to make sure traps are executed even when a child blocks signals.
401	 */
402	if (Tflag &&
403	    trap[signo] != NULL &&
404	    ! (trap[signo][0] == '\0') &&
405	    ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
406		breakwaitcmd = 1;
407
408#ifndef NO_HISTORY
409	if (signo == SIGWINCH)
410		gotwinch = 1;
411#endif
412}
413
414
415/*
416 * Called to execute a trap.  Perhaps we should avoid entering new trap
417 * handlers while we are executing a trap handler.
418 */
419void
420dotrap(void)
421{
422	int i;
423	int savestatus;
424
425	in_dotrap++;
426	for (;;) {
427		for (i = 1; i < NSIG; i++) {
428			if (gotsig[i]) {
429				gotsig[i] = 0;
430				if (trap[i]) {
431					/*
432					 * Ignore SIGCHLD to avoid infinite
433					 * recursion if the trap action does
434					 * a fork.
435					 */
436					if (i == SIGCHLD)
437						ignore_sigchld++;
438					savestatus = exitstatus;
439					evalstring(trap[i], 0);
440					exitstatus = savestatus;
441					if (i == SIGCHLD)
442						ignore_sigchld--;
443				}
444				break;
445			}
446		}
447		if (i >= NSIG)
448			break;
449	}
450	in_dotrap--;
451	pendingsigs = 0;
452}
453
454
455/*
456 * Controls whether the shell is interactive or not.
457 */
458void
459setinteractive(int on)
460{
461	static int is_interactive = -1;
462
463	if (on == is_interactive)
464		return;
465	setsignal(SIGINT);
466	setsignal(SIGQUIT);
467	setsignal(SIGTERM);
468#ifndef NO_HISTORY
469	setsignal(SIGWINCH);
470#endif
471	is_interactive = on;
472}
473
474
475/*
476 * Called to exit the shell.
477 */
478void
479exitshell(int status)
480{
481	struct jmploc loc1, loc2;
482	char *p;
483
484	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
485	if (setjmp(loc1.loc)) {
486		goto l1;
487	}
488	if (setjmp(loc2.loc)) {
489		goto l2;
490	}
491	handler = &loc1;
492	if ((p = trap[0]) != NULL && *p != '\0') {
493		trap[0] = NULL;
494		evalstring(p, 0);
495	}
496l1:   handler = &loc2;			/* probably unnecessary */
497	flushall();
498#if JOBS
499	setjobctl(0);
500#endif
501l2:   _exit(status);
502}
503