1 /* signals.c -- signal handling support for readline. */
3 /* Copyright (C) 1987-2017 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <stdio.h> /* Just for NULL. Yuck. */
29 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 /* System-specific feature definitions and include files. */
39 #if defined (GWINSZ_IN_SYS_IOCTL)
40 # include <sys/ioctl.h>
41 #endif /* GWINSZ_IN_SYS_IOCTL */
43 /* Some standard library routines. */
47 #include "rlprivate.h"
49 #if defined (HANDLE_SIGNALS)
51 #if !defined (RETSIGTYPE)
52 # if defined (VOID_SIGHANDLER)
53 # define RETSIGTYPE void
55 # define RETSIGTYPE int
56 # endif /* !VOID_SIGHANDLER */
57 #endif /* !RETSIGTYPE */
59 #if defined (VOID_SIGHANDLER)
60 # define SIGHANDLER_RETURN return
62 # define SIGHANDLER_RETURN return (0)
65 /* This typedef is equivalent to the one for Function; it allows us
66 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
67 typedef RETSIGTYPE
SigHandler ();
69 #if defined (HAVE_POSIX_SIGNALS)
70 typedef struct sigaction sighandler_cxt
;
71 # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh)
73 typedef struct { SigHandler
*sa_handler
; int sa_mask
, sa_flags
; } sighandler_cxt
;
74 # define sigemptyset(m)
75 #endif /* !HAVE_POSIX_SIGNALS */
81 static SigHandler
*rl_set_sighandler
PARAMS((int, SigHandler
*, sighandler_cxt
*));
82 static void rl_maybe_set_sighandler
PARAMS((int, SigHandler
*, sighandler_cxt
*));
83 static void rl_maybe_restore_sighandler
PARAMS((int, sighandler_cxt
*));
85 static RETSIGTYPE rl_signal_handler
PARAMS((int));
86 static RETSIGTYPE _rl_handle_signal
PARAMS((int));
88 /* Exported variables for use by applications. */
90 /* If non-zero, readline will install its own signal handlers for
91 SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */
92 int rl_catch_signals
= 1;
94 /* If non-zero, readline will install a signal handler for SIGWINCH. */
96 int rl_catch_sigwinch
= 1;
98 int rl_catch_sigwinch
= 0; /* for the readline state struct in readline.c */
101 /* Private variables. */
102 int _rl_interrupt_immediately
= 0;
103 int volatile _rl_caught_signal
= 0; /* should be sig_atomic_t, but that requires including <signal.h> everywhere */
105 /* If non-zero, print characters corresponding to received signals as long as
106 the user has indicated his desire to do so (_rl_echo_control_chars). */
109 int _rl_intr_char
= 0;
110 int _rl_quit_char
= 0;
111 int _rl_susp_char
= 0;
113 static int signals_set_flag
;
114 static int sigwinch_set_flag
;
116 #if defined (HAVE_POSIX_SIGNALS)
117 sigset_t _rl_orig_sigset
;
118 #endif /* !HAVE_POSIX_SIGNALS */
120 /* **************************************************************** */
122 /* Signal Handling */
124 /* **************************************************************** */
126 static sighandler_cxt old_int
, old_term
, old_hup
, old_alrm
, old_quit
;
127 #if defined (SIGTSTP)
128 static sighandler_cxt old_tstp
, old_ttou
, old_ttin
;
130 #if defined (SIGWINCH)
131 static sighandler_cxt old_winch
;
134 _rl_sigcleanup_func_t
*_rl_sigcleanup
;
135 void *_rl_sigcleanarg
;
137 /* Readline signal handler functions. */
139 /* Called from RL_CHECK_SIGNALS() macro */
141 _rl_signal_handler (int sig
)
143 _rl_caught_signal
= 0; /* XXX */
145 #if defined (SIGWINCH)
148 rl_resize_terminal ();
149 /* XXX - experimental for now */
150 /* Call a signal hook because though we called the original signal handler
151 in rl_sigwinch_handler below, we will not resend the signal to
153 if (rl_signal_event_hook
)
154 (*rl_signal_event_hook
) ();
158 _rl_handle_signal (sig
);
164 rl_signal_handler (int sig
)
166 if (_rl_interrupt_immediately
)
168 _rl_interrupt_immediately
= 0;
169 _rl_handle_signal (sig
);
172 _rl_caught_signal
= sig
;
178 _rl_handle_signal (int sig
)
180 #if defined (HAVE_POSIX_SIGNALS)
182 #else /* !HAVE_POSIX_SIGNALS */
183 # if defined (HAVE_BSD_SIGNALS)
185 # else /* !HAVE_BSD_SIGNALS */
186 sighandler_cxt dummy_cxt
; /* needed for rl_set_sighandler call */
187 # endif /* !HAVE_BSD_SIGNALS */
188 #endif /* !HAVE_POSIX_SIGNALS */
190 RL_SETSTATE(RL_STATE_SIGHANDLER
);
192 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
193 /* Since the signal will not be blocked while we are in the signal
194 handler, ignore it until rl_clear_signals resets the catcher. */
195 # if defined (SIGALRM)
196 if (sig
== SIGINT
|| sig
== SIGALRM
)
200 rl_set_sighandler (sig
, SIG_IGN
, &dummy_cxt
);
201 #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */
203 /* If there's a sig cleanup function registered, call it and `deregister'
204 the cleanup function to avoid multiple calls */
207 (*_rl_sigcleanup
) (sig
, _rl_sigcleanarg
);
215 _rl_reset_completion_state ();
216 rl_free_line_state ();
217 #if defined (READLINE_CALLBACKS)
218 rl_callback_sigcleanup ();
223 #if defined (SIGTSTP)
226 # if defined (HAVE_POSIX_SIGNALS)
227 /* Block SIGTTOU so we can restore the terminal settings to something
228 sane without stopping on SIGTTOU if we have been placed into the
229 background. Even trying to get the current terminal pgrp with
230 tcgetpgrp() will generate SIGTTOU, so we don't bother. Don't bother
231 doing this if we've been stopped on SIGTTOU; it's aready too late. */
233 sigaddset (&set
, SIGTTOU
);
234 sigprocmask (SIG_BLOCK
, &set
, (sigset_t
*)NULL
);
242 #if defined (SIGALRM)
245 #if defined (SIGQUIT)
248 rl_echo_signal_char (sig
);
249 rl_cleanup_after_signal ();
251 #if defined (HAVE_POSIX_SIGNALS)
252 # if defined (SIGTSTP)
253 /* Unblock SIGTTOU blocked above */
254 if (sig
== SIGTTIN
|| sig
== SIGTSTP
)
255 sigprocmask (SIG_UNBLOCK
, &set
, (sigset_t
*)NULL
);
259 sigprocmask (SIG_BLOCK
, (sigset_t
*)NULL
, &set
);
260 sigdelset (&set
, sig
);
261 #else /* !HAVE_POSIX_SIGNALS */
262 # if defined (HAVE_BSD_SIGNALS)
263 omask
= sigblock (0);
264 # endif /* HAVE_BSD_SIGNALS */
265 #endif /* !HAVE_POSIX_SIGNALS */
267 #if defined (__EMX__)
268 signal (sig
, SIG_ACK
);
271 #if defined (HAVE_KILL)
272 kill (getpid (), sig
);
274 raise (sig
); /* assume we have raise */
277 /* Let the signal that we just sent through. */
278 #if defined (HAVE_POSIX_SIGNALS)
279 sigprocmask (SIG_SETMASK
, &set
, (sigset_t
*)NULL
);
280 #else /* !HAVE_POSIX_SIGNALS */
281 # if defined (HAVE_BSD_SIGNALS)
282 sigsetmask (omask
& ~(sigmask (sig
)));
283 # endif /* HAVE_BSD_SIGNALS */
284 #endif /* !HAVE_POSIX_SIGNALS */
286 rl_reset_after_signal ();
289 RL_UNSETSTATE(RL_STATE_SIGHANDLER
);
293 #if defined (SIGWINCH)
295 rl_sigwinch_handler (int sig
)
299 #if defined (MUST_REINSTALL_SIGHANDLERS)
300 sighandler_cxt dummy_winch
;
302 /* We don't want to change old_winch -- it holds the state of SIGWINCH
303 disposition set by the calling application. We need this state
304 because we call the application's SIGWINCH handler after updating
305 our own idea of the screen size. */
306 rl_set_sighandler (SIGWINCH
, rl_sigwinch_handler
, &dummy_winch
);
309 RL_SETSTATE(RL_STATE_SIGHANDLER
);
310 _rl_caught_signal
= sig
;
312 /* If another sigwinch handler has been installed, call it. */
313 oh
= (SigHandler
*)old_winch
.sa_handler
;
314 if (oh
&& oh
!= (SigHandler
*)SIG_IGN
&& oh
!= (SigHandler
*)SIG_DFL
)
317 RL_UNSETSTATE(RL_STATE_SIGHANDLER
);
320 #endif /* SIGWINCH */
322 /* Functions to manage signal handling. */
324 #if !defined (HAVE_POSIX_SIGNALS)
326 rl_sigaction (int sig
, sighandler_cxt
*nh
, sighandler_cxt
*oh
)
328 oh
->sa_handler
= signal (sig
, nh
->sa_handler
);
331 #endif /* !HAVE_POSIX_SIGNALS */
333 /* Set up a readline-specific signal handler, saving the old signal
334 information in OHANDLER. Return the old signal handler, like
337 rl_set_sighandler (int sig
, SigHandler
*handler
, sighandler_cxt
*ohandler
)
339 sighandler_cxt old_handler
;
340 #if defined (HAVE_POSIX_SIGNALS)
341 struct sigaction act
;
343 act
.sa_handler
= handler
;
344 # if defined (SIGWINCH)
345 act
.sa_flags
= (sig
== SIGWINCH
) ? SA_RESTART
: 0;
348 # endif /* SIGWINCH */
349 sigemptyset (&act
.sa_mask
);
350 sigemptyset (&ohandler
->sa_mask
);
351 sigaction (sig
, &act
, &old_handler
);
353 old_handler
.sa_handler
= (SigHandler
*)signal (sig
, handler
);
354 #endif /* !HAVE_POSIX_SIGNALS */
356 /* XXX -- assume we have memcpy */
357 /* If rl_set_signals is called twice in a row, don't set the old handler to
358 rl_signal_handler, because that would cause infinite recursion. */
359 if (handler
!= rl_signal_handler
|| old_handler
.sa_handler
!= rl_signal_handler
)
360 memcpy (ohandler
, &old_handler
, sizeof (sighandler_cxt
));
362 return (ohandler
->sa_handler
);
365 /* Set disposition of SIG to HANDLER, returning old state in OHANDLER. Don't
366 change disposition if OHANDLER indicates the signal was ignored. */
368 rl_maybe_set_sighandler (int sig
, SigHandler
*handler
, sighandler_cxt
*ohandler
)
370 sighandler_cxt dummy
;
373 sigemptyset (&dummy
.sa_mask
);
375 oh
= rl_set_sighandler (sig
, handler
, ohandler
);
376 if (oh
== (SigHandler
*)SIG_IGN
)
377 rl_sigaction (sig
, ohandler
, &dummy
);
380 /* Set the disposition of SIG to HANDLER, if HANDLER->sa_handler indicates the
381 signal was not being ignored. MUST only be called for signals whose
382 disposition was changed using rl_maybe_set_sighandler or for which the
383 SIG_IGN check was performed inline (e.g., SIGALRM below). */
385 rl_maybe_restore_sighandler (int sig
, sighandler_cxt
*handler
)
387 sighandler_cxt dummy
;
389 sigemptyset (&dummy
.sa_mask
);
391 if (handler
->sa_handler
!= SIG_IGN
)
392 rl_sigaction (sig
, handler
, &dummy
);
396 rl_set_signals (void)
398 sighandler_cxt dummy
;
400 #if defined (HAVE_POSIX_SIGNALS)
401 static int sigmask_set
= 0;
402 static sigset_t bset
, oset
;
405 #if defined (HAVE_POSIX_SIGNALS)
406 if (rl_catch_signals
&& sigmask_set
== 0)
410 sigaddset (&bset
, SIGINT
);
411 sigaddset (&bset
, SIGTERM
);
413 sigaddset (&bset
, SIGHUP
);
415 #if defined (SIGQUIT)
416 sigaddset (&bset
, SIGQUIT
);
418 #if defined (SIGALRM)
419 sigaddset (&bset
, SIGALRM
);
421 #if defined (SIGTSTP)
422 sigaddset (&bset
, SIGTSTP
);
424 #if defined (SIGTTIN)
425 sigaddset (&bset
, SIGTTIN
);
427 #if defined (SIGTTOU)
428 sigaddset (&bset
, SIGTTOU
);
432 #endif /* HAVE_POSIX_SIGNALS */
434 if (rl_catch_signals
&& signals_set_flag
== 0)
436 #if defined (HAVE_POSIX_SIGNALS)
437 sigemptyset (&_rl_orig_sigset
);
438 sigprocmask (SIG_BLOCK
, &bset
, &_rl_orig_sigset
);
441 rl_maybe_set_sighandler (SIGINT
, rl_signal_handler
, &old_int
);
442 rl_maybe_set_sighandler (SIGTERM
, rl_signal_handler
, &old_term
);
444 rl_maybe_set_sighandler (SIGHUP
, rl_signal_handler
, &old_hup
);
446 #if defined (SIGQUIT)
447 rl_maybe_set_sighandler (SIGQUIT
, rl_signal_handler
, &old_quit
);
450 #if defined (SIGALRM)
451 oh
= rl_set_sighandler (SIGALRM
, rl_signal_handler
, &old_alrm
);
452 if (oh
== (SigHandler
*)SIG_IGN
)
453 rl_sigaction (SIGALRM
, &old_alrm
, &dummy
);
454 #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART)
455 /* If the application using readline has already installed a signal
456 handler with SA_RESTART, SIGALRM will cause reads to be restarted
457 automatically, so readline should just get out of the way. Since
458 we tested for SIG_IGN above, we can just test for SIG_DFL here. */
459 if (oh
!= (SigHandler
*)SIG_DFL
&& (old_alrm
.sa_flags
& SA_RESTART
))
460 rl_sigaction (SIGALRM
, &old_alrm
, &dummy
);
461 #endif /* HAVE_POSIX_SIGNALS */
464 #if defined (SIGTSTP)
465 rl_maybe_set_sighandler (SIGTSTP
, rl_signal_handler
, &old_tstp
);
468 #if defined (SIGTTOU)
469 rl_maybe_set_sighandler (SIGTTOU
, rl_signal_handler
, &old_ttou
);
472 #if defined (SIGTTIN)
473 rl_maybe_set_sighandler (SIGTTIN
, rl_signal_handler
, &old_ttin
);
476 signals_set_flag
= 1;
478 #if defined (HAVE_POSIX_SIGNALS)
479 sigprocmask (SIG_SETMASK
, &_rl_orig_sigset
, (sigset_t
*)NULL
);
482 else if (rl_catch_signals
== 0)
484 #if defined (HAVE_POSIX_SIGNALS)
485 sigemptyset (&_rl_orig_sigset
);
486 sigprocmask (SIG_BLOCK
, (sigset_t
*)NULL
, &_rl_orig_sigset
);
490 #if defined (SIGWINCH)
491 if (rl_catch_sigwinch
&& sigwinch_set_flag
== 0)
493 rl_maybe_set_sighandler (SIGWINCH
, rl_sigwinch_handler
, &old_winch
);
494 sigwinch_set_flag
= 1;
496 #endif /* SIGWINCH */
502 rl_clear_signals (void)
504 sighandler_cxt dummy
;
506 if (rl_catch_signals
&& signals_set_flag
== 1)
508 /* Since rl_maybe_set_sighandler doesn't override a SIG_IGN handler,
509 we should in theory not have to restore a handler where
510 old_xxx.sa_handler == SIG_IGN. That's what rl_maybe_restore_sighandler
511 does. Fewer system calls should reduce readline's per-line
513 rl_maybe_restore_sighandler (SIGINT
, &old_int
);
514 rl_maybe_restore_sighandler (SIGTERM
, &old_term
);
516 rl_maybe_restore_sighandler (SIGHUP
, &old_hup
);
518 #if defined (SIGQUIT)
519 rl_maybe_restore_sighandler (SIGQUIT
, &old_quit
);
521 #if defined (SIGALRM)
522 rl_maybe_restore_sighandler (SIGALRM
, &old_alrm
);
525 #if defined (SIGTSTP)
526 rl_maybe_restore_sighandler (SIGTSTP
, &old_tstp
);
529 #if defined (SIGTTOU)
530 rl_maybe_restore_sighandler (SIGTTOU
, &old_ttou
);
533 #if defined (SIGTTIN)
534 rl_maybe_restore_sighandler (SIGTTIN
, &old_ttin
);
537 signals_set_flag
= 0;
540 #if defined (SIGWINCH)
541 if (rl_catch_sigwinch
&& sigwinch_set_flag
== 1)
543 sigemptyset (&dummy
.sa_mask
);
544 rl_sigaction (SIGWINCH
, &old_winch
, &dummy
);
545 sigwinch_set_flag
= 0;
552 /* Clean up the terminal and readline state after catching a signal, before
553 resending it to the calling application. */
555 rl_cleanup_after_signal (void)
557 _rl_clean_up_for_exit ();
558 if (rl_deprep_term_function
)
559 (*rl_deprep_term_function
) ();
560 rl_clear_pending_input ();
564 /* Reset the terminal and readline state after a signal handler returns. */
566 rl_reset_after_signal (void)
568 if (rl_prep_term_function
)
569 (*rl_prep_term_function
) (_rl_meta_flag
);
573 /* Free up the readline variable line state for the current line (undo list,
574 any partial history entry, any keyboard macros in progress, and any
575 numeric arguments in process) after catching a signal, before calling
576 rl_cleanup_after_signal(). */
578 rl_free_line_state (void)
580 register HIST_ENTRY
*entry
;
582 rl_free_undo_list ();
584 entry
= current_history ();
586 entry
->data
= (char *)NULL
;
588 _rl_kill_kbd_macro ();
590 _rl_reset_argument ();
594 rl_pending_signal (void)
596 return (_rl_caught_signal
);
600 rl_check_signals (void)
604 #endif /* HANDLE_SIGNALS */
606 /* **************************************************************** */
608 /* SIGINT Management */
610 /* **************************************************************** */
612 #if defined (HAVE_POSIX_SIGNALS)
613 static sigset_t sigint_set
, sigint_oset
;
614 static sigset_t sigwinch_set
, sigwinch_oset
;
615 #else /* !HAVE_POSIX_SIGNALS */
616 # if defined (HAVE_BSD_SIGNALS)
617 static int sigint_oldmask
;
618 static int sigwinch_oldmask
;
619 # endif /* HAVE_BSD_SIGNALS */
620 #endif /* !HAVE_POSIX_SIGNALS */
622 static int sigint_blocked
;
623 static int sigwinch_blocked
;
625 /* Cause SIGINT to not be delivered until the corresponding call to
628 _rl_block_sigint (void)
636 /* Allow SIGINT to be delivered. */
638 _rl_release_sigint (void)
640 if (sigint_blocked
== 0)
647 /* Cause SIGWINCH to not be delivered until the corresponding call to
648 release_sigwinch(). */
650 _rl_block_sigwinch (void)
652 if (sigwinch_blocked
)
655 #if defined (SIGWINCH)
657 #if defined (HAVE_POSIX_SIGNALS)
658 sigemptyset (&sigwinch_set
);
659 sigemptyset (&sigwinch_oset
);
660 sigaddset (&sigwinch_set
, SIGWINCH
);
661 sigprocmask (SIG_BLOCK
, &sigwinch_set
, &sigwinch_oset
);
662 #else /* !HAVE_POSIX_SIGNALS */
663 # if defined (HAVE_BSD_SIGNALS)
664 sigwinch_oldmask
= sigblock (sigmask (SIGWINCH
));
665 # else /* !HAVE_BSD_SIGNALS */
666 # if defined (HAVE_USG_SIGHOLD)
668 # endif /* HAVE_USG_SIGHOLD */
669 # endif /* !HAVE_BSD_SIGNALS */
670 #endif /* !HAVE_POSIX_SIGNALS */
672 #endif /* SIGWINCH */
674 sigwinch_blocked
= 1;
677 /* Allow SIGWINCH to be delivered. */
679 _rl_release_sigwinch (void)
681 if (sigwinch_blocked
== 0)
684 #if defined (SIGWINCH)
686 #if defined (HAVE_POSIX_SIGNALS)
687 sigprocmask (SIG_SETMASK
, &sigwinch_oset
, (sigset_t
*)NULL
);
689 # if defined (HAVE_BSD_SIGNALS)
690 sigsetmask (sigwinch_oldmask
);
691 # else /* !HAVE_BSD_SIGNALS */
692 # if defined (HAVE_USG_SIGHOLD)
694 # endif /* HAVE_USG_SIGHOLD */
695 # endif /* !HAVE_BSD_SIGNALS */
696 #endif /* !HAVE_POSIX_SIGNALS */
698 #endif /* SIGWINCH */
700 sigwinch_blocked
= 0;
703 /* **************************************************************** */
705 /* Echoing special control characters */
707 /* **************************************************************** */
709 rl_echo_signal_char (int sig
)
714 if (_rl_echoctl
== 0 || _rl_echo_control_chars
== 0)
719 case SIGINT
: c
= _rl_intr_char
; break;
720 #if defined (SIGQUIT)
721 case SIGQUIT
: c
= _rl_quit_char
; break;
723 #if defined (SIGTSTP)
724 case SIGTSTP
: c
= _rl_susp_char
; break;
729 if (CTRL_CHAR (c
) || c
== RUBOUT
)
732 cstr
[1] = CTRL_CHAR (c
) ? UNCTRL (c
) : '?';
733 cstr
[cslen
= 2] = '\0';
738 cstr
[cslen
= 1] = '\0';
741 _rl_output_some_chars (cstr
, cslen
);