From b2851e8d6287ab9bb3cd6e42395271c45ce0e377 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 23 Aug 2023 13:25:35 +0900 Subject: [PATCH] sd-dhcp-client: make client initially in stopped state 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 | 18 +++++++++--------- src/libsystemd-network/sd-dhcp-client.c | 14 ++++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/libsystemd-network/dhcp-protocol.h b/src/libsystemd-network/dhcp-protocol.h index 062ce006f30..2dc0660cc7d 100644 --- a/src/libsystemd-network/dhcp-protocol.h +++ b/src/libsystemd-network/dhcp-protocol.h @@ -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; diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 8f330c8f145..265750d0904 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -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, -- 2.39.2