diff --git a/os/log.c b/os/log.c index c0ff0ee93..8fe21ed9d 100644 --- a/os/log.c +++ b/os/log.c @@ -179,23 +179,26 @@ LogFilePrep(const char *fname, const char *backup, const char *idstring) FatalError("Cannot allocate space for the log file name\n"); if (backup && *backup) { - struct stat buf; + int fd = open(logFileName, O_RDWR | O_NOFOLLOW); + if (fd != -1) { + struct stat buf; + if (fstat(fd, &buf) == 0 && S_ISREG(buf.st_mode)) { + char *suffix; + char *oldLog; - if (!stat(logFileName, &buf) && S_ISREG(buf.st_mode)) { - char *suffix; - char *oldLog; + if ((asprintf(&suffix, backup, idstring) == -1) || + (asprintf(&oldLog, "%s%s", logFileName, suffix) == -1)) { + FatalError("Cannot allocate space for the log file name\n"); + } + free(suffix); - if ((asprintf(&suffix, backup, idstring) == -1) || - (asprintf(&oldLog, "%s%s", logFileName, suffix) == -1)) { - FatalError("Cannot allocate space for the log file name\n"); + if (renameat(AT_FDCWD, logFileName, AT_FDCWD, oldLog) == -1) { + FatalError("Cannot move old log file \"%s\" to \"%s\"\n", + logFileName, oldLog); + } + free(oldLog); } - free(suffix); - - if (rename(logFileName, oldLog) == -1) { - FatalError("Cannot move old log file \"%s\" to \"%s\"\n", - logFileName, oldLog); - } - free(oldLog); + close(fd); } } else {