1 /* sig.c - interface for shell signal handlers and signal initialization. */
3 /* Copyright (C) 1994-2018 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 #include "execute_cmd.h"
39 #if defined (JOB_CONTROL)
41 #endif /* JOB_CONTROL */
46 #include "builtins/common.h"
47 #include "builtins/builtext.h"
49 #if defined (READLINE)
50 # include "bashline.h"
51 # include <readline/readline.h>
55 # include "bashhist.h"
58 extern void initialize_siglist ();
60 #if !defined (JOB_CONTROL)
61 extern void initialize_job_signals
__P((void));
64 /* Non-zero after SIGINT. */
65 volatile sig_atomic_t interrupt_state
= 0;
67 /* Non-zero after SIGWINCH */
68 volatile sig_atomic_t sigwinch_received
= 0;
70 /* Non-zero after SIGTERM */
71 volatile sig_atomic_t sigterm_received
= 0;
73 /* Set to the value of any terminating signal received. */
74 volatile sig_atomic_t terminating_signal
= 0;
76 /* The environment at the top-level R-E loop. We use this in
77 the case of error return. */
80 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
81 /* The signal masks that this shell runs with. */
82 sigset_t top_level_mask
;
83 #endif /* JOB_CONTROL */
85 /* When non-zero, we throw_to_top_level (). */
86 int interrupt_immediately
= 0;
88 /* When non-zero, we call the terminating signal handler immediately. */
89 int terminate_immediately
= 0;
91 #if defined (SIGWINCH)
92 static SigHandler
*old_winch
= (SigHandler
*)SIG_DFL
;
95 static void initialize_shell_signals
__P((void));
98 initialize_signals (reinit
)
101 initialize_shell_signals ();
102 initialize_job_signals ();
103 #if !defined (HAVE_SYS_SIGLIST) && !defined (HAVE_UNDER_SYS_SIGLIST) && !defined (HAVE_STRSIGNAL)
105 initialize_siglist ();
106 #endif /* !HAVE_SYS_SIGLIST && !HAVE_UNDER_SYS_SIGLIST && !HAVE_STRSIGNAL */
109 /* A structure describing a signal that terminates the shell if not
110 caught. The orig_handler member is present so children can reset
111 these signals back to their original handlers. */
114 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, 1},
138 { SIGTRAP
, NULL_HANDLER
, 0, 1 },
142 { SIGIOT
, NULL_HANDLER
, 0, 1 },
146 { SIGDANGER
, NULL_HANDLER
, 0 },
150 { SIGEMT
, NULL_HANDLER
, 0 },
154 { SIGFPE
, NULL_HANDLER
, 0, 1 },
158 { SIGBUS
, NULL_HANDLER
, 0, 1 },
162 { SIGSEGV
, NULL_HANDLER
, 0, 1 },
166 { SIGSYS
, NULL_HANDLER
, 0, 1 },
170 { SIGPIPE
, NULL_HANDLER
, 0 },
174 { SIGALRM
, NULL_HANDLER
, 0 },
178 { SIGTERM
, NULL_HANDLER
, 0 },
181 /* These don't generate core dumps on anything but Linux, but we're doing
182 this just for Linux anyway. */
184 { SIGXCPU
, NULL_HANDLER
, 0, 1 },
188 { SIGXFSZ
, NULL_HANDLER
, 0, 1 },
192 { SIGVTALRM
, NULL_HANDLER
, 0 },
197 { SIGPROF
, NULL_HANDLER
, 0 },
202 { SIGLOST
, NULL_HANDLER
, 0 },
206 { SIGUSR1
, NULL_HANDLER
, 0 },
210 { SIGUSR2
, NULL_HANDLER
, 0 },
214 #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (struct termsig))
216 #define XSIG(x) (terminating_signals[x].signum)
217 #define XHANDLER(x) (terminating_signals[x].orig_handler)
218 #define XSAFLAGS(x) (terminating_signals[x].orig_flags)
219 #define XCOREDUMP(x) (terminating_signals[x].core_dump)
221 static int termsigs_initialized
= 0;
223 /* Initialize signals that will terminate the shell to do some
224 unwind protection. For non-interactive shells, we only call
225 this when a trap is defined for EXIT (0) or when trap is run
226 to display signal dispositions. */
228 initialize_terminating_signals ()
231 #if defined (HAVE_POSIX_SIGNALS)
232 struct sigaction act
, oact
;
235 if (termsigs_initialized
)
238 /* The following code is to avoid an expensive call to
239 set_signal_handler () for each terminating_signals. Fortunately,
240 this is possible in Posix. Unfortunately, we have to call signal ()
241 on non-Posix systems for each signal in terminating_signals. */
242 #if defined (HAVE_POSIX_SIGNALS)
243 act
.sa_handler
= termsig_sighandler
;
245 sigemptyset (&act
.sa_mask
);
246 sigemptyset (&oact
.sa_mask
);
247 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
248 sigaddset (&act
.sa_mask
, XSIG (i
));
249 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
251 /* If we've already trapped it, don't do anything. */
252 if (signal_is_trapped (XSIG (i
)))
255 sigaction (XSIG (i
), &act
, &oact
);
256 XHANDLER(i
) = oact
.sa_handler
;
257 XSAFLAGS(i
) = oact
.sa_flags
;
258 /* Don't do anything with signals that are ignored at shell entry
259 if the shell is not interactive. */
260 /* XXX - should we do this for interactive shells, too? */
261 if (interactive_shell
== 0 && XHANDLER (i
) == SIG_IGN
)
263 sigaction (XSIG (i
), &oact
, &act
);
264 set_signal_hard_ignored (XSIG (i
));
266 #if defined (SIGPROF) && !defined (_MINIX)
267 if (XSIG (i
) == SIGPROF
&& XHANDLER (i
) != SIG_DFL
&& XHANDLER (i
) != SIG_IGN
)
268 sigaction (XSIG (i
), &oact
, (struct sigaction
*)NULL
);
269 #endif /* SIGPROF && !_MINIX */
271 #else /* !HAVE_POSIX_SIGNALS */
273 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
275 /* If we've already trapped it, don't do anything. */
276 if (signal_is_trapped (XSIG (i
)))
279 XHANDLER(i
) = signal (XSIG (i
), termsig_sighandler
);
281 /* Don't do anything with signals that are ignored at shell entry
282 if the shell is not interactive. */
283 /* XXX - should we do this for interactive shells, too? */
284 if (interactive_shell
== 0 && XHANDLER (i
) == SIG_IGN
)
286 signal (XSIG (i
), SIG_IGN
);
287 set_signal_hard_ignored (XSIG (i
));
290 if (XSIG (i
) == SIGPROF
&& XHANDLER (i
) != SIG_DFL
&& XHANDLER (i
) != SIG_IGN
)
291 signal (XSIG (i
), XHANDLER (i
));
295 #endif /* !HAVE_POSIX_SIGNALS */
297 termsigs_initialized
= 1;
301 initialize_shell_signals ()
304 initialize_terminating_signals ();
306 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
307 /* All shells use the signal mask they inherit, and pass it along
308 to child processes. Children will never block SIGCHLD, though. */
309 sigemptyset (&top_level_mask
);
310 sigprocmask (SIG_BLOCK
, (sigset_t
*)NULL
, &top_level_mask
);
311 # if defined (SIGCHLD)
312 sigdelset (&top_level_mask
, SIGCHLD
);
314 #endif /* JOB_CONTROL || HAVE_POSIX_SIGNALS */
316 /* And, some signals that are specifically ignored by the shell. */
317 set_signal_handler (SIGQUIT
, SIG_IGN
);
321 set_signal_handler (SIGINT
, sigint_sighandler
);
322 get_original_signal (SIGTERM
);
323 if (signal_is_hard_ignored (SIGTERM
) == 0)
324 set_signal_handler (SIGTERM
, sigterm_sighandler
);
325 set_sigwinch_handler ();
330 reset_terminating_signals ()
333 #if defined (HAVE_POSIX_SIGNALS)
334 struct sigaction act
;
337 if (termsigs_initialized
== 0)
340 #if defined (HAVE_POSIX_SIGNALS)
342 sigemptyset (&act
.sa_mask
);
343 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
345 /* Skip a signal if it's trapped or handled specially, because the
346 trap code will restore the correct value. */
347 if (signal_is_trapped (XSIG (i
)) || signal_is_special (XSIG (i
)))
350 act
.sa_handler
= XHANDLER (i
);
351 act
.sa_flags
= XSAFLAGS (i
);
352 sigaction (XSIG (i
), &act
, (struct sigaction
*) NULL
);
354 #else /* !HAVE_POSIX_SIGNALS */
355 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
357 if (signal_is_trapped (XSIG (i
)) || signal_is_special (XSIG (i
)))
360 signal (XSIG (i
), XHANDLER (i
));
362 #endif /* !HAVE_POSIX_SIGNALS */
364 termsigs_initialized
= 0;
368 /* Run some of the cleanups that should be performed when we run
369 jump_to_top_level from a builtin command context. XXX - might want to
370 also call reset_parser here. */
374 /* Clean up string parser environment. */
375 while (parse_and_execute_level
)
376 parse_and_execute_cleanup (-1);
378 #if defined (PROCESS_SUBSTITUTION)
380 #endif /* PROCESS_SUBSTITUTION */
382 run_unwind_protects ();
383 loop_level
= continuing
= breaking
= funcnest
= 0;
384 executing_list
= comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
387 /* What to do when we've been interrupted, and it is safe to handle it. */
389 throw_to_top_level ()
391 int print_newline
= 0;
395 if (last_command_exit_value
< 128)
396 last_command_exit_value
= 128 + SIGINT
;
404 last_command_exit_signal
= (last_command_exit_value
> 128) ?
405 (last_command_exit_value
- 128) : 0;
406 last_command_exit_value
|= 128;
408 /* Run any traps set on SIGINT, mostly for interactive shells */
409 if (signal_is_trapped (SIGINT
))
410 run_interrupt_trap (1);
412 /* Clean up string parser environment. */
413 while (parse_and_execute_level
)
414 parse_and_execute_cleanup (-1);
416 if (running_trap
> 0)
417 run_trap_cleanup (running_trap
- 1);
419 #if defined (JOB_CONTROL)
420 give_terminal_to (shell_pgrp
, 0);
421 #endif /* JOB_CONTROL */
423 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
424 /* This needs to stay because jobs.c:make_child() uses it without resetting
426 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
431 #if defined (READLINE)
434 #endif /* READLINE */
436 #if defined (PROCESS_SUBSTITUTION)
438 #endif /* PROCESS_SUBSTITUTION */
440 run_unwind_protects ();
441 loop_level
= continuing
= breaking
= funcnest
= 0;
442 executing_list
= comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
444 if (interactive
&& print_newline
)
447 fprintf (stderr
, "\n");
451 /* An interrupted `wait' command in a script does not exit the script. */
452 if (interactive
|| (interactive_shell
&& !shell_initialized
) ||
453 (print_newline
&& signal_is_trapped (SIGINT
)))
454 jump_to_top_level (DISCARD
);
456 jump_to_top_level (EXITPROG
);
459 /* This is just here to isolate the longjmp calls. */
461 jump_to_top_level (value
)
464 sh_longjmp (top_level
, value
);
468 termsig_sighandler (sig
)
471 /* If we get called twice with the same signal before handling it,
472 terminate right away. */
510 sig
== terminating_signal
)
511 terminate_immediately
= 1;
513 terminating_signal
= sig
;
515 /* XXX - should this also trigger when interrupt_immediately is set? */
516 if (terminate_immediately
)
518 #if defined (HISTORY)
519 /* XXX - will inhibit history file being written */
520 # if defined (READLINE)
521 if (interactive_shell
== 0 || interactive
== 0 || (sig
!= SIGHUP
&& sig
!= SIGTERM
) || no_line_editing
|| (RL_ISSTATE (RL_STATE_READCMD
) == 0))
523 history_lines_this_session
= 0;
525 terminate_immediately
= 0;
526 termsig_handler (sig
);
529 #if defined (READLINE)
530 /* Set the event hook so readline will call it after the signal handlers
531 finish executing, so if this interrupted character input we can get
532 quick response. If readline is active or has modified the terminal we
533 need to set this no matter what the signal is, though the check for
534 RL_STATE_TERMPREPPED is possibly redundant. */
535 if (RL_ISSTATE (RL_STATE_SIGHANDLER
) || RL_ISSTATE (RL_STATE_TERMPREPPED
))
536 bashline_set_event_hook ();
543 termsig_handler (sig
)
546 static int handling_termsig
= 0;
550 /* Simple semaphore to keep this function from being executed multiple
551 times. Since we no longer are running as a signal handler, we don't
552 block multiple occurrences of the terminating signals while running. */
553 if (handling_termsig
)
555 handling_termsig
= 1;
556 terminating_signal
= 0; /* keep macro from re-testing true. */
558 /* I don't believe this condition ever tests true. */
559 if (sig
== SIGINT
&& signal_is_trapped (SIGINT
))
560 run_interrupt_trap (0);
562 #if defined (HISTORY)
563 /* If we don't do something like this, the history will not be saved when
564 an interactive shell is running in a terminal window that gets closed
565 with the `close' button. We can't test for RL_STATE_READCMD because
566 readline no longer handles SIGTERM synchronously. */
567 if (interactive_shell
&& interactive
&& (sig
== SIGHUP
|| sig
== SIGTERM
) && remember_on_history
)
568 maybe_save_shell_history ();
571 if (this_shell_builtin
== read_builtin
)
574 #if defined (JOB_CONTROL)
575 if (sig
== SIGHUP
&& (interactive
|| (subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
))))
578 if ((subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
)) == 0)
580 #endif /* JOB_CONTROL */
582 #if defined (PROCESS_SUBSTITUTION)
584 #endif /* PROCESS_SUBSTITUTION */
586 /* Reset execution context */
587 loop_level
= continuing
= breaking
= funcnest
= 0;
588 executing_list
= comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
590 run_exit_trap (); /* XXX - run exit trap possibly in signal context? */
592 /* We don't change the set of blocked signals. If a user starts the shell
593 with a terminating signal blocked, we won't get here (and if by some
594 magic chance we do, we'll exit below). */
595 set_signal_handler (sig
, SIG_DFL
);
597 kill (getpid (), sig
);
599 if (dollar_dollar_pid
!= 1)
600 exit (128+sig
); /* just in case the kill fails? */
602 /* We get here only under extraordinary circumstances. */
604 /* We are PID 1, and the kill above failed to kill the process. We assume
605 this means that we are running as an init process in a pid namespace
606 on Linux. In this case, we can't send ourselves a fatal signal, so we
607 determine whether or not we should have generated a core dump with the
608 kill call and attempt to trick the kernel into generating one if
610 sigprocmask (SIG_SETMASK
, (sigset_t
*)NULL
, &mask
);
611 for (i
= core
= 0; i
< TERMSIGS_LENGTH
; i
++)
613 set_signal_handler (XSIG (i
), SIG_DFL
);
614 sigdelset (&mask
, XSIG (i
));
616 core
= XCOREDUMP (i
);
618 sigprocmask (SIG_SETMASK
, &mask
, (sigset_t
*)NULL
);
621 *((volatile unsigned long *) NULL
) = 0xdead0000 + sig
; /* SIGSEGV */
627 /* What we really do when SIGINT occurs. */
629 sigint_sighandler (sig
)
632 #if defined (MUST_REINSTALL_SIGHANDLERS)
633 signal (sig
, sigint_sighandler
);
636 /* interrupt_state needs to be set for the stack of interrupts to work
637 right. Should it be set unconditionally? */
638 if (interrupt_state
== 0)
641 /* We will get here in interactive shells with job control active; allow
642 an interactive wait to be interrupted. wait_intr_flag is only set during
643 the execution of the wait builtin and when wait_intr_buf is valid. */
646 last_command_exit_value
= 128 + sig
;
647 wait_signal_received
= sig
;
651 if (interrupt_immediately
)
653 interrupt_immediately
= 0;
654 last_command_exit_value
= 128 + sig
;
655 throw_to_top_level ();
657 #if defined (READLINE)
658 /* Set the event hook so readline will call it after the signal handlers
659 finish executing, so if this interrupted character input we can get
661 else if (RL_ISSTATE (RL_STATE_SIGHANDLER
))
662 bashline_set_event_hook ();
668 #if defined (SIGWINCH)
670 sigwinch_sighandler (sig
)
673 #if defined (MUST_REINSTALL_SIGHANDLERS)
674 set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
675 #endif /* MUST_REINSTALL_SIGHANDLERS */
676 sigwinch_received
= 1;
679 #endif /* SIGWINCH */
682 set_sigwinch_handler ()
684 #if defined (SIGWINCH)
685 old_winch
= set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
690 unset_sigwinch_handler ()
692 #if defined (SIGWINCH)
693 set_signal_handler (SIGWINCH
, old_winch
);
698 sigterm_sighandler (sig
)
701 sigterm_received
= 1; /* XXX - counter? */
705 /* Signal functions used by the rest of the code. */
706 #if !defined (HAVE_POSIX_SIGNALS)
708 /* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
709 sigprocmask (operation
, newset
, oldset
)
710 int operation
, *newset
, *oldset
;
722 old
= sigblock (new);
726 old
= sigsetmask (new);
730 internal_error (_("sigprocmask: %d: invalid operation"), operation
);
739 #if !defined (SA_INTERRUPT)
740 # define SA_INTERRUPT 0
743 #if !defined (SA_RESTART)
744 # define SA_RESTART 0
748 set_signal_handler (sig
, handler
)
752 struct sigaction act
, oact
;
754 act
.sa_handler
= handler
;
758 /* We don't want a child death to interrupt interruptible system calls, even
759 if we take the time to reap children */
760 #if defined (SIGCHLD)
762 act
.sa_flags
|= SA_RESTART
; /* XXX */
764 /* Let's see if we can keep SIGWINCH from interrupting interruptible system
765 calls, like open(2)/read(2)/write(2) */
766 #if defined (SIGWINCH)
768 act
.sa_flags
|= SA_RESTART
; /* XXX */
770 /* If we're installing a SIGTERM handler for interactive shells, we want
771 it to be as close to SIG_IGN as possible. */
772 if (sig
== SIGTERM
&& handler
== sigterm_sighandler
)
773 act
.sa_flags
|= SA_RESTART
; /* XXX */
775 sigemptyset (&act
.sa_mask
);
776 sigemptyset (&oact
.sa_mask
);
777 if (sigaction (sig
, &act
, &oact
) == 0)
778 return (oact
.sa_handler
);
782 #endif /* HAVE_POSIX_SIGNALS */