]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/trap.c
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";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
47 #include "nodes.h" /* for other headers */
59 #include "myhistedit.h"
62 #define sys_nsig (NSIG)
63 #endif /* __APPLE__ */
66 * Sigmode records the current value of the signal handlers for the various
67 * modes. A value of zero means that the current handler is not known.
68 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
71 #define S_DFL 1 /* default signal handling (SIG_DFL) */
72 #define S_CATCH 2 /* signal is caught */
73 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
74 #define S_HARD_IGN 4 /* signal is ignored permanently */
75 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
78 static char sigmode
[NSIG
]; /* current value of signal */
79 volatile sig_atomic_t pendingsig
; /* indicates some signal received */
80 volatile sig_atomic_t pendingsig_waitcmd
; /* indicates wait builtin should be interrupted */
81 static int in_dotrap
; /* do we execute in a trap handler? */
82 static char *volatile trap
[NSIG
]; /* trap handler commands */
83 static volatile sig_atomic_t gotsig
[NSIG
];
84 /* indicates specified signal received */
85 static int ignore_sigchld
; /* Used while handling SIGCHLD traps. */
86 static int last_trapsig
;
88 static int exiting
; /* exitshell() has been called */
89 static int exiting_exitstatus
; /* value passed to exitshell() */
91 static int getsigaction(int, sig_t
*);
95 * Map a string to a signal number.
97 * Note: the signal number may exceed NSIG.
100 sigstring_to_signum(char *sig
)
103 if (is_number(sig
)) {
107 return ((signo
>= 0 && signo
< NSIG
) ? signo
: (-1));
108 } else if (strcasecmp(sig
, "EXIT") == 0) {
113 if (strncasecmp(sig
, "SIG", 3) == 0)
115 for (n
= 1; n
< sys_nsig
; n
++)
116 if (sys_signame
[n
] &&
117 strcasecmp(sys_signame
[n
], sig
) == 0)
125 * Print a list of valid signal names.
133 for (n
= 1; n
< sys_nsig
; n
++) {
134 if (sys_signame
[n
]) {
135 out1fmt("%s", sys_signame
[n
]);
136 outlen
+= strlen(sys_signame
[n
]);
139 outlen
+= 3; /* good enough */
142 if (outlen
> 71 || n
== sys_nsig
- 1) {
156 trapcmd(int argc __unused
, char **argv
)
163 while ((i
= nextopt("l")) != '\0') {
173 for (signo
= 0 ; signo
< sys_nsig
; signo
++) {
174 if (signo
< NSIG
&& trap
[signo
] != NULL
) {
176 out1qstr(trap
[signo
]);
179 } else if (sys_signame
[signo
]) {
180 out1fmt(" %s\n", sys_signame
[signo
]);
182 out1fmt(" %d\n", signo
);
189 if (*argv
&& !is_number(*argv
)) {
190 if (strcmp(*argv
, "-") == 0)
197 for (; *argv
; argv
++) {
198 if ((signo
= sigstring_to_signum(*argv
)) == -1) {
199 warning("bad signal %s", *argv
);
205 action
= savestr(action
);
208 trap
[signo
] = action
;
218 * Clear traps on a fork.
225 for (tp
= trap
; tp
<= &trap
[NSIG
- 1] ; tp
++) {
226 if (*tp
&& **tp
) { /* trap not NULL or SIG_IGN */
231 setsignal(tp
- trap
);
239 * Check if we have any traps enabled.
246 for (tp
= trap
; tp
<= &trap
[NSIG
- 1] ; tp
++) {
247 if (*tp
&& **tp
) /* trap not NULL or SIG_IGN */
254 * Set the signal handler for the specified signal. The routine figures
255 * out what it should be set to.
261 sig_t sigact
= SIG_DFL
;
265 if ((t
= trap
[signo
]) == NULL
)
271 if (action
== S_DFL
) {
288 if (rootshell
&& iflag
)
294 if (rootshell
&& mflag
)
304 * current setting unknown
306 if (!getsigaction(signo
, &sigact
)) {
308 * Pretend it worked; maybe we should give a warning
309 * here, but other shells don't. We don't alter
310 * sigmode, so that we retry every time.
314 if (sigact
== SIG_IGN
) {
315 if (mflag
&& (signo
== SIGTSTP
||
316 signo
== SIGTTIN
|| signo
== SIGTTOU
)) {
317 *t
= S_IGN
; /* don't hard ignore these */
321 *t
= S_RESET
; /* force to be set */
324 if (*t
== S_HARD_IGN
|| *t
== action
)
327 case S_DFL
: sigact
= SIG_DFL
; break;
328 case S_CATCH
: sigact
= onsig
; break;
329 case S_IGN
: sigact
= SIG_IGN
; break;
332 sa
.sa_handler
= sigact
;
334 sigemptyset(&sa
.sa_mask
);
335 sigaction(signo
, &sa
, NULL
);
340 * Return the current setting for sig w/o changing it.
343 getsigaction(int signo
, sig_t
*sigact
)
347 if (sigaction(signo
, (struct sigaction
*)0, &sa
) == -1)
349 *sigact
= (sig_t
) sa
.sa_handler
;
361 if (sigmode
[signo
] == 0)
363 if (sigmode
[signo
] != S_IGN
&& sigmode
[signo
] != S_HARD_IGN
) {
364 signal(signo
, SIG_IGN
);
365 sigmode
[signo
] = S_IGN
;
371 issigchldtrapped(void)
374 return (trap
[SIGCHLD
] != NULL
&& *trap
[SIGCHLD
] != '\0');
385 if (signo
== SIGINT
&& trap
[SIGINT
] == NULL
) {
387 * The !in_dotrap here is safe. The only way we can arrive
388 * here with in_dotrap set is that a trap handler set SIGINT to
389 * SIG_DFL and killed itself.
391 if (suppressint
&& !in_dotrap
)
398 /* If we are currently in a wait builtin, prepare to break it */
399 if (signo
== SIGINT
|| signo
== SIGQUIT
)
400 pendingsig_waitcmd
= signo
;
402 if (trap
[signo
] != NULL
&& trap
[signo
][0] != '\0' &&
403 (signo
!= SIGCHLD
|| !ignore_sigchld
)) {
406 pendingsig_waitcmd
= signo
;
412 * Called to execute a trap. Perhaps we should avoid entering new trap
413 * handlers while we are executing a trap handler.
419 int savestatus
, prev_evalskip
, prev_skipcount
;
424 pendingsig_waitcmd
= 0;
425 for (i
= 1; i
< NSIG
; i
++) {
430 * Ignore SIGCHLD to avoid infinite
431 * recursion if the trap action does
438 * Backup current evalskip
439 * state and reset it before
440 * executing a trap, so that the
441 * trap is not disturbed by an
442 * ongoing break/continue/return
445 prev_evalskip
= evalskip
;
446 prev_skipcount
= skipcount
;
450 savestatus
= exitstatus
;
451 evalstring(trap
[i
], 0);
454 * If such a command was not
455 * already in progress, allow a
456 * break/continue/return in the
457 * trap action to have an effect
461 prev_evalskip
!= 0) {
462 evalskip
= prev_evalskip
;
463 skipcount
= prev_skipcount
;
464 exitstatus
= savestatus
;
481 * Controls whether the shell is interactive or not.
484 setinteractive(int on
)
486 static int is_interactive
= -1;
488 if (on
== is_interactive
)
498 * Called to exit the shell.
501 exitshell(int status
)
503 TRACE(("exitshell(%d) pid=%d\n", status
, getpid()));
505 exiting_exitstatus
= status
;
506 exitshell_savedstatus();
510 exitshell_savedstatus(void)
512 struct jmploc loc1
, loc2
;
518 if (in_dotrap
&& last_trapsig
) {
520 exiting_exitstatus
= sig
+ 128;
522 exiting_exitstatus
= oexitstatus
;
524 exitstatus
= oexitstatus
= exiting_exitstatus
;
525 if (!setjmp(loc1
.loc
)) {
527 if ((p
= trap
[0]) != NULL
&& *p
!= '\0') {
529 * Reset evalskip, or the trap on EXIT could be
530 * interrupted if the last command was a "return".
537 if (!setjmp(loc2
.loc
)) {
538 handler
= &loc2
; /* probably unnecessary */
544 if (sig
!= 0 && sig
!= SIGSTOP
&& sig
!= SIGTSTP
&& sig
!= SIGTTIN
&&
546 signal(sig
, SIG_DFL
);
548 sigaddset(&sigs
, sig
);
549 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
551 /* If the default action is to ignore, fall back to _exit(). */
553 _exit(exiting_exitstatus
);