From 5e6b5e0b300d3fd4b0ad2a0e4138bc4f618ad431 Mon Sep 17 00:00:00 2001 From: Xiubo Li Date: Wed, 21 Nov 2018 12:02:23 +0800 Subject: [PATCH] logrotate: update the logdir path whenever it changes Signed-off-by: Xiubo Li --- libtcmu_log.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/libtcmu_log.c b/libtcmu_log.c index 8bca60e7..c69b042b 100644 --- a/libtcmu_log.c +++ b/libtcmu_log.c @@ -7,6 +7,7 @@ */ #define _GNU_SOURCE +#include #include #include #include @@ -524,6 +525,78 @@ static bool tcmu_log_dir_check(const char *path) return true; } +#define TCMU_LOGROTATE_PATH "/etc/logrotate.d/tcmu-runner" + +static int tcmu_logrotate_conf_set(const char *logdir) +{ + char *buf = NULL, *line = NULL, *p; + int ret, m, len; + size_t n; + FILE *fp; + + fp = fopen(TCMU_LOGROTATE_PATH, "r+"); + if (fp == NULL) { + ret = -errno; + tcmu_err("Failed to open file '%s', %m\n", TCMU_LOGROTATE_PATH); + return ret; + } + + ret = fseek(fp, 0L, SEEK_END); + if (ret == -1) { + ret = -errno; + tcmu_err("Failed to seek file '%s', %m\n", TCMU_LOGROTATE_PATH); + goto error; + } + + len = ftell(fp); + if (len == -1) { + ret = -errno; + tcmu_err("Failed to get the length of file '%s', %m\n", TCMU_LOGROTATE_PATH); + goto error; + } + + /* to make sure we have enough size */ + len += strlen(logdir) + 1; + buf = calloc(len, sizeof(char)); + if (!buf) { + ret = -ENOMEM; + goto error; + } + + p = buf; + fseek(fp, 0L, SEEK_SET); + while ((m = getline(&line, &n, fp)) != -1) { + if (strstr(line, "tcmu-runner*.log") && strchr(line, '{')) + m = sprintf(p, "%s/tcmu-runner*.log {\n", logdir); + else + m = sprintf(p, "%s", line); + if (m < 0) { + ret = m; + goto error; + } + + p += m; + } + *p = '\0'; + len = p - buf; + + fseek(fp, 0L, SEEK_SET); + truncate(TCMU_LOGROTATE_PATH, 0L); + ret = fwrite(buf, 1, len, fp); + if (ret != len) { + tcmu_err("Failed to update '%s', %m\n", TCMU_LOGROTATE_PATH); + goto error; + } + + ret = 0; +error: + if (fp) + fclose(fp); + free(buf); + free(line); + return ret; +} + static int tcmu_log_dir_set(const char *log_dir) { char *new_dir; @@ -536,7 +609,8 @@ static int tcmu_log_dir_set(const char *log_dir) tcmu_log_dir_free(); tcmu_log_dir = new_dir; - return 0; + + return tcmu_logrotate_conf_set(tcmu_log_dir); } static int tcmu_mkdir(const char *path)