]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: warn if UTI_OpenFile() is stuck in a loop master
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 2 Jul 2025 13:34:03 +0000 (15:34 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 2 Jul 2025 14:02:41 +0000 (16:02 +0200)
When UTI_OpenFile() is removing an existing file to be replaced by a new
file, it could potentially get stuck in an infinite loop if something
was able to consistently win the race and create a new file before
chronyd.

Log a warning message after 100 failed attempts and repeat on each 10x
increase to make it more obvious to the admin, if it ever happens.

Reported-by: Eric Sesterhenn <eric.sesterhenn@x41-dsec.de>
util.c

diff --git a/util.c b/util.c
index 26eaa35f573ed343b2762b39aa764d58951c15e3..837330b2b666815a72ba3107fbe6c645db3f10b2 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1364,6 +1364,7 @@ FILE *
 UTI_OpenFile(const char *basedir, const char *name, const char *suffix,
              char mode, mode_t perm)
 {
+  uint64_t attempts = 0, warn_attempts = 100;
   const char *file_mode;
   char path[PATH_MAX];
   LOG_Severity severity;
@@ -1407,6 +1408,12 @@ try_again:
         return NULL;
       }
       DEBUG_LOG("Removed %s", path);
+
+      if (++attempts == warn_attempts) {
+        LOG(LOGS_WARN, "Failing to replace %s (%"PRIu64" attempts)", path, attempts);
+        warn_attempts *= 10;
+      }
+
       goto try_again;
     }
     LOG(severity, "Could not open %s : %s", path, strerror(errno));