Give subsystems optional config formats and state formats.

The formats, when provided, are now added to the global config_mgr_t
objects.
This commit is contained in:
Nick Mathewson 2019-10-30 09:28:34 -04:00
parent a7cfddc8d1
commit 7ac4f9d5ec
5 changed files with 89 additions and 1 deletions

View file

@ -918,6 +918,8 @@ get_options_mgr(void)
{
if (PREDICT_UNLIKELY(options_mgr == NULL)) {
options_mgr = config_mgr_new(&options_format);
int rv = subsystems_register_options_formats(options_mgr);
tor_assert(rv == 0);
config_mgr_freeze(options_mgr);
}
return options_mgr;

View file

@ -45,6 +45,7 @@
#include "feature/relay/routermode.h"
#include "lib/sandbox/sandbox.h"
#include "app/config/statefile.h"
#include "app/main/subsysmgr.h"
#include "lib/encoding/confline.h"
#include "lib/net/resolve.h"
#include "lib/version/torversion.h"
@ -180,6 +181,8 @@ get_state_mgr(void)
{
if (PREDICT_UNLIKELY(state_mgr == NULL)) {
state_mgr = config_mgr_new(&state_format);
int rv = subsystems_register_state_formats(state_mgr);
tor_assert(rv == 0);
config_mgr_freeze(state_mgr);
}
return state_mgr;

View file

@ -14,10 +14,12 @@
#include "orconfig.h"
#include "app/main/subsysmgr.h"
#include "lib/confmgt/confmgt.h"
#include "lib/dispatch/dispatch_naming.h"
#include "lib/dispatch/msgtypes.h"
#include "lib/err/torerr.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/pubsub/pubsub_build.h"
#include "lib/pubsub/pubsub_connect.h"
@ -37,6 +39,10 @@ static bool subsystem_array_validated = false;
typedef struct subsys_status_t {
/** True if the given subsystem is initialized. */
bool initialized;
/** Index for this subsystem's options object, or -1 for none. */
int options_idx;
/** Index for this subsystem's state object, or -1 for none. */
int state_idx;
} subsys_status_t;
/** An overestimate of the number of subsystems. */
@ -48,6 +54,18 @@ typedef struct subsys_status_t {
**/
static subsys_status_t sys_status[N_SYS_STATUS];
/** Set <b>status</b> to a default (not set-up) state. */
static void
subsys_status_clear(subsys_status_t *status)
{
if (!status)
return;
memset(status, 0, sizeof(*status));
status->initialized = false;
status->state_idx = -1;
status->options_idx = -1;
}
/**
* Exit with a raw assertion if the subsystems list is inconsistent;
* initialize the subsystem_initialized array.
@ -77,6 +95,8 @@ check_and_setup(void)
sys->name, i, sys->level, last_level);
raw_assert_unreached_msg("There is a bug in subsystem_list.c");
}
subsys_status_clear(&sys_status[i]);
last_level = sys->level;
}
@ -202,7 +222,7 @@ subsystems_shutdown_downto(int target_level)
log_debug(LD_GENERAL, "Shutting down %s", sys->name);
sys->shutdown();
}
sys_status[i].initialized = false;
subsys_status_clear(&sys_status[i]);
}
}
@ -268,3 +288,49 @@ subsystems_thread_cleanup(void)
}
}
}
/**
* Register all subsystem-declared options formats in <b>mgr</b>.
*
* Return 0 on success, -1 on failure.
**/
int
subsystems_register_options_formats(config_mgr_t *mgr)
{
tor_assert(mgr);
check_and_setup();
for (unsigned i = 0; i < n_tor_subsystems; ++i) {
const subsys_fns_t *sys = tor_subsystems[i];
if (sys->options_format) {
int options_idx = config_mgr_add_format(mgr, sys->options_format);
sys_status[i].options_idx = options_idx;
log_debug(LD_CONFIG, "Added options format for %s with index %d",
sys->name, options_idx);
}
}
return 0;
}
/**
* Register all subsystem-declared state formats in <b>mgr</b>.
*
* Return 0 on success, -1 on failure.
**/
int
subsystems_register_state_formats(config_mgr_t *mgr)
{
tor_assert(mgr);
check_and_setup();
for (unsigned i = 0; i < n_tor_subsystems; ++i) {
const subsys_fns_t *sys = tor_subsystems[i];
if (sys->state_format) {
int state_idx = config_mgr_add_format(mgr, sys->state_format);
sys_status[i].state_idx = state_idx;
log_debug(LD_CONFIG, "Added state format for %s with index %d",
sys->name, state_idx);
}
}
return 0;
}

View file

@ -31,4 +31,8 @@ void subsystems_prefork(void);
void subsystems_postfork(void);
void subsystems_thread_cleanup(void);
struct config_mgr_t;
int subsystems_register_options_formats(struct config_mgr_t *mgr);
int subsystems_register_state_formats(struct config_mgr_t *mgr);
#endif /* !defined(TOR_SUBSYSMGR_T) */

View file

@ -14,6 +14,7 @@
#include <stdbool.h>
struct pubsub_connector_t;
struct config_format_t;
/**
* A subsystem is a part of Tor that is initialized, shut down, configured,
@ -88,6 +89,18 @@ typedef struct subsys_fns_t {
**/
void (*shutdown)(void);
/**
* A config_format_t describing all of the torrc fields owned by this
* subsystem.
**/
const struct config_format_t *options_format;
/**
* A config_format_t describing all of the DataDir/state fields owned by
* this subsystem.
**/
const struct config_format_t *state_format;
} subsys_fns_t;
/**