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; 824b88c807SRodney W. Grimes 835134c3f7SWarner Losh static int getsigaction(int, sig_t *); 84aa9caaf6SPeter Wemm 85f98e1b80SSteve Price 86f98e1b80SSteve Price /* 87f98e1b80SSteve Price * Map a string to a signal number. 8897054a76SNate Lawson * 8997054a76SNate Lawson * Note: the signal number may exceed NSIG. 90f98e1b80SSteve Price */ 91f98e1b80SSteve Price static int 925134c3f7SWarner Losh sigstring_to_signum(char *sig) 93f98e1b80SSteve Price { 94f98e1b80SSteve Price 95f98e1b80SSteve Price if (is_number(sig)) { 96f98e1b80SSteve Price int signo; 97f98e1b80SSteve Price 98f98e1b80SSteve Price signo = atoi(sig); 99f98e1b80SSteve Price return ((signo >= 0 && signo < NSIG) ? signo : (-1)); 100f98e1b80SSteve Price } else if (strcasecmp(sig, "exit") == 0) { 101f98e1b80SSteve Price return (0); 102f98e1b80SSteve Price } else { 103f98e1b80SSteve Price int n; 104f98e1b80SSteve Price 105f98e1b80SSteve Price if (strncasecmp(sig, "sig", 3) == 0) 106f98e1b80SSteve Price sig += 3; 1071b13752aSNate Lawson for (n = 1; n < sys_nsig; n++) 108aa0b7126SNate Lawson if (sys_signame[n] && 109aa0b7126SNate Lawson strcasecmp(sys_signame[n], sig) == 0) 110f98e1b80SSteve Price return (n); 111f98e1b80SSteve Price } 112f98e1b80SSteve Price return (-1); 113f98e1b80SSteve Price } 114f98e1b80SSteve Price 115f98e1b80SSteve Price 116f98e1b80SSteve Price /* 117f98e1b80SSteve Price * Print a list of valid signal names. 118f98e1b80SSteve Price */ 119f98e1b80SSteve Price static void 1205134c3f7SWarner Losh printsignals(void) 121f98e1b80SSteve Price { 12297054a76SNate Lawson int n, outlen; 123f98e1b80SSteve Price 12497054a76SNate Lawson outlen = 0; 1251b13752aSNate Lawson for (n = 1; n < sys_nsig; n++) { 12697054a76SNate Lawson if (sys_signame[n]) { 127f98e1b80SSteve Price out1fmt("%s", sys_signame[n]); 12897054a76SNate Lawson outlen += strlen(sys_signame[n]); 12997054a76SNate Lawson } else { 13097054a76SNate Lawson out1fmt("%d", n); 13197054a76SNate Lawson outlen += 3; /* good enough */ 13297054a76SNate Lawson } 13397054a76SNate Lawson ++outlen; 13497054a76SNate Lawson if (outlen > 70 || n == sys_nsig - 1) { 135f98e1b80SSteve Price out1str("\n"); 13697054a76SNate Lawson outlen = 0; 13797054a76SNate Lawson } else { 138f98e1b80SSteve Price out1c(' '); 139f98e1b80SSteve Price } 140f98e1b80SSteve Price } 14197054a76SNate Lawson } 142f98e1b80SSteve Price 143f98e1b80SSteve Price 1444b88c807SRodney W. Grimes /* 1454b88c807SRodney W. Grimes * The trap builtin. 1464b88c807SRodney W. Grimes */ 147aa9caaf6SPeter Wemm int 1485134c3f7SWarner Losh trapcmd(int argc, char **argv) 149aa9caaf6SPeter Wemm { 1504b88c807SRodney W. Grimes char *action; 1514b88c807SRodney W. Grimes int signo; 1524b88c807SRodney W. Grimes 1534b88c807SRodney W. Grimes if (argc <= 1) { 1541b13752aSNate Lawson for (signo = 0 ; signo < sys_nsig ; signo++) { 15597054a76SNate Lawson if (signo < NSIG && trap[signo] != NULL) { 1562a5e306dSStefan Farfeleder out1str("trap -- "); 1572a5e306dSStefan Farfeleder out1qstr(trap[signo]); 15897054a76SNate Lawson if (signo == 0) { 1592a5e306dSStefan Farfeleder out1str(" exit\n"); 16097054a76SNate Lawson } else if (sys_signame[signo]) { 1612a5e306dSStefan Farfeleder out1fmt(" %s\n", sys_signame[signo]); 16297054a76SNate Lawson } else { 1632a5e306dSStefan Farfeleder out1fmt(" %d\n", signo); 16497054a76SNate Lawson } 16597054a76SNate Lawson } 1664b88c807SRodney W. Grimes } 1674b88c807SRodney W. Grimes return 0; 1684b88c807SRodney W. Grimes } 1694b88c807SRodney W. Grimes action = NULL; 170f98e1b80SSteve Price if (*++argv && strcmp(*argv, "--") == 0) 171f98e1b80SSteve Price argv++; 172f98e1b80SSteve Price if (*argv && sigstring_to_signum(*argv) == -1) { 173f98e1b80SSteve Price if ((*argv)[0] != '-') { 174f98e1b80SSteve Price action = *argv; 175f98e1b80SSteve Price argv++; 176f98e1b80SSteve Price } else if ((*argv)[1] == '\0') { 177f98e1b80SSteve Price argv++; 178f98e1b80SSteve Price } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') { 179f98e1b80SSteve Price printsignals(); 180f98e1b80SSteve Price return 0; 181f98e1b80SSteve Price } else { 182f98e1b80SSteve Price error("bad option %s", *argv); 183f98e1b80SSteve Price } 184f98e1b80SSteve Price } 185f98e1b80SSteve Price while (*argv) { 186f98e1b80SSteve Price if ((signo = sigstring_to_signum(*argv)) == -1) 187f98e1b80SSteve Price error("bad signal %s", *argv); 1884b88c807SRodney W. Grimes INTOFF; 1894b88c807SRodney W. Grimes if (action) 1904b88c807SRodney W. Grimes action = savestr(action); 1914b88c807SRodney W. Grimes if (trap[signo]) 1924b88c807SRodney W. Grimes ckfree(trap[signo]); 1934b88c807SRodney W. Grimes trap[signo] = action; 1944b88c807SRodney W. Grimes if (signo != 0) 1954b88c807SRodney W. Grimes setsignal(signo); 1964b88c807SRodney W. Grimes INTON; 197f98e1b80SSteve Price argv++; 1984b88c807SRodney W. Grimes } 1994b88c807SRodney W. Grimes return 0; 2004b88c807SRodney W. Grimes } 2014b88c807SRodney W. Grimes 2024b88c807SRodney W. Grimes 2034b88c807SRodney W. Grimes /* 2044b88c807SRodney W. Grimes * Clear traps on a fork. 2054b88c807SRodney W. Grimes */ 2064b88c807SRodney W. Grimes void 2075134c3f7SWarner Losh clear_traps(void) 208f98e1b80SSteve Price { 209135421ddSMartin Cracauer char *volatile *tp; 2104b88c807SRodney W. Grimes 211f98e1b80SSteve Price for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 2124b88c807SRodney W. Grimes if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 2134b88c807SRodney W. Grimes INTOFF; 2144b88c807SRodney W. Grimes ckfree(*tp); 2154b88c807SRodney W. Grimes *tp = NULL; 2164b88c807SRodney W. Grimes if (tp != &trap[0]) 2174b88c807SRodney W. Grimes setsignal(tp - trap); 2184b88c807SRodney W. Grimes INTON; 2194b88c807SRodney W. Grimes } 2204b88c807SRodney W. Grimes } 2214b88c807SRodney W. Grimes } 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes 2244b88c807SRodney W. Grimes /* 2256e28dacfSJilles Tjoelker * Check if we have any traps enabled. 2266e28dacfSJilles Tjoelker */ 2276e28dacfSJilles Tjoelker int 2286e28dacfSJilles Tjoelker have_traps(void) 2296e28dacfSJilles Tjoelker { 2306e28dacfSJilles Tjoelker char *volatile *tp; 2316e28dacfSJilles Tjoelker 2326e28dacfSJilles Tjoelker for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 2336e28dacfSJilles Tjoelker if (*tp && **tp) /* trap not NULL or SIG_IGN */ 2346e28dacfSJilles Tjoelker return 1; 2356e28dacfSJilles Tjoelker } 2366e28dacfSJilles Tjoelker return 0; 2376e28dacfSJilles Tjoelker } 2386e28dacfSJilles Tjoelker 2396e28dacfSJilles Tjoelker /* 2404b88c807SRodney W. Grimes * Set the signal handler for the specified signal. The routine figures 2414b88c807SRodney W. Grimes * out what it should be set to. 2424b88c807SRodney W. Grimes */ 24328701136SBruce Evans void 2445134c3f7SWarner Losh setsignal(int signo) 245aa9caaf6SPeter Wemm { 2464b88c807SRodney W. Grimes int action; 24728701136SBruce Evans sig_t sig, sigact = SIG_DFL; 2484b88c807SRodney W. Grimes char *t; 2494b88c807SRodney W. Grimes 2504b88c807SRodney W. Grimes if ((t = trap[signo]) == NULL) 2514b88c807SRodney W. Grimes action = S_DFL; 2524b88c807SRodney W. Grimes else if (*t != '\0') 2534b88c807SRodney W. Grimes action = S_CATCH; 2544b88c807SRodney W. Grimes else 2554b88c807SRodney W. Grimes action = S_IGN; 2567a8e920bSMartin Cracauer if (action == S_DFL) { 2574b88c807SRodney W. Grimes switch (signo) { 2584b88c807SRodney W. Grimes case SIGINT: 2594b88c807SRodney W. Grimes action = S_CATCH; 2604b88c807SRodney W. Grimes break; 2614b88c807SRodney W. Grimes case SIGQUIT: 2624b88c807SRodney W. Grimes #ifdef DEBUG 2634b88c807SRodney W. Grimes { 2644b88c807SRodney W. Grimes extern int debug; 2654b88c807SRodney W. Grimes 2664b88c807SRodney W. Grimes if (debug) 2674b88c807SRodney W. Grimes break; 2684b88c807SRodney W. Grimes } 2694b88c807SRodney W. Grimes #endif 27040d009a7SMartin Cracauer action = S_CATCH; 2717a8e920bSMartin Cracauer break; 2724b88c807SRodney W. Grimes case SIGTERM: 2737a8e920bSMartin Cracauer if (rootshell && iflag) 2744b88c807SRodney W. Grimes action = S_IGN; 2754b88c807SRodney W. Grimes break; 2764b88c807SRodney W. Grimes #if JOBS 2774b88c807SRodney W. Grimes case SIGTSTP: 2784b88c807SRodney W. Grimes case SIGTTOU: 2797a8e920bSMartin Cracauer if (rootshell && mflag) 2804b88c807SRodney W. Grimes action = S_IGN; 2814b88c807SRodney W. Grimes break; 2824b88c807SRodney W. Grimes #endif 2839de7305eSTim J. Robbins #ifndef NO_HISTORY 2849de7305eSTim J. Robbins case SIGWINCH: 285be58cc48STim J. Robbins if (rootshell && iflag) 2869de7305eSTim J. Robbins action = S_CATCH; 2879de7305eSTim J. Robbins break; 2889de7305eSTim J. Robbins #endif 2894b88c807SRodney W. Grimes } 2904b88c807SRodney W. Grimes } 291aa9caaf6SPeter Wemm 292f98e1b80SSteve Price t = &sigmode[signo]; 2934b88c807SRodney W. Grimes if (*t == 0) { 2944b88c807SRodney W. Grimes /* 2954b88c807SRodney W. Grimes * current setting unknown 2964b88c807SRodney W. Grimes */ 297aa9caaf6SPeter Wemm if (!getsigaction(signo, &sigact)) { 298aa9caaf6SPeter Wemm /* 299aa9caaf6SPeter Wemm * Pretend it worked; maybe we should give a warning 300aa9caaf6SPeter Wemm * here, but other shells don't. We don't alter 301aa9caaf6SPeter Wemm * sigmode, so that we retry every time. 302aa9caaf6SPeter Wemm */ 30328701136SBruce Evans return; 304aa9caaf6SPeter Wemm } 3054b88c807SRodney W. Grimes if (sigact == SIG_IGN) { 3064b88c807SRodney W. Grimes if (mflag && (signo == SIGTSTP || 3074b88c807SRodney W. Grimes signo == SIGTTIN || signo == SIGTTOU)) { 3084b88c807SRodney W. Grimes *t = S_IGN; /* don't hard ignore these */ 3094b88c807SRodney W. Grimes } else 3104b88c807SRodney W. Grimes *t = S_HARD_IGN; 3114b88c807SRodney W. Grimes } else { 3124b88c807SRodney W. Grimes *t = S_RESET; /* force to be set */ 3134b88c807SRodney W. Grimes } 3144b88c807SRodney W. Grimes } 3154b88c807SRodney W. Grimes if (*t == S_HARD_IGN || *t == action) 31628701136SBruce Evans return; 3174b88c807SRodney W. Grimes switch (action) { 3184b88c807SRodney W. Grimes case S_DFL: sigact = SIG_DFL; break; 3194b88c807SRodney W. Grimes case S_CATCH: sigact = onsig; break; 3204b88c807SRodney W. Grimes case S_IGN: sigact = SIG_IGN; break; 3214b88c807SRodney W. Grimes } 3224b88c807SRodney W. Grimes *t = action; 32328701136SBruce Evans sig = signal(signo, sigact); 32428701136SBruce Evans if (sig != SIG_ERR && action == S_CATCH) 32528701136SBruce Evans siginterrupt(signo, 1); 3264b88c807SRodney W. Grimes } 3274b88c807SRodney W. Grimes 328f98e1b80SSteve Price 3294b88c807SRodney W. Grimes /* 3304b88c807SRodney W. Grimes * Return the current setting for sig w/o changing it. 3314b88c807SRodney W. Grimes */ 332aa9caaf6SPeter Wemm static int 3335134c3f7SWarner Losh getsigaction(int signo, sig_t *sigact) 334aa9caaf6SPeter Wemm { 3354b88c807SRodney W. Grimes struct sigaction sa; 3364b88c807SRodney W. Grimes 3374b88c807SRodney W. Grimes if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 338aa9caaf6SPeter Wemm return 0; 339aa9caaf6SPeter Wemm *sigact = (sig_t) sa.sa_handler; 340aa9caaf6SPeter Wemm return 1; 3414b88c807SRodney W. Grimes } 3424b88c807SRodney W. Grimes 343f98e1b80SSteve Price 3444b88c807SRodney W. Grimes /* 3454b88c807SRodney W. Grimes * Ignore a signal. 3464b88c807SRodney W. Grimes */ 3474b88c807SRodney W. Grimes void 3485134c3f7SWarner Losh ignoresig(int signo) 349aa9caaf6SPeter Wemm { 350f98e1b80SSteve Price 351f98e1b80SSteve Price if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { 3524b88c807SRodney W. Grimes signal(signo, SIG_IGN); 3534b88c807SRodney W. Grimes } 354f98e1b80SSteve Price sigmode[signo] = S_HARD_IGN; 3554b88c807SRodney W. Grimes } 3564b88c807SRodney W. Grimes 3574b88c807SRodney W. Grimes 3584b88c807SRodney W. Grimes #ifdef mkinit 359aa9caaf6SPeter Wemm INCLUDE <signal.h> 3604b88c807SRodney W. Grimes INCLUDE "trap.h" 3614b88c807SRodney W. Grimes 3624b88c807SRodney W. Grimes SHELLPROC { 3634b88c807SRodney W. Grimes char *sm; 3644b88c807SRodney W. Grimes 3654b88c807SRodney W. Grimes clear_traps(); 366aa9caaf6SPeter Wemm for (sm = sigmode ; sm < sigmode + NSIG ; sm++) { 3674b88c807SRodney W. Grimes if (*sm == S_IGN) 3684b88c807SRodney W. Grimes *sm = S_HARD_IGN; 3694b88c807SRodney W. Grimes } 3704b88c807SRodney W. Grimes } 3714b88c807SRodney W. Grimes #endif 3724b88c807SRodney W. Grimes 3734b88c807SRodney W. Grimes 3744b88c807SRodney W. Grimes /* 3754b88c807SRodney W. Grimes * Signal handler. 3764b88c807SRodney W. Grimes */ 3774b88c807SRodney W. Grimes void 3785134c3f7SWarner Losh onsig(int signo) 379aa9caaf6SPeter Wemm { 38028701136SBruce Evans 3814b88c807SRodney W. Grimes if (signo == SIGINT && trap[SIGINT] == NULL) { 3824b88c807SRodney W. Grimes onint(); 3834b88c807SRodney W. Grimes return; 3844b88c807SRodney W. Grimes } 385135421ddSMartin Cracauer 386f98e1b80SSteve Price if (signo != SIGCHLD || !ignore_sigchld) 387f98e1b80SSteve Price gotsig[signo] = 1; 3884b88c807SRodney W. Grimes pendingsigs++; 389135421ddSMartin Cracauer 390135421ddSMartin Cracauer /* If we are currently in a wait builtin, prepare to break it */ 391135421ddSMartin Cracauer if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0) 3927a8e920bSMartin Cracauer breakwaitcmd = 1; 393135421ddSMartin Cracauer /* 3949bbfa415SMartin Cracauer * If a trap is set, not ignored and not the null command, we need 3959bbfa415SMartin Cracauer * to make sure traps are executed even when a child blocks signals. 396135421ddSMartin Cracauer */ 397c1c72a3cSMartin Cracauer if (Tflag && 398c1c72a3cSMartin Cracauer trap[signo] != NULL && 399f7d95a07SRalf S. Engelschall ! (trap[signo][0] == '\0') && 400e6ac45ddSMartin Cracauer ! (trap[signo][0] == ':' && trap[signo][1] == '\0')) 401135421ddSMartin Cracauer breakwaitcmd = 1; 4029de7305eSTim J. Robbins 4039de7305eSTim J. Robbins #ifndef NO_HISTORY 4049de7305eSTim J. Robbins if (signo == SIGWINCH) 405be58cc48STim J. Robbins gotwinch = 1; 4069de7305eSTim J. Robbins #endif 4074b88c807SRodney W. Grimes } 4084b88c807SRodney W. Grimes 4094b88c807SRodney W. Grimes 4104b88c807SRodney W. Grimes /* 4114b88c807SRodney W. Grimes * Called to execute a trap. Perhaps we should avoid entering new trap 4124b88c807SRodney W. Grimes * handlers while we are executing a trap handler. 4134b88c807SRodney W. Grimes */ 4144b88c807SRodney W. Grimes void 4155134c3f7SWarner Losh dotrap(void) 416f98e1b80SSteve Price { 4174b88c807SRodney W. Grimes int i; 4184b88c807SRodney W. Grimes int savestatus; 4194b88c807SRodney W. Grimes 4207a8e920bSMartin Cracauer in_dotrap++; 4214b88c807SRodney W. Grimes for (;;) { 422f98e1b80SSteve Price for (i = 1; i < NSIG; i++) { 423f98e1b80SSteve Price if (gotsig[i]) { 424f98e1b80SSteve Price gotsig[i] = 0; 425f98e1b80SSteve Price if (trap[i]) { 426f98e1b80SSteve Price /* 427aa0b7126SNate Lawson * Ignore SIGCHLD to avoid infinite 428aa0b7126SNate Lawson * recursion if the trap action does 429aa0b7126SNate Lawson * a fork. 430f98e1b80SSteve Price */ 431f98e1b80SSteve Price if (i == SIGCHLD) 432f98e1b80SSteve Price ignore_sigchld++; 4334b88c807SRodney W. Grimes savestatus = exitstatus; 434cb806389SStefan Farfeleder evalstring(trap[i], 0); 4354b88c807SRodney W. Grimes exitstatus = savestatus; 436f98e1b80SSteve Price if (i == SIGCHLD) 437f98e1b80SSteve Price ignore_sigchld--; 4384b88c807SRodney W. Grimes } 439f98e1b80SSteve Price break; 440f98e1b80SSteve Price } 441f98e1b80SSteve Price } 442f98e1b80SSteve Price if (i >= NSIG) 443f98e1b80SSteve Price break; 444f98e1b80SSteve Price } 4457a8e920bSMartin Cracauer in_dotrap--; 4464b88c807SRodney W. Grimes pendingsigs = 0; 4474b88c807SRodney W. Grimes } 4484b88c807SRodney W. Grimes 4494b88c807SRodney W. Grimes 4504b88c807SRodney W. Grimes /* 4514b88c807SRodney W. Grimes * Controls whether the shell is interactive or not. 4524b88c807SRodney W. Grimes */ 4534b88c807SRodney W. Grimes void 4545134c3f7SWarner Losh setinteractive(int on) 455aa9caaf6SPeter Wemm { 4567a8e920bSMartin Cracauer static int is_interactive = -1; 4574b88c807SRodney W. Grimes 4584b88c807SRodney W. Grimes if (on == is_interactive) 4594b88c807SRodney W. Grimes return; 4604b88c807SRodney W. Grimes setsignal(SIGINT); 4614b88c807SRodney W. Grimes setsignal(SIGQUIT); 4624b88c807SRodney W. Grimes setsignal(SIGTERM); 4639de7305eSTim J. Robbins #ifndef NO_HISTORY 4649de7305eSTim J. Robbins setsignal(SIGWINCH); 4659de7305eSTim J. Robbins #endif 4664b88c807SRodney W. Grimes is_interactive = on; 4674b88c807SRodney W. Grimes } 4684b88c807SRodney W. Grimes 4694b88c807SRodney W. Grimes 4704b88c807SRodney W. Grimes /* 4714b88c807SRodney W. Grimes * Called to exit the shell. 4724b88c807SRodney W. Grimes */ 4734b88c807SRodney W. Grimes void 4745134c3f7SWarner Losh exitshell(int status) 475aa9caaf6SPeter Wemm { 4764b88c807SRodney W. Grimes struct jmploc loc1, loc2; 4774b88c807SRodney W. Grimes char *p; 4784b88c807SRodney W. Grimes 4794b88c807SRodney W. Grimes TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 4804b88c807SRodney W. Grimes if (setjmp(loc1.loc)) { 4814b88c807SRodney W. Grimes goto l1; 4824b88c807SRodney W. Grimes } 4834b88c807SRodney W. Grimes if (setjmp(loc2.loc)) { 4844b88c807SRodney W. Grimes goto l2; 4854b88c807SRodney W. Grimes } 4864b88c807SRodney W. Grimes handler = &loc1; 4874b88c807SRodney W. Grimes if ((p = trap[0]) != NULL && *p != '\0') { 4884b88c807SRodney W. Grimes trap[0] = NULL; 489cb806389SStefan Farfeleder evalstring(p, 0); 4904b88c807SRodney W. Grimes } 4914b88c807SRodney W. Grimes l1: handler = &loc2; /* probably unnecessary */ 4924b88c807SRodney W. Grimes flushall(); 4934b88c807SRodney W. Grimes #if JOBS 4944b88c807SRodney W. Grimes setjobctl(0); 4954b88c807SRodney W. Grimes #endif 4964b88c807SRodney W. Grimes l2: _exit(status); 4974b88c807SRodney W. Grimes } 498