statefiles: introduce statefiles_write()
authorDavid Härdeman <[email protected]>
Fri, 7 Nov 2025 16:58:05 +0000 (17:58 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 11 Nov 2025 07:30:06 +0000 (08:30 +0100)
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 <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/302
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/dhcpv4.c
src/dhcpv6-ia.c
src/statefiles.c
src/statefiles.h

index 96e18a19d0e21b837f97ba784c807cec3125642c..61ec82d457840a9e48b6e437397d3ed0fd72dddc 100644 (file)
@@ -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);
 }
index 2a1c079bc296c834e94dd1c3fe0f0357b3607ef6..9e24e5e427b329848c528eedf9cace25b8ff82bf 100644 (file)
@@ -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;
index 0e8694002287c84fd71576e8a37995ec35fcab8d..e90e7c6cdc2eddda4a2e8b800ae4b553615b88a1 100644 (file)
@@ -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;
 }
index 6a06ec2d278a9f6b61384f2b1648a6f0d4f6c528..f6fc93393d8905082ea0005df33f51e3a5d86a65 100644 (file)
@@ -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_ */