14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <signal.h> 42aa9caaf6SPeter Wemm #include <unistd.h> 43aa9caaf6SPeter Wemm #include <stdlib.h> 44aa9caaf6SPeter Wemm 454b88c807SRodney W. Grimes #include "shell.h" 464b88c807SRodney W. Grimes #include "main.h" 474b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 484b88c807SRodney W. Grimes #include "eval.h" 494b88c807SRodney W. Grimes #include "jobs.h" 50aa9caaf6SPeter Wemm #include "show.h" 514b88c807SRodney W. Grimes #include "options.h" 524b88c807SRodney W. Grimes #include "syntax.h" 534b88c807SRodney W. Grimes #include "output.h" 544b88c807SRodney W. Grimes #include "memalloc.h" 554b88c807SRodney W. Grimes #include "error.h" 564b88c807SRodney W. Grimes #include "trap.h" 574b88c807SRodney W. Grimes #include "mystring.h" 589de7305eSTim J. Robbins #include "myhistedit.h" 594b88c807SRodney W. Grimes 604b88c807SRodney W. Grimes 614b88c807SRodney W. Grimes /* 624b88c807SRodney W. Grimes * Sigmode records the current value of the signal handlers for the various 634b88c807SRodney W. Grimes * modes. A value of zero means that the current handler is not known. 644b88c807SRodney W. Grimes * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 654b88c807SRodney W. Grimes */ 664b88c807SRodney W. Grimes 674b88c807SRodney W. Grimes #define S_DFL 1 /* default signal handling (SIG_DFL) */ 684b88c807SRodney W. Grimes #define S_CATCH 2 /* signal is caught */ 694b88c807SRodney W. Grimes #define S_IGN 3 /* signal is ignored (SIG_IGN) */ 7046be34b9SKris Kennaway #define S_HARD_IGN 4 /* signal is ignored permanently */ 714b88c807SRodney W. Grimes #define S_RESET 5 /* temporary - to reset a hard ignored sig */ 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes 74aa9caaf6SPeter Wemm MKINIT char sigmode[NSIG]; /* current value of signal */ 754b88c807SRodney W. Grimes int pendingsigs; /* indicates some signal received */ 761f40b47bSMartin Cracauer int in_dotrap; /* do we execute in a trap handler? */ 77135421ddSMartin Cracauer static char *volatile trap[NSIG]; /* trap handler commands */ 787a8e920bSMartin Cracauer static volatile sig_atomic_t gotsig[NSIG]; 797a8e920bSMartin Cracauer /* indicates specified signal received */ 80f98e1b80SSteve Price static int ignore_sigchld; /* Used while handling SIGCHLD traps. */ 81be58cc48STim J. Robbins volatile sig_atomic_t gotwinch; 82ebdfd6dcSJilles Tjoelker static int last_trapsig; 834b88c807SRodney W. Grimes 8470df11eaSJilles Tjoelker static int exiting; /* exitshell() has been called */ 8570df11eaSJilles Tjoelker static int exiting_exitstatus; /* value passed to exitshell() */ 8670df11eaSJilles Tjoelker 8788328642SDavid E. O'Brien static int getsigaction(int, sig_t *); 88aa9caaf6SPeter Wemm 89f98e1b80SSteve Price 90f98e1b80SSteve Price /* 91f98e1b80SSteve Price * Map a string to a signal number. 9297054a76SNate Lawson * 9397054a76SNate Lawson * Note: the signal number may exceed NSIG. 94f98e1b80SSteve Price */ 9588328642SDavid E. O'Brien static int 965134c3f7SWarner Losh sigstring_to_signum(char *sig) 97f98e1b80SSteve Price { 98f98e1b80SSteve Price 99f98e1b80SSteve Price if (is_number(sig)) { 100f98e1b80SSteve Price int signo; 101f98e1b80SSteve Price 102f98e1b80SSteve Price signo = atoi(sig); 103f98e1b80SSteve Price return ((signo >= 0 && signo < NSIG) ? signo : (-1)); 104*12dacf62SJilles Tjoelker } else if (strcasecmp(sig, "EXIT") == 0) { 105f98e1b80SSteve Price return (0); 106f98e1b80SSteve Price } else { 107f98e1b80SSteve Price int n; 108f98e1b80SSteve Price 109*12dacf62SJilles Tjoelker if (strncasecmp(sig, "SIG", 3) == 0) 110f98e1b80SSteve Price sig += 3; 1111b13752aSNate Lawson for (n = 1; n < sys_nsig; n++) 112aa0b7126SNate Lawson if (sys_signame[n] && 113aa0b7126SNate Lawson strcasecmp(sys_signame[n], sig) == 0) 114f98e1b80SSteve Price return (n); 115f98e1b80SSteve Price } 116f98e1b80SSteve Price return (-1); 117f98e1b80SSteve Price } 118f98e1b80SSteve Price 119f98e1b80SSteve Price 120f98e1b80SSteve Price /* 121f98e1b80SSteve Price * Print a list of valid signal names. 122f98e1b80SSteve Price */ 12388328642SDavid E. O'Brien static void 1245134c3f7SWarner Losh printsignals(void) 125f98e1b80SSteve Price { 12697054a76SNate Lawson int n, outlen; 127f98e1b80SSteve Price 12897054a76SNate Lawson outlen = 0; 1291b13752aSNate Lawson for (n = 1; n < sys_nsig; n++) { 13097054a76SNate Lawson if (sys_signame[n]) { 131f98e1b80SSteve Price out1fmt("%s", sys_signame[n]); 13297054a76SNate Lawson outlen += strlen(sys_signame[n]); 13397054a76SNate Lawson } else { 13497054a76SNate Lawson out1fmt("%d", n); 13597054a76SNate Lawson outlen += 3; /* good enough */ 13697054a76SNate Lawson } 13797054a76SNate Lawson ++outlen; 13845b3c176SJilles Tjoelker if (outlen > 71 || n == sys_nsig - 1) { 139f98e1b80SSteve Price out1str("\n"); 14097054a76SNate Lawson outlen = 0; 14197054a76SNate Lawson } else { 142f98e1b80SSteve Price out1c(' '); 143f98e1b80SSteve Price } 144f98e1b80SSteve Price } 14597054a76SNate Lawson } 146f98e1b80SSteve Price 147f98e1b80SSteve Price 1484b88c807SRodney W. Grimes /* 1494b88c807SRodney W. Grimes * The trap builtin. 1504b88c807SRodney W. Grimes */ 151aa9caaf6SPeter Wemm int 1525134c3f7SWarner Losh trapcmd(int argc, char **argv) 153aa9caaf6SPeter Wemm { 1544b88c807SRodney W. Grimes char *action; 1554b88c807SRodney W. Grimes int signo; 156e3c2cd72SJilles Tjoelker int errors = 0; 157a043cc4cSJilles Tjoelker int i; 1584b88c807SRodney W. Grimes 159a043cc4cSJilles Tjoelker while ((i = nextopt("l")) != '\0') { 160a043cc4cSJilles Tjoelker switch (i) { 161a043cc4cSJilles Tjoelker case 'l': 162a043cc4cSJilles Tjoelker printsignals(); 163a043cc4cSJilles Tjoelker return (0); 164a043cc4cSJilles Tjoelker } 165a043cc4cSJilles Tjoelker } 166a043cc4cSJilles Tjoelker argv = argptr; 167a043cc4cSJilles Tjoelker 168a043cc4cSJilles Tjoelker if (*argv == NULL) { 1691b13752aSNate Lawson for (signo = 0 ; signo < sys_nsig ; signo++) { 17097054a76SNate Lawson if (signo < NSIG && trap[signo] != NULL) { 1712a5e306dSStefan Farfeleder out1str("trap -- "); 1722a5e306dSStefan Farfeleder out1qstr(trap[signo]); 17397054a76SNate Lawson if (signo == 0) { 174*12dacf62SJilles Tjoelker out1str(" EXIT\n"); 17597054a76SNate Lawson } else if (sys_signame[signo]) { 1762a5e306dSStefan Farfeleder out1fmt(" %s\n", sys_signame[signo]); 17797054a76SNate Lawson } else { 1782a5e306dSStefan Farfeleder out1fmt(" %d\n", signo); 17997054a76SNate Lawson } 18097054a76SNate Lawson } 1814b88c807SRodney W. Grimes } 1824b88c807SRodney W. Grimes return 0; 1834b88c807SRodney W. Grimes } 1844b88c807SRodney W. Grimes action = NULL; 185f98e1b80SSteve Price if (*argv && sigstring_to_signum(*argv) == -1) { 186a043cc4cSJilles Tjoelker if (strcmp(*argv, "-") == 0) 187a043cc4cSJilles Tjoelker argv++; 188a043cc4cSJilles Tjoelker else { 189f98e1b80SSteve Price action = *argv; 190f98e1b80SSteve Price argv++; 191f98e1b80SSteve Price } 192f98e1b80SSteve Price } 193f98e1b80SSteve Price while (*argv) { 194e3c2cd72SJilles Tjoelker if ((signo = sigstring_to_signum(*argv)) == -1) { 1955fe9123fSJilles Tjoelker warning("bad signal %s", *argv); 196e3c2cd72SJilles Tjoelker errors = 1; 197e3c2cd72SJilles Tjoelker } 1984b88c807SRodney W. Grimes INTOFF; 1994b88c807SRodney W. Grimes if (action) 2004b88c807SRodney W. Grimes action = savestr(action); 2014b88c807SRodney W. Grimes if (trap[signo]) 2024b88c807SRodney W. Grimes ckfree(trap[signo]); 2034b88c807SRodney W. Grimes trap[signo] = action; 2044b88c807SRodney W. Grimes if (signo != 0) 2054b88c807SRodney W. Grimes setsignal(signo); 2064b88c807SRodney W. Grimes INTON; 207f98e1b80SSteve Price argv++; 2084b88c807SRodney W. Grimes } 209e3c2cd72SJilles Tjoelker return errors; 2104b88c807SRodney W. Grimes } 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes 2134b88c807SRodney W. Grimes /* 2144b88c807SRodney W. Grimes * Clear traps on a fork. 2154b88c807SRodney W. Grimes */ 2164b88c807SRodney W. Grimes void 2175134c3f7SWarner Losh clear_traps(void) 218f98e1b80SSteve Price { 219135421ddSMartin Cracauer char *volatile *tp; 2204b88c807SRodney W. Grimes 221f98e1b80SSteve Price for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 2224b88c807SRodney W. Grimes if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 2234b88c807SRodney W. Grimes INTOFF; 2244b88c807SRodney W. Grimes ckfree(*tp); 2254b88c807SRodney W. Grimes *tp = NULL; 2264b88c807SRodney W. Grimes if (tp != &trap[0]) 2274b88c807SRodney W. Grimes setsignal(tp - trap); 2284b88c807SRodney W. Grimes INTON; 2294b88c807SRodney W. Grimes } 2304b88c807SRodney W. Grimes } 2314b88c807SRodney W. Grimes } 2324b88c807SRodney W. Grimes 2334b88c807SRodney W. Grimes 2344b88c807SRodney W. Grimes /* 2356e28dacfSJilles Tjoelker * Check if we have any traps enabled. 2366e28dacfSJilles Tjoelker */ 2376e28dacfSJilles Tjoelker int 2386e28dacfSJilles Tjoelker have_traps(void) 2396e28dacfSJilles Tjoelker { 2406e28dacfSJilles Tjoelker char *volatile *tp; 2416e28dacfSJilles Tjoelker 2426e28dacfSJilles Tjoelker for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 2436e28dacfSJilles Tjoelker if (*tp && **tp) /* trap not NULL or SIG_IGN */ 2446e28dacfSJilles Tjoelker return 1; 2456e28dacfSJilles Tjoelker } 2466e28dacfSJilles Tjoelker return 0; 2476e28dacfSJilles Tjoelker } 2486e28dacfSJilles Tjoelker 2496e28dacfSJilles Tjoelker /* 2504b88c807SRodney W. Grimes * Set the signal handler for the specified signal. The routine figures 2514b88c807SRodney W. Grimes * out what it should be set to. 2524b88c807SRodney W. Grimes */ 25328701136SBruce Evans void 2545134c3f7SWarner Losh setsignal(int signo) 255aa9caaf6SPeter Wemm { 2564b88c807SRodney W. Grimes int action; 2573f228d74SJilles Tjoelker sig_t sigact = SIG_DFL; 2583f228d74SJilles Tjoelker struct sigaction sa; 2594b88c807SRodney W. Grimes char *t; 2604b88c807SRodney W. Grimes 2614b88c807SRodney W. Grimes if ((t = trap[signo]) == NULL) 2624b88c807SRodney W. Grimes action = S_DFL; 2634b88c807SRodney W. Grimes else if (*t != '\0') 2644b88c807SRodney W. Grimes action = S_CATCH; 2654b88c807SRodney W. Grimes else 2664b88c807SRodney W. Grimes action = S_IGN; 2677a8e920bSMartin Cracauer if (action == S_DFL) { 2684b88c807SRodney W. Grimes switch (signo) { 2694b88c807SRodney W. Grimes case SIGINT: 2704b88c807SRodney W. Grimes action = S_CATCH; 2714b88c807SRodney W. Grimes break; 2724b88c807SRodney W. Grimes case SIGQUIT: 2734b88c807SRodney W. Grimes #ifdef DEBUG 2744b88c807SRodney W. Grimes { 2754b88c807SRodney W. Grimes extern int debug; 2764b88c807SRodney W. Grimes 2774b88c807SRodney W. Grimes if (debug) 2784b88c807SRodney W. Grimes break; 2794b88c807SRodney W. Grimes } 2804b88c807SRodney W. Grimes #endif 28140d009a7SMartin Cracauer action = S_CATCH; 2827a8e920bSMartin Cracauer break; 2834b88c807SRodney W. Grimes case SIGTERM: 2847a8e920bSMartin Cracauer if (rootshell && iflag) 2854b88c807SRodney W. Grimes action = S_IGN; 2864b88c807SRodney W. Grimes break; 2874b88c807SRodney W. Grimes #if JOBS 2884b88c807SRodney W. Grimes case SIGTSTP: 2894b88c807SRodney W. Grimes case SIGTTOU: 2907a8e920bSMartin Cracauer if (rootshell && mflag) 2914b88c807SRodney W. Grimes action = S_IGN; 2924b88c807SRodney W. Grimes break; 2934b88c807SRodney W. Grimes #endif 2949de7305eSTim J. Robbins #ifndef NO_HISTORY 2959de7305eSTim J. Robbins case SIGWINCH: 296be58cc48STim J. Robbins if (rootshell && iflag) 2979de7305eSTim J. Robbins action = S_CATCH; 2989de7305eSTim J. Robbins break; 2999de7305eSTim J. Robbins #endif 3004b88c807SRodney W. Grimes } 3014b88c807SRodney W. Grimes } 302aa9caaf6SPeter Wemm 303f98e1b80SSteve Price t = &sigmode[signo]; 3044b88c807SRodney W. Grimes if (*t == 0) { 3054b88c807SRodney W. Grimes /* 3064b88c807SRodney W. Grimes * current setting unknown 3074b88c807SRodney W. Grimes */ 308aa9caaf6SPeter Wemm if (!getsigaction(signo, &sigact)) { 309aa9caaf6SPeter Wemm /* 310aa9caaf6SPeter Wemm * Pretend it worked; maybe we should give a warning 311aa9caaf6SPeter Wemm * here, but other shells don't. We don't alter 312aa9caaf6SPeter Wemm * sigmode, so that we retry every time. 313aa9caaf6SPeter Wemm */ 31428701136SBruce Evans return; 315aa9caaf6SPeter Wemm } 3164b88c807SRodney W. Grimes if (sigact == SIG_IGN) { 3174b88c807SRodney W. Grimes if (mflag && (signo == SIGTSTP || 3184b88c807SRodney W. Grimes signo == SIGTTIN || signo == SIGTTOU)) { 3194b88c807SRodney W. Grimes *t = S_IGN; /* don't hard ignore these */ 3204b88c807SRodney W. Grimes } else 3214b88c807SRodney W. Grimes *t = S_HARD_IGN; 3224b88c807SRodney W. Grimes } else { 3234b88c807SRodney W. Grimes *t = S_RESET; /* force to be set */ 3244b88c807SRodney W. Grimes } 3254b88c807SRodney W. Grimes } 3264b88c807SRodney W. Grimes if (*t == S_HARD_IGN || *t == action) 32728701136SBruce Evans return; 3284b88c807SRodney W. Grimes switch (action) { 3294b88c807SRodney W. Grimes case S_DFL: sigact = SIG_DFL; break; 3304b88c807SRodney W. Grimes case S_CATCH: sigact = onsig; break; 3314b88c807SRodney W. Grimes case S_IGN: sigact = SIG_IGN; break; 3324b88c807SRodney W. Grimes } 3334b88c807SRodney W. Grimes *t = action; 3343f228d74SJilles Tjoelker sa.sa_handler = sigact; 3353f228d74SJilles Tjoelker sa.sa_flags = 0; 3363f228d74SJilles Tjoelker sigemptyset(&sa.sa_mask); 3373f228d74SJilles Tjoelker sigaction(signo, &sa, NULL); 3384b88c807SRodney W. Grimes } 3394b88c807SRodney W. Grimes 340f98e1b80SSteve Price 3414b88c807SRodney W. Grimes /* 3424b88c807SRodney W. Grimes * Return the current setting for sig w/o changing it. 3434b88c807SRodney W. Grimes */ 34488328642SDavid E. O'Brien static int 3455134c3f7SWarner Losh getsigaction(int signo, sig_t *sigact) 346aa9caaf6SPeter Wemm { 3474b88c807SRodney W. Grimes struct sigaction sa; 3484b88c807SRodney W. Grimes 3494b88c807SRodney W. Grimes if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 350aa9caaf6SPeter Wemm return 0; 351aa9caaf6SPeter Wemm *sigact = (sig_t) sa.sa_handler; 352aa9caaf6SPeter Wemm return 1; 3534b88c807SRodney W. Grimes } 3544b88c807SRodney W. Grimes 355f98e1b80SSteve Price 3564b88c807SRodney W. Grimes /* 3574b88c807SRodney W. Grimes * Ignore a signal. 3584b88c807SRodney W. Grimes */ 3594b88c807SRodney W. Grimes void 3605134c3f7SWarner Losh ignoresig(int signo) 361aa9caaf6SPeter Wemm { 362f98e1b80SSteve Price 363f98e1b80SSteve Price if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { 3644b88c807SRodney W. Grimes signal(signo, SIG_IGN); 3654b88c807SRodney W. Grimes } 366f98e1b80SSteve Price sigmode[signo] = S_HARD_IGN; 3674b88c807SRodney W. Grimes } 3684b88c807SRodney W. Grimes 3694b88c807SRodney W. Grimes 3704b88c807SRodney W. Grimes #ifdef mkinit 371aa9caaf6SPeter Wemm INCLUDE <signal.h> 3724b88c807SRodney W. Grimes INCLUDE "trap.h" 3734b88c807SRodney W. Grimes 3744b88c807SRodney W. Grimes SHELLPROC { 3754b88c807SRodney W. Grimes char *sm; 3764b88c807SRodney W. Grimes 3774b88c807SRodney W. Grimes clear_traps(); 378aa9caaf6SPeter Wemm for (sm = sigmode ; sm < sigmode + NSIG ; sm++) { 3794b88c807SRodney W. Grimes if (*sm == S_IGN) 3804b88c807SRodney W. Grimes *sm = S_HARD_IGN; 3814b88c807SRodney W. Grimes } 3824b88c807SRodney W. Grimes } 3834b88c807SRodney W. Grimes #endif 3844b88c807SRodney W. Grimes 3854b88c807SRodney W. Grimes 3864b88c807SRodney W. Grimes /* 3874b88c807SRodney W. Grimes * Signal handler. 3884b88c807SRodney W. Grimes */ 3894b88c807SRodney W. Grimes void 3905134c3f7SWarner Losh onsig(int signo) 391aa9caaf6SPeter Wemm { 39228701136SBruce Evans 3934b88c807SRodney W. Grimes if (signo == SIGINT && trap[SIGINT] == NULL) { 3944b88c807SRodney W. Grimes onint(); 3954b88c807SRodney W. Grimes return; 3964b88c807SRodney W. Grimes } 397135421ddSMartin Cracauer 398f98e1b80SSteve Price if (signo != SIGCHLD || !ignore_sigchld) 399f98e1b80SSteve Price gotsig[signo] = 1; 4004b88c807SRodney W. Grimes pendingsigs++; 401135421ddSMartin Cracauer 402135421ddSMartin Cracauer /* If we are currently in a wait builtin, prepare to break it */ 403135421ddSMartin Cracauer if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0) 4047a8e920bSMartin Cracauer breakwaitcmd = 1; 405135421ddSMartin Cracauer /* 4069bbfa415SMartin Cracauer * If a trap is set, not ignored and not the null command, we need 4079bbfa415SMartin Cracauer * to make sure traps are executed even when a child blocks signals. 408135421ddSMartin Cracauer */ 409c1c72a3cSMartin Cracauer if (Tflag && 410c1c72a3cSMartin Cracauer trap[signo] != NULL && 411f7d95a07SRalf S. Engelschall ! (trap[signo][0] == '\0') && 412e6ac45ddSMartin Cracauer ! (trap[signo][0] == ':' && trap[signo][1] == '\0')) 413135421ddSMartin Cracauer breakwaitcmd = 1; 4149de7305eSTim J. Robbins 4159de7305eSTim J. Robbins #ifndef NO_HISTORY 4169de7305eSTim J. Robbins if (signo == SIGWINCH) 417be58cc48STim J. Robbins gotwinch = 1; 4189de7305eSTim J. Robbins #endif 4194b88c807SRodney W. Grimes } 4204b88c807SRodney W. Grimes 4214b88c807SRodney W. Grimes 4224b88c807SRodney W. Grimes /* 4234b88c807SRodney W. Grimes * Called to execute a trap. Perhaps we should avoid entering new trap 4244b88c807SRodney W. Grimes * handlers while we are executing a trap handler. 4254b88c807SRodney W. Grimes */ 4264b88c807SRodney W. Grimes void 4275134c3f7SWarner Losh dotrap(void) 428f98e1b80SSteve Price { 4294b88c807SRodney W. Grimes int i; 4304b88c807SRodney W. Grimes int savestatus; 4314b88c807SRodney W. Grimes 4327a8e920bSMartin Cracauer in_dotrap++; 4334b88c807SRodney W. Grimes for (;;) { 434f98e1b80SSteve Price for (i = 1; i < NSIG; i++) { 435f98e1b80SSteve Price if (gotsig[i]) { 436f98e1b80SSteve Price gotsig[i] = 0; 437f98e1b80SSteve Price if (trap[i]) { 438f98e1b80SSteve Price /* 439aa0b7126SNate Lawson * Ignore SIGCHLD to avoid infinite 440aa0b7126SNate Lawson * recursion if the trap action does 441aa0b7126SNate Lawson * a fork. 442f98e1b80SSteve Price */ 443f98e1b80SSteve Price if (i == SIGCHLD) 444f98e1b80SSteve Price ignore_sigchld++; 445ebdfd6dcSJilles Tjoelker last_trapsig = i; 4464b88c807SRodney W. Grimes savestatus = exitstatus; 447cb806389SStefan Farfeleder evalstring(trap[i], 0); 4484b88c807SRodney W. Grimes exitstatus = savestatus; 449f98e1b80SSteve Price if (i == SIGCHLD) 450f98e1b80SSteve Price ignore_sigchld--; 4514b88c807SRodney W. Grimes } 452f98e1b80SSteve Price break; 453f98e1b80SSteve Price } 454f98e1b80SSteve Price } 455f98e1b80SSteve Price if (i >= NSIG) 456f98e1b80SSteve Price break; 457f98e1b80SSteve Price } 4587a8e920bSMartin Cracauer in_dotrap--; 4594b88c807SRodney W. Grimes pendingsigs = 0; 4604b88c807SRodney W. Grimes } 4614b88c807SRodney W. Grimes 4624b88c807SRodney W. Grimes 4634b88c807SRodney W. Grimes /* 4644b88c807SRodney W. Grimes * Controls whether the shell is interactive or not. 4654b88c807SRodney W. Grimes */ 4664b88c807SRodney W. Grimes void 4675134c3f7SWarner Losh setinteractive(int on) 468aa9caaf6SPeter Wemm { 4697a8e920bSMartin Cracauer static int is_interactive = -1; 4704b88c807SRodney W. Grimes 4714b88c807SRodney W. Grimes if (on == is_interactive) 4724b88c807SRodney W. Grimes return; 4734b88c807SRodney W. Grimes setsignal(SIGINT); 4744b88c807SRodney W. Grimes setsignal(SIGQUIT); 4754b88c807SRodney W. Grimes setsignal(SIGTERM); 4769de7305eSTim J. Robbins #ifndef NO_HISTORY 4779de7305eSTim J. Robbins setsignal(SIGWINCH); 4789de7305eSTim J. Robbins #endif 4794b88c807SRodney W. Grimes is_interactive = on; 4804b88c807SRodney W. Grimes } 4814b88c807SRodney W. Grimes 4824b88c807SRodney W. Grimes 4834b88c807SRodney W. Grimes /* 4844b88c807SRodney W. Grimes * Called to exit the shell. 4854b88c807SRodney W. Grimes */ 4864b88c807SRodney W. Grimes void 4875134c3f7SWarner Losh exitshell(int status) 488aa9caaf6SPeter Wemm { 48970df11eaSJilles Tjoelker TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 49070df11eaSJilles Tjoelker exiting = 1; 49170df11eaSJilles Tjoelker exiting_exitstatus = status; 49270df11eaSJilles Tjoelker exitshell_savedstatus(); 49370df11eaSJilles Tjoelker } 49470df11eaSJilles Tjoelker 49570df11eaSJilles Tjoelker void 49670df11eaSJilles Tjoelker exitshell_savedstatus(void) 49770df11eaSJilles Tjoelker { 4984b88c807SRodney W. Grimes struct jmploc loc1, loc2; 4994b88c807SRodney W. Grimes char *p; 500ebdfd6dcSJilles Tjoelker int sig = 0; 501ebdfd6dcSJilles Tjoelker sigset_t sigs; 5024b88c807SRodney W. Grimes 503ebdfd6dcSJilles Tjoelker if (!exiting) { 504ebdfd6dcSJilles Tjoelker if (in_dotrap && last_trapsig) { 505ebdfd6dcSJilles Tjoelker sig = last_trapsig; 506ebdfd6dcSJilles Tjoelker exiting_exitstatus = sig + 128; 507ebdfd6dcSJilles Tjoelker } else 50870df11eaSJilles Tjoelker exiting_exitstatus = oexitstatus; 509ebdfd6dcSJilles Tjoelker } 51070df11eaSJilles Tjoelker exitstatus = oexitstatus = exiting_exitstatus; 5114b88c807SRodney W. Grimes if (setjmp(loc1.loc)) { 5124b88c807SRodney W. Grimes goto l1; 5134b88c807SRodney W. Grimes } 5144b88c807SRodney W. Grimes if (setjmp(loc2.loc)) { 5154b88c807SRodney W. Grimes goto l2; 5164b88c807SRodney W. Grimes } 5174b88c807SRodney W. Grimes handler = &loc1; 5184b88c807SRodney W. Grimes if ((p = trap[0]) != NULL && *p != '\0') { 5194b88c807SRodney W. Grimes trap[0] = NULL; 520cb806389SStefan Farfeleder evalstring(p, 0); 5214b88c807SRodney W. Grimes } 5224b88c807SRodney W. Grimes l1: handler = &loc2; /* probably unnecessary */ 5234b88c807SRodney W. Grimes flushall(); 5244b88c807SRodney W. Grimes #if JOBS 5254b88c807SRodney W. Grimes setjobctl(0); 5264b88c807SRodney W. Grimes #endif 527ebdfd6dcSJilles Tjoelker l2: 528ebdfd6dcSJilles Tjoelker if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN && 529ebdfd6dcSJilles Tjoelker sig != SIGTTOU) { 530ebdfd6dcSJilles Tjoelker signal(sig, SIG_DFL); 531ebdfd6dcSJilles Tjoelker sigemptyset(&sigs); 532ebdfd6dcSJilles Tjoelker sigaddset(&sigs, sig); 533ebdfd6dcSJilles Tjoelker sigprocmask(SIG_UNBLOCK, &sigs, NULL); 534ebdfd6dcSJilles Tjoelker kill(getpid(), sig); 535ebdfd6dcSJilles Tjoelker /* If the default action is to ignore, fall back to _exit(). */ 536ebdfd6dcSJilles Tjoelker } 537ebdfd6dcSJilles Tjoelker _exit(exiting_exitstatus); 5384b88c807SRodney W. Grimes } 539