Use libevent to handle all non-fatal signals.

This commit is contained in:
Guus Sliepen 2007-05-17 22:41:34 +00:00
parent 531d5a904a
commit 6d19ebd612
2 changed files with 71 additions and 75 deletions

View file

@ -40,7 +40,6 @@
#include "subnet.h" #include "subnet.h"
#include "xalloc.h" #include "xalloc.h"
bool do_purge = false;
volatile bool running = false; volatile bool running = false;
time_t now = 0; time_t now = 0;
@ -293,13 +292,58 @@ static void dummy(int a, short b, void *c)
{ {
} }
void sighup_handler(int signal, short events, void *data) { static void sigterm_handler(int signal, short events, void *data) {
logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
running = false;
event_loopexit(NULL);
}
static void sigint_handler(int signal, short events, void *data) {
static int saved_debug_level = -1;
logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
if(saved_debug_level != -1) {
logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
saved_debug_level);
debug_level = saved_debug_level;
saved_debug_level = -1;
} else {
logger(LOG_NOTICE,
_("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
debug_level);
saved_debug_level = debug_level;
debug_level = 5;
}
}
static void sigusr1_handler(int signal, short events, void *data) {
logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
dump_connections();
}
static void sigusr2_handler(int signal, short events, void *data) {
logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
dump_device_stats();
dump_nodes();
dump_edges();
dump_subnets();
}
static void sigwinch_handler(int signal, short events, void *data) {
logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
purge();
}
static void sighup_handler(int signal, short events, void *data) {
connection_t *c; connection_t *c;
avl_node_t *node; avl_node_t *node;
char *fname; char *fname;
struct stat s; struct stat s;
static time_t last_config_check = 0; static time_t last_config_check = 0;
logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
/* Reread our own configuration file */ /* Reread our own configuration file */
exit_configuration(&config_tree); exit_configuration(&config_tree);
@ -347,11 +391,29 @@ int main_loop(void)
time_t last_ping_check; time_t last_ping_check;
struct event timeout; struct event timeout;
struct event sighup_event; struct event sighup_event;
struct event sigint_event;
struct event sigterm_event;
struct event sigquit_event;
struct event sigusr1_event;
struct event sigusr2_event;
struct event sigwinch_event;
cp(); cp();
signal_set(&sighup_event, SIGHUP, sighup_handler, NULL); signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
signal_add(&sighup_event, NULL); signal_add(&sighup_event, NULL);
signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
signal_add(&sigint_event, NULL);
signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
signal_add(&sigterm_event, NULL);
signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
signal_add(&sigquit_event, NULL);
signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
signal_add(&sigusr1_event, NULL);
signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
signal_add(&sigusr2_event, NULL);
signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
signal_add(&sigwinch_event, NULL);
last_ping_check = now; last_ping_check = now;
@ -391,11 +453,6 @@ int main_loop(void)
/* XXX: more libevent transition */ /* XXX: more libevent transition */
timeout_del(&timeout); timeout_del(&timeout);
if(do_purge) {
purge();
do_purge = false;
}
/* Let's check if everybody is still alive */ /* Let's check if everybody is still alive */
if(last_ping_check + pingtimeout < now) { if(last_ping_check + pingtimeout < now) {
@ -428,6 +485,12 @@ int main_loop(void)
} }
signal_del(&sighup_event); signal_del(&sighup_event);
signal_del(&sigint_event);
signal_del(&sigterm_event);
signal_del(&sigquit_event);
signal_del(&sigusr1_event);
signal_del(&sigusr2_event);
signal_del(&sigwinch_event);
return 0; return 0;
} }

View file

@ -440,24 +440,6 @@ bool execute_script(const char *name, char **envp)
*/ */
#ifndef HAVE_MINGW #ifndef HAVE_MINGW
static RETSIGTYPE sigterm_handler(int a)
{
logger(LOG_NOTICE, _("Got %s signal"), "TERM");
if(running)
running = false;
else
exit(1);
}
static RETSIGTYPE sigquit_handler(int a)
{
logger(LOG_NOTICE, _("Got %s signal"), "QUIT");
if(running)
running = false;
else
exit(1);
}
static RETSIGTYPE fatal_signal_square(int a) static RETSIGTYPE fatal_signal_square(int a)
{ {
logger(LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a, logger(LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a,
@ -490,48 +472,6 @@ static RETSIGTYPE fatal_signal_handler(int a)
} }
} }
static RETSIGTYPE sigint_handler(int a)
{
logger(LOG_NOTICE, _("Got %s signal"), "INT");
if(saved_debug_level != -1) {
logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
saved_debug_level);
debug_level = saved_debug_level;
saved_debug_level = -1;
} else {
logger(LOG_NOTICE,
_("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
debug_level);
saved_debug_level = debug_level;
debug_level = 5;
}
}
static RETSIGTYPE sigalrm_handler(int a)
{
logger(LOG_NOTICE, _("Got %s signal"), "ALRM");
sigalrm = true;
}
static RETSIGTYPE sigusr1_handler(int a)
{
dump_connections();
}
static RETSIGTYPE sigusr2_handler(int a)
{
dump_device_stats();
dump_nodes();
dump_edges();
dump_subnets();
}
static RETSIGTYPE sigwinch_handler(int a)
{
do_purge = true;
}
static RETSIGTYPE unexpected_signal_handler(int a) static RETSIGTYPE unexpected_signal_handler(int a)
{ {
logger(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a)); logger(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
@ -547,18 +487,11 @@ static struct {
int signal; int signal;
void (*handler)(int); void (*handler)(int);
} sighandlers[] = { } sighandlers[] = {
{SIGTERM, sigterm_handler},
{SIGQUIT, sigquit_handler},
{SIGSEGV, fatal_signal_handler}, {SIGSEGV, fatal_signal_handler},
{SIGBUS, fatal_signal_handler}, {SIGBUS, fatal_signal_handler},
{SIGILL, fatal_signal_handler}, {SIGILL, fatal_signal_handler},
{SIGPIPE, ignore_signal_handler}, {SIGPIPE, ignore_signal_handler},
{SIGINT, sigint_handler},
{SIGUSR1, sigusr1_handler},
{SIGUSR2, sigusr2_handler},
{SIGCHLD, ignore_signal_handler}, {SIGCHLD, ignore_signal_handler},
{SIGALRM, sigalrm_handler},
{SIGWINCH, sigwinch_handler},
{0, NULL} {0, NULL}
}; };
#endif #endif
@ -586,7 +519,7 @@ void setup_signals(void)
/* If we didn't detach, allow coredumps */ /* If we didn't detach, allow coredumps */
if(!do_detach) if(!do_detach)
sighandlers[2].handler = SIG_DFL; sighandlers[0].handler = SIG_DFL;
/* Then, for each known signal that we want to catch, assign a /* Then, for each known signal that we want to catch, assign a
handler to the signal, with error checking this time. */ handler to the signal, with error checking this time. */