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 #define NO_SIGINTERRUPT
67 #define NO_SYS_SIGNAME
68 #define NO_SYS_SIGLIST
72 typedef void (*sig_T
)(int);
75 * Sigmode records the current value of the signal handlers for the various
76 * modes. A value of zero means that the current handler is not known.
77 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
80 #define S_DFL 1 /* default signal handling (SIG_DFL) */
81 #define S_CATCH 2 /* signal is caught */
82 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
83 #define S_HARD_IGN 4 /* signal is ignored permanently */
84 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
87 MKINIT
char sigmode
[_NSIG
]; /* current value of signal */
88 int pendingsigs
; /* indicates some signal received */
89 int is_interactive
= -1; /* Shell is interactive */
90 int in_dotrap
; /* do we execute in a trap handler? */
91 static char *volatile trap
[_NSIG
]; /* trap handler commands */
92 static volatile sig_atomic_t gotsig
[_NSIG
];
93 /* indicates specified signal received */
94 static int ignore_sigchld
; /* Used while handling SIGCHLD traps. */
95 volatile sig_atomic_t gotwinch
;
97 static int sigstring_to_signum (char *);
98 static void printsignals (void);
99 static int getsigaction(int, sig_T
*);
100 static void onsig (int);
101 #ifdef NO_SIGINTERRUPT
102 static int siginterrupt (int,int);
104 static char *strsigname (int);
108 * Map a string to a signal number.
111 sigstring_to_signum(char *sig
)
114 if (is_number(sig
)) {
118 return ((signo
>= 0 && signo
< _NSIG
) ? signo
: (-1));
119 } else if (strcasecmp(sig
, "exit") == 0) {
124 if (strncasecmp(sig
, "sig", 3) == 0)
126 for (n
= 1; n
< _NSIG
; n
++)
127 if (strcasecmp(strsigname(n
), sig
) == 0)
135 * Print a list of valid signal names.
143 for (n
= 1; n
< _NSIG
; n
++) {
145 out1fmt("%s", strsigname(n
));
146 outlen
+= strlen(strsigname(n
));
149 outlen
+= 3; /* good enough */
152 if (outlen
> 70 || n
== _NSIG
- 1) {
166 trapcmd(int argc
, char **argv
)
172 for (signo
= 0 ; signo
< _NSIG
; signo
++) {
173 if (trap
[signo
] != NULL
) {
175 out1fmt("trap -- '%s' %s\n",
176 trap
[signo
], "exit");
177 } else if (strsigname(signo
)) {
178 out1fmt("trap -- '%s' %s\n",
179 trap
[signo
], strsigname(signo
));
181 out1fmt("trap -- '%s' %d\n",
189 if (*++argv
&& strcmp(*argv
, "--") == 0)
191 if (*argv
&& sigstring_to_signum(*argv
) == -1) {
192 if ((*argv
)[0] != '-') {
195 } else if ((*argv
)[1] == '\0') {
197 } else if ((*argv
)[1] == 'l' && (*argv
)[2] == '\0') {
201 error("bad option %s", *argv
);
205 if ((signo
= sigstring_to_signum(*argv
)) == -1)
206 error("bad signal %s", *argv
);
209 action
= savestr(action
);
212 trap
[signo
] = action
;
223 * Clear traps on a fork.
230 for (tp
= trap
; tp
<= &trap
[_NSIG
- 1] ; tp
++) {
231 if (*tp
&& **tp
) { /* trap not NULL or SIG_IGN */
236 setsignal(tp
- trap
);
244 * Set the signal handler for the specified signal. The routine figures
245 * out what it should be set to.
251 sig_T sig
, sigact
= SIG_DFL
;
254 if ((t
= trap
[signo
]) == NULL
)
260 if (action
== S_DFL
) {
277 if (rootshell
&& iflag
)
283 if (rootshell
&& mflag
)
289 if (rootshell
&& iflag
)
299 * current setting unknown
301 if (!getsigaction(signo
, &sigact
)) {
303 * Pretend it worked; maybe we should give a warning
304 * here, but other shells don't. We don't alter
305 * sigmode, so that we retry every time.
309 if (sigact
== SIG_IGN
) {
310 if (mflag
&& (signo
== SIGTSTP
||
311 signo
== SIGTTIN
|| signo
== SIGTTOU
)) {
312 *t
= S_IGN
; /* don't hard ignore these */
316 *t
= S_RESET
; /* force to be set */
319 if (*t
== S_HARD_IGN
|| *t
== action
)
322 case S_DFL
: sigact
= SIG_DFL
; break;
323 case S_CATCH
: sigact
= onsig
; break;
324 case S_IGN
: sigact
= SIG_IGN
; break;
327 sig
= signal(signo
, sigact
);
328 if (sig
!= SIG_ERR
&& action
== S_CATCH
)
329 siginterrupt(signo
, 1);
334 * Return the current setting for sig w/o changing it.
337 getsigaction(int signo
, sig_T
*sigact
)
341 if (sigaction(signo
, (struct sigaction
*)0, &sa
) == -1)
343 *sigact
= (sig_T
) sa
.sa_handler
;
355 if (sigmode
[signo
] != S_IGN
&& sigmode
[signo
] != S_HARD_IGN
) {
356 signal(signo
, SIG_IGN
);
358 sigmode
[signo
] = S_HARD_IGN
;
370 for (sm
= sigmode
; sm
< sigmode
+ _NSIG
; sm
++) {
386 signal(signo
, onsig
);
388 if (signo
== SIGINT
&& trap
[SIGINT
] == NULL
) {
393 if (signo
!= SIGCHLD
|| !ignore_sigchld
)
397 /* If we are currently in a wait builtin, prepare to break it */
398 if ((signo
== SIGINT
|| signo
== SIGQUIT
) && in_waitcmd
!= 0)
401 * If a trap is set, not ignored and not the null command, we need
402 * to make sure traps are executed even when a child blocks signals.
405 trap
[signo
] != NULL
&&
406 ! trap
[signo
][0] == '\0' &&
407 ! (trap
[signo
][0] == ':' && trap
[signo
][1] == '\0'))
411 if (signo
== SIGWINCH
)
418 * Called to execute a trap. Perhaps we should avoid entering new trap
419 * handlers while we are executing a trap handler.
429 for (i
= 1; i
< _NSIG
; i
++) {
434 * Ignore SIGCHLD to avoid infinite
435 * recursion if the trap action does
440 savestatus
= exitstatus
;
442 exitstatus
= savestatus
;
458 * Controls whether the shell is interactive or not.
461 setinteractive(int on
)
463 if (on
== is_interactive
)
476 * Called to exit the shell.
479 exitshell(int status
)
481 struct jmploc loc1
, loc2
;
484 TRACE(("exitshell(%d) pid=%d\n", status
, getpid()));
485 if (setjmp(loc1
.loc
)) {
488 if (setjmp(loc2
.loc
)) {
492 if ((p
= trap
[0]) != NULL
&& *p
!= '\0') {
496 l1
: handler
= &loc2
; /* probably unnecessary */
504 #ifdef NO_SIGINTERRUPT
505 static int siginterrupt(sig
, flag
)
513 #ifdef NO_SYS_SIGNAME
514 static char *strsigname(sig
)
519 case 0: return "Signal 0"; /* 0 */
520 case SIGHUP
: return "hup"; /* 1 */
521 case SIGINT
: return "int"; /* 2 */
522 case SIGQUIT
: return "quit"; /* 3 */
523 case SIGILL
: return "ill"; /* 4 */
524 case SIGTRAP
: return "trap"; /* 5 */
525 case SIGABRT
: return "abrt"; /* 6 */
527 case SIGEMT
: return "emt"; /* 7 */
529 case SIGBUS
: return "bus"; /* 7 */
531 case SIGFPE
: return "fpe"; /* 8 */
532 case SIGKILL
: return "kill"; /* 9 */
533 case SIGUSR1
: return "usr1"; /* 10 */
534 case SIGSEGV
: return "segv"; /* 11 */
535 case SIGUSR2
: return "usr2"; /* 12 */
536 case SIGPIPE
: return "pipe"; /* 13 */
537 case SIGALRM
: return "alrm"; /* 14 */
538 case SIGTERM
: return "term"; /* 15 */
540 case 16: return "Signal 16"; /* 16 */
542 case SIGEMT
: return "emt"; /* 16 */
544 case SIGCHLD
: return "chld"; /* 17 */
545 case SIGCONT
: return "cont"; /* 18 */
546 case SIGSTOP
: return "stop"; /* 19 */
547 case SIGTSTP
: return "tstp"; /* 20 */
548 case SIGTTIN
: return "ttin"; /* 21 */
549 case SIGTTOU
: return "ttou"; /* 22 */
550 case SIGWINCH
: return "winch"; /* 23 */
551 case SIGVTALRM
: return "vtalrm"; /* 24 */
552 case SIGPROF
: return "prof"; /* 25 */
554 case SIGFPEMU
: return "fpemu"; /* 30 */
556 default: return "Signal n";
560 static char *strsigname(sig
)
563 return sys_signame
[sig
];
567 #ifdef NO_SYS_SIGLIST
568 #include "signames.h"
569 char *strsiglist(sig
)
577 char *strsiglist(sig
)
580 return sys_siglist
[sig
];
585 * $PchId: trap.c,v 1.7 2006/05/23 11:56:21 philip Exp $