Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* trap.c -- Not the trap command, but useful functions for manipulating |
| 2 | those objects. The trap command is in builtins/trap.def. */ |
| 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | /* Copyright (C) 1987-2013 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 21 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 24 | #if defined (HAVE_UNISTD_H) |
| 25 | # include <unistd.h> |
| 26 | #endif |
| 27 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 28 | #include "bashtypes.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 29 | #include "bashansi.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 30 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 32 | #include <errno.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 33 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 34 | #include "bashintl.h" |
| 35 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 36 | #include <signal.h> |
| 37 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 38 | #include "trap.h" |
| 39 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | #include "shell.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 41 | #include "flags.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 42 | #include "input.h" /* for save_token_state, restore_token_state */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 43 | #include "jobs.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 44 | #include "signames.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 45 | #include "builtins.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | #include "builtins/common.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 47 | #include "builtins/builtext.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 48 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 49 | #if defined (READLINE) |
| 50 | # include <readline/readline.h> |
| 51 | # include "bashline.h" |
| 52 | #endif |
| 53 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 54 | #ifndef errno |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 55 | #include <errno.h> |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | /* Flags which describe the current handling state of a signal. */ |
| 59 | #define SIG_INHERITED 0x0 /* Value inherited from parent. */ |
| 60 | #define SIG_TRAPPED 0x1 /* Currently trapped. */ |
| 61 | #define SIG_HARD_IGNORE 0x2 /* Signal was ignored on shell entry. */ |
| 62 | #define SIG_SPECIAL 0x4 /* Treat this signal specially. */ |
| 63 | #define SIG_NO_TRAP 0x8 /* Signal cannot be trapped. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | #define SIG_INPROGRESS 0x10 /* Signal handler currently executing. */ |
| 65 | #define SIG_CHANGED 0x20 /* Trap value changed in trap handler. */ |
| 66 | #define SIG_IGNORED 0x40 /* The signal is currently being ignored. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 67 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 68 | #define SPECIAL_TRAP(s) ((s) == EXIT_TRAP || (s) == DEBUG_TRAP || (s) == ERROR_TRAP || (s) == RETURN_TRAP) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 69 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 70 | /* An array of such flags, one for each signal, describing what the |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 71 | shell will do with a signal. DEBUG_TRAP == NSIG; some code below |
| 72 | assumes this. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 73 | static int sigmodes[BASH_NSIG]; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 74 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 75 | static void free_trap_command __P((int)); |
| 76 | static void change_signal __P((int, char *)); |
| 77 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 78 | static int _run_trap_internal __P((int, char *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 79 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 80 | static void free_trap_string __P((int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 81 | static void reset_signal __P((int)); |
| 82 | static void restore_signal __P((int)); |
| 83 | static void reset_or_restore_signal_handlers __P((sh_resetsig_func_t *)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | |
| 85 | /* Variables used here but defined in other files. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 86 | extern int last_command_exit_value; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 87 | extern int line_number; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 88 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 89 | extern int sigalrm_seen; |
| 90 | extern procenv_t alrmbuf; |
| 91 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 92 | extern char *this_command_name; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 93 | extern sh_builtin_func_t *this_shell_builtin; |
| 94 | extern procenv_t wait_intr_buf; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 95 | extern int return_catch_flag, return_catch_value; |
| 96 | extern int subshell_level; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 97 | extern WORD_LIST *subst_assign_varlist; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 98 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 99 | /* The list of things to do originally, before we started trapping. */ |
| 100 | SigHandler *original_signals[NSIG]; |
| 101 | |
| 102 | /* For each signal, a slot for a string, which is a command to be |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 103 | executed when that signal is received. The slot can also contain |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 104 | DEFAULT_SIG, which means do whatever you were going to do before |
| 105 | you were so rudely interrupted, or IGNORE_SIG, which says ignore |
| 106 | this signal. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 107 | char *trap_list[BASH_NSIG]; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 108 | |
| 109 | /* A bitmap of signals received for which we have trap handlers. */ |
| 110 | int pending_traps[NSIG]; |
| 111 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 112 | /* Set to the number of the signal we're running the trap for + 1. |
| 113 | Used in execute_cmd.c and builtins/common.c to clean up when |
| 114 | parse_and_execute does not return normally after executing the |
| 115 | trap command (e.g., when `return' is executed in the trap command). */ |
| 116 | int running_trap; |
| 117 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 118 | /* Set to last_command_exit_value before running a trap. */ |
| 119 | int trap_saved_exit_value; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 120 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 121 | /* The (trapped) signal received while executing in the `wait' builtin */ |
| 122 | int wait_signal_received; |
| 123 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 124 | int trapped_signal_received; |
| 125 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 126 | #define GETORIGSIG(sig) \ |
| 127 | do { \ |
| 128 | original_signals[sig] = (SigHandler *)set_signal_handler (sig, SIG_DFL); \ |
| 129 | set_signal_handler (sig, original_signals[sig]); \ |
| 130 | if (original_signals[sig] == SIG_IGN) \ |
| 131 | sigmodes[sig] |= SIG_HARD_IGNORE; \ |
| 132 | } while (0) |
| 133 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 134 | #define SETORIGSIG(sig,handler) \ |
| 135 | do { \ |
| 136 | original_signals[sig] = handler; \ |
| 137 | if (original_signals[sig] == SIG_IGN) \ |
| 138 | sigmodes[sig] |= SIG_HARD_IGNORE; \ |
| 139 | } while (0) |
| 140 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 141 | #define GET_ORIGINAL_SIGNAL(sig) \ |
| 142 | if (sig && sig < NSIG && original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER) \ |
| 143 | GETORIGSIG(sig) |
| 144 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 145 | void |
| 146 | initialize_traps () |
| 147 | { |
| 148 | register int i; |
| 149 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 150 | initialize_signames(); |
| 151 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 152 | trap_list[EXIT_TRAP] = trap_list[DEBUG_TRAP] = trap_list[ERROR_TRAP] = trap_list[RETURN_TRAP] = (char *)NULL; |
| 153 | sigmodes[EXIT_TRAP] = sigmodes[DEBUG_TRAP] = sigmodes[ERROR_TRAP] = sigmodes[RETURN_TRAP] = SIG_INHERITED; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 154 | original_signals[EXIT_TRAP] = IMPOSSIBLE_TRAP_HANDLER; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 155 | |
| 156 | for (i = 1; i < NSIG; i++) |
| 157 | { |
| 158 | pending_traps[i] = 0; |
| 159 | trap_list[i] = (char *)DEFAULT_SIG; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 160 | sigmodes[i] = SIG_INHERITED; /* XXX - only set, not used */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 161 | original_signals[i] = IMPOSSIBLE_TRAP_HANDLER; |
| 162 | } |
| 163 | |
| 164 | /* Show which signals are treated specially by the shell. */ |
| 165 | #if defined (SIGCHLD) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 166 | GETORIGSIG (SIGCHLD); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 167 | sigmodes[SIGCHLD] |= (SIG_SPECIAL | SIG_NO_TRAP); |
| 168 | #endif /* SIGCHLD */ |
| 169 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 170 | GETORIGSIG (SIGINT); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 171 | sigmodes[SIGINT] |= SIG_SPECIAL; |
| 172 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 173 | #if defined (__BEOS__) |
| 174 | /* BeOS sets SIGINT to SIG_IGN! */ |
| 175 | original_signals[SIGINT] = SIG_DFL; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 176 | sigmodes[SIGINT] &= ~SIG_HARD_IGNORE; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 177 | #endif |
| 178 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 179 | GETORIGSIG (SIGQUIT); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 180 | sigmodes[SIGQUIT] |= SIG_SPECIAL; |
| 181 | |
| 182 | if (interactive) |
| 183 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 184 | GETORIGSIG (SIGTERM); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 185 | sigmodes[SIGTERM] |= SIG_SPECIAL; |
| 186 | } |
| 187 | } |
| 188 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 189 | #ifdef DEBUG |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 190 | /* Return a printable representation of the trap handler for SIG. */ |
| 191 | static char * |
| 192 | trap_handler_string (sig) |
| 193 | int sig; |
| 194 | { |
| 195 | if (trap_list[sig] == (char *)DEFAULT_SIG) |
| 196 | return "DEFAULT_SIG"; |
| 197 | else if (trap_list[sig] == (char *)IGNORE_SIG) |
| 198 | return "IGNORE_SIG"; |
| 199 | else if (trap_list[sig] == (char *)IMPOSSIBLE_TRAP_HANDLER) |
| 200 | return "IMPOSSIBLE_TRAP_HANDLER"; |
| 201 | else if (trap_list[sig]) |
| 202 | return trap_list[sig]; |
| 203 | else |
| 204 | return "NULL"; |
| 205 | } |
| 206 | #endif |
| 207 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 208 | /* Return the print name of this signal. */ |
| 209 | char * |
| 210 | signal_name (sig) |
| 211 | int sig; |
| 212 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 213 | char *ret; |
| 214 | |
| 215 | /* on cygwin32, signal_names[sig] could be null */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 216 | ret = (sig >= BASH_NSIG || sig < 0 || signal_names[sig] == NULL) |
| 217 | ? _("invalid signal number") |
| 218 | : signal_names[sig]; |
| 219 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 220 | return ret; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* Turn a string into a signal number, or a number into |
| 224 | a signal number. If STRING is "2", "SIGINT", or "INT", |
| 225 | then (int)2 is returned. Return NO_SIG if STRING doesn't |
| 226 | contain a valid signal descriptor. */ |
| 227 | int |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 228 | decode_signal (string, flags) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 229 | char *string; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 230 | int flags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 231 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 232 | intmax_t sig; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 233 | char *name; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 234 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 235 | if (legal_number (string, &sig)) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 236 | return ((sig >= 0 && sig < NSIG) ? (int)sig : NO_SIG); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 237 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 238 | /* A leading `SIG' may be omitted. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 239 | for (sig = 0; sig < BASH_NSIG; sig++) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 240 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 241 | name = signal_names[sig]; |
| 242 | if (name == 0 || name[0] == '\0') |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 243 | continue; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 244 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 245 | /* Check name without the SIG prefix first case sensitively or |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 246 | insensitively depending on whether flags includes DSIG_NOCASE */ |
| 247 | if (STREQN (name, "SIG", 3)) |
| 248 | { |
| 249 | name += 3; |
| 250 | |
| 251 | if ((flags & DSIG_NOCASE) && strcasecmp (string, name) == 0) |
| 252 | return ((int)sig); |
| 253 | else if ((flags & DSIG_NOCASE) == 0 && strcmp (string, name) == 0) |
| 254 | return ((int)sig); |
| 255 | /* If we can't use the `SIG' prefix to match, punt on this |
| 256 | name now. */ |
| 257 | else if ((flags & DSIG_SIGPREFIX) == 0) |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | /* Check name with SIG prefix case sensitively or insensitively |
| 262 | depending on whether flags includes DSIG_NOCASE */ |
| 263 | name = signal_names[sig]; |
| 264 | if ((flags & DSIG_NOCASE) && strcasecmp (string, name) == 0) |
| 265 | return ((int)sig); |
| 266 | else if ((flags & DSIG_NOCASE) == 0 && strcmp (string, name) == 0) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 267 | return ((int)sig); |
| 268 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 269 | |
| 270 | return (NO_SIG); |
| 271 | } |
| 272 | |
| 273 | /* Non-zero when we catch a trapped signal. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 274 | static int catch_flag; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 275 | |
| 276 | void |
| 277 | run_pending_traps () |
| 278 | { |
| 279 | register int sig; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 280 | int old_exit_value; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 281 | WORD_LIST *save_subst_varlist; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 282 | sh_parser_state_t pstate; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 283 | #if defined (ARRAY_VARS) |
| 284 | ARRAY *ps; |
| 285 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 286 | |
| 287 | if (catch_flag == 0) /* simple optimization */ |
| 288 | return; |
| 289 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 290 | if (running_trap > 0) |
| 291 | { |
| 292 | #if defined (DEBUG) |
| 293 | internal_warning ("run_pending_traps: recursive invocation while running trap for signal %d", running_trap-1); |
| 294 | #endif |
| 295 | #if 0 |
| 296 | return; /* no recursive trap invocations */ |
| 297 | #else |
| 298 | ; |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | catch_flag = trapped_signal_received = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 303 | |
| 304 | /* Preserve $? when running trap. */ |
| 305 | old_exit_value = last_command_exit_value; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 306 | #if defined (ARRAY_VARS) |
| 307 | ps = save_pipestatus_array (); |
| 308 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 309 | |
| 310 | for (sig = 1; sig < NSIG; sig++) |
| 311 | { |
| 312 | /* XXX this could be made into a counter by using |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 313 | while (pending_traps[sig]--) instead of the if statement. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 314 | if (pending_traps[sig]) |
| 315 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 316 | if (running_trap == sig+1) |
| 317 | /*continue*/; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 318 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 319 | running_trap = sig + 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 320 | |
| 321 | if (sig == SIGINT) |
| 322 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 323 | pending_traps[sig] = 0; /* XXX */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 324 | run_interrupt_trap (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 325 | CLRINTERRUPT; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 326 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 327 | #if defined (JOB_CONTROL) && defined (SIGCHLD) |
| 328 | else if (sig == SIGCHLD && |
| 329 | trap_list[SIGCHLD] != (char *)IMPOSSIBLE_TRAP_HANDLER && |
| 330 | (sigmodes[SIGCHLD] & SIG_INPROGRESS) == 0) |
| 331 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 332 | sigmodes[SIGCHLD] |= SIG_INPROGRESS; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 333 | run_sigchld_trap (pending_traps[sig]); /* use as counter */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 334 | sigmodes[SIGCHLD] &= ~SIG_INPROGRESS; |
| 335 | } |
| 336 | else if (sig == SIGCHLD && |
| 337 | trap_list[SIGCHLD] == (char *)IMPOSSIBLE_TRAP_HANDLER && |
| 338 | (sigmodes[SIGCHLD] & SIG_INPROGRESS) != 0) |
| 339 | { |
| 340 | /* This can happen when run_pending_traps is called while |
| 341 | running a SIGCHLD trap handler. */ |
| 342 | running_trap = 0; |
| 343 | /* want to leave pending_traps[SIGCHLD] alone here */ |
| 344 | continue; /* XXX */ |
| 345 | } |
| 346 | else if (sig == SIGCHLD && (sigmodes[SIGCHLD] & SIG_INPROGRESS)) |
| 347 | { |
| 348 | /* whoops -- print warning? */ |
| 349 | running_trap = 0; /* XXX */ |
| 350 | /* want to leave pending_traps[SIGCHLD] alone here */ |
| 351 | continue; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 352 | } |
| 353 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 354 | else if (trap_list[sig] == (char *)DEFAULT_SIG || |
| 355 | trap_list[sig] == (char *)IGNORE_SIG || |
| 356 | trap_list[sig] == (char *)IMPOSSIBLE_TRAP_HANDLER) |
| 357 | { |
| 358 | /* This is possible due to a race condition. Say a bash |
| 359 | process has SIGTERM trapped. A subshell is spawned |
| 360 | using { list; } & and the parent does something and kills |
| 361 | the subshell with SIGTERM. It's possible for the subshell |
| 362 | to set pending_traps[SIGTERM] to 1 before the code in |
| 363 | execute_cmd.c eventually calls restore_original_signals |
| 364 | to reset the SIGTERM signal handler in the subshell. The |
| 365 | next time run_pending_traps is called, pending_traps[SIGTERM] |
| 366 | will be 1, but the trap handler in trap_list[SIGTERM] will |
| 367 | be invalid (probably DEFAULT_SIG, but it could be IGNORE_SIG). |
| 368 | Unless we catch this, the subshell will dump core when |
| 369 | trap_list[SIGTERM] == DEFAULT_SIG, because DEFAULT_SIG is |
| 370 | usually 0x0. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 371 | internal_warning (_("run_pending_traps: bad value in trap_list[%d]: %p"), |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 372 | sig, trap_list[sig]); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 373 | if (trap_list[sig] == (char *)DEFAULT_SIG) |
| 374 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 375 | internal_warning (_("run_pending_traps: signal handler is SIG_DFL, resending %d (%s) to myself"), sig, signal_name (sig)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 376 | kill (getpid (), sig); |
| 377 | } |
| 378 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 379 | else |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 380 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 381 | /* XXX - should we use save_parser_state/restore_parser_state? */ |
| 382 | save_parser_state (&pstate); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 383 | save_subst_varlist = subst_assign_varlist; |
| 384 | subst_assign_varlist = 0; |
| 385 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 386 | #if defined (JOB_CONTROL) |
| 387 | save_pipeline (1); /* XXX only provides one save level */ |
| 388 | #endif |
| 389 | /* XXX - set pending_traps[sig] = 0 here? */ |
| 390 | pending_traps[sig] = 0; |
| 391 | evalstring (savestring (trap_list[sig]), "trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE); |
| 392 | #if defined (JOB_CONTROL) |
| 393 | restore_pipeline (1); |
| 394 | #endif |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 395 | |
| 396 | subst_assign_varlist = save_subst_varlist; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 397 | restore_parser_state (&pstate); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 398 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 399 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 400 | pending_traps[sig] = 0; /* XXX - move before evalstring? */ |
| 401 | running_trap = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 405 | #if defined (ARRAY_VARS) |
| 406 | restore_pipestatus_array (ps); |
| 407 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 408 | last_command_exit_value = old_exit_value; |
| 409 | } |
| 410 | |
| 411 | sighandler |
| 412 | trap_handler (sig) |
| 413 | int sig; |
| 414 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 415 | int oerrno; |
| 416 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 417 | if ((sigmodes[sig] & SIG_TRAPPED) == 0) |
| 418 | { |
| 419 | #if defined (DEBUG) |
| 420 | internal_warning ("trap_handler: signal %d: signal not trapped", sig); |
| 421 | #endif |
| 422 | SIGRETURN (0); |
| 423 | } |
| 424 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 425 | if ((sig >= NSIG) || |
| 426 | (trap_list[sig] == (char *)DEFAULT_SIG) || |
| 427 | (trap_list[sig] == (char *)IGNORE_SIG)) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 428 | programming_error (_("trap_handler: bad signal %d"), sig); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 429 | else |
| 430 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 431 | oerrno = errno; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 432 | #if defined (MUST_REINSTALL_SIGHANDLERS) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 433 | # if defined (JOB_CONTROL) && defined (SIGCHLD) |
| 434 | if (sig != SIGCHLD) |
| 435 | # endif /* JOB_CONTROL && SIGCHLD */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 436 | set_signal_handler (sig, trap_handler); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 437 | #endif /* MUST_REINSTALL_SIGHANDLERS */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 438 | |
| 439 | catch_flag = 1; |
| 440 | pending_traps[sig]++; |
| 441 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 442 | trapped_signal_received = sig; |
| 443 | |
| 444 | if (this_shell_builtin && (this_shell_builtin == wait_builtin)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 445 | { |
| 446 | wait_signal_received = sig; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 447 | if (interrupt_immediately) |
| 448 | longjmp (wait_intr_buf, 1); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 451 | #if defined (READLINE) |
| 452 | /* Set the event hook so readline will call it after the signal handlers |
| 453 | finish executing, so if this interrupted character input we can get |
| 454 | quick response. */ |
| 455 | if (RL_ISSTATE (RL_STATE_SIGHANDLER) && interrupt_immediately == 0) |
| 456 | bashline_set_event_hook (); |
| 457 | #endif |
| 458 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 459 | if (interrupt_immediately) |
| 460 | run_pending_traps (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 461 | |
| 462 | errno = oerrno; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 463 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 464 | |
| 465 | SIGRETURN (0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 468 | int |
| 469 | first_pending_trap () |
| 470 | { |
| 471 | register int i; |
| 472 | |
| 473 | for (i = 1; i < NSIG; i++) |
| 474 | if (pending_traps[i]) |
| 475 | return i; |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | int |
| 480 | any_signals_trapped () |
| 481 | { |
| 482 | register int i; |
| 483 | |
| 484 | for (i = 1; i < NSIG; i++) |
| 485 | if (sigmodes[i] & SIG_TRAPPED) |
| 486 | return i; |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | void |
| 491 | check_signals () |
| 492 | { |
| 493 | CHECK_ALRM; /* set by the read builtin */ |
| 494 | QUIT; |
| 495 | } |
| 496 | |
| 497 | /* Convenience functions the rest of the shell can use */ |
| 498 | void |
| 499 | check_signals_and_traps () |
| 500 | { |
| 501 | check_signals (); |
| 502 | |
| 503 | run_pending_traps (); |
| 504 | } |
| 505 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 506 | #if defined (JOB_CONTROL) && defined (SIGCHLD) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 507 | |
| 508 | #ifdef INCLUDE_UNUSED |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 509 | /* Make COMMAND_STRING be executed when SIGCHLD is caught. */ |
| 510 | void |
| 511 | set_sigchld_trap (command_string) |
| 512 | char *command_string; |
| 513 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 514 | set_signal (SIGCHLD, command_string); |
| 515 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 516 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 517 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 518 | /* Make COMMAND_STRING be executed when SIGCHLD is caught iff SIGCHLD |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 519 | is not already trapped. IMPOSSIBLE_TRAP_HANDLER is used as a sentinel |
| 520 | to make sure that a SIGCHLD trap handler run via run_sigchld_trap can |
| 521 | reset the disposition to the default and not have the original signal |
| 522 | accidentally restored, undoing the user's command. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 523 | void |
| 524 | maybe_set_sigchld_trap (command_string) |
| 525 | char *command_string; |
| 526 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 527 | if ((sigmodes[SIGCHLD] & SIG_TRAPPED) == 0 && trap_list[SIGCHLD] == (char *)IMPOSSIBLE_TRAP_HANDLER) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 528 | set_signal (SIGCHLD, command_string); |
| 529 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 530 | |
| 531 | /* Temporarily set the SIGCHLD trap string to IMPOSSIBLE_TRAP_HANDLER. Used |
| 532 | as a sentinel in run_sigchld_trap and maybe_set_sigchld_trap to see whether |
| 533 | or not a SIGCHLD trap handler reset SIGCHLD disposition to the default. */ |
| 534 | void |
| 535 | set_impossible_sigchld_trap () |
| 536 | { |
| 537 | restore_default_signal (SIGCHLD); |
| 538 | change_signal (SIGCHLD, (char *)IMPOSSIBLE_TRAP_HANDLER); |
| 539 | sigmodes[SIGCHLD] &= ~SIG_TRAPPED; /* maybe_set_sigchld_trap checks this */ |
| 540 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 541 | |
| 542 | /* Act as if we received SIGCHLD NCHILD times and increment |
| 543 | pending_traps[SIGCHLD] by that amount. This allows us to still run the |
| 544 | SIGCHLD trap once for each exited child. */ |
| 545 | void |
| 546 | queue_sigchld_trap (nchild) |
| 547 | int nchild; |
| 548 | { |
| 549 | if (nchild > 0) |
| 550 | { |
| 551 | catch_flag = 1; |
| 552 | pending_traps[SIGCHLD] += nchild; |
| 553 | trapped_signal_received = SIGCHLD; |
| 554 | } |
| 555 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 556 | #endif /* JOB_CONTROL && SIGCHLD */ |
| 557 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 558 | void |
| 559 | set_debug_trap (command) |
| 560 | char *command; |
| 561 | { |
| 562 | set_signal (DEBUG_TRAP, command); |
| 563 | } |
| 564 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 565 | void |
| 566 | set_error_trap (command) |
| 567 | char *command; |
| 568 | { |
| 569 | set_signal (ERROR_TRAP, command); |
| 570 | } |
| 571 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 572 | void |
| 573 | set_return_trap (command) |
| 574 | char *command; |
| 575 | { |
| 576 | set_signal (RETURN_TRAP, command); |
| 577 | } |
| 578 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 579 | #ifdef INCLUDE_UNUSED |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 580 | void |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 581 | set_sigint_trap (command) |
| 582 | char *command; |
| 583 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 584 | set_signal (SIGINT, command); |
| 585 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 586 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 587 | |
| 588 | /* Reset the SIGINT handler so that subshells that are doing `shellsy' |
| 589 | things, like waiting for command substitution or executing commands |
| 590 | in explicit subshells ( ( cmd ) ), can catch interrupts properly. */ |
| 591 | SigHandler * |
| 592 | set_sigint_handler () |
| 593 | { |
| 594 | if (sigmodes[SIGINT] & SIG_HARD_IGNORE) |
| 595 | return ((SigHandler *)SIG_IGN); |
| 596 | |
| 597 | else if (sigmodes[SIGINT] & SIG_IGNORED) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 598 | return ((SigHandler *)set_signal_handler (SIGINT, SIG_IGN)); /* XXX */ |
| 599 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 600 | else if (sigmodes[SIGINT] & SIG_TRAPPED) |
| 601 | return ((SigHandler *)set_signal_handler (SIGINT, trap_handler)); |
| 602 | |
| 603 | /* The signal is not trapped, so set the handler to the shell's special |
| 604 | interrupt handler. */ |
| 605 | else if (interactive) /* XXX - was interactive_shell */ |
| 606 | return (set_signal_handler (SIGINT, sigint_sighandler)); |
| 607 | else |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 608 | return (set_signal_handler (SIGINT, termsig_sighandler)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 611 | /* Return the correct handler for signal SIG according to the values in |
| 612 | sigmodes[SIG]. */ |
| 613 | SigHandler * |
| 614 | trap_to_sighandler (sig) |
| 615 | int sig; |
| 616 | { |
| 617 | if (sigmodes[sig] & (SIG_IGNORED|SIG_HARD_IGNORE)) |
| 618 | return (SIG_IGN); |
| 619 | else if (sigmodes[sig] & SIG_TRAPPED) |
| 620 | return (trap_handler); |
| 621 | else |
| 622 | return (SIG_DFL); |
| 623 | } |
| 624 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 625 | /* Set SIG to call STRING as a command. */ |
| 626 | void |
| 627 | set_signal (sig, string) |
| 628 | int sig; |
| 629 | char *string; |
| 630 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 631 | sigset_t set, oset; |
| 632 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 633 | if (SPECIAL_TRAP (sig)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 634 | { |
| 635 | change_signal (sig, savestring (string)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 636 | if (sig == EXIT_TRAP && interactive == 0) |
| 637 | initialize_terminating_signals (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 641 | /* A signal ignored on entry to the shell cannot be trapped or reset, but |
| 642 | no error is reported when attempting to do so. -- Posix.2 */ |
| 643 | if (sigmodes[sig] & SIG_HARD_IGNORE) |
| 644 | return; |
| 645 | |
| 646 | /* Make sure we have original_signals[sig] if the signal has not yet |
| 647 | been trapped. */ |
| 648 | if ((sigmodes[sig] & SIG_TRAPPED) == 0) |
| 649 | { |
| 650 | /* If we aren't sure of the original value, check it. */ |
| 651 | if (original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 652 | GETORIGSIG (sig); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 653 | if (original_signals[sig] == SIG_IGN) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 654 | return; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* Only change the system signal handler if SIG_NO_TRAP is not set. |
| 658 | The trap command string is changed in either case. The shell signal |
| 659 | handlers for SIGINT and SIGCHLD run the user specified traps in an |
| 660 | environment in which it is safe to do so. */ |
| 661 | if ((sigmodes[sig] & SIG_NO_TRAP) == 0) |
| 662 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 663 | BLOCK_SIGNAL (sig, set, oset); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 664 | change_signal (sig, savestring (string)); |
| 665 | set_signal_handler (sig, trap_handler); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 666 | UNBLOCK_SIGNAL (oset); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 667 | } |
| 668 | else |
| 669 | change_signal (sig, savestring (string)); |
| 670 | } |
| 671 | |
| 672 | static void |
| 673 | free_trap_command (sig) |
| 674 | int sig; |
| 675 | { |
| 676 | if ((sigmodes[sig] & SIG_TRAPPED) && trap_list[sig] && |
| 677 | (trap_list[sig] != (char *)IGNORE_SIG) && |
| 678 | (trap_list[sig] != (char *)DEFAULT_SIG) && |
| 679 | (trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER)) |
| 680 | free (trap_list[sig]); |
| 681 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 682 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 683 | /* If SIG has a string assigned to it, get rid of it. Then give it |
| 684 | VALUE. */ |
| 685 | static void |
| 686 | change_signal (sig, value) |
| 687 | int sig; |
| 688 | char *value; |
| 689 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 690 | if ((sigmodes[sig] & SIG_INPROGRESS) == 0) |
| 691 | free_trap_command (sig); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 692 | trap_list[sig] = value; |
| 693 | |
| 694 | sigmodes[sig] |= SIG_TRAPPED; |
| 695 | if (value == (char *)IGNORE_SIG) |
| 696 | sigmodes[sig] |= SIG_IGNORED; |
| 697 | else |
| 698 | sigmodes[sig] &= ~SIG_IGNORED; |
| 699 | if (sigmodes[sig] & SIG_INPROGRESS) |
| 700 | sigmodes[sig] |= SIG_CHANGED; |
| 701 | } |
| 702 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 703 | void |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 704 | get_original_signal (sig) |
| 705 | int sig; |
| 706 | { |
| 707 | /* If we aren't sure the of the original value, then get it. */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 708 | if (sig > 0 && sig < NSIG && original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 709 | GETORIGSIG (sig); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 712 | void |
| 713 | get_all_original_signals () |
| 714 | { |
| 715 | register int i; |
| 716 | |
| 717 | for (i = 1; i < NSIG; i++) |
| 718 | GET_ORIGINAL_SIGNAL (i); |
| 719 | } |
| 720 | |
| 721 | void |
| 722 | set_original_signal (sig, handler) |
| 723 | int sig; |
| 724 | SigHandler *handler; |
| 725 | { |
| 726 | if (sig > 0 && sig < NSIG && original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER) |
| 727 | SETORIGSIG (sig, handler); |
| 728 | } |
| 729 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 730 | /* Restore the default action for SIG; i.e., the action the shell |
| 731 | would have taken before you used the trap command. This is called |
| 732 | from trap_builtin (), which takes care to restore the handlers for |
| 733 | the signals the shell treats specially. */ |
| 734 | void |
| 735 | restore_default_signal (sig) |
| 736 | int sig; |
| 737 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 738 | if (SPECIAL_TRAP (sig)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 739 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 740 | if ((sig != DEBUG_TRAP && sig != ERROR_TRAP && sig != RETURN_TRAP) || |
| 741 | (sigmodes[sig] & SIG_INPROGRESS) == 0) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 742 | free_trap_command (sig); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 743 | trap_list[sig] = (char *)NULL; |
| 744 | sigmodes[sig] &= ~SIG_TRAPPED; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 745 | if (sigmodes[sig] & SIG_INPROGRESS) |
| 746 | sigmodes[sig] |= SIG_CHANGED; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 747 | return; |
| 748 | } |
| 749 | |
| 750 | GET_ORIGINAL_SIGNAL (sig); |
| 751 | |
| 752 | /* A signal ignored on entry to the shell cannot be trapped or reset, but |
| 753 | no error is reported when attempting to do so. Thanks Posix.2. */ |
| 754 | if (sigmodes[sig] & SIG_HARD_IGNORE) |
| 755 | return; |
| 756 | |
| 757 | /* If we aren't trapping this signal, don't bother doing anything else. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 758 | /* We special-case SIGCHLD and IMPOSSIBLE_TRAP_HANDLER (see above) as a |
| 759 | sentinel to determine whether or not disposition is reset to the default |
| 760 | while the trap handler is executing. */ |
| 761 | if (((sigmodes[sig] & SIG_TRAPPED) == 0) && |
| 762 | (sig != SIGCHLD || (sigmodes[sig] & SIG_INPROGRESS) == 0 || trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 763 | return; |
| 764 | |
| 765 | /* Only change the signal handler for SIG if it allows it. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | if ((sigmodes[sig] & SIG_NO_TRAP) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 767 | set_signal_handler (sig, original_signals[sig]); |
| 768 | |
| 769 | /* Change the trap command in either case. */ |
| 770 | change_signal (sig, (char *)DEFAULT_SIG); |
| 771 | |
| 772 | /* Mark the signal as no longer trapped. */ |
| 773 | sigmodes[sig] &= ~SIG_TRAPPED; |
| 774 | } |
| 775 | |
| 776 | /* Make this signal be ignored. */ |
| 777 | void |
| 778 | ignore_signal (sig) |
| 779 | int sig; |
| 780 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 781 | if (SPECIAL_TRAP (sig) && ((sigmodes[sig] & SIG_IGNORED) == 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 782 | { |
| 783 | change_signal (sig, (char *)IGNORE_SIG); |
| 784 | return; |
| 785 | } |
| 786 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 787 | GET_ORIGINAL_SIGNAL (sig); |
| 788 | |
| 789 | /* A signal ignored on entry to the shell cannot be trapped or reset. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 790 | No error is reported when the user attempts to do so. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 791 | if (sigmodes[sig] & SIG_HARD_IGNORE) |
| 792 | return; |
| 793 | |
| 794 | /* If already trapped and ignored, no change necessary. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 795 | if (sigmodes[sig] & SIG_IGNORED) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 796 | return; |
| 797 | |
| 798 | /* Only change the signal handler for SIG if it allows it. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 799 | if ((sigmodes[sig] & SIG_NO_TRAP) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 800 | set_signal_handler (sig, SIG_IGN); |
| 801 | |
| 802 | /* Change the trap command in either case. */ |
| 803 | change_signal (sig, (char *)IGNORE_SIG); |
| 804 | } |
| 805 | |
| 806 | /* Handle the calling of "trap 0". The only sticky situation is when |
| 807 | the command to be executed includes an "exit". This is why we have |
| 808 | to provide our own place for top_level to jump to. */ |
| 809 | int |
| 810 | run_exit_trap () |
| 811 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 812 | char *trap_command; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 813 | int code, function_code, retval; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 814 | #if defined (ARRAY_VARS) |
| 815 | ARRAY *ps; |
| 816 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 817 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 818 | trap_saved_exit_value = last_command_exit_value; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 819 | #if defined (ARRAY_VARS) |
| 820 | ps = save_pipestatus_array (); |
| 821 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 822 | function_code = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 823 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 824 | /* Run the trap only if signal 0 is trapped and not ignored, and we are not |
| 825 | currently running in the trap handler (call to exit in the list of |
| 826 | commands given to trap 0). */ |
| 827 | if ((sigmodes[EXIT_TRAP] & SIG_TRAPPED) && |
| 828 | (sigmodes[EXIT_TRAP] & (SIG_IGNORED|SIG_INPROGRESS)) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 829 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 830 | trap_command = savestring (trap_list[EXIT_TRAP]); |
| 831 | sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED; |
| 832 | sigmodes[EXIT_TRAP] |= SIG_INPROGRESS; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 833 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 834 | retval = trap_saved_exit_value; |
| 835 | running_trap = 1; |
| 836 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 837 | code = setjmp_nosigs (top_level); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 838 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 839 | /* If we're in a function, make sure return longjmps come here, too. */ |
| 840 | if (return_catch_flag) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 841 | function_code = setjmp_nosigs (return_catch); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 842 | |
| 843 | if (code == 0 && function_code == 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 844 | { |
| 845 | reset_parser (); |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 846 | parse_and_execute (trap_command, "exit trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 847 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 848 | else if (code == ERREXIT) |
| 849 | retval = last_command_exit_value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 850 | else if (code == EXITPROG) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 851 | retval = last_command_exit_value; |
| 852 | else if (function_code != 0) |
| 853 | retval = return_catch_value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 854 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 855 | retval = trap_saved_exit_value; |
| 856 | |
| 857 | running_trap = 0; |
| 858 | return retval; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 861 | #if defined (ARRAY_VARS) |
| 862 | restore_pipestatus_array (ps); |
| 863 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 864 | return (trap_saved_exit_value); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 867 | void |
| 868 | run_trap_cleanup (sig) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 869 | int sig; |
| 870 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 871 | sigmodes[sig] &= ~(SIG_INPROGRESS|SIG_CHANGED); |
| 872 | } |
| 873 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 874 | #define RECURSIVE_SIG(s) (SPECIAL_TRAP(s) == 0) |
| 875 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 876 | /* Run a trap command for SIG. SIG is one of the signals the shell treats |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 877 | specially. Returns the exit status of the executed trap command list. */ |
| 878 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 879 | _run_trap_internal (sig, tag) |
| 880 | int sig; |
| 881 | char *tag; |
| 882 | { |
| 883 | char *trap_command, *old_trap; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 884 | int trap_exit_value, *token_state; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 885 | volatile int save_return_catch_flag, function_code; |
| 886 | int flags; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 887 | procenv_t save_return_catch; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 888 | WORD_LIST *save_subst_varlist; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 889 | sh_parser_state_t pstate; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 890 | #if defined (ARRAY_VARS) |
| 891 | ARRAY *ps; |
| 892 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 893 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 894 | trap_exit_value = function_code = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 895 | /* Run the trap only if SIG is trapped and not ignored, and we are not |
| 896 | currently executing in the trap handler. */ |
| 897 | if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0) && |
| 898 | (trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER) && |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 899 | #if 0 |
| 900 | /* Uncomment this to allow some special signals to recursively execute |
| 901 | trap handlers. */ |
| 902 | (RECURSIVE_SIG (sig) || (sigmodes[sig] & SIG_INPROGRESS) == 0)) |
| 903 | #else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 904 | ((sigmodes[sig] & SIG_INPROGRESS) == 0)) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 905 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 906 | { |
| 907 | old_trap = trap_list[sig]; |
| 908 | sigmodes[sig] |= SIG_INPROGRESS; |
| 909 | sigmodes[sig] &= ~SIG_CHANGED; /* just to be sure */ |
| 910 | trap_command = savestring (old_trap); |
| 911 | |
| 912 | running_trap = sig + 1; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 913 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 914 | #if defined (ARRAY_VARS) |
| 915 | ps = save_pipestatus_array (); |
| 916 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 917 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 918 | save_parser_state (&pstate); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 919 | save_subst_varlist = subst_assign_varlist; |
| 920 | subst_assign_varlist = 0; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 921 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 922 | #if defined (JOB_CONTROL) |
Chet Ramey | 7f89f4c | 2014-03-28 11:53:02 -0400 | [diff] [blame] | 923 | if (sig != DEBUG_TRAP) /* run_debug_trap does this */ |
| 924 | save_pipeline (1); /* XXX only provides one save level */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 925 | #endif |
| 926 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 927 | /* If we're in a function, make sure return longjmps come here, too. */ |
| 928 | save_return_catch_flag = return_catch_flag; |
| 929 | if (return_catch_flag) |
| 930 | { |
| 931 | COPY_PROCENV (return_catch, save_return_catch); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 932 | function_code = setjmp_nosigs (return_catch); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 935 | flags = SEVAL_NONINT|SEVAL_NOHIST; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 936 | if (sig != DEBUG_TRAP && sig != RETURN_TRAP && sig != ERROR_TRAP) |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 937 | flags |= SEVAL_RESETLINE; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 938 | if (function_code == 0) |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 939 | parse_and_execute (trap_command, tag, flags); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 940 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 941 | trap_exit_value = last_command_exit_value; |
| 942 | |
| 943 | #if defined (JOB_CONTROL) |
Chet Ramey | 7f89f4c | 2014-03-28 11:53:02 -0400 | [diff] [blame] | 944 | if (sig != DEBUG_TRAP) /* run_debug_trap does this */ |
| 945 | restore_pipeline (1); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 946 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 947 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 948 | subst_assign_varlist = save_subst_varlist; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 949 | restore_parser_state (&pstate); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 950 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 951 | #if defined (ARRAY_VARS) |
| 952 | restore_pipestatus_array (ps); |
| 953 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 954 | running_trap = 0; |
| 955 | |
| 956 | sigmodes[sig] &= ~SIG_INPROGRESS; |
| 957 | |
| 958 | if (sigmodes[sig] & SIG_CHANGED) |
| 959 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 960 | #if 0 |
| 961 | /* Special traps like EXIT, DEBUG, RETURN are handled explicitly in |
| 962 | the places where they can be changed using unwind-protects. For |
| 963 | example, look at execute_cmd.c:execute_function(). */ |
| 964 | if (SPECIAL_TRAP (sig) == 0) |
| 965 | #endif |
| 966 | free (old_trap); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 967 | sigmodes[sig] &= ~SIG_CHANGED; |
| 968 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 969 | |
| 970 | if (save_return_catch_flag) |
| 971 | { |
| 972 | return_catch_flag = save_return_catch_flag; |
| 973 | return_catch_value = trap_exit_value; |
| 974 | COPY_PROCENV (save_return_catch, return_catch); |
| 975 | if (function_code) |
| 976 | longjmp (return_catch, 1); |
| 977 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 978 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 979 | |
| 980 | return trap_exit_value; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 983 | int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 984 | run_debug_trap () |
| 985 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 986 | int trap_exit_value; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 987 | pid_t save_pgrp; |
| 988 | int save_pipe[2]; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 989 | |
| 990 | /* XXX - question: should the DEBUG trap inherit the RETURN trap? */ |
| 991 | trap_exit_value = 0; |
| 992 | if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && ((sigmodes[DEBUG_TRAP] & SIG_IGNORED) == 0) && ((sigmodes[DEBUG_TRAP] & SIG_INPROGRESS) == 0)) |
| 993 | { |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 994 | #if defined (JOB_CONTROL) |
| 995 | save_pgrp = pipeline_pgrp; |
| 996 | pipeline_pgrp = 0; |
| 997 | save_pipeline (1); |
| 998 | # if defined (PGRP_PIPE) |
| 999 | save_pgrp_pipe (save_pipe, 1); |
| 1000 | # endif |
| 1001 | stop_making_children (); |
| 1002 | #endif |
| 1003 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1004 | trap_exit_value = _run_trap_internal (DEBUG_TRAP, "debug trap"); |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 1005 | |
| 1006 | #if defined (JOB_CONTROL) |
| 1007 | pipeline_pgrp = save_pgrp; |
| 1008 | restore_pipeline (1); |
| 1009 | # if defined (PGRP_PIPE) |
| 1010 | close_pgrp_pipe (); |
| 1011 | restore_pgrp_pipe (save_pipe); |
| 1012 | # endif |
| 1013 | if (pipeline_pgrp > 0) |
| 1014 | give_terminal_to (pipeline_pgrp, 1); |
| 1015 | notify_and_cleanup (); |
| 1016 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1017 | |
| 1018 | #if defined (DEBUGGER) |
| 1019 | /* If we're in the debugger and the DEBUG trap returns 2 while we're in |
| 1020 | a function or sourced script, we force a `return'. */ |
| 1021 | if (debugging_mode && trap_exit_value == 2 && return_catch_flag) |
| 1022 | { |
| 1023 | return_catch_value = trap_exit_value; |
| 1024 | longjmp (return_catch, 1); |
| 1025 | } |
| 1026 | #endif |
| 1027 | } |
| 1028 | return trap_exit_value; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1031 | void |
| 1032 | run_error_trap () |
| 1033 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1034 | if ((sigmodes[ERROR_TRAP] & SIG_TRAPPED) && ((sigmodes[ERROR_TRAP] & SIG_IGNORED) == 0) && (sigmodes[ERROR_TRAP] & SIG_INPROGRESS) == 0) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1035 | _run_trap_internal (ERROR_TRAP, "error trap"); |
| 1036 | } |
| 1037 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1038 | void |
| 1039 | run_return_trap () |
| 1040 | { |
| 1041 | int old_exit_value; |
| 1042 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1043 | #if 0 |
| 1044 | if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && (sigmodes[DEBUG_TRAP] & SIG_INPROGRESS)) |
| 1045 | return; |
| 1046 | #endif |
| 1047 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1048 | if ((sigmodes[RETURN_TRAP] & SIG_TRAPPED) && ((sigmodes[RETURN_TRAP] & SIG_IGNORED) == 0) && (sigmodes[RETURN_TRAP] & SIG_INPROGRESS) == 0) |
| 1049 | { |
| 1050 | old_exit_value = last_command_exit_value; |
| 1051 | _run_trap_internal (RETURN_TRAP, "return trap"); |
| 1052 | last_command_exit_value = old_exit_value; |
| 1053 | } |
| 1054 | } |
| 1055 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1056 | /* Run a trap set on SIGINT. This is called from throw_to_top_level (), and |
| 1057 | declared here to localize the trap functions. */ |
| 1058 | void |
| 1059 | run_interrupt_trap () |
| 1060 | { |
| 1061 | _run_trap_internal (SIGINT, "interrupt trap"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* Free all the allocated strings in the list of traps and reset the trap |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1065 | values to the default. Intended to be called from subshells that want |
| 1066 | to complete work done by reset_signal_handlers upon execution of a |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1067 | subsequent `trap' command that changes a signal's disposition. We need |
| 1068 | to make sure that we duplicate the behavior of |
| 1069 | reset_or_restore_signal_handlers and not change the disposition of signals |
| 1070 | that are set to be ignored. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1071 | void |
| 1072 | free_trap_strings () |
| 1073 | { |
| 1074 | register int i; |
| 1075 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1076 | for (i = 0; i < BASH_NSIG; i++) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1077 | { |
| 1078 | if (trap_list[i] != (char *)IGNORE_SIG) |
| 1079 | free_trap_string (i); |
| 1080 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1081 | trap_list[DEBUG_TRAP] = trap_list[EXIT_TRAP] = trap_list[ERROR_TRAP] = trap_list[RETURN_TRAP] = (char *)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1082 | } |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 1083 | |
| 1084 | /* Free a trap command string associated with SIG without changing signal |
| 1085 | disposition. Intended to be called from free_trap_strings() */ |
| 1086 | static void |
| 1087 | free_trap_string (sig) |
| 1088 | int sig; |
| 1089 | { |
| 1090 | change_signal (sig, (char *)DEFAULT_SIG); |
| 1091 | sigmodes[sig] &= ~SIG_TRAPPED; |
| 1092 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1093 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1094 | /* Reset the handler for SIG to the original value but leave the trap string |
| 1095 | in place. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1096 | static void |
| 1097 | reset_signal (sig) |
| 1098 | int sig; |
| 1099 | { |
| 1100 | set_signal_handler (sig, original_signals[sig]); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1101 | sigmodes[sig] &= ~SIG_TRAPPED; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1104 | /* Set the handler signal SIG to the original and free any trap |
| 1105 | command associated with it. */ |
| 1106 | static void |
| 1107 | restore_signal (sig) |
| 1108 | int sig; |
| 1109 | { |
| 1110 | set_signal_handler (sig, original_signals[sig]); |
| 1111 | change_signal (sig, (char *)DEFAULT_SIG); |
| 1112 | sigmodes[sig] &= ~SIG_TRAPPED; |
| 1113 | } |
| 1114 | |
| 1115 | static void |
| 1116 | reset_or_restore_signal_handlers (reset) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1117 | sh_resetsig_func_t *reset; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1118 | { |
| 1119 | register int i; |
| 1120 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1121 | /* Take care of the exit trap first */ |
| 1122 | if (sigmodes[EXIT_TRAP] & SIG_TRAPPED) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1123 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1124 | sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1125 | if (reset != reset_signal) |
| 1126 | { |
| 1127 | free_trap_command (EXIT_TRAP); |
| 1128 | trap_list[EXIT_TRAP] = (char *)NULL; |
| 1129 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1130 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1131 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1132 | for (i = 1; i < NSIG; i++) |
| 1133 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1134 | if (sigmodes[i] & SIG_TRAPPED) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1135 | { |
| 1136 | if (trap_list[i] == (char *)IGNORE_SIG) |
| 1137 | set_signal_handler (i, SIG_IGN); |
| 1138 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1139 | (*reset) (i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1140 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1141 | else if (sigmodes[i] & SIG_SPECIAL) |
| 1142 | (*reset) (i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1143 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1144 | |
| 1145 | /* Command substitution and other child processes don't inherit the |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1146 | debug, error, or return traps. If we're in the debugger, and the |
| 1147 | `functrace' or `errtrace' options have been set, then let command |
| 1148 | substitutions inherit them. Let command substitution inherit the |
| 1149 | RETURN trap if we're in the debugger and tracing functions. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1150 | if (function_trace_mode == 0) |
| 1151 | { |
| 1152 | sigmodes[DEBUG_TRAP] &= ~SIG_TRAPPED; |
| 1153 | sigmodes[RETURN_TRAP] &= ~SIG_TRAPPED; |
| 1154 | } |
| 1155 | if (error_trace_mode == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 1156 | sigmodes[ERROR_TRAP] &= ~SIG_TRAPPED; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1159 | /* Reset trapped signals to their original values, but don't free the |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1160 | trap strings. Called by the command substitution code and other places |
| 1161 | that create a "subshell environment". */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1162 | void |
| 1163 | reset_signal_handlers () |
| 1164 | { |
| 1165 | reset_or_restore_signal_handlers (reset_signal); |
| 1166 | } |
| 1167 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1168 | /* Reset all trapped signals to their original values. Signals set to be |
| 1169 | ignored with trap '' SIGNAL should be ignored, so we make sure that they |
| 1170 | are. Called by child processes after they are forked. */ |
| 1171 | void |
| 1172 | restore_original_signals () |
| 1173 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1174 | reset_or_restore_signal_handlers (restore_signal); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* If a trap handler exists for signal SIG, then call it; otherwise just |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1178 | return failure. Returns 1 if it called the trap handler. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1179 | int |
| 1180 | maybe_call_trap_handler (sig) |
| 1181 | int sig; |
| 1182 | { |
| 1183 | /* Call the trap handler for SIG if the signal is trapped and not ignored. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1184 | if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1185 | { |
| 1186 | switch (sig) |
| 1187 | { |
| 1188 | case SIGINT: |
| 1189 | run_interrupt_trap (); |
| 1190 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1191 | case EXIT_TRAP: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1192 | run_exit_trap (); |
| 1193 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1194 | case DEBUG_TRAP: |
| 1195 | run_debug_trap (); |
| 1196 | break; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1197 | case ERROR_TRAP: |
| 1198 | run_error_trap (); |
| 1199 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1200 | default: |
| 1201 | trap_handler (sig); |
| 1202 | break; |
| 1203 | } |
| 1204 | return (1); |
| 1205 | } |
| 1206 | else |
| 1207 | return (0); |
| 1208 | } |
| 1209 | |
| 1210 | int |
| 1211 | signal_is_trapped (sig) |
| 1212 | int sig; |
| 1213 | { |
| 1214 | return (sigmodes[sig] & SIG_TRAPPED); |
| 1215 | } |
| 1216 | |
| 1217 | int |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1218 | signal_is_pending (sig) |
| 1219 | int sig; |
| 1220 | { |
| 1221 | return (pending_traps[sig]); |
| 1222 | } |
| 1223 | |
| 1224 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1225 | signal_is_special (sig) |
| 1226 | int sig; |
| 1227 | { |
| 1228 | return (sigmodes[sig] & SIG_SPECIAL); |
| 1229 | } |
| 1230 | |
| 1231 | int |
| 1232 | signal_is_ignored (sig) |
| 1233 | int sig; |
| 1234 | { |
| 1235 | return (sigmodes[sig] & SIG_IGNORED); |
| 1236 | } |
| 1237 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 1238 | int |
| 1239 | signal_is_hard_ignored (sig) |
| 1240 | int sig; |
| 1241 | { |
| 1242 | return (sigmodes[sig] & SIG_HARD_IGNORE); |
| 1243 | } |
| 1244 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1245 | void |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1246 | set_signal_hard_ignored (sig) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1247 | int sig; |
| 1248 | { |
| 1249 | sigmodes[sig] |= SIG_HARD_IGNORE; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1250 | original_signals[sig] = SIG_IGN; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1251 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1252 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1253 | void |
| 1254 | set_signal_ignored (sig) |
| 1255 | int sig; |
| 1256 | { |
| 1257 | original_signals[sig] = SIG_IGN; |
| 1258 | } |
| 1259 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1260 | int |
| 1261 | signal_in_progress (sig) |
| 1262 | int sig; |
| 1263 | { |
| 1264 | return (sigmodes[sig] & SIG_INPROGRESS); |
| 1265 | } |