[tor-commits] [tor/master] Make a config_suite_t type to hold multiple config sub-objects

dgoulet at torproject.org dgoulet at torproject.org
Wed Sep 4 14:39:08 UTC 2019


commit 38b770bbbb37aef6cd3cef5fd6f425cd951affe2
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Jul 23 11:32:52 2019 -0400

    Make a config_suite_t type to hold multiple config sub-objects
    
    Right now, it doesn't do anything; this patch is meant to make sure
    that we're doing memory management correctly.
---
 src/app/config/config.c                   |  1 +
 src/app/config/confparse.c                | 49 +++++++++++++++++++++++++++++++
 src/app/config/confparse.h                |  6 ++++
 src/app/config/or_options_st.h            |  4 +++
 src/app/config/or_state_st.h              |  4 +++
 src/app/config/statefile.c                |  1 +
 src/feature/dirauth/shared_random_state.c |  1 +
 src/test/test_confparse.c                 |  2 ++
 8 files changed, 68 insertions(+)

diff --git a/src/app/config/config.c b/src/app/config/config.c
index cdf5fa266..bff403a99 100644
--- a/src/app/config/config.c
+++ b/src/app/config/config.c
@@ -864,6 +864,7 @@ static const config_format_t options_format = {
   options_validate_cb,
   options_clear_cb,
   NULL,
+  offsetof(or_options_t, subconfigs_),
 };
 
 /*
diff --git a/src/app/config/confparse.c b/src/app/config/confparse.c
index 8e3ad7faf..d94a1d9ec 100644
--- a/src/app/config/confparse.c
+++ b/src/app/config/confparse.c
@@ -72,6 +72,36 @@ managed_var_free_(managed_var_t *mv)
 #define managed_var_free(mv) \
   FREE_AND_NULL(managed_var_t, managed_var_free_, (mv))
 
+struct config_suite_t {
+  /* NOTE: This object isn't implemented yet; it's just a placeholder
+   * to make sure we have our memory menagement right.
+   */
+  int foo;
+};
+
+/**
+ * Allocate a new empty config_suite_t.
+ **/
+static config_suite_t *
+config_suite_new(void)
+{
+  config_suite_t *suite = tor_malloc_zero(sizeof(config_suite_t));
+  return suite;
+}
+
+/** Release all storage held by a config_suite_t.  (Does not free
+ * any configuration objects it holds; the caller must do that first.) */
+static void
+config_suite_free_(config_suite_t *suite)
+{
+  if (!suite)
+    return;
+  tor_free(suite);
+}
+
+#define config_suite_free(suite) \
+  FREE_AND_NULL(config_suite_t, config_suite_free_, (suite))
+
 struct config_mgr_t {
   /** The 'top-level' configuration format.  This one is used for legacy
    * options that have not yet been assigned to different sub-modules.
@@ -152,6 +182,16 @@ config_mgr_register_fmt(config_mgr_t *mgr,
   }
 }
 
+/** Return a pointer to the config_suite_t * pointer inside a
+ * configuration object; returns NULL if there is no such member. */
+static inline config_suite_t **
+config_mgr_get_suite_ptr(const config_mgr_t *mgr, void *toplevel)
+{
+  if (mgr->toplevel->config_suite_offset < 0)
+    return NULL;
+  return STRUCT_VAR_P(toplevel, mgr->toplevel->config_suite_offset);
+}
+
 /**
  * Return a pointer to the configuration object within <b>toplevel</b> whose
  * index is <b>idx</b>.
@@ -272,6 +312,10 @@ config_new(const config_mgr_t *mgr)
   const config_format_t *fmt = mgr->toplevel;
   void *opts = tor_malloc_zero(fmt->size);
   struct_set_magic(opts, &mgr->toplevel_magic);
+  config_suite_t **suitep = config_mgr_get_suite_ptr(mgr, opts);
+  if (suitep) {
+    *suitep = config_suite_new();
+  }
   CONFIG_CHECK(mgr, opts);
   return opts;
 }
@@ -802,6 +846,11 @@ config_free_(const config_mgr_t *mgr, void *options)
     config_free_lines(*linep);
     *linep = NULL;
   }
+
+  config_suite_t **suitep = config_mgr_get_suite_ptr(mgr, options);
+  if (suitep)
+    config_suite_free(*suitep);
+
   tor_free(options);
 }
 
diff --git a/src/app/config/confparse.h b/src/app/config/confparse.h
index 2e1a4b4f5..c18e85423 100644
--- a/src/app/config/confparse.h
+++ b/src/app/config/confparse.h
@@ -61,6 +61,9 @@ typedef struct config_format_t {
   /** If present, extra denotes a LINELIST variable for unrecognized
    * lines.  Otherwise, unrecognized lines are an error. */
   const struct_member_t *extra;
+  /** The position of a config_suite_t pointer within the toplevel object,
+   * or -1 if there is no such pointer. */
+  int config_suite_offset;
 } config_format_t;
 
 /**
@@ -79,6 +82,9 @@ void config_mgr_freeze(config_mgr_t *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);
 
+/** A collection of managed configuration objects. */
+typedef struct config_suite_t config_suite_t;
+
 #define CAL_USE_DEFAULTS      (1u<<0)
 #define CAL_CLEAR_FIRST       (1u<<1)
 #define CAL_WARN_DEPRECATIONS (1u<<2)
