Home | History | Annotate | Line # | Download | only in sh
trap.c revision 1.23
      1  1.23     elric /*	$NetBSD: trap.c,v 1.23 2000/05/13 20:50:15 elric 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.23     elric __RCSID("$NetBSD: trap.c,v 1.23 2000/05/13 20:50:15 elric 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.1       cgd extern char nullstr[1];		/* null string */
     81   1.1       cgd 
     82   1.8       jtc char *trap[NSIG+1];		/* trap handler commands */
     83   1.8       jtc MKINIT char sigmode[NSIG];	/* current value of signal */
     84   1.8       jtc char gotsig[NSIG];		/* indicates specified signal received */
     85   1.6       jtc int pendingsigs;			/* indicates some signal received */
     86   1.1       cgd 
     87  1.15  christos static int getsigaction __P((int, sig_t *));
     88  1.15  christos 
     89   1.1       cgd /*
     90   1.1       cgd  * The trap builtin.
     91   1.1       cgd  */
     92   1.1       cgd 
     93  1.10       cgd int
     94  1.10       cgd trapcmd(argc, argv)
     95  1.10       cgd 	int argc;
     96  1.16  christos 	char **argv;
     97  1.10       cgd {
     98   1.1       cgd 	char *action;
     99   1.1       cgd 	char **ap;
    100   1.1       cgd 	int signo;
    101   1.1       cgd 
    102   1.1       cgd 	if (argc <= 1) {
    103   1.8       jtc 		for (signo = 0 ; signo <= NSIG ; signo++) {
    104   1.1       cgd 			if (trap[signo] != NULL)
    105   1.1       cgd 				out1fmt("%d: %s\n", signo, trap[signo]);
    106   1.1       cgd 		}
    107   1.1       cgd 		return 0;
    108   1.1       cgd 	}
    109   1.1       cgd 	ap = argv + 1;
    110   1.1       cgd 	if (is_number(*ap))
    111   1.1       cgd 		action = NULL;
    112   1.1       cgd 	else
    113   1.1       cgd 		action = *ap++;
    114   1.1       cgd 	while (*ap) {
    115   1.8       jtc 		if ((signo = number(*ap)) < 0 || signo > NSIG)
    116   1.1       cgd 			error("%s: bad trap", *ap);
    117   1.1       cgd 		INTOFF;
    118   1.1       cgd 		if (action)
    119   1.1       cgd 			action = savestr(action);
    120   1.1       cgd 		if (trap[signo])
    121   1.1       cgd 			ckfree(trap[signo]);
    122   1.1       cgd 		trap[signo] = action;
    123   1.1       cgd 		if (signo != 0)
    124  1.23     elric 			setsignal(signo, 0);
    125   1.1       cgd 		INTON;
    126   1.1       cgd 		ap++;
    127   1.1       cgd 	}
    128   1.1       cgd 	return 0;
    129   1.1       cgd }
    130   1.1       cgd 
    131   1.1       cgd 
    132   1.1       cgd 
    133   1.1       cgd /*
    134  1.23     elric  * Clear traps on a fork or vfork.
    135  1.23     elric  * Takes one arg vfork, to tell it to not be destructive of
    136  1.23     elric  * the parents variables.
    137   1.1       cgd  */
    138   1.1       cgd 
    139   1.1       cgd void
    140  1.23     elric clear_traps(vforked)
    141  1.23     elric 	int vforked;
    142  1.23     elric {
    143   1.1       cgd 	char **tp;
    144   1.1       cgd 
    145   1.8       jtc 	for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
    146   1.1       cgd 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
    147   1.1       cgd 			INTOFF;
    148  1.23     elric 			if (!vforked) {
    149  1.23     elric 				ckfree(*tp);
    150  1.23     elric 				*tp = NULL;
    151  1.23     elric 			}
    152   1.1       cgd 			if (tp != &trap[0])
    153  1.23     elric 				setsignal(tp - trap, vforked);
    154   1.1       cgd 			INTON;
    155   1.1       cgd 		}
    156   1.1       cgd 	}
    157   1.1       cgd }
    158   1.1       cgd 
    159   1.1       cgd 
    160   1.1       cgd 
    161   1.1       cgd /*
    162   1.1       cgd  * Set the signal handler for the specified signal.  The routine figures
    163   1.1       cgd  * out what it should be set to.
    164   1.1       cgd  */
    165   1.1       cgd 
    166  1.11       cgd long
    167  1.23     elric setsignal(signo, vforked)
    168  1.10       cgd 	int signo;
    169  1.23     elric 	int vforked;
    170  1.10       cgd {
    171   1.1       cgd 	int action;
    172  1.13  christos 	sig_t sigact = SIG_DFL;
    173  1.23     elric 	char *t, tsig;
    174   1.1       cgd 
    175   1.1       cgd 	if ((t = trap[signo]) == NULL)
    176   1.1       cgd 		action = S_DFL;
    177   1.1       cgd 	else if (*t != '\0')
    178   1.1       cgd 		action = S_CATCH;
    179   1.1       cgd 	else
    180   1.1       cgd 		action = S_IGN;
    181  1.23     elric 	if (rootshell && !vforked && action == S_DFL) {
    182   1.1       cgd 		switch (signo) {
    183   1.1       cgd 		case SIGINT:
    184  1.21  christos 			if (iflag || minusc || sflag == 0)
    185   1.1       cgd 				action = S_CATCH;
    186   1.1       cgd 			break;
    187   1.1       cgd 		case SIGQUIT:
    188   1.1       cgd #ifdef DEBUG
    189   1.1       cgd 			{
    190   1.1       cgd 			extern int debug;
    191   1.1       cgd 
    192   1.1       cgd 			if (debug)
    193   1.1       cgd 				break;
    194   1.1       cgd 			}
    195   1.1       cgd #endif
    196   1.1       cgd 			/* FALLTHROUGH */
    197   1.1       cgd 		case SIGTERM:
    198   1.1       cgd 			if (iflag)
    199   1.1       cgd 				action = S_IGN;
    200   1.1       cgd 			break;
    201   1.1       cgd #if JOBS
    202   1.1       cgd 		case SIGTSTP:
    203   1.1       cgd 		case SIGTTOU:
    204   1.6       jtc 			if (mflag)
    205   1.1       cgd 				action = S_IGN;
    206   1.1       cgd 			break;
    207   1.1       cgd #endif
    208   1.1       cgd 		}
    209   1.1       cgd 	}
    210  1.14  christos 
    211   1.6       jtc 	t = &sigmode[signo - 1];
    212  1.23     elric 	tsig = *t;
    213  1.23     elric 	if (tsig == 0) {
    214  1.16  christos 		/*
    215  1.16  christos 		 * current setting unknown
    216   1.1       cgd 		 */
    217  1.15  christos 		if (!getsigaction(signo, &sigact)) {
    218  1.15  christos 			/*
    219  1.15  christos 			 * Pretend it worked; maybe we should give a warning
    220  1.15  christos 			 * here, but other shells don't. We don't alter
    221  1.15  christos 			 * sigmode, so that we retry every time.
    222  1.15  christos 			 */
    223  1.15  christos 			return 0;
    224  1.15  christos 		}
    225   1.1       cgd 		if (sigact == SIG_IGN) {
    226  1.16  christos 			if (mflag && (signo == SIGTSTP ||
    227   1.6       jtc 			     signo == SIGTTIN || signo == SIGTTOU)) {
    228  1.23     elric 				tsig = S_IGN;	/* don't hard ignore these */
    229   1.6       jtc 			} else
    230  1.23     elric 				tsig = S_HARD_IGN;
    231   1.1       cgd 		} else {
    232  1.23     elric 			tsig = S_RESET;	/* force to be set */
    233   1.1       cgd 		}
    234   1.1       cgd 	}
    235  1.23     elric 	if (tsig == S_HARD_IGN || tsig == action)
    236   1.1       cgd 		return 0;
    237   1.1       cgd 	switch (action) {
    238   1.1       cgd 		case S_DFL:	sigact = SIG_DFL;	break;
    239   1.1       cgd 		case S_CATCH:  	sigact = onsig;		break;
    240   1.1       cgd 		case S_IGN:	sigact = SIG_IGN;	break;
    241   1.1       cgd 	}
    242  1.23     elric 	if (!vforked)
    243  1.23     elric 		*t = action;
    244  1.19  christos 	siginterrupt(signo, 1);
    245  1.11       cgd 	return (long)signal(signo, sigact);
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.6       jtc /*
    249   1.6       jtc  * Return the current setting for sig w/o changing it.
    250   1.6       jtc  */
    251  1.15  christos static int
    252  1.16  christos getsigaction(signo, sigact)
    253  1.10       cgd 	int signo;
    254  1.15  christos 	sig_t *sigact;
    255  1.10       cgd {
    256   1.6       jtc 	struct sigaction sa;
    257   1.6       jtc 
    258   1.6       jtc 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
    259  1.15  christos 		return 0;
    260  1.15  christos 	*sigact = (sig_t) sa.sa_handler;
    261  1.15  christos 	return 1;
    262   1.6       jtc }
    263   1.1       cgd 
    264   1.1       cgd /*
    265   1.1       cgd  * Ignore a signal.
    266   1.1       cgd  */
    267   1.1       cgd 
    268   1.1       cgd void
    269  1.23     elric ignoresig(signo, vforked)
    270  1.10       cgd 	int signo;
    271  1.23     elric 	int vforked;
    272  1.10       cgd {
    273   1.6       jtc 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
    274   1.1       cgd 		signal(signo, SIG_IGN);
    275   1.1       cgd 	}
    276  1.23     elric 	if (!vforked)
    277  1.23     elric 		sigmode[signo - 1] = S_HARD_IGN;
    278   1.1       cgd }
    279   1.1       cgd 
    280   1.1       cgd 
    281   1.1       cgd #ifdef mkinit
    282   1.8       jtc INCLUDE <signal.h>
    283   1.1       cgd INCLUDE "trap.h"
    284   1.1       cgd 
    285   1.1       cgd SHELLPROC {
    286   1.1       cgd 	char *sm;
    287   1.1       cgd 
    288  1.23     elric 	clear_traps(0);
    289   1.8       jtc 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
    290   1.1       cgd 		if (*sm == S_IGN)
    291   1.1       cgd 			*sm = S_HARD_IGN;
    292   1.1       cgd 	}
    293   1.1       cgd }
    294   1.1       cgd #endif
    295   1.1       cgd 
    296   1.1       cgd 
    297   1.1       cgd 
    298   1.1       cgd /*
    299   1.1       cgd  * Signal handler.
    300   1.1       cgd  */
    301   1.1       cgd 
    302   1.1       cgd void
    303  1.16  christos onsig(signo)
    304  1.10       cgd 	int signo;
    305  1.10       cgd {
    306   1.1       cgd 	signal(signo, onsig);
    307   1.1       cgd 	if (signo == SIGINT && trap[SIGINT] == NULL) {
    308   1.1       cgd 		onint();
    309   1.1       cgd 		return;
    310   1.1       cgd 	}
    311   1.6       jtc 	gotsig[signo - 1] = 1;
    312   1.1       cgd 	pendingsigs++;
    313   1.1       cgd }
    314   1.1       cgd 
    315   1.1       cgd 
    316   1.1       cgd 
    317   1.1       cgd /*
    318   1.1       cgd  * Called to execute a trap.  Perhaps we should avoid entering new trap
    319   1.1       cgd  * handlers while we are executing a trap handler.
    320   1.1       cgd  */
    321   1.1       cgd 
    322   1.1       cgd void
    323   1.1       cgd dotrap() {
    324   1.1       cgd 	int i;
    325   1.1       cgd 	int savestatus;
    326   1.1       cgd 
    327   1.1       cgd 	for (;;) {
    328   1.1       cgd 		for (i = 1 ; ; i++) {
    329   1.6       jtc 			if (gotsig[i - 1])
    330   1.6       jtc 				break;
    331   1.8       jtc 			if (i >= NSIG)
    332   1.5   mycroft 				goto done;
    333   1.1       cgd 		}
    334   1.6       jtc 		gotsig[i - 1] = 0;
    335   1.1       cgd 		savestatus=exitstatus;
    336  1.22  christos 		evalstring(trap[i], 0);
    337   1.1       cgd 		exitstatus=savestatus;
    338   1.1       cgd 	}
    339   1.1       cgd done:
    340   1.1       cgd 	pendingsigs = 0;
    341   1.1       cgd }
    342   1.1       cgd 
    343   1.1       cgd 
    344   1.1       cgd 
    345   1.1       cgd /*
    346   1.1       cgd  * Controls whether the shell is interactive or not.
    347   1.1       cgd  */
    348   1.1       cgd 
    349   1.1       cgd 
    350   1.1       cgd void
    351  1.10       cgd setinteractive(on)
    352  1.10       cgd 	int on;
    353  1.10       cgd {
    354   1.6       jtc 	static int is_interactive;
    355   1.6       jtc 
    356   1.1       cgd 	if (on == is_interactive)
    357   1.1       cgd 		return;
    358  1.23     elric 	setsignal(SIGINT, 0);
    359  1.23     elric 	setsignal(SIGQUIT, 0);
    360  1.23     elric 	setsignal(SIGTERM, 0);
    361   1.1       cgd 	is_interactive = on;
    362   1.1       cgd }
    363   1.1       cgd 
    364   1.1       cgd 
    365   1.1       cgd 
    366   1.1       cgd /*
    367   1.1       cgd  * Called to exit the shell.
    368   1.1       cgd  */
    369   1.1       cgd 
    370   1.1       cgd void
    371  1.16  christos exitshell(status)
    372  1.10       cgd 	int status;
    373  1.10       cgd {
    374   1.1       cgd 	struct jmploc loc1, loc2;
    375   1.1       cgd 	char *p;
    376   1.1       cgd 
    377   1.1       cgd 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
    378   1.6       jtc 	if (setjmp(loc1.loc)) {
    379   1.6       jtc 		goto l1;
    380   1.6       jtc 	}
    381   1.6       jtc 	if (setjmp(loc2.loc)) {
    382   1.6       jtc 		goto l2;
    383   1.6       jtc 	}
    384   1.1       cgd 	handler = &loc1;
    385   1.1       cgd 	if ((p = trap[0]) != NULL && *p != '\0') {
    386   1.1       cgd 		trap[0] = NULL;
    387  1.22  christos 		evalstring(p, 0);
    388   1.1       cgd 	}
    389   1.1       cgd l1:   handler = &loc2;			/* probably unnecessary */
    390   1.1       cgd 	flushall();
    391   1.1       cgd #if JOBS
    392   1.1       cgd 	setjobctl(0);
    393   1.1       cgd #endif
    394   1.1       cgd l2:   _exit(status);
    395  1.18   mycroft 	/* NOTREACHED */
    396   1.1       cgd }
    397