Home | History | Annotate | Line # | Download | only in sh
trap.c revision 1.29
      1  1.29       agc /*	$NetBSD: trap.c,v 1.29 2003/08/07 09:05:39 agc 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.29       agc __RCSID("$NetBSD: trap.c,v 1.29 2003/08/07 09:05:39 agc 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.13  christos 
     48   1.1       cgd #include "shell.h"
     49   1.1       cgd #include "main.h"
     50   1.1       cgd #include "nodes.h"	/* for other headers */
     51   1.1       cgd #include "eval.h"
     52   1.1       cgd #include "jobs.h"
     53  1.13  christos #include "show.h"
     54   1.1       cgd #include "options.h"
     55   1.1       cgd #include "syntax.h"
     56   1.1       cgd #include "output.h"
     57   1.1       cgd #include "memalloc.h"
     58   1.1       cgd #include "error.h"
     59   1.1       cgd #include "trap.h"
     60   1.1       cgd #include "mystring.h"
     61   1.1       cgd 
     62   1.1       cgd 
     63   1.1       cgd /*
     64   1.1       cgd  * Sigmode records the current value of the signal handlers for the various
     65   1.1       cgd  * modes.  A value of zero means that the current handler is not known.
     66   1.1       cgd  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
     67   1.1       cgd  */
     68   1.1       cgd 
     69   1.1       cgd #define S_DFL 1			/* default signal handling (SIG_DFL) */
     70   1.1       cgd #define S_CATCH 2		/* signal is caught */
     71   1.1       cgd #define S_IGN 3			/* signal is ignored (SIG_IGN) */
     72   1.1       cgd #define S_HARD_IGN 4		/* signal is ignored permenantly */
     73   1.6       jtc #define S_RESET 5		/* temporary - to reset a hard ignored sig */
     74   1.1       cgd 
     75   1.1       cgd 
     76   1.8       jtc char *trap[NSIG+1];		/* trap handler commands */
     77   1.8       jtc MKINIT char sigmode[NSIG];	/* current value of signal */
     78   1.8       jtc char gotsig[NSIG];		/* indicates specified signal received */
     79  1.28  christos int pendingsigs;		/* indicates some signal received */
     80   1.1       cgd 
     81  1.28  christos static int getsigaction(int, sig_t *);
     82  1.26      wulf 
     83  1.26      wulf /*
     84  1.26      wulf  * return the signal number described by `p' (as a number or a name)
     85  1.26      wulf  * or -1 if it isn't one
     86  1.26      wulf  */
     87  1.26      wulf 
     88  1.26      wulf static int
     89  1.28  christos signame_to_signum(const char *p)
     90  1.26      wulf {
     91  1.26      wulf 	int i;
     92  1.26      wulf 
     93  1.26      wulf 	if (is_number(p))
     94  1.26      wulf 		return number(p);
     95  1.26      wulf 
     96  1.26      wulf 	if (strcasecmp(p, "exit") == 0 )
     97  1.26      wulf 		return 0;
     98  1.26      wulf 
     99  1.26      wulf 	if (strncasecmp(p, "sig", 3) == 0)
    100  1.26      wulf 		p += 3;
    101  1.26      wulf 
    102  1.26      wulf 	for (i = 0; i < NSIG; ++i)
    103  1.26      wulf 		if (strcasecmp (p, sys_signame[i]) == 0)
    104  1.26      wulf 			return i;
    105  1.26      wulf 	return -1;
    106  1.26      wulf }
    107  1.26      wulf 
    108  1.26      wulf /*
    109  1.26      wulf  * Print a list of valid signal names
    110  1.26      wulf  */
    111  1.28  christos static void
    112  1.26      wulf printsignals(void)
    113  1.26      wulf {
    114  1.26      wulf 	int n;
    115  1.26      wulf 
    116  1.26      wulf 	out1str("EXIT ");
    117  1.26      wulf 
    118  1.26      wulf 	for (n = 1; n < NSIG; n++) {
    119  1.26      wulf 		out1fmt("%s", sys_signame[n]);
    120  1.26      wulf 		if ((n == NSIG/2) ||  n == (NSIG - 1))
    121  1.26      wulf 			out1str("\n");
    122  1.26      wulf 		else
    123  1.26      wulf 			out1c(' ');
    124  1.26      wulf 	}
    125  1.26      wulf }
    126  1.15  christos 
    127   1.1       cgd /*
    128   1.1       cgd  * The trap builtin.
    129   1.1       cgd  */
    130   1.1       cgd 
    131  1.10       cgd int
    132  1.28  christos trapcmd(int argc, char **argv)
    133  1.10       cgd {
    134   1.1       cgd 	char *action;
    135   1.1       cgd 	char **ap;
    136   1.1       cgd 	int signo;
    137   1.1       cgd 
    138   1.1       cgd 	if (argc <= 1) {
    139   1.8       jtc 		for (signo = 0 ; signo <= NSIG ; signo++) {
    140   1.1       cgd 			if (trap[signo] != NULL)
    141  1.26      wulf 				out1fmt("trap -- '%s' %s\n", trap[signo],
    142  1.26      wulf 				    (signo) ? sys_signame[signo] : "EXIT");
    143   1.1       cgd 		}
    144   1.1       cgd 		return 0;
    145   1.1       cgd 	}
    146   1.1       cgd 	ap = argv + 1;
    147  1.26      wulf 
    148  1.26      wulf 	action = NULL;
    149  1.26      wulf 
    150  1.26      wulf 	if (strcmp(*ap, "--") == 0)
    151  1.26      wulf 		if (*++ap == NULL)
    152  1.26      wulf 			return 0;
    153  1.26      wulf 
    154  1.26      wulf 	if (signame_to_signum(*ap) == -1) {
    155  1.26      wulf 		if ((*ap)[0] =='-') {
    156  1.26      wulf 			if ((*ap)[1] == NULL)
    157  1.26      wulf 				ap++;
    158  1.26      wulf 			else if ((*ap)[1] == 'l' && (*ap)[2] == NULL) {
    159  1.26      wulf 				printsignals();
    160  1.26      wulf 				return 0;
    161  1.26      wulf 			}
    162  1.26      wulf 			else
    163  1.26      wulf 				error("bad option %s\n", *ap);
    164  1.26      wulf 		}
    165  1.26      wulf 		else
    166  1.26      wulf 			action = *ap++;
    167  1.26      wulf 	}
    168  1.26      wulf 
    169   1.1       cgd 	while (*ap) {
    170  1.26      wulf 		if (is_number(*ap))
    171  1.26      wulf 			signo = number(*ap);
    172  1.26      wulf 		else
    173  1.26      wulf 			signo = signame_to_signum(*ap);
    174  1.26      wulf 
    175  1.26      wulf 		if (signo < 0 || signo > NSIG)
    176   1.1       cgd 			error("%s: bad trap", *ap);
    177  1.26      wulf 
    178   1.1       cgd 		INTOFF;
    179   1.1       cgd 		if (action)
    180   1.1       cgd 			action = savestr(action);
    181  1.26      wulf 
    182   1.1       cgd 		if (trap[signo])
    183   1.1       cgd 			ckfree(trap[signo]);
    184  1.26      wulf 
    185   1.1       cgd 		trap[signo] = action;
    186  1.26      wulf 
    187   1.1       cgd 		if (signo != 0)
    188  1.27  christos 			setsignal(signo, 0);
    189   1.1       cgd 		INTON;
    190   1.1       cgd 		ap++;
    191   1.1       cgd 	}
    192   1.1       cgd 	return 0;
    193   1.1       cgd }
    194   1.1       cgd 
    195   1.1       cgd 
    196   1.1       cgd 
    197   1.1       cgd /*
    198  1.27  christos  * Clear traps on a fork or vfork.
    199  1.27  christos  * Takes one arg vfork, to tell it to not be destructive of
    200  1.27  christos  * the parents variables.
    201   1.1       cgd  */
    202   1.1       cgd 
    203   1.1       cgd void
    204  1.28  christos clear_traps(int vforked)
    205  1.27  christos {
    206   1.1       cgd 	char **tp;
    207   1.1       cgd 
    208   1.8       jtc 	for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
    209   1.1       cgd 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
    210   1.1       cgd 			INTOFF;
    211  1.27  christos 			if (!vforked) {
    212  1.27  christos 				ckfree(*tp);
    213  1.27  christos 				*tp = NULL;
    214  1.27  christos 			}
    215   1.1       cgd 			if (tp != &trap[0])
    216  1.27  christos 				setsignal(tp - trap, vforked);
    217   1.1       cgd 			INTON;
    218   1.1       cgd 		}
    219   1.1       cgd 	}
    220   1.1       cgd }
    221   1.1       cgd 
    222   1.1       cgd 
    223   1.1       cgd 
    224   1.1       cgd /*
    225   1.1       cgd  * Set the signal handler for the specified signal.  The routine figures
    226   1.1       cgd  * out what it should be set to.
    227   1.1       cgd  */
    228   1.1       cgd 
    229  1.11       cgd long
    230  1.28  christos setsignal(int signo, int vforked)
    231  1.10       cgd {
    232   1.1       cgd 	int action;
    233  1.13  christos 	sig_t sigact = SIG_DFL;
    234  1.27  christos 	char *t, tsig;
    235   1.1       cgd 
    236   1.1       cgd 	if ((t = trap[signo]) == NULL)
    237   1.1       cgd 		action = S_DFL;
    238   1.1       cgd 	else if (*t != '\0')
    239   1.1       cgd 		action = S_CATCH;
    240   1.1       cgd 	else
    241   1.1       cgd 		action = S_IGN;
    242  1.27  christos 	if (rootshell && !vforked && action == S_DFL) {
    243   1.1       cgd 		switch (signo) {
    244   1.1       cgd 		case SIGINT:
    245  1.21  christos 			if (iflag || minusc || sflag == 0)
    246   1.1       cgd 				action = S_CATCH;
    247   1.1       cgd 			break;
    248   1.1       cgd 		case SIGQUIT:
    249   1.1       cgd #ifdef DEBUG
    250   1.1       cgd 			if (debug)
    251   1.1       cgd 				break;
    252   1.1       cgd #endif
    253   1.1       cgd 			/* FALLTHROUGH */
    254   1.1       cgd 		case SIGTERM:
    255   1.1       cgd 			if (iflag)
    256   1.1       cgd 				action = S_IGN;
    257   1.1       cgd 			break;
    258   1.1       cgd #if JOBS
    259   1.1       cgd 		case SIGTSTP:
    260   1.1       cgd 		case SIGTTOU:
    261   1.6       jtc 			if (mflag)
    262   1.1       cgd 				action = S_IGN;
    263   1.1       cgd 			break;
    264   1.1       cgd #endif
    265   1.1       cgd 		}
    266   1.1       cgd 	}
    267  1.14  christos 
    268   1.6       jtc 	t = &sigmode[signo - 1];
    269  1.27  christos 	tsig = *t;
    270  1.27  christos 	if (tsig == 0) {
    271  1.16  christos 		/*
    272  1.16  christos 		 * current setting unknown
    273   1.1       cgd 		 */
    274  1.15  christos 		if (!getsigaction(signo, &sigact)) {
    275  1.15  christos 			/*
    276  1.15  christos 			 * Pretend it worked; maybe we should give a warning
    277  1.15  christos 			 * here, but other shells don't. We don't alter
    278  1.15  christos 			 * sigmode, so that we retry every time.
    279  1.15  christos 			 */
    280  1.15  christos 			return 0;
    281  1.15  christos 		}
    282   1.1       cgd 		if (sigact == SIG_IGN) {
    283  1.16  christos 			if (mflag && (signo == SIGTSTP ||
    284   1.6       jtc 			     signo == SIGTTIN || signo == SIGTTOU)) {
    285  1.27  christos 				tsig = S_IGN;	/* don't hard ignore these */
    286   1.6       jtc 			} else
    287  1.27  christos 				tsig = S_HARD_IGN;
    288   1.1       cgd 		} else {
    289  1.27  christos 			tsig = S_RESET;	/* force to be set */
    290   1.1       cgd 		}
    291   1.1       cgd 	}
    292  1.27  christos 	if (tsig == S_HARD_IGN || tsig == action)
    293   1.1       cgd 		return 0;
    294   1.1       cgd 	switch (action) {
    295   1.1       cgd 		case S_DFL:	sigact = SIG_DFL;	break;
    296   1.1       cgd 		case S_CATCH:  	sigact = onsig;		break;
    297   1.1       cgd 		case S_IGN:	sigact = SIG_IGN;	break;
    298   1.1       cgd 	}
    299  1.27  christos 	if (!vforked)
    300  1.27  christos 		*t = action;
    301  1.19  christos 	siginterrupt(signo, 1);
    302  1.11       cgd 	return (long)signal(signo, sigact);
    303   1.1       cgd }
    304   1.1       cgd 
    305   1.6       jtc /*
    306   1.6       jtc  * Return the current setting for sig w/o changing it.
    307   1.6       jtc  */
    308  1.15  christos static int
    309  1.28  christos getsigaction(int signo, sig_t *sigact)
    310  1.10       cgd {
    311   1.6       jtc 	struct sigaction sa;
    312   1.6       jtc 
    313   1.6       jtc 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
    314  1.15  christos 		return 0;
    315  1.15  christos 	*sigact = (sig_t) sa.sa_handler;
    316  1.15  christos 	return 1;
    317   1.6       jtc }
    318   1.1       cgd 
    319   1.1       cgd /*
    320   1.1       cgd  * Ignore a signal.
    321   1.1       cgd  */
    322   1.1       cgd 
    323   1.1       cgd void
    324  1.28  christos ignoresig(int signo, int vforked)
    325  1.10       cgd {
    326   1.6       jtc 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
    327   1.1       cgd 		signal(signo, SIG_IGN);
    328   1.1       cgd 	}
    329  1.27  christos 	if (!vforked)
    330  1.27  christos 		sigmode[signo - 1] = S_HARD_IGN;
    331   1.1       cgd }
    332   1.1       cgd 
    333   1.1       cgd 
    334   1.1       cgd #ifdef mkinit
    335   1.8       jtc INCLUDE <signal.h>
    336   1.1       cgd INCLUDE "trap.h"
    337   1.1       cgd 
    338   1.1       cgd SHELLPROC {
    339   1.1       cgd 	char *sm;
    340   1.1       cgd 
    341  1.27  christos 	clear_traps(0);
    342   1.8       jtc 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
    343   1.1       cgd 		if (*sm == S_IGN)
    344   1.1       cgd 			*sm = S_HARD_IGN;
    345   1.1       cgd 	}
    346   1.1       cgd }
    347   1.1       cgd #endif
    348   1.1       cgd 
    349   1.1       cgd 
    350   1.1       cgd 
    351   1.1       cgd /*
    352   1.1       cgd  * Signal handler.
    353   1.1       cgd  */
    354   1.1       cgd 
    355   1.1       cgd void
    356  1.28  christos onsig(int signo)
    357  1.10       cgd {
    358   1.1       cgd 	signal(signo, onsig);
    359   1.1       cgd 	if (signo == SIGINT && trap[SIGINT] == NULL) {
    360   1.1       cgd 		onint();
    361   1.1       cgd 		return;
    362   1.1       cgd 	}
    363   1.6       jtc 	gotsig[signo - 1] = 1;
    364   1.1       cgd 	pendingsigs++;
    365   1.1       cgd }
    366   1.1       cgd 
    367   1.1       cgd 
    368   1.1       cgd 
    369   1.1       cgd /*
    370   1.1       cgd  * Called to execute a trap.  Perhaps we should avoid entering new trap
    371   1.1       cgd  * handlers while we are executing a trap handler.
    372   1.1       cgd  */
    373   1.1       cgd 
    374   1.1       cgd void
    375  1.28  christos dotrap(void)
    376  1.28  christos {
    377   1.1       cgd 	int i;
    378   1.1       cgd 	int savestatus;
    379   1.1       cgd 
    380   1.1       cgd 	for (;;) {
    381   1.1       cgd 		for (i = 1 ; ; i++) {
    382   1.6       jtc 			if (gotsig[i - 1])
    383   1.6       jtc 				break;
    384   1.8       jtc 			if (i >= NSIG)
    385   1.5   mycroft 				goto done;
    386   1.1       cgd 		}
    387   1.6       jtc 		gotsig[i - 1] = 0;
    388   1.1       cgd 		savestatus=exitstatus;
    389  1.22  christos 		evalstring(trap[i], 0);
    390   1.1       cgd 		exitstatus=savestatus;
    391   1.1       cgd 	}
    392   1.1       cgd done:
    393   1.1       cgd 	pendingsigs = 0;
    394   1.1       cgd }
    395   1.1       cgd 
    396   1.1       cgd 
    397   1.1       cgd 
    398   1.1       cgd /*
    399   1.1       cgd  * Controls whether the shell is interactive or not.
    400   1.1       cgd  */
    401   1.1       cgd 
    402   1.1       cgd 
    403   1.1       cgd void
    404  1.28  christos setinteractive(int on)
    405  1.10       cgd {
    406   1.6       jtc 	static int is_interactive;
    407   1.6       jtc 
    408   1.1       cgd 	if (on == is_interactive)
    409   1.1       cgd 		return;
    410  1.27  christos 	setsignal(SIGINT, 0);
    411  1.27  christos 	setsignal(SIGQUIT, 0);
    412  1.27  christos 	setsignal(SIGTERM, 0);
    413   1.1       cgd 	is_interactive = on;
    414   1.1       cgd }
    415   1.1       cgd 
    416   1.1       cgd 
    417   1.1       cgd 
    418   1.1       cgd /*
    419   1.1       cgd  * Called to exit the shell.
    420   1.1       cgd  */
    421   1.1       cgd 
    422   1.1       cgd void
    423  1.28  christos exitshell(int status)
    424  1.10       cgd {
    425   1.1       cgd 	struct jmploc loc1, loc2;
    426   1.1       cgd 	char *p;
    427   1.1       cgd 
    428  1.28  christos 	TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
    429   1.6       jtc 	if (setjmp(loc1.loc)) {
    430   1.6       jtc 		goto l1;
    431   1.6       jtc 	}
    432   1.6       jtc 	if (setjmp(loc2.loc)) {
    433   1.6       jtc 		goto l2;
    434   1.6       jtc 	}
    435   1.1       cgd 	handler = &loc1;
    436   1.1       cgd 	if ((p = trap[0]) != NULL && *p != '\0') {
    437   1.1       cgd 		trap[0] = NULL;
    438  1.22  christos 		evalstring(p, 0);
    439   1.1       cgd 	}
    440   1.1       cgd l1:   handler = &loc2;			/* probably unnecessary */
    441   1.1       cgd 	flushall();
    442   1.1       cgd #if JOBS
    443   1.1       cgd 	setjobctl(0);
    444   1.1       cgd #endif
    445   1.1       cgd l2:   _exit(status);
    446  1.18   mycroft 	/* NOTREACHED */
    447   1.1       cgd }
    448