1 /* sig.c - interface for shell signal handlers and signal initialization. */
3 /* Copyright (C) 1994-2024 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 initialize_shell_signals (# 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 (void);
59 extern void set_original_signal (int, SigHandler
*);
61 #if !defined (JOB_CONTROL)
62 extern void initialize_job_signals (void);
65 /* Non-zero after SIGINT. */
66 volatile sig_atomic_t interrupt_state
= 0;
68 /* Non-zero after SIGWINCH */
69 volatile sig_atomic_t sigwinch_received
= 0;
71 /* Non-zero after SIGTERM */
72 volatile sig_atomic_t sigterm_received
= 0;
74 /* Set to the value of any terminating signal received. */
75 volatile sig_atomic_t terminating_signal
= 0;
77 volatile int builtin_catch_sigpipe
= 0;
79 /* The environment at the top-level R-E loop. We use this in
80 the case of error return. */
83 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
84 /* The signal masks that this shell runs with. */
85 sigset_t top_level_mask
;
86 #endif /* JOB_CONTROL */
88 /* When non-zero, we throw_to_top_level (). */
89 int interrupt_immediately
= 0;
91 /* When non-zero, we call the terminating signal handler immediately. */
92 int terminate_immediately
= 0;
94 #if defined (SIGWINCH)
95 static SigHandler
*old_winch
= (SigHandler
*)SIG_DFL
;
98 static void initialize_shell_signals (void);
99 static void kill_shell (int);
102 initialize_signals (int reinit
)
104 initialize_shell_signals ();
105 initialize_job_signals ();
106 #if !defined (HAVE_SYS_SIGLIST) && !defined (HAVE_UNDER_SYS_SIGLIST) && !defined (HAVE_STRSIGNAL)
108 initialize_siglist ();
109 #endif /* !HAVE_SYS_SIGLIST && !HAVE_UNDER_SYS_SIGLIST && !HAVE_STRSIGNAL */
112 /* A structure describing a signal that terminates the shell if not
113 caught. The orig_handler member is present so children can reset
114 these signals back to their original handlers. */
117 SigHandler
*orig_handler
;
122 #define NULL_HANDLER (SigHandler *)SIG_DFL
124 /* The list of signals that would terminate the shell if not caught.
125 We catch them, but just so that we can write the history file,
127 static struct termsig terminating_signals
[] = {
129 { SIGHUP
, NULL_HANDLER
, 0 },
133 { SIGINT
, NULL_HANDLER
, 0 },
137 { SIGILL
, NULL_HANDLER
, 0, 1},
141 { SIGTRAP
, NULL_HANDLER
, 0, 1 },
145 { SIGIOT
, NULL_HANDLER
, 0, 1 },
149 { SIGDANGER
, NULL_HANDLER
, 0 },
153 { SIGEMT
, NULL_HANDLER
, 0 },
157 { SIGFPE
, NULL_HANDLER
, 0, 1 },
161 { SIGBUS
, NULL_HANDLER
, 0, 1 },
165 { SIGSEGV
, NULL_HANDLER
, 0, 1 },
169 { SIGSYS
, NULL_HANDLER
, 0, 1 },
173 { SIGPIPE
, NULL_HANDLER
, 0 },
177 { SIGALRM
, NULL_HANDLER
, 0 },
181 { SIGTERM
, NULL_HANDLER
, 0 },
184 /* These don't generate core dumps on anything but Linux, but we're doing
185 this just for Linux anyway. */
187 { SIGXCPU
, NULL_HANDLER
, 0, 1 },
191 { SIGXFSZ
, NULL_HANDLER
, 0, 1 },
195 { SIGVTALRM
, NULL_HANDLER
, 0 },
200 { SIGPROF
, NULL_HANDLER
, 0 },
205 { SIGLOST
, NULL_HANDLER
, 0 },
209 { SIGUSR1
, NULL_HANDLER
, 0 },
213 { SIGUSR2
, NULL_HANDLER
, 0 },
217 #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (struct termsig))
219 #define XSIG(x) (terminating_signals[x].signum)
220 #define XHANDLER(x) (terminating_signals[x].orig_handler)
221 #define XSAFLAGS(x) (terminating_signals[x].orig_flags)
222 #define XCOREDUMP(x) (terminating_signals[x].core_dump)
224 static int termsigs_initialized
= 0;
226 /* Initialize signals that will terminate the shell to do some
227 unwind protection. For non-interactive shells, we only call
228 this when a trap is defined for EXIT (0) or when trap is run
229 to display signal dispositions. */
231 initialize_terminating_signals (void)
234 #if defined (HAVE_POSIX_SIGNALS)
235 struct sigaction act
, oact
;
238 if (termsigs_initialized
)
241 /* The following code is to avoid an expensive call to
242 set_signal_handler () for each terminating_signals. Fortunately,
243 this is possible in Posix. Unfortunately, we have to call signal ()
244 on non-Posix systems for each signal in terminating_signals. */
245 #if defined (HAVE_POSIX_SIGNALS)
246 act
.sa_handler
= termsig_sighandler
;
248 sigemptyset (&act
.sa_mask
);
249 sigemptyset (&oact
.sa_mask
);
250 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
251 sigaddset (&act
.sa_mask
, XSIG (i
));
252 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
254 /* If we've already trapped it, don't do anything. */
255 if (signal_is_trapped (XSIG (i
)))
257 if (signal_is_async_ignored (XSIG (i
)))
260 sigaction (XSIG (i
), &act
, &oact
);
261 XHANDLER(i
) = oact
.sa_handler
;
262 XSAFLAGS(i
) = oact
.sa_flags
;
265 set_original_signal (XSIG(i
), XHANDLER(i
)); /* optimization */
267 set_original_signal (XSIG(i
), act
.sa_handler
); /* optimization */
270 /* Don't do anything with signals that are ignored at shell entry
271 if the shell is not interactive. */
272 /* XXX - should we do this for interactive shells, too? */
273 if (interactive_shell
== 0 && XHANDLER (i
) == SIG_IGN
)
275 sigaction (XSIG (i
), &oact
, &act
);
276 set_signal_hard_ignored (XSIG (i
));
278 #if defined (SIGPROF) && !defined (_MINIX)
279 if (XSIG (i
) == SIGPROF
&& XHANDLER (i
) != SIG_DFL
&& XHANDLER (i
) != SIG_IGN
)
280 sigaction (XSIG (i
), &oact
, (struct sigaction
*)NULL
);
281 #endif /* SIGPROF && !_MINIX */
283 #else /* !HAVE_POSIX_SIGNALS */
285 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
287 /* If we've already trapped it, don't do anything. */
288 if (signal_is_trapped (XSIG (i
)))
291 XHANDLER(i
) = signal (XSIG (i
), termsig_sighandler
);
293 /* Don't do anything with signals that are ignored at shell entry
294 if the shell is not interactive. */
295 /* XXX - should we do this for interactive shells, too? */
296 if (interactive_shell
== 0 && XHANDLER (i
) == SIG_IGN
)
298 signal (XSIG (i
), SIG_IGN
);
299 set_signal_hard_ignored (XSIG (i
));
302 if (XSIG (i
) == SIGPROF
&& XHANDLER (i
) != SIG_DFL
&& XHANDLER (i
) != SIG_IGN
)
303 signal (XSIG (i
), XHANDLER (i
));
307 #endif /* !HAVE_POSIX_SIGNALS */
309 termsigs_initialized
= 1;
313 initialize_shell_signals (void)
316 initialize_terminating_signals ();
318 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
319 /* All shells use the signal mask they inherit, and pass it along
320 to child processes. Children will never block SIGCHLD, though. */
321 sigemptyset (&top_level_mask
);
322 sigprocmask (SIG_BLOCK
, (sigset_t
*)NULL
, &top_level_mask
);
323 # if defined (SIGCHLD)
324 if (sigismember (&top_level_mask
, SIGCHLD
))
326 sigdelset (&top_level_mask
, SIGCHLD
);
327 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
330 #endif /* JOB_CONTROL || HAVE_POSIX_SIGNALS */
332 /* And, some signals that are specifically ignored by the shell. */
333 set_signal_handler (SIGQUIT
, SIG_IGN
);
337 set_signal_handler (SIGINT
, sigint_sighandler
);
338 get_original_signal (SIGTERM
);
339 set_signal_handler (SIGTERM
, SIG_IGN
);
340 set_sigwinch_handler ();
345 reset_terminating_signals (void)
348 #if defined (HAVE_POSIX_SIGNALS)
349 struct sigaction act
;
352 if (termsigs_initialized
== 0)
355 #if defined (HAVE_POSIX_SIGNALS)
357 sigemptyset (&act
.sa_mask
);
358 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
360 /* Skip a signal if it's trapped or handled specially, because the
361 trap code will restore the correct value. */
362 if (signal_is_trapped (XSIG (i
)) || signal_is_special (XSIG (i
)))
365 act
.sa_handler
= XHANDLER (i
);
366 act
.sa_flags
= XSAFLAGS (i
);
367 sigaction (XSIG (i
), &act
, (struct sigaction
*) NULL
);
369 #else /* !HAVE_POSIX_SIGNALS */
370 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
372 if (signal_is_trapped (XSIG (i
)) || signal_is_special (XSIG (i
)))
375 signal (XSIG (i
), XHANDLER (i
));
377 #endif /* !HAVE_POSIX_SIGNALS */
379 termsigs_initialized
= 0;
383 /* Run some of the cleanups that should be performed when we run
384 jump_to_top_level from a builtin command context. XXX - might want to
385 also call reset_parser here. */
387 top_level_cleanup (void)
389 /* Clean up string parser environment. */
390 while (parse_and_execute_level
)
391 parse_and_execute_cleanup (-1);
393 #if defined (PROCESS_SUBSTITUTION)
395 #endif /* PROCESS_SUBSTITUTION */
397 run_unwind_protects ();
398 loop_level
= continuing
= breaking
= funcnest
= 0;
399 interrupt_execution
= retain_fifos
= executing_funsub
= 0;
400 comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
401 variable_context
= 0; /* XXX */
404 /* What to do when we've been interrupted, and it is safe to handle it. */
406 throw_to_top_level (void)
408 int print_newline
= 0;
412 if (last_command_exit_value
< 128)
413 last_command_exit_value
= 128 + SIGINT
;
414 set_pipestatus_from_exit (last_command_exit_value
);
422 last_command_exit_signal
= (last_command_exit_value
> 128) ?
423 (last_command_exit_value
- 128) : 0;
424 last_command_exit_value
|= 128;
425 set_pipestatus_from_exit (last_command_exit_value
);
427 /* Run any traps set on SIGINT, mostly for interactive shells */
428 if (signal_is_trapped (SIGINT
) && signal_is_pending (SIGINT
))
429 run_interrupt_trap (1);
431 /* Clean up string parser environment. */
432 while (parse_and_execute_level
)
433 parse_and_execute_cleanup (-1);
435 if (running_trap
> 0)
437 run_trap_cleanup (running_trap
- 1);
441 #if defined (JOB_CONTROL)
442 give_terminal_to (shell_pgrp
, 0);
443 #endif /* JOB_CONTROL */
445 /* This needs to stay because jobs.c:make_child() uses it without resetting
451 #if defined (READLINE)
454 if (RL_ISSTATE (RL_STATE_SIGHANDLER
) == 0)
455 rl_cleanup_after_signal ();
459 #endif /* READLINE */
461 #if defined (PROCESS_SUBSTITUTION)
463 #endif /* PROCESS_SUBSTITUTION */
465 run_unwind_protects ();
466 loop_level
= continuing
= breaking
= funcnest
= 0;
467 interrupt_execution
= retain_fifos
= executing_funsub
= 0;
468 comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
469 variable_context
= 0;
471 if (interactive
&& print_newline
)
474 fprintf (stderr
, "\n");
478 /* An interrupted `wait' command in a script does not exit the script. */
479 if (interactive
|| (interactive_shell
&& !shell_initialized
) ||
480 (print_newline
&& signal_is_trapped (SIGINT
)))
481 jump_to_top_level (DISCARD
);
483 jump_to_top_level (EXITPROG
);
486 /* This is just here to isolate the longjmp calls. */
488 jump_to_top_level (int value
)
490 sh_longjmp (top_level
, value
);
494 restore_sigmask (void)
496 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
497 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
501 static int handling_termsig
= 0;
504 termsig_sighandler (int sig
)
506 /* If we get called twice with the same signal before handling it,
507 terminate right away. */
545 sig
== terminating_signal
)
546 terminate_immediately
= 1;
548 /* If we are currently handling a terminating signal, we have a couple of
549 choices here. We can ignore this second terminating signal and let the
550 shell exit from the first one, or we can exit immediately by killing
551 the shell with this signal. This code implements the latter; to implement
552 the former, replace the kill_shell(sig) with return. */
553 if (handling_termsig
)
554 kill_shell (sig
); /* just short-circuit now */
556 terminating_signal
= sig
;
558 if (terminate_immediately
)
560 #if defined (HISTORY)
561 /* XXX - will inhibit history file being written */
562 # if defined (READLINE)
563 if (interactive_shell
== 0 || interactive
== 0 || (sig
!= SIGHUP
&& sig
!= SIGTERM
) || no_line_editing
|| (RL_ISSTATE (RL_STATE_READCMD
) == 0))
565 history_lines_this_session
= 0;
567 terminate_immediately
= 0;
568 termsig_handler (sig
);
571 #if defined (READLINE)
572 /* Set the event hook so readline will call it after the signal handlers
573 finish executing, so if this interrupted character input we can get
574 quick response. If readline is active or has modified the terminal we
575 need to set this no matter what the signal is, though the check for
576 RL_STATE_TERMPREPPED is possibly redundant. */
577 if (RL_ISSTATE (RL_STATE_SIGHANDLER
) || RL_ISSTATE (RL_STATE_TERMPREPPED
))
578 bashline_set_event_hook ();
579 else if (RL_ISSTATE (RL_STATE_COMPLETING
|RL_STATE_DISPATCHING
))
580 bashline_set_event_hook ();
587 termsig_handler (int sig
)
589 /* Simple semaphore to keep this function from being executed multiple
590 times. Since we no longer are running as a signal handler, we don't
591 block multiple occurrences of the terminating signals while running. */
592 if (handling_termsig
)
595 handling_termsig
= terminating_signal
; /* for termsig_sighandler */
596 terminating_signal
= 0; /* keep macro from re-testing true. */
598 if (builtin_catch_sigpipe
)
599 sigpipe_handler (sig
);
601 /* I don't believe this condition ever tests true. */
602 if (sig
== SIGINT
&& signal_is_trapped (SIGINT
))
603 run_interrupt_trap (0);
605 #if defined (HISTORY)
606 /* If we don't do something like this, the history will not be saved when
607 an interactive shell is running in a terminal window that gets closed
608 with the `close' button. We can't test for RL_STATE_READCMD because
609 readline no longer handles SIGTERM synchronously. */
610 if (interactive_shell
&& interactive
&& (sig
== SIGHUP
|| sig
== SIGTERM
) && remember_on_history
)
611 maybe_save_shell_history ();
614 if (this_shell_builtin
== read_builtin
)
617 #if defined (JOB_CONTROL)
618 if (sig
== SIGHUP
&& (interactive
|| (subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
))))
621 /* XXX - should we also suppress this call if SUBSHELL_PIPE? */
622 if ((subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
)) == 0)
624 #endif /* JOB_CONTROL */
626 #if defined (PROCESS_SUBSTITUTION)
628 # if defined (JOB_CONTROL)
631 #endif /* PROCESS_SUBSTITUTION */
633 /* Reset execution context */
634 loop_level
= continuing
= breaking
= funcnest
= 0;
635 interrupt_execution
= retain_fifos
= executing_funsub
= 0;
636 comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
638 run_exit_trap (); /* XXX - run exit trap possibly in signal context? */
649 /* We don't change the set of blocked signals. If a user starts the shell
650 with a terminating signal blocked, we won't get here (and if by some
651 magic chance we do, we'll exit below). What we do is to restore the
652 top-level signal mask, in case this is called from a terminating signal
653 handler context, in which case the signal is blocked. */
656 set_signal_handler (sig
, SIG_DFL
);
658 kill (getpid (), sig
);
660 if (dollar_dollar_pid
!= 1)
661 exit (128+sig
); /* just in case the kill fails? */
663 /* We get here only under extraordinarily rare circumstances. */
665 /* We are PID 1, and the kill above failed to kill the process. We assume
666 this means that we are running as an init process in a pid namespace
667 on Linux. In this case, we can't send ourselves a fatal signal, so we
668 determine whether or not we should have generated a core dump with the
669 kill call and attempt to trick the kernel into generating one if
671 sigprocmask (SIG_SETMASK
, (sigset_t
*)NULL
, &mask
);
672 for (i
= core
= 0; i
< TERMSIGS_LENGTH
; i
++)
674 set_signal_handler (XSIG (i
), SIG_DFL
);
675 sigdelset (&mask
, XSIG (i
));
677 core
= XCOREDUMP (i
);
679 sigprocmask (SIG_SETMASK
, &mask
, (sigset_t
*)NULL
);
682 *((volatile unsigned long *) NULL
) = 0xdead0000 + sig
; /* SIGSEGV */
688 /* What we really do when SIGINT occurs. */
690 sigint_sighandler (int sig
)
692 #if defined (MUST_REINSTALL_SIGHANDLERS)
693 signal (sig
, sigint_sighandler
);
696 /* interrupt_state needs to be set for the stack of interrupts to work
697 right. Should it be set unconditionally? */
698 if (interrupt_state
== 0)
701 /* We will get here in interactive shells with job control active; allow
702 an interactive wait to be interrupted. wait_intr_flag is only set during
703 the execution of the wait builtin and when wait_intr_buf is valid. */
706 last_command_exit_value
= 128 + sig
;
707 set_pipestatus_from_exit (last_command_exit_value
);
708 wait_signal_received
= sig
;
712 /* In interactive shells, we will get here instead of trap_handler() so
713 note that we have a trap pending. */
714 if (signal_is_trapped (sig
))
715 set_trap_state (sig
);
717 /* This is no longer used, but this code block remains as a reminder. */
718 if (interrupt_immediately
)
720 interrupt_immediately
= 0;
721 set_exit_status (128 + sig
);
722 throw_to_top_level ();
725 #if defined (READLINE)
726 /* Set the event hook so readline will call it after the signal handlers
727 finish executing, so if this interrupted character input we can get
729 else if (RL_ISSTATE (RL_STATE_SIGHANDLER
))
730 bashline_set_event_hook ();
731 else if (RL_ISSTATE (RL_STATE_COMPLETING
|RL_STATE_DISPATCHING
))
732 bashline_set_event_hook ();
738 #if defined (SIGWINCH)
740 sigwinch_sighandler (int sig
)
742 #if defined (MUST_REINSTALL_SIGHANDLERS)
743 set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
744 #endif /* MUST_REINSTALL_SIGHANDLERS */
745 sigwinch_received
= 1;
748 #endif /* SIGWINCH */
751 set_sigwinch_handler (void)
753 #if defined (SIGWINCH)
754 old_winch
= set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
759 unset_sigwinch_handler (void)
761 #if defined (SIGWINCH)
762 set_signal_handler (SIGWINCH
, old_winch
);
767 sigterm_sighandler (int sig
)
769 sigterm_received
= 1; /* XXX - counter? */
774 sigpipe_handler (int sig
)
776 handling_termsig
= 0;
777 builtin_catch_sigpipe
= 0;
778 last_command_exit_value
= 128 + sig
;
779 throw_to_top_level ();
782 /* Signal functions used by the rest of the code. */
783 #if !defined (HAVE_POSIX_SIGNALS)
785 /* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
786 sigprocmask (int operation
, int *newset
, int *oldset
)
798 old
= sigblock (new);
802 old
= sigsetmask (new);
806 internal_error (_("sigprocmask: %d: invalid operation"), operation
);
815 #if !defined (SA_INTERRUPT)
816 # define SA_INTERRUPT 0
819 #if !defined (SA_RESTART)
820 # define SA_RESTART 0
824 set_signal_handler (int sig
, SigHandler
*handler
)
826 struct sigaction act
, oact
;
828 act
.sa_handler
= handler
;
832 /* We don't want a child death to interrupt interruptible system calls, even
833 if we take the time to reap children */
834 #if defined (SIGCHLD)
836 act
.sa_flags
|= SA_RESTART
; /* XXX */
838 /* Let's see if we can keep SIGWINCH from interrupting interruptible system
839 calls, like open(2)/read(2)/write(2) */
840 #if defined (SIGWINCH)
842 act
.sa_flags
|= SA_RESTART
; /* XXX */
844 /* If we're installing a SIGTERM handler for interactive shells, we want
845 it to be as close to SIG_IGN as possible. */
846 if (sig
== SIGTERM
&& handler
== sigterm_sighandler
)
847 act
.sa_flags
|= SA_RESTART
; /* XXX */
849 sigemptyset (&act
.sa_mask
);
850 sigemptyset (&oact
.sa_mask
);
851 if (sigaction (sig
, &act
, &oact
) == 0)
852 return (oact
.sa_handler
);
856 #endif /* HAVE_POSIX_SIGNALS */