diff --git a/cli/gluster-block.c b/cli/gluster-block.c index 9f46838..30b79bc 100644 --- a/cli/gluster-block.c +++ b/cli/gluster-block.c @@ -961,7 +961,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if(initLogging()) { + if(initLogging(NULL)) { exit(EXIT_FAILURE); } diff --git a/daemon/gluster-blockd.c b/daemon/gluster-blockd.c index 6e86d6f..3a8eb3b 100644 --- a/daemon/gluster-blockd.c +++ b/daemon/gluster-blockd.c @@ -437,7 +437,7 @@ main (int argc, char **argv) int errnosv = 0; - if(initLogging()) { + if(initLogging("mgmt")) { exit(EXIT_FAILURE); } diff --git a/utils/utils.c b/utils/utils.c index d4116c1..01a0985 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -253,12 +253,102 @@ void fetchGlfsVolServerFromEnv() LOG("mgmt", GB_LOG_INFO, "Block Hosting Volfile Server Set to: %s", gbConf.volServer); } + +static int glusterLogrotateConfigSet(const char *dom, char *logDir) +{ + char *buf = NULL, *line = NULL, *p; + int ret, m, len; + size_t n; + FILE *fp; + + fp = fopen(GB_LOGROTATE_PATH, "r+"); + if (fp == NULL) { + ret = -errno; + if (dom) { + LOG(dom, GB_LOG_ERROR, "Failed to open file '%s', %m\n", GB_LOGROTATE_PATH); + } else { + fprintf(stderr, "Failed to open file '%s', %m\n", GB_LOGROTATE_PATH); + } + return ret; + } + + ret = fseek(fp, 0L, SEEK_END); + if (ret == -1) { + ret = -errno; + if (dom) { + LOG(dom, GB_LOG_ERROR, "Failed to seek file '%s', %m\n", GB_LOGROTATE_PATH); + } else { + fprintf(stderr, "Failed to seek file '%s', %m\n", GB_LOGROTATE_PATH); + } + goto error; + } + + len = ftell(fp); + if (len == -1) { + ret = -errno; + if (dom) { + LOG(dom, GB_LOG_ERROR, "Failed to get the length of file '%s', %m\n", GB_LOGROTATE_PATH); + } else { + fprintf(stderr, "Failed to get the length of file '%s', %m\n", GB_LOGROTATE_PATH); + } + goto error; + } + + /* to make sure we have enough size */ + len += strlen(logDir) + 1; + if (GB_ALLOC_N(buf, len) < 0) { + ret = -ENOMEM; + goto error; + } + + p = buf; + fseek(fp, 0L, SEEK_SET); + while ((m = getline(&line, &n, fp)) != -1) { + if (strstr(line, "*.log") && strchr(line, '{')) { + m = sprintf(p, "%s/*.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(GB_LOGROTATE_PATH, 0L); + ret = fwrite(buf, 1, len, fp); + if (ret != len) { + if (dom) { + LOG(dom, GB_LOG_ERROR, "Failed to update '%s', %m\n", GB_LOGROTATE_PATH); + } else { + fprintf(stderr, "Failed to update '%s', %m\n", GB_LOGROTATE_PATH); + } + goto error; + } + + ret = 0; + error: + if (fp) { + fclose(fp); + } + GB_FREE(buf); + GB_FREE(line); + return ret; +} + + static int -initLogDirAndFiles(char *newLogDir) +initLogDirAndFiles(const char *dom, char *newLogDir) { char *logDir = NULL; char *tmpLogDir = NULL; int ret = 0; + bool def = false; /* @@ -278,6 +368,7 @@ initLogDirAndFiles(char *newLogDir) } if (!logDir) { + def = true; logDir = GB_LOGDIR_DEF; } } @@ -310,22 +401,27 @@ initLogDirAndFiles(char *newLogDir) unlock: UNLOCK(gbConf.lock); + + if (!def) { + glusterLogrotateConfigSet(dom, gbConf.logDir); + } + GB_FREE(tmpLogDir); return ret; } int -initLogging(void) +initLogging(const char *dom) { - return initLogDirAndFiles(NULL); + return initLogDirAndFiles(dom, NULL); } bool glusterBlockSetLogDir(char *logDir) { - return initLogDirAndFiles(logDir); + return initLogDirAndFiles("mgmt", logDir); } diff --git a/utils/utils.h b/utils/utils.h index 4211ee8..ed467ff 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -29,6 +29,7 @@ # include "list.h" +# define GB_LOGROTATE_PATH "/etc/logrotate.d/gluster-block" # define GB_LOGDIR_DEF DATADIR "/log/gluster-block" # define GB_INFODIR DATADIR "/run" @@ -596,7 +597,7 @@ void fetchGlfsVolServerFromEnv(void); bool glusterBlockSetLogDir(char *logDir); -int initLogging(void); +int initLogging(const char *dom); int gbRunnerExitStatus(int exitStatus);