/* Copyright (c) 2001-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "ioloop.h"
#include "write-full.h"
#include "lib-signals.h"
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#if !defined(SA_SIGINFO) && !defined(SI_NOINFO)
/* without SA_SIGINFO we don't know what the real code is. we need SI_NOINFO
to make sure lib_signal_code_to_str() returns "". */
#endif
struct signal_handler {
void *context;
};
volatile unsigned int signal_term_counter = 0;
/* Remember that these are accessed inside signal handler which may be called
even while we're initializing/deinitializing. Try hard to keep everything
in consistent state. */
{
/* common */
switch (sicode) {
#ifdef SI_NOINFO
case SI_NOINFO:
return "";
#endif
case SI_USER:
return "kill";
#ifdef SI_KERNEL
case SI_KERNEL:
return "kernel";
#endif
case SI_TIMER:
return "timer";
}
/* If SEGV_MAPERR is supported, the rest of them must be too.
FreeBSD 6 at least doesn't support these. */
#ifdef SEGV_MAPERR
switch (signo) {
case SIGSEGV:
switch (sicode) {
case SEGV_MAPERR:
return "address not mapped";
case SEGV_ACCERR:
return "invalid permissions";
}
break;
case SIGBUS:
switch (sicode) {
case BUS_ADRALN:
return "invalid address alignment";
#ifdef BUS_ADRERR /* for OSX 10.3 */
case BUS_ADRERR:
return "nonexistent physical address";
#endif
#ifdef BUS_OBJERR /* for OSX 10.3 */
case BUS_OBJERR:
return "object-specific hardware error";
#endif
}
}
#endif
}
#ifdef SA_SIGINFO
#else
static void sig_handler(int signo)
#endif
{
struct signal_handler *h;
int saved_errno;
char c = 0;
#if defined(SI_NOINFO) || !defined(SA_SIGINFO)
#ifndef SA_SIGINFO
#endif
/* Solaris can leave this to NULL */
}
#endif
return;
if (SIGNAL_IS_TERMINAL(signo))
/* remember that we're inside a signal handler which might have been
called at any time. don't do anything that's unsafe. we might also
get interrupted by another signal while inside this handler. */
saved_errno = errno;
if ((h->flags & LIBSIG_FLAG_DELAYED) == 0)
if (!have_pending_signals) {
lib_signals_syscall_error("signal: write(sigpipe) failed: ");
}
}
}
errno = saved_errno;
}
#ifdef SA_SIGINFO
void *context ATTR_UNUSED)
#else
#endif
{
/* if we used SIG_IGN instead of this function,
the system call might be restarted */
}
static void
signal_handle_shadowed(void)
{
unsigned int count, i;
if (!array_is_created(&pending_shadowed_signals) ||
array_count(&pending_shadowed_signals) == 0)
return;
for (i = 0; i < count; i++) {
struct signal_handler *h;
if ((h->flags & LIBSIG_FLAG_DELAYED) == 0 ||
(h->flags & LIBSIG_FLAG_NO_IOLOOP_AUTOMOVE) == 0)
continue;
continue;
}
/* handler can be called now */
}
if (!shadowed) {
/* no handlers are shadowed anymore; delete the signal info */
}
}
}
static void
signal_check_shadowed(void)
{
if (!array_is_created(&pending_shadowed_signals) ||
array_count(&pending_shadowed_signals) == 0)
return;
}
static void
{
unsigned int count, i;
/* remember last signal info for handlers that cannot run in
current ioloop */
for (i = 0; i < count; i++) {
break;
}
}
static void ATTR_NULL(1)
{
struct signal_handler *h;
int signo;
if (ioloop_switched) {
/* handle any delayed signal handlers that emerged from the shadow */
}
if (sigfillset(&fullset) < 0)
i_fatal("sigfillset() failed: %m");
i_fatal("sigprocmask() failed: %m");
/* typically we should read only a single byte, but if a signal
is sent while signal handler is running we might get more. */
if (ret > 0) {
} else if (ret < 0) {
i_fatal("read(sigpipe) failed: %m");
} else {
i_fatal("read(sigpipe) failed: EOF");
}
i_fatal("sigprocmask() failed: %m");
if (ret < 0)
return;
/* call the delayed handlers after signals are copied and unblocked */
continue;
if ((h->flags & LIBSIG_FLAG_DELAYED) == 0) {
/* handler already called immediately in signal context */
continue;
}
if ((h->flags & LIBSIG_FLAG_NO_IOLOOP_AUTOMOVE) != 0 &&
h->current_ioloop != current_ioloop) {
/* cannot run handler in current ioloop (shadowed) */
continue;
}
/* handler can be called now */
}
if (shadowed) {
/* remember last signal info for handlers that cannot run in
current ioloop (shadowed) */
}
}
}
static void
{
if (current_ioloop != NULL) {
signal_read, NULL);
}
}
static void
{
}
static void
{
/* check whether we can now handle any shadowed delayed signals */
}
void lib_signals_ioloop_detach(void)
{
if (!ioloop_attached) {
return;
}
}
void lib_signals_ioloop_attach(void)
{
if (ioloop_attached)
return;
}
{
i_fatal("sigemptyset(): %m");
#ifdef SA_SIGINFO
#else
#endif
if ((flags & LIBSIG_FLAG_RESTART) != 0)
}
{
struct signal_handler *h;
i_panic("Trying to set signal %d handler, but max is %d",
}
h->current_ioloop = current_ioloop;
/* atomically set to signal_handlers[] list */
signal_handlers[signo] = h;
/* first delayed handler */
if (pipe(sig_pipe_fd) < 0)
i_fatal("pipe() failed: %m");
if (signals_initialized)
}
}
{
i_panic("Trying to ignore signal %d, but max is %d",
}
i_fatal("sigemptyset(): %m");
if (restart_syscalls) {
} else {
#ifdef SA_SIGINFO
#else
#endif
}
}
void *context)
{
struct signal_handler *h, **p;
h = *p;
*p = h->next;
i_free(h);
return;
}
}
i_panic("lib_signals_unset_handler(%d, %p, %p): handler not found",
}
{
struct signal_handler *h;
h->current_ioloop = current_ioloop;
/* check whether we can now handle any shadowed delayed signals */
return;
}
}
i_panic("lib_signals_switch_ioloop(%d, %p, %p): handler not found",
}
{
/* @UNSAFE: We're in a signal handler. It's very limited what is
allowed in here. Especially strerror() isn't at least officially
allowed. */
/* can't really do anything */
}
}
void lib_signals_init(void)
{
int i;
/* add signals that were already registered */
for (i = 0; i < MAX_SIGNAL_VALUE; i++) {
if (signal_handlers[i] != NULL)
}
if (sig_pipe_fd[0] != -1)
}
void lib_signals_deinit(void)
{
int i;
for (i = 0; i < MAX_SIGNAL_VALUE; i++) {
if (signal_handlers[i] != NULL) {
/* atomically remove from signal_handlers[] list */
handlers = signal_handlers[i];
signal_handlers[i] = NULL;
h = handlers;
i_free(h);
}
}
}
if (sig_pipe_fd[0] != -1) {
if (close(sig_pipe_fd[0]) < 0)
i_error("close(sigpipe) failed: %m");
i_error("close(sigpipe) failed: %m");
}
}