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 * 3689730b29SDavid Greenman * $Id$ 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 404b88c807SRodney W. Grimes static char sccsid[] = "@(#)trap.c 8.1 (Berkeley) 5/31/93"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 434b88c807SRodney W. Grimes #include "shell.h" 444b88c807SRodney W. Grimes #include "main.h" 454b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 464b88c807SRodney W. Grimes #include "eval.h" 474b88c807SRodney W. Grimes #include "jobs.h" 484b88c807SRodney W. Grimes #include "options.h" 494b88c807SRodney W. Grimes #include "syntax.h" 504b88c807SRodney W. Grimes #include "signames.h" 514b88c807SRodney W. Grimes #include "output.h" 524b88c807SRodney W. Grimes #include "memalloc.h" 534b88c807SRodney W. Grimes #include "error.h" 544b88c807SRodney W. Grimes #include "trap.h" 554b88c807SRodney W. Grimes #include "mystring.h" 564b88c807SRodney W. Grimes #include <signal.h> 574b88c807SRodney W. Grimes 584b88c807SRodney W. Grimes 594b88c807SRodney W. Grimes /* 604b88c807SRodney W. Grimes * Sigmode records the current value of the signal handlers for the various 614b88c807SRodney W. Grimes * modes. A value of zero means that the current handler is not known. 624b88c807SRodney W. Grimes * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 634b88c807SRodney W. Grimes */ 644b88c807SRodney W. Grimes 654b88c807SRodney W. Grimes #define S_DFL 1 /* default signal handling (SIG_DFL) */ 664b88c807SRodney W. Grimes #define S_CATCH 2 /* signal is caught */ 674b88c807SRodney W. Grimes #define S_IGN 3 /* signal is ignored (SIG_IGN) */ 684b88c807SRodney W. Grimes #define S_HARD_IGN 4 /* signal is ignored permenantly */ 694b88c807SRodney W. Grimes #define S_RESET 5 /* temporary - to reset a hard ignored sig */ 704b88c807SRodney W. Grimes 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes extern char nullstr[1]; /* null string */ 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes char *trap[MAXSIG+1]; /* trap handler commands */ 754b88c807SRodney W. Grimes MKINIT char sigmode[MAXSIG]; /* current value of signal */ 764b88c807SRodney W. Grimes char gotsig[MAXSIG]; /* indicates specified signal received */ 774b88c807SRodney W. Grimes int pendingsigs; /* indicates some signal received */ 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes /* 804b88c807SRodney W. Grimes * The trap builtin. 814b88c807SRodney W. Grimes */ 824b88c807SRodney W. Grimes 834b88c807SRodney W. Grimes trapcmd(argc, argv) char **argv; { 844b88c807SRodney W. Grimes char *action; 854b88c807SRodney W. Grimes char **ap; 864b88c807SRodney W. Grimes int signo; 874b88c807SRodney W. Grimes 884b88c807SRodney W. Grimes if (argc <= 1) { 894b88c807SRodney W. Grimes for (signo = 0 ; signo <= MAXSIG ; signo++) { 904b88c807SRodney W. Grimes if (trap[signo] != NULL) 914b88c807SRodney W. Grimes out1fmt("%d: %s\n", signo, trap[signo]); 924b88c807SRodney W. Grimes } 934b88c807SRodney W. Grimes return 0; 944b88c807SRodney W. Grimes } 954b88c807SRodney W. Grimes ap = argv + 1; 964b88c807SRodney W. Grimes if (is_number(*ap)) 974b88c807SRodney W. Grimes action = NULL; 984b88c807SRodney W. Grimes else 994b88c807SRodney W. Grimes action = *ap++; 1004b88c807SRodney W. Grimes while (*ap) { 1014b88c807SRodney W. Grimes if ((signo = number(*ap)) < 0 || signo > MAXSIG) 1024b88c807SRodney W. Grimes error("%s: bad trap", *ap); 1034b88c807SRodney W. Grimes INTOFF; 1044b88c807SRodney W. Grimes if (action) 1054b88c807SRodney W. Grimes action = savestr(action); 1064b88c807SRodney W. Grimes if (trap[signo]) 1074b88c807SRodney W. Grimes ckfree(trap[signo]); 1084b88c807SRodney W. Grimes trap[signo] = action; 1094b88c807SRodney W. Grimes if (signo != 0) 1104b88c807SRodney W. Grimes setsignal(signo); 1114b88c807SRodney W. Grimes INTON; 1124b88c807SRodney W. Grimes ap++; 1134b88c807SRodney W. Grimes } 1144b88c807SRodney W. Grimes return 0; 1154b88c807SRodney W. Grimes } 1164b88c807SRodney W. Grimes 1174b88c807SRodney W. Grimes 1184b88c807SRodney W. Grimes 1194b88c807SRodney W. Grimes /* 1204b88c807SRodney W. Grimes * Clear traps on a fork. 1214b88c807SRodney W. Grimes */ 1224b88c807SRodney W. Grimes 1234b88c807SRodney W. Grimes void 1244b88c807SRodney W. Grimes clear_traps() { 1254b88c807SRodney W. Grimes char **tp; 1264b88c807SRodney W. Grimes 1274b88c807SRodney W. Grimes for (tp = trap ; tp <= &trap[MAXSIG] ; tp++) { 1284b88c807SRodney W. Grimes if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 1294b88c807SRodney W. Grimes INTOFF; 1304b88c807SRodney W. Grimes ckfree(*tp); 1314b88c807SRodney W. Grimes *tp = NULL; 1324b88c807SRodney W. Grimes if (tp != &trap[0]) 1334b88c807SRodney W. Grimes setsignal(tp - trap); 1344b88c807SRodney W. Grimes INTON; 1354b88c807SRodney W. Grimes } 1364b88c807SRodney W. Grimes } 1374b88c807SRodney W. Grimes } 1384b88c807SRodney W. Grimes 1394b88c807SRodney W. Grimes 1404b88c807SRodney W. Grimes 1414b88c807SRodney W. Grimes /* 1424b88c807SRodney W. Grimes * Set the signal handler for the specified signal. The routine figures 1434b88c807SRodney W. Grimes * out what it should be set to. 1444b88c807SRodney W. Grimes */ 1454b88c807SRodney W. Grimes 1464b88c807SRodney W. Grimes int 1474b88c807SRodney W. Grimes setsignal(signo) { 1484b88c807SRodney W. Grimes int action; 1494b88c807SRodney W. Grimes sig_t sigact; 1504b88c807SRodney W. Grimes char *t; 1514b88c807SRodney W. Grimes extern void onsig(); 1524b88c807SRodney W. Grimes extern sig_t getsigaction(); 1534b88c807SRodney W. Grimes 1544b88c807SRodney W. Grimes if ((t = trap[signo]) == NULL) 1554b88c807SRodney W. Grimes action = S_DFL; 1564b88c807SRodney W. Grimes else if (*t != '\0') 1574b88c807SRodney W. Grimes action = S_CATCH; 1584b88c807SRodney W. Grimes else 1594b88c807SRodney W. Grimes action = S_IGN; 1604b88c807SRodney W. Grimes if (rootshell && action == S_DFL) { 1614b88c807SRodney W. Grimes switch (signo) { 1624b88c807SRodney W. Grimes case SIGINT: 1634b88c807SRodney W. Grimes if (iflag) 1644b88c807SRodney W. Grimes action = S_CATCH; 1654b88c807SRodney W. Grimes break; 1664b88c807SRodney W. Grimes case SIGQUIT: 1674b88c807SRodney W. Grimes #ifdef DEBUG 1684b88c807SRodney W. Grimes { 1694b88c807SRodney W. Grimes extern int debug; 1704b88c807SRodney W. Grimes 1714b88c807SRodney W. Grimes if (debug) 1724b88c807SRodney W. Grimes break; 1734b88c807SRodney W. Grimes } 1744b88c807SRodney W. Grimes #endif 1754b88c807SRodney W. Grimes /* FALLTHROUGH */ 1764b88c807SRodney W. Grimes case SIGTERM: 1774b88c807SRodney W. Grimes if (iflag) 1784b88c807SRodney W. Grimes action = S_IGN; 1794b88c807SRodney W. Grimes break; 1804b88c807SRodney W. Grimes #if JOBS 1814b88c807SRodney W. Grimes case SIGTSTP: 1824b88c807SRodney W. Grimes case SIGTTOU: 1834b88c807SRodney W. Grimes if (mflag) 1844b88c807SRodney W. Grimes action = S_IGN; 1854b88c807SRodney W. Grimes break; 1864b88c807SRodney W. Grimes #endif 1874b88c807SRodney W. Grimes } 1884b88c807SRodney W. Grimes } 1894b88c807SRodney W. Grimes t = &sigmode[signo - 1]; 1904b88c807SRodney W. Grimes if (*t == 0) { 1914b88c807SRodney W. Grimes /* 1924b88c807SRodney W. Grimes * current setting unknown 1934b88c807SRodney W. Grimes */ 1944b88c807SRodney W. Grimes sigact = getsigaction(signo); 1954b88c807SRodney W. Grimes if (sigact == SIG_IGN) { 1964b88c807SRodney W. Grimes if (mflag && (signo == SIGTSTP || 1974b88c807SRodney W. Grimes signo == SIGTTIN || signo == SIGTTOU)) { 1984b88c807SRodney W. Grimes *t = S_IGN; /* don't hard ignore these */ 1994b88c807SRodney W. Grimes } else 2004b88c807SRodney W. Grimes *t = S_HARD_IGN; 2014b88c807SRodney W. Grimes } else { 2024b88c807SRodney W. Grimes *t = S_RESET; /* force to be set */ 2034b88c807SRodney W. Grimes } 2044b88c807SRodney W. Grimes } 2054b88c807SRodney W. Grimes if (*t == S_HARD_IGN || *t == action) 2064b88c807SRodney W. Grimes return 0; 2074b88c807SRodney W. Grimes switch (action) { 2084b88c807SRodney W. Grimes case S_DFL: sigact = SIG_DFL; break; 2094b88c807SRodney W. Grimes case S_CATCH: sigact = onsig; break; 2104b88c807SRodney W. Grimes case S_IGN: sigact = SIG_IGN; break; 2114b88c807SRodney W. Grimes } 2124b88c807SRodney W. Grimes *t = action; 2134b88c807SRodney W. Grimes return (int)signal(signo, sigact); 2144b88c807SRodney W. Grimes } 2154b88c807SRodney W. Grimes 2164b88c807SRodney W. Grimes /* 2174b88c807SRodney W. Grimes * Return the current setting for sig w/o changing it. 2184b88c807SRodney W. Grimes */ 2194b88c807SRodney W. Grimes sig_t 2204b88c807SRodney W. Grimes getsigaction(signo) { 2214b88c807SRodney W. Grimes struct sigaction sa; 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 2244b88c807SRodney W. Grimes error("Sigaction system call failed"); 2254b88c807SRodney W. Grimes 2264b88c807SRodney W. Grimes return sa.sa_handler; 2274b88c807SRodney W. Grimes } 2284b88c807SRodney W. Grimes 2294b88c807SRodney W. Grimes /* 2304b88c807SRodney W. Grimes * Ignore a signal. 2314b88c807SRodney W. Grimes */ 2324b88c807SRodney W. Grimes 2334b88c807SRodney W. Grimes void 2344b88c807SRodney W. Grimes ignoresig(signo) { 2354b88c807SRodney W. Grimes if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { 2364b88c807SRodney W. Grimes signal(signo, SIG_IGN); 2374b88c807SRodney W. Grimes } 2384b88c807SRodney W. Grimes sigmode[signo - 1] = S_HARD_IGN; 2394b88c807SRodney W. Grimes } 2404b88c807SRodney W. Grimes 2414b88c807SRodney W. Grimes 2424b88c807SRodney W. Grimes #ifdef mkinit 2434b88c807SRodney W. Grimes INCLUDE "signames.h" 2444b88c807SRodney W. Grimes INCLUDE "trap.h" 2454b88c807SRodney W. Grimes 2464b88c807SRodney W. Grimes SHELLPROC { 2474b88c807SRodney W. Grimes char *sm; 2484b88c807SRodney W. Grimes 2494b88c807SRodney W. Grimes clear_traps(); 2504b88c807SRodney W. Grimes for (sm = sigmode ; sm < sigmode + MAXSIG ; sm++) { 2514b88c807SRodney W. Grimes if (*sm == S_IGN) 2524b88c807SRodney W. Grimes *sm = S_HARD_IGN; 2534b88c807SRodney W. Grimes } 2544b88c807SRodney W. Grimes } 2554b88c807SRodney W. Grimes #endif 2564b88c807SRodney W. Grimes 2574b88c807SRodney W. Grimes 2584b88c807SRodney W. Grimes 2594b88c807SRodney W. Grimes /* 2604b88c807SRodney W. Grimes * Signal handler. 2614b88c807SRodney W. Grimes */ 2624b88c807SRodney W. Grimes 2634b88c807SRodney W. Grimes void 2644b88c807SRodney W. Grimes onsig(signo) { 2654b88c807SRodney W. Grimes signal(signo, onsig); 2664b88c807SRodney W. Grimes if (signo == SIGINT && trap[SIGINT] == NULL) { 2674b88c807SRodney W. Grimes onint(); 2684b88c807SRodney W. Grimes return; 2694b88c807SRodney W. Grimes } 2704b88c807SRodney W. Grimes gotsig[signo - 1] = 1; 2714b88c807SRodney W. Grimes pendingsigs++; 2724b88c807SRodney W. Grimes } 2734b88c807SRodney W. Grimes 2744b88c807SRodney W. Grimes 2754b88c807SRodney W. Grimes 2764b88c807SRodney W. Grimes /* 2774b88c807SRodney W. Grimes * Called to execute a trap. Perhaps we should avoid entering new trap 2784b88c807SRodney W. Grimes * handlers while we are executing a trap handler. 2794b88c807SRodney W. Grimes */ 2804b88c807SRodney W. Grimes 2814b88c807SRodney W. Grimes void 2824b88c807SRodney W. Grimes dotrap() { 2834b88c807SRodney W. Grimes int i; 2844b88c807SRodney W. Grimes int savestatus; 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes for (;;) { 2874b88c807SRodney W. Grimes for (i = 1 ; ; i++) { 2884b88c807SRodney W. Grimes if (gotsig[i - 1]) 2894b88c807SRodney W. Grimes break; 2904b88c807SRodney W. Grimes if (i >= MAXSIG) 2914b88c807SRodney W. Grimes goto done; 2924b88c807SRodney W. Grimes } 2934b88c807SRodney W. Grimes gotsig[i - 1] = 0; 2944b88c807SRodney W. Grimes savestatus=exitstatus; 2954b88c807SRodney W. Grimes evalstring(trap[i]); 2964b88c807SRodney W. Grimes exitstatus=savestatus; 2974b88c807SRodney W. Grimes } 2984b88c807SRodney W. Grimes done: 2994b88c807SRodney W. Grimes pendingsigs = 0; 3004b88c807SRodney W. Grimes } 3014b88c807SRodney W. Grimes 3024b88c807SRodney W. Grimes 3034b88c807SRodney W. Grimes 3044b88c807SRodney W. Grimes /* 3054b88c807SRodney W. Grimes * Controls whether the shell is interactive or not. 3064b88c807SRodney W. Grimes */ 3074b88c807SRodney W. Grimes 3084b88c807SRodney W. Grimes 3094b88c807SRodney W. Grimes void 3104b88c807SRodney W. Grimes setinteractive(on) { 3114b88c807SRodney W. Grimes static int is_interactive; 3124b88c807SRodney W. Grimes 3134b88c807SRodney W. Grimes if (on == is_interactive) 3144b88c807SRodney W. Grimes return; 3154b88c807SRodney W. Grimes setsignal(SIGINT); 3164b88c807SRodney W. Grimes setsignal(SIGQUIT); 3174b88c807SRodney W. Grimes setsignal(SIGTERM); 3184b88c807SRodney W. Grimes is_interactive = on; 3194b88c807SRodney W. Grimes } 3204b88c807SRodney W. Grimes 3214b88c807SRodney W. Grimes 3224b88c807SRodney W. Grimes 3234b88c807SRodney W. Grimes /* 3244b88c807SRodney W. Grimes * Called to exit the shell. 3254b88c807SRodney W. Grimes */ 3264b88c807SRodney W. Grimes 3274b88c807SRodney W. Grimes void 3284b88c807SRodney W. Grimes exitshell(status) { 3294b88c807SRodney W. Grimes struct jmploc loc1, loc2; 3304b88c807SRodney W. Grimes char *p; 3314b88c807SRodney W. Grimes 3324b88c807SRodney W. Grimes TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 3334b88c807SRodney W. Grimes if (setjmp(loc1.loc)) { 3344b88c807SRodney W. Grimes goto l1; 3354b88c807SRodney W. Grimes } 3364b88c807SRodney W. Grimes if (setjmp(loc2.loc)) { 3374b88c807SRodney W. Grimes goto l2; 3384b88c807SRodney W. Grimes } 3394b88c807SRodney W. Grimes handler = &loc1; 3404b88c807SRodney W. Grimes if ((p = trap[0]) != NULL && *p != '\0') { 3414b88c807SRodney W. Grimes trap[0] = NULL; 3424b88c807SRodney W. Grimes evalstring(p); 3434b88c807SRodney W. Grimes } 3444b88c807SRodney W. Grimes l1: handler = &loc2; /* probably unnecessary */ 3454b88c807SRodney W. Grimes flushall(); 3464b88c807SRodney W. Grimes #if JOBS 3474b88c807SRodney W. Grimes setjobctl(0); 3484b88c807SRodney W. Grimes #endif 3494b88c807SRodney W. Grimes l2: _exit(status); 3504b88c807SRodney W. Grimes } 351