Use libevent to handle HUP signal.
This commit is contained in:
parent
4d0621b1f3
commit
294ce72441
3 changed files with 52 additions and 55 deletions
94
src/net.c
94
src/net.c
|
@ -294,6 +294,50 @@ static void dummy(int a, short b, void *c)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sighup_handler(int signal, short events, void *data) {
|
||||||
|
connection_t *c;
|
||||||
|
avl_node_t *node;
|
||||||
|
char *fname;
|
||||||
|
struct stat s;
|
||||||
|
static time_t last_config_check = 0;
|
||||||
|
|
||||||
|
/* Reread our own configuration file */
|
||||||
|
|
||||||
|
exit_configuration(&config_tree);
|
||||||
|
init_configuration(&config_tree);
|
||||||
|
|
||||||
|
if(!read_server_config()) {
|
||||||
|
logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
|
||||||
|
event_loopexit(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Close connections to hosts that have a changed or deleted host config file */
|
||||||
|
|
||||||
|
for(node = connection_tree->head; node; node = node->next) {
|
||||||
|
c = node->data;
|
||||||
|
|
||||||
|
if(c->outgoing) {
|
||||||
|
free(c->outgoing->name);
|
||||||
|
if(c->outgoing->ai)
|
||||||
|
freeaddrinfo(c->outgoing->ai);
|
||||||
|
free(c->outgoing);
|
||||||
|
c->outgoing = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
asprintf(&fname, "%s/hosts/%s", confbase, c->name);
|
||||||
|
if(stat(fname, &s) || s.st_mtime > last_config_check)
|
||||||
|
terminate_connection(c, c->status.active);
|
||||||
|
free(fname);
|
||||||
|
}
|
||||||
|
|
||||||
|
last_config_check = time(NULL);
|
||||||
|
|
||||||
|
/* Try to make outgoing connections */
|
||||||
|
|
||||||
|
try_outgoing_connections();
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
this is where it all happens...
|
this is where it all happens...
|
||||||
*/
|
*/
|
||||||
|
@ -301,14 +345,17 @@ int main_loop(void)
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int r;
|
int r;
|
||||||
time_t last_ping_check, last_config_check;
|
time_t last_ping_check;
|
||||||
tevent_t *event;
|
tevent_t *event;
|
||||||
struct event timeout;
|
struct event timeout;
|
||||||
|
struct event sighup_event;
|
||||||
|
|
||||||
cp();
|
cp();
|
||||||
|
|
||||||
|
signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
|
||||||
|
signal_add(&sighup_event, NULL);
|
||||||
|
|
||||||
last_ping_check = now;
|
last_ping_check = now;
|
||||||
last_config_check = now;
|
|
||||||
|
|
||||||
srand(now);
|
srand(now);
|
||||||
|
|
||||||
|
@ -387,50 +434,9 @@ int main_loop(void)
|
||||||
sigalrm = false;
|
sigalrm = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sighup) {
|
|
||||||
connection_t *c;
|
|
||||||
avl_node_t *node;
|
|
||||||
char *fname;
|
|
||||||
struct stat s;
|
|
||||||
|
|
||||||
sighup = false;
|
|
||||||
|
|
||||||
/* Reread our own configuration file */
|
|
||||||
|
|
||||||
exit_configuration(&config_tree);
|
|
||||||
init_configuration(&config_tree);
|
|
||||||
|
|
||||||
if(!read_server_config()) {
|
|
||||||
logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close connections to hosts that have a changed or deleted host config file */
|
signal_del(&sighup_event);
|
||||||
|
|
||||||
for(node = connection_tree->head; node; node = node->next) {
|
|
||||||
c = node->data;
|
|
||||||
|
|
||||||
if(c->outgoing) {
|
|
||||||
free(c->outgoing->name);
|
|
||||||
if(c->outgoing->ai)
|
|
||||||
freeaddrinfo(c->outgoing->ai);
|
|
||||||
free(c->outgoing);
|
|
||||||
c->outgoing = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
asprintf(&fname, "%s/hosts/%s", confbase, c->name);
|
|
||||||
if(stat(fname, &s) || s.st_mtime > last_config_check)
|
|
||||||
terminate_connection(c, c->status.active);
|
|
||||||
free(fname);
|
|
||||||
}
|
|
||||||
|
|
||||||
last_config_check = now;
|
|
||||||
|
|
||||||
/* Try to make outgoing connections */
|
|
||||||
|
|
||||||
try_outgoing_connections();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@
|
||||||
|
|
||||||
/* If zero, don't detach from the terminal. */
|
/* If zero, don't detach from the terminal. */
|
||||||
bool do_detach = true;
|
bool do_detach = true;
|
||||||
bool sighup = false;
|
|
||||||
bool sigalrm = false;
|
bool sigalrm = false;
|
||||||
|
|
||||||
extern char *identname;
|
extern char *identname;
|
||||||
|
@ -491,12 +490,6 @@ static RETSIGTYPE fatal_signal_handler(int a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static RETSIGTYPE sighup_handler(int a)
|
|
||||||
{
|
|
||||||
logger(LOG_NOTICE, _("Got %s signal"), "HUP");
|
|
||||||
sighup = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static RETSIGTYPE sigint_handler(int a)
|
static RETSIGTYPE sigint_handler(int a)
|
||||||
{
|
{
|
||||||
logger(LOG_NOTICE, _("Got %s signal"), "INT");
|
logger(LOG_NOTICE, _("Got %s signal"), "INT");
|
||||||
|
@ -554,7 +547,6 @@ static struct {
|
||||||
int signal;
|
int signal;
|
||||||
void (*handler)(int);
|
void (*handler)(int);
|
||||||
} sighandlers[] = {
|
} sighandlers[] = {
|
||||||
{SIGHUP, sighup_handler},
|
|
||||||
{SIGTERM, sigterm_handler},
|
{SIGTERM, sigterm_handler},
|
||||||
{SIGQUIT, sigquit_handler},
|
{SIGQUIT, sigquit_handler},
|
||||||
{SIGSEGV, fatal_signal_handler},
|
{SIGSEGV, fatal_signal_handler},
|
||||||
|
@ -594,7 +586,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[3].handler = SIG_DFL;
|
sighandlers[2].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. */
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
#define __TINC_PROCESS_H__
|
#define __TINC_PROCESS_H__
|
||||||
|
|
||||||
extern bool do_detach;
|
extern bool do_detach;
|
||||||
extern bool sighup;
|
|
||||||
extern bool sigalrm;
|
extern bool sigalrm;
|
||||||
|
|
||||||
extern void setup_signals(void);
|
extern void setup_signals(void);
|
||||||
|
|
Loading…
Reference in a new issue