blob: 8bc45c17f478e4f7b99255d126586fe65f4e33e4 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* sig.c - interface for shell signal handlers and signal initialization. */
2
Chet Rameyac50fba2014-02-26 09:36:43 -05003/* Copyright (C) 1994-2013 Free Software Foundation, Inc.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltoccc6cda1996-12-23 17:02:34 +000020
21#include "config.h"
22
23#include "bashtypes.h"
24
25#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000026# ifdef _MINIX
27# include <sys/types.h>
28# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000029# include <unistd.h>
30#endif
31
32#include <stdio.h>
33#include <signal.h>
34
Jari Aaltob80f6442004-07-27 13:29:18 +000035#include "bashintl.h"
36
Jari Aaltoccc6cda1996-12-23 17:02:34 +000037#include "shell.h"
38#if defined (JOB_CONTROL)
39#include "jobs.h"
40#endif /* JOB_CONTROL */
41#include "siglist.h"
42#include "sig.h"
43#include "trap.h"
44
45#include "builtins/common.h"
Chet Rameyac50fba2014-02-26 09:36:43 -050046#include "builtins/builtext.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000047
48#if defined (READLINE)
49# include "bashline.h"
Chet Rameyd5d00962011-11-22 20:00:47 -050050# include <readline/readline.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000051#endif
52
53#if defined (HISTORY)
54# include "bashhist.h"
55#endif
56
57extern int last_command_exit_value;
Jari Aaltob80f6442004-07-27 13:29:18 +000058extern int last_command_exit_signal;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059extern int return_catch_flag;
Chet Ramey495aee42011-11-22 19:11:26 -050060extern int loop_level, continuing, breaking, funcnest;
Jari Aalto31859422009-01-12 13:36:28 +000061extern int executing_list;
62extern int comsub_ignore_return;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000063extern int parse_and_execute_level, shell_initialized;
Chet Ramey495aee42011-11-22 19:11:26 -050064#if defined (HISTORY)
65extern int history_lines_this_session;
66#endif
Chet Rameyd5d00962011-11-22 20:00:47 -050067extern int no_line_editing;
Chet Rameyac50fba2014-02-26 09:36:43 -050068extern int wait_signal_received;
69extern sh_builtin_func_t *this_shell_builtin;
Chet Ramey495aee42011-11-22 19:11:26 -050070
71extern void initialize_siglist ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +000072
73/* Non-zero after SIGINT. */
Chet Rameyac50fba2014-02-26 09:36:43 -050074volatile sig_atomic_t interrupt_state = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000075
Jari Aalto95732b42005-12-07 14:08:12 +000076/* Non-zero after SIGWINCH */
Chet Rameyac50fba2014-02-26 09:36:43 -050077volatile sig_atomic_t sigwinch_received = 0;
78
79/* Non-zero after SIGTERM */
80volatile sig_atomic_t sigterm_received = 0;
Jari Aalto95732b42005-12-07 14:08:12 +000081
Jari Aalto06285672006-10-10 14:15:34 +000082/* Set to the value of any terminating signal received. */
Chet Rameyac50fba2014-02-26 09:36:43 -050083volatile sig_atomic_t terminating_signal = 0;
Jari Aalto06285672006-10-10 14:15:34 +000084
Jari Aaltoccc6cda1996-12-23 17:02:34 +000085/* The environment at the top-level R-E loop. We use this in
86 the case of error return. */
87procenv_t top_level;
88
89#if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
90/* The signal masks that this shell runs with. */
91sigset_t top_level_mask;
92#endif /* JOB_CONTROL */
93
94/* When non-zero, we throw_to_top_level (). */
95int interrupt_immediately = 0;
96
Jari Aalto06285672006-10-10 14:15:34 +000097/* When non-zero, we call the terminating signal handler immediately. */
98int terminate_immediately = 0;
99
Jari Aalto95732b42005-12-07 14:08:12 +0000100#if defined (SIGWINCH)
101static SigHandler *old_winch = (SigHandler *)SIG_DFL;
102#endif
103
Jari Aaltof73dda02001-11-13 17:56:06 +0000104static void initialize_shell_signals __P((void));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000105
106void
Jari Aalto7117c2d2002-07-17 14:10:11 +0000107initialize_signals (reinit)
108 int reinit;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109{
Jari Aaltod166f041997-06-05 14:59:13 +0000110 initialize_shell_signals ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000111 initialize_job_signals ();
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000112#if !defined (HAVE_SYS_SIGLIST) && !defined (HAVE_UNDER_SYS_SIGLIST) && !defined (HAVE_STRSIGNAL)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000113 if (reinit == 0)
114 initialize_siglist ();
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000115#endif /* !HAVE_SYS_SIGLIST && !HAVE_UNDER_SYS_SIGLIST && !HAVE_STRSIGNAL */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000116}
117
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000118/* A structure describing a signal that terminates the shell if not
119 caught. The orig_handler member is present so children can reset
120 these signals back to their original handlers. */
121struct termsig {
122 int signum;
123 SigHandler *orig_handler;
Jari Aalto95732b42005-12-07 14:08:12 +0000124 int orig_flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000125};
126
127#define NULL_HANDLER (SigHandler *)SIG_DFL
128
129/* The list of signals that would terminate the shell if not caught.
130 We catch them, but just so that we can write the history file,
131 and so forth. */
132static struct termsig terminating_signals[] = {
133#ifdef SIGHUP
Jari Aalto95732b42005-12-07 14:08:12 +0000134{ SIGHUP, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000135#endif
136
137#ifdef SIGINT
Jari Aalto95732b42005-12-07 14:08:12 +0000138{ SIGINT, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139#endif
140
141#ifdef SIGILL
Jari Aalto95732b42005-12-07 14:08:12 +0000142{ SIGILL, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000143#endif
144
145#ifdef SIGTRAP
Jari Aalto95732b42005-12-07 14:08:12 +0000146{ SIGTRAP, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000147#endif
148
149#ifdef SIGIOT
Jari Aalto95732b42005-12-07 14:08:12 +0000150{ SIGIOT, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000151#endif
152
153#ifdef SIGDANGER
Jari Aalto95732b42005-12-07 14:08:12 +0000154{ SIGDANGER, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000155#endif
156
157#ifdef SIGEMT
Jari Aalto95732b42005-12-07 14:08:12 +0000158{ SIGEMT, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000159#endif
160
161#ifdef SIGFPE
Jari Aalto95732b42005-12-07 14:08:12 +0000162{ SIGFPE, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000163#endif
164
165#ifdef SIGBUS
Jari Aalto95732b42005-12-07 14:08:12 +0000166{ SIGBUS, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000167#endif
168
169#ifdef SIGSEGV
Jari Aalto95732b42005-12-07 14:08:12 +0000170{ SIGSEGV, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000171#endif
172
173#ifdef SIGSYS
Jari Aalto95732b42005-12-07 14:08:12 +0000174{ SIGSYS, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000175#endif
176
177#ifdef SIGPIPE
Jari Aalto95732b42005-12-07 14:08:12 +0000178{ SIGPIPE, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000179#endif
180
181#ifdef SIGALRM
Jari Aalto95732b42005-12-07 14:08:12 +0000182{ SIGALRM, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000183#endif
184
185#ifdef SIGTERM
Jari Aalto95732b42005-12-07 14:08:12 +0000186{ SIGTERM, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000187#endif
188
189#ifdef SIGXCPU
Jari Aalto95732b42005-12-07 14:08:12 +0000190{ SIGXCPU, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000191#endif
192
193#ifdef SIGXFSZ
Jari Aalto95732b42005-12-07 14:08:12 +0000194{ SIGXFSZ, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000195#endif
196
197#ifdef SIGVTALRM
Jari Aalto95732b42005-12-07 14:08:12 +0000198{ SIGVTALRM, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000199#endif
200
Jari Aaltobb706242000-03-17 21:46:59 +0000201#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000202#ifdef SIGPROF
Jari Aalto95732b42005-12-07 14:08:12 +0000203{ SIGPROF, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000205#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000206
207#ifdef SIGLOST
Jari Aalto95732b42005-12-07 14:08:12 +0000208{ SIGLOST, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000209#endif
210
211#ifdef SIGUSR1
Jari Aalto95732b42005-12-07 14:08:12 +0000212{ SIGUSR1, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000213#endif
214
215#ifdef SIGUSR2
Jari Aalto95732b42005-12-07 14:08:12 +0000216{ SIGUSR2, NULL_HANDLER, 0 },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000217#endif
218};
219
220#define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (struct termsig))
221
222#define XSIG(x) (terminating_signals[x].signum)
223#define XHANDLER(x) (terminating_signals[x].orig_handler)
Jari Aalto95732b42005-12-07 14:08:12 +0000224#define XSAFLAGS(x) (terminating_signals[x].orig_flags)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000225
Jari Aaltod166f041997-06-05 14:59:13 +0000226static int termsigs_initialized = 0;
227
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000228/* Initialize signals that will terminate the shell to do some
Jari Aaltod166f041997-06-05 14:59:13 +0000229 unwind protection. For non-interactive shells, we only call
Chet Ramey495aee42011-11-22 19:11:26 -0500230 this when a trap is defined for EXIT (0) or when trap is run
231 to display signal dispositions. */
Jari Aaltod166f041997-06-05 14:59:13 +0000232void
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000233initialize_terminating_signals ()
234{
235 register int i;
Jari Aaltod166f041997-06-05 14:59:13 +0000236#if defined (HAVE_POSIX_SIGNALS)
237 struct sigaction act, oact;
238#endif
239
240 if (termsigs_initialized)
241 return;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000242
243 /* The following code is to avoid an expensive call to
244 set_signal_handler () for each terminating_signals. Fortunately,
245 this is possible in Posix. Unfortunately, we have to call signal ()
246 on non-Posix systems for each signal in terminating_signals. */
247#if defined (HAVE_POSIX_SIGNALS)
Jari Aalto06285672006-10-10 14:15:34 +0000248 act.sa_handler = termsig_sighandler;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000249 act.sa_flags = 0;
250 sigemptyset (&act.sa_mask);
251 sigemptyset (&oact.sa_mask);
252 for (i = 0; i < TERMSIGS_LENGTH; i++)
253 sigaddset (&act.sa_mask, XSIG (i));
254 for (i = 0; i < TERMSIGS_LENGTH; i++)
255 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000256 /* If we've already trapped it, don't do anything. */
257 if (signal_is_trapped (XSIG (i)))
258 continue;
259
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000260 sigaction (XSIG (i), &act, &oact);
Jari Aaltod166f041997-06-05 14:59:13 +0000261 XHANDLER(i) = oact.sa_handler;
Jari Aalto95732b42005-12-07 14:08:12 +0000262 XSAFLAGS(i) = oact.sa_flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000263 /* Don't do anything with signals that are ignored at shell entry
264 if the shell is not interactive. */
Chet Ramey495aee42011-11-22 19:11:26 -0500265 /* XXX - should we do this for interactive shells, too? */
266 if (interactive_shell == 0 && XHANDLER (i) == SIG_IGN)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000267 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000268 sigaction (XSIG (i), &oact, &act);
Chet Rameyac50fba2014-02-26 09:36:43 -0500269 set_signal_hard_ignored (XSIG (i));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000270 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000271#if defined (SIGPROF) && !defined (_MINIX)
Jari Aaltod166f041997-06-05 14:59:13 +0000272 if (XSIG (i) == SIGPROF && XHANDLER (i) != SIG_DFL && XHANDLER (i) != SIG_IGN)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000273 sigaction (XSIG (i), &oact, (struct sigaction *)NULL);
Jari Aaltocce855b1998-04-17 19:52:44 +0000274#endif /* SIGPROF && !_MINIX */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000275 }
276
277#else /* !HAVE_POSIX_SIGNALS */
278
279 for (i = 0; i < TERMSIGS_LENGTH; i++)
280 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000281 /* If we've already trapped it, don't do anything. */
282 if (signal_is_trapped (XSIG (i)))
283 continue;
284
Jari Aalto06285672006-10-10 14:15:34 +0000285 XHANDLER(i) = signal (XSIG (i), termsig_sighandler);
Jari Aalto95732b42005-12-07 14:08:12 +0000286 XSAFLAGS(i) = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000287 /* Don't do anything with signals that are ignored at shell entry
288 if the shell is not interactive. */
Chet Ramey495aee42011-11-22 19:11:26 -0500289 /* XXX - should we do this for interactive shells, too? */
290 if (interactive_shell == 0 && XHANDLER (i) == SIG_IGN)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000291 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000292 signal (XSIG (i), SIG_IGN);
Chet Rameyac50fba2014-02-26 09:36:43 -0500293 set_signal_hard_ignored (XSIG (i));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000294 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000295#ifdef SIGPROF
Jari Aaltod166f041997-06-05 14:59:13 +0000296 if (XSIG (i) == SIGPROF && XHANDLER (i) != SIG_DFL && XHANDLER (i) != SIG_IGN)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000297 signal (XSIG (i), XHANDLER (i));
Jari Aaltocce855b1998-04-17 19:52:44 +0000298#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000299 }
300
301#endif /* !HAVE_POSIX_SIGNALS */
302
Jari Aaltod166f041997-06-05 14:59:13 +0000303 termsigs_initialized = 1;
304}
305
306static void
307initialize_shell_signals ()
308{
309 if (interactive)
310 initialize_terminating_signals ();
311
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000312#if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
313 /* All shells use the signal mask they inherit, and pass it along
314 to child processes. Children will never block SIGCHLD, though. */
315 sigemptyset (&top_level_mask);
316 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &top_level_mask);
Jari Aaltobb706242000-03-17 21:46:59 +0000317# if defined (SIGCHLD)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000318 sigdelset (&top_level_mask, SIGCHLD);
Jari Aaltobb706242000-03-17 21:46:59 +0000319# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000320#endif /* JOB_CONTROL || HAVE_POSIX_SIGNALS */
321
322 /* And, some signals that are specifically ignored by the shell. */
323 set_signal_handler (SIGQUIT, SIG_IGN);
324
325 if (interactive)
326 {
327 set_signal_handler (SIGINT, sigint_sighandler);
Chet Rameyac50fba2014-02-26 09:36:43 -0500328 get_original_signal (SIGTERM);
329 if (signal_is_hard_ignored (SIGTERM) == 0)
330 set_signal_handler (SIGTERM, sigterm_sighandler);
Jari Aalto95732b42005-12-07 14:08:12 +0000331 set_sigwinch_handler ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000332 }
333}
334
335void
336reset_terminating_signals ()
337{
338 register int i;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000339#if defined (HAVE_POSIX_SIGNALS)
340 struct sigaction act;
Jari Aaltod166f041997-06-05 14:59:13 +0000341#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000342
Jari Aaltod166f041997-06-05 14:59:13 +0000343 if (termsigs_initialized == 0)
344 return;
345
346#if defined (HAVE_POSIX_SIGNALS)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000347 act.sa_flags = 0;
348 sigemptyset (&act.sa_mask);
349 for (i = 0; i < TERMSIGS_LENGTH; i++)
350 {
351 /* Skip a signal if it's trapped or handled specially, because the
352 trap code will restore the correct value. */
353 if (signal_is_trapped (XSIG (i)) || signal_is_special (XSIG (i)))
354 continue;
355
356 act.sa_handler = XHANDLER (i);
Jari Aalto95732b42005-12-07 14:08:12 +0000357 act.sa_flags = XSAFLAGS (i);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000358 sigaction (XSIG (i), &act, (struct sigaction *) NULL);
359 }
360#else /* !HAVE_POSIX_SIGNALS */
361 for (i = 0; i < TERMSIGS_LENGTH; i++)
362 {
363 if (signal_is_trapped (XSIG (i)) || signal_is_special (XSIG (i)))
364 continue;
365
366 signal (XSIG (i), XHANDLER (i));
367 }
368#endif /* !HAVE_POSIX_SIGNALS */
Chet Rameyac50fba2014-02-26 09:36:43 -0500369
370 termsigs_initialized = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000371}
372#undef XSIG
373#undef XHANDLER
374
Jari Aaltof1be6662008-11-18 13:15:12 +0000375/* Run some of the cleanups that should be performed when we run
376 jump_to_top_level from a builtin command context. XXX - might want to
377 also call reset_parser here. */
378void
379top_level_cleanup ()
380{
381 /* Clean up string parser environment. */
382 while (parse_and_execute_level)
383 parse_and_execute_cleanup ();
384
385#if defined (PROCESS_SUBSTITUTION)
386 unlink_fifo_list ();
387#endif /* PROCESS_SUBSTITUTION */
388
389 run_unwind_protects ();
Chet Ramey495aee42011-11-22 19:11:26 -0500390 loop_level = continuing = breaking = funcnest = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000391 executing_list = comsub_ignore_return = return_catch_flag = 0;
Jari Aaltof1be6662008-11-18 13:15:12 +0000392}
393
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000394/* What to do when we've been interrupted, and it is safe to handle it. */
395void
396throw_to_top_level ()
397{
398 int print_newline = 0;
399
400 if (interrupt_state)
401 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500402 if (last_command_exit_value < 128)
403 last_command_exit_value = 128 + SIGINT;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000404 print_newline = 1;
405 DELINTERRUPT;
406 }
407
408 if (interrupt_state)
409 return;
410
Jari Aaltob80f6442004-07-27 13:29:18 +0000411 last_command_exit_signal = (last_command_exit_value > 128) ?
412 (last_command_exit_value - 128) : 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000413 last_command_exit_value |= 128;
414
415 /* Run any traps set on SIGINT. */
416 run_interrupt_trap ();
417
Jari Aalto31859422009-01-12 13:36:28 +0000418 /* Clean up string parser environment. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000419 while (parse_and_execute_level)
420 parse_and_execute_cleanup ();
421
422#if defined (JOB_CONTROL)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000423 give_terminal_to (shell_pgrp, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000424#endif /* JOB_CONTROL */
425
426#if defined (JOB_CONTROL) || defined (HAVE_POSIX_SIGNALS)
Chet Rameyac50fba2014-02-26 09:36:43 -0500427 /* This needs to stay because jobs.c:make_child() uses it without resetting
428 the signal mask. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000429 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
430#endif
431
432 reset_parser ();
433
434#if defined (READLINE)
435 if (interactive)
Jari Aalto31859422009-01-12 13:36:28 +0000436 bashline_reset ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000437#endif /* READLINE */
438
439#if defined (PROCESS_SUBSTITUTION)
440 unlink_fifo_list ();
441#endif /* PROCESS_SUBSTITUTION */
442
443 run_unwind_protects ();
Chet Ramey495aee42011-11-22 19:11:26 -0500444 loop_level = continuing = breaking = funcnest = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000445 executing_list = comsub_ignore_return = return_catch_flag = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000446
447 if (interactive && print_newline)
448 {
449 fflush (stdout);
450 fprintf (stderr, "\n");
451 fflush (stderr);
452 }
453
454 /* An interrupted `wait' command in a script does not exit the script. */
455 if (interactive || (interactive_shell && !shell_initialized) ||
456 (print_newline && signal_is_trapped (SIGINT)))
457 jump_to_top_level (DISCARD);
458 else
459 jump_to_top_level (EXITPROG);
460}
461
462/* This is just here to isolate the longjmp calls. */
463void
464jump_to_top_level (value)
465 int value;
466{
467 longjmp (top_level, value);
468}
469
470sighandler
Jari Aalto06285672006-10-10 14:15:34 +0000471termsig_sighandler (sig)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000472 int sig;
473{
Chet Ramey89a92862011-11-21 20:49:12 -0500474 /* If we get called twice with the same signal before handling it,
475 terminate right away. */
476 if (
477#ifdef SIGHUP
478 sig != SIGHUP &&
479#endif
480#ifdef SIGINT
481 sig != SIGINT &&
482#endif
483#ifdef SIGDANGER
484 sig != SIGDANGER &&
485#endif
486#ifdef SIGPIPE
487 sig != SIGPIPE &&
488#endif
489#ifdef SIGALRM
490 sig != SIGALRM &&
491#endif
492#ifdef SIGTERM
493 sig != SIGTERM &&
494#endif
495#ifdef SIGXCPU
496 sig != SIGXCPU &&
497#endif
498#ifdef SIGXFSZ
499 sig != SIGXFSZ &&
500#endif
501#ifdef SIGVTALRM
502 sig != SIGVTALRM &&
503#endif
504#ifdef SIGLOST
505 sig != SIGLOST &&
506#endif
507#ifdef SIGUSR1
508 sig != SIGUSR1 &&
509#endif
510#ifdef SIGUSR2
511 sig != SIGUSR2 &&
512#endif
513 sig == terminating_signal)
514 terminate_immediately = 1;
515
Jari Aalto06285672006-10-10 14:15:34 +0000516 terminating_signal = sig;
517
Jari Aalto31859422009-01-12 13:36:28 +0000518 /* XXX - should this also trigger when interrupt_immediately is set? */
Jari Aalto06285672006-10-10 14:15:34 +0000519 if (terminate_immediately)
520 {
Chet Ramey495aee42011-11-22 19:11:26 -0500521#if defined (HISTORY)
522 /* XXX - will inhibit history file being written */
Chet Rameyd5d00962011-11-22 20:00:47 -0500523# if defined (READLINE)
524 if (interactive_shell == 0 || interactive == 0 || (sig != SIGHUP && sig != SIGTERM) || no_line_editing || (RL_ISSTATE (RL_STATE_READCMD) == 0))
525# endif
526 history_lines_this_session = 0;
Chet Ramey495aee42011-11-22 19:11:26 -0500527#endif
Jari Aalto06285672006-10-10 14:15:34 +0000528 terminate_immediately = 0;
529 termsig_handler (sig);
530 }
531
Chet Rameyac50fba2014-02-26 09:36:43 -0500532#if defined (READLINE)
533 /* Set the event hook so readline will call it after the signal handlers
534 finish executing, so if this interrupted character input we can get
Chet Rameybbc8b482015-01-15 10:21:08 -0500535 quick response. If readline is active or has modified the terminal we
536 need to set this no matter what the signal is, though the check for
537 RL_STATE_TERMPREPPED is possibly redundant. */
538 if (RL_ISSTATE (RL_STATE_SIGHANDLER) || RL_ISSTATE (RL_STATE_TERMPREPPED))
Chet Rameyac50fba2014-02-26 09:36:43 -0500539 bashline_set_event_hook ();
540#endif
541
Jari Aalto06285672006-10-10 14:15:34 +0000542 SIGRETURN (0);
543}
544
545void
546termsig_handler (sig)
547 int sig;
548{
549 static int handling_termsig = 0;
550
551 /* Simple semaphore to keep this function from being executed multiple
552 times. Since we no longer are running as a signal handler, we don't
553 block multiple occurrences of the terminating signals while running. */
554 if (handling_termsig)
555 return;
556 handling_termsig = 1;
557 terminating_signal = 0; /* keep macro from re-testing true. */
558
Jari Aalto95732b42005-12-07 14:08:12 +0000559 /* I don't believe this condition ever tests true. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000560 if (sig == SIGINT && signal_is_trapped (SIGINT))
561 run_interrupt_trap ();
562
563#if defined (HISTORY)
Chet Rameyac50fba2014-02-26 09:36:43 -0500564 /* If we don't do something like this, the history will not be saved when
565 an interactive shell is running in a terminal window that gets closed
566 with the `close' button. We can't test for RL_STATE_READCMD because
567 readline no longer handles SIGTERM synchronously. */
568 if (interactive_shell && interactive && (sig == SIGHUP || sig == SIGTERM) && remember_on_history)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000569 maybe_save_shell_history ();
570#endif /* HISTORY */
571
572#if defined (JOB_CONTROL)
Chet Ramey00018032011-11-21 20:51:19 -0500573 if (sig == SIGHUP && (interactive || (subshell_environment & (SUBSHELL_COMSUB|SUBSHELL_PROCSUB))))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000574 hangup_all_jobs ();
575 end_job_control ();
576#endif /* JOB_CONTROL */
577
578#if defined (PROCESS_SUBSTITUTION)
579 unlink_fifo_list ();
580#endif /* PROCESS_SUBSTITUTION */
581
Jari Aalto31859422009-01-12 13:36:28 +0000582 /* Reset execution context */
Chet Ramey495aee42011-11-22 19:11:26 -0500583 loop_level = continuing = breaking = funcnest = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000584 executing_list = comsub_ignore_return = return_catch_flag = 0;
585
Chet Rameyac50fba2014-02-26 09:36:43 -0500586 run_exit_trap (); /* XXX - run exit trap possibly in signal context? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000587 set_signal_handler (sig, SIG_DFL);
588 kill (getpid (), sig);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000589}
590
591/* What we really do when SIGINT occurs. */
592sighandler
593sigint_sighandler (sig)
594 int sig;
595{
596#if defined (MUST_REINSTALL_SIGHANDLERS)
597 signal (sig, sigint_sighandler);
598#endif
599
600 /* interrupt_state needs to be set for the stack of interrupts to work
601 right. Should it be set unconditionally? */
602 if (interrupt_state == 0)
603 ADDINTERRUPT;
604
Chet Rameyac50fba2014-02-26 09:36:43 -0500605 /* We will get here in interactive shells with job control active; allow
606 an interactive wait to be interrupted. */
607 if (this_shell_builtin && this_shell_builtin == wait_builtin)
608 {
609 last_command_exit_value = 128 + sig;
610 wait_signal_received = sig;
611 SIGRETURN (0);
612 }
613
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000614 if (interrupt_immediately)
615 {
616 interrupt_immediately = 0;
Chet Ramey00018032011-11-21 20:51:19 -0500617 last_command_exit_value = 128 + sig;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000618 throw_to_top_level ();
619 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500620#if defined (READLINE)
621 /* Set the event hook so readline will call it after the signal handlers
622 finish executing, so if this interrupted character input we can get
623 quick response. */
624 else if (RL_ISSTATE (RL_STATE_SIGHANDLER))
625 bashline_set_event_hook ();
626#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000627
628 SIGRETURN (0);
629}
630
Jari Aalto95732b42005-12-07 14:08:12 +0000631#if defined (SIGWINCH)
632sighandler
633sigwinch_sighandler (sig)
634 int sig;
635{
636#if defined (MUST_REINSTALL_SIGHANDLERS)
637 set_signal_handler (SIGWINCH, sigwinch_sighandler);
638#endif /* MUST_REINSTALL_SIGHANDLERS */
639 sigwinch_received = 1;
640 SIGRETURN (0);
641}
642#endif /* SIGWINCH */
643
644void
645set_sigwinch_handler ()
646{
647#if defined (SIGWINCH)
648 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
649#endif
650}
651
652void
653unset_sigwinch_handler ()
654{
655#if defined (SIGWINCH)
656 set_signal_handler (SIGWINCH, old_winch);
657#endif
658}
659
Chet Rameyac50fba2014-02-26 09:36:43 -0500660sighandler
661sigterm_sighandler (sig)
662 int sig;
663{
664 sigterm_received = 1; /* XXX - counter? */
665 SIGRETURN (0);
666}
667
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000668/* Signal functions used by the rest of the code. */
669#if !defined (HAVE_POSIX_SIGNALS)
670
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000671/* Perform OPERATION on NEWSET, perhaps leaving information in OLDSET. */
672sigprocmask (operation, newset, oldset)
673 int operation, *newset, *oldset;
674{
675 int old, new;
676
677 if (newset)
678 new = *newset;
679 else
680 new = 0;
681
682 switch (operation)
683 {
684 case SIG_BLOCK:
685 old = sigblock (new);
686 break;
687
688 case SIG_SETMASK:
Chet Rameyac50fba2014-02-26 09:36:43 -0500689 old = sigsetmask (new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000690 break;
691
692 default:
Jari Aaltob80f6442004-07-27 13:29:18 +0000693 internal_error (_("sigprocmask: %d: invalid operation"), operation);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000694 }
695
696 if (oldset)
697 *oldset = old;
698}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000699
700#else
701
702#if !defined (SA_INTERRUPT)
703# define SA_INTERRUPT 0
704#endif
705
706#if !defined (SA_RESTART)
707# define SA_RESTART 0
708#endif
709
710SigHandler *
711set_signal_handler (sig, handler)
712 int sig;
713 SigHandler *handler;
714{
715 struct sigaction act, oact;
716
717 act.sa_handler = handler;
718 act.sa_flags = 0;
Chet Ramey495aee42011-11-22 19:11:26 -0500719
720 /* XXX - bash-4.2 */
721 /* We don't want a child death to interrupt interruptible system calls, even
722 if we take the time to reap children */
Chet Rameyac50fba2014-02-26 09:36:43 -0500723#if defined (SIGCHLD)
Chet Ramey30d188c2011-11-21 20:57:16 -0500724 if (sig == SIGCHLD)
Chet Ramey495aee42011-11-22 19:11:26 -0500725 act.sa_flags |= SA_RESTART; /* XXX */
Chet Rameyac50fba2014-02-26 09:36:43 -0500726#endif
727 /* If we're installing a SIGTERM handler for interactive shells, we want
728 it to be as close to SIG_IGN as possible. */
729 if (sig == SIGTERM && handler == sigterm_sighandler)
730 act.sa_flags |= SA_RESTART; /* XXX */
Chet Ramey495aee42011-11-22 19:11:26 -0500731
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000732 sigemptyset (&act.sa_mask);
733 sigemptyset (&oact.sa_mask);
Chet Rameyac50fba2014-02-26 09:36:43 -0500734 if (sigaction (sig, &act, &oact) == 0)
735 return (oact.sa_handler);
736 else
737 return (SIG_DFL);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000738}
739#endif /* HAVE_POSIX_SIGNALS */