Fix every place in config.c that knew about option_vars_.

Iterating over this array was once a good idea, but now that we are
going to have a separate structure for each submodule's
configuration variables, we should indirect through the config_mgr_t
object.
This commit is contained in:
Nick Mathewson 2019-07-22 16:02:32 -04:00
parent 89a3051365
commit 627ab9dba3
3 changed files with 46 additions and 18 deletions

View File

@ -2655,25 +2655,25 @@ print_usage(void)
static void
list_torrc_options(void)
{
int i;
for (i = 0; option_vars_[i].member.name; ++i) {
const config_var_t *var = &option_vars_[i];
smartlist_t *vars = config_mgr_list_vars(get_options_mgr());
SMARTLIST_FOREACH_BEGIN(vars, const config_var_t *, var) {
if (! config_var_is_settable(var)) {
/* This variable cannot be set, or cannot be set by this name. */
continue;
}
printf("%s\n", var->member.name);
}
} SMARTLIST_FOREACH_END(var);
smartlist_free(vars);
}
/** Print all deprecated but non-obsolete torrc options. */
static void
list_deprecated_options(void)
{
const config_deprecation_t *d;
for (d = option_deprecation_notes_; d->name; ++d) {
printf("%s\n", d->name);
}
smartlist_t *deps = config_mgr_list_deprecated_vars(get_options_mgr());
SMARTLIST_FOREACH(deps, const char *, name,
printf("%s\n", name));
smartlist_free(deps);
}
/** Print all compile-time modules and their enabled/disabled status. */
@ -8125,34 +8125,34 @@ getinfo_helper_config(control_connection_t *conn,
(void) errmsg;
if (!strcmp(question, "config/names")) {
smartlist_t *sl = smartlist_new();
int i;
for (i = 0; option_vars_[i].member.name; ++i) {
const config_var_t *var = &option_vars_[i];
smartlist_t *vars = config_mgr_list_vars(get_options_mgr());
SMARTLIST_FOREACH_BEGIN(vars, const config_var_t *, var) {
/* don't tell controller about triple-underscore options */
if (option_vars_[i].flags & CVFLAG_INVISIBLE)
if (var->flags & CVFLAG_INVISIBLE)
continue;
const char *type = struct_var_get_typename(&var->member);
if (!type)
continue;
smartlist_add_asprintf(sl, "%s %s\n",var->member.name,type);
}
} SMARTLIST_FOREACH_END(var);
*answer = smartlist_join_strings(sl, "", 0, NULL);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
smartlist_free(sl);
smartlist_free(vars);
} else if (!strcmp(question, "config/defaults")) {
smartlist_t *sl = smartlist_new();
int dirauth_lines_seen = 0, fallback_lines_seen = 0;
for (int i = 0; option_vars_[i].member.name; ++i) {
const config_var_t *var = &option_vars_[i];
smartlist_t *vars = config_mgr_list_vars(get_options_mgr());
SMARTLIST_FOREACH_BEGIN(vars, const config_var_t *, var) {
if (var->initvalue != NULL) {
if (strcmp(option_vars_[i].member.name, "DirAuthority") == 0) {
if (strcmp(var->member.name, "DirAuthority") == 0) {
/*
* Count dirauth lines we have a default for; we'll use the
* count later to decide whether to add the defaults manually
*/
++dirauth_lines_seen;
}
if (strcmp(option_vars_[i].member.name, "FallbackDir") == 0) {
if (strcmp(var->member.name, "FallbackDir") == 0) {
/*
* Similarly count fallback lines, so that we can decided later
* to add the defaults manually.
@ -8163,7 +8163,8 @@ getinfo_helper_config(control_connection_t *conn,
smartlist_add_asprintf(sl, "%s %s\n",var->member.name,val);
tor_free(val);
}
}
} SMARTLIST_FOREACH_END(var);
smartlist_free(vars);
if (dirauth_lines_seen == 0) {
/*

View File

@ -175,6 +175,31 @@ config_mgr_free_(config_mgr_t *mgr)
tor_free(mgr);
}
/** Return a new smartlist_t containing a config_var_t for every variable that
* <b>mgr</b> knows about. The elements of this smartlist do not need
* to be freed. */
smartlist_t *
config_mgr_list_vars(const config_mgr_t *mgr)
{
smartlist_t *result = smartlist_new();
tor_assert(mgr);
SMARTLIST_FOREACH(mgr->all_vars, managed_var_t *, mv,
smartlist_add(result, (void*) mv->cvar));
return result;
}
/** Return a new smartlist_t containing the names of all deprecated variables.
* The elements of this smartlist do not need to be freed. */
smartlist_t *
config_mgr_list_deprecated_vars(const config_mgr_t *mgr)
{
smartlist_t *result = smartlist_new();
tor_assert(mgr);
SMARTLIST_FOREACH(mgr->all_deprecations, config_deprecation_t *, d,
smartlist_add(result, &d->name));
return result;
}
/** Allocate an empty configuration object of a given format type. */
void *
config_new(const config_mgr_t *mgr)

View File

@ -75,6 +75,8 @@ config_mgr_t *config_mgr_new(const config_format_t *toplevel_fmt);
void config_mgr_free_(config_mgr_t *mgr);
#define config_mgr_free(mgr) \
FREE_AND_NULL(config_mgr_t, config_mgr_free_, (mgr))
struct smartlist_t *config_mgr_list_vars(const config_mgr_t *mgr);
struct smartlist_t *config_mgr_list_deprecated_vars(const config_mgr_t *mgr);
/** Macro: assert that <b>cfg</b> has the right magic field for format
* <b>fmt</b>. */