From: Heming Zhao Date: Sat, 25 Sep 2021 22:21:40 +0800 Subject: [Cluster-devel] [PATCH 3/5] dlm_controld: add reload_config feature In-Reply-To: <20210925142142.1821-1-heming.zhao@suse.com> References: <20210925142142.1821-1-heming.zhao@suse.com> Message-ID: <20210925142142.1821-4-heming.zhao@suse.com> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Signed-off-by: Heming Zhao --- dlm_controld/action.c | 5 ++ dlm_controld/config.c | 119 ++++++++++++++++++++++++++++---- dlm_controld/dlm_controld.h | 1 + dlm_controld/dlm_daemon.h | 4 ++ dlm_controld/lib.c | 19 ++++++ dlm_controld/libdlmcontrol.h | 1 + dlm_controld/logging.c | 18 +++-- dlm_controld/main.c | 128 +++++++++++++++++++++-------------- dlm_tool/main.c | 21 +++++- 9 files changed, 247 insertions(+), 69 deletions(-) diff --git a/dlm_controld/action.c b/dlm_controld/action.c index 0eff27997e1c..baddaf81cb4f 100644 --- a/dlm_controld/action.c +++ b/dlm_controld/action.c @@ -766,6 +766,11 @@ static int set_configfs_cluster(const char *name, char *str, int num) return 0; } +int set_configfs_opt(const char *name, char *str, int num) +{ + return set_configfs_cluster(name, str, num); +} + #define NET_RMEM_DEFAULT 4194304 #define NET_RMEM_MAX 4194304 diff --git a/dlm_controld/config.c b/dlm_controld/config.c index 947480dabe85..91ca10a62b15 100644 --- a/dlm_controld/config.c +++ b/dlm_controld/config.c @@ -188,6 +188,55 @@ static void get_val_str(char *line, char *val_out) strcpy(val_out, val); } +inline static void reload_setting(int index) +{ + switch(index) { + case log_debug_ind: + set_configfs_opt("log_debug", NULL, opt(log_debug_ind)); + break; + case debug_logfile_ind: + set_logfile_priority(); + break; + default: + break; + } +} + +static void reset_opt_value(int index) +{ + struct dlm_option *o = &dlm_options[index]; + + /* config priority: cli, config file, default */ + + if (o->cli_set) { + o->use_int = o->cli_int; + o->use_uint = o->cli_uint; + o->use_str = o->cli_str; + + } else if (o->file_set) { + o->use_int = o->file_int; + o->use_uint = o->file_uint; + o->use_str = o->file_str; + + } else { + o->use_int = o->default_int; + o->use_uint = o->default_uint; + o->use_str = (char *)o->default_str; + } + + /* + * We don't handle reset value same as legacy value. + * + * i.e. + * 1. option abc default value is 0, while in dlm.conf abc=0. + * 2. Then remove abc from dlm.conf. + * 3. This function still call reload_setting(), and won't bypass this calling + * for no change. + */ + reload_setting(index); + return; +} + void set_opt_file(int update) { unsigned int uval = 0; @@ -195,7 +244,8 @@ void set_opt_file(int update) FILE *file; char line[MAX_LINE]; char str[MAX_LINE]; - int i, val = 0; + int i, val = 0, ind; + char scanned_dlm_opt[dlm_options_max]; if (!path_exists(CONF_FILE_PATH)) return; @@ -204,6 +254,12 @@ void set_opt_file(int update) if (!file) return; + /* In update mode, there is a little bit bother if one option ever set + * but later be removed or commented out */ + memset(scanned_dlm_opt, 0, sizeof(scanned_dlm_opt)); + scanned_dlm_opt[help_ind] = 1; + scanned_dlm_opt[version_ind] = 1; + while (fgets(line, MAX_LINE, file)) { if (line[0] == '#') continue; @@ -226,30 +282,31 @@ void set_opt_file(int update) str[i] = line[i]; } - o = get_dlm_option(str); + ind = get_ind_name(str); + if (ind < 0) + continue; + o = &dlm_options[ind]; if (!o) continue; - /* Doesn't make sense to set this in conf file. */ - if (!strcmp(str, "daemon_debug")) + scanned_dlm_opt[ind] = 1; + + /* In update flow, option item doesn't support reload. */ + if (update && !o->reload) continue; o->file_set++; if (!o->req_arg) { - /* ignore any = x */ - - o->file_int = 1; - - if (!o->cli_set) - o->use_int = o->file_int; - - log_debug("config file %s = %d cli_set %d use %d", - o->name, o->file_int, o->cli_set, o->use_int); + /* current only help & version are no_arg type, ignore */ + continue; } else if (o->req_arg == req_arg_int) { get_val_int(line, &val); + if (update && (o->file_int == val)) + continue; + o->file_int = val; if (!o->cli_set) @@ -261,6 +318,9 @@ void set_opt_file(int update) } else if (o->req_arg == req_arg_uint) { get_val_uint(line, &uval); + if (update && (o->file_uint == uval)) + continue; + o->file_uint = uval; if (!o->cli_set) @@ -271,8 +331,12 @@ void set_opt_file(int update) } else if (o->req_arg == req_arg_bool) { get_val_int(line, &val); + val = val ? 1 : 0; + + if (update && (o->file_int == val)) + continue; - o->file_int = val ? 1 : 0; + o->file_int = val; if (!o->cli_set) o->use_int = o->file_int; @@ -283,6 +347,11 @@ void set_opt_file(int update) memset(str, 0, sizeof(str)); get_val_str(line, str); + if (update && !strcmp(o->file_str, str)) + continue; + + if (o->file_str) + free(o->file_str); o->file_str = strdup(str); if (!o->cli_set) @@ -291,6 +360,28 @@ void set_opt_file(int update) log_debug("config file %s = %s cli_set %d use %s", o->name, o->file_str, o->cli_set, o->use_str); } + + if (update) + reload_setting(ind); + } + + if (update) { + /* handle commented out options */ + for (i=0; icli_set++; - if (!o->req_arg) { - /* "-x" has same effect as "-x 1" */ - o->cli_int = 1; - o->use_int = 1; + if (!o->req_arg || (o->req_arg == req_arg_bool)) { + bundled = 0; - /* save bundled, arg-less, single letters, e.g. -DKP */ + /* current for no_arg type, there is not possible to have bundled options. + * for req_arg_bool, bundled options, e.g. -DKP. all treat as "true". + * below code save bundled, arg-less, single letters */ if ((p[0] == '-') && isalpha(p[1]) && (strlen(p) > 2)) { for (b = 2; b < strlen(p) && blc < blc_max; b++) { if (!isalpha(p[b])) break; bundled_letters[blc++] = p[b]; + bundled = 1; } } - continue; + if (bundled) { + /* "-x" has same effect as "-x 1" */ + o->cli_int = 1; + o->use_int = 1; + continue; + } } arg_str = NULL; @@ -2024,17 +2036,31 @@ static void set_opt_cli(int argc, char **argv) } else { /* space separates arg from name or letter */ - if (i >= argc) { - fprintf(stderr, "option %s no arg", p); - exit(EXIT_FAILURE); + if (o->req_arg == req_arg_bool) { + /* bool type treat empty arg as true */ + if (i >= argc || argv[i][0] == '-') + arg_str = bool_str; + else + arg_str = argv[i++]; + } else { + if (i >= argc) { + fprintf(stderr, "option %s no arg\n", p); + exit(EXIT_FAILURE); + } + arg_str = argv[i++]; } - arg_str = argv[i++]; } if (!arg_str || arg_str[0] == '-' || arg_str[0] == '\0') { - fprintf(stderr, "option %s requires arg", p); + fprintf(stderr, "option %s requires arg\n", p); exit(EXIT_FAILURE); } + if ((o->req_arg != req_arg_str) && !strtol(arg_str, &endptr, 10)) { + if (endptr == arg_str) { + fprintf(stderr, "option %s requires digit number\n", p); + exit(EXIT_FAILURE); + } + } if (o->req_arg == req_arg_str) { o->cli_str = strdup(arg_str); @@ -2059,6 +2085,7 @@ static void set_opt_cli(int argc, char **argv) fprintf(stderr, "unknown option char %c\n", bundled_letters[i]); exit(EXIT_FAILURE); } + /* bundled letter must be bool type, treat it with "true" value */ o = &dlm_options[ind]; o->cli_set++; o->cli_int = 1; @@ -2109,8 +2136,11 @@ int main(int argc, char **argv) * - explicit cli setting will override default, * - explicit file setting will override default * - explicit file setting will not override explicit cli setting + * + * "dlm reload_config" will trigger to reload config file, and + * reload action also follows the rule: not override explicit + * cli setting */ - set_opt_defaults(); set_opt_cli(argc, argv); set_opt_file(0); @@ -2128,7 +2158,7 @@ int main(int argc, char **argv) INIT_LIST_HEAD(&run_ops); init_daemon(); - if (!opt(daemon_debug_ind) && !opt(foreground_ind)) { + if (!opt(foreground_ind)) { if (daemon(0, 0) < 0) { perror("daemon error"); exit(EXIT_FAILURE); diff --git a/dlm_tool/main.c b/dlm_tool/main.c index 53d9336d59c6..07f020d71a66 100644 --- a/dlm_tool/main.c +++ b/dlm_tool/main.c @@ -47,6 +47,7 @@ #define OP_RUN_CANCEL 17 #define OP_RUN_LIST 18 #define OP_DUMP_RUN 19 +#define OP_RELOAD_CONFIG 20 static char *prog_name; static char *lsname; @@ -196,7 +197,7 @@ static void print_usage(void) printf("dlm_tool [command] [options] [name]\n"); printf("\n"); printf("Commands:\n"); - printf("ls, status, dump, dump_config, fence_ack\n"); + printf("ls, status, dump, dump_config, reload_config, fence_ack\n"); printf("log_plock, plocks\n"); printf("join, leave, lockdebug\n"); printf("run, run_start, run_check, run_cancel, run_list\n"); @@ -363,6 +364,12 @@ static void decode_arguments(int argc, char **argv) opt_ind = optind + 1; need_lsname = 0; break; + } else if (!strncmp(argv[optind], "reload_config", 13) && + (strlen(argv[optind]) == 13)) { + operation = OP_RELOAD_CONFIG; + opt_ind = optind + 1; + need_lsname = 0; + break; } else if (!strncmp(argv[optind], "plocks", 6) && (strlen(argv[optind]) == 6)) { operation = OP_PLOCKS; @@ -1472,6 +1479,14 @@ static void do_dump(int op) printf("\n"); } +static void do_reload_config(void) +{ + if (dlmc_reload_config() < 0) + printf("Issue reload_config failed.\n"); + else + printf("Issue reload_config done.\n"); +} + static void do_log_plock(void) { char buf[DLMC_DUMP_SIZE]; @@ -1570,6 +1585,10 @@ int main(int argc, char **argv) do_dump(operation); break; + case OP_RELOAD_CONFIG: + do_reload_config(); + break; + case OP_LOG_PLOCK: do_log_plock(); break; -- 2.32.0