[PATCH] ntpd: reload resolv.conf file when DNS resolution fails

Dávid Benko davidbenko at davidbenko.dev
Sun Aug 24 20:57:43 UTC 2025


Hi,

sending a small patch for NTPD client applet. The issue is that time
synchronization may completely fail if DNS resolution is not working
properly when the NTPD service is started.

Discovered on dumb APs running OpenWrt 24.10 with BusyBox 1.36.1.
Changes were tested on Ubuntu 20.04.

My assumption is that libresolv is available on all supported platforms.
If that's not the case, let me know (or add some config flag for it
yourself).

David


-- >8 --

>From d8c6c48ca863aa5ac7dc947ad1be531c388e3107 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=A1vid=20Benko?= <davidbenko at davidbenko.dev>
Date: Sun, 24 Aug 2025 22:21:01 +0200
Subject: [PATCH] ntpd: reload resolv.conf file when DNS resolution fails
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When NTPD is started with peers identified by hostnames while no DNS
server is configured in resolv.conf file, no NTP synchronization is
ever carried out as DNS resolution fails indefinitely, even when
resolv.conf changes.

This is especially problematic on devices with local DNS server
(e.g. dnsmasq) disabled and just DHCP client upstream interface(s) -
for example dumb APs. During boot process, ntpd service may start
sooner than DHCP address allocation is finished and DNS servers are
set up causing complete time synchronization failure.

Proposed solution is simple - just reload resolv.conf file in each
consecutive DNS resolution attempt.

Signed-off-by: Dávid Benko <davidbenko at davidbenko.dev>
---
 Makefile.flags    |  4 +++-
 networking/ntpd.c | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/Makefile.flags b/Makefile.flags
index 97cb4dca2..b90dfca84 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -184,7 +184,9 @@ CPPFLAGS += $(SELINUX_CFLAGS)
 LDLIBS += $(if $(SELINUX_LIBS),$(SELINUX_LIBS:-l%=%),$(SELINUX_PC_MODULES:lib%=%))
 endif
 
-ifeq ($(CONFIG_FEATURE_NSLOOKUP_BIG),y)
+ifeq ($(CONFIG_NTPD),y)
+LDLIBS += resolv
+else ifeq ($(CONFIG_FEATURE_NSLOOKUP_BIG),y)
 ifneq (,$(findstring linux,$(shell $(CC) $(CFLAGS) -dumpmachine)))
 LDLIBS += resolv
 endif
diff --git a/networking/ntpd.c b/networking/ntpd.c
index dd0a9c91f..c15eb3004 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -108,6 +108,7 @@
 #include "libbb.h"
 #include <math.h>
 #include <netinet/ip.h> /* For IPTOS_DSCP_AF21 definition */
+#include <resolv.h>
 #include <sys/timex.h>
 #ifndef IPTOS_DSCP_AF21
 # define IPTOS_DSCP_AF21 0x48
@@ -2591,6 +2592,7 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 		if (nfds <= 0) {
 			double ct;
 			int dns_error;
+			bool resolv_reloaded = false;
 
 			if (bb_got_signal)
 				break; /* poll was interrupted by a signal */
@@ -2611,6 +2613,23 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 			for (item = G.ntp_peers; item != NULL; item = item->link) {
 				peer_t *p = (peer_t *) item->data;
 				if (p->next_action_time <= ct && !p->p_lsa) {
+					/* Reload resolv.conf file
+					 * DNS resolution may be failing due to missing definitions
+					 * of DNS servers. This is especially problematic
+					 * on devices with local DNS server (e.g. dnsmasq) disabled
+					 * and just DHCP client upstream interface(s) - for example
+					 * dumb APs. During boot process, ntpd service may start
+					 * sooner than DHCP address allocation is finished and DNS
+					 * servers are set up causing complete time synchronization
+					 * failure.
+					 * resolv_reloaded flag ensures reload is performed only
+					 * once for all unresolved peers in one resolution cycle.
+					 */
+					if (!resolv_reloaded) {
+						res_init();
+						resolv_reloaded = true;
+					}
+
 					/* This can take up to ~10 sec per each DNS query */
 					dns_error |= (!resolve_peer_hostname(p));
 				}
-- 
2.25.1


More information about the busybox mailing list