325ae8cce46490a4a90c9640660465286e0ff5d5
[platform/upstream/bash.git] / lib / readline / signals.c
1 /* signals.c -- signal handling support for readline. */
2
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
4
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.      
7
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.
12
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.
17
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/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>              /* Just for NULL.  Yuck. */
29 #include <sys/types.h>
30 #include <signal.h>
31
32 #if defined (HAVE_UNISTD_H)
33 #  include <unistd.h>
34 #endif /* HAVE_UNISTD_H */
35
36 /* System-specific feature definitions and include files. */
37 #include "rldefs.h"
38
39 #if defined (GWINSZ_IN_SYS_IOCTL)
40 #  include <sys/ioctl.h>
41 #endif /* GWINSZ_IN_SYS_IOCTL */
42
43 /* Some standard library routines. */
44 #include "readline.h"
45 #include "history.h"
46
47 #include "rlprivate.h"
48
49 #if defined (HANDLE_SIGNALS)
50
51 #if !defined (RETSIGTYPE)
52 #  if defined (VOID_SIGHANDLER)
53 #    define RETSIGTYPE void
54 #  else
55 #    define RETSIGTYPE int
56 #  endif /* !VOID_SIGHANDLER */
57 #endif /* !RETSIGTYPE */
58
59 #if defined (VOID_SIGHANDLER)
60 #  define SIGHANDLER_RETURN return
61 #else
62 #  define SIGHANDLER_RETURN return (0)
63 #endif
64
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 ();
68
69 #if defined (HAVE_POSIX_SIGNALS)
70 typedef struct sigaction sighandler_cxt;
71 #  define rl_sigaction(s, nh, oh)       sigaction(s, nh, oh)
72 #else
73 typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt;
74 #  define sigemptyset(m)
75 #endif /* !HAVE_POSIX_SIGNALS */
76
77 #ifndef SA_RESTART
78 #  define SA_RESTART 0
79 #endif
80
81 static SigHandler *rl_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *));
82 static void rl_maybe_set_sighandler PARAMS((int, SigHandler *, sighandler_cxt *));
83
84 static RETSIGTYPE rl_signal_handler PARAMS((int));
85 static RETSIGTYPE _rl_handle_signal PARAMS((int));
86      
87 /* Exported variables for use by applications. */
88
89 /* If non-zero, readline will install its own signal handlers for
90    SIGINT, SIGTERM, SIGQUIT, SIGALRM, SIGTSTP, SIGTTIN, and SIGTTOU. */
91 int rl_catch_signals = 1;
92
93 /* If non-zero, readline will install a signal handler for SIGWINCH. */
94 #ifdef SIGWINCH
95 int rl_catch_sigwinch = 1;
96 #else
97 int rl_catch_sigwinch = 0;      /* for the readline state struct in readline.c */
98 #endif
99
100 /* Private variables. */
101 int _rl_interrupt_immediately = 0;
102 int volatile _rl_caught_signal = 0;     /* should be sig_atomic_t, but that requires including <signal.h> everywhere */
103
104 /* If non-zero, print characters corresponding to received signals. */
105 int _rl_echoctl = 0;
106
107 int _rl_intr_char = 0;
108 int _rl_quit_char = 0;
109 int _rl_susp_char = 0;
110
111 static int signals_set_flag;
112 static int sigwinch_set_flag;
113
114 /* **************************************************************** */
115 /*                                                                  */
116 /*                         Signal Handling                          */
117 /*                                                                  */
118 /* **************************************************************** */
119
120 static sighandler_cxt old_int, old_term, old_alrm, old_quit;
121 #if defined (SIGTSTP)
122 static sighandler_cxt old_tstp, old_ttou, old_ttin;
123 #endif
124 #if defined (SIGWINCH)
125 static sighandler_cxt old_winch;
126 #endif
127
128 /* Readline signal handler functions. */
129
130 /* Called from RL_CHECK_SIGNALS() macro */
131 RETSIGTYPE
132 _rl_signal_handler (sig)
133 {
134   _rl_caught_signal = 0;        /* XXX */
135
136   _rl_handle_signal (sig);
137   SIGHANDLER_RETURN;
138 }
139
140 static RETSIGTYPE
141 rl_signal_handler (sig)
142      int sig;
143 {
144   if (_rl_interrupt_immediately)
145     {
146       _rl_interrupt_immediately = 0;
147       _rl_handle_signal (sig);
148     }
149
150   _rl_caught_signal = sig;
151   SIGHANDLER_RETURN;
152 }
153
154 static RETSIGTYPE
155 _rl_handle_signal (sig)
156      int sig;
157 {
158 #if defined (HAVE_POSIX_SIGNALS)
159   sigset_t set;
160 #else /* !HAVE_POSIX_SIGNALS */
161 #  if defined (HAVE_BSD_SIGNALS)
162   long omask;
163 #  else /* !HAVE_BSD_SIGNALS */
164   sighandler_cxt dummy_cxt;     /* needed for rl_set_sighandler call */
165 #  endif /* !HAVE_BSD_SIGNALS */
166 #endif /* !HAVE_POSIX_SIGNALS */
167
168   RL_SETSTATE(RL_STATE_SIGHANDLER);
169
170 #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
171   /* Since the signal will not be blocked while we are in the signal
172      handler, ignore it until rl_clear_signals resets the catcher. */
173 #  if defined (SIGALRM)
174   if (sig == SIGINT || sig == SIGALRM)
175 #  else
176   if (sig == SIGINT)
177 #  endif
178     rl_set_sighandler (sig, SIG_IGN, &dummy_cxt);
179 #endif /* !HAVE_BSD_SIGNALS && !HAVE_POSIX_SIGNALS */
180
181   switch (sig)
182     {
183     case SIGINT:
184       _rl_reset_completion_state ();
185       rl_free_line_state ();
186       /* FALLTHROUGH */
187
188     case SIGTERM:
189 #if defined (SIGTSTP)
190     case SIGTSTP:
191     case SIGTTOU:
192     case SIGTTIN:
193 #endif /* SIGTSTP */
194 #if defined (SIGALRM)
195     case SIGALRM:
196 #endif
197 #if defined (SIGQUIT)
198     case SIGQUIT:
199 #endif
200       rl_echo_signal_char (sig);
201       rl_cleanup_after_signal ();
202
203 #if defined (HAVE_POSIX_SIGNALS)
204       sigemptyset (&set);
205       sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
206       sigdelset (&set, sig);
207 #else /* !HAVE_POSIX_SIGNALS */
208 #  if defined (HAVE_BSD_SIGNALS)
209       omask = sigblock (0);
210 #  endif /* HAVE_BSD_SIGNALS */
211 #endif /* !HAVE_POSIX_SIGNALS */
212
213 #if defined (__EMX__)
214       signal (sig, SIG_ACK);
215 #endif
216
217 #if defined (HAVE_KILL)
218       kill (getpid (), sig);
219 #else
220       raise (sig);              /* assume we have raise */
221 #endif
222
223       /* Let the signal that we just sent through.  */
224 #if defined (HAVE_POSIX_SIGNALS)
225       sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
226 #else /* !HAVE_POSIX_SIGNALS */
227 #  if defined (HAVE_BSD_SIGNALS)
228       sigsetmask (omask & ~(sigmask (sig)));
229 #  endif /* HAVE_BSD_SIGNALS */
230 #endif /* !HAVE_POSIX_SIGNALS */
231
232       rl_reset_after_signal ();
233     }
234
235   RL_UNSETSTATE(RL_STATE_SIGHANDLER);
236   SIGHANDLER_RETURN;
237 }
238
239 #if defined (SIGWINCH)
240 static RETSIGTYPE
241 rl_sigwinch_handler (sig)
242      int sig;
243 {
244   SigHandler *oh;
245
246 #if defined (MUST_REINSTALL_SIGHANDLERS)
247   sighandler_cxt dummy_winch;
248
249   /* We don't want to change old_winch -- it holds the state of SIGWINCH
250      disposition set by the calling application.  We need this state
251      because we call the application's SIGWINCH handler after updating
252      our own idea of the screen size. */
253   rl_set_sighandler (SIGWINCH, rl_sigwinch_handler, &dummy_winch);
254 #endif
255
256   RL_SETSTATE(RL_STATE_SIGHANDLER);
257   rl_resize_terminal ();
258
259   /* If another sigwinch handler has been installed, call it. */
260   oh = (SigHandler *)old_winch.sa_handler;
261   if (oh &&  oh != (SigHandler *)SIG_IGN && oh != (SigHandler *)SIG_DFL)
262     (*oh) (sig);
263
264   RL_UNSETSTATE(RL_STATE_SIGHANDLER);
265   SIGHANDLER_RETURN;
266 }
267 #endif  /* SIGWINCH */
268
269 /* Functions to manage signal handling. */
270
271 #if !defined (HAVE_POSIX_SIGNALS)
272 static int
273 rl_sigaction (sig, nh, oh)
274      int sig;
275      sighandler_cxt *nh, *oh;
276 {
277   oh->sa_handler = signal (sig, nh->sa_handler);
278   return 0;
279 }
280 #endif /* !HAVE_POSIX_SIGNALS */
281
282 /* Set up a readline-specific signal handler, saving the old signal
283    information in OHANDLER.  Return the old signal handler, like
284    signal(). */
285 static SigHandler *
286 rl_set_sighandler (sig, handler, ohandler)
287      int sig;
288      SigHandler *handler;
289      sighandler_cxt *ohandler;
290 {
291   sighandler_cxt old_handler;
292 #if defined (HAVE_POSIX_SIGNALS)
293   struct sigaction act;
294
295   act.sa_handler = handler;
296 #  if defined (SIGWINCH)
297   act.sa_flags = (sig == SIGWINCH) ? SA_RESTART : 0;
298 #  else
299   act.sa_flags = 0;
300 #  endif /* SIGWINCH */
301   sigemptyset (&act.sa_mask);
302   sigemptyset (&ohandler->sa_mask);
303   sigaction (sig, &act, &old_handler);
304 #else
305   old_handler.sa_handler = (SigHandler *)signal (sig, handler);
306 #endif /* !HAVE_POSIX_SIGNALS */
307
308   /* XXX -- assume we have memcpy */
309   /* If rl_set_signals is called twice in a row, don't set the old handler to
310      rl_signal_handler, because that would cause infinite recursion. */
311   if (handler != rl_signal_handler || old_handler.sa_handler != rl_signal_handler)
312     memcpy (ohandler, &old_handler, sizeof (sighandler_cxt));
313
314   return (ohandler->sa_handler);
315 }
316
317 static void
318 rl_maybe_set_sighandler (sig, handler, ohandler)
319      int sig;
320      SigHandler *handler;
321      sighandler_cxt *ohandler;
322 {
323   sighandler_cxt dummy;
324   SigHandler *oh;
325
326   sigemptyset (&dummy.sa_mask);
327   oh = rl_set_sighandler (sig, handler, ohandler);
328   if (oh == (SigHandler *)SIG_IGN)
329     rl_sigaction (sig, ohandler, &dummy);
330 }
331
332 int
333 rl_set_signals ()
334 {
335   sighandler_cxt dummy;
336   SigHandler *oh;
337 #if defined (HAVE_POSIX_SIGNALS)
338   static int sigmask_set = 0;
339   static sigset_t bset, oset;
340 #endif
341
342 #if defined (HAVE_POSIX_SIGNALS)
343   if (rl_catch_signals && sigmask_set == 0)
344     {
345       sigemptyset (&bset);
346
347       sigaddset (&bset, SIGINT);
348       sigaddset (&bset, SIGTERM);
349 #if defined (SIGQUIT)
350       sigaddset (&bset, SIGQUIT);
351 #endif
352 #if defined (SIGALRM)
353       sigaddset (&bset, SIGALRM);
354 #endif
355 #if defined (SIGTSTP)
356       sigaddset (&bset, SIGTSTP);
357 #endif
358 #if defined (SIGTTIN)
359       sigaddset (&bset, SIGTTIN);
360 #endif
361 #if defined (SIGTTOU)
362       sigaddset (&bset, SIGTTOU);
363 #endif
364       sigmask_set = 1;
365     }      
366 #endif /* HAVE_POSIX_SIGNALS */
367
368   if (rl_catch_signals && signals_set_flag == 0)
369     {
370 #if defined (HAVE_POSIX_SIGNALS)
371       sigemptyset (&oset);
372       sigprocmask (SIG_BLOCK, &bset, &oset);
373 #endif
374
375       rl_maybe_set_sighandler (SIGINT, rl_signal_handler, &old_int);
376       rl_maybe_set_sighandler (SIGTERM, rl_signal_handler, &old_term);
377 #if defined (SIGQUIT)
378       rl_maybe_set_sighandler (SIGQUIT, rl_signal_handler, &old_quit);
379 #endif
380
381 #if defined (SIGALRM)
382       oh = rl_set_sighandler (SIGALRM, rl_signal_handler, &old_alrm);
383       if (oh == (SigHandler *)SIG_IGN)
384         rl_sigaction (SIGALRM, &old_alrm, &dummy);
385 #if defined (HAVE_POSIX_SIGNALS) && defined (SA_RESTART)
386       /* If the application using readline has already installed a signal
387          handler with SA_RESTART, SIGALRM will cause reads to be restarted
388          automatically, so readline should just get out of the way.  Since
389          we tested for SIG_IGN above, we can just test for SIG_DFL here. */
390       if (oh != (SigHandler *)SIG_DFL && (old_alrm.sa_flags & SA_RESTART))
391         rl_sigaction (SIGALRM, &old_alrm, &dummy);
392 #endif /* HAVE_POSIX_SIGNALS */
393 #endif /* SIGALRM */
394
395 #if defined (SIGTSTP)
396       rl_maybe_set_sighandler (SIGTSTP, rl_signal_handler, &old_tstp);
397 #endif /* SIGTSTP */
398
399 #if defined (SIGTTOU)
400       rl_maybe_set_sighandler (SIGTTOU, rl_signal_handler, &old_ttou);
401 #endif /* SIGTTOU */
402
403 #if defined (SIGTTIN)
404       rl_maybe_set_sighandler (SIGTTIN, rl_signal_handler, &old_ttin);
405 #endif /* SIGTTIN */
406
407       signals_set_flag = 1;
408
409 #if defined (HAVE_POSIX_SIGNALS)
410       sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
411 #endif
412     }
413
414 #if defined (SIGWINCH)
415   if (rl_catch_sigwinch && sigwinch_set_flag == 0)
416     {
417       rl_maybe_set_sighandler (SIGWINCH, rl_sigwinch_handler, &old_winch);
418       sigwinch_set_flag = 1;
419     }
420 #endif /* SIGWINCH */
421
422   return 0;
423 }
424
425 int
426 rl_clear_signals ()
427 {
428   sighandler_cxt dummy;
429
430   if (rl_catch_signals && signals_set_flag == 1)
431     {
432       sigemptyset (&dummy.sa_mask);
433
434       rl_sigaction (SIGINT, &old_int, &dummy);
435       rl_sigaction (SIGTERM, &old_term, &dummy);
436 #if defined (SIGQUIT)
437       rl_sigaction (SIGQUIT, &old_quit, &dummy);
438 #endif
439 #if defined (SIGALRM)
440       rl_sigaction (SIGALRM, &old_alrm, &dummy);
441 #endif
442
443 #if defined (SIGTSTP)
444       rl_sigaction (SIGTSTP, &old_tstp, &dummy);
445 #endif /* SIGTSTP */
446
447 #if defined (SIGTTOU)
448       rl_sigaction (SIGTTOU, &old_ttou, &dummy);
449 #endif /* SIGTTOU */
450
451 #if defined (SIGTTIN)
452       rl_sigaction (SIGTTIN, &old_ttin, &dummy);
453 #endif /* SIGTTIN */
454
455       signals_set_flag = 0;
456     }
457
458 #if defined (SIGWINCH)
459   if (rl_catch_sigwinch && sigwinch_set_flag == 1)
460     {
461       sigemptyset (&dummy.sa_mask);
462       rl_sigaction (SIGWINCH, &old_winch, &dummy);
463       sigwinch_set_flag = 0;
464     }
465 #endif
466
467   return 0;
468 }
469
470 /* Clean up the terminal and readline state after catching a signal, before
471    resending it to the calling application. */
472 void
473 rl_cleanup_after_signal ()
474 {
475   _rl_clean_up_for_exit ();
476   if (rl_deprep_term_function)
477     (*rl_deprep_term_function) ();
478   rl_clear_pending_input ();
479   rl_clear_signals ();
480 }
481
482 /* Reset the terminal and readline state after a signal handler returns. */
483 void
484 rl_reset_after_signal ()
485 {
486   if (rl_prep_term_function)
487     (*rl_prep_term_function) (_rl_meta_flag);
488   rl_set_signals ();
489 }
490
491 /* Free up the readline variable line state for the current line (undo list,
492    any partial history entry, any keyboard macros in progress, and any
493    numeric arguments in process) after catching a signal, before calling
494    rl_cleanup_after_signal(). */ 
495 void
496 rl_free_line_state ()
497 {
498   register HIST_ENTRY *entry;
499
500   rl_free_undo_list ();
501
502   entry = current_history ();
503   if (entry)
504     entry->data = (char *)NULL;
505
506   _rl_kill_kbd_macro ();
507   rl_clear_message ();
508   _rl_reset_argument ();
509 }
510
511 #endif  /* HANDLE_SIGNALS */
512
513 /* **************************************************************** */
514 /*                                                                  */
515 /*                         SIGINT Management                        */
516 /*                                                                  */
517 /* **************************************************************** */
518
519 #if defined (HAVE_POSIX_SIGNALS)
520 static sigset_t sigint_set, sigint_oset;
521 #else /* !HAVE_POSIX_SIGNALS */
522 #  if defined (HAVE_BSD_SIGNALS)
523 static int sigint_oldmask;
524 #  endif /* HAVE_BSD_SIGNALS */
525 #endif /* !HAVE_POSIX_SIGNALS */
526
527 static int sigint_blocked;
528
529 /* Cause SIGINT to not be delivered until the corresponding call to
530    release_sigint(). */
531 void
532 _rl_block_sigint ()
533 {
534   if (sigint_blocked)
535     return;
536
537 #if defined (HAVE_POSIX_SIGNALS)
538   sigemptyset (&sigint_set);
539   sigemptyset (&sigint_oset);
540   sigaddset (&sigint_set, SIGINT);
541   sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset);
542 #else /* !HAVE_POSIX_SIGNALS */
543 #  if defined (HAVE_BSD_SIGNALS)
544   sigint_oldmask = sigblock (sigmask (SIGINT));
545 #  else /* !HAVE_BSD_SIGNALS */
546 #    if defined (HAVE_USG_SIGHOLD)
547   sighold (SIGINT);
548 #    endif /* HAVE_USG_SIGHOLD */
549 #  endif /* !HAVE_BSD_SIGNALS */
550 #endif /* !HAVE_POSIX_SIGNALS */
551
552   sigint_blocked = 1;
553 }
554
555 /* Allow SIGINT to be delivered. */
556 void
557 _rl_release_sigint ()
558 {
559   if (sigint_blocked == 0)
560     return;
561
562 #if defined (HAVE_POSIX_SIGNALS)
563   sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL);
564 #else
565 #  if defined (HAVE_BSD_SIGNALS)
566   sigsetmask (sigint_oldmask);
567 #  else /* !HAVE_BSD_SIGNALS */
568 #    if defined (HAVE_USG_SIGHOLD)
569   sigrelse (SIGINT);
570 #    endif /* HAVE_USG_SIGHOLD */
571 #  endif /* !HAVE_BSD_SIGNALS */
572 #endif /* !HAVE_POSIX_SIGNALS */
573
574   sigint_blocked = 0;
575 }
576
577 /* **************************************************************** */
578 /*                                                                  */
579 /*              Echoing special control characters                  */
580 /*                                                                  */
581 /* **************************************************************** */
582 void
583 rl_echo_signal_char (sig)
584      int sig;
585 {
586   char cstr[3];
587   int cslen, c;
588
589   if (_rl_echoctl == 0)
590     return;
591
592   switch (sig)
593     {
594     case SIGINT:  c = _rl_intr_char; break;
595     case SIGQUIT: c = _rl_quit_char; break;
596     case SIGTSTP: c = _rl_susp_char; break;
597     default: return;
598     }
599
600   if (CTRL_CHAR (c) || c == RUBOUT)
601     {
602       cstr[0] = '^';
603       cstr[1] = CTRL_CHAR (c) ? UNCTRL (c) : '?';
604       cstr[cslen = 2] = '\0';
605     }
606   else
607     {
608       cstr[0] = c;
609       cstr[cslen = 1] = '\0';
610     }
611
612   _rl_output_some_chars (cstr, cslen);
613 }