Skip to content

Commit

Permalink
config: add the logdir option dynamic reloading support
Browse files Browse the repository at this point in the history
NOTE: for now this couldn't support the glfs's logging setting, and
the glfs will use the old path.

Signed-off-by: Xiubo Li <xiubli@redhat.com>
  • Loading branch information
lxbsz committed Feb 6, 2018
1 parent 87307c7 commit e830f34
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
18 changes: 14 additions & 4 deletions libtcmu_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ do { \

static void tcmu_conf_set_options(struct tcmu_config *cfg, bool reloading)
{
int ret;

/* set log_level option */
TCMU_PARSE_CFG_INT(cfg, log_level);
if (cfg->log_level) {
tcmu_set_log_level(cfg->log_level);
}

/* set log_dir path option */
TCMU_PARSE_CFG_STR(cfg, log_dir_path);
if (!reloading) {
/* set log_dir path option */
TCMU_PARSE_CFG_STR(cfg, log_dir_path);
/*
* The priority of the logdir setting is:
* 1, --tcmu_log_dir/-l LOG_DIR_PATH
Expand All @@ -149,11 +151,19 @@ static void tcmu_conf_set_options(struct tcmu_config *cfg, bool reloading)
* 4, default /var/log/
*/
if (!tcmu_get_logdir())
tcmu_logdir_create(cfg->log_dir_path);
tcmu_logdir_create(cfg->log_dir_path, false);
else
tcmu_warn("The logdir option from the tcmu.conf will be ignored\n");
} else {
tcmu_warn("The logdir option is not supported by dynamic reloading for now!\n");
/*
* Here we asume that users want to change the
* log_dir_path without considering the priority
* mentioned above.
*/
tcmu_logdir_create(cfg->log_dir_path, true);
ret = tcmu_create_file_output(tcmu_get_log_level(), TCMU_LOG_FILENAME, true);
if (ret < 0)
tcmu_err("create file output error \n");
}

/* add your new config options */
Expand Down
53 changes: 45 additions & 8 deletions libtcmu_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct log_output {
char *name;
void *data;
tcmu_log_destination dest;
bool enabled;
};

static int tcmu_log_level = TCMU_LOG_INFO;
Expand Down Expand Up @@ -100,7 +101,7 @@ bool tcmu_logdir_getenv(void)
if (!log_path)
return true;

if (!tcmu_logdir_create(log_path))
if (!tcmu_logdir_create(log_path, false))
return false;

return true;
Expand Down Expand Up @@ -275,12 +276,26 @@ static int append_output(log_output_fn_t output_fn, log_close_fn_t close_fn, voi
output.priority = pri;
output.dest = dest;
output.name = ndup;
output.enabled = true;

darray_append(logbuf->outputs, output);

return 0;
}

static void log_output_disable(const tcmu_log_destination dest)
{
struct log_output *output;

/* This will just find the first one and disable it. */
darray_foreach(output, logbuf->outputs) {
if (output->dest == dest) {
output->enabled = false;
break;
}
}
}

static int output_to_syslog(int pri, const char *timestamp,
const char *str, void *data)
{
Expand Down Expand Up @@ -383,7 +398,7 @@ static int create_stdout_output(int pri)
return 0;
}

static int create_file_output(int pri, const char *filename)
int tcmu_create_file_output(int pri, const char *filename, bool reloading)
{
char log_file_path[PATH_MAX];
int fd, ret;
Expand All @@ -408,6 +423,10 @@ static int create_file_output(int pri, const char *filename)
return ret;
}

/* Disable the old entries */
if (reloading)
log_output_disable(TCMU_LOG_TO_FILE);

return 0;
}

Expand All @@ -416,15 +435,30 @@ static void log_output(int pri, const char *msg)
struct log_output *output;
char timestamp[TCMU_TIME_STRING_BUFLEN] = {0, };
int ret;
int i = 0;

ret = time_string_now(timestamp);
if (ret < 0)
return;

darray_foreach (output, logbuf->outputs) {
if (pri <= output->priority) {
output->output_fn(pri, timestamp, msg, output->data);
if (output->enabled) {
if (pri <= output->priority)
output->output_fn(pri, timestamp, msg, output->data);
} else {
/*
* We just close and free the resource here to make
* sure no outputing operation is in process.
*/
if (output->close_fn != NULL)
output->close_fn(output->data);
if (output->name != NULL)
free(output->name);

darray_remove(logbuf->outputs, i);
continue;
}
i++;
}
}

Expand Down Expand Up @@ -533,7 +567,7 @@ char *tcmu_get_logdir(void)
return tcmu_log_dir;
}

static char *tcmu_alloc_and_set_log_dir(const char *log_dir)
static char *tcmu_alloc_and_set_log_dir(const char *log_dir, bool reloading)
{
/*
* Do nothing here and will use the /var/log/
Expand All @@ -542,6 +576,9 @@ static char *tcmu_alloc_and_set_log_dir(const char *log_dir)
if (!log_dir)
return NULL;

if (reloading && tcmu_log_dir)
free(tcmu_log_dir);

tcmu_log_dir = strdup(log_dir);
if (!tcmu_log_dir)
tcmu_err("Failed to copy log dir: %s\n", log_dir);
Expand Down Expand Up @@ -601,15 +638,15 @@ static int tcmu_mkdirs(const char *pathname)
return tcmu_mkdir(path);
}

bool tcmu_logdir_create(const char *path)
bool tcmu_logdir_create(const char *path, bool reloading)
{
if (!tcmu_logdir_check(path))
return false;

if (!tcmu_mkdirs(path))
return false;

return !!tcmu_alloc_and_set_log_dir(path);
return !!tcmu_alloc_and_set_log_dir(path, reloading);
}

int tcmu_make_absolute_logfile(char *path, const char *filename)
Expand Down Expand Up @@ -646,7 +683,7 @@ int tcmu_setup_log(void)
if (ret < 0)
tcmu_err("create stdout output error \n");

ret = create_file_output(TCMU_LOG_DEBUG, TCMU_LOG_FILENAME);
ret = tcmu_create_file_output(TCMU_LOG_DEBUG, TCMU_LOG_FILENAME, false);
if (ret < 0)
tcmu_err("create file output error \n");

Expand Down
3 changes: 2 additions & 1 deletion libtcmu_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ void tcmu_dbg_scsi_cmd_message(struct tcmu_device *dev, const char *funcname, in
char *tcmu_get_logdir(void);
void tcmu_logdir_destroy(void);
bool tcmu_logdir_getenv(void);
bool tcmu_logdir_create(const char *path);
bool tcmu_logdir_create(const char *path, bool reloading);
int tcmu_make_absolute_logfile(char *path, const char *filename);
int tcmu_create_file_output(int pri, const char *filename, bool reloading);


#define tcmu_dev_err(dev, ...) {tcmu_err_message(dev, __func__, __LINE__, __VA_ARGS__);}
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ int main(int argc, char **argv)
}
break;
case 'l':
if (!tcmu_logdir_create(optarg))
if (!tcmu_logdir_create(optarg, false))
goto free_opt;
break;
case 'f':
Expand Down

0 comments on commit e830f34

Please sign in to comment.