From 63f61d080aa524ed3c79518e678ddfa6dbe5a33c Mon Sep 17 00:00:00 2001 From: zhangshuai39 Date: Fri, 6 Jun 2025 20:44:56 +0800 Subject: [PATCH] netutils/ntpclient: Fix a large number of print DNS request errors Before attempting to sample via NTP, network connectivity should be checked to avoid frequent sampling that could cause network error logs to overflow. Signed-off-by: zhangshuai39 --- netutils/ntpclient/ntpclient.c | 39 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/netutils/ntpclient/ntpclient.c b/netutils/ntpclient/ntpclient.c index 19962f4d5..22c448fdd 100644 --- a/netutils/ntpclient/ntpclient.c +++ b/netutils/ntpclient/ntpclient.c @@ -52,6 +52,7 @@ #include +#include "netutils/netlib.h" #include "netutils/ntpclient.h" #include "ntpv3.h" @@ -103,6 +104,8 @@ #define MAX_SERVER_SELECTION_RETRIES 3 +#define MAX_RETRY_INTERVAL 120 + #ifndef STR # define STR2(x) #x # define STR(x) STR2(x) @@ -1233,6 +1236,7 @@ static int ntpc_daemon(int argc, FAR char **argv) FAR struct ntp_servers_s *srvs; int exitcode = EXIT_SUCCESS; int retries = 0; + int retry_delay = 1; int nsamples; int ret; @@ -1302,21 +1306,24 @@ static int ntpc_daemon(int argc, FAR char **argv) clock_gettime(CLOCK_REALTIME, &start_realtime); clock_gettime(CLOCK_MONOTONIC, &start_monotonic); - /* Collect samples. */ - nsamples = 0; - for (i = 0; i < CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES; i++) + if (netlib_check_ipconnectivity(NULL, 1, 1) > 0) { - /* Get next sample. */ + /* Collect samples. */ - ret = ntpc_get_ntp_sample(srvs, samples, nsamples); - if (ret < 0) + for (i = 0; i < CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES; i++) { - errval = errno; - } - else - { - ++nsamples; + /* Get next sample. */ + + ret = ntpc_get_ntp_sample(srvs, samples, nsamples); + if (ret < 0) + { + errval = errno; + } + else + { + ++nsamples; + } } } @@ -1405,6 +1412,7 @@ static int ntpc_daemon(int argc, FAR char **argv) sleep(CONFIG_NETUTILS_NTPCLIENT_POLLDELAYSEC); retries = 0; + retry_delay = 1; } continue; @@ -1424,7 +1432,14 @@ static int ntpc_daemon(int argc, FAR char **argv) if (errval != EINTR) { - sleep(1); + ninfo("Retry %d in %d seconds...\n", retries, retry_delay); + sleep(retry_delay); + + retry_delay *= 2; + if (retry_delay > MAX_RETRY_INTERVAL) + { + retry_delay = MAX_RETRY_INTERVAL; + } } /* Keep retrying. */