From e830f3418a05addb500e5fd1d9ce932fb46a7d32 Mon Sep 17 00:00:00 2001 From: Xiubo Li Date: Mon, 5 Feb 2018 20:18:27 -0500 Subject: [PATCH] config: add the logdir option dynamic reloading support 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 --- libtcmu_config.c | 18 ++++++++++++---- libtcmu_log.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- libtcmu_log.h | 3 ++- main.c | 2 +- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/libtcmu_config.c b/libtcmu_config.c index 65f8cfd9..4751e196 100644 --- a/libtcmu_config.c +++ b/libtcmu_config.c @@ -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 @@ -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 */ diff --git a/libtcmu_log.c b/libtcmu_log.c index a798d27d..6333371e 100644 --- a/libtcmu_log.c +++ b/libtcmu_log.c @@ -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; @@ -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; @@ -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) { @@ -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; @@ -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; } @@ -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++; } } @@ -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/ @@ -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); @@ -601,7 +638,7 @@ 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; @@ -609,7 +646,7 @@ bool tcmu_logdir_create(const char *path) 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) @@ -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"); diff --git a/libtcmu_log.h b/libtcmu_log.h index 681a3f0a..6d085360 100644 --- a/libtcmu_log.h +++ b/libtcmu_log.h @@ -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__);} diff --git a/main.c b/main.c index f5480fda..01acb561 100644 --- a/main.c +++ b/main.c @@ -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':