2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-ui/curses.h>
14 #include "mutt_signal.h"
16 static sigset_t Sigset;
17 static sigset_t SigsetSys;
18 static struct sigaction SysOldInt;
19 static struct sigaction SysOldQuit;
20 static int IsEndwin = 0;
22 /* Attempt to catch "ordinary" signals and shut down gracefully. */
23 static RETSIGTYPE exit_handler (int sig)
26 endwin (); /* just to be safe */
27 #ifdef SYS_SIGLIST_DECLARED
28 printf (_("%s... Exiting.\n"), sys_siglist[sig]);
30 #if defined(__sun__) && defined(__svr4__)
31 printf (_("Caught %s... Exiting.\n"), _sys_siglist[sig]);
33 #if defined(__alpha) && defined(__osf__)
34 printf (_("Caught %s... Exiting.\n"), __sys_siglist[sig]);
36 printf (_("Caught signal %d... Exiting.\n"), sig);
43 static RETSIGTYPE chld_handler(int sig __attribute__((unused)))
48 static RETSIGTYPE sighandler (int sig)
50 int save_errno = errno;
53 case SIGTSTP: /* user requested a suspend */
54 if (!option (OPTSUSPEND))
56 IsEndwin = isendwin ();
66 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
67 /* We don't receive SIGWINCH when suspended; however, no harm is done by
68 * just assuming we received one, and triggering the 'resize' anyway. */
73 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
87 #ifdef USE_SLANG_CURSES
88 int mutt_intr_hook (void)
92 #endif /* USE_SLANG_CURSES */
94 void mutt_signal_initialize (void)
98 sigemptyset (&act.sa_mask);
100 act.sa_handler = SIG_IGN;
101 sigaction (SIGPIPE, &act, NULL);
103 act.sa_handler = exit_handler;
104 sigaction (SIGTERM, &act, NULL);
105 sigaction (SIGHUP, &act, NULL);
106 sigaction (SIGQUIT, &act, NULL);
108 /* we want to avoid race conditions */
109 sigaddset (&act.sa_mask, SIGTSTP);
111 act.sa_handler = sighandler;
113 /* we want SIGALRM to abort the current syscall, so we do this before
114 * setting the SA_RESTART flag below. currently this is only used to
115 * timeout on a connect() call in a reasonable amout of time.
117 sigaction (SIGALRM, &act, NULL);
119 /* we also don't want to mess with interrupted system calls */
121 act.sa_flags = SA_RESTART;
124 sigaction (SIGCONT, &act, NULL);
125 sigaction (SIGTSTP, &act, NULL);
126 sigaction (SIGINT, &act, NULL);
127 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
128 sigaction (SIGWINCH, &act, NULL);
131 /* POSIX doesn't allow us to ignore SIGCHLD,
132 * so we just install a dummy handler for it
134 act.sa_handler = chld_handler;
135 /* don't need to block any other signals here */
136 sigemptyset (&act.sa_mask);
137 /* we don't want to mess with stopped children */
138 act.sa_flags |= SA_NOCLDSTOP;
139 sigaction (SIGCHLD, &act, NULL);
141 #ifdef USE_SLANG_CURSES
142 /* This bit of code is required because of the implementation of
143 * SLcurses_wgetch(). If a signal is received (like SIGWINCH) when we
144 * are in blocking mode, SLsys_getkey() will not return an error unless
145 * a handler function is defined and it returns -1. This is needed so
146 * that if the user resizes the screen while at a prompt, it will just
147 * abort and go back to the main-menu.
149 SLang_getkey_intr_hook = mutt_intr_hook;
153 /* signals which are important to block while doing critical ops */
154 void mutt_block_signals (void)
156 if (!option (OPTSIGNALSBLOCKED)) {
157 sigemptyset (&Sigset);
158 sigaddset (&Sigset, SIGTERM);
159 sigaddset (&Sigset, SIGHUP);
160 sigaddset (&Sigset, SIGTSTP);
161 sigaddset (&Sigset, SIGINT);
162 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
163 sigaddset (&Sigset, SIGWINCH);
165 sigprocmask (SIG_BLOCK, &Sigset, 0);
166 set_option (OPTSIGNALSBLOCKED);
170 /* restore the previous signal mask */
171 void mutt_unblock_signals (void)
173 if (option (OPTSIGNALSBLOCKED)) {
174 sigprocmask (SIG_UNBLOCK, &Sigset, 0);
175 unset_option (OPTSIGNALSBLOCKED);
179 void mutt_block_signals_system (void)
183 if (!option (OPTSYSSIGNALSBLOCKED)) {
184 /* POSIX: ignore SIGINT and SIGQUIT & block SIGCHLD before exec */
185 sa.sa_handler = SIG_IGN;
187 sigemptyset (&sa.sa_mask);
188 sigaction (SIGINT, &sa, &SysOldInt);
189 sigaction (SIGQUIT, &sa, &SysOldQuit);
191 sigemptyset (&SigsetSys);
192 sigaddset (&SigsetSys, SIGCHLD);
193 sigprocmask (SIG_BLOCK, &SigsetSys, 0);
194 set_option (OPTSYSSIGNALSBLOCKED);
198 void mutt_unblock_signals_system (int catch)
200 if (option (OPTSYSSIGNALSBLOCKED)) {
201 sigprocmask (SIG_UNBLOCK, &SigsetSys, NULL);
203 sigaction (SIGQUIT, &SysOldQuit, NULL);
204 sigaction (SIGINT, &SysOldInt, NULL);
209 sa.sa_handler = SIG_DFL;
210 sigemptyset (&sa.sa_mask);
212 sigaction (SIGQUIT, &sa, NULL);
213 sigaction (SIGINT, &sa, NULL);
216 unset_option (OPTSYSSIGNALSBLOCKED);
220 void mutt_allow_interrupt (int disposition)
225 sa.sa_handler = sighandler;
227 if (disposition == 0)
228 sa.sa_flags |= SA_RESTART;
230 sigaction (SIGINT, &sa, NULL);