#include <stdbool.h>
#include <net/if.h>
-#include <sys/wait.h>
#include <sys/syscall.h>
#include <arpa/inet.h>
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[])
{
}
}
+ if (allow_slaac_only > 0)
+ script_sync_delay = allow_slaac_only;
+
openlog("odhcp6c", logopt, LOG_DAEMON);
if (!verbosity)
setlogmask(LOG_UPTO(LOG_WARNING));
signal(SIGIO, sighandler);
signal(SIGHUP, sighandler);
signal(SIGINT, sighandler);
- signal(SIGCHLD, sighandler);
signal(SIGTERM, sighandler);
signal(SIGUSR1, sighandler);
signal(SIGUSR2, sighandler);
}
}
- script_call("started");
+ script_call("started", 0, false);
while (!signal_term) { // Main logic
odhcp6c_clear_state(STATE_SERVER_ID);
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();
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) {
int res = dhcpv6_poll_reconfigure();
odhcp6c_signal_process();
if (res > 0) {
- script_call("updated");
+ script_call("updated", 0, false);
continue;
}
odhcp6c_signal_process();
if (res > 0) { // Renew was succesfull
// Publish updates
- script_call("updated");
+ script_call("updated", 0, false);
continue; // Renew was successful
}
odhcp6c_signal_process();
if (res > 0)
- script_call("rebound");
+ script_call("rebound", 0, true);
else {
break;
}
// 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);
odhcp6c_clear_state(STATE_IA_PD);
}
- script_call("stopped");
+ script_call("stopped", 0, true);
return 0;
}
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;
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;
#include <unistd.h>
#include <inttypes.h>
#include <arpa/inet.h>
+#include <sys/wait.h>
#include <netinet/in.h>
#include "odhcp6c.h"
};
+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;
}
}
-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));
script_hexlify(&buf[9], passthru, passthru_len);
putenv(buf);
- argv[2] = (char*)status;
execv(argv[0], argv);
_exit(128);
}