]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp-client: make client initially in stopped state
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 23 Aug 2023 04:25:35 +0000 (13:25 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 28 Aug 2023 09:25:14 +0000 (10:25 +0100)
Previously, DHCP_STATE_STOPPED and DHCP_STATE_INIT are both handled as
not-running. Moreover, previously after sd_dhcp_client_start() is
called, the client still in INIT state (and thus handled as not-running)
even if its internal timer event sources are initialized.

Let's make only STOPPED state handled as not-running, and make the
client initially in STOPPED state.

Prompted by #28896.

src/libsystemd-network/dhcp-protocol.h
src/libsystemd-network/sd-dhcp-client.c

index 062ce006f30ff601222e7b2ee8f33acddba488ef..2dc0660cc7d7b59dbce56e32d6c9e1a532d8d048 100644 (file)
@@ -55,15 +55,15 @@ enum {
 };
 
 enum DHCPState {
-        DHCP_STATE_INIT                         = 0,
-        DHCP_STATE_SELECTING                    = 1,
-        DHCP_STATE_INIT_REBOOT                  = 2,
-        DHCP_STATE_REBOOTING                    = 3,
-        DHCP_STATE_REQUESTING                   = 4,
-        DHCP_STATE_BOUND                        = 5,
-        DHCP_STATE_RENEWING                     = 6,
-        DHCP_STATE_REBINDING                    = 7,
-        DHCP_STATE_STOPPED                      = 8,
+        DHCP_STATE_STOPPED                      = 0,
+        DHCP_STATE_INIT                         = 1,
+        DHCP_STATE_SELECTING                    = 2,
+        DHCP_STATE_INIT_REBOOT                  = 3,
+        DHCP_STATE_REBOOTING                    = 4,
+        DHCP_STATE_REQUESTING                   = 5,
+        DHCP_STATE_BOUND                        = 6,
+        DHCP_STATE_RENEWING                     = 7,
+        DHCP_STATE_REBINDING                    = 8,
 };
 
 typedef enum DHCPState DHCPState;
index 8f330c8f145628adab3c050b7fb86351f641c4de..265750d090419b229238d812a1644e3f90268d30 100644 (file)
@@ -753,7 +753,7 @@ static int client_initialize(sd_dhcp_client *client) {
 
         client->attempt = 0;
 
-        client->state = DHCP_STATE_INIT;
+        client->state = DHCP_STATE_STOPPED;
         client->xid = 0;
 
         client->lease = sd_dhcp_lease_unref(client->lease);
@@ -1434,7 +1434,7 @@ static int client_start_delayed(sd_dhcp_client *client) {
         assert_return(client->ifindex > 0, -EINVAL);
         assert_return(client->fd < 0, -EBUSY);
         assert_return(client->xid == 0, -EINVAL);
-        assert_return(IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_INIT_REBOOT), -EBUSY);
+        assert_return(IN_SET(client->state, DHCP_STATE_STOPPED, DHCP_STATE_INIT_REBOOT), -EBUSY);
 
         client->xid = random_u32();
 
@@ -1448,8 +1448,10 @@ static int client_start_delayed(sd_dhcp_client *client) {
         }
         client->fd = r;
 
-        if (IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_INIT_REBOOT))
-                client->start_time = now(CLOCK_BOOTTIME);
+        client->start_time = now(CLOCK_BOOTTIME);
+
+        if (client->state == DHCP_STATE_STOPPED)
+                client->state = DHCP_STATE_INIT;
 
         return client_initialize_events(client, client_receive_message_raw);
 }
@@ -2061,7 +2063,7 @@ int sd_dhcp_client_is_running(sd_dhcp_client *client) {
         if (!client)
                 return 0;
 
-        return !IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_STOPPED);
+        return client->state != DHCP_STATE_STOPPED;
 }
 
 int sd_dhcp_client_start(sd_dhcp_client *client) {
@@ -2263,7 +2265,7 @@ int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) {
 
         *client = (sd_dhcp_client) {
                 .n_ref = 1,
-                .state = DHCP_STATE_INIT,
+                .state = DHCP_STATE_STOPPED,
                 .ifindex = -1,
                 .fd = -EBADF,
                 .mtu = DHCP_MIN_PACKET_SIZE,