Home | History | Annotate | Line # | Download | only in sh
trap.c revision 1.41.4.3
      1  1.41.4.3  pgoyette /*	$NetBSD: trap.c,v 1.41.4.3 2018/11/26 01:49:54 pgoyette 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.29       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35      1.17  christos #include <sys/cdefs.h>
     36       1.1       cgd #ifndef lint
     37      1.12       cgd #if 0
     38      1.15  christos static char sccsid[] = "@(#)trap.c	8.5 (Berkeley) 6/5/95";
     39      1.12       cgd #else
     40  1.41.4.3  pgoyette __RCSID("$NetBSD: trap.c,v 1.41.4.3 2018/11/26 01:49:54 pgoyette Exp $");
     41      1.12       cgd #endif
     42       1.1       cgd #endif /* not lint */
     43       1.1       cgd 
     44      1.13  christos #include <signal.h>
     45      1.13  christos #include <unistd.h>
     46      1.13  christos #include <stdlib.h>
     47      1.40       kre #include <stdio.h>
     48      1.13  christos 
     49       1.1       cgd #include "shell.h"
     50       1.1       cgd #include "main.h"
     51       1.1       cgd #include "nodes.h"	/* for other headers */
     52       1.1       cgd #include "eval.h"
     53       1.1       cgd #include "jobs.h"
     54      1.13  christos #include "show.h"
     55       1.1       cgd #include "options.h"
     56      1.35  christos #include "builtins.h"
     57       1.1       cgd #include "syntax.h"
     58       1.1       cgd #include "output.h"
     59       1.1       cgd #include "memalloc.h"
     60       1.1       cgd #include "error.h"
     61       1.1       cgd #include "trap.h"
     62       1.1       cgd #include "mystring.h"
     63      1.31  christos #include "var.h"
     64       1.1       cgd 
     65       1.1       cgd 
     66       1.1       cgd /*
     67       1.1       cgd  * Sigmode records the current value of the signal handlers for the various
     68       1.1       cgd  * modes.  A value of zero means that the current handler is not known.
     69       1.1       cgd  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
     70       1.1       cgd  */
     71       1.1       cgd 
     72       1.1       cgd #define S_DFL 1			/* default signal handling (SIG_DFL) */
     73       1.1       cgd #define S_CATCH 2		/* signal is caught */
     74       1.1       cgd #define S_IGN 3			/* signal is ignored (SIG_IGN) */
     75       1.1       cgd #define S_HARD_IGN 4		/* signal is ignored permenantly */
     76       1.6       jtc #define S_RESET 5		/* temporary - to reset a hard ignored sig */
     77       1.1       cgd 
     78       1.1       cgd 
     79      1.40       kre char *trap[NSIG];		/* trap handler commands */
     80       1.8       jtc MKINIT char sigmode[NSIG];	/* current value of signal */
     81      1.36  christos static volatile char gotsig[NSIG];/* indicates specified signal received */
     82      1.36  christos volatile int pendingsigs;	/* indicates some signal received */
     83       1.1       cgd 
     84      1.28  christos static int getsigaction(int, sig_t *);
     85      1.40       kre STATIC const char *trap_signame(int);
     86  1.41.4.3  pgoyette void printsignals(struct output *, int);
     87      1.26      wulf 
     88      1.26      wulf /*
     89      1.26      wulf  * return the signal number described by `p' (as a number or a name)
     90      1.26      wulf  * or -1 if it isn't one
     91      1.26      wulf  */
     92      1.26      wulf 
     93      1.26      wulf static int
     94      1.28  christos signame_to_signum(const char *p)
     95      1.26      wulf {
     96      1.26      wulf 	int i;
     97      1.26      wulf 
     98      1.26      wulf 	if (is_number(p))
     99      1.26      wulf 		return number(p);
    100      1.26      wulf 
    101      1.26      wulf 	if (strcasecmp(p, "exit") == 0 )
    102      1.26      wulf 		return 0;
    103      1.26      wulf 
    104  1.41.4.3  pgoyette 	i = signalnumber(p);
    105  1.41.4.3  pgoyette 	if (i == 0)
    106  1.41.4.3  pgoyette 		i = -1;
    107  1.41.4.3  pgoyette 	return i;
    108      1.26      wulf }
    109      1.26      wulf 
    110      1.26      wulf /*
    111      1.40       kre  * return the name of a signal used by the "trap" command
    112      1.40       kre  */
    113      1.40       kre STATIC const char *
    114      1.40       kre trap_signame(int signo)
    115      1.40       kre {
    116      1.40       kre 	static char nbuf[12];
    117  1.41.4.3  pgoyette 	const char *p;
    118      1.40       kre 
    119      1.40       kre 	if (signo == 0)
    120      1.40       kre 		return "EXIT";
    121  1.41.4.3  pgoyette 	p = signalname(signo);
    122      1.40       kre 	if (p != NULL)
    123      1.40       kre 		return p;
    124      1.40       kre 	(void)snprintf(nbuf, sizeof nbuf, "%d", signo);
    125      1.40       kre 	return nbuf;
    126      1.40       kre }
    127      1.40       kre 
    128  1.41.4.3  pgoyette #if 0		/* Share the version of this in src/bin/kill/kill.c */
    129      1.40       kre /*
    130      1.26      wulf  * Print a list of valid signal names
    131      1.26      wulf  */
    132  1.41.4.3  pgoyette void
    133  1.41.4.3  pgoyette printsignals(struct output *out, int len)
    134      1.26      wulf {
    135      1.26      wulf 	int n;
    136      1.26      wulf 
    137  1.41.4.3  pgoyette 	if (len != 0)
    138  1.41.4.3  pgoyette 		outc(' ', out);
    139      1.26      wulf 	for (n = 1; n < NSIG; n++) {
    140  1.41.4.3  pgoyette 		outfmt(out, "%s", trap_signame(n));
    141      1.26      wulf 		if ((n == NSIG/2) ||  n == (NSIG - 1))
    142  1.41.4.3  pgoyette 			outstr("\n", out);
    143      1.26      wulf 		else
    144  1.41.4.3  pgoyette 			outc(' ', out);
    145      1.26      wulf 	}
    146      1.26      wulf }
    147  1.41.4.3  pgoyette #endif
    148      1.15  christos 
    149       1.1       cgd /*
    150       1.1       cgd  * The trap builtin.
    151       1.1       cgd  */
    152       1.1       cgd 
    153      1.10       cgd int
    154      1.28  christos trapcmd(int argc, char **argv)
    155      1.10       cgd {
    156       1.1       cgd 	char *action;
    157       1.1       cgd 	char **ap;
    158       1.1       cgd 	int signo;
    159      1.39       kre 	int errs = 0;
    160      1.40       kre 	int printonly = 0;
    161      1.39       kre 
    162      1.39       kre 	ap = argv + 1;
    163      1.39       kre 
    164      1.39       kre 	if (argc == 2 && strcmp(*ap, "-l") == 0) {
    165  1.41.4.3  pgoyette 		out1str("EXIT");
    166  1.41.4.3  pgoyette 		printsignals(out1, 4);
    167      1.39       kre 		return 0;
    168      1.39       kre 	}
    169      1.40       kre 	if (argc == 2 && strcmp(*ap, "-") == 0) {
    170      1.40       kre 		for (signo = 0; signo < NSIG; signo++) {
    171      1.40       kre 			if (trap[signo] == NULL)
    172      1.40       kre 				continue;
    173      1.40       kre 			INTOFF;
    174      1.40       kre 			ckfree(trap[signo]);
    175      1.40       kre 			trap[signo] = NULL;
    176      1.40       kre 			if (signo != 0)
    177      1.40       kre 				setsignal(signo, 0);
    178      1.40       kre 			INTON;
    179      1.40       kre 		}
    180      1.40       kre 		return 0;
    181      1.40       kre 	}
    182      1.40       kre 	if (argc >= 2 && strcmp(*ap, "-p") == 0) {
    183      1.40       kre 		printonly = 1;
    184      1.40       kre 		ap++;
    185      1.40       kre 		argc--;
    186      1.40       kre 	}
    187      1.39       kre 
    188      1.39       kre 	if (argc > 1 && strcmp(*ap, "--") == 0) {
    189      1.39       kre 		argc--;
    190      1.39       kre 		ap++;
    191      1.39       kre 	}
    192       1.1       cgd 
    193       1.1       cgd 	if (argc <= 1) {
    194      1.40       kre 		int count;
    195      1.40       kre 
    196      1.40       kre 		if (printonly) {
    197      1.40       kre 			for (count = 0, signo = 0 ; signo < NSIG ; signo++)
    198      1.40       kre 				if (trap[signo] == NULL) {
    199      1.40       kre 					if (count == 0)
    200      1.40       kre 						out1str("trap -- -");
    201      1.40       kre 					out1fmt(" %s", trap_signame(signo));
    202      1.40       kre 					/* oh! unlucky 13 */
    203      1.40       kre 					if (++count >= 13) {
    204      1.40       kre 						out1str("\n");
    205      1.40       kre 						count = 0;
    206      1.40       kre 					}
    207      1.40       kre 				}
    208      1.40       kre 			if (count)
    209      1.40       kre 				out1str("\n");
    210      1.40       kre 		}
    211      1.40       kre 
    212      1.40       kre 		for (count = 0, signo = 0 ; signo < NSIG ; signo++)
    213      1.40       kre 			if (trap[signo] != NULL && trap[signo][0] == '\0') {
    214      1.40       kre 				if (count == 0)
    215      1.40       kre 					out1str("trap -- ''");
    216      1.40       kre 				out1fmt(" %s", trap_signame(signo));
    217      1.40       kre 				/*
    218      1.40       kre 				 * the prefix is 10 bytes, with 4 byte
    219      1.40       kre 				 * signal names (common) we have room in
    220      1.40       kre 				 * the 70 bytes left on a normal line for
    221      1.40       kre 				 * 70/(4+1) signals, that's 14, but to
    222      1.40       kre 				 * allow for the occasional longer sig name
    223      1.40       kre 				 * we output one less...
    224      1.40       kre 				 */
    225      1.40       kre 				if (++count >= 13) {
    226      1.40       kre 					out1str("\n");
    227      1.40       kre 					count = 0;
    228      1.40       kre 				}
    229      1.40       kre 			}
    230      1.40       kre 		if (count)
    231      1.40       kre 			out1str("\n");
    232      1.40       kre 
    233      1.40       kre 		for (signo = 0 ; signo < NSIG ; signo++)
    234      1.40       kre 			if (trap[signo] != NULL && trap[signo][0] != '\0') {
    235      1.40       kre 				out1str("trap -- ");
    236      1.31  christos 				print_quoted(trap[signo]);
    237      1.40       kre 				out1fmt(" %s\n", trap_signame(signo));
    238      1.31  christos 			}
    239      1.40       kre 
    240       1.1       cgd 		return 0;
    241       1.1       cgd 	}
    242      1.26      wulf 
    243      1.26      wulf 	action = NULL;
    244      1.26      wulf 
    245      1.40       kre 	if (!printonly && !is_number(*ap)) {
    246      1.39       kre 		if ((*ap)[0] == '-' && (*ap)[1] == '\0')
    247      1.39       kre 			ap++;			/* reset to default */
    248      1.39       kre 		else
    249      1.39       kre 			action = *ap++;		/* can be '' for "ignore" */
    250      1.39       kre 		argc--;
    251      1.39       kre 	}
    252      1.26      wulf 
    253      1.39       kre 	if (argc < 2) {		/* there must be at least 1 condition */
    254      1.39       kre 		out2str("Usage: trap [-l]\n"
    255      1.40       kre 			"       trap -p [condition ...]\n"
    256      1.39       kre 			"       trap action condition ...\n"
    257      1.39       kre 			"       trap N condition ...\n");
    258      1.39       kre 		return 2;
    259      1.26      wulf 	}
    260      1.26      wulf 
    261      1.39       kre 
    262       1.1       cgd 	while (*ap) {
    263      1.39       kre 		signo = signame_to_signum(*ap);
    264      1.26      wulf 
    265      1.40       kre 		if (signo < 0 || signo >= NSIG) {
    266      1.39       kre 			/* This is not a fatal error, so sayeth posix */
    267      1.39       kre 			outfmt(out2, "trap: '%s' bad condition\n", *ap);
    268      1.39       kre 			errs = 1;
    269      1.40       kre 			ap++;
    270      1.40       kre 			continue;
    271      1.40       kre 		}
    272      1.40       kre 		ap++;
    273      1.40       kre 
    274      1.40       kre 		if (printonly) {
    275      1.40       kre 			out1str("trap -- ");
    276      1.40       kre 			if (trap[signo] == NULL)
    277      1.40       kre 				out1str("-");
    278      1.40       kre 			else
    279      1.40       kre 				print_quoted(trap[signo]);
    280      1.40       kre 			out1fmt(" %s\n", trap_signame(signo));
    281      1.40       kre 			continue;
    282      1.39       kre 		}
    283      1.26      wulf 
    284       1.1       cgd 		INTOFF;
    285       1.1       cgd 		if (action)
    286       1.1       cgd 			action = savestr(action);
    287      1.26      wulf 
    288       1.1       cgd 		if (trap[signo])
    289       1.1       cgd 			ckfree(trap[signo]);
    290      1.26      wulf 
    291       1.1       cgd 		trap[signo] = action;
    292      1.26      wulf 
    293       1.1       cgd 		if (signo != 0)
    294      1.27  christos 			setsignal(signo, 0);
    295       1.1       cgd 		INTON;
    296       1.1       cgd 	}
    297      1.39       kre 	return errs;
    298       1.1       cgd }
    299       1.1       cgd 
    300       1.1       cgd 
    301       1.1       cgd 
    302       1.1       cgd /*
    303      1.27  christos  * Clear traps on a fork or vfork.
    304      1.27  christos  * Takes one arg vfork, to tell it to not be destructive of
    305      1.27  christos  * the parents variables.
    306       1.1       cgd  */
    307       1.1       cgd 
    308       1.1       cgd void
    309      1.28  christos clear_traps(int vforked)
    310      1.27  christos {
    311       1.1       cgd 	char **tp;
    312       1.1       cgd 
    313      1.40       kre 	for (tp = trap ; tp < &trap[NSIG] ; tp++) {
    314       1.1       cgd 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
    315       1.1       cgd 			INTOFF;
    316      1.27  christos 			if (!vforked) {
    317      1.27  christos 				ckfree(*tp);
    318      1.27  christos 				*tp = NULL;
    319      1.27  christos 			}
    320       1.1       cgd 			if (tp != &trap[0])
    321      1.27  christos 				setsignal(tp - trap, vforked);
    322       1.1       cgd 			INTON;
    323       1.1       cgd 		}
    324       1.1       cgd 	}
    325       1.1       cgd }
    326       1.1       cgd 
    327  1.41.4.2  pgoyette /*
    328  1.41.4.2  pgoyette  * See if there are any defined traps
    329  1.41.4.2  pgoyette  */
    330  1.41.4.2  pgoyette int
    331  1.41.4.2  pgoyette have_traps(void)
    332  1.41.4.2  pgoyette {
    333  1.41.4.2  pgoyette 	char **tp;
    334       1.1       cgd 
    335  1.41.4.2  pgoyette 	for (tp = trap ; tp < &trap[NSIG] ; tp++)
    336  1.41.4.2  pgoyette 		if (*tp && **tp)	/* trap not NULL or SIG_IGN */
    337  1.41.4.2  pgoyette 			return 1;
    338  1.41.4.2  pgoyette 	return 0;
    339  1.41.4.2  pgoyette }
    340       1.1       cgd 
    341       1.1       cgd /*
    342       1.1       cgd  * Set the signal handler for the specified signal.  The routine figures
    343       1.1       cgd  * out what it should be set to.
    344       1.1       cgd  */
    345       1.1       cgd 
    346      1.32  christos sig_t
    347      1.28  christos setsignal(int signo, int vforked)
    348      1.10       cgd {
    349       1.1       cgd 	int action;
    350      1.32  christos 	sig_t sigact = SIG_DFL, sig;
    351      1.27  christos 	char *t, tsig;
    352       1.1       cgd 
    353       1.1       cgd 	if ((t = trap[signo]) == NULL)
    354       1.1       cgd 		action = S_DFL;
    355       1.1       cgd 	else if (*t != '\0')
    356       1.1       cgd 		action = S_CATCH;
    357       1.1       cgd 	else
    358       1.1       cgd 		action = S_IGN;
    359      1.27  christos 	if (rootshell && !vforked && action == S_DFL) {
    360       1.1       cgd 		switch (signo) {
    361       1.1       cgd 		case SIGINT:
    362      1.21  christos 			if (iflag || minusc || sflag == 0)
    363       1.1       cgd 				action = S_CATCH;
    364       1.1       cgd 			break;
    365       1.1       cgd 		case SIGQUIT:
    366       1.1       cgd #ifdef DEBUG
    367       1.1       cgd 			if (debug)
    368       1.1       cgd 				break;
    369       1.1       cgd #endif
    370       1.1       cgd 			/* FALLTHROUGH */
    371       1.1       cgd 		case SIGTERM:
    372       1.1       cgd 			if (iflag)
    373       1.1       cgd 				action = S_IGN;
    374       1.1       cgd 			break;
    375       1.1       cgd #if JOBS
    376       1.1       cgd 		case SIGTSTP:
    377       1.1       cgd 		case SIGTTOU:
    378       1.6       jtc 			if (mflag)
    379       1.1       cgd 				action = S_IGN;
    380       1.1       cgd 			break;
    381       1.1       cgd #endif
    382       1.1       cgd 		}
    383       1.1       cgd 	}
    384      1.14  christos 
    385       1.6       jtc 	t = &sigmode[signo - 1];
    386      1.27  christos 	tsig = *t;
    387      1.27  christos 	if (tsig == 0) {
    388      1.16  christos 		/*
    389      1.16  christos 		 * current setting unknown
    390       1.1       cgd 		 */
    391      1.15  christos 		if (!getsigaction(signo, &sigact)) {
    392      1.15  christos 			/*
    393      1.15  christos 			 * Pretend it worked; maybe we should give a warning
    394      1.15  christos 			 * here, but other shells don't. We don't alter
    395      1.15  christos 			 * sigmode, so that we retry every time.
    396      1.15  christos 			 */
    397      1.15  christos 			return 0;
    398      1.15  christos 		}
    399       1.1       cgd 		if (sigact == SIG_IGN) {
    400      1.32  christos 			/*
    401      1.33  christos 			 * POSIX 3.14.13 states that non-interactive shells
    402      1.33  christos 			 * should ignore trap commands for signals that were
    403      1.33  christos 			 * ignored upon entry, and leaves the behavior
    404      1.33  christos 			 * unspecified for interactive shells. On interactive
    405      1.33  christos 			 * shells, or if job control is on, and we have a job
    406      1.33  christos 			 * control related signal, we allow the trap to work.
    407      1.33  christos 			 *
    408      1.33  christos 			 * This change allows us to be POSIX compliant, and
    409      1.33  christos 			 * at the same time override the default behavior if
    410      1.33  christos 			 * we need to by setting the interactive flag.
    411      1.32  christos 			 */
    412      1.33  christos 			if ((mflag && (signo == SIGTSTP ||
    413      1.33  christos 			     signo == SIGTTIN || signo == SIGTTOU)) || iflag) {
    414      1.33  christos 				tsig = S_IGN;
    415      1.33  christos 			} else
    416      1.33  christos 				tsig = S_HARD_IGN;
    417       1.1       cgd 		} else {
    418      1.27  christos 			tsig = S_RESET;	/* force to be set */
    419       1.1       cgd 		}
    420       1.1       cgd 	}
    421      1.27  christos 	if (tsig == S_HARD_IGN || tsig == action)
    422       1.1       cgd 		return 0;
    423       1.1       cgd 	switch (action) {
    424       1.1       cgd 		case S_DFL:	sigact = SIG_DFL;	break;
    425       1.1       cgd 		case S_CATCH:  	sigact = onsig;		break;
    426       1.1       cgd 		case S_IGN:	sigact = SIG_IGN;	break;
    427       1.1       cgd 	}
    428      1.32  christos 	sig = signal(signo, sigact);
    429      1.32  christos 	if (sig != SIG_ERR) {
    430      1.32  christos 		sigset_t ss;
    431      1.32  christos 		if (!vforked)
    432      1.32  christos 			*t = action;
    433      1.32  christos 		if (action == S_CATCH)
    434      1.32  christos 			(void)siginterrupt(signo, 1);
    435      1.32  christos 		/*
    436      1.32  christos 		 * If our parent accidentally blocked signals for
    437      1.32  christos 		 * us make sure we unblock them
    438      1.32  christos 		 */
    439      1.32  christos 		(void)sigemptyset(&ss);
    440      1.32  christos 		(void)sigaddset(&ss, signo);
    441      1.32  christos 		(void)sigprocmask(SIG_UNBLOCK, &ss, NULL);
    442      1.32  christos 	}
    443      1.32  christos 	return sig;
    444       1.1       cgd }
    445       1.1       cgd 
    446       1.6       jtc /*
    447       1.6       jtc  * Return the current setting for sig w/o changing it.
    448       1.6       jtc  */
    449      1.15  christos static int
    450      1.28  christos getsigaction(int signo, sig_t *sigact)
    451      1.10       cgd {
    452       1.6       jtc 	struct sigaction sa;
    453       1.6       jtc 
    454       1.6       jtc 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
    455      1.15  christos 		return 0;
    456      1.15  christos 	*sigact = (sig_t) sa.sa_handler;
    457      1.15  christos 	return 1;
    458       1.6       jtc }
    459       1.1       cgd 
    460       1.1       cgd /*
    461       1.1       cgd  * Ignore a signal.
    462       1.1       cgd  */
    463       1.1       cgd 
    464       1.1       cgd void
    465      1.28  christos ignoresig(int signo, int vforked)
    466      1.10       cgd {
    467       1.6       jtc 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
    468       1.1       cgd 		signal(signo, SIG_IGN);
    469       1.1       cgd 	}
    470      1.27  christos 	if (!vforked)
    471      1.27  christos 		sigmode[signo - 1] = S_HARD_IGN;
    472       1.1       cgd }
    473       1.1       cgd 
    474       1.1       cgd 
    475       1.1       cgd #ifdef mkinit
    476       1.8       jtc INCLUDE <signal.h>
    477       1.1       cgd INCLUDE "trap.h"
    478       1.1       cgd 
    479       1.1       cgd SHELLPROC {
    480       1.1       cgd 	char *sm;
    481       1.1       cgd 
    482      1.27  christos 	clear_traps(0);
    483       1.8       jtc 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
    484       1.1       cgd 		if (*sm == S_IGN)
    485       1.1       cgd 			*sm = S_HARD_IGN;
    486       1.1       cgd 	}
    487       1.1       cgd }
    488       1.1       cgd #endif
    489       1.1       cgd 
    490       1.1       cgd 
    491       1.1       cgd 
    492       1.1       cgd /*
    493       1.1       cgd  * Signal handler.
    494       1.1       cgd  */
    495       1.1       cgd 
    496       1.1       cgd void
    497      1.28  christos onsig(int signo)
    498      1.10       cgd {
    499      1.41       kre 	CTRACE(DBG_SIG, ("Signal %d, had: pending %d, gotsig[%d]=%d\n",
    500      1.41       kre 	    signo, pendingsigs, signo, gotsig[signo]));
    501      1.41       kre 
    502       1.1       cgd 	signal(signo, onsig);
    503       1.1       cgd 	if (signo == SIGINT && trap[SIGINT] == NULL) {
    504       1.1       cgd 		onint();
    505       1.1       cgd 		return;
    506       1.1       cgd 	}
    507      1.40       kre 	gotsig[signo] = 1;
    508       1.1       cgd 	pendingsigs++;
    509       1.1       cgd }
    510       1.1       cgd 
    511       1.1       cgd 
    512       1.1       cgd 
    513       1.1       cgd /*
    514       1.1       cgd  * Called to execute a trap.  Perhaps we should avoid entering new trap
    515       1.1       cgd  * handlers while we are executing a trap handler.
    516       1.1       cgd  */
    517       1.1       cgd 
    518       1.1       cgd void
    519      1.28  christos dotrap(void)
    520      1.28  christos {
    521       1.1       cgd 	int i;
    522       1.1       cgd 	int savestatus;
    523      1.38       kre 	char *tr;
    524       1.1       cgd 
    525       1.1       cgd 	for (;;) {
    526       1.1       cgd 		for (i = 1 ; ; i++) {
    527      1.40       kre 			if (i >= NSIG) {
    528      1.40       kre 				pendingsigs = 0;
    529      1.40       kre 				return;
    530      1.40       kre 			}
    531      1.40       kre 			if (gotsig[i])
    532       1.6       jtc 				break;
    533       1.1       cgd 		}
    534      1.40       kre 		gotsig[i] = 0;
    535       1.1       cgd 		savestatus=exitstatus;
    536      1.41       kre 		CTRACE(DBG_TRAP|DBG_SIG, ("dotrap %d: \"%s\"\n", i,
    537      1.41       kre 		    trap[i] ? trap[i] : "-NULL-"));
    538  1.41.4.1  pgoyette 		if ((tr = trap[i]) != NULL) {
    539  1.41.4.1  pgoyette 			tr = savestr(tr);	/* trap code may free trap[i] */
    540  1.41.4.1  pgoyette 			evalstring(tr, 0);
    541  1.41.4.1  pgoyette 			ckfree(tr);
    542  1.41.4.1  pgoyette 		}
    543       1.1       cgd 		exitstatus=savestatus;
    544       1.1       cgd 	}
    545       1.1       cgd }
    546       1.1       cgd 
    547      1.37  christos int
    548      1.37  christos lastsig(void)
    549      1.37  christos {
    550      1.37  christos 	int i;
    551       1.1       cgd 
    552      1.40       kre 	for (i = NSIG; --i > 0; )
    553      1.40       kre 		if (gotsig[i])
    554      1.37  christos 			return i;
    555      1.37  christos 	return SIGINT;	/* XXX */
    556      1.37  christos }
    557       1.1       cgd 
    558       1.1       cgd /*
    559       1.1       cgd  * Controls whether the shell is interactive or not.
    560       1.1       cgd  */
    561       1.1       cgd 
    562       1.1       cgd 
    563       1.1       cgd void
    564      1.28  christos setinteractive(int on)
    565      1.10       cgd {
    566       1.6       jtc 	static int is_interactive;
    567       1.6       jtc 
    568       1.1       cgd 	if (on == is_interactive)
    569       1.1       cgd 		return;
    570      1.27  christos 	setsignal(SIGINT, 0);
    571      1.27  christos 	setsignal(SIGQUIT, 0);
    572      1.27  christos 	setsignal(SIGTERM, 0);
    573       1.1       cgd 	is_interactive = on;
    574       1.1       cgd }
    575       1.1       cgd 
    576       1.1       cgd 
    577       1.1       cgd 
    578       1.1       cgd /*
    579       1.1       cgd  * Called to exit the shell.
    580       1.1       cgd  */
    581       1.1       cgd 
    582       1.1       cgd void
    583      1.28  christos exitshell(int status)
    584      1.10       cgd {
    585       1.1       cgd 	struct jmploc loc1, loc2;
    586       1.1       cgd 	char *p;
    587       1.1       cgd 
    588      1.41       kre 	CTRACE(DBG_ERRS|DBG_PROCS|DBG_CMDS|DBG_TRAP,
    589      1.41       kre 	    ("pid %d, exitshell(%d)\n", getpid(), status));
    590      1.41       kre 
    591       1.6       jtc 	if (setjmp(loc1.loc)) {
    592       1.6       jtc 		goto l1;
    593       1.6       jtc 	}
    594       1.6       jtc 	if (setjmp(loc2.loc)) {
    595       1.6       jtc 		goto l2;
    596       1.6       jtc 	}
    597       1.1       cgd 	handler = &loc1;
    598       1.1       cgd 	if ((p = trap[0]) != NULL && *p != '\0') {
    599       1.1       cgd 		trap[0] = NULL;
    600      1.41       kre 		VTRACE(DBG_TRAP, ("exit trap: \"%s\"\n", p));
    601      1.22  christos 		evalstring(p, 0);
    602       1.1       cgd 	}
    603      1.41       kre  l1:	handler = &loc2;			/* probably unnecessary */
    604       1.1       cgd 	flushall();
    605       1.1       cgd #if JOBS
    606       1.1       cgd 	setjobctl(0);
    607       1.1       cgd #endif
    608      1.41       kre  l2:	_exit(status);
    609      1.18   mycroft 	/* NOTREACHED */
    610       1.1       cgd }
    611