]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: don't log unlink() error if file is not accessible
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 29 Jan 2020 11:28:43 +0000 (12:28 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 18 Feb 2020 15:01:25 +0000 (16:01 +0100)
Try stat() before calling unlink() to make sure the file is accessible.

This fixes chronyc running under a non-root/chrony user printing an
error message due to missing permissions on /var/run/chrony before
trying to bind its socket.

util.c

diff --git a/util.c b/util.c
index 2ef971e86c5e31ecd567f222438d588499b91ecb..0758fb6a3ce7f888bc99cd900245ddd7fc6a1137 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1218,13 +1218,19 @@ int
 UTI_RemoveFile(const char *basedir, const char *name, const char *suffix)
 {
   char path[PATH_MAX];
+  struct stat buf;
 
   if (!join_path(basedir, name, suffix, path, sizeof (path), LOGS_ERR))
     return 0;
 
+  /* Avoid logging an error message if the file is not accessible */
+  if (stat(path, &buf) < 0) {
+    DEBUG_LOG("Could not remove %s : %s", path, strerror(errno));
+    return 0;
+  }
+
   if (unlink(path) < 0) {
-    LOG(errno != ENOENT ? LOGS_ERR : LOGS_DEBUG,
-        "Could not remove %s : %s", path, strerror(errno));
+    LOG(LOGS_ERR, "Could not remove %s : %s", path, strerror(errno));
     return 0;
   }