2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid
[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/trap.c,v 1.29 2004/04/06 20:06:51 markm Exp $");
49 #include "nodes.h" /* for other headers */
60 #if !defined(NO_HISTORY) && !defined(EDITLINE)
61 #include "myhistedit.h"
66 #if !defined(__NBSD_LIBC)
67 #define NO_SIGINTERRUPT
69 #define NO_SYS_SIGNAME
70 #define NO_SYS_SIGLIST
73 typedef void (*sig_T
)(int);
76 * Sigmode records the current value of the signal handlers for the various
77 * modes. A value of zero means that the current handler is not known.
78 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
81 #define S_DFL 1 /* default signal handling (SIG_DFL) */
82 #define S_CATCH 2 /* signal is caught */
83 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
84 #define S_HARD_IGN 4 /* signal is ignored permanently */
85 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
88 MKINIT
char sigmode
[_NSIG
]; /* current value of signal */
89 int pendingsigs
; /* indicates some signal received */
90 int is_interactive
= -1; /* Shell is interactive */
91 int in_dotrap
; /* do we execute in a trap handler? */
92 static char *volatile trap
[_NSIG
]; /* trap handler commands */
93 static volatile sig_atomic_t gotsig
[_NSIG
];
94 /* indicates specified signal received */
95 static int ignore_sigchld
; /* Used while handling SIGCHLD traps. */
96 volatile sig_atomic_t gotwinch
;
98 static int sigstring_to_signum (char *);
99 static void printsignals (void);
100 static int getsigaction(int, sig_T
*);
101 static void onsig (int);
102 #ifdef NO_SIGINTERRUPT
103 static int siginterrupt (int,int);
105 static char *strsigname (int);
109 * Map a string to a signal number.
112 sigstring_to_signum(char *sig
)
115 if (is_number(sig
)) {
119 return ((signo
>= 0 && signo
< _NSIG
) ? signo
: (-1));
120 } else if (strcasecmp(sig
, "exit") == 0) {
125 if (strncasecmp(sig
, "sig", 3) == 0)
127 for (n
= 1; n
< _NSIG
; n
++)
128 if (strcasecmp(strsigname(n
), sig
) == 0)
136 * Print a list of valid signal names.
144 for (n
= 1; n
< _NSIG
; n
++) {
146 out1fmt("%s", strsigname(n
));
147 outlen
+= strlen(strsigname(n
));
150 outlen
+= 3; /* good enough */
153 if (outlen
> 70 || n
== _NSIG
- 1) {
167 trapcmd(int argc
, char **argv
)
173 for (signo
= 0 ; signo
< _NSIG
; signo
++) {
174 if (trap
[signo
] != NULL
) {
176 out1fmt("trap -- '%s' %s\n",
177 trap
[signo
], "exit");
178 } else if (strsigname(signo
)) {
179 out1fmt("trap -- '%s' %s\n",
180 trap
[signo
], strsigname(signo
));
182 out1fmt("trap -- '%s' %d\n",
190 if (*++argv
&& strcmp(*argv
, "--") == 0)
192 if (*argv
&& sigstring_to_signum(*argv
) == -1) {
193 if ((*argv
)[0] != '-') {
196 } else if ((*argv
)[1] == '\0') {
198 } else if ((*argv
)[1] == 'l' && (*argv
)[2] == '\0') {
202 error("bad option %s", *argv
);
206 if ((signo
= sigstring_to_signum(*argv
)) == -1)
207 error("bad signal %s", *argv
);
210 action
= savestr(action
);
213 trap
[signo
] = action
;
224 * Clear traps on a fork.
231 for (tp
= trap
; tp
<= &trap
[_NSIG
- 1] ; tp
++) {
232 if (*tp
&& **tp
) { /* trap not NULL or SIG_IGN */
237 setsignal(tp
- trap
);
245 * Set the signal handler for the specified signal. The routine figures
246 * out what it should be set to.
252 sig_T sig
, sigact
= SIG_DFL
;
255 if ((t
= trap
[signo
]) == NULL
)
261 if (action
== S_DFL
) {
278 if (rootshell
&& iflag
)
284 if (rootshell
&& mflag
)
290 if (rootshell
&& iflag
)
300 * current setting unknown
302 if (!getsigaction(signo
, &sigact
)) {
304 * Pretend it worked; maybe we should give a warning
305 * here, but other shells don't. We don't alter
306 * sigmode, so that we retry every time.
310 if (sigact
== SIG_IGN
) {
311 if (mflag
&& (signo
== SIGTSTP
||
312 signo
== SIGTTIN
|| signo
== SIGTTOU
)) {
313 *t
= S_IGN
; /* don't hard ignore these */
317 *t
= S_RESET
; /* force to be set */
320 if (*t
== S_HARD_IGN
|| *t
== action
)
323 case S_DFL
: sigact
= SIG_DFL
; break;
324 case S_CATCH
: sigact
= onsig
; break;
325 case S_IGN
: sigact
= SIG_IGN
; break;
328 sig
= signal(signo
, sigact
);
329 if (sig
!= SIG_ERR
&& action
== S_CATCH
)
330 siginterrupt(signo
, 1);
335 * Return the current setting for sig w/o changing it.
338 getsigaction(int signo
, sig_T
*sigact
)
342 if (sigaction(signo
, (struct sigaction
*)0, &sa
) == -1)
344 *sigact
= (sig_T
) sa
.sa_handler
;
356 if (sigmode
[signo
] != S_IGN
&& sigmode
[signo
] != S_HARD_IGN
) {
357 signal(signo
, SIG_IGN
);
359 sigmode
[signo
] = S_HARD_IGN
;
371 for (sm
= sigmode
; sm
< sigmode
+ _NSIG
; sm
++) {
387 signal(signo
, onsig
);
389 if (signo
== SIGINT
&& trap
[SIGINT
] == NULL
) {
394 if (signo
!= SIGCHLD
|| !ignore_sigchld
)
398 /* If we are currently in a wait builtin, prepare to break it */
399 if ((signo
== SIGINT
|| signo
== SIGQUIT
) && in_waitcmd
!= 0)
402 * If a trap is set, not ignored and not the null command, we need
403 * to make sure traps are executed even when a child blocks signals.
406 trap
[signo
] != NULL
&&
407 ! trap
[signo
][0] == '\0' &&
408 ! (trap
[signo
][0] == ':' && trap
[signo
][1] == '\0'))
412 if (signo
== SIGWINCH
)
419 * Called to execute a trap. Perhaps we should avoid entering new trap
420 * handlers while we are executing a trap handler.
430 for (i
= 1; i
< _NSIG
; i
++) {
435 * Ignore SIGCHLD to avoid infinite
436 * recursion if the trap action does
441 savestatus
= exitstatus
;
443 exitstatus
= savestatus
;
459 * Controls whether the shell is interactive or not.
462 setinteractive(int on
)
464 if (on
== is_interactive
)
477 * Called to exit the shell.
480 exitshell(int status
)
482 struct jmploc loc1
, loc2
;
485 TRACE(("exitshell(%d) pid=%d\n", status
, getpid()));
486 if (setjmp(loc1
.loc
)) {
489 if (setjmp(loc2
.loc
)) {
493 if ((p
= trap
[0]) != NULL
&& *p
!= '\0') {
497 l1
: handler
= &loc2
; /* probably unnecessary */
505 #ifdef NO_SIGINTERRUPT
506 static int siginterrupt(sig
, flag
)
514 #ifdef NO_SYS_SIGNAME
515 static char *strsigname(sig
)
520 case 0: return "Signal 0"; /* 0 */
521 case SIGHUP
: return "hup"; /* 1 */
522 case SIGINT
: return "int"; /* 2 */
523 case SIGQUIT
: return "quit"; /* 3 */
524 case SIGILL
: return "ill"; /* 4 */
525 case SIGTRAP
: return "trap"; /* 5 */
526 case SIGABRT
: return "abrt"; /* 6 */
528 case SIGEMT
: return "emt"; /* 7 */
530 case SIGBUS
: return "bus"; /* 7 */
532 case SIGFPE
: return "fpe"; /* 8 */
533 case SIGKILL
: return "kill"; /* 9 */
534 case SIGUSR1
: return "usr1"; /* 10 */
535 case SIGSEGV
: return "segv"; /* 11 */
536 case SIGUSR2
: return "usr2"; /* 12 */
537 case SIGPIPE
: return "pipe"; /* 13 */
538 case SIGALRM
: return "alrm"; /* 14 */
539 case SIGTERM
: return "term"; /* 15 */
541 case 16: return "Signal 16"; /* 16 */
543 case SIGEMT
: return "emt"; /* 16 */
545 case SIGCHLD
: return "chld"; /* 17 */
546 case SIGCONT
: return "cont"; /* 18 */
547 case SIGSTOP
: return "stop"; /* 19 */
548 case SIGTSTP
: return "tstp"; /* 20 */
549 case SIGTTIN
: return "ttin"; /* 21 */
550 case SIGTTOU
: return "ttou"; /* 22 */
551 case SIGWINCH
: return "winch"; /* 23 */
552 case SIGVTALRM
: return "vtalrm"; /* 24 */
553 case SIGPROF
: return "prof"; /* 25 */
555 case SIGFPEMU
: return "fpemu"; /* 30 */
557 default: return "Signal n";
561 static char *strsigname(sig
)
564 return (char *)sys_signame
[sig
];
568 #ifdef NO_SYS_SIGLIST
569 #include "signames.h"
570 char *strsiglist(sig
)
578 char *strsiglist(sig
)
581 return (char *)sys_siglist
[sig
];
586 * $PchId: trap.c,v 1.7 2006/05/23 11:56:21 philip Exp $