1 /* sig.c - interface for shell signal handlers and signal initialization. */
3 /* Copyright (C) 1994-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
23 #include "bushtypes.h"
25 #if defined (HAVE_UNISTD_H)
27 # include <sys/types.h>
38 #include "runner/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 "input/bushline.h"
51 # include <readline/readline.h>
55 # include "bushhist.h"
58 extern void initialize_siglist ();
60 #if !defined (JOB_CONTROL)
61 extern void initialize_job_signals
PARAMS((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
PARAMS((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 if (sigismember (&top_level_mask
, SIGCHLD
))
314 sigdelset (&top_level_mask
, SIGCHLD
);
315 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
318 #endif /* JOB_CONTROL || HAVE_POSIX_SIGNALS */
320 /* And, some signals that are specifically ignored by the shell. */
321 set_signal_handler (SIGQUIT
, SIG_IGN
);
325 set_signal_handler (SIGINT
, sigint_sighandler
);
326 get_original_signal (SIGTERM
);
327 set_signal_handler (SIGTERM
, SIG_IGN
);
328 set_sigwinch_handler ();
333 reset_terminating_signals ()
336 #if defined (HAVE_POSIX_SIGNALS)
337 struct sigaction act
;
340 if (termsigs_initialized
== 0)
343 #if defined (HAVE_POSIX_SIGNALS)
345 sigemptyset (&act
.sa_mask
);
346 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
348 /* Skip a signal if it's trapped or handled specially, because the
349 trap code will restore the correct value. */
350 if (signal_is_trapped (XSIG (i
)) || signal_is_special (XSIG (i
)))
353 act
.sa_handler
= XHANDLER (i
);
354 act
.sa_flags
= XSAFLAGS (i
);
355 sigaction (XSIG (i
), &act
, (struct sigaction
*) NULL
);
357 #else /* !HAVE_POSIX_SIGNALS */
358 for (i
= 0; i
< TERMSIGS_LENGTH
; i
++)
360 if (signal_is_trapped (XSIG (i
)) || signal_is_special (XSIG (i
)))
363 signal (XSIG (i
), XHANDLER (i
));
365 #endif /* !HAVE_POSIX_SIGNALS */
367 termsigs_initialized
= 0;
371 /* Run some of the cleanups that should be performed when we run
372 jump_to_top_level from a builtin command context. XXX - might want to
373 also call reset_parser here. */
377 /* Clean up string parser environment. */
378 while (parse_and_execute_level
)
379 parse_and_execute_cleanup (-1);
381 #if defined (PROCESS_SUBSTITUTION)
383 #endif /* PROCESS_SUBSTITUTION */
385 run_unwind_protects ();
386 loop_level
= continuing
= breaking
= funcnest
= 0;
387 executing_list
= comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
390 /* What to do when we've been interrupted, and it is safe to handle it. */
392 throw_to_top_level ()
394 int print_newline
= 0;
398 if (last_command_exit_value
< 128)
399 last_command_exit_value
= 128 + SIGINT
;
400 set_pipestatus_from_exit (last_command_exit_value
);
408 last_command_exit_signal
= (last_command_exit_value
> 128) ?
409 (last_command_exit_value
- 128) : 0;
410 last_command_exit_value
|= 128;
411 set_pipestatus_from_exit (last_command_exit_value
);
413 /* Run any traps set on SIGINT, mostly for interactive shells */
414 if (signal_is_trapped (SIGINT
) && signal_is_pending (SIGINT
))
415 run_interrupt_trap (1);
417 /* Clean up string parser environment. */
418 while (parse_and_execute_level
)
419 parse_and_execute_cleanup (-1);
421 if (running_trap
> 0)
423 run_trap_cleanup (running_trap
- 1);
427 #if defined (JOB_CONTROL)
428 give_terminal_to (shell_pgrp
, 0);
429 #endif /* JOB_CONTROL */
431 /* This needs to stay because jobs.c:make_child() uses it without resetting
437 #if defined (READLINE)
440 #endif /* READLINE */
442 #if defined (PROCESS_SUBSTITUTION)
444 #endif /* PROCESS_SUBSTITUTION */
446 run_unwind_protects ();
447 loop_level
= continuing
= breaking
= funcnest
= 0;
448 executing_list
= comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
450 if (interactive
&& print_newline
)
453 fprintf (stderr
, "\n");
457 /* An interrupted `wait' command in a script does not exit the script. */
458 if (interactive
|| (interactive_shell
&& !shell_initialized
) ||
459 (print_newline
&& signal_is_trapped (SIGINT
)))
460 jump_to_top_level (DISCARD
);
462 jump_to_top_level (EXITPROG
);
465 /* This is just here to isolate the longjmp calls. */
467 jump_to_top_level (value
)
470 sh_longjmp (top_level
, value
);
476 #if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
477 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
482 termsig_sighandler (sig
)
485 /* If we get called twice with the same signal before handling it,
486 terminate right away. */
524 sig
== terminating_signal
)
525 terminate_immediately
= 1;
527 terminating_signal
= sig
;
529 if (terminate_immediately
)
531 #if defined (HISTORY)
532 /* XXX - will inhibit history file being written */
533 # if defined (READLINE)
534 if (interactive_shell
== 0 || interactive
== 0 || (sig
!= SIGHUP
&& sig
!= SIGTERM
) || no_line_editing
|| (RL_ISSTATE (RL_STATE_READCMD
) == 0))
536 history_lines_this_session
= 0;
538 terminate_immediately
= 0;
539 termsig_handler (sig
);
542 #if defined (READLINE)
543 /* Set the event hook so readline will call it after the signal handlers
544 finish executing, so if this interrupted character input we can get
545 quick response. If readline is active or has modified the terminal we
546 need to set this no matter what the signal is, though the check for
547 RL_STATE_TERMPREPPED is possibly redundant. */
548 if (RL_ISSTATE (RL_STATE_SIGHANDLER
) || RL_ISSTATE (RL_STATE_TERMPREPPED
))
549 bushline_set_event_hook ();
556 termsig_handler (sig
)
559 static int handling_termsig
= 0;
563 /* Simple semaphore to keep this function from being executed multiple
564 times. Since we no longer are running as a signal handler, we don't
565 block multiple occurrences of the terminating signals while running. */
566 if (handling_termsig
)
568 handling_termsig
= 1;
569 terminating_signal
= 0; /* keep macro from re-testing true. */
571 /* I don't believe this condition ever tests true. */
572 if (sig
== SIGINT
&& signal_is_trapped (SIGINT
))
573 run_interrupt_trap (0);
575 #if defined (HISTORY)
576 /* If we don't do something like this, the history will not be saved when
577 an interactive shell is running in a terminal window that gets closed
578 with the `close' button. We can't test for RL_STATE_READCMD because
579 readline no longer handles SIGTERM synchronously. */
580 if (interactive_shell
&& interactive
&& (sig
== SIGHUP
|| sig
== SIGTERM
) && remember_on_history
)
581 maybe_save_shell_history ();
584 if (this_shell_builtin
== read_builtin
)
587 #if defined (JOB_CONTROL)
588 if (sig
== SIGHUP
&& (interactive
|| (subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
))))
591 if ((subshell_environment
& (SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
)) == 0)
593 #endif /* JOB_CONTROL */
595 #if defined (PROCESS_SUBSTITUTION)
597 # if defined (JOB_CONTROL)
600 #endif /* PROCESS_SUBSTITUTION */
602 /* Reset execution context */
603 loop_level
= continuing
= breaking
= funcnest
= 0;
604 executing_list
= comsub_ignore_return
= return_catch_flag
= wait_intr_flag
= 0;
606 run_exit_trap (); /* XXX - run exit trap possibly in signal context? */
608 /* We don't change the set of blocked signals. If a user starts the shell
609 with a terminating signal blocked, we won't get here (and if by some
610 magic chance we do, we'll exit below). What we do is to restore the
611 top-level signal mask, in case this is called from a terminating signal
612 handler context, in which case the signal is blocked. */
615 set_signal_handler (sig
, SIG_DFL
);
617 kill (getpid (), sig
);
619 if (dollar_dollar_pid
!= 1)
620 exit (128+sig
); /* just in case the kill fails? */
622 /* We get here only under extraordinary circumstances. */
624 /* We are PID 1, and the kill above failed to kill the process. We assume
625 this means that we are running as an init process in a pid namespace
626 on Linux. In this case, we can't send ourselves a fatal signal, so we
627 determine whether or not we should have generated a core dump with the
628 kill call and attempt to trick the kernel into generating one if
630 sigprocmask (SIG_SETMASK
, (sigset_t
*)NULL
, &mask
);
631 for (i
= core
= 0; i
< TERMSIGS_LENGTH
; i
++)
633 set_signal_handler (XSIG (i
), SIG_DFL
);
634 sigdelset (&mask
, XSIG (i
));
636 core
= XCOREDUMP (i
);
638 sigprocmask (SIG_SETMASK
, &mask
, (sigset_t
*)NULL
);
641 *((volatile unsigned long *) NULL
) = 0xdead0000 + sig
; /* SIGSEGV */
647 /* What we really do when SIGINT occurs. */
649 sigint_sighandler (sig
)
652 #if defined (MUST_REINSTALL_SIGHANDLERS)
653 signal (sig
, sigint_sighandler
);
656 /* interrupt_state needs to be set for the stack of interrupts to work
657 right. Should it be set unconditionally? */
658 if (interrupt_state
== 0)
661 /* We will get here in interactive shells with job control active; allow
662 an interactive wait to be interrupted. wait_intr_flag is only set during
663 the execution of the wait builtin and when wait_intr_buf is valid. */
666 last_command_exit_value
= 128 + sig
;
667 set_pipestatus_from_exit (last_command_exit_value
);
668 wait_signal_received
= sig
;
672 /* In interactive shells, we will get here instead of trap_handler() so
673 note that we have a trap pending. */
674 if (signal_is_trapped (sig
))
675 set_trap_state (sig
);
677 /* This is no longer used, but this code block remains as a reminder. */
678 if (interrupt_immediately
)
680 interrupt_immediately
= 0;
681 set_exit_status (128 + sig
);
682 throw_to_top_level ();
684 #if defined (READLINE)
685 /* Set the event hook so readline will call it after the signal handlers
686 finish executing, so if this interrupted character input we can get
688 else if (RL_ISSTATE (RL_STATE_SIGHANDLER
))
689 bushline_set_event_hook ();
695 #if defined (SIGWINCH)
697 sigwinch_sighandler (sig
)
700 #if defined (MUST_REINSTALL_SIGHANDLERS)
701 set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
702 #endif /* MUST_REINSTALL_SIGHANDLERS */
703 sigwinch_received
= 1;
706 #endif /* SIGWINCH */
709 set_sigwinch_handler ()
711 #if defined (SIGWINCH)
712 old_winch
= set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
717 unset_sigwinch_handler ()
719 #if defined (SIGWINCH)
720 set_signal_handler (SIGWINCH
, old_winch
);
725 sigterm_sighandler (sig
)
728 sigterm_received
= 1; /* XXX - counter? */
732 /* Signal functions used by the rest of the code. */
733 #if !defined (HAVE_POSIX_SIGNALS)
735 /* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
736 sigprocmask (operation
, newset
, oldset
)
737 int operation
, *newset
, *oldset
;
749 old
= sigblock (new);
753 old
= sigsetmask (new);
757 internal_error (_("sigprocmask: %d: invalid operation"), operation
);
766 #if !defined (SA_INTERRUPT)
767 # define SA_INTERRUPT 0
770 #if !defined (SA_RESTART)
771 # define SA_RESTART 0
775 set_signal_handler (sig
, handler
)
779 struct sigaction act
, oact
;
781 act
.sa_handler
= handler
;
785 /* We don't want a child death to interrupt interruptible system calls, even
786 if we take the time to reap children */
787 #if defined (SIGCHLD)
789 act
.sa_flags
|= SA_RESTART
; /* XXX */
791 /* Let's see if we can keep SIGWINCH from interrupting interruptible system
792 calls, like open(2)/read(2)/write(2) */
793 #if defined (SIGWINCH)
795 act
.sa_flags
|= SA_RESTART
; /* XXX */
797 /* If we're installing a SIGTERM handler for interactive shells, we want
798 it to be as close to SIG_IGN as possible. */
799 if (sig
== SIGTERM
&& handler
== sigterm_sighandler
)
800 act
.sa_flags
|= SA_RESTART
; /* XXX */
802 sigemptyset (&act
.sa_mask
);
803 sigemptyset (&oact
.sa_mask
);
804 if (sigaction (sig
, &act
, &oact
) == 0)
805 return (oact
.sa_handler
);
809 #endif /* HAVE_POSIX_SIGNALS */