From 68c6e8340a165a6a60e096736d922dd1da8063ac Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Fri, 29 May 2026 12:57:39 +0200 Subject: [PATCH] init/dhcp: only overwrite resolv.conf with DNS Only overwrite resolv.conf when the DHCP server has provide us some nameservers. This allows users to keep the original resolv.conf in case it was populated beforehand. Signed-off-by: Sergio Lopez --- init/dhcp.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/init/dhcp.c b/init/dhcp.c index e852bded8..3c107e5b6 100644 --- a/init/dhcp.c +++ b/init/dhcp.c @@ -293,6 +293,9 @@ static unsigned char get_dhcp_msg_type(const unsigned char *response, static int handle_dhcp_ack(int nl_sock, int iface_index, const unsigned char *response, ssize_t len) { + FILE *resolv = NULL; + bool tried_opening_resolv = false; + /* Need at least 240 bytes (DHCP header + magic cookie) + 1 for options */ if (len < 241) { printf("DHCPACK too short (%zd bytes)\n", len); @@ -314,11 +317,6 @@ static int handle_dhcp_ack(int nl_sock, int iface_index, /* Clamp MTU to passt's limit */ uint16_t mtu = 65520; - FILE *resolv = fopen("/etc/resolv.conf", "w"); - if (!resolv) { - perror("Failed to open /etc/resolv.conf"); - } - /* Parse DHCP options (start at offset 240 after magic cookie) */ size_t p = 240; while (p < (size_t)len) { @@ -353,6 +351,14 @@ static int handle_dhcp_ack(int nl_sock, int iface_index, memcpy(&router.s_addr, &response[p], sizeof(router.s_addr)); } else if (opt == 6 && opt_len >= 4) { /* Option 6: Domain Name Server */ + if (!resolv && !tried_opening_resolv) { + tried_opening_resolv = true; + resolv = fopen("/etc/resolv.conf", "w"); + if (!resolv) { + perror("Failed to open /etc/resolv.conf"); + } + } + if (resolv) { for (int dns_p = p; dns_p + 4 <= p + opt_len; dns_p += 4) { fprintf(resolv, "nameserver %d.%d.%d.%d\n", response[dns_p],