diff --git a/src/app/config/or_options_st.h b/src/app/config/or_options_st.h
index 8156d2ca1..2c1a8f0c0 100644
--- a/src/app/config/or_options_st.h
+++ b/src/app/config/or_options_st.h
@@ -18,6 +18,7 @@
 
 struct smartlist_t;
 struct config_line_t;
+struct config_suite_t;
 
 /** Enumeration of outbound address configuration types:
  * Exit-only, OR-only, or both */
@@ -1107,6 +1108,9 @@ struct or_options_t {
    * a possible previous dormant state.
    **/
   int DormantCanceledByStartup;
+
+  /**DOCDOC*/
+  struct config_suite_t *subconfigs_;
 };
 
 #endif /* !defined(TOR_OR_OPTIONS_ST_H) */
diff --git a/src/app/config/or_state_st.h b/src/app/config/or_state_st.h
index f45c6196c..54390fa2d 100644
--- a/src/app/config/or_state_st.h
+++ b/src/app/config/or_state_st.h
@@ -15,6 +15,7 @@
 
 #include "lib/cc/torint.h"
 struct smartlist_t;
+struct config_suite_t;
 
 /** Persistent state for an onion router, as saved to disk. */
 struct or_state_t {
@@ -94,6 +95,9 @@ struct or_state_t {
   /** True if we were dormant when we last wrote the file; false if we
    * weren't.  "auto" on initial startup. */
   int Dormant;
+
+  /**DOCDOC*/
+  struct config_suite_t *substates_;
 };
 
 #endif /* !defined(TOR_OR_STATE_ST_H) */
diff --git a/src/app/config/statefile.c b/src/app/config/statefile.c
index ede35e6ce..bcc06809b 100644
--- a/src/app/config/statefile.c
+++ b/src/app/config/statefile.c
@@ -170,6 +170,7 @@ static const config_format_t state_format = {
   or_state_validate_cb,
   NULL,
   &state_extra_var,
+  offsetof(or_state_t, substates_),
 };
 
 /* A global configuration manager for state-file objects */
diff --git a/src/feature/dirauth/shared_random_state.c b/src/feature/dirauth/shared_random_state.c
index fac1efc54..f2a626c73 100644
--- a/src/feature/dirauth/shared_random_state.c
+++ b/src/feature/dirauth/shared_random_state.c
@@ -100,6 +100,7 @@ static const config_format_t state_format = {
   disk_state_validate_cb,
   NULL,
   &state_extra_var,
+  -1,
 };
 
 /* Global configuration manager for the shared-random state file */
diff --git a/src/test/test_confparse.c b/src/test/test_confparse.c
index 273232fe5..eaa257aed 100644
--- a/src/test/test_confparse.c
+++ b/src/test/test_confparse.c
@@ -134,6 +134,7 @@ static const config_format_t test_fmt = {
   test_validate_cb,
   NULL,
   NULL,
+  -1,
 };
 
 /* Make sure that config_init sets everything to the right defaults. */
@@ -815,6 +816,7 @@ static config_format_t etest_fmt = {
   test_validate_cb,
   NULL,
   &extra,
+  -1,
 };
 
 /* Try out the feature where we can store unrecognized lines and dump them





More information about the tor-commits mailing list