From: Yehuda Sadeh Date: Mon, 2 Mar 2009 22:38:57 +0000 (-0800) Subject: conf: revised cmd line parsing and conf reading X-Git-Tag: v0.7~83 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b971b9c3aa9ea4f72b3992b166d28c7ac439c557;p=ceph.git conf: revised cmd line parsing and conf reading still need to set all the default initialization --- diff --git a/src/common/common_init.cc b/src/common/common_init.cc index 228bd5f7134..cebd329f7fd 100644 --- a/src/common/common_init.cc +++ b/src/common/common_init.cc @@ -6,6 +6,7 @@ void common_init(std::vector& args, bool open) { tls_init(); tls_get_val()->disable_assert = 0; + preparse_config_options(args, open); parse_config_options(args, open); } diff --git a/src/config.cc b/src/config.cc index 08eac2e930f..bac5d26ba46 100644 --- a/src/config.cc +++ b/src/config.cc @@ -185,7 +185,8 @@ std::map g_fake_kill_after; entity_addr_t g_my_addr; md_config_t g_debug_after_conf; - +md_config_t g_conf; +#if 0 md_config_t g_conf = { num_mon: 1, num_mds: 1, @@ -481,7 +482,7 @@ md_config_t g_conf = { bdbstore_transactional: false #endif // USE_OSBDB }; - +#endif #include #include @@ -587,6 +588,7 @@ void parse_config_option_string(string& s) *p++ = 0; while (*p && *p == ' ') p++; } + preparse_config_options(nargs, false); parse_config_options(nargs, false); } @@ -605,7 +607,7 @@ void sighup_handler(int signum) #define CF_READ_STR(section, var, inout) \ cf->read(section, var, (char **)&g_conf.inout, (char *)g_conf.inout) -void parse_config_file(ConfFile *cf, bool auto_update) +void parse_config_file0(ConfFile *cf, bool auto_update) { cf->set_auto_update(true); @@ -838,7 +840,7 @@ void parse_config_file(ConfFile *cf, bool auto_update) #endif } -void parse_config_options(std::vector& args, bool open) +void parse_config_options0(std::vector& args, bool open) { std::vector nargs; @@ -867,7 +869,6 @@ void parse_config_options(std::vector& args, bool open) if (strcmp(args[i],"--bind") == 0 && isarg) { assert_warn(parse_ip_port(args[++i], g_my_addr)); - exit(1); } else if (strcmp(args[i], "--nummon") == 0 && isarg) g_conf.num_mon = atoi(args[++i]); else if (strcmp(args[i], "--nummds") == 0 && isarg) @@ -1414,3 +1415,276 @@ void parse_config_options(std::vector& args, bool open) } +#define STRINGIFY(x) #x + +typedef enum { + NONE, INT, STR, DOUBLE, FLOAT, BOOL +} opt_type_t; + + + +struct config_option { + const char *section; + const char *conf_name; + const char *name; + void *val_ptr; + + const char *def_val; + opt_type_t type; + char char_option; // if any +}; + +#define OPTION(section, name, schar, type, def_val) \ + { STRINGIFY(section), NULL, STRINGIFY(name), \ + &g_conf.name, STRINGIFY(def_val), type, schar } + +static struct config_option config_optionsp[] = { + OPTION(debug, debug, 0, INT, 0), + OPTION(debug, debug_ms, 0, INT, 0), + OPTION(debug, debug_mds, 0, INT, 0), + OPTION(global, daemonize, 'd', BOOL, false), + OPTION(global, conf_file, 'c', STR, ceph.conf), + OPTION(mon, mon_lease, 'z', FLOAT, 1.129), + +}; + +static bool set_conf_val(void *field, opt_type_t type, const char *val) +{ + switch (type) { + case BOOL: + if (strcasecmp(val, "false") == 0) + *(bool *)field = false; + else if (strcasecmp(val, "true") == 0) + *(bool *)field = true; + else + *(bool *)field = (bool)atoi(val); + break; + case INT: + *(int *)field = atoi(val); + break; + case STR: + *(char **)field = strdup(val); + break; + case FLOAT: + *(float *)field = atof(val); + break; + case DOUBLE: + *(double *)field = strtod(val, NULL); + break; + default: + return false; + } + + return true; +} + +static void set_conf_name(config_option *opt) +{ + char *newsection = (char *)opt->section; + char *newconf = (char *)opt->name; + int i; + + if (opt->section[0] == 0) { + newsection = strdup("global"); + } + + if (strncmp(newsection, opt->name, strlen(newsection)) == 0) { + /* if key starts with the name of the section, remove name of the section + unless key equals to it */ + + if (strcmp(newsection, opt->name) == 0) + goto done; + + newconf = strdup(&opt->name[strlen(newsection)+1]); + } else { + newconf = strdup(opt->name); + } + + i = 0; + while (newconf[i]) { + if (newconf[i] == '_') + newconf[i] = ' '; + + ++i; + } + + done: + opt->section = newsection; + opt->conf_name = (const char *)newconf; +} + +static bool init_g_conf() +{ + int len = sizeof(config_optionsp)/sizeof(config_option); + int i; + config_option *opt; + + for (i = 0; ival_ptr, + opt->type, + opt->def_val)) { + cerr << "error initializing g_conf value num " << i << std::endl; + return false; + } + + set_conf_name(opt); + } + + return true; +} + +static bool g_conf_initialized = init_g_conf(); + +static bool cmd_is_char(const char *cmd) +{ + return ((cmd[0] == '-') && + cmd[1] && !cmd[2]); +} + +static bool cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos) +{ + unsigned int i; + unsigned int len = strlen(opt); + + *val_pos = 0; + + if (!*cmd) + return false; + + if (char_opt && cmd_is_char(cmd)) + return (char_opt == cmd[1]); + + if ((cmd[0] != '-') || (cmd[1] != '-')) + return false; + + for (i=0; i& args, bool open) +{ + int opt_len = sizeof(config_optionsp)/sizeof(config_option); + unsigned int val_pos; + + std::vector nargs; + for (unsigned i=0; i& args, int& argc, const char **&argv); +void preparse_config_options(std::vector& args, bool open=true); void parse_config_options(std::vector& args, bool open=true); void parse_config_option_string(string& s);