Home | History | Annotate | Line # | Download | only in sh
trap.c revision 1.3
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Kenneth Almquist.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd static char sccsid[] = "@(#)trap.c	5.2 (Berkeley) 4/12/91";
     39  1.3  cgd static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/sh/trap.c,v 1.3 1993/03/23 00:29:28 cgd Exp $";
     40  1.1  cgd #endif /* not lint */
     41  1.1  cgd 
     42  1.1  cgd #include "shell.h"
     43  1.1  cgd #include "main.h"
     44  1.1  cgd #include "nodes.h"	/* for other headers */
     45  1.1  cgd #include "eval.h"
     46  1.1  cgd #include "jobs.h"
     47  1.1  cgd #include "options.h"
     48  1.1  cgd #include "syntax.h"
     49  1.1  cgd #include "signames.h"
     50  1.1  cgd #include "output.h"
     51  1.1  cgd #include "memalloc.h"
     52  1.1  cgd #include "error.h"
     53  1.1  cgd #include "trap.h"
     54  1.1  cgd #include "mystring.h"
     55  1.1  cgd #include <signal.h>
     56  1.1  cgd 
     57  1.1  cgd 
     58  1.1  cgd /*
     59  1.1  cgd  * Sigmode records the current value of the signal handlers for the various
     60  1.1  cgd  * modes.  A value of zero means that the current handler is not known.
     61  1.1  cgd  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
     62  1.1  cgd  */
     63  1.1  cgd 
     64  1.1  cgd #define S_DFL 1			/* default signal handling (SIG_DFL) */
     65  1.1  cgd #define S_CATCH 2		/* signal is caught */
     66  1.1  cgd #define S_IGN 3			/* signal is ignored (SIG_IGN) */
     67  1.1  cgd #define S_HARD_IGN 4		/* signal is ignored permenantly */
     68  1.1  cgd 
     69  1.1  cgd 
     70  1.1  cgd extern char nullstr[1];		/* null string */
     71  1.1  cgd 
     72  1.1  cgd char *trap[MAXSIG+1];		/* trap handler commands */
     73  1.1  cgd MKINIT char sigmode[MAXSIG];	/* current value of signal */
     74  1.1  cgd char gotsig[MAXSIG];		/* indicates specified signal received */
     75  1.1  cgd int pendingsigs;			/* indicates some signal received */
     76  1.1  cgd 
     77  1.1  cgd /*
     78  1.1  cgd  * The trap builtin.
     79  1.1  cgd  */
     80  1.1  cgd 
     81  1.1  cgd trapcmd(argc, argv)  char **argv; {
     82  1.1  cgd 	char *action;
     83  1.1  cgd 	char **ap;
     84  1.1  cgd 	int signo;
     85  1.1  cgd 
     86  1.1  cgd 	if (argc <= 1) {
     87  1.1  cgd 		for (signo = 0 ; signo <= MAXSIG ; signo++) {
     88  1.1  cgd 			if (trap[signo] != NULL)
     89  1.1  cgd 				out1fmt("%d: %s\n", signo, trap[signo]);
     90  1.1  cgd 		}
     91  1.1  cgd 		return 0;
     92  1.1  cgd 	}
     93  1.1  cgd 	ap = argv + 1;
     94  1.1  cgd 	if (is_number(*ap))
     95  1.1  cgd 		action = NULL;
     96  1.1  cgd 	else
     97  1.1  cgd 		action = *ap++;
     98  1.1  cgd 	while (*ap) {
     99  1.1  cgd 		if ((signo = number(*ap)) < 0 || signo > MAXSIG)
    100  1.1  cgd 			error("%s: bad trap", *ap);
    101  1.1  cgd 		INTOFF;
    102  1.1  cgd 		if (action)
    103  1.1  cgd 			action = savestr(action);
    104  1.1  cgd 		if (trap[signo])
    105  1.1  cgd 			ckfree(trap[signo]);
    106  1.1  cgd 		trap[signo] = action;
    107  1.1  cgd 		if (signo != 0)
    108  1.1  cgd 			setsignal(signo);
    109  1.1  cgd 		INTON;
    110  1.1  cgd 		ap++;
    111  1.1  cgd 	}
    112  1.1  cgd 	return 0;
    113  1.1  cgd }
    114  1.1  cgd 
    115  1.1  cgd 
    116  1.1  cgd 
    117  1.1  cgd /*
    118  1.1  cgd  * Clear traps on a fork.
    119  1.1  cgd  */
    120  1.1  cgd 
    121  1.1  cgd void
    122  1.1  cgd clear_traps() {
    123  1.1  cgd 	char **tp;
    124  1.1  cgd 
    125  1.1  cgd 	for (tp = trap ; tp <= &trap[MAXSIG] ; tp++) {
    126  1.1  cgd 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
    127  1.1  cgd 			INTOFF;
    128  1.1  cgd 			ckfree(*tp);
    129  1.1  cgd 			*tp = NULL;
    130  1.1  cgd 			if (tp != &trap[0])
    131  1.1  cgd 				setsignal(tp - trap);
    132  1.1  cgd 			INTON;
    133  1.1  cgd 		}
    134  1.1  cgd 	}
    135  1.1  cgd }
    136  1.1  cgd 
    137  1.1  cgd 
    138  1.1  cgd 
    139  1.1  cgd /*
    140  1.1  cgd  * Set the signal handler for the specified signal.  The routine figures
    141  1.1  cgd  * out what it should be set to.
    142  1.1  cgd  */
    143  1.1  cgd 
    144  1.1  cgd int
    145  1.1  cgd setsignal(signo) {
    146  1.1  cgd 	int action;
    147  1.1  cgd 	sig_t sigact;
    148  1.1  cgd 	char *t;
    149  1.1  cgd 	extern void onsig();
    150  1.1  cgd 
    151  1.1  cgd 	if ((t = trap[signo]) == NULL)
    152  1.1  cgd 		action = S_DFL;
    153  1.1  cgd 	else if (*t != '\0')
    154  1.1  cgd 		action = S_CATCH;
    155  1.1  cgd 	else
    156  1.1  cgd 		action = S_IGN;
    157  1.1  cgd 	if (rootshell && action == S_DFL) {
    158  1.1  cgd 		switch (signo) {
    159  1.1  cgd 		case SIGINT:
    160  1.1  cgd 			if (iflag)
    161  1.1  cgd 				action = S_CATCH;
    162  1.1  cgd 			break;
    163  1.1  cgd 		case SIGQUIT:
    164  1.1  cgd #ifdef DEBUG
    165  1.1  cgd 			{
    166  1.1  cgd 			extern int debug;
    167  1.1  cgd 
    168  1.1  cgd 			if (debug)
    169  1.1  cgd 				break;
    170  1.1  cgd 			}
    171  1.1  cgd #endif
    172  1.1  cgd 			/* FALLTHROUGH */
    173  1.1  cgd 		case SIGTERM:
    174  1.1  cgd 			if (iflag)
    175  1.1  cgd 				action = S_IGN;
    176  1.1  cgd 			break;
    177  1.1  cgd #if JOBS
    178  1.1  cgd 		case SIGTSTP:
    179  1.1  cgd 		case SIGTTOU:
    180  1.1  cgd 			if (jflag)
    181  1.1  cgd 				action = S_IGN;
    182  1.1  cgd 			break;
    183  1.1  cgd #endif
    184  1.1  cgd 		}
    185  1.1  cgd 	}
    186  1.1  cgd 	t = &sigmode[signo - 1];
    187  1.1  cgd 	if (*t == 0) {	/* current setting unknown */
    188  1.1  cgd 		/*
    189  1.1  cgd 		 * There is a race condition here if action is not S_IGN.
    190  1.1  cgd 		 * A signal can be ignored that shouldn't be.
    191  1.1  cgd 		 */
    192  1.1  cgd 		if ((int)(sigact = signal(signo, SIG_IGN)) == -1)
    193  1.1  cgd 			error("Signal system call failed");
    194  1.1  cgd 		if (sigact == SIG_IGN) {
    195  1.1  cgd 			*t = S_HARD_IGN;
    196  1.1  cgd 		} else {
    197  1.1  cgd 			*t = S_IGN;
    198  1.1  cgd 		}
    199  1.1  cgd 	}
    200  1.1  cgd 	if (*t == S_HARD_IGN || *t == action)
    201  1.1  cgd 		return 0;
    202  1.1  cgd 	switch (action) {
    203  1.1  cgd 		case S_DFL:	sigact = SIG_DFL;	break;
    204  1.1  cgd 		case S_CATCH:  	sigact = onsig;		break;
    205  1.1  cgd 		case S_IGN:	sigact = SIG_IGN;	break;
    206  1.1  cgd 	}
    207  1.1  cgd 	*t = action;
    208  1.1  cgd 	return (int)signal(signo, sigact);
    209  1.1  cgd }
    210  1.1  cgd 
    211  1.1  cgd 
    212  1.1  cgd /*
    213  1.1  cgd  * Ignore a signal.
    214  1.1  cgd  */
    215  1.1  cgd 
    216  1.1  cgd void
    217  1.1  cgd ignoresig(signo) {
    218  1.1  cgd 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
    219  1.1  cgd 		signal(signo, SIG_IGN);
    220  1.1  cgd 	}
    221  1.1  cgd 	sigmode[signo - 1] = S_HARD_IGN;
    222  1.1  cgd }
    223  1.1  cgd 
    224  1.1  cgd 
    225  1.1  cgd #ifdef mkinit
    226  1.1  cgd INCLUDE "signames.h"
    227  1.1  cgd INCLUDE "trap.h"
    228  1.1  cgd 
    229  1.1  cgd SHELLPROC {
    230  1.1  cgd 	char *sm;
    231  1.1  cgd 
    232  1.1  cgd 	clear_traps();
    233  1.1  cgd 	for (sm = sigmode ; sm < sigmode + MAXSIG ; sm++) {
    234  1.1  cgd 		if (*sm == S_IGN)
    235  1.1  cgd 			*sm = S_HARD_IGN;
    236  1.1  cgd 	}
    237  1.1  cgd }
    238  1.1  cgd #endif
    239  1.1  cgd 
    240  1.1  cgd 
    241  1.1  cgd 
    242  1.1  cgd /*
    243  1.1  cgd  * Signal handler.
    244  1.1  cgd  */
    245  1.1  cgd 
    246  1.1  cgd void
    247  1.1  cgd onsig(signo) {
    248  1.1  cgd 	signal(signo, onsig);
    249  1.1  cgd 	if (signo == SIGINT && trap[SIGINT] == NULL) {
    250  1.1  cgd 		onint();
    251  1.1  cgd 		return;
    252  1.1  cgd 	}
    253  1.1  cgd 	gotsig[signo - 1] = 1;
    254  1.1  cgd 	pendingsigs++;
    255  1.1  cgd }
    256  1.1  cgd 
    257  1.1  cgd 
    258  1.1  cgd 
    259  1.1  cgd /*
    260  1.1  cgd  * Called to execute a trap.  Perhaps we should avoid entering new trap
    261  1.1  cgd  * handlers while we are executing a trap handler.
    262  1.1  cgd  */
    263  1.1  cgd 
    264  1.1  cgd void
    265  1.1  cgd dotrap() {
    266  1.1  cgd 	int i;
    267  1.1  cgd 	int savestatus;
    268  1.1  cgd 
    269  1.1  cgd 	for (;;) {
    270  1.1  cgd 		for (i = 1 ; ; i++) {
    271  1.1  cgd 			if (gotsig[i - 1])
    272  1.1  cgd 				break;
    273  1.1  cgd 			if (i >= MAXSIG)
    274  1.1  cgd 				goto done;
    275  1.1  cgd 		}
    276  1.1  cgd 		gotsig[i - 1] = 0;
    277  1.1  cgd 		savestatus=exitstatus;
    278  1.1  cgd 		evalstring(trap[i]);
    279  1.1  cgd 		exitstatus=savestatus;
    280  1.1  cgd 	}
    281  1.1  cgd done:
    282  1.1  cgd 	pendingsigs = 0;
    283  1.1  cgd }
    284  1.1  cgd 
    285  1.1  cgd 
    286  1.1  cgd 
    287  1.1  cgd /*
    288  1.1  cgd  * Controls whether the shell is interactive or not.
    289  1.1  cgd  */
    290  1.1  cgd 
    291  1.1  cgd int is_interactive;
    292  1.1  cgd 
    293  1.1  cgd void
    294  1.1  cgd setinteractive(on) {
    295  1.1  cgd 	if (on == is_interactive)
    296  1.1  cgd 		return;
    297  1.1  cgd 	setsignal(SIGINT);
    298  1.1  cgd 	setsignal(SIGQUIT);
    299  1.1  cgd 	setsignal(SIGTERM);
    300  1.1  cgd 	is_interactive = on;
    301  1.1  cgd }
    302  1.1  cgd 
    303  1.1  cgd 
    304  1.1  cgd 
    305  1.1  cgd /*
    306  1.1  cgd  * Called to exit the shell.
    307  1.1  cgd  */
    308  1.1  cgd 
    309  1.1  cgd void
    310  1.1  cgd exitshell(status) {
    311  1.1  cgd 	struct jmploc loc1, loc2;
    312  1.1  cgd 	char *p;
    313  1.1  cgd 
    314  1.1  cgd 	TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
    315  1.1  cgd 	if (setjmp(loc1.loc))  goto l1;
    316  1.1  cgd 	if (setjmp(loc2.loc))  goto l2;
    317  1.1  cgd 	handler = &loc1;
    318  1.1  cgd 	if ((p = trap[0]) != NULL && *p != '\0') {
    319  1.1  cgd 		trap[0] = NULL;
    320  1.1  cgd 		evalstring(p);
    321  1.1  cgd 	}
    322  1.1  cgd l1:   handler = &loc2;			/* probably unnecessary */
    323  1.1  cgd 	flushall();
    324  1.1  cgd #if JOBS
    325  1.1  cgd 	setjobctl(0);
    326  1.1  cgd #endif
    327  1.1  cgd l2:   _exit(status);
    328  1.1  cgd }
    329