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 * 3. All advertising materials mentioning features or use of this software 174b88c807SRodney W. Grimes * must display the following acknowledgement: 184b88c807SRodney W. Grimes * This product includes software developed by the University of 194b88c807SRodney W. Grimes * California, Berkeley and its contributors. 204b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 214b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 224b88c807SRodney W. Grimes * without specific prior written permission. 234b88c807SRodney W. Grimes * 244b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344b88c807SRodney W. Grimes * SUCH DAMAGE. 3589730b29SDavid Greenman * 36aa9caaf6SPeter Wemm * $Id: trap.c,v 1.3 1995/05/30 00:07:23 rgrimes Exp $ 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 40aa9caaf6SPeter Wemm static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 43aa9caaf6SPeter Wemm #include <signal.h> 44aa9caaf6SPeter Wemm #include <unistd.h> 45aa9caaf6SPeter Wemm #include <stdlib.h> 46aa9caaf6SPeter Wemm 474b88c807SRodney W. Grimes #include "shell.h" 484b88c807SRodney W. Grimes #include "main.h" 494b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 504b88c807SRodney W. Grimes #include "eval.h" 514b88c807SRodney W. Grimes #include "jobs.h" 52aa9caaf6SPeter Wemm #include "show.h" 534b88c807SRodney W. Grimes #include "options.h" 544b88c807SRodney W. Grimes #include "syntax.h" 554b88c807SRodney W. Grimes #include "output.h" 564b88c807SRodney W. Grimes #include "memalloc.h" 574b88c807SRodney W. Grimes #include "error.h" 584b88c807SRodney W. Grimes #include "trap.h" 594b88c807SRodney W. Grimes #include "mystring.h" 604b88c807SRodney W. Grimes 614b88c807SRodney W. Grimes 624b88c807SRodney W. Grimes /* 634b88c807SRodney W. Grimes * Sigmode records the current value of the signal handlers for the various 644b88c807SRodney W. Grimes * modes. A value of zero means that the current handler is not known. 654b88c807SRodney W. Grimes * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 664b88c807SRodney W. Grimes */ 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes #define S_DFL 1 /* default signal handling (SIG_DFL) */ 694b88c807SRodney W. Grimes #define S_CATCH 2 /* signal is caught */ 704b88c807SRodney W. Grimes #define S_IGN 3 /* signal is ignored (SIG_IGN) */ 714b88c807SRodney W. Grimes #define S_HARD_IGN 4 /* signal is ignored permenantly */ 724b88c807SRodney W. Grimes #define S_RESET 5 /* temporary - to reset a hard ignored sig */ 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes extern char nullstr[1]; /* null string */ 764b88c807SRodney W. Grimes 77aa9caaf6SPeter Wemm char *trap[NSIG+1]; /* trap handler commands */ 78aa9caaf6SPeter Wemm MKINIT char sigmode[NSIG]; /* current value of signal */ 79aa9caaf6SPeter Wemm char gotsig[NSIG]; /* indicates specified signal received */ 804b88c807SRodney W. Grimes int pendingsigs; /* indicates some signal received */ 814b88c807SRodney W. Grimes 82aa9caaf6SPeter Wemm static int getsigaction __P((int, sig_t *)); 83aa9caaf6SPeter Wemm 844b88c807SRodney W. Grimes /* 854b88c807SRodney W. Grimes * The trap builtin. 864b88c807SRodney W. Grimes */ 874b88c807SRodney W. Grimes 88aa9caaf6SPeter Wemm int 89aa9caaf6SPeter Wemm trapcmd(argc, argv) 90aa9caaf6SPeter Wemm int argc; 91aa9caaf6SPeter Wemm char **argv; 92aa9caaf6SPeter Wemm { 934b88c807SRodney W. Grimes char *action; 944b88c807SRodney W. Grimes char **ap; 954b88c807SRodney W. Grimes int signo; 964b88c807SRodney W. Grimes 974b88c807SRodney W. Grimes if (argc <= 1) { 98aa9caaf6SPeter Wemm for (signo = 0 ; signo <= NSIG ; signo++) { 994b88c807SRodney W. Grimes if (trap[signo] != NULL) 1004b88c807SRodney W. Grimes out1fmt("%d: %s\n", signo, trap[signo]); 1014b88c807SRodney W. Grimes } 1024b88c807SRodney W. Grimes return 0; 1034b88c807SRodney W. Grimes } 1044b88c807SRodney W. Grimes ap = argv + 1; 1054b88c807SRodney W. Grimes if (is_number(*ap)) 1064b88c807SRodney W. Grimes action = NULL; 1074b88c807SRodney W. Grimes else 1084b88c807SRodney W. Grimes action = *ap++; 1094b88c807SRodney W. Grimes while (*ap) { 110aa9caaf6SPeter Wemm if ((signo = number(*ap)) < 0 || signo > NSIG) 1114b88c807SRodney W. Grimes error("%s: bad trap", *ap); 1124b88c807SRodney W. Grimes INTOFF; 1134b88c807SRodney W. Grimes if (action) 1144b88c807SRodney W. Grimes action = savestr(action); 1154b88c807SRodney W. Grimes if (trap[signo]) 1164b88c807SRodney W. Grimes ckfree(trap[signo]); 1174b88c807SRodney W. Grimes trap[signo] = action; 1184b88c807SRodney W. Grimes if (signo != 0) 1194b88c807SRodney W. Grimes setsignal(signo); 1204b88c807SRodney W. Grimes INTON; 1214b88c807SRodney W. Grimes ap++; 1224b88c807SRodney W. Grimes } 1234b88c807SRodney W. Grimes return 0; 1244b88c807SRodney W. Grimes } 1254b88c807SRodney W. Grimes 1264b88c807SRodney W. Grimes 1274b88c807SRodney W. Grimes 1284b88c807SRodney W. Grimes /* 1294b88c807SRodney W. Grimes * Clear traps on a fork. 1304b88c807SRodney W. Grimes */ 1314b88c807SRodney W. Grimes 1324b88c807SRodney W. Grimes void 1334b88c807SRodney W. Grimes clear_traps() { 1344b88c807SRodney W. Grimes char **tp; 1354b88c807SRodney W. Grimes 136aa9caaf6SPeter Wemm for (tp = trap ; tp <= &trap[NSIG] ; tp++) { 1374b88c807SRodney W. Grimes if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 1384b88c807SRodney W. Grimes INTOFF; 1394b88c807SRodney W. Grimes ckfree(*tp); 1404b88c807SRodney W. Grimes *tp = NULL; 1414b88c807SRodney W. Grimes if (tp != &trap[0]) 1424b88c807SRodney W. Grimes setsignal(tp - trap); 1434b88c807SRodney W. Grimes INTON; 1444b88c807SRodney W. Grimes } 1454b88c807SRodney W. Grimes } 1464b88c807SRodney W. Grimes } 1474b88c807SRodney W. Grimes 1484b88c807SRodney W. Grimes 1494b88c807SRodney W. Grimes 1504b88c807SRodney W. Grimes /* 1514b88c807SRodney W. Grimes * Set the signal handler for the specified signal. The routine figures 1524b88c807SRodney W. Grimes * out what it should be set to. 1534b88c807SRodney W. Grimes */ 1544b88c807SRodney W. Grimes 155aa9caaf6SPeter Wemm long 156aa9caaf6SPeter Wemm setsignal(signo) 157aa9caaf6SPeter Wemm int signo; 158aa9caaf6SPeter Wemm { 1594b88c807SRodney W. Grimes int action; 160aa9caaf6SPeter Wemm sig_t sigact = SIG_DFL; 1614b88c807SRodney W. Grimes char *t; 1624b88c807SRodney W. Grimes extern void onsig(); 1634b88c807SRodney W. Grimes 1644b88c807SRodney W. Grimes if ((t = trap[signo]) == NULL) 1654b88c807SRodney W. Grimes action = S_DFL; 1664b88c807SRodney W. Grimes else if (*t != '\0') 1674b88c807SRodney W. Grimes action = S_CATCH; 1684b88c807SRodney W. Grimes else 1694b88c807SRodney W. Grimes action = S_IGN; 1704b88c807SRodney W. Grimes if (rootshell && action == S_DFL) { 1714b88c807SRodney W. Grimes switch (signo) { 1724b88c807SRodney W. Grimes case SIGINT: 1734b88c807SRodney W. Grimes if (iflag) 1744b88c807SRodney W. Grimes action = S_CATCH; 1754b88c807SRodney W. Grimes break; 1764b88c807SRodney W. Grimes case SIGQUIT: 1774b88c807SRodney W. Grimes #ifdef DEBUG 1784b88c807SRodney W. Grimes { 1794b88c807SRodney W. Grimes extern int debug; 1804b88c807SRodney W. Grimes 1814b88c807SRodney W. Grimes if (debug) 1824b88c807SRodney W. Grimes break; 1834b88c807SRodney W. Grimes } 1844b88c807SRodney W. Grimes #endif 1854b88c807SRodney W. Grimes /* FALLTHROUGH */ 1864b88c807SRodney W. Grimes case SIGTERM: 1874b88c807SRodney W. Grimes if (iflag) 1884b88c807SRodney W. Grimes action = S_IGN; 1894b88c807SRodney W. Grimes break; 1904b88c807SRodney W. Grimes #if JOBS 1914b88c807SRodney W. Grimes case SIGTSTP: 1924b88c807SRodney W. Grimes case SIGTTOU: 1934b88c807SRodney W. Grimes if (mflag) 1944b88c807SRodney W. Grimes action = S_IGN; 1954b88c807SRodney W. Grimes break; 1964b88c807SRodney W. Grimes #endif 1974b88c807SRodney W. Grimes } 1984b88c807SRodney W. Grimes } 199aa9caaf6SPeter Wemm 2004b88c807SRodney W. Grimes t = &sigmode[signo - 1]; 2014b88c807SRodney W. Grimes if (*t == 0) { 2024b88c807SRodney W. Grimes /* 2034b88c807SRodney W. Grimes * current setting unknown 2044b88c807SRodney W. Grimes */ 205aa9caaf6SPeter Wemm if (!getsigaction(signo, &sigact)) { 206aa9caaf6SPeter Wemm /* 207aa9caaf6SPeter Wemm * Pretend it worked; maybe we should give a warning 208aa9caaf6SPeter Wemm * here, but other shells don't. We don't alter 209aa9caaf6SPeter Wemm * sigmode, so that we retry every time. 210aa9caaf6SPeter Wemm */ 211aa9caaf6SPeter Wemm return 0; 212aa9caaf6SPeter Wemm } 2134b88c807SRodney W. Grimes if (sigact == SIG_IGN) { 2144b88c807SRodney W. Grimes if (mflag && (signo == SIGTSTP || 2154b88c807SRodney W. Grimes signo == SIGTTIN || signo == SIGTTOU)) { 2164b88c807SRodney W. Grimes *t = S_IGN; /* don't hard ignore these */ 2174b88c807SRodney W. Grimes } else 2184b88c807SRodney W. Grimes *t = S_HARD_IGN; 2194b88c807SRodney W. Grimes } else { 2204b88c807SRodney W. Grimes *t = S_RESET; /* force to be set */ 2214b88c807SRodney W. Grimes } 2224b88c807SRodney W. Grimes } 2234b88c807SRodney W. Grimes if (*t == S_HARD_IGN || *t == action) 2244b88c807SRodney W. Grimes return 0; 2254b88c807SRodney W. Grimes switch (action) { 2264b88c807SRodney W. Grimes case S_DFL: sigact = SIG_DFL; break; 2274b88c807SRodney W. Grimes case S_CATCH: sigact = onsig; break; 2284b88c807SRodney W. Grimes case S_IGN: sigact = SIG_IGN; break; 2294b88c807SRodney W. Grimes } 2304b88c807SRodney W. Grimes *t = action; 231aa9caaf6SPeter Wemm return (long)signal(signo, sigact); 2324b88c807SRodney W. Grimes } 2334b88c807SRodney W. Grimes 2344b88c807SRodney W. Grimes /* 2354b88c807SRodney W. Grimes * Return the current setting for sig w/o changing it. 2364b88c807SRodney W. Grimes */ 237aa9caaf6SPeter Wemm static int 238aa9caaf6SPeter Wemm getsigaction(signo, sigact) 239aa9caaf6SPeter Wemm int signo; 240aa9caaf6SPeter Wemm sig_t *sigact; 241aa9caaf6SPeter Wemm { 2424b88c807SRodney W. Grimes struct sigaction sa; 2434b88c807SRodney W. Grimes 2444b88c807SRodney W. Grimes if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 245aa9caaf6SPeter Wemm return 0; 246aa9caaf6SPeter Wemm *sigact = (sig_t) sa.sa_handler; 247aa9caaf6SPeter Wemm return 1; 2484b88c807SRodney W. Grimes } 2494b88c807SRodney W. Grimes 2504b88c807SRodney W. Grimes /* 2514b88c807SRodney W. Grimes * Ignore a signal. 2524b88c807SRodney W. Grimes */ 2534b88c807SRodney W. Grimes 2544b88c807SRodney W. Grimes void 255aa9caaf6SPeter Wemm ignoresig(signo) 256aa9caaf6SPeter Wemm int signo; 257aa9caaf6SPeter Wemm { 2584b88c807SRodney W. Grimes if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { 2594b88c807SRodney W. Grimes signal(signo, SIG_IGN); 2604b88c807SRodney W. Grimes } 2614b88c807SRodney W. Grimes sigmode[signo - 1] = S_HARD_IGN; 2624b88c807SRodney W. Grimes } 2634b88c807SRodney W. Grimes 2644b88c807SRodney W. Grimes 2654b88c807SRodney W. Grimes #ifdef mkinit 266aa9caaf6SPeter Wemm INCLUDE <signal.h> 2674b88c807SRodney W. Grimes INCLUDE "trap.h" 2684b88c807SRodney W. Grimes 2694b88c807SRodney W. Grimes SHELLPROC { 2704b88c807SRodney W. Grimes char *sm; 2714b88c807SRodney W. Grimes 2724b88c807SRodney W. Grimes clear_traps(); 273aa9caaf6SPeter Wemm for (sm = sigmode ; sm < sigmode + NSIG ; sm++) { 2744b88c807SRodney W. Grimes if (*sm == S_IGN) 2754b88c807SRodney W. Grimes *sm = S_HARD_IGN; 2764b88c807SRodney W. Grimes } 2774b88c807SRodney W. Grimes } 2784b88c807SRodney W. Grimes #endif 2794b88c807SRodney W. Grimes 2804b88c807SRodney W. Grimes 2814b88c807SRodney W. Grimes 2824b88c807SRodney W. Grimes /* 2834b88c807SRodney W. Grimes * Signal handler. 2844b88c807SRodney W. Grimes */ 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes void 287aa9caaf6SPeter Wemm onsig(signo) 288aa9caaf6SPeter Wemm int signo; 289aa9caaf6SPeter Wemm { 2904b88c807SRodney W. Grimes signal(signo, onsig); 2914b88c807SRodney W. Grimes if (signo == SIGINT && trap[SIGINT] == NULL) { 2924b88c807SRodney W. Grimes onint(); 2934b88c807SRodney W. Grimes return; 2944b88c807SRodney W. Grimes } 2954b88c807SRodney W. Grimes gotsig[signo - 1] = 1; 2964b88c807SRodney W. Grimes pendingsigs++; 2974b88c807SRodney W. Grimes } 2984b88c807SRodney W. Grimes 2994b88c807SRodney W. Grimes 3004b88c807SRodney W. Grimes 3014b88c807SRodney W. Grimes /* 3024b88c807SRodney W. Grimes * Called to execute a trap. Perhaps we should avoid entering new trap 3034b88c807SRodney W. Grimes * handlers while we are executing a trap handler. 3044b88c807SRodney W. Grimes */ 3054b88c807SRodney W. Grimes 3064b88c807SRodney W. Grimes void 3074b88c807SRodney W. Grimes dotrap() { 3084b88c807SRodney W. Grimes int i; 3094b88c807SRodney W. Grimes int savestatus; 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes for (;;) { 3124b88c807SRodney W. Grimes for (i = 1 ; ; i++) { 3134b88c807SRodney W. Grimes if (gotsig[i - 1]) 3144b88c807SRodney W. Grimes break; 315aa9caaf6SPeter Wemm if (i >= NSIG) 3164b88c807SRodney W. Grimes goto done; 3174b88c807SRodney W. Grimes } 3184b88c807SRodney W. Grimes gotsig[i - 1] = 0; 3194b88c807SRodney W. Grimes savestatus=exitstatus; 3204b88c807SRodney W. Grimes evalstring(trap[i]); 3214b88c807SRodney W. Grimes exitstatus=savestatus; 3224b88c807SRodney W. Grimes } 3234b88c807SRodney W. Grimes done: 3244b88c807SRodney W. Grimes pendingsigs = 0; 3254b88c807SRodney W. Grimes } 3264b88c807SRodney W. Grimes 3274b88c807SRodney W. Grimes 3284b88c807SRodney W. Grimes 3294b88c807SRodney W. Grimes /* 3304b88c807SRodney W. Grimes * Controls whether the shell is interactive or not. 3314b88c807SRodney W. Grimes */ 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes 3344b88c807SRodney W. Grimes void 335aa9caaf6SPeter Wemm setinteractive(on) 336aa9caaf6SPeter Wemm int on; 337aa9caaf6SPeter Wemm { 3384b88c807SRodney W. Grimes static int is_interactive; 3394b88c807SRodney W. Grimes 3404b88c807SRodney W. Grimes if (on == is_interactive) 3414b88c807SRodney W. Grimes return; 3424b88c807SRodney W. Grimes setsignal(SIGINT); 3434b88c807SRodney W. Grimes setsignal(SIGQUIT); 3444b88c807SRodney W. Grimes setsignal(SIGTERM); 3454b88c807SRodney W. Grimes is_interactive = on; 3464b88c807SRodney W. Grimes } 3474b88c807SRodney W. Grimes 3484b88c807SRodney W. Grimes 3494b88c807SRodney W. Grimes 3504b88c807SRodney W. Grimes /* 3514b88c807SRodney W. Grimes * Called to exit the shell. 3524b88c807SRodney W. Grimes */ 3534b88c807SRodney W. Grimes 3544b88c807SRodney W. Grimes void 355aa9caaf6SPeter Wemm exitshell(status) 356aa9caaf6SPeter Wemm int status; 357aa9caaf6SPeter Wemm { 3584b88c807SRodney W. Grimes struct jmploc loc1, loc2; 3594b88c807SRodney W. Grimes char *p; 3604b88c807SRodney W. Grimes 3614b88c807SRodney W. Grimes TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 3624b88c807SRodney W. Grimes if (setjmp(loc1.loc)) { 3634b88c807SRodney W. Grimes goto l1; 3644b88c807SRodney W. Grimes } 3654b88c807SRodney W. Grimes if (setjmp(loc2.loc)) { 3664b88c807SRodney W. Grimes goto l2; 3674b88c807SRodney W. Grimes } 3684b88c807SRodney W. Grimes handler = &loc1; 3694b88c807SRodney W. Grimes if ((p = trap[0]) != NULL && *p != '\0') { 3704b88c807SRodney W. Grimes trap[0] = NULL; 3714b88c807SRodney W. Grimes evalstring(p); 3724b88c807SRodney W. Grimes } 3734b88c807SRodney W. Grimes l1: handler = &loc2; /* probably unnecessary */ 3744b88c807SRodney W. Grimes flushall(); 3754b88c807SRodney W. Grimes #if JOBS 3764b88c807SRodney W. Grimes setjobctl(0); 3774b88c807SRodney W. Grimes #endif 3784b88c807SRodney W. Grimes l2: _exit(status); 3794b88c807SRodney W. Grimes } 380