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 30, 2018
1 parent cac41db commit 5771283
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cli/gluster-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

if(initLogging()) {
if(initLogging(NULL)) {
exit(EXIT_FAILURE);
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/gluster-blockd.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ main (int argc, char **argv)
int errnosv = 0;


if(initLogging()) {
if(initLogging("mgmt")) {
exit(EXIT_FAILURE);
}

Expand Down
104 changes: 100 additions & 4 deletions utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/*
Expand All @@ -278,6 +368,7 @@ initLogDirAndFiles(char *newLogDir)
}

if (!logDir) {
def = true;
logDir = GB_LOGDIR_DEF;
}
}
Expand Down Expand Up @@ -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);
}


Expand Down
3 changes: 2 additions & 1 deletion utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -601,7 +602,7 @@ void fetchGlfsVolServerFromEnv(void);

bool glusterBlockSetLogDir(char *logDir);

int initLogging(void);
int initLogging(const char *dom);

int gbRunnerExitStatus(int exitStatus);

Expand Down

0 comments on commit 5771283

Please sign in to comment.