Home | History | Annotate | Line # | Download | only in sh
trap.c revision 1.24.4.3
      1  1.24.4.3        he /*	$NetBSD: trap.c,v 1.24.4.3 2002/02/23 15:53:50 he 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1       cgd  *    must display the following acknowledgement:
     20       1.1       cgd  *	This product includes software developed by the University of
     21       1.1       cgd  *	California, Berkeley and its contributors.
     22       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1       cgd  *    may be used to endorse or promote products derived from this software
     24       1.1       cgd  *    without specific prior written permission.
     25       1.1       cgd  *
     26       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1       cgd  * SUCH DAMAGE.
     37       1.1       cgd  */
     38       1.1       cgd 
     39      1.17  christos #include <sys/cdefs.h>
     40       1.1       cgd #ifndef lint
     41      1.12       cgd #if 0
     42      1.15  christos static char sccsid[] = "@(#)trap.c	8.5 (Berkeley) 6/5/95";
     43      1.12       cgd #else
     44  1.24.4.3        he __RCSID("$NetBSD: trap.c,v 1.24.4.3 2002/02/23 15:53:50 he Exp $");
     45      1.12       cgd #endif
     46       1.1       cgd #endif /* not lint */
     47       1.1       cgd 
     48      1.13  christos #include <signal.h>
     49      1.13  christos #include <unistd.h>
     50      1.13  christos #include <stdlib.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.1       cgd #include "syntax.h"
     60       1.1       cgd #include "output.h"
     61       1.1       cgd #include "memalloc.h"
     62       1.1       cgd #include "error.h"
     63       1.1       cgd #include "trap.h"
     64       1.1       cgd #include "mystring.h"
     65       1.1       cgd 
     66       1.1       cgd 
     67       1.1       cgd /*
     68       1.1       cgd  * Sigmode records the current value of the signal handlers for the various
     69       1.1       cgd  * modes.  A value of zero means that the current handler is not known.
     70       1.1       cgd  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
     71       1.1       cgd  */
     72       1.1       cgd 
     73       1.1       cgd #define S_DFL 1			/* default signal handling (SIG_DFL) */
     74       1.1       cgd #define S_CATCH 2		/* signal is caught */
     75       1.1       cgd #define S_IGN 3			/* signal is ignored (SIG_IGN) */
     76       1.1       cgd #define S_HARD_IGN 4		/* signal is ignored permenantly */
     77       1.6       jtc #define S_RESET 5		/* temporary - to reset a hard ignored sig */
     78       1.1       cgd 
     79       1.1       cgd 
     80       1.8       jtc char *trap[NSIG+1];		/* trap handler commands */
     81       1.8       jtc MKINIT char sigmode[NSIG];	/* current value of signal */
     82       1.8       jtc char gotsig[NSIG];		/* indicates specified signal received */
     83       1.6       jtc int pendingsigs;			/* indicates some signal received */
     84       1.1       cgd 
     85      1.15  christos static int getsigaction __P((int, sig_t *));
     86  1.24.4.3        he static int signame_to_signum __P((const char *));
     87  1.24.4.3        he void printsignals __P((void));
     88  1.24.4.3        he 
     89  1.24.4.3        he /*
     90  1.24.4.3        he  * return the signal number described by `p' (as a number or a name)
     91  1.24.4.3        he  * or -1 if it isn't one
     92  1.24.4.3        he  */
     93  1.24.4.3        he 
     94  1.24.4.3        he static int
     95  1.24.4.3        he signame_to_signum(p)
     96  1.24.4.3        he 	const char *p;
     97  1.24.4.3        he {
     98  1.24.4.3        he 	int i;
     99  1.24.4.3        he 
    100  1.24.4.3        he 	if (is_number(p))
    101  1.24.4.3        he 		return number(p);
    102  1.24.4.3        he 
    103  1.24.4.3        he 	if (strcasecmp(p, "exit") == 0 )
    104  1.24.4.3        he 		return 0;
    105  1.24.4.3        he 
    106  1.24.4.3        he 	if (strncasecmp(p, "sig", 3) == 0)
    107  1.24.4.3        he 		p += 3;
    108  1.24.4.3        he 
    109  1.24.4.3        he 	for (i = 0; i < NSIG; ++i)
    110  1.24.4.3        he 		if (strcasecmp (p, sys_signame[i]) == 0)
    111  1.24.4.3        he 			return i;
    112  1.24.4.3        he 	return -1;
    113  1.24.4.3        he }
    114  1.24.4.3        he 
    115  1.24.4.3        he /*
    116  1.24.4.3        he  * Print a list of valid signal names
    117  1.24.4.3        he  */
    118  1.24.4.3        he void
    119  1.24.4.3        he printsignals(void)
    120  1.24.4.3        he {
    121  1.24.4.3        he 	int n;
    122  1.24.4.3        he 
    123  1.24.4.3        he 	out1str("EXIT ");
    124  1.24.4.3        he 
    125  1.24.4.3        he 	for (n = 1; n < NSIG; n++) {
    126  1.24.4.3        he 		out1fmt("%s", sys_signame[n]);
    127  1.24.4.3        he 		if ((n == NSIG/2) ||  n == (NSIG - 1))
    128  1.24.4.3        he 			out1str("\n");
    129  1.24.4.3        he 		else
    130  1.24.4.3        he 			out1c(' ');
    131  1.24.4.3        he 	}
    132  1.24.4.3        he }
    133      1.15  christos 
    134       1.1       cgd /*
    135       1.1       cgd  * The trap builtin.
    136       1.1       cgd  */
    137       1.1       cgd 
    138      1.10       cgd int
    139      1.10       cgd trapcmd(argc, argv)
    140      1.10       cgd 	int argc;
    141      1.16  christos 	char **argv;
    142      1.10       cgd {
    143       1.1       cgd 	char *action;
    144       1.1       cgd 	char **ap;
    145       1.1       cgd 	int signo;
    146       1.1       cgd 
    147       1.1       cgd 	if (argc <= 1) {
    148       1.8       jtc 		for (signo = 0 ; signo <= NSIG ; signo++) {
    149       1.1       cgd 			if (trap[signo] != NULL)
    150  1.24.4.3        he 				out1fmt("trap -- '%s' %s\n", trap[signo],
    151  1.24.4.3        he 				    (signo) ? sys_signame[signo] : "EXIT");
    152       1.1       cgd 		}
    153       1.1       cgd 		return 0;
    154       1.1       cgd 	}
    155       1.1       cgd 	ap = argv + 1;
    156  1.24.4.3        he 
    157  1.24.4.3        he 	action = NULL;
    158  1.24.4.3        he 
    159  1.24.4.3        he 	if (strcmp(*ap, "--") == 0)
    160  1.24.4.3        he 		if (*++ap == NULL)
    161  1.24.4.3        he 			return 0;
    162  1.24.4.3        he 
    163  1.24.4.3        he 	if (signame_to_signum(*ap) == -1) {
    164  1.24.4.3        he 		if ((*ap)[0] =='-') {
    165  1.24.4.3        he 			if ((*ap)[1] == NULL)
    166  1.24.4.3        he 				ap++;
    167  1.24.4.3        he 			else if ((*ap)[1] == 'l' && (*ap)[2] == NULL) {
    168  1.24.4.3        he 				printsignals();
    169  1.24.4.3        he 				return 0;
    170  1.24.4.3        he 			}
    171  1.24.4.3        he 			else
    172  1.24.4.3        he 				error("bad option %s\n", *ap);
    173  1.24.4.3        he 		}
    174  1.24.4.3        he 		else
    175  1.24.4.3        he 			action = *ap++;
    176  1.24.4.3        he 	}
    177  1.24.4.3        he 
    178       1.1       cgd 	while (*ap) {
    179  1.24.4.3        he 		if (is_number(*ap))
    180  1.24.4.3        he 			signo = number(*ap);
    181  1.24.4.3        he 		else
    182  1.24.4.3        he 			signo = signame_to_signum(*ap);
    183  1.24.4.3        he 
    184  1.24.4.3        he 		if (signo < 0 || signo > NSIG)
    185       1.1       cgd 			error("%s: bad trap", *ap);
    186  1.24.4.3        he 
    187       1.1       cgd 		INTOFF;
    188       1.1       cgd 		if (action)
    189       1.1       cgd 			action = savestr(action);
    190  1.24.4.3        he 
    191       1.1       cgd 		if (trap[signo])
    192       1.1       cgd 			ckfree(trap[signo]);
    193  1.24.4.3        he 
    194       1.1       cgd 		trap[signo] = action;
    195  1.24.4.3        he 
    196       1.1       cgd 		if (signo != 0)
    197      1.24     elric 			setsignal(signo);
    198       1.1       cgd 		INTON;
    199       1.1       cgd 		ap++;
    200       1.1       cgd 	}
    201       1.1       cgd 	return 0;
    202       1.1       cgd }
    203       1.1       cgd 
    204       1.1       cgd 
    205       1.1       cgd 
    206       1.1       cgd /*
    207      1.24     elric  * Clear traps on a fork.
    208       1.1       cgd  */
    209       1.1       cgd 
    210       1.1       cgd void
    211      1.24     elric clear_traps() {
    212       1.1       cgd 	char **tp;
    213       1.1       cgd 
    214       1.8       jtc 	for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
    215       1.1       cgd 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
    216       1.1       cgd 			INTOFF;
    217      1.24     elric 			ckfree(*tp);
    218      1.24     elric 			*tp = NULL;
    219       1.1       cgd 			if (tp != &trap[0])
    220      1.24     elric 				setsignal(tp - trap);
    221       1.1       cgd 			INTON;
    222       1.1       cgd 		}
    223       1.1       cgd 	}
    224       1.1       cgd }
    225       1.1       cgd 
    226       1.1       cgd 
    227       1.1       cgd 
    228       1.1       cgd /*
    229       1.1       cgd  * Set the signal handler for the specified signal.  The routine figures
    230       1.1       cgd  * out what it should be set to.
    231       1.1       cgd  */
    232       1.1       cgd 
    233      1.11       cgd long
    234      1.24     elric setsignal(signo)
    235      1.10       cgd 	int signo;
    236      1.10       cgd {
    237       1.1       cgd 	int action;
    238      1.13  christos 	sig_t sigact = SIG_DFL;
    239      1.24     elric 	char *t;
    240       1.1       cgd 
    241       1.1       cgd 	if ((t = trap[signo]) == NULL)
    242       1.1       cgd 		action = S_DFL;
    243       1.1       cgd 	else if (*t != '\0')
    244       1.1       cgd 		action = S_CATCH;
    245       1.1       cgd 	else
    246       1.1       cgd 		action = S_IGN;
    247      1.24     elric 	if (rootshell && action == S_DFL) {
    248       1.1       cgd 		switch (signo) {
    249       1.1       cgd 		case SIGINT:
    250      1.21  christos 			if (iflag || minusc || sflag == 0)
    251       1.1       cgd 				action = S_CATCH;
    252       1.1       cgd 			break;
    253       1.1       cgd 		case SIGQUIT:
    254       1.1       cgd #ifdef DEBUG
    255       1.1       cgd 			{
    256       1.1       cgd 			extern int debug;
    257       1.1       cgd 
    258       1.1       cgd 			if (debug)
    259       1.1       cgd 				break;
    260       1.1       cgd 			}
    261       1.1       cgd #endif
    262       1.1       cgd 			/* FALLTHROUGH */
    263       1.1       cgd 		case SIGTERM:
    264       1.1       cgd 			if (iflag)
    265       1.1       cgd 				action = S_IGN;
    266       1.1       cgd 			break;
    267       1.1       cgd #if JOBS
    268       1.1       cgd 		case SIGTSTP:
    269       1.1       cgd 		case SIGTTOU:
    270       1.6       jtc 			if (mflag)
    271       1.1       cgd 				action = S_IGN;
    272       1.1       cgd 			break;
    273       1.1       cgd #endif
    274       1.1       cgd 		}
    275       1.1       cgd 	}
    276      1.14  christos 
    277       1.6       jtc 	t = &sigmode[signo - 1];
    278      1.24     elric 	if (*t == 0) {
    279      1.16  christos 		/*
    280      1.16  christos 		 * current setting unknown
    281       1.1       cgd 		 */
    282      1.15  christos 		if (!getsigaction(signo, &sigact)) {
    283      1.15  christos 			/*
    284      1.15  christos 			 * Pretend it worked; maybe we should give a warning
    285      1.15  christos 			 * here, but other shells don't. We don't alter
    286      1.15  christos 			 * sigmode, so that we retry every time.
    287      1.15  christos 			 */
    288      1.15  christos 			return 0;
    289      1.15  christos 		}
    290       1.1       cgd 		if (sigact == SIG_IGN) {
    291      1.16  christos 			if (mflag && (signo == SIGTSTP ||
    292       1.6       jtc 			     signo == SIGTTIN || signo == SIGTTOU)) {
    293      1.24     elric 				*t = S_IGN;	/* don't hard ignore these */
    294       1.6       jtc 			} else
    295      1.24     elric 				*t = S_HARD_IGN;
    296       1.1       cgd 		} else {
    297      1.24     elric 			*t = S_RESET;	/* force to be set */
    298       1.1       cgd 		}
    299       1.1       cgd 	}
    300      1.24     elric 	if (*t == S_HARD_IGN || *t == action)
    301       1.1       cgd 		return 0;
    302       1.1       cgd 	switch (action) {
    303       1.1       cgd 		case S_DFL:	sigact = SIG_DFL;	break;
    304       1.1       cgd 		case S_CATCH:  	sigact = onsig;		break;
    305       1.1       cgd 		case S_IGN:	sigact = SIG_IGN;	break;
    306       1.1       cgd 	}
    307      1.24     elric 	*t = action;
    308      1.19  christos 	siginterrupt(signo, 1);
    309      1.11       cgd 	return (long)signal(signo, sigact);
    310       1.1       cgd }
    311       1.1       cgd 
    312       1.6       jtc /*
    313       1.6       jtc  * Return the current setting for sig w/o changing it.
    314       1.6       jtc  */
    315      1.15  christos static int
    316      1.16  christos getsigaction(signo, sigact)
    317      1.10       cgd 	int signo;
    318      1.15  christos 	sig_t *sigact;
    319      1.10       cgd {
    320       1.6       jtc 	struct sigaction sa;
    321       1.6       jtc 
    322       1.6       jtc 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
    323      1.15  christos 		return 0;
    324      1.15  christos 	*sigact = (sig_t) sa.sa_handler;
    325      1.15  christos 	return 1;
    326       1.6       jtc }
    327       1.1       cgd 
    328       1.1       cgd /*
    329       1.1       cgd  * Ignore a signal.
    330       1.1       cgd  */
    331       1.1       cgd 
    332       1.1       cgd void
    333      1.24     elric ignoresig(signo)
    334      1.10       cgd 	int signo;
    335      1.10       cgd {
    336       1.6       jtc 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
    337       1.1       cgd 		signal(signo, SIG_IGN);
    338       1.1       cgd 	}
    339      1.24     elric 	sigmode[signo - 1] = S_HARD_IGN;
    340       1.1       cgd }
    341       1.1       cgd 
    342       1.1       cgd 
    343       1.1       cgd #ifdef mkinit
    344       1.8       jtc INCLUDE <signal.h>
    345       1.1       cgd INCLUDE "trap.h"
    346       1.1       cgd 
    347       1.1       cgd SHELLPROC {
    348       1.1       cgd 	char *sm;
    349       1.1       cgd 
    350      1.24     elric 	clear_traps();
    351       1.8       jtc 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
    352       1.1       cgd 		if (*sm == S_IGN)
    353       1.1       cgd 			*sm = S_HARD_IGN;
    354       1.1       cgd 	}
    355       1.1       cgd }
    356       1.1       cgd #endif
    357       1.1       cgd 
    358       1.1       cgd 
    359       1.1       cgd 
    360       1.1       cgd /*
    361       1.1       cgd  * Signal handler.
    362       1.1       cgd  */
    363       1.1       cgd 
    364       1.1       cgd void
    365      1.16  christos onsig(signo)
    366      1.10       cgd 	int signo;
    367      1.10       cgd {
    368       1.1       cgd 	signal(signo, onsig);
    369       1.1       cgd 	if (signo == SIGINT && trap[SIGINT] == NULL) {
    370       1.1       cgd 		onint();
    371       1.1       cgd 		return;
    372       1.1       cgd 	}
    373       1.6       jtc 	gotsig[signo - 1] = 1;
    374       1.1       cgd 	pendingsigs++;
    375       1.1       cgd }
    376       1.1       cgd 
    377       1.1       cgd 
    378       1.1       cgd 
    379       1.1       cgd /*
    380       1.1       cgd  * Called to execute a trap.  Perhaps we should avoid entering new trap
    381       1.1       cgd  * handlers while we are executing a trap handler.
    382       1.1       cgd  */
    383       1.1       cgd 
    384       1.1       cgd void
    385       1.1       cgd dotrap() {
    386       1.1       cgd 	int i;
    387       1.1       cgd 	int savestatus;
    388       1.1       cgd 
    389       1.1       cgd 	for (;;) {
    390       1.1       cgd 		for (i = 1 ; ; i++) {
    391       1.6       jtc 			if (gotsig[i - 1])
    392       1.6       jtc 				break;
    393       1.8       jtc 			if (i >= NSIG)
    394       1.5   mycroft 				goto done;
    395       1.1       cgd 		}
    396       1.6       jtc 		gotsig[i - 1] = 0;
    397       1.1       cgd 		savestatus=exitstatus;
    398      1.22  christos 		evalstring(trap[i], 0);
    399       1.1       cgd 		exitstatus=savestatus;
    400       1.1       cgd 	}
    401       1.1       cgd done:
    402       1.1       cgd 	pendingsigs = 0;
    403       1.1       cgd }
    404       1.1       cgd 
    405       1.1       cgd 
    406       1.1       cgd 
    407       1.1       cgd /*
    408       1.1       cgd  * Controls whether the shell is interactive or not.
    409       1.1       cgd  */
    410       1.1       cgd 
    411       1.1       cgd 
    412       1.1       cgd void
    413      1.10       cgd setinteractive(on)
    414      1.10       cgd 	int on;
    415      1.10       cgd {
    416       1.6       jtc 	static int is_interactive;
    417       1.6       jtc 
    418       1.1       cgd 	if (on == is_interactive)
    419       1.1       cgd 		return;
    420      1.24     elric 	setsignal(SIGINT);
    421      1.24     elric 	setsignal(SIGQUIT);
    422      1.24     elric 	setsignal(SIGTERM);
    423       1.1       cgd 	is_interactive = on;
    424       1.1       cgd }
    425       1.1       cgd 
    426       1.1       cgd 
    427       1.1       cgd 
    428       1.1       cgd /*
    429       1.1       cgd  * Called to exit the shell.
    430       1.1       cgd  */
    431       1.1       cgd 
    432       1.1       cgd void
    433      1.16  christos exitshell(status)
    434      1.10       cgd 	int status;
    435      1.10       cgd {
    436       1.1       cgd 	struct jmploc loc1, loc2;
    437       1.1       cgd 	char *p;
    438       1.1       cgd 
    439       1.1       cgd 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
    440       1.6       jtc 	if (setjmp(loc1.loc)) {
    441       1.6       jtc 		goto l1;
    442       1.6       jtc 	}
    443       1.6       jtc 	if (setjmp(loc2.loc)) {
    444       1.6       jtc 		goto l2;
    445       1.6       jtc 	}
    446       1.1       cgd 	handler = &loc1;
    447       1.1       cgd 	if ((p = trap[0]) != NULL && *p != '\0') {
    448       1.1       cgd 		trap[0] = NULL;
    449      1.22  christos 		evalstring(p, 0);
    450       1.1       cgd 	}
    451       1.1       cgd l1:   handler = &loc2;			/* probably unnecessary */
    452       1.1       cgd 	flushall();
    453       1.1       cgd #if JOBS
    454       1.1       cgd 	setjobctl(0);
    455       1.1       cgd #endif
    456       1.1       cgd l2:   _exit(status);
    457      1.18   mycroft 	/* NOTREACHED */
    458       1.1       cgd }
    459