conmon: Re-open the log file to not exceed max log file size

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
Mrunal Patel 2017-09-25 15:36:14 -07:00
parent 82899bdb4e
commit c7d33e1899

View file

@ -134,6 +134,8 @@ static GOptionEntry opt_entries[] =
#define CGROUP_ROOT "/sys/fs/cgroup"
static int log_fd = -1;
static ssize_t write_all(int fd, const void *buf, size_t count)
{
size_t remaining = count;
@ -336,6 +338,31 @@ static int write_k8s_log(int fd, stdpipe_t pipe, const char *buf, ssize_t buflen
}
}
/*
* We re-open the log file if writing out the bytes will exceed the max
* log size. We also reset the state so that the new file is started with
* a timestamp.
*/
if ((opt_log_size_max > 0) && (bytes_written + bytes_to_be_written) > opt_log_size_max) {
ninfo("Creating new log file");
insert_newline = FALSE;
insert_timestamp = TRUE;
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);
if (log_fd < 0)
pexit("Failed to open log file");
fd = log_fd;
}
/* Output a newline */
if (insert_newline) {
@ -511,7 +538,6 @@ static int conn_sock = -1;
static int conn_sock_readable;
static int conn_sock_writable;
static int log_fd = -1;
static int oom_event_fd = -1;
static int attach_socket_fd = -1;
static int console_socket_fd = -1;