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