]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
conf: fix sourcedir reloading to not multiply sources
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 2 Jun 2025 08:53:47 +0000 (10:53 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 3 Jun 2025 09:41:12 +0000 (11:41 +0200)
The sourcedir reload triggered by the chronyc "reload sources"
command incorrectly assumed that NSR_AddSourceByName() can return
only the NSR_Success status when a source is added. It ignored the
NSR_UnresolvedName status returned for a source whose name needs to
be resolved after the call (i.e. not specified with an IP address)
and added the source again, effectively multiplying it if the name
can be resolved to a different IP address.

Fix the code to check for the NSR_UnresolvedName status to correctly
determine whether the source was already added before and should not be
added again.

Reported-by: MichaelR <MichaelR42@runbox.com>
Fixes: 916ed70c4a81 ("conf: save source status in sourcedir reload")
conf.c

diff --git a/conf.c b/conf.c
index cd3466b88a9e8f8ba290a19711192bb2e3212cd9..a6bb3bf4156fa24af6a2c4f98f485642080b3fd9 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -1872,8 +1872,8 @@ reload_source_dirs(void)
   NTP_Source *prev_sources, *new_sources, *source;
   unsigned int i, j, prev_size, new_size, unresolved;
   char buf[MAX_LINE_LENGTH];
+  int d, pass, was_added;
   NSR_Status s;
-  int d, pass;
 
   /* Ignore reload command before adding configured sources */
   if (!conf_ntp_sources_added)
@@ -1912,13 +1912,16 @@ reload_source_dirs(void)
       else
         d = i < prev_size ? -1 : 1;
 
+      was_added = d <= 0 && (prev_sources[i].status == NSR_Success ||
+                             prev_sources[i].status == NSR_UnresolvedName);
+
       /* Remove missing sources before adding others to avoid conflicts */
-      if (pass == 0 && d < 0 && prev_sources[i].status == NSR_Success) {
+      if (pass == 0 && d < 0 && was_added) {
         NSR_RemoveSourcesById(prev_sources[i].conf_id);
       }
 
       /* Add new sources and sources that could not be added before */
-      if (pass == 1 && (d > 0 || (d == 0 && prev_sources[i].status != NSR_Success))) {
+      if (pass == 1 && (d > 0 || (d == 0 && !was_added))) {
         source = &new_sources[j];
         s = NSR_AddSourceByName(source->params.name, source->params.family, source->params.port,
                                 source->pool, source->type, &source->params.params,