11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: releng/10.3/bin/sh/trap.c 287750 2015-09-13 13:43:08Z jilles $"); 401556Srgrimes 4117987Speter#include <signal.h> 4217987Speter#include <unistd.h> 4317987Speter#include <stdlib.h> 4417987Speter 451556Srgrimes#include "shell.h" 461556Srgrimes#include "main.h" 471556Srgrimes#include "nodes.h" /* for other headers */ 481556Srgrimes#include "eval.h" 491556Srgrimes#include "jobs.h" 5017987Speter#include "show.h" 511556Srgrimes#include "options.h" 521556Srgrimes#include "syntax.h" 531556Srgrimes#include "output.h" 541556Srgrimes#include "memalloc.h" 551556Srgrimes#include "error.h" 561556Srgrimes#include "trap.h" 571556Srgrimes#include "mystring.h" 58223060Sjilles#include "builtins.h" 59100578Stjr#include "myhistedit.h" 601556Srgrimes 611556Srgrimes 621556Srgrimes/* 631556Srgrimes * Sigmode records the current value of the signal handlers for the various 641556Srgrimes * modes. A value of zero means that the current handler is not known. 651556Srgrimes * S_HARD_IGN indicates that the signal was ignored on entry to the shell, 661556Srgrimes */ 671556Srgrimes 681556Srgrimes#define S_DFL 1 /* default signal handling (SIG_DFL) */ 691556Srgrimes#define S_CATCH 2 /* signal is caught */ 701556Srgrimes#define S_IGN 3 /* signal is ignored (SIG_IGN) */ 7146684Skris#define S_HARD_IGN 4 /* signal is ignored permanently */ 721556Srgrimes#define S_RESET 5 /* temporary - to reset a hard ignored sig */ 731556Srgrimes 741556Srgrimes 75253658Sjillesstatic char sigmode[NSIG]; /* current value of signal */ 76247206Sjillesvolatile sig_atomic_t pendingsig; /* indicates some signal received */ 77255157Sjillesvolatile sig_atomic_t pendingsig_waitcmd; /* indicates SIGINT/SIGQUIT received */ 7838536Scracauerint in_dotrap; /* do we execute in a trap handler? */ 7938950Scracauerstatic char *volatile trap[NSIG]; /* trap handler commands */ 80157811Sschweikhstatic volatile sig_atomic_t gotsig[NSIG]; 8138521Scracauer /* indicates specified signal received */ 8220902Sstevestatic int ignore_sigchld; /* Used while handling SIGCHLD traps. */ 83217472Sjillesstatic int last_trapsig; 841556Srgrimes 85217175Sjillesstatic int exiting; /* exitshell() has been called */ 86217175Sjillesstatic int exiting_exitstatus; /* value passed to exitshell() */ 87217175Sjilles 88213811Sobrienstatic int getsigaction(int, sig_t *); 8917987Speter 9020902Ssteve 911556Srgrimes/* 9220902Ssteve * Map a string to a signal number. 93125727Snjl * 94125727Snjl * Note: the signal number may exceed NSIG. 9520902Ssteve */ 96213811Sobrienstatic int 9790111Simpsigstring_to_signum(char *sig) 9820902Ssteve{ 9920902Ssteve 10020902Ssteve if (is_number(sig)) { 10120902Ssteve int signo; 10220902Ssteve 10320902Ssteve signo = atoi(sig); 10420902Ssteve return ((signo >= 0 && signo < NSIG) ? signo : (-1)); 105218285Sjilles } else if (strcasecmp(sig, "EXIT") == 0) { 10620902Ssteve return (0); 10720902Ssteve } else { 10820902Ssteve int n; 10920902Ssteve 110218285Sjilles if (strncasecmp(sig, "SIG", 3) == 0) 11120902Ssteve sig += 3; 112125155Snjl for (n = 1; n < sys_nsig; n++) 113125728Snjl if (sys_signame[n] && 114125728Snjl strcasecmp(sys_signame[n], sig) == 0) 11520902Ssteve return (n); 11620902Ssteve } 11720902Ssteve return (-1); 11820902Ssteve} 11920902Ssteve 12020902Ssteve 12120902Ssteve/* 12220902Ssteve * Print a list of valid signal names. 12320902Ssteve */ 124213811Sobrienstatic void 12590111Simpprintsignals(void) 12620902Ssteve{ 127125727Snjl int n, outlen; 12820902Ssteve 129125727Snjl outlen = 0; 130125155Snjl for (n = 1; n < sys_nsig; n++) { 131125727Snjl if (sys_signame[n]) { 132125727Snjl out1fmt("%s", sys_signame[n]); 133125727Snjl outlen += strlen(sys_signame[n]); 134125727Snjl } else { 135125727Snjl out1fmt("%d", n); 136125727Snjl outlen += 3; /* good enough */ 137125727Snjl } 138125727Snjl ++outlen; 139217425Sjilles if (outlen > 71 || n == sys_nsig - 1) { 14020902Ssteve out1str("\n"); 141125727Snjl outlen = 0; 142125727Snjl } else { 14320902Ssteve out1c(' '); 144125727Snjl } 14520902Ssteve } 14620902Ssteve} 14720902Ssteve 14820902Ssteve 14920902Ssteve/* 1501556Srgrimes * The trap builtin. 1511556Srgrimes */ 15217987Speterint 153248980Sjillestrapcmd(int argc __unused, char **argv) 15417987Speter{ 1551556Srgrimes char *action; 1561556Srgrimes int signo; 157199641Sjilles int errors = 0; 158217461Sjilles int i; 1591556Srgrimes 160217461Sjilles while ((i = nextopt("l")) != '\0') { 161217461Sjilles switch (i) { 162217461Sjilles case 'l': 163217461Sjilles printsignals(); 164217461Sjilles return (0); 165217461Sjilles } 166217461Sjilles } 167217461Sjilles argv = argptr; 168217461Sjilles 169217461Sjilles if (*argv == NULL) { 170125155Snjl for (signo = 0 ; signo < sys_nsig ; signo++) { 171125727Snjl if (signo < NSIG && trap[signo] != NULL) { 172153244Sstefanf out1str("trap -- "); 173153244Sstefanf out1qstr(trap[signo]); 174125727Snjl if (signo == 0) { 175218285Sjilles out1str(" EXIT\n"); 176125727Snjl } else if (sys_signame[signo]) { 177153244Sstefanf out1fmt(" %s\n", sys_signame[signo]); 178125727Snjl } else { 179153244Sstefanf out1fmt(" %d\n", signo); 180125727Snjl } 181125727Snjl } 1821556Srgrimes } 1831556Srgrimes return 0; 1841556Srgrimes } 18520902Ssteve action = NULL; 18620902Ssteve if (*argv && sigstring_to_signum(*argv) == -1) { 187217461Sjilles if (strcmp(*argv, "-") == 0) 188217461Sjilles argv++; 189217461Sjilles else { 19020902Ssteve action = *argv; 19120902Ssteve argv++; 19220902Ssteve } 19320902Ssteve } 194230117Sjilles for (; *argv; argv++) { 195199641Sjilles if ((signo = sigstring_to_signum(*argv)) == -1) { 196216622Sjilles warning("bad signal %s", *argv); 197199641Sjilles errors = 1; 198230117Sjilles continue; 199199641Sjilles } 2001556Srgrimes INTOFF; 2011556Srgrimes if (action) 2021556Srgrimes action = savestr(action); 2031556Srgrimes if (trap[signo]) 2041556Srgrimes ckfree(trap[signo]); 2051556Srgrimes trap[signo] = action; 2061556Srgrimes if (signo != 0) 2071556Srgrimes setsignal(signo); 2081556Srgrimes INTON; 2091556Srgrimes } 210199641Sjilles return errors; 2111556Srgrimes} 2121556Srgrimes 2131556Srgrimes 2141556Srgrimes/* 2151556Srgrimes * Clear traps on a fork. 2161556Srgrimes */ 2171556Srgrimesvoid 21890111Simpclear_traps(void) 21920902Ssteve{ 22038950Scracauer char *volatile *tp; 2211556Srgrimes 22220902Ssteve for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 2231556Srgrimes if (*tp && **tp) { /* trap not NULL or SIG_IGN */ 2241556Srgrimes INTOFF; 2251556Srgrimes ckfree(*tp); 2261556Srgrimes *tp = NULL; 2271556Srgrimes if (tp != &trap[0]) 2281556Srgrimes setsignal(tp - trap); 2291556Srgrimes INTON; 2301556Srgrimes } 2311556Srgrimes } 2321556Srgrimes} 2331556Srgrimes 2341556Srgrimes 2351556Srgrimes/* 236194127Sjilles * Check if we have any traps enabled. 237194127Sjilles */ 238194127Sjillesint 239194127Sjilleshave_traps(void) 240194127Sjilles{ 241194127Sjilles char *volatile *tp; 242194127Sjilles 243194127Sjilles for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { 244194127Sjilles if (*tp && **tp) /* trap not NULL or SIG_IGN */ 245194127Sjilles return 1; 246194127Sjilles } 247194127Sjilles return 0; 248194127Sjilles} 249194127Sjilles 250194127Sjilles/* 2511556Srgrimes * Set the signal handler for the specified signal. The routine figures 2521556Srgrimes * out what it should be set to. 2531556Srgrimes */ 25431098Sbdevoid 25590111Simpsetsignal(int signo) 25617987Speter{ 2571556Srgrimes int action; 258199205Sjilles sig_t sigact = SIG_DFL; 259199205Sjilles struct sigaction sa; 2601556Srgrimes char *t; 2611556Srgrimes 2621556Srgrimes if ((t = trap[signo]) == NULL) 2631556Srgrimes action = S_DFL; 2641556Srgrimes else if (*t != '\0') 2651556Srgrimes action = S_CATCH; 2661556Srgrimes else 2671556Srgrimes action = S_IGN; 26838521Scracauer if (action == S_DFL) { 2691556Srgrimes switch (signo) { 2701556Srgrimes case SIGINT: 27138521Scracauer action = S_CATCH; 2721556Srgrimes break; 2731556Srgrimes case SIGQUIT: 2741556Srgrimes#ifdef DEBUG 2751556Srgrimes { 2761556Srgrimes extern int debug; 2771556Srgrimes 2781556Srgrimes if (debug) 2791556Srgrimes break; 2801556Srgrimes } 2811556Srgrimes#endif 28238535Scracauer action = S_CATCH; 28338521Scracauer break; 2841556Srgrimes case SIGTERM: 28538521Scracauer if (rootshell && iflag) 2861556Srgrimes action = S_IGN; 2871556Srgrimes break; 2881556Srgrimes#if JOBS 2891556Srgrimes case SIGTSTP: 2901556Srgrimes case SIGTTOU: 29138521Scracauer if (rootshell && mflag) 2921556Srgrimes action = S_IGN; 2931556Srgrimes break; 2941556Srgrimes#endif 2951556Srgrimes } 2961556Srgrimes } 29717987Speter 29820902Ssteve t = &sigmode[signo]; 2998855Srgrimes if (*t == 0) { 3008855Srgrimes /* 3018855Srgrimes * current setting unknown 3021556Srgrimes */ 30317987Speter if (!getsigaction(signo, &sigact)) { 30417987Speter /* 30517987Speter * Pretend it worked; maybe we should give a warning 30617987Speter * here, but other shells don't. We don't alter 30717987Speter * sigmode, so that we retry every time. 30817987Speter */ 30931098Sbde return; 31017987Speter } 3111556Srgrimes if (sigact == SIG_IGN) { 3128855Srgrimes if (mflag && (signo == SIGTSTP || 3131556Srgrimes signo == SIGTTIN || signo == SIGTTOU)) { 3141556Srgrimes *t = S_IGN; /* don't hard ignore these */ 3151556Srgrimes } else 3161556Srgrimes *t = S_HARD_IGN; 3171556Srgrimes } else { 3181556Srgrimes *t = S_RESET; /* force to be set */ 3191556Srgrimes } 3201556Srgrimes } 3211556Srgrimes if (*t == S_HARD_IGN || *t == action) 32231098Sbde return; 3231556Srgrimes switch (action) { 3241556Srgrimes case S_DFL: sigact = SIG_DFL; break; 3251556Srgrimes case S_CATCH: sigact = onsig; break; 3261556Srgrimes case S_IGN: sigact = SIG_IGN; break; 3271556Srgrimes } 3281556Srgrimes *t = action; 329199205Sjilles sa.sa_handler = sigact; 330199205Sjilles sa.sa_flags = 0; 331199205Sjilles sigemptyset(&sa.sa_mask); 332199205Sjilles sigaction(signo, &sa, NULL); 3331556Srgrimes} 3341556Srgrimes 33520902Ssteve 3361556Srgrimes/* 3371556Srgrimes * Return the current setting for sig w/o changing it. 3381556Srgrimes */ 339213811Sobrienstatic int 34090111Simpgetsigaction(int signo, sig_t *sigact) 34117987Speter{ 3421556Srgrimes struct sigaction sa; 3431556Srgrimes 3441556Srgrimes if (sigaction(signo, (struct sigaction *)0, &sa) == -1) 34517987Speter return 0; 34617987Speter *sigact = (sig_t) sa.sa_handler; 34717987Speter return 1; 3481556Srgrimes} 3491556Srgrimes 35020902Ssteve 3511556Srgrimes/* 3521556Srgrimes * Ignore a signal. 3531556Srgrimes */ 3541556Srgrimesvoid 35590111Simpignoresig(int signo) 35617987Speter{ 35720902Ssteve 358262951Sjmmv if (sigmode[signo] == 0) 359262951Sjmmv setsignal(signo); 36020902Ssteve if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { 3611556Srgrimes signal(signo, SIG_IGN); 362262951Sjmmv sigmode[signo] = S_IGN; 3631556Srgrimes } 3641556Srgrimes} 3651556Srgrimes 3661556Srgrimes 367238888Sjillesint 368238888Sjillesissigchldtrapped(void) 369238888Sjilles{ 370238888Sjilles 371238888Sjilles return (trap[SIGCHLD] != NULL && *trap[SIGCHLD] != '\0'); 372238888Sjilles} 373238888Sjilles 374238888Sjilles 3751556Srgrimes/* 3761556Srgrimes * Signal handler. 3771556Srgrimes */ 3781556Srgrimesvoid 37990111Simponsig(int signo) 38017987Speter{ 38131098Sbde 3821556Srgrimes if (signo == SIGINT && trap[SIGINT] == NULL) { 3831556Srgrimes onint(); 3841556Srgrimes return; 3851556Srgrimes } 38638950Scracauer 38738950Scracauer /* If we are currently in a wait builtin, prepare to break it */ 388255157Sjilles if (signo == SIGINT || signo == SIGQUIT) 389255157Sjilles pendingsig_waitcmd = signo; 390100578Stjr 391247206Sjilles if (trap[signo] != NULL && trap[signo][0] != '\0' && 392247206Sjilles (signo != SIGCHLD || !ignore_sigchld)) { 393247206Sjilles gotsig[signo] = 1; 394247206Sjilles pendingsig = signo; 395247206Sjilles } 3961556Srgrimes} 3971556Srgrimes 3981556Srgrimes 3991556Srgrimes/* 4001556Srgrimes * Called to execute a trap. Perhaps we should avoid entering new trap 4011556Srgrimes * handlers while we are executing a trap handler. 4021556Srgrimes */ 4031556Srgrimesvoid 40490111Simpdotrap(void) 40520902Ssteve{ 4061556Srgrimes int i; 407230212Sdumbbell int savestatus, prev_evalskip, prev_skipcount; 4081556Srgrimes 40938521Scracauer in_dotrap++; 4101556Srgrimes for (;;) { 411247206Sjilles pendingsig = 0; 412255157Sjilles pendingsig_waitcmd = 0; 41320902Ssteve for (i = 1; i < NSIG; i++) { 41420902Ssteve if (gotsig[i]) { 41520902Ssteve gotsig[i] = 0; 41620902Ssteve if (trap[i]) { 41720902Ssteve /* 418125728Snjl * Ignore SIGCHLD to avoid infinite 419125728Snjl * recursion if the trap action does 420125728Snjl * a fork. 42120902Ssteve */ 42220902Ssteve if (i == SIGCHLD) 42320902Ssteve ignore_sigchld++; 424230212Sdumbbell 425230212Sdumbbell /* 426230212Sdumbbell * Backup current evalskip 427230212Sdumbbell * state and reset it before 428230212Sdumbbell * executing a trap, so that the 429230212Sdumbbell * trap is not disturbed by an 430230212Sdumbbell * ongoing break/continue/return 431230212Sdumbbell * statement. 432230212Sdumbbell */ 433230212Sdumbbell prev_evalskip = evalskip; 434230212Sdumbbell prev_skipcount = skipcount; 435230212Sdumbbell evalskip = 0; 436230212Sdumbbell 437217472Sjilles last_trapsig = i; 43820902Ssteve savestatus = exitstatus; 439193169Sstefanf evalstring(trap[i], 0); 440230212Sdumbbell 441230212Sdumbbell /* 442230212Sdumbbell * If such a command was not 443230212Sdumbbell * already in progress, allow a 444230212Sdumbbell * break/continue/return in the 445230212Sdumbbell * trap action to have an effect 446230212Sdumbbell * outside of it. 447230212Sdumbbell */ 448247720Sjilles if (evalskip == 0 || 449247720Sjilles prev_evalskip != 0) { 450230212Sdumbbell evalskip = prev_evalskip; 451230212Sdumbbell skipcount = prev_skipcount; 452247720Sjilles exitstatus = savestatus; 453230212Sdumbbell } 454230212Sdumbbell 45520902Ssteve if (i == SIGCHLD) 45620902Ssteve ignore_sigchld--; 45720902Ssteve } 4581556Srgrimes break; 45920902Ssteve } 4601556Srgrimes } 46120902Ssteve if (i >= NSIG) 46220902Ssteve break; 4631556Srgrimes } 46438521Scracauer in_dotrap--; 4651556Srgrimes} 4661556Srgrimes 4671556Srgrimes 4681556Srgrimes/* 4691556Srgrimes * Controls whether the shell is interactive or not. 4701556Srgrimes */ 4711556Srgrimesvoid 47290111Simpsetinteractive(int on) 47317987Speter{ 47438521Scracauer static int is_interactive = -1; 4751556Srgrimes 4761556Srgrimes if (on == is_interactive) 4771556Srgrimes return; 4781556Srgrimes setsignal(SIGINT); 4791556Srgrimes setsignal(SIGQUIT); 4801556Srgrimes setsignal(SIGTERM); 4811556Srgrimes is_interactive = on; 4821556Srgrimes} 4831556Srgrimes 4841556Srgrimes 4851556Srgrimes/* 4861556Srgrimes * Called to exit the shell. 4871556Srgrimes */ 4881556Srgrimesvoid 48990111Simpexitshell(int status) 49017987Speter{ 491217175Sjilles TRACE(("exitshell(%d) pid=%d\n", status, getpid())); 492217175Sjilles exiting = 1; 493217175Sjilles exiting_exitstatus = status; 494217175Sjilles exitshell_savedstatus(); 495217175Sjilles} 496217175Sjilles 497217175Sjillesvoid 498217175Sjillesexitshell_savedstatus(void) 499217175Sjilles{ 5001556Srgrimes struct jmploc loc1, loc2; 5011556Srgrimes char *p; 502217472Sjilles int sig = 0; 503217472Sjilles sigset_t sigs; 5041556Srgrimes 505217472Sjilles if (!exiting) { 506217472Sjilles if (in_dotrap && last_trapsig) { 507217472Sjilles sig = last_trapsig; 508217472Sjilles exiting_exitstatus = sig + 128; 509217472Sjilles } else 510217472Sjilles exiting_exitstatus = oexitstatus; 511217472Sjilles } 512217175Sjilles exitstatus = oexitstatus = exiting_exitstatus; 513287750Sjilles if (!setjmp(loc1.loc)) { 514287750Sjilles handler = &loc1; 515287750Sjilles if ((p = trap[0]) != NULL && *p != '\0') { 516287750Sjilles /* 517287750Sjilles * Reset evalskip, or the trap on EXIT could be 518287750Sjilles * interrupted if the last command was a "return". 519287750Sjilles */ 520287750Sjilles evalskip = 0; 521287750Sjilles trap[0] = NULL; 522287750Sjilles evalstring(p, 0); 523287750Sjilles } 5241556Srgrimes } 525287750Sjilles if (!setjmp(loc2.loc)) { 526287750Sjilles handler = &loc2; /* probably unnecessary */ 527287750Sjilles flushall(); 5281556Srgrimes#if JOBS 529287750Sjilles setjobctl(0); 5301556Srgrimes#endif 531287750Sjilles } 532217472Sjilles if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN && 533217472Sjilles sig != SIGTTOU) { 534217472Sjilles signal(sig, SIG_DFL); 535217472Sjilles sigemptyset(&sigs); 536217472Sjilles sigaddset(&sigs, sig); 537217472Sjilles sigprocmask(SIG_UNBLOCK, &sigs, NULL); 538217472Sjilles kill(getpid(), sig); 539217472Sjilles /* If the default action is to ignore, fall back to _exit(). */ 540217472Sjilles } 541217472Sjilles _exit(exiting_exitstatus); 5421556Srgrimes} 543