diff options
author | Nick Mathewson <nickm@torproject.org> | 2019-10-30 09:28:34 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2019-11-07 07:28:43 -0500 |
commit | 7ac4f9d5ec1b31a0d4b76ab62c1afc039c8fe627 (patch) | |
tree | 865fb327567676c3c84cc25c7b2ab119f0ec837e /src/app | |
parent | a7cfddc8d18c39be8fb212ee2a96da2d1905d9c8 (diff) | |
download | tor-7ac4f9d5ec1b31a0d4b76ab62c1afc039c8fe627.tar.gz tor-7ac4f9d5ec1b31a0d4b76ab62c1afc039c8fe627.zip |
Give subsystems optional config formats and state formats.
The formats, when provided, are now added to the global config_mgr_t
objects.
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/config/config.c | 2 | ||||
-rw-r--r-- | src/app/config/statefile.c | 3 | ||||
-rw-r--r-- | src/app/main/subsysmgr.c | 68 | ||||
-rw-r--r-- | src/app/main/subsysmgr.h | 4 |
4 files changed, 76 insertions, 1 deletions
diff --git a/src/app/config/config.c b/src/app/config/config.c index 5f9a55ed17..7317a5d2f1 100644 --- a/src/app/config/config.c +++ b/src/app/config/config.c @@ -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; diff --git a/src/app/config/statefile.c b/src/app/config/statefile.c index db4d780a78..f6b0915da4 100644 --- a/src/app/config/statefile.c +++ b/src/app/config/statefile.c @@ -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; diff --git a/src/app/main/subsysmgr.c b/src/app/main/subsysmgr.c index 5fc298dbf6..e5c76b8e55 100644 --- a/src/app/main/subsysmgr.c +++ b/src/app/main/subsysmgr.c @@ -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; +} diff --git a/src/app/main/subsysmgr.h b/src/app/main/subsysmgr.h index f8bc83e0ad..54193f9649 100644 --- a/src/app/main/subsysmgr.h +++ b/src/app/main/subsysmgr.h @@ -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) */ |