Skip to content

Commit

Permalink
logrotate: update the logdir path whenever it changes
Browse files Browse the repository at this point in the history
Signed-off-by: Xiubo Li <[email protected]>
  • Loading branch information
lxbsz committed Nov 21, 2018
1 parent 3166545 commit 83b39e9
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion libtcmu_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
Expand Down Expand Up @@ -524,6 +525,76 @@ 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;
int ret, m, len;
size_t n;
FILE *fp;
char *p;

fp = fopen(TCMU_LOGROTATE_PATH, "r+");
if (fp == NULL) {
tcmu_err("Failed to open file '%s', %m\n", TCMU_LOGROTATE_PATH);
return -errno;
}

ret = fseek(fp,0L,SEEK_END);
if (ret == -1) {
tcmu_err("Failed to seek file '%s', %m\n", TCMU_LOGROTATE_PATH);
ret = -errno;
goto error;
}

len = ftell(fp);
if (len == -1) {
tcmu_err("Failed to get the length of file '%s', %m\n", TCMU_LOGROTATE_PATH);
ret = -errno;
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';

fseek(fp, 0L, SEEK_SET);
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;
Expand All @@ -536,7 +607,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)
Expand Down

0 comments on commit 83b39e9

Please sign in to comment.