From dc186d6d2b0dd4ad23ca5fc69c00e81f796ff6d9 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Mon, 27 Jul 2015 15:39:11 +0200 Subject: [PATCH] odhcp6c: sync and accumulate RA & DHCPv6 events correctly --- src/odhcp6c.c | 40 +++++++++++-------- src/odhcp6c.h | 2 +- src/script.c | 105 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 93 insertions(+), 54 deletions(-) diff --git a/src/odhcp6c.c b/src/odhcp6c.c index a71218c..133ade7 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -25,7 +25,6 @@ #include #include -#include #include #include @@ -46,10 +45,12 @@ static volatile bool signal_usr2 = false; static volatile bool signal_term = false; static int urandom_fd = -1, allow_slaac_only = 0; -static bool bound = false, release = true; +static bool bound = false, release = true, ra = false; static time_t last_update = 0; static unsigned int min_update_interval = DEFAULT_MIN_UPDATE_INTERVAL; +static unsigned int script_sync_delay = 10; +static unsigned int script_accu_delay = 1; int main(_unused int argc, char* const argv[]) { @@ -222,6 +223,9 @@ int main(_unused int argc, char* const argv[]) } } + if (allow_slaac_only > 0) + script_sync_delay = allow_slaac_only; + openlog("odhcp6c", logopt, LOG_DAEMON); if (!verbosity) setlogmask(LOG_UPTO(LOG_WARNING)); @@ -234,7 +238,6 @@ int main(_unused int argc, char* const argv[]) signal(SIGIO, sighandler); signal(SIGHUP, sighandler); signal(SIGINT, sighandler); - signal(SIGCHLD, sighandler); signal(SIGTERM, sighandler); signal(SIGUSR1, sighandler); signal(SIGUSR2, sighandler); @@ -266,7 +269,7 @@ int main(_unused int argc, char* const argv[]) } } - script_call("started"); + script_call("started", 0, false); while (!signal_term) { // Main logic odhcp6c_clear_state(STATE_SERVER_ID); @@ -316,7 +319,7 @@ int main(_unused int argc, char* const argv[]) while (!signal_usr2 && !signal_term) { signal_usr1 = false; - script_call("informed"); + script_call("informed", script_sync_delay, true); int res = dhcpv6_poll_reconfigure(); odhcp6c_signal_process(); @@ -342,7 +345,7 @@ int main(_unused int argc, char* const argv[]) case DHCPV6_STATEFUL: bound = true; - script_call("bound"); + script_call("bound", script_sync_delay, true); syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname); while (!signal_usr2 && !signal_term) { @@ -351,7 +354,7 @@ int main(_unused int argc, char* const argv[]) int res = dhcpv6_poll_reconfigure(); odhcp6c_signal_process(); if (res > 0) { - script_call("updated"); + script_call("updated", 0, false); continue; } @@ -366,7 +369,7 @@ int main(_unused int argc, char* const argv[]) odhcp6c_signal_process(); if (res > 0) { // Renew was succesfull // Publish updates - script_call("updated"); + script_call("updated", 0, false); continue; // Renew was successful } @@ -385,7 +388,7 @@ int main(_unused int argc, char* const argv[]) odhcp6c_signal_process(); if (res > 0) - script_call("rebound"); + script_call("rebound", 0, true); else { break; } @@ -405,7 +408,7 @@ int main(_unused int argc, char* const argv[]) // Add all prefixes to lost prefixes bound = false; - script_call("unbound"); + script_call("unbound", 0, true); if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release) dhcpv6_request(DHCPV6_MSG_RELEASE); @@ -414,7 +417,7 @@ int main(_unused int argc, char* const argv[]) odhcp6c_clear_state(STATE_IA_PD); } - script_call("stopped"); + script_call("stopped", 0, true); return 0; } @@ -484,11 +487,16 @@ bool odhcp6c_signal_process(void) bool ra_updated = ra_process(); - if (ra_link_up()) + if (ra_link_up()) { signal_usr2 = true; + ra = false; + } - if (ra_updated && (bound || allow_slaac_only >= 0)) - script_call("ra-updated"); // Immediate process urgent events + if (ra_updated && (bound || allow_slaac_only >= 0)) { + script_call("ra-updated", (!ra && !bound) ? + script_sync_delay : script_accu_delay, false); + ra = true; + } } return signal_usr1 || signal_usr2 || signal_term; @@ -669,9 +677,7 @@ bool odhcp6c_is_bound(void) static void sighandler(int signal) { - if (signal == SIGCHLD) - while (waitpid(-1, NULL, WNOHANG) > 0); - else if (signal == SIGUSR1) + if (signal == SIGUSR1) signal_usr1 = true; else if (signal == SIGUSR2) signal_usr2 = true; diff --git a/src/odhcp6c.h b/src/odhcp6c.h index c9a7398..d202790 100644 --- a/src/odhcp6c.h +++ b/src/odhcp6c.h @@ -324,7 +324,7 @@ int ra_conf_retransmit(int newvalue); int script_init(const char *path, const char *ifname); ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src); -void script_call(const char *status); +void script_call(const char *status, int delay, bool resume); bool odhcp6c_signal_process(void); uint64_t odhcp6c_get_milli_time(void); diff --git a/src/script.c b/src/script.c index dbdd89b..3d313cf 100644 --- a/src/script.c +++ b/src/script.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "odhcp6c.h" @@ -39,14 +40,27 @@ static const int8_t hexvals[] = { }; +static char action[16] = ""; +static char *argv[4] = {NULL, NULL, action, NULL}; +static volatile pid_t running = 0; +static time_t started; -static char *argv[4] = {NULL, NULL, NULL, NULL}; +static void script_sighandle(int signal) +{ + if (signal == SIGCHLD) { + pid_t child; + while ((child = waitpid(-1, NULL, WNOHANG)) > 0) + if (running == child) + running = 0; + } +} int script_init(const char *path, const char *ifname) { argv[0] = (char*)path; argv[1] = (char*)ifname; + signal(SIGCHLD, script_sighandle); return 0; } @@ -336,41 +350,61 @@ static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len } -void script_call(const char *status) +void script_call(const char *status, int delay, bool resume) { - size_t dns_len, search_len, custom_len, sntp_ip_len, ntp_ip_len, ntp_dns_len; - size_t sip_ip_len, sip_fqdn_len, aftr_name_len, cer_len, addr_len; - size_t s46_mapt_len, s46_mape_len, s46_lw_len, passthru_len; - - odhcp6c_expire(); - - struct in6_addr *addr = odhcp6c_get_state(STATE_SERVER_ADDR, &addr_len); - struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len); - uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len); - uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len); - struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len); - struct in6_addr *ntp = odhcp6c_get_state(STATE_NTP_IP, &ntp_ip_len); - uint8_t *ntp_dns = odhcp6c_get_state(STATE_NTP_FQDN, &ntp_dns_len); - struct in6_addr *sip = odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len); - uint8_t *sip_fqdn = odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len); - uint8_t *aftr_name = odhcp6c_get_state(STATE_AFTR_NAME, &aftr_name_len); - struct in6_addr *cer = odhcp6c_get_state(STATE_CER, &cer_len); - uint8_t *s46_mapt = odhcp6c_get_state(STATE_S46_MAPT, &s46_mapt_len); - uint8_t *s46_mape = odhcp6c_get_state(STATE_S46_MAPE, &s46_mape_len); - uint8_t *s46_lw = odhcp6c_get_state(STATE_S46_LW, &s46_lw_len); - uint8_t *passthru = odhcp6c_get_state(STATE_PASSTHRU, &passthru_len); - - size_t prefix_len, address_len, ra_pref_len, - ra_route_len, ra_dns_len, ra_search_len; - uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len); - uint8_t *address = odhcp6c_get_state(STATE_IA_NA, &address_len); - uint8_t *ra_pref = odhcp6c_get_state(STATE_RA_PREFIX, &ra_pref_len); - uint8_t *ra_route = odhcp6c_get_state(STATE_RA_ROUTE, &ra_route_len); - uint8_t *ra_dns = odhcp6c_get_state(STATE_RA_DNS, &ra_dns_len); - uint8_t *ra_search = odhcp6c_get_state(STATE_RA_SEARCH, &ra_search_len); - - // Don't set environment before forking, because env is leaky. - if (fork() == 0) { + time_t now = odhcp6c_get_milli_time() / 1000; + + if (running) { + kill(running, SIGTERM); + delay -= now - started; + } + + if (resume || !action[0]) + strncpy(action, status, sizeof(action) - 1); + + pid_t pid = fork(); + if (pid > 0) { + running = pid; + started = now; + + if (!resume) + action[0] = 0; + } else if (pid == 0) { + size_t dns_len, search_len, custom_len, sntp_ip_len, ntp_ip_len, ntp_dns_len; + size_t sip_ip_len, sip_fqdn_len, aftr_name_len, cer_len, addr_len; + size_t s46_mapt_len, s46_mape_len, s46_lw_len, passthru_len; + + signal(SIGTERM, SIG_DFL); + if (delay > 0) { + sleep(delay); + odhcp6c_expire(); + } + + struct in6_addr *addr = odhcp6c_get_state(STATE_SERVER_ADDR, &addr_len); + struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len); + uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len); + uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len); + struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len); + struct in6_addr *ntp = odhcp6c_get_state(STATE_NTP_IP, &ntp_ip_len); + uint8_t *ntp_dns = odhcp6c_get_state(STATE_NTP_FQDN, &ntp_dns_len); + struct in6_addr *sip = odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len); + uint8_t *sip_fqdn = odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len); + uint8_t *aftr_name = odhcp6c_get_state(STATE_AFTR_NAME, &aftr_name_len); + struct in6_addr *cer = odhcp6c_get_state(STATE_CER, &cer_len); + uint8_t *s46_mapt = odhcp6c_get_state(STATE_S46_MAPT, &s46_mapt_len); + uint8_t *s46_mape = odhcp6c_get_state(STATE_S46_MAPE, &s46_mape_len); + uint8_t *s46_lw = odhcp6c_get_state(STATE_S46_LW, &s46_lw_len); + uint8_t *passthru = odhcp6c_get_state(STATE_PASSTHRU, &passthru_len); + + size_t prefix_len, address_len, ra_pref_len, + ra_route_len, ra_dns_len, ra_search_len; + uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len); + uint8_t *address = odhcp6c_get_state(STATE_IA_NA, &address_len); + uint8_t *ra_pref = odhcp6c_get_state(STATE_RA_PREFIX, &ra_pref_len); + uint8_t *ra_route = odhcp6c_get_state(STATE_RA_ROUTE, &ra_route_len); + uint8_t *ra_dns = odhcp6c_get_state(STATE_RA_DNS, &ra_dns_len); + uint8_t *ra_search = odhcp6c_get_state(STATE_RA_SEARCH, &ra_search_len); + ipv6_to_env("SERVER", addr, addr_len / sizeof(*addr)); ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns)); ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp)); @@ -406,7 +440,6 @@ void script_call(const char *status) script_hexlify(&buf[9], passthru, passthru_len); putenv(buf); - argv[2] = (char*)status; execv(argv[0], argv); _exit(128); } -- 2.25.1