[tor-commits] [tor/master] Give subsystems optional config formats and state formats.

nickm at torproject.org nickm at torproject.org
Thu Nov 7 13:59:49 UTC 2019


commit 7ac4f9d5ec1b31a0d4b76ab62c1afc039c8fe627
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Oct 30 09:28:34 2019 -0400

    Give subsystems optional config formats and state formats.
    
    The formats, when provided, are now added to the global config_mgr_t
    objects.
---
 src/app/config/config.c    |  2 ++
 src/app/config/statefile.c |  3 ++
 src/app/main/subsysmgr.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++-
 src/app/main/subsysmgr.h   |  4 +++
 src/lib/subsys/subsys.h    | 13 +++++++++
 5 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/src/app/config/config.c b/src/app/config/config.c
index 5f9a55ed1..7317a5d2f 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 db4d780a7..f6b0915da 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 5fc298dbf..e5c76b8e5 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 f8bc83e0a..54193f964 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) */
diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h
index 91abdb7d7..1cb3fe94a 100644
--- a/src/lib/subsys/subsys.h
+++ b/src/lib/subsys/subsys.h
@@ -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;
 
 /**





More information about the tor-commits mailing list