From c7d33e18995f7286cde9db77db43aff014c78940 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 25 Sep 2017 15:36:14 -0700 Subject: [PATCH] conmon: Re-open the log file to not exceed max log file size Signed-off-by: Mrunal Patel --- conmon/conmon.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/conmon/conmon.c b/conmon/conmon.c index 223a2a73..1232a0b8 100644 --- a/conmon/conmon.c +++ b/conmon/conmon.c @@ -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;