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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 #include "mutt_curses.h"
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;
33 /* Attempt to catch "ordinary" signals and shut down gracefully. */
34 RETSIGTYPE exit_handler (int sig)
37 endwin (); /* just to be safe */
38 #if SYS_SIGLIST_DECLARED
39 printf(_("%s... Exiting.\n"), sys_siglist[sig]);
41 #if (__sun__ && __svr4__)
42 printf(_("Caught %s... Exiting.\n"), _sys_siglist[sig]);
44 #if (__alpha && __osf__)
45 printf(_("Caught %s... Exiting.\n"), __sys_siglist[sig]);
47 printf(_("Caught signal %d... Exiting.\n"), sig);
54 RETSIGTYPE chld_handler (int sig)
59 RETSIGTYPE sighandler (int sig)
61 int save_errno = errno;
65 case SIGTSTP: /* user requested a suspend */
66 if (!option (OPTSUSPEND))
68 IsEndwin = isendwin ();
80 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
94 #ifdef USE_SLANG_CURSES
95 int mutt_intr_hook (void)
99 #endif /* USE_SLANG_CURSES */
101 void mutt_signal_init (void)
103 struct sigaction act;
105 sigemptyset (&act.sa_mask);
107 act.sa_handler = SIG_IGN;
108 sigaction (SIGPIPE, &act, NULL);
110 act.sa_handler = exit_handler;
111 sigaction (SIGTERM, &act, NULL);
112 sigaction (SIGHUP, &act, NULL);
113 sigaction (SIGQUIT, &act, NULL);
115 /* we want to avoid race conditions */
116 sigaddset (&act.sa_mask, SIGTSTP);
118 act.sa_handler = sighandler;
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.
124 sigaction (SIGALRM, &act, NULL);
126 /* we also don't want to mess with interrupted system calls */
128 act.sa_flags = SA_RESTART;
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);
138 /* POSIX doesn't allow us to ignore SIGCHLD,
139 * so we just install a dummy handler for it
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);
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.
156 SLang_getkey_intr_hook = mutt_intr_hook;
160 /* signals which are important to block while doing critical ops */
161 void mutt_block_signals (void)
163 if (!option (OPTSIGNALSBLOCKED))
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);
173 sigprocmask (SIG_BLOCK, &Sigset, 0);
174 set_option (OPTSIGNALSBLOCKED);
178 /* restore the previous signal mask */
179 void mutt_unblock_signals (void)
181 if (option (OPTSIGNALSBLOCKED))
183 sigprocmask (SIG_UNBLOCK, &Sigset, 0);
184 unset_option (OPTSIGNALSBLOCKED);
188 void mutt_block_signals_system (void)
192 if (! option (OPTSYSSIGNALSBLOCKED))
194 /* POSIX: ignore SIGINT and SIGQUIT & block SIGCHLD before exec */
195 sa.sa_handler = SIG_IGN;
197 sigemptyset (&sa.sa_mask);
198 sigaction (SIGINT, &sa, &SysOldInt);
199 sigaction (SIGQUIT, &sa, &SysOldQuit);
201 sigemptyset (&SigsetSys);
202 sigaddset (&SigsetSys, SIGCHLD);
203 sigprocmask (SIG_BLOCK, &SigsetSys, 0);
204 set_option (OPTSYSSIGNALSBLOCKED);
208 void mutt_unblock_signals_system (int catch)
210 if (option (OPTSYSSIGNALSBLOCKED))
212 sigprocmask (SIG_UNBLOCK, &SigsetSys, NULL);
215 sigaction (SIGQUIT, &SysOldQuit, NULL);
216 sigaction (SIGINT, &SysOldInt, NULL);
222 sa.sa_handler = SIG_DFL;
223 sigemptyset (&sa.sa_mask);
225 sigaction (SIGQUIT, &sa, NULL);
226 sigaction (SIGINT, &sa, NULL);
229 unset_option (OPTSYSSIGNALSBLOCKED);
233 void mutt_allow_interrupt (int disposition)
237 memset (&sa, 0, sizeof sa);
238 sa.sa_handler = sighandler;
240 if (disposition == 0)
241 sa.sa_flags |= SA_RESTART;
243 sigaction (SIGINT, &sa, NULL);