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.
This commit is contained in:
Miroslav Lichvar 2020-01-29 12:28:43 +01:00
parent 994409a036
commit 1858104b5c

10
util.c
View file

@ -1218,13 +1218,19 @@ int
UTI_RemoveFile(const char *basedir, const char *name, const char *suffix) UTI_RemoveFile(const char *basedir, const char *name, const char *suffix)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
struct stat buf;
if (!join_path(basedir, name, suffix, path, sizeof (path), LOGS_ERR)) if (!join_path(basedir, name, suffix, path, sizeof (path), LOGS_ERR))
return 0; 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) { if (unlink(path) < 0) {
LOG(errno != ENOENT ? LOGS_ERR : LOGS_DEBUG, LOG(LOGS_ERR, "Could not remove %s : %s", path, strerror(errno));
"Could not remove %s : %s", path, strerror(errno));
return 0; return 0;
} }