diff options
author | Nick Mathewson <nickm@torproject.org> | 2013-09-03 10:30:50 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2013-09-13 12:36:40 -0400 |
commit | aac4f30d234491d8325b55918647242bb7c40537 (patch) | |
tree | baa59071186b654c82d52642ff970777d7050559 /src | |
parent | 7972af7073a621f3affa69d04bd05e0e432d7309 (diff) | |
download | tor-aac4f30d234491d8325b55918647242bb7c40537.tar tor-aac4f30d234491d8325b55918647242bb7c40537.tar.gz |
Add a --dump-config option to help testing option parsing.
Diffstat (limited to 'src')
-rw-r--r-- | src/or/config.c | 31 | ||||
-rw-r--r-- | src/or/confparse.c | 4 | ||||
-rw-r--r-- | src/or/confparse.h | 2 | ||||
-rw-r--r-- | src/or/control.c | 2 | ||||
-rw-r--r-- | src/or/main.c | 39 | ||||
-rw-r--r-- | src/or/or.h | 2 |
6 files changed, 68 insertions, 12 deletions
diff --git a/src/or/config.c b/src/or/config.c index 40bdf91af..2a86dac87 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -1801,6 +1801,7 @@ static const struct { { "-f", 1 }, { "--defaults-torrc", 1 }, { "--hash-password", 1 }, + { "--dump-config", 1 }, { "--list-fingerprint", 0 }, { "--verify-config", 0 }, { "--ignore-missing-torrc", 0 }, @@ -2268,10 +2269,29 @@ options_init(or_options_t *options) * include options that are the same as Tor's defaults. */ char * -options_dump(const or_options_t *options, int minimal) +options_dump(const or_options_t *options, int how_to_dump) { - return config_dump(&options_format, global_default_options, - options, minimal, 0); + const or_options_t *use_defaults; + int minimal; + switch (how_to_dump) { + case OPTIONS_DUMP_MINIMAL: + use_defaults = global_default_options; + minimal = 1; + break; + case OPTIONS_DUMP_DEFAULTS: + use_defaults = NULL; + minimal = 1; + break; + case OPTIONS_DUMP_ALL: + use_defaults = NULL; + minimal = 0; + break; + default: + log_warn(LD_BUG, "Bogus value for how_to_dump==%d", how_to_dump); + return NULL; + } + + return config_dump(&options_format, use_defaults, options, minimal, 0); } /** Return 0 if every element of sl is a string holding a decimal @@ -3894,6 +3914,9 @@ options_init_from_torrc(int argc, char **argv) } else if (!strcmp(p_index->key, "--hash-password")) { command = CMD_HASH_PASSWORD; command_arg = p_index->value; + } else if (!strcmp(p_index->key, "--dump-config")) { + command = CMD_DUMP_CONFIG; + command_arg = p_index->value; } else if (!strcmp(p_index->key, "--verify-config")) { command = CMD_VERIFY_CONFIG; } @@ -6119,7 +6142,7 @@ write_configuration_file(const char *fname, const or_options_t *options) return -1; } - if (!(new_conf = options_dump(options, 1))) { + if (!(new_conf = options_dump(options, OPTIONS_DUMP_MINIMAL))) { log_warn(LD_BUG, "Couldn't get configuration string"); goto err; } diff --git a/src/or/confparse.c b/src/or/confparse.c index 12ab0e455..f2ada4f42 100644 --- a/src/or/confparse.c +++ b/src/or/confparse.c @@ -1074,8 +1074,8 @@ config_dump(const config_format_t *fmt, const void *default_options, /* XXX use a 1 here so we don't add a new log line while dumping */ if (default_options == NULL) { - if (fmt->validate_fn(NULL, defaults_tmp, 1, &msg) < 0) { - log_err(LD_BUG, "Failed to validate default config."); + if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) { + log_err(LD_BUG, "Failed to validate default config: %s", msg); tor_free(msg); tor_assert(0); } diff --git a/src/or/confparse.h b/src/or/confparse.h index a1b627725..2cd6c49a2 100644 --- a/src/or/confparse.h +++ b/src/or/confparse.h @@ -71,7 +71,7 @@ typedef struct config_var_description_t { /** Type of a callback to validate whether a given configuration is * well-formed and consistent. See options_trial_assign() for documentation * of arguments. */ -typedef int (*validate_fn_t)(void*,void*,int,char**); +typedef int (*validate_fn_t)(void*,void*,void*,int,char**); /** Information on the keys, value types, key-to-struct-member mappings, * variable descriptions, validation functions, and abbreviations for a diff --git a/src/or/control.c b/src/or/control.c index 7034605c2..e97c18d89 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -1406,7 +1406,7 @@ getinfo_helper_misc(control_connection_t *conn, const char *question, } else if (!strcmp(question, "config-defaults-file")) { *answer = tor_strdup(get_torrc_fname(1)); } else if (!strcmp(question, "config-text")) { - *answer = options_dump(get_options(), 1); + *answer = options_dump(get_options(), OPTIONS_DUMP_MINIMAL); } else if (!strcmp(question, "info/names")) { *answer = list_getinfo_options(); } else if (!strcmp(question, "dormant")) { diff --git a/src/or/main.c b/src/or/main.c index 598d099d2..48d815554 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -2343,10 +2343,12 @@ tor_init(int argc, char *argv[]) for (cl = cmdline_opts; cl; cl = cl->next) { if (!strcmp(cl->key, "--hush")) quiet = 1; - if (!strcmp(cl->key, "--quiet")) + if (!strcmp(cl->key, "--quiet") || + !strcmp(cl->key, "--dump-config")) quiet = 2; - /* --version, --digests, and --help imply --quiet */ + /* --version, --digests, and --help imply --husth */ if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") || + !strcmp(cl->key, "--list-torrc-options") || !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) quiet = 1; } @@ -2597,7 +2599,7 @@ do_list_fingerprint(void) const char *nickname = get_options()->Nickname; if (!server_mode(get_options())) { log_err(LD_GENERAL, - "Clients don't have long-term identity keys. Exiting.\n"); + "Clients don't have long-term identity keys. Exiting."); return -1; } tor_assert(nickname); @@ -2635,6 +2637,34 @@ do_hash_password(void) printf("16:%s\n",output); } +/** Entry point for configuration dumping: write the configuration to + * stdout. */ +static int +do_dump_config(void) +{ + const or_options_t *options = get_options(); + const char *arg = options->command_arg; + int how; + char *opts; + if (!strcmp(arg, "short")) { + how = OPTIONS_DUMP_MINIMAL; + } else if (!strcmp(arg, "non-builtin")) { + how = OPTIONS_DUMP_DEFAULTS; + } else if (!strcmp(arg, "full")) { + how = OPTIONS_DUMP_ALL; + } else { + printf("%s is not a recognized argument to --dump-config. " + "Please select 'short', 'non-builtin', or 'full'", arg); + return -1; + } + + opts = options_dump(options, how); + printf("%s", opts); + tor_free(opts); + + return 0; +} + #if defined (WINCE) int find_flashcard_path(PWCHAR path, size_t size) @@ -2752,6 +2782,9 @@ tor_main(int argc, char *argv[]) printf("Configuration was valid\n"); result = 0; break; + case CMD_DUMP_CONFIG: + result = do_dump_config(); + break; case CMD_RUN_UNITTESTS: /* only set by test.c */ default: log_warn(LD_BUG,"Illegal command number %d: internal error.", diff --git a/src/or/or.h b/src/or/or.h index 43e0212b8..bd038f783 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -3398,7 +3398,7 @@ typedef struct { /** What should the tor process actually do? */ enum { CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD, - CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS + CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS, CMD_DUMP_CONFIG } command; char *command_arg; /**< Argument for command-line option. */ |