conmon: open+rename the log file instead of unlink+open

at no time the log file is not accessible by its path.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2018-02-23 11:09:41 +01:00
parent 8ea79e755f
commit a62b39ffa4
No known key found for this signature in database
GPG Key ID: 263D6DF2E163E1EA
1 changed files with 8 additions and 6 deletions

View File

@ -341,21 +341,23 @@ static int write_k8s_log(int fd, stdpipe_t pipe, const char *buf, ssize_t buflen
* a timestamp.
*/
if ((opt_log_size_max > 0) && (bytes_written + bytes_to_be_written) > opt_log_size_max) {
_cleanup_free_ char *opt_log_path_tmp = g_strdup_printf("%s.tmp", opt_log_path);
ninfo("Creating new log file");
bytes_written = 0;
/* Close the existing fd */
close(fd);
/* Unlink the file */
if (unlink(opt_log_path) < 0) {
pexit("Failed to unlink log file");
}
/* Open the log path file again */
log_fd = open(opt_log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600);
log_fd = open(opt_log_path_tmp, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0600);
if (log_fd < 0)
pexit("Failed to open log file %s: %s", opt_log_path, strerror(errno));
/* Replace the previous file */
if (rename(opt_log_path_tmp, opt_log_path) < 0) {
pexit("Failed to rename log file");
}
fd = log_fd;
}