1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Copyright (C) 1996-2000,2012 Michael R. Elkins <me@mutt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "mutt.h"
#include "mutt_curses.h"
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
static sigset_t Sigset;
static sigset_t SigsetSys;
static struct sigaction SysOldInt;
static struct sigaction SysOldQuit;
static int IsEndwin = 0;
static char *Caught_Signal_L10N = NULL;
static char *Exiting_L10N = NULL;
static void exit_print_int_recursive (int n)
{
char digit;
if (n > 9)
exit_print_int_recursive (n / 10);
digit = '0' + (n % 10);
write (1, &digit, 1);
}
static void exit_print_int (int n)
{
if (n < 0)
{
write (1, "-", 1);
n = -n;
}
exit_print_int_recursive (n);
}
static void exit_print_string (const char *str)
{
size_t len = 0;
if (!str)
return;
while (str[len])
len++;
if (len > 0)
write (1, str, len);
}
/* Attempt to catch "ordinary" signals and shut down gracefully.
* Do not do l10n translations as _() can invoke malloc(), (__MEM_CHECKED__)
* which may not be re-entrant. */
static void exit_handler (int sig)
{
curs_set (1);
endwin (); /* just to be safe */
exit_print_string (Caught_Signal_L10N ? Caught_Signal_L10N : "Caught signal ");
#if SYS_SIGLIST_DECLARED
exit_print_string (sys_siglist[sig]);
#else
#if (__sun__ && __svr4__)
exit_print_string (_sys_siglist[sig]);
#else
#if (__alpha && __osf__)
exit_print_string (__sys_siglist[sig]);
#else
exit_print_int (sig);
#endif
#endif
#endif
exit_print_string (Exiting_L10N ? Exiting_L10N : "... Exiting.\n");
exit (0);
}
/* These are gettext() translated in advance because that
* is not guaranteed reentrant.
*/
static void exit_handler_l10n_init (void)
{
if (!Caught_Signal_L10N)
{
/* L10N:
This is printed in the exit handler when a signal is caught.
The whole string is "Caught signal [XXX]... Exiting\n".
This is the first part of the string: note with a trailing space.
*/
Caught_Signal_L10N = safe_strdup (_("Caught signal "));
}
if (!Exiting_L10N)
{
/* L10N:
This is printed in the exit handler when a signal is caught.
The whole string is "Caught signal [XXX]... Exiting\n".
This is the second part of the string, printed after the
signal number or name.
*/
Exiting_L10N = safe_strdup (_("... Exiting.\n"));
}
}
static void exit_handler_l10n_cleanup (void)
{
FREE (&Caught_Signal_L10N);
FREE (&Exiting_L10N);
}
static void chld_handler (int sig)
{
SigChld = 1;
}
static void sighandler (int sig)
{
int save_errno = errno;
switch (sig)
{
case SIGTSTP: /* user requested a suspend */
if (!option (OPTSUSPEND))
break;
IsEndwin = isendwin ();
curs_set (1);
if (!IsEndwin)
endwin ();
kill (0, SIGSTOP);
/* fall through */
case SIGCONT:
if (!IsEndwin)
refresh ();
mutt_curs_set (-1);
#if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
/* We don't receive SIGWINCH when suspended; however, no harm is done by
* just assuming we received one, and triggering the 'resize' anyway. */
SigWinch = 1;
#endif
break;
#if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
case SIGWINCH:
SigWinch = 1;
break;
#endif
case SIGINT:
SigInt = 1;
break;
}
errno = save_errno;
}
#ifdef USE_SLANG_CURSES
int mutt_intr_hook (void)
{
return (-1);
}
#endif /* USE_SLANG_CURSES */
void mutt_signal_init (void)
{
struct sigaction act;
exit_handler_l10n_init ();
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = SIG_IGN;
sigaction (SIGPIPE, &act, NULL);
act.sa_handler = exit_handler;
sigaction (SIGTERM, &act, NULL);
sigaction (SIGHUP, &act, NULL);
sigaction (SIGQUIT, &act, NULL);
/* we want to avoid race conditions */
sigaddset (&act.sa_mask, SIGTSTP);
act.sa_handler = sighandler;
/* we want SIGALRM to abort the current syscall, so we do this before
* setting the SA_RESTART flag below. currently this is only used to
* timeout on a connect() call in a reasonable amount of time.
*/
sigaction (SIGALRM, &act, NULL);
/* we also don't want to mess with interrupted system calls */
#ifdef SA_RESTART
act.sa_flags = SA_RESTART;
#endif
sigaction (SIGCONT, &act, NULL);
sigaction (SIGTSTP, &act, NULL);
sigaction (SIGINT, &act, NULL);
#if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
sigaction (SIGWINCH, &act, NULL);
#endif
/* Handle SIGCHLD. Tracked for background editing processes. */
act.sa_handler = chld_handler;
/* don't need to block any other signals here */
sigemptyset (&act.sa_mask);
/* we don't want to mess with stopped children */
act.sa_flags |= SA_NOCLDSTOP;
sigaction (SIGCHLD, &act, NULL);
#ifdef USE_SLANG_CURSES
/* This bit of code is required because of the implementation of
* SLcurses_wgetch(). If a signal is received (like SIGWINCH) when we
* are in blocking mode, SLsys_getkey() will not return an error unless
* a handler function is defined and it returns -1. This is needed so
* that if the user resizes the screen while at a prompt, it will just
* abort and go back to the main-menu.
*/
SLang_getkey_intr_hook = mutt_intr_hook;
#endif
}
void mutt_signal_cleanup (void)
{
exit_handler_l10n_cleanup ();
}
/* signals which are important to block while doing critical ops */
void mutt_block_signals (void)
{
if (!option (OPTSIGNALSBLOCKED))
{
sigemptyset (&Sigset);
sigaddset (&Sigset, SIGTERM);
sigaddset (&Sigset, SIGHUP);
sigaddset (&Sigset, SIGTSTP);
sigaddset (&Sigset, SIGINT);
#if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
sigaddset (&Sigset, SIGWINCH);
#endif
sigprocmask (SIG_BLOCK, &Sigset, 0);
set_option (OPTSIGNALSBLOCKED);
}
}
/* restore the previous signal mask */
void mutt_unblock_signals (void)
{
if (option (OPTSIGNALSBLOCKED))
{
sigprocmask (SIG_UNBLOCK, &Sigset, 0);
unset_option (OPTSIGNALSBLOCKED);
}
}
void mutt_block_signals_system (void)
{
struct sigaction sa;
if (! option (OPTSYSSIGNALSBLOCKED))
{
/* POSIX: ignore SIGINT and SIGQUIT & block SIGCHLD before exec */
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
sigaction (SIGINT, &sa, &SysOldInt);
sigaction (SIGQUIT, &sa, &SysOldQuit);
sigemptyset (&SigsetSys);
sigaddset (&SigsetSys, SIGCHLD);
sigprocmask (SIG_BLOCK, &SigsetSys, 0);
set_option (OPTSYSSIGNALSBLOCKED);
}
}
void mutt_unblock_signals_system (int catch)
{
if (option (OPTSYSSIGNALSBLOCKED))
{
sigprocmask (SIG_UNBLOCK, &SigsetSys, NULL);
if (catch)
{
sigaction (SIGQUIT, &SysOldQuit, NULL);
sigaction (SIGINT, &SysOldInt, NULL);
}
else
{
struct sigaction sa;
sa.sa_handler = SIG_DFL;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sigaction (SIGQUIT, &sa, NULL);
sigaction (SIGINT, &sa, NULL);
}
unset_option (OPTSYSSIGNALSBLOCKED);
}
}
/* Resets ignored signals back to the default.
*
* See sigaction(2):
* A child created via fork(2) inherits a copy of its parent's
* signal dispositions. During an execve(2), the dispositions of
* handled signals are reset to the default; the dispositions of
* ignored signals are left unchanged.
*/
void mutt_reset_child_signals (void)
{
struct sigaction act;
act.sa_handler = SIG_DFL;
act.sa_flags = 0;
sigemptyset (&act.sa_mask);
/* These signals are set to SIG_IGN and must be reset */
sigaction (SIGPIPE, &act, NULL);
/* These technically don't need to be reset, but the code has been
* doing so for a long time. */
sigaction (SIGTERM, &act, NULL);
sigaction (SIGTSTP, &act, NULL);
sigaction (SIGCONT, &act, NULL);
}
void mutt_allow_interrupt (int disposition)
{
struct sigaction sa;
memset (&sa, 0, sizeof sa);
sa.sa_handler = sighandler;
#ifdef SA_RESTART
if (disposition == 0)
sa.sa_flags |= SA_RESTART;
#endif
sigaction (SIGINT, &sa, NULL);
}
|