Add a CFLG_IMMUTABLE flag to mark a field as unchangeable.

This commit is contained in:
Nick Mathewson 2019-10-30 15:00:53 -04:00
parent f579541f13
commit 2a2c7049b8
2 changed files with 48 additions and 2 deletions

View File

@ -178,6 +178,11 @@ typedef struct struct_magic_decl_t {
* however, setting them appends to their old value. * however, setting them appends to their old value.
*/ */
#define CFLG_NOREPLACE (1u<<5) #define CFLG_NOREPLACE (1u<<5)
/**
* Flag to indicate that an option or type cannot be changed while Tor is
* running.
**/
#define CFLG_IMMUTABLE (1u<<6)
/** /**
* A group of flags that should be set on all obsolete options and types. * A group of flags that should be set on all obsolete options and types.

View File

@ -1153,6 +1153,41 @@ config_init(const config_mgr_t *mgr, void *options)
} SMARTLIST_FOREACH_END(mv); } SMARTLIST_FOREACH_END(mv);
} }
/**
* Helper for config_validate_single: see whether any immutable option
* has changed between old_options and new_options.
*
* On success return 0; on failure set *msg_out to a newly allocated
* string explaining what is wrong, and return -1.
*/
static int
config_check_immutable_flags(const config_format_t *fmt,
const void *old_options,
const void *new_options,
char **msg_out)
{
tor_assert(fmt);
tor_assert(new_options);
if (BUG(! old_options))
return 0;
unsigned i;
for (i = 0; fmt->vars[i].member.name; ++i) {
const config_var_t *v = &fmt->vars[i];
if (! config_var_has_flag(v, CFLG_IMMUTABLE))
continue;
if (! struct_var_eq(old_options, new_options, &v->member)) {
tor_asprintf(msg_out,
"While Tor is running, changing %s is not allowed",
v->member.name);
return -1;
}
}
return 0;
}
/** /**
* Normalize and validate a single object `options` within a configuration * Normalize and validate a single object `options` within a configuration
* suite, according to its format. `options` may be modified as appropriate * suite, according to its format. `options` may be modified as appropriate
@ -1189,11 +1224,17 @@ config_validate_single(const config_format_t *fmt,
} }
} }
if (fmt->check_transition_fn && old_options) { if (old_options) {
if (config_check_immutable_flags(fmt, old_options, options, msg_out) < 0) {
return VSTAT_TRANSITION_ERR;
}
if (fmt->check_transition_fn) {
if (fmt->check_transition_fn(old_options, options, msg_out) < 0) { if (fmt->check_transition_fn(old_options, options, msg_out) < 0) {
return VSTAT_TRANSITION_ERR; return VSTAT_TRANSITION_ERR;
} }
} }
}
if (fmt->post_normalize_fn) { if (fmt->post_normalize_fn) {
if (fmt->post_normalize_fn(options, msg_out) < 0) { if (fmt->post_normalize_fn(options, msg_out) < 0) {