From: David Härdeman Date: Fri, 7 Nov 2025 16:58:05 +0000 (+0100) Subject: statefiles: introduce statefiles_write() X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=98276afcff82884d304f3a06e5dadc5d9192f96e;p=project%2Fodhcpd.git statefiles: introduce statefiles_write() First, the name makes it clearer that several files might be written (and it doesn't refer to dhcpv6 anymore). This also lays the ground for the coming patches. Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/302 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 96e18a1..61ec82d 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -1597,7 +1597,7 @@ static void dhcpv4_valid_until_cb(struct uloop_timeout *event) } if (update_statefile) - dhcpv6_ia_write_statefile(); + statefiles_write(); uloop_timeout_set(event, 5000); } diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index 2a1c079..9e24e5e 100644 --- a/src/dhcpv6-ia.c +++ b/src/dhcpv6-ia.c @@ -502,7 +502,7 @@ static void handle_addrlist_change(struct netevent_handler_info *info) dhcpv6_free_lease(c); } - dhcpv6_ia_write_statefile(); + statefiles_write(); } static void reconf_timeout_cb(struct uloop_timeout *event) @@ -1313,7 +1313,7 @@ proceed: break; } - dhcpv6_ia_write_statefile(); + statefiles_write(); out: return response_len; diff --git a/src/statefiles.c b/src/statefiles.c index 0e86940..e90e7c6 100644 --- a/src/statefiles.c +++ b/src/statefiles.c @@ -144,7 +144,7 @@ static void dhcpv6_write_ia_addr(struct in6_addr *addr, int prefix, _unused uint "%s/%d ", ipbuf, prefix); } -static void dhcpv6_ia_write_hostsfile(time_t now) +static void statefiles_write_hosts(time_t now) { struct write_ctxt ctxt; size_t tmp_hostsfile_strlen; @@ -221,17 +221,18 @@ err: close(fd); } -void dhcpv6_ia_write_statefile(void) +static bool statefiles_write_state(time_t now) { struct write_ctxt ctxt; size_t tmp_statefile_strlen; char *tmp_statefile; - time_t now = odhcpd_time(), wall_time = time(NULL); + time_t wall_time = time(NULL); char leasebuf[512]; + uint8_t newmd5[16]; int fd; if (config.dhcp_statedir_fd < 0 || !config.dhcp_statefile) - return; + return false; tmp_statefile_strlen = strlen(config.dhcp_statefile) + 2; tmp_statefile = alloca(tmp_statefile_strlen); @@ -338,30 +339,40 @@ void dhcpv6_ia_write_statefile(void) } fclose(ctxt.fp); - - uint8_t newmd5[16]; md5_end(newmd5, &ctxt.md5); renameat(config.dhcp_statedir_fd, tmp_statefile, config.dhcp_statedir_fd, config.dhcp_statefile); - if (memcmp(newmd5, statemd5, sizeof(newmd5))) { - memcpy(statemd5, newmd5, sizeof(statemd5)); + if (!memcmp(newmd5, statemd5, sizeof(newmd5))) + return false; - dhcpv6_ia_write_hostsfile(now); + memcpy(statemd5, newmd5, sizeof(statemd5)); - if (config.dhcp_cb) { - char *argv[2] = {config.dhcp_cb, NULL}; - if (!vfork()) { - execv(argv[0], argv); - _exit(128); - } - } - } - - return; + return true; err: error("Unable to write statefile: %m"); close(fd); + return false; +} + +bool statefiles_write() +{ + time_t now = odhcpd_time(); + + if (!statefiles_write_state(now)) + return false; + + statefiles_write_hosts(now); + + if (config.dhcp_cb) { + char *argv[2] = { config.dhcp_cb, NULL }; + if (!vfork()) { + execv(argv[0], argv); + _exit(128); + } + } + + return true; } diff --git a/src/statefiles.h b/src/statefiles.h index 6a06ec2..f6fc933 100644 --- a/src/statefiles.h +++ b/src/statefiles.h @@ -10,6 +10,6 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcpv6_lease *lease, time_t now, dhcpv6_binding_cb_handler_t func, void *arg); -void dhcpv6_ia_write_statefile(void); +bool statefiles_write(void); #endif /* _STATEFILES_H_ */