Home | History | Annotate | Line # | Download | only in sh
trap.c revision 1.49
      1  1.48       kre /*	$NetBSD: trap.c,v 1.49 2018/12/05 22:25:38 kre Exp $	*/
      2  1.12       cgd 
      3   1.1       cgd /*-
      4   1.6       jtc  * Copyright (c) 1991, 1993
      5   1.6       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.17  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.12       cgd #if 0
     38  1.15  christos static char sccsid[] = "@(#)trap.c	8.5 (Berkeley) 6/5/95";
     39  1.12       cgd #else
     40  1.48       kre __RCSID("$NetBSD: trap.c,v 1.49 2018/12/05 22:25:38 kre Exp $");
     41  1.12       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44  1.13  christos #include <signal.h>
     45  1.13  christos #include <unistd.h>
     46  1.13  christos #include <stdlib.h>
     47  1.40       kre #include <stdio.h>
     48  1.47       kre #include <errno.h>
     49  1.47       kre 
     50  1.47       kre #include <sys/resource.h>
     51  1.13  christos 
     52   1.1       cgd #include "shell.h"
     53   1.1       cgd #include "main.h"
     54   1.1       cgd #include "nodes.h"	/* for other headers */
     55   1.1       cgd #include "eval.h"
     56   1.1       cgd #include "jobs.h"
     57  1.13  christos #include "show.h"
     58   1.1       cgd #include "options.h"
     59  1.35  christos #include "builtins.h"
     60   1.1       cgd #include "syntax.h"
     61   1.1       cgd #include "output.h"
     62   1.1       cgd #include "memalloc.h"
     63   1.1       cgd #include "error.h"
     64   1.1       cgd #include "trap.h"
     65   1.1       cgd #include "mystring.h"
     66  1.31  christos #include "var.h"
     67   1.1       cgd 
     68   1.1       cgd 
     69   1.1       cgd /*
     70   1.1       cgd  * Sigmode records the current value of the signal handlers for the various
     71   1.1       cgd  * modes.  A value of zero means that the current handler is not known.
     72   1.1       cgd  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
     73   1.1       cgd  */
     74   1.1       cgd 
     75   1.1       cgd #define S_DFL 1			/* default signal handling (SIG_DFL) */
     76   1.1       cgd #define S_CATCH 2		/* signal is caught */
     77   1.1       cgd #define S_IGN 3			/* signal is ignored (SIG_IGN) */
     78   1.1       cgd #define S_HARD_IGN 4		/* signal is ignored permenantly */
     79   1.6       jtc #define S_RESET 5		/* temporary - to reset a hard ignored sig */
     80   1.1       cgd 
     81   1.1       cgd 
     82   1.8       jtc MKINIT char sigmode[NSIG];	/* current value of signal */
     83  1.47       kre static volatile sig_atomic_t gotsig[NSIG];/* indicates specified signal received */
     84  1.47       kre volatile sig_atomic_t pendingsigs;	/* indicates some signal received */
     85  1.47       kre 
     86  1.47       kre int traps_invalid;		/* in a subshell, but trap[] not yet cleared */
     87  1.47       kre static char * volatile trap[NSIG];	/* trap handler commands */
     88  1.47       kre static int in_dotrap;
     89  1.47       kre static int last_trapsig;
     90  1.47       kre 
     91  1.47       kre static int exiting;		/* exitshell() has been done */
     92  1.47       kre static int exiting_status;	/* the status to use for exit() */
     93   1.1       cgd 
     94  1.28  christos static int getsigaction(int, sig_t *);
     95  1.40       kre STATIC const char *trap_signame(int);
     96  1.46       kre void printsignals(struct output *, int);
     97  1.26      wulf 
     98  1.26      wulf /*
     99  1.26      wulf  * return the signal number described by `p' (as a number or a name)
    100  1.26      wulf  * or -1 if it isn't one
    101  1.26      wulf  */
    102  1.26      wulf 
    103  1.26      wulf static int
    104  1.28  christos signame_to_signum(const char *p)
    105  1.26      wulf {
    106  1.26      wulf 	int i;
    107  1.26      wulf 
    108  1.26      wulf 	if (is_number(p))
    109  1.26      wulf 		return number(p);
    110  1.26      wulf 
    111  1.26      wulf 	if (strcasecmp(p, "exit") == 0 )
    112  1.26      wulf 		return 0;
    113  1.26      wulf 
    114  1.46       kre 	i = signalnumber(p);
    115  1.46       kre 	if (i == 0)
    116  1.46       kre 		i = -1;
    117  1.46       kre 	return i;
    118  1.26      wulf }
    119  1.26      wulf 
    120  1.26      wulf /*
    121  1.40       kre  * return the name of a signal used by the "trap" command
    122  1.40       kre  */
    123  1.40       kre STATIC const char *
    124  1.40       kre trap_signame(int signo)
    125  1.40       kre {
    126  1.40       kre 	static char nbuf[12];
    127  1.46       kre 	const char *p;
    128  1.40       kre 
    129  1.40       kre 	if (signo == 0)
    130  1.40       kre 		return "EXIT";
    131  1.46       kre 	p = signalname(signo);
    132  1.40       kre 	if (p != NULL)
    133  1.40       kre 		return p;
    134  1.40       kre 	(void)snprintf(nbuf, sizeof nbuf, "%d", signo);
    135  1.40       kre 	return nbuf;
    136  1.40       kre }
    137  1.40       kre 
    138  1.46       kre #if 0		/* Share the version of this in src/bin/kill/kill.c */
    139  1.40       kre /*
    140  1.26      wulf  * Print a list of valid signal names
    141  1.26      wulf  */
    142  1.46       kre void
    143  1.46       kre printsignals(struct output *out, int len)
    144  1.26      wulf {
    145  1.26      wulf 	int n;
    146  1.26      wulf 
    147  1.46       kre 	if (len != 0)
    148  1.46       kre 		outc(' ', out);
    149  1.26      wulf 	for (n = 1; n < NSIG; n++) {
    150  1.46       kre 		outfmt(out, "%s", trap_signame(n));
    151  1.26      wulf 		if ((n == NSIG/2) ||  n == (NSIG - 1))
    152  1.46       kre 			outstr("\n", out);
    153  1.26      wulf 		else
    154  1.46       kre 			outc(' ', out);
    155  1.26      wulf 	}
    156  1.26      wulf }
    157  1.46       kre #endif
    158  1.15  christos 
    159   1.1       cgd /*
    160   1.1       cgd  * The trap builtin.
    161   1.1       cgd  */
    162   1.1       cgd 
    163  1.10       cgd int
    164  1.28  christos trapcmd(int argc, char **argv)
    165  1.10       cgd {
    166   1.1       cgd 	char *action;
    167   1.1       cgd 	char **ap;
    168   1.1       cgd 	int signo;
    169  1.39       kre 	int errs = 0;
    170  1.40       kre 	int printonly = 0;
    171  1.39       kre 
    172  1.39       kre 	ap = argv + 1;
    173  1.39       kre 
    174  1.47       kre 	CTRACE(DBG_TRAP, ("trapcmd: "));
    175  1.39       kre 	if (argc == 2 && strcmp(*ap, "-l") == 0) {
    176  1.47       kre 		CTRACE(DBG_TRAP, ("-l\n"));
    177  1.46       kre 		out1str("EXIT");
    178  1.46       kre 		printsignals(out1, 4);
    179  1.39       kre 		return 0;
    180  1.39       kre 	}
    181  1.40       kre 	if (argc == 2 && strcmp(*ap, "-") == 0) {
    182  1.47       kre 		CTRACE(DBG_TRAP, ("-\n"));
    183  1.40       kre 		for (signo = 0; signo < NSIG; signo++) {
    184  1.40       kre 			if (trap[signo] == NULL)
    185  1.40       kre 				continue;
    186  1.40       kre 			INTOFF;
    187  1.40       kre 			ckfree(trap[signo]);
    188  1.40       kre 			trap[signo] = NULL;
    189  1.40       kre 			if (signo != 0)
    190  1.40       kre 				setsignal(signo, 0);
    191  1.40       kre 			INTON;
    192  1.40       kre 		}
    193  1.47       kre 		traps_invalid = 0;
    194  1.40       kre 		return 0;
    195  1.40       kre 	}
    196  1.40       kre 	if (argc >= 2 && strcmp(*ap, "-p") == 0) {
    197  1.47       kre 		CTRACE(DBG_TRAP, ("-p "));
    198  1.40       kre 		printonly = 1;
    199  1.40       kre 		ap++;
    200  1.40       kre 		argc--;
    201  1.40       kre 	}
    202  1.39       kre 
    203  1.39       kre 	if (argc > 1 && strcmp(*ap, "--") == 0) {
    204  1.39       kre 		argc--;
    205  1.39       kre 		ap++;
    206  1.39       kre 	}
    207   1.1       cgd 
    208   1.1       cgd 	if (argc <= 1) {
    209  1.40       kre 		int count;
    210  1.40       kre 
    211  1.47       kre 		CTRACE(DBG_TRAP, ("*all*\n"));
    212  1.40       kre 		if (printonly) {
    213  1.40       kre 			for (count = 0, signo = 0 ; signo < NSIG ; signo++)
    214  1.40       kre 				if (trap[signo] == NULL) {
    215  1.40       kre 					if (count == 0)
    216  1.40       kre 						out1str("trap -- -");
    217  1.40       kre 					out1fmt(" %s", trap_signame(signo));
    218  1.40       kre 					/* oh! unlucky 13 */
    219  1.40       kre 					if (++count >= 13) {
    220  1.40       kre 						out1str("\n");
    221  1.40       kre 						count = 0;
    222  1.40       kre 					}
    223  1.40       kre 				}
    224  1.40       kre 			if (count)
    225  1.40       kre 				out1str("\n");
    226  1.40       kre 		}
    227  1.40       kre 
    228  1.40       kre 		for (count = 0, signo = 0 ; signo < NSIG ; signo++)
    229  1.40       kre 			if (trap[signo] != NULL && trap[signo][0] == '\0') {
    230  1.40       kre 				if (count == 0)
    231  1.40       kre 					out1str("trap -- ''");
    232  1.40       kre 				out1fmt(" %s", trap_signame(signo));
    233  1.40       kre 				/*
    234  1.40       kre 				 * the prefix is 10 bytes, with 4 byte
    235  1.40       kre 				 * signal names (common) we have room in
    236  1.40       kre 				 * the 70 bytes left on a normal line for
    237  1.40       kre 				 * 70/(4+1) signals, that's 14, but to
    238  1.40       kre 				 * allow for the occasional longer sig name
    239  1.40       kre 				 * we output one less...
    240  1.40       kre 				 */
    241  1.40       kre 				if (++count >= 13) {
    242  1.40       kre 					out1str("\n");
    243  1.40       kre 					count = 0;
    244  1.40       kre 				}
    245  1.40       kre 			}
    246  1.40       kre 		if (count)
    247  1.40       kre 			out1str("\n");
    248  1.40       kre 
    249  1.40       kre 		for (signo = 0 ; signo < NSIG ; signo++)
    250  1.40       kre 			if (trap[signo] != NULL && trap[signo][0] != '\0') {
    251  1.40       kre 				out1str("trap -- ");
    252  1.31  christos 				print_quoted(trap[signo]);
    253  1.40       kre 				out1fmt(" %s\n", trap_signame(signo));
    254  1.31  christos 			}
    255  1.40       kre 
    256   1.1       cgd 		return 0;
    257   1.1       cgd 	}
    258  1.47       kre 	CTRACE(DBG_TRAP, ("\n"));
    259  1.26      wulf 
    260  1.26      wulf 	action = NULL;
    261  1.26      wulf 
    262  1.47       kre 	if (!printonly && traps_invalid)
    263  1.47       kre 		free_traps();
    264  1.47       kre 
    265  1.40       kre 	if (!printonly && !is_number(*ap)) {
    266  1.39       kre 		if ((*ap)[0] == '-' && (*ap)[1] == '\0')
    267  1.39       kre 			ap++;			/* reset to default */
    268  1.39       kre 		else
    269  1.39       kre 			action = *ap++;		/* can be '' for "ignore" */
    270  1.39       kre 		argc--;
    271  1.39       kre 	}
    272  1.26      wulf 
    273  1.39       kre 	if (argc < 2) {		/* there must be at least 1 condition */
    274  1.39       kre 		out2str("Usage: trap [-l]\n"
    275  1.40       kre 			"       trap -p [condition ...]\n"
    276  1.39       kre 			"       trap action condition ...\n"
    277  1.39       kre 			"       trap N condition ...\n");
    278  1.39       kre 		return 2;
    279  1.26      wulf 	}
    280  1.26      wulf 
    281  1.39       kre 
    282   1.1       cgd 	while (*ap) {
    283  1.39       kre 		signo = signame_to_signum(*ap);
    284  1.26      wulf 
    285  1.40       kre 		if (signo < 0 || signo >= NSIG) {
    286  1.39       kre 			/* This is not a fatal error, so sayeth posix */
    287  1.39       kre 			outfmt(out2, "trap: '%s' bad condition\n", *ap);
    288  1.39       kre 			errs = 1;
    289  1.40       kre 			ap++;
    290  1.40       kre 			continue;
    291  1.40       kre 		}
    292  1.40       kre 		ap++;
    293  1.40       kre 
    294  1.40       kre 		if (printonly) {
    295  1.40       kre 			out1str("trap -- ");
    296  1.40       kre 			if (trap[signo] == NULL)
    297  1.40       kre 				out1str("-");
    298  1.40       kre 			else
    299  1.40       kre 				print_quoted(trap[signo]);
    300  1.40       kre 			out1fmt(" %s\n", trap_signame(signo));
    301  1.40       kre 			continue;
    302  1.39       kre 		}
    303  1.26      wulf 
    304   1.1       cgd 		INTOFF;
    305   1.1       cgd 		if (action)
    306   1.1       cgd 			action = savestr(action);
    307  1.26      wulf 
    308  1.47       kre 		VTRACE(DBG_TRAP, ("trap for %d from %s%s%s to %s%s%s\n", signo,
    309  1.47       kre 			trap[signo] ? "'" : "", trap[signo] ? trap[signo] : "-",
    310  1.47       kre 			trap[signo] ? "'" : "", action ?  "'" : "",
    311  1.47       kre 			action ? action : "-", action ?  "'" : ""));
    312  1.47       kre 
    313   1.1       cgd 		if (trap[signo])
    314   1.1       cgd 			ckfree(trap[signo]);
    315  1.26      wulf 
    316   1.1       cgd 		trap[signo] = action;
    317  1.26      wulf 
    318   1.1       cgd 		if (signo != 0)
    319  1.27  christos 			setsignal(signo, 0);
    320   1.1       cgd 		INTON;
    321   1.1       cgd 	}
    322  1.39       kre 	return errs;
    323   1.1       cgd }
    324   1.1       cgd 
    325   1.1       cgd 
    326   1.1       cgd 
    327   1.1       cgd /*
    328  1.27  christos  * Clear traps on a fork or vfork.
    329  1.27  christos  * Takes one arg vfork, to tell it to not be destructive of
    330  1.27  christos  * the parents variables.
    331   1.1       cgd  */
    332   1.1       cgd void
    333  1.28  christos clear_traps(int vforked)
    334  1.27  christos {
    335  1.47       kre 	char * volatile *tp;
    336  1.47       kre 
    337  1.47       kre 	VTRACE(DBG_TRAP, ("clear_traps(%d)\n", vforked));
    338  1.47       kre 	if (!vforked)
    339  1.47       kre 		traps_invalid = 1;
    340   1.1       cgd 
    341  1.47       kre 	for (tp = &trap[1] ; tp < &trap[NSIG] ; tp++) {
    342   1.1       cgd 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
    343   1.1       cgd 			INTOFF;
    344  1.47       kre 			setsignal(tp - trap, vforked == 1);
    345   1.1       cgd 			INTON;
    346   1.1       cgd 		}
    347   1.1       cgd 	}
    348  1.47       kre 	if (vforked == 2)
    349  1.47       kre 		free_traps();
    350  1.47       kre }
    351  1.47       kre 
    352  1.47       kre void
    353  1.47       kre free_traps(void)
    354  1.47       kre {
    355  1.47       kre 	char * volatile *tp;
    356  1.47       kre 
    357  1.47       kre 	VTRACE(DBG_TRAP, ("free_traps%s\n", traps_invalid ? "(invalid)" : ""));
    358  1.47       kre 	INTOFF;
    359  1.47       kre 	for (tp = trap ; tp < &trap[NSIG] ; tp++)
    360  1.47       kre 		if (*tp && **tp) {
    361  1.47       kre 			ckfree(*tp);
    362  1.47       kre 			*tp = NULL;
    363  1.47       kre 		}
    364  1.47       kre 	traps_invalid = 0;
    365  1.47       kre 	INTON;
    366   1.1       cgd }
    367   1.1       cgd 
    368  1.45       kre /*
    369  1.45       kre  * See if there are any defined traps
    370  1.45       kre  */
    371  1.45       kre int
    372  1.45       kre have_traps(void)
    373  1.45       kre {
    374  1.47       kre 	char * volatile *tp;
    375  1.47       kre 
    376  1.47       kre 	if (traps_invalid)
    377  1.47       kre 		return 0;
    378   1.1       cgd 
    379  1.45       kre 	for (tp = trap ; tp < &trap[NSIG] ; tp++)
    380  1.45       kre 		if (*tp && **tp)	/* trap not NULL or SIG_IGN */
    381  1.45       kre 			return 1;
    382  1.45       kre 	return 0;
    383  1.45       kre }
    384   1.1       cgd 
    385   1.1       cgd /*
    386   1.1       cgd  * Set the signal handler for the specified signal.  The routine figures
    387   1.1       cgd  * out what it should be set to.
    388   1.1       cgd  */
    389  1.47       kre void
    390  1.28  christos setsignal(int signo, int vforked)
    391  1.10       cgd {
    392   1.1       cgd 	int action;
    393  1.32  christos 	sig_t sigact = SIG_DFL, sig;
    394  1.27  christos 	char *t, tsig;
    395   1.1       cgd 
    396   1.1       cgd 	if ((t = trap[signo]) == NULL)
    397   1.1       cgd 		action = S_DFL;
    398   1.1       cgd 	else if (*t != '\0')
    399   1.1       cgd 		action = S_CATCH;
    400   1.1       cgd 	else
    401   1.1       cgd 		action = S_IGN;
    402  1.47       kre 
    403  1.47       kre 	VTRACE(DBG_TRAP, ("setsignal(%d%s) -> %d", signo,
    404  1.47       kre 	    vforked ? ", VF" : "", action));
    405  1.27  christos 	if (rootshell && !vforked && action == S_DFL) {
    406   1.1       cgd 		switch (signo) {
    407   1.1       cgd 		case SIGINT:
    408  1.21  christos 			if (iflag || minusc || sflag == 0)
    409   1.1       cgd 				action = S_CATCH;
    410   1.1       cgd 			break;
    411   1.1       cgd 		case SIGQUIT:
    412   1.1       cgd #ifdef DEBUG
    413   1.1       cgd 			if (debug)
    414   1.1       cgd 				break;
    415   1.1       cgd #endif
    416   1.1       cgd 			/* FALLTHROUGH */
    417   1.1       cgd 		case SIGTERM:
    418  1.47       kre 			if (rootshell && iflag)
    419   1.1       cgd 				action = S_IGN;
    420   1.1       cgd 			break;
    421   1.1       cgd #if JOBS
    422   1.1       cgd 		case SIGTSTP:
    423   1.1       cgd 		case SIGTTOU:
    424  1.47       kre 			if (rootshell && mflag)
    425   1.1       cgd 				action = S_IGN;
    426   1.1       cgd 			break;
    427   1.1       cgd #endif
    428   1.1       cgd 		}
    429   1.1       cgd 	}
    430  1.14  christos 
    431  1.47       kre 	/*
    432  1.47       kre 	 * Never let users futz with SIGCHLD
    433  1.47       kre 	 * instead we will give them pseudo SIGCHLD's
    434  1.47       kre 	 * when background jobs complete.
    435  1.47       kre 	 */
    436  1.47       kre 	if (signo == SIGCHLD)
    437  1.47       kre 		action = S_DFL;
    438  1.47       kre 
    439  1.47       kre 	VTRACE(DBG_TRAP, (" -> %d", action));
    440  1.47       kre 
    441  1.47       kre 	t = &sigmode[signo];
    442  1.27  christos 	tsig = *t;
    443  1.27  christos 	if (tsig == 0) {
    444  1.16  christos 		/*
    445  1.16  christos 		 * current setting unknown
    446   1.1       cgd 		 */
    447  1.15  christos 		if (!getsigaction(signo, &sigact)) {
    448  1.15  christos 			/*
    449  1.15  christos 			 * Pretend it worked; maybe we should give a warning
    450  1.15  christos 			 * here, but other shells don't. We don't alter
    451  1.15  christos 			 * sigmode, so that we retry every time.
    452  1.15  christos 			 */
    453  1.47       kre 			VTRACE(DBG_TRAP, (" getsigaction (%d)\n", errno));
    454  1.47       kre 			return;
    455  1.15  christos 		}
    456  1.47       kre 		VTRACE(DBG_TRAP, (" [%s]%s%s", sigact==SIG_IGN ? "IGN" :
    457  1.47       kre 		    sigact==SIG_DFL ? "DFL" : "caught",
    458  1.47       kre 		    iflag ? "i" : "", mflag ? "m" : ""));
    459  1.47       kre 
    460   1.1       cgd 		if (sigact == SIG_IGN) {
    461  1.32  christos 			/*
    462  1.33  christos 			 * POSIX 3.14.13 states that non-interactive shells
    463  1.33  christos 			 * should ignore trap commands for signals that were
    464  1.33  christos 			 * ignored upon entry, and leaves the behavior
    465  1.33  christos 			 * unspecified for interactive shells. On interactive
    466  1.33  christos 			 * shells, or if job control is on, and we have a job
    467  1.33  christos 			 * control related signal, we allow the trap to work.
    468  1.33  christos 			 *
    469  1.33  christos 			 * This change allows us to be POSIX compliant, and
    470  1.33  christos 			 * at the same time override the default behavior if
    471  1.33  christos 			 * we need to by setting the interactive flag.
    472  1.32  christos 			 */
    473  1.33  christos 			if ((mflag && (signo == SIGTSTP ||
    474  1.33  christos 			     signo == SIGTTIN || signo == SIGTTOU)) || iflag) {
    475  1.33  christos 				tsig = S_IGN;
    476  1.33  christos 			} else
    477  1.33  christos 				tsig = S_HARD_IGN;
    478   1.1       cgd 		} else {
    479  1.27  christos 			tsig = S_RESET;	/* force to be set */
    480   1.1       cgd 		}
    481   1.1       cgd 	}
    482  1.47       kre 	VTRACE(DBG_TRAP, (" tsig=%d\n", tsig));
    483  1.47       kre 
    484  1.27  christos 	if (tsig == S_HARD_IGN || tsig == action)
    485  1.47       kre 		return;
    486  1.47       kre 
    487   1.1       cgd 	switch (action) {
    488   1.1       cgd 		case S_DFL:	sigact = SIG_DFL;	break;
    489   1.1       cgd 		case S_CATCH:  	sigact = onsig;		break;
    490   1.1       cgd 		case S_IGN:	sigact = SIG_IGN;	break;
    491   1.1       cgd 	}
    492  1.47       kre 
    493  1.32  christos 	sig = signal(signo, sigact);
    494  1.47       kre 
    495  1.32  christos 	if (sig != SIG_ERR) {
    496  1.32  christos 		sigset_t ss;
    497  1.47       kre 
    498  1.32  christos 		if (!vforked)
    499  1.32  christos 			*t = action;
    500  1.47       kre 
    501  1.32  christos 		if (action == S_CATCH)
    502  1.32  christos 			(void)siginterrupt(signo, 1);
    503  1.32  christos 		/*
    504  1.32  christos 		 * If our parent accidentally blocked signals for
    505  1.32  christos 		 * us make sure we unblock them
    506  1.32  christos 		 */
    507  1.32  christos 		(void)sigemptyset(&ss);
    508  1.32  christos 		(void)sigaddset(&ss, signo);
    509  1.32  christos 		(void)sigprocmask(SIG_UNBLOCK, &ss, NULL);
    510  1.32  christos 	}
    511  1.47       kre 	return;
    512   1.1       cgd }
    513   1.1       cgd 
    514   1.6       jtc /*
    515   1.6       jtc  * Return the current setting for sig w/o changing it.
    516   1.6       jtc  */
    517  1.15  christos static int
    518  1.28  christos getsigaction(int signo, sig_t *sigact)
    519  1.10       cgd {
    520   1.6       jtc 	struct sigaction sa;
    521   1.6       jtc 
    522   1.6       jtc 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
    523  1.15  christos 		return 0;
    524  1.15  christos 	*sigact = (sig_t) sa.sa_handler;
    525  1.15  christos 	return 1;
    526   1.6       jtc }
    527   1.1       cgd 
    528   1.1       cgd /*
    529   1.1       cgd  * Ignore a signal.
    530   1.1       cgd  */
    531   1.1       cgd 
    532   1.1       cgd void
    533  1.28  christos ignoresig(int signo, int vforked)
    534  1.10       cgd {
    535  1.47       kre 	if (sigmode[signo] == 0)
    536  1.47       kre 		setsignal(signo, vforked);
    537  1.47       kre 
    538  1.47       kre 	VTRACE(DBG_TRAP, ("ignoresig(%d%s)\n", signo, vforked ? ", VF" : ""));
    539  1.47       kre 	if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
    540   1.1       cgd 		signal(signo, SIG_IGN);
    541  1.47       kre 		if (!vforked)
    542  1.47       kre 			sigmode[signo] = S_IGN;
    543   1.1       cgd 	}
    544  1.47       kre }
    545  1.47       kre 
    546  1.47       kre char *
    547  1.47       kre child_trap(void)
    548  1.47       kre {
    549  1.47       kre 	char * p;
    550  1.47       kre 
    551  1.47       kre 	p = trap[SIGCHLD];
    552  1.47       kre 
    553  1.47       kre 	if (p != NULL && *p == '\0')
    554  1.47       kre 		p = NULL;
    555  1.47       kre 
    556  1.47       kre 	return p;
    557   1.1       cgd }
    558   1.1       cgd 
    559   1.1       cgd 
    560   1.1       cgd #ifdef mkinit
    561   1.8       jtc INCLUDE <signal.h>
    562   1.1       cgd INCLUDE "trap.h"
    563  1.47       kre INCLUDE "shell.h"
    564  1.47       kre INCLUDE "show.h"
    565   1.1       cgd 
    566   1.1       cgd SHELLPROC {
    567   1.1       cgd 	char *sm;
    568   1.1       cgd 
    569  1.47       kre 	INTOFF;
    570  1.47       kre 	clear_traps(2);
    571   1.8       jtc 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
    572  1.47       kre 		if (*sm == S_IGN) {
    573   1.1       cgd 			*sm = S_HARD_IGN;
    574  1.49       kre 			VTRACE(DBG_TRAP, ("SHELLPROC: %d -> hard_ign\n",
    575  1.49       kre 			    (sm - sigmode)));
    576  1.47       kre 		}
    577   1.1       cgd 	}
    578  1.47       kre 	INTON;
    579   1.1       cgd }
    580   1.1       cgd #endif
    581   1.1       cgd 
    582   1.1       cgd 
    583   1.1       cgd 
    584   1.1       cgd /*
    585   1.1       cgd  * Signal handler.
    586   1.1       cgd  */
    587   1.1       cgd 
    588   1.1       cgd void
    589  1.28  christos onsig(int signo)
    590  1.10       cgd {
    591  1.41       kre 	CTRACE(DBG_SIG, ("Signal %d, had: pending %d, gotsig[%d]=%d\n",
    592  1.41       kre 	    signo, pendingsigs, signo, gotsig[signo]));
    593  1.41       kre 
    594  1.47       kre 	/*			This should not be needed.
    595   1.1       cgd 	signal(signo, onsig);
    596  1.47       kre 	*/
    597  1.47       kre 
    598   1.1       cgd 	if (signo == SIGINT && trap[SIGINT] == NULL) {
    599   1.1       cgd 		onint();
    600   1.1       cgd 		return;
    601   1.1       cgd 	}
    602  1.47       kre 
    603  1.47       kre 	/*
    604  1.47       kre 	 * if the signal will do nothing, no point reporting it
    605  1.47       kre 	 */
    606  1.47       kre 	if (trap[signo] != NULL && trap[signo][0] != '\0' &&
    607  1.47       kre 	    signo != SIGCHLD) {
    608  1.47       kre 		gotsig[signo] = 1;
    609  1.47       kre 		pendingsigs++;
    610  1.47       kre 	}
    611   1.1       cgd }
    612   1.1       cgd 
    613   1.1       cgd 
    614   1.1       cgd 
    615   1.1       cgd /*
    616   1.1       cgd  * Called to execute a trap.  Perhaps we should avoid entering new trap
    617   1.1       cgd  * handlers while we are executing a trap handler.
    618   1.1       cgd  */
    619   1.1       cgd 
    620   1.1       cgd void
    621  1.28  christos dotrap(void)
    622  1.28  christos {
    623   1.1       cgd 	int i;
    624  1.47       kre 	char *tr;
    625   1.1       cgd 	int savestatus;
    626  1.47       kre 	struct skipsave saveskip;
    627  1.47       kre 
    628  1.47       kre 	in_dotrap++;
    629   1.1       cgd 
    630  1.47       kre 	CTRACE(DBG_TRAP, ("dotrap[%d]: %d pending, traps %sinvalid\n",
    631  1.47       kre 	    in_dotrap, pendingsigs, traps_invalid ? "" : "not "));
    632   1.1       cgd 	for (;;) {
    633  1.47       kre 		pendingsigs = 0;
    634   1.1       cgd 		for (i = 1 ; ; i++) {
    635  1.47       kre 			if (i >= NSIG)
    636  1.40       kre 				return;
    637  1.40       kre 			if (gotsig[i])
    638   1.6       jtc 				break;
    639   1.1       cgd 		}
    640  1.40       kre 		gotsig[i] = 0;
    641  1.47       kre 
    642  1.47       kre 		if (traps_invalid)
    643  1.47       kre 			continue;
    644  1.47       kre 
    645  1.47       kre 		tr = trap[i];
    646  1.47       kre 
    647  1.47       kre 		CTRACE(DBG_TRAP|DBG_SIG, ("dotrap %d: %s%s%s\n", i,
    648  1.47       kre 		    tr ? "\"" : "", tr ? tr : "NULL", tr ? "\"" : ""));
    649  1.47       kre 
    650  1.47       kre 		if (tr != NULL) {
    651  1.47       kre 			last_trapsig = i;
    652  1.47       kre 			save_skipstate(&saveskip);
    653  1.47       kre 			savestatus = exitstatus;
    654  1.47       kre 
    655  1.44       kre 			tr = savestr(tr);	/* trap code may free trap[i] */
    656  1.44       kre 			evalstring(tr, 0);
    657  1.44       kre 			ckfree(tr);
    658  1.47       kre 
    659  1.47       kre 			if (current_skipstate() == SKIPNONE ||
    660  1.47       kre 			    saveskip.state != SKIPNONE) {
    661  1.47       kre 				restore_skipstate(&saveskip);
    662  1.47       kre 				exitstatus = savestatus;
    663  1.47       kre 			}
    664  1.44       kre 		}
    665   1.1       cgd 	}
    666  1.47       kre 
    667  1.47       kre 	in_dotrap--;
    668   1.1       cgd }
    669   1.1       cgd 
    670  1.37  christos int
    671  1.37  christos lastsig(void)
    672  1.37  christos {
    673  1.37  christos 	int i;
    674   1.1       cgd 
    675  1.40       kre 	for (i = NSIG; --i > 0; )
    676  1.40       kre 		if (gotsig[i])
    677  1.37  christos 			return i;
    678  1.37  christos 	return SIGINT;	/* XXX */
    679  1.37  christos }
    680   1.1       cgd 
    681   1.1       cgd /*
    682   1.1       cgd  * Controls whether the shell is interactive or not.
    683   1.1       cgd  */
    684   1.1       cgd 
    685   1.1       cgd 
    686   1.1       cgd void
    687  1.28  christos setinteractive(int on)
    688  1.10       cgd {
    689   1.6       jtc 	static int is_interactive;
    690   1.6       jtc 
    691   1.1       cgd 	if (on == is_interactive)
    692   1.1       cgd 		return;
    693  1.27  christos 	setsignal(SIGINT, 0);
    694  1.27  christos 	setsignal(SIGQUIT, 0);
    695  1.27  christos 	setsignal(SIGTERM, 0);
    696   1.1       cgd 	is_interactive = on;
    697   1.1       cgd }
    698   1.1       cgd 
    699   1.1       cgd 
    700   1.1       cgd 
    701   1.1       cgd /*
    702   1.1       cgd  * Called to exit the shell.
    703   1.1       cgd  */
    704  1.47       kre void
    705  1.47       kre exitshell(int status)
    706  1.47       kre {
    707  1.47       kre 	CTRACE(DBG_ERRS|DBG_PROCS|DBG_CMDS|DBG_TRAP,
    708  1.47       kre 	    ("pid %d: exitshell(%d)\n", getpid(), status));
    709  1.47       kre 
    710  1.47       kre 	exiting = 1;
    711  1.47       kre 	exiting_status = status;
    712  1.47       kre 	exitshell_savedstatus();
    713  1.47       kre }
    714   1.1       cgd 
    715   1.1       cgd void
    716  1.47       kre exitshell_savedstatus(void)
    717  1.10       cgd {
    718  1.47       kre 	struct jmploc loc;
    719   1.1       cgd 	char *p;
    720  1.47       kre 	volatile int sig = 0;
    721  1.47       kre 	int s;
    722  1.47       kre 	sigset_t sigs;
    723   1.1       cgd 
    724  1.41       kre 	CTRACE(DBG_ERRS|DBG_PROCS|DBG_CMDS|DBG_TRAP,
    725  1.47       kre          ("pid %d: exitshell_savedstatus()%s $?=%d xs=%d dt=%d ts=%d\n",
    726  1.47       kre 	    getpid(), exiting ? " exiting" : "", exitstatus,
    727  1.47       kre 	    exiting_status, in_dotrap, last_trapsig));
    728  1.47       kre 
    729  1.47       kre 	if (!exiting) {
    730  1.47       kre 		if (in_dotrap && last_trapsig) {
    731  1.47       kre 			sig = last_trapsig;
    732  1.47       kre 			exiting_status = sig + 128;
    733  1.47       kre 		} else
    734  1.47       kre 			exiting_status = exitstatus;
    735  1.47       kre 	}
    736  1.47       kre 	exitstatus = exiting_status;
    737  1.47       kre 
    738  1.47       kre 	if (!setjmp(loc.loc)) {
    739  1.47       kre 		handler = &loc;
    740  1.47       kre 
    741  1.47       kre 		if (!traps_invalid && (p = trap[0]) != NULL && *p != '\0') {
    742  1.47       kre 			reset_eval();
    743  1.47       kre 			trap[0] = NULL;
    744  1.47       kre 			VTRACE(DBG_TRAP, ("exit trap: \"%s\"\n", p));
    745  1.47       kre 			evalstring(p, 0);
    746  1.47       kre 		}
    747  1.47       kre 	}
    748  1.41       kre 
    749  1.47       kre 	INTOFF;			/*  we're done, no more interrupts. */
    750  1.47       kre 
    751  1.47       kre 	if (!setjmp(loc.loc)) {
    752  1.47       kre 		handler = &loc;		/* probably unnecessary */
    753  1.47       kre 		flushall();
    754  1.47       kre #if JOBS
    755  1.47       kre 		setjobctl(0);
    756  1.47       kre #endif
    757   1.6       jtc 	}
    758  1.47       kre 
    759  1.47       kre 	if ((s = sig) != 0 && s != SIGSTOP && s != SIGTSTP && s != SIGTTIN &&
    760  1.47       kre 	    s != SIGTTOU) {
    761  1.47       kre 		struct rlimit nocore;
    762  1.47       kre 
    763  1.47       kre 		/*
    764  1.47       kre 		 * if the signal is of the core dump variety, don't...
    765  1.47       kre 		 */
    766  1.47       kre 		nocore.rlim_cur = nocore.rlim_max = 0;
    767  1.47       kre 		(void) setrlimit(RLIMIT_CORE, &nocore);
    768  1.47       kre 
    769  1.47       kre 		signal(s, SIG_DFL);
    770  1.47       kre 		sigemptyset(&sigs);
    771  1.47       kre 		sigaddset(&sigs, s);
    772  1.47       kre 		sigprocmask(SIG_UNBLOCK, &sigs, NULL);
    773  1.47       kre 
    774  1.47       kre 		kill(getpid(), s);
    775   1.1       cgd 	}
    776  1.47       kre 	_exit(exiting_status);
    777  1.18   mycroft 	/* NOTREACHED */
    778   1.1       cgd }
    779