git-svn-id: svn://svn.berlios.de/mutt-ng/trunk@33 e385b8ad-14ed-0310-8656-cc95a2468c6d
[apps/madmutt.git] / signal.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */ 
18
19 #include "mutt.h"
20 #include "mutt_curses.h"
21
22 #include <signal.h>
23 #include <string.h>
24 #include <sys/wait.h>
25 #include <errno.h>
26
27 static sigset_t Sigset;
28 static sigset_t SigsetSys;
29 static struct sigaction SysOldInt;
30 static struct sigaction SysOldQuit;
31 static int IsEndwin = 0;
32
33 /* Attempt to catch "ordinary" signals and shut down gracefully. */
34 RETSIGTYPE exit_handler (int sig)
35 {
36   curs_set (1);
37   endwin (); /* just to be safe */
38 #if SYS_SIGLIST_DECLARED
39   printf(_("%s...  Exiting.\n"), sys_siglist[sig]);
40 #else
41 #if (__sun__ && __svr4__)
42   printf(_("Caught %s...  Exiting.\n"), _sys_siglist[sig]);
43 #else
44 #if (__alpha && __osf__)
45   printf(_("Caught %s...  Exiting.\n"), __sys_siglist[sig]);
46 #else
47   printf(_("Caught signal %d...  Exiting.\n"), sig);
48 #endif
49 #endif
50 #endif
51   exit (0);
52 }
53
54 RETSIGTYPE chld_handler (int sig)
55 {
56   /* empty */
57 }
58
59 RETSIGTYPE sighandler (int sig)
60 {
61   int save_errno = errno;
62
63   switch (sig)
64   {
65     case SIGTSTP: /* user requested a suspend */
66       if (!option (OPTSUSPEND))
67         break;
68       IsEndwin = isendwin ();
69       curs_set (1);
70       if (!IsEndwin)
71         endwin ();
72       kill (0, SIGSTOP);
73
74     case SIGCONT:
75       if (!IsEndwin)
76         refresh ();
77       mutt_curs_set (-1);
78       break;
79
80 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
81     case SIGWINCH:
82       SigWinch = 1;
83       break;
84 #endif
85
86     case SIGINT:
87       SigInt = 1;
88       break;
89
90   }
91   errno = save_errno;
92 }
93
94 #ifdef USE_SLANG_CURSES
95 int mutt_intr_hook (void)
96 {
97   return (-1);
98 }
99 #endif /* USE_SLANG_CURSES */
100
101 void mutt_signal_init (void)
102 {
103   struct sigaction act;
104
105   sigemptyset (&act.sa_mask);
106   act.sa_flags = 0;
107   act.sa_handler = SIG_IGN;
108   sigaction (SIGPIPE, &act, NULL);
109
110   act.sa_handler = exit_handler;
111   sigaction (SIGTERM, &act, NULL);
112   sigaction (SIGHUP, &act, NULL);
113   sigaction (SIGQUIT, &act, NULL);
114
115   /* we want to avoid race conditions */
116   sigaddset (&act.sa_mask, SIGTSTP);
117
118   act.sa_handler = sighandler;
119
120   /* we want SIGALRM to abort the current syscall, so we do this before
121    * setting the SA_RESTART flag below.  currently this is only used to
122    * timeout on a connect() call in a reasonable amout of time.
123    */
124   sigaction (SIGALRM, &act, NULL);
125
126   /* we also don't want to mess with interrupted system calls */
127 #ifdef SA_RESTART
128   act.sa_flags = SA_RESTART;
129 #endif
130
131   sigaction (SIGCONT, &act, NULL);
132   sigaction (SIGTSTP, &act, NULL);
133   sigaction (SIGINT, &act, NULL);
134 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
135   sigaction (SIGWINCH, &act, NULL);
136 #endif
137
138   /* POSIX doesn't allow us to ignore SIGCHLD,
139    * so we just install a dummy handler for it
140    */
141   act.sa_handler = chld_handler;
142   /* don't need to block any other signals here */
143   sigemptyset (&act.sa_mask);
144   /* we don't want to mess with stopped children */
145   act.sa_flags |= SA_NOCLDSTOP;
146   sigaction (SIGCHLD, &act, NULL);
147
148 #ifdef USE_SLANG_CURSES
149   /* This bit of code is required because of the implementation of
150    * SLcurses_wgetch().  If a signal is received (like SIGWINCH) when we
151    * are in blocking mode, SLsys_getkey() will not return an error unless
152    * a handler function is defined and it returns -1.  This is needed so
153    * that if the user resizes the screen while at a prompt, it will just
154    * abort and go back to the main-menu.
155    */
156   SLang_getkey_intr_hook = mutt_intr_hook;
157 #endif
158 }
159
160 /* signals which are important to block while doing critical ops */
161 void mutt_block_signals (void)
162 {
163   if (!option (OPTSIGNALSBLOCKED))
164   {
165     sigemptyset (&Sigset);
166     sigaddset (&Sigset, SIGTERM);
167     sigaddset (&Sigset, SIGHUP);
168     sigaddset (&Sigset, SIGTSTP);
169     sigaddset (&Sigset, SIGINT);
170 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
171     sigaddset (&Sigset, SIGWINCH);
172 #endif
173     sigprocmask (SIG_BLOCK, &Sigset, 0);
174     set_option (OPTSIGNALSBLOCKED);
175   }
176 }
177
178 /* restore the previous signal mask */
179 void mutt_unblock_signals (void)
180 {
181   if (option (OPTSIGNALSBLOCKED))
182   {
183     sigprocmask (SIG_UNBLOCK, &Sigset, 0);
184     unset_option (OPTSIGNALSBLOCKED);
185   }
186 }
187
188 void mutt_block_signals_system (void)
189 {
190   struct sigaction sa;
191
192   if (! option (OPTSYSSIGNALSBLOCKED))
193   {
194     /* POSIX: ignore SIGINT and SIGQUIT & block SIGCHLD  before exec */
195     sa.sa_handler = SIG_IGN;
196     sa.sa_flags = 0;
197     sigemptyset (&sa.sa_mask);
198     sigaction (SIGINT, &sa, &SysOldInt);
199     sigaction (SIGQUIT, &sa, &SysOldQuit);
200
201     sigemptyset (&SigsetSys);
202     sigaddset (&SigsetSys, SIGCHLD);
203     sigprocmask (SIG_BLOCK, &SigsetSys, 0);
204     set_option (OPTSYSSIGNALSBLOCKED);
205   }
206 }
207
208 void mutt_unblock_signals_system (int catch)
209 {
210   if (option (OPTSYSSIGNALSBLOCKED))
211   {
212     sigprocmask (SIG_UNBLOCK, &SigsetSys, NULL);
213     if (catch)
214     {
215       sigaction (SIGQUIT, &SysOldQuit, NULL);
216       sigaction (SIGINT, &SysOldInt, NULL);
217     }
218     else
219     {
220       struct sigaction sa;
221
222       sa.sa_handler = SIG_DFL;
223       sigemptyset (&sa.sa_mask);
224       sa.sa_flags = 0;
225       sigaction (SIGQUIT, &sa, NULL);
226       sigaction (SIGINT, &sa, NULL);
227     }
228
229     unset_option (OPTSYSSIGNALSBLOCKED);
230   }
231 }
232
233 void mutt_allow_interrupt (int disposition)
234 {
235   struct sigaction sa;
236   
237   memset (&sa, 0, sizeof sa);
238   sa.sa_handler = sighandler;
239 #ifdef SA_RESTART
240   if (disposition == 0)
241     sa.sa_flags |= SA_RESTART;
242 #endif
243   sigaction (SIGINT, &sa, NULL);
244 }