lib-signals.c revision 18c8a17e9cb8109b1aa3601cdb3b3fbb36adc79f
/* Copyright (c) 2001-2009 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "fd-close-on-exec.h"
#include "lib-signals.h"
#include <signal.h>
#include <unistd.h>
#define MAX_SIGNAL_VALUE 31
struct signal_handler {
void *context;
bool delayed;
struct signal_handler *next;
};
/* 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. */
static bool signals_initialized = FALSE;
{
/* common */
switch (sicode) {
case SI_USER:
return "kill";
#ifdef SI_KERNEL
case SI_KERNEL:
return "kernel";
#endif
case SI_TIMER:
return "timer";
}
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";
case BUS_ADRERR:
return "nonexistent physical address";
case BUS_OBJERR:
return "object-specific hardware error";
}
}
}
{
struct signal_handler *h;
bool delayed_sent = FALSE;
return;
/* remember that we're inside a signal handler which might have been
called at any time. don't do anything that's unsafe. */
if (!h->delayed)
else if (!delayed_sent) {
int saved_errno = errno;
i_error("write(sigpipe) failed: %m");
delayed_sent = TRUE;
errno = saved_errno;
}
}
}
void *context ATTR_UNUSED)
{
/* if we used SIG_IGN instead of this function,
the system call might be restarted */
}
{
int signo;
if (ret > 0) {
i_fatal("read(sigpipe) returned partial data");
/* get rid of duplicate signals */
for (i = 0; i < ret; i++) {
if (signo > MAX_SIGNAL_VALUE) {
i_panic("sigpipe contains signal %d > %d",
}
}
/* call the delayed handlers */
struct signal_handler *h =
if (h->delayed) {
h->context);
}
}
}
}
} else if (ret < 0) {
i_fatal("read(sigpipe) failed: %m");
} else {
i_fatal("read(sigpipe) failed: EOF");
}
}
{
i_fatal("sigemptyset(): %m");
}
{
struct signal_handler *h;
i_panic("Trying to set signal %d handler, but max is %d",
}
/* first delayed handler */
if (pipe(sig_pipe_fd) < 0)
i_fatal("pipe() failed: %m");
if (signals_initialized) {
signal_read, NULL);
}
}
/* atomically set to signal_handlers[] list */
signal_handlers[signo] = h;
}
{
i_panic("Trying to ignore signal %d, but max is %d",
}
i_fatal("sigemptyset(): %m");
if (restart_syscalls) {
} else {
}
}
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",
}
void lib_signals_reset_ioloop(void)
{
}
}
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)
lib_signals_set(i, FALSE);
}
if (sig_pipe_fd[0] != -1)
}
void lib_signals_deinit(void)
{
struct signal_handler *handlers, *h;
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");
}
}