From: Miroslav Lichvar Date: Wed, 29 Jan 2020 11:28:43 +0000 (+0100) Subject: util: don't log unlink() error if file is not accessible X-Git-Tag: 4.0-pre1~76 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1858104b5ca2ed060fa13bf229b73669a6d7bc0c;p=thirdparty%2Fchrony.git util: don't log unlink() error if file is not accessible 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. --- diff --git a/util.c b/util.c index 2ef971e8..0758fb6a 100644 --- 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; }