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