2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_curses.h"
31 static sigset_t Sigset;
32 static sigset_t SigsetSys;
33 static struct sigaction SysOldInt;
34 static struct sigaction SysOldQuit;
35 static int IsEndwin = 0;
37 /* Attempt to catch "ordinary" signals and shut down gracefully. */
38 static RETSIGTYPE exit_handler (int sig)
41 endwin (); /* just to be safe */
42 #if SYS_SIGLIST_DECLARED
43 printf(_("%s... Exiting.\n"), sys_siglist[sig]);
45 #if (__sun__ && __svr4__)
46 printf(_("Caught %s... Exiting.\n"), _sys_siglist[sig]);
48 #if (__alpha && __osf__)
49 printf(_("Caught %s... Exiting.\n"), __sys_siglist[sig]);
51 printf(_("Caught signal %d... Exiting.\n"), sig);
58 static RETSIGTYPE chld_handler (int sig)
63 static RETSIGTYPE sighandler (int sig)
65 int save_errno = errno;
69 case SIGTSTP: /* user requested a suspend */
70 if (!option (OPTSUSPEND))
72 IsEndwin = isendwin ();
82 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
83 /* We don't receive SIGWINCH when suspended; however, no harm is done by
84 * just assuming we received one, and triggering the 'resize' anyway. */
89 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
103 #ifdef USE_SLANG_CURSES
104 int mutt_intr_hook (void)
108 #endif /* USE_SLANG_CURSES */
110 void mutt_signal_init (void)
112 struct sigaction act;
114 sigemptyset (&act.sa_mask);
116 act.sa_handler = SIG_IGN;
117 sigaction (SIGPIPE, &act, NULL);
119 act.sa_handler = exit_handler;
120 sigaction (SIGTERM, &act, NULL);
121 sigaction (SIGHUP, &act, NULL);
122 sigaction (SIGQUIT, &act, NULL);
124 /* we want to avoid race conditions */
125 sigaddset (&act.sa_mask, SIGTSTP);
127 act.sa_handler = sighandler;
129 /* we want SIGALRM to abort the current syscall, so we do this before
130 * setting the SA_RESTART flag below. currently this is only used to
131 * timeout on a connect() call in a reasonable amout of time.
133 sigaction (SIGALRM, &act, NULL);
135 /* we also don't want to mess with interrupted system calls */
137 act.sa_flags = SA_RESTART;
140 sigaction (SIGCONT, &act, NULL);
141 sigaction (SIGTSTP, &act, NULL);
142 sigaction (SIGINT, &act, NULL);
143 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
144 sigaction (SIGWINCH, &act, NULL);
147 /* POSIX doesn't allow us to ignore SIGCHLD,
148 * so we just install a dummy handler for it
150 act.sa_handler = chld_handler;
151 /* don't need to block any other signals here */
152 sigemptyset (&act.sa_mask);
153 /* we don't want to mess with stopped children */
154 act.sa_flags |= SA_NOCLDSTOP;
155 sigaction (SIGCHLD, &act, NULL);
157 #ifdef USE_SLANG_CURSES
158 /* This bit of code is required because of the implementation of
159 * SLcurses_wgetch(). If a signal is received (like SIGWINCH) when we
160 * are in blocking mode, SLsys_getkey() will not return an error unless
161 * a handler function is defined and it returns -1. This is needed so
162 * that if the user resizes the screen while at a prompt, it will just
163 * abort and go back to the main-menu.
165 SLang_getkey_intr_hook = mutt_intr_hook;
169 /* signals which are important to block while doing critical ops */
170 void mutt_block_signals (void)
172 if (!option (OPTSIGNALSBLOCKED))
174 sigemptyset (&Sigset);
175 sigaddset (&Sigset, SIGTERM);
176 sigaddset (&Sigset, SIGHUP);
177 sigaddset (&Sigset, SIGTSTP);
178 sigaddset (&Sigset, SIGINT);
179 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
180 sigaddset (&Sigset, SIGWINCH);
182 sigprocmask (SIG_BLOCK, &Sigset, 0);
183 set_option (OPTSIGNALSBLOCKED);
187 /* restore the previous signal mask */
188 void mutt_unblock_signals (void)
190 if (option (OPTSIGNALSBLOCKED))
192 sigprocmask (SIG_UNBLOCK, &Sigset, 0);
193 unset_option (OPTSIGNALSBLOCKED);
197 void mutt_block_signals_system (void)
201 if (! option (OPTSYSSIGNALSBLOCKED))
203 /* POSIX: ignore SIGINT and SIGQUIT & block SIGCHLD before exec */
204 sa.sa_handler = SIG_IGN;
206 sigemptyset (&sa.sa_mask);
207 sigaction (SIGINT, &sa, &SysOldInt);
208 sigaction (SIGQUIT, &sa, &SysOldQuit);
210 sigemptyset (&SigsetSys);
211 sigaddset (&SigsetSys, SIGCHLD);
212 sigprocmask (SIG_BLOCK, &SigsetSys, 0);
213 set_option (OPTSYSSIGNALSBLOCKED);
217 void mutt_unblock_signals_system (int catch)
219 if (option (OPTSYSSIGNALSBLOCKED))
221 sigprocmask (SIG_UNBLOCK, &SigsetSys, NULL);
224 sigaction (SIGQUIT, &SysOldQuit, NULL);
225 sigaction (SIGINT, &SysOldInt, NULL);
231 sa.sa_handler = SIG_DFL;
232 sigemptyset (&sa.sa_mask);
234 sigaction (SIGQUIT, &sa, NULL);
235 sigaction (SIGINT, &sa, NULL);
238 unset_option (OPTSYSSIGNALSBLOCKED);
242 void mutt_allow_interrupt (int disposition)
246 memset (&sa, 0, sizeof sa);
247 sa.sa_handler = sighandler;
249 if (disposition == 0)
250 sa.sa_flags |= SA_RESTART;
252 sigaction (SIGINT, &sa, NULL);