1 /* sig.c - interface for shell signal handlers and signal initialization. */
3 /* Copyright (C) 1994-2010 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
25 #if defined (HAVE_UNISTD_H)
27 # include <sys/types.h>
38 #if defined (JOB_CONTROL)
40 #endif /* JOB_CONTROL */
45 #include "builtins/common.h"
47 #if defined (READLINE)
48 # include "bashline.h"
52 # include "bashhist.h"
55 extern int last_command_exit_value;
56 extern int last_command_exit_signal;
57 extern int return_catch_flag;
58 extern int loop_level, continuing, breaking, funcnest;
59 extern int executing_list;
60 extern int comsub_ignore_return;
61 extern int parse_and_execute_level, shell_initialized;
63 extern int history_lines_this_session;
66 extern void initialize_siglist ();
68 /* Non-zero after SIGINT. */
69 volatile int interrupt_state = 0;
71 /* Non-zero after SIGWINCH */
72 volatile int sigwinch_received = 0;
74 /* Set to the value of any terminating signal received. */
75 volatile int terminating_signal = 0;
77 /* The environment at the top-level R-E loop. We use this in
78 the case of error return. */
81 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
82 /* The signal masks that this shell runs with. */
83 sigset_t top_level_mask;
84 #endif /* JOB_CONTROL */
86 /* When non-zero, we throw_to_top_level (). */
87 int interrupt_immediately = 0;
89 /* When non-zero, we call the terminating signal handler immediately. */
90 int terminate_immediately = 0;
92 #if defined (SIGWINCH)
93 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
96 static void initialize_shell_signals __P((void));
99 initialize_signals (reinit)
102 initialize_shell_signals ();
103 initialize_job_signals ();
104 #if !defined (HAVE_SYS_SIGLIST) && !defined (HAVE_UNDER_SYS_SIGLIST) && !defined (HAVE_STRSIGNAL)
106 initialize_siglist ();
107 #endif /* !HAVE_SYS_SIGLIST && !HAVE_UNDER_SYS_SIGLIST && !HAVE_STRSIGNAL */
110 /* A structure describing a signal that terminates the shell if not
111 caught. The orig_handler member is present so children can reset
112 these signals back to their original handlers. */
115 SigHandler *orig_handler;
119 #define NULL_HANDLER (SigHandler *)SIG_DFL
121 /* The list of signals that would terminate the shell if not caught.
122 We catch them, but just so that we can write the history file,
124 static struct termsig terminating_signals[] = {
126 { SIGHUP, NULL_HANDLER, 0 },
130 { SIGINT, NULL_HANDLER, 0 },
134 { SIGILL, NULL_HANDLER, 0 },
138 { SIGTRAP, NULL_HANDLER, 0 },
142 { SIGIOT, NULL_HANDLER, 0 },
146 { SIGDANGER, NULL_HANDLER, 0 },
150 { SIGEMT, NULL_HANDLER, 0 },
154 { SIGFPE, NULL_HANDLER, 0 },
158 { SIGBUS, NULL_HANDLER, 0 },
162 { SIGSEGV, NULL_HANDLER, 0 },
166 { SIGSYS, NULL_HANDLER, 0 },
170 { SIGPIPE, NULL_HANDLER, 0 },
174 { SIGALRM, NULL_HANDLER, 0 },
178 { SIGTERM, NULL_HANDLER, 0 },
182 { SIGXCPU, NULL_HANDLER, 0 },
186 { SIGXFSZ, NULL_HANDLER, 0 },
190 { SIGVTALRM, NULL_HANDLER, 0 },
195 { SIGPROF, NULL_HANDLER, 0 },
200 { SIGLOST, NULL_HANDLER, 0 },
204 { SIGUSR1, NULL_HANDLER, 0 },
208 { SIGUSR2, NULL_HANDLER, 0 },
212 #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (struct termsig))
214 #define XSIG(x) (terminating_signals[x].signum)
215 #define XHANDLER(x) (terminating_signals[x].orig_handler)
216 #define XSAFLAGS(x) (terminating_signals[x].orig_flags)
218 static int termsigs_initialized = 0;
220 /* Initialize signals that will terminate the shell to do some
221 unwind protection. For non-interactive shells, we only call
222 this when a trap is defined for EXIT (0) or when trap is run
223 to display signal dispositions. */
225 initialize_terminating_signals ()
228 #if defined (HAVE_POSIX_SIGNALS)
229 struct sigaction act, oact;
232 if (termsigs_initialized)
235 /* The following code is to avoid an expensive call to
236 set_signal_handler () for each terminating_signals. Fortunately,
237 this is possible in Posix. Unfortunately, we have to call signal ()
238 on non-Posix systems for each signal in terminating_signals. */
239 #if defined (HAVE_POSIX_SIGNALS)
240 act.sa_handler = termsig_sighandler;
242 sigemptyset (&act.sa_mask);
243 sigemptyset (&oact.sa_mask);
244 for (i = 0; i < TERMSIGS_LENGTH; i++)
245 sigaddset (&act.sa_mask, XSIG (i));
246 for (i = 0; i < TERMSIGS_LENGTH; i++)
248 /* If we've already trapped it, don't do anything. */
249 if (signal_is_trapped (XSIG (i)))
252 sigaction (XSIG (i), &act, &oact);
253 XHANDLER(i) = oact.sa_handler;
254 XSAFLAGS(i) = oact.sa_flags;
255 /* Don't do anything with signals that are ignored at shell entry
256 if the shell is not interactive. */
257 /* XXX - should we do this for interactive shells, too? */
258 if (interactive_shell == 0 && XHANDLER (i) == SIG_IGN)
260 sigaction (XSIG (i), &oact, &act);
261 set_signal_ignored (XSIG (i));
263 #if defined (SIGPROF) && !defined (_MINIX)
264 if (XSIG (i) == SIGPROF && XHANDLER (i) != SIG_DFL && XHANDLER (i) != SIG_IGN)
265 sigaction (XSIG (i), &oact, (struct sigaction *)NULL);
266 #endif /* SIGPROF && !_MINIX */
269 #else /* !HAVE_POSIX_SIGNALS */
271 for (i = 0; i < TERMSIGS_LENGTH; i++)
273 /* If we've already trapped it, don't do anything. */
274 if (signal_is_trapped (XSIG (i)))
277 XHANDLER(i) = signal (XSIG (i), termsig_sighandler);
279 /* Don't do anything with signals that are ignored at shell entry
280 if the shell is not interactive. */
281 /* XXX - should we do this for interactive shells, too? */
282 if (interactive_shell == 0 && XHANDLER (i) == SIG_IGN)
284 signal (XSIG (i), SIG_IGN);
285 set_signal_ignored (XSIG (i));
288 if (XSIG (i) == SIGPROF && XHANDLER (i) != SIG_DFL && XHANDLER (i) != SIG_IGN)
289 signal (XSIG (i), XHANDLER (i));
293 #endif /* !HAVE_POSIX_SIGNALS */
295 termsigs_initialized = 1;
299 initialize_shell_signals ()
302 initialize_terminating_signals ();
304 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
305 /* All shells use the signal mask they inherit, and pass it along
306 to child processes. Children will never block SIGCHLD, though. */
307 sigemptyset (&top_level_mask);
308 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &top_level_mask);
309 # if defined (SIGCHLD)
310 sigdelset (&top_level_mask, SIGCHLD);
312 #endif /* JOB_CONTROL || HAVE_POSIX_SIGNALS */
314 /* And, some signals that are specifically ignored by the shell. */
315 set_signal_handler (SIGQUIT, SIG_IGN);
319 set_signal_handler (SIGINT, sigint_sighandler);
320 set_signal_handler (SIGTERM, SIG_IGN);
321 set_sigwinch_handler ();
326 reset_terminating_signals ()
329 #if defined (HAVE_POSIX_SIGNALS)
330 struct sigaction act;
333 if (termsigs_initialized == 0)
336 #if defined (HAVE_POSIX_SIGNALS)
338 sigemptyset (&act.sa_mask);
339 for (i = 0; i < TERMSIGS_LENGTH; i++)
341 /* Skip a signal if it's trapped or handled specially, because the
342 trap code will restore the correct value. */
343 if (signal_is_trapped (XSIG (i)) || signal_is_special (XSIG (i)))
346 act.sa_handler = XHANDLER (i);
347 act.sa_flags = XSAFLAGS (i);
348 sigaction (XSIG (i), &act, (struct sigaction *) NULL);
350 #else /* !HAVE_POSIX_SIGNALS */
351 for (i = 0; i < TERMSIGS_LENGTH; i++)
353 if (signal_is_trapped (XSIG (i)) || signal_is_special (XSIG (i)))
356 signal (XSIG (i), XHANDLER (i));
358 #endif /* !HAVE_POSIX_SIGNALS */
363 /* Run some of the cleanups that should be performed when we run
364 jump_to_top_level from a builtin command context. XXX - might want to
365 also call reset_parser here. */
369 /* Clean up string parser environment. */
370 while (parse_and_execute_level)
371 parse_and_execute_cleanup ();
373 #if defined (PROCESS_SUBSTITUTION)
375 #endif /* PROCESS_SUBSTITUTION */
377 run_unwind_protects ();
378 loop_level = continuing = breaking = funcnest = 0;
379 executing_list = comsub_ignore_return = return_catch_flag = 0;
382 /* What to do when we've been interrupted, and it is safe to handle it. */
384 throw_to_top_level ()
386 int print_newline = 0;
397 last_command_exit_signal = (last_command_exit_value > 128) ?
398 (last_command_exit_value - 128) : 0;
399 last_command_exit_value |= 128;
401 /* Run any traps set on SIGINT. */
402 run_interrupt_trap ();
404 /* Clean up string parser environment. */
405 while (parse_and_execute_level)
406 parse_and_execute_cleanup ();
408 #if defined (JOB_CONTROL)
409 give_terminal_to (shell_pgrp, 0);
410 #endif /* JOB_CONTROL */
412 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
413 /* This should not be necessary on systems using sigsetjmp/siglongjmp. */
414 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
419 #if defined (READLINE)
422 #endif /* READLINE */
424 #if defined (PROCESS_SUBSTITUTION)
426 #endif /* PROCESS_SUBSTITUTION */
428 run_unwind_protects ();
429 loop_level = continuing = breaking = funcnest = 0;
430 executing_list = comsub_ignore_return = return_catch_flag = 0;
432 if (interactive && print_newline)
435 fprintf (stderr, "\n");
439 /* An interrupted `wait' command in a script does not exit the script. */
440 if (interactive || (interactive_shell && !shell_initialized) ||
441 (print_newline && signal_is_trapped (SIGINT)))
442 jump_to_top_level (DISCARD);
444 jump_to_top_level (EXITPROG);
447 /* This is just here to isolate the longjmp calls. */
449 jump_to_top_level (value)
452 longjmp (top_level, value);
456 termsig_sighandler (sig)
459 /* If we get called twice with the same signal before handling it,
460 terminate right away. */
498 sig == terminating_signal)
499 terminate_immediately = 1;
501 terminating_signal = sig;
503 /* XXX - should this also trigger when interrupt_immediately is set? */
504 if (terminate_immediately)
506 #if defined (HISTORY)
507 /* XXX - will inhibit history file being written */
508 history_lines_this_session = 0;
510 terminate_immediately = 0;
511 termsig_handler (sig);
518 termsig_handler (sig)
521 static int handling_termsig = 0;
523 /* Simple semaphore to keep this function from being executed multiple
524 times. Since we no longer are running as a signal handler, we don't
525 block multiple occurrences of the terminating signals while running. */
526 if (handling_termsig)
528 handling_termsig = 1;
529 terminating_signal = 0; /* keep macro from re-testing true. */
531 /* I don't believe this condition ever tests true. */
532 if (sig == SIGINT && signal_is_trapped (SIGINT))
533 run_interrupt_trap ();
535 #if defined (HISTORY)
536 if (interactive_shell && sig != SIGABRT)
537 maybe_save_shell_history ();
540 #if defined (JOB_CONTROL)
541 if (sig == SIGHUP && (interactive || (subshell_environment & (SUBSHELL_COMSUB|SUBSHELL_PROCSUB))))
544 #endif /* JOB_CONTROL */
546 #if defined (PROCESS_SUBSTITUTION)
548 #endif /* PROCESS_SUBSTITUTION */
550 /* Reset execution context */
551 loop_level = continuing = breaking = funcnest = 0;
552 executing_list = comsub_ignore_return = return_catch_flag = 0;
555 set_signal_handler (sig, SIG_DFL);
556 kill (getpid (), sig);
559 /* What we really do when SIGINT occurs. */
561 sigint_sighandler (sig)
564 #if defined (MUST_REINSTALL_SIGHANDLERS)
565 signal (sig, sigint_sighandler);
568 /* interrupt_state needs to be set for the stack of interrupts to work
569 right. Should it be set unconditionally? */
570 if (interrupt_state == 0)
573 if (interrupt_immediately)
575 interrupt_immediately = 0;
576 last_command_exit_value = 128 + sig;
577 throw_to_top_level ();
583 #if defined (SIGWINCH)
585 sigwinch_sighandler (sig)
588 #if defined (MUST_REINSTALL_SIGHANDLERS)
589 set_signal_handler (SIGWINCH, sigwinch_sighandler);
590 #endif /* MUST_REINSTALL_SIGHANDLERS */
591 sigwinch_received = 1;
594 #endif /* SIGWINCH */
597 set_sigwinch_handler ()
599 #if defined (SIGWINCH)
600 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
605 unset_sigwinch_handler ()
607 #if defined (SIGWINCH)
608 set_signal_handler (SIGWINCH, old_winch);
612 /* Signal functions used by the rest of the code. */
613 #if !defined (HAVE_POSIX_SIGNALS)
615 #if defined (JOB_CONTROL)
616 /* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
617 sigprocmask (operation, newset, oldset)
618 int operation, *newset, *oldset;
630 old = sigblock (new);
638 internal_error (_("sigprocmask: %d: invalid operation"), operation);
644 #endif /* JOB_CONTROL */
648 #if !defined (SA_INTERRUPT)
649 # define SA_INTERRUPT 0
652 #if !defined (SA_RESTART)
653 # define SA_RESTART 0
657 set_signal_handler (sig, handler)
661 struct sigaction act, oact;
663 act.sa_handler = handler;
667 /* We don't want a child death to interrupt interruptible system calls, even
668 if we take the time to reap children */
670 act.sa_flags |= SA_RESTART; /* XXX */
672 sigemptyset (&act.sa_mask);
673 sigemptyset (&oact.sa_mask);
674 sigaction (sig, &act, &oact);
675 return (oact.sa_handler);
677 #endif /* HAVE_POSIX_SIGNALS */