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/lib-ui.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 void exit_handler (int sig)
26 endwin(); /* just to be safe */
27 printf(_("Caught %s... Exiting.\n"), strsignal(sig));
31 static void chld_handler(int sig __attribute__((unused)))
36 static void sighandler (int sig)
38 int save_errno = errno;
41 case SIGTSTP: /* user requested a suspend */
42 IsEndwin = isendwin ();
52 /* We don't receive SIGWINCH when suspended; however, no harm is done by
53 * just assuming we received one, and triggering the 'resize' anyway. */
65 void mutt_signal_initialize (void)
69 sigemptyset (&act.sa_mask);
71 act.sa_handler = SIG_IGN;
72 sigaction (SIGPIPE, &act, NULL);
74 act.sa_handler = exit_handler;
75 sigaction (SIGTERM, &act, NULL);
76 sigaction (SIGHUP, &act, NULL);
77 sigaction (SIGQUIT, &act, NULL);
79 /* we want to avoid race conditions */
80 sigaddset (&act.sa_mask, SIGTSTP);
82 act.sa_handler = sighandler;
84 /* we want SIGALRM to abort the current syscall, so we do this before
85 * setting the SA_RESTART flag below. currently this is only used to
86 * timeout on a connect() call in a reasonable amout of time.
88 sigaction (SIGALRM, &act, NULL);
90 /* we also don't want to mess with interrupted system calls */
92 act.sa_flags = SA_RESTART;
95 sigaction (SIGCONT, &act, NULL);
96 sigaction (SIGTSTP, &act, NULL);
97 sigaction (SIGINT, &act, NULL);
98 sigaction (SIGWINCH, &act, NULL);
100 /* POSIX doesn't allow us to ignore SIGCHLD,
101 * so we just install a dummy handler for it
103 act.sa_handler = chld_handler;
104 /* don't need to block any other signals here */
105 sigemptyset (&act.sa_mask);
106 /* we don't want to mess with stopped children */
107 act.sa_flags |= SA_NOCLDSTOP;
108 sigaction (SIGCHLD, &act, NULL);
111 /* signals which are important to block while doing critical ops */
112 void mutt_block_signals (void)
114 if (!option (OPTSIGNALSBLOCKED)) {
115 sigemptyset (&Sigset);
116 sigaddset (&Sigset, SIGTERM);
117 sigaddset (&Sigset, SIGHUP);
118 sigaddset (&Sigset, SIGTSTP);
119 sigaddset (&Sigset, SIGINT);
120 sigaddset (&Sigset, SIGWINCH);
121 sigprocmask (SIG_BLOCK, &Sigset, 0);
122 set_option (OPTSIGNALSBLOCKED);
126 /* restore the previous signal mask */
127 void mutt_unblock_signals (void)
129 if (option (OPTSIGNALSBLOCKED)) {
130 sigprocmask (SIG_UNBLOCK, &Sigset, 0);
131 unset_option (OPTSIGNALSBLOCKED);
135 void mutt_block_signals_system (void)
139 if (!option (OPTSYSSIGNALSBLOCKED)) {
140 /* POSIX: ignore SIGINT and SIGQUIT & block SIGCHLD before exec */
141 sa.sa_handler = SIG_IGN;
143 sigemptyset (&sa.sa_mask);
144 sigaction (SIGINT, &sa, &SysOldInt);
145 sigaction (SIGQUIT, &sa, &SysOldQuit);
147 sigemptyset (&SigsetSys);
148 sigaddset (&SigsetSys, SIGCHLD);
149 sigprocmask (SIG_BLOCK, &SigsetSys, 0);
150 set_option (OPTSYSSIGNALSBLOCKED);
154 void mutt_unblock_signals_system (int catch)
156 if (option (OPTSYSSIGNALSBLOCKED)) {
157 sigprocmask (SIG_UNBLOCK, &SigsetSys, NULL);
159 sigaction (SIGQUIT, &SysOldQuit, NULL);
160 sigaction (SIGINT, &SysOldInt, NULL);
165 sa.sa_handler = SIG_DFL;
166 sigemptyset (&sa.sa_mask);
168 sigaction (SIGQUIT, &sa, NULL);
169 sigaction (SIGINT, &sa, NULL);
172 unset_option (OPTSYSSIGNALSBLOCKED);
176 void mutt_allow_interrupt (int disposition)
181 sa.sa_handler = sighandler;
183 if (disposition == 0)
184 sa.sa_flags |= SA_RESTART;
186 sigaction (SIGINT, &sa, NULL);