[tor-commits] [tor/master] Move netstatus (mainloop) state fields into mainloop's state.

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


commit 280a9a476031db7c5c5923b0ad9b23e475abeae9
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Nov 5 11:58:31 2019 -0500

    Move netstatus (mainloop) state fields into mainloop's state.
---
 src/app/config/or_state_st.h          |  7 -----
 src/app/config/statefile.c            |  7 +----
 src/app/config/statefile.h            |  2 ++
 src/core/include.am                   |  2 ++
 src/core/mainloop/.may_include        |  4 ++-
 src/core/mainloop/mainloop_state.inc  | 19 +++++++++++++
 src/core/mainloop/mainloop_state_st.h | 23 ++++++++++++++++
 src/core/mainloop/mainloop_sys.c      | 52 +++++++++++++++++++++++++++++++++++
 src/core/mainloop/netstatus.c         |  5 ++--
 src/core/mainloop/netstatus.h         |  7 +++--
 src/test/test_mainloop.c              | 20 +++++++++++---
 11 files changed, 126 insertions(+), 22 deletions(-)

diff --git a/src/app/config/or_state_st.h b/src/app/config/or_state_st.h
index 27cc936c7..6bfad3edb 100644
--- a/src/app/config/or_state_st.h
+++ b/src/app/config/or_state_st.h
@@ -89,13 +89,6 @@ struct or_state_t {
   /** When did we last rotate our onion key?  "0" for 'no idea'. */
   time_t LastRotatedOnionKey;
 
-  /** Number of minutes since the last user-initiated request (as defined by
-   * the dormant net-status system.) Set to zero if we are dormant. */
-  int MinutesSinceUserActivity;
-  /** True if we were dormant when we last wrote the file; false if we
-   * weren't.  "auto" on initial startup. */
-  int Dormant;
-
   /**
    * State objects for individual modules.
    *
diff --git a/src/app/config/statefile.c b/src/app/config/statefile.c
index 3e793ff40..af64dd47c 100644
--- a/src/app/config/statefile.c
+++ b/src/app/config/statefile.c
@@ -132,9 +132,6 @@ static const config_var_t state_vars_[] = {
   VAR("CircuitBuildTimeBin",          LINELIST_S, BuildtimeHistogram, NULL),
   VAR("BuildtimeHistogram",           LINELIST_V, BuildtimeHistogram, NULL),
 
-  V(MinutesSinceUserActivity,         POSINT,     NULL),
-  V(Dormant,                          AUTOBOOL, "auto"),
-
   END_OF_CONFIG_VARS
 };
 
@@ -177,7 +174,7 @@ static const config_format_t state_format = {
 static config_mgr_t *state_mgr = NULL;
 
 /** Return the configuration manager for state-file objects. */
-static const config_mgr_t *
+STATIC const config_mgr_t *
 get_state_mgr(void)
 {
   if (PREDICT_UNLIKELY(state_mgr == NULL)) {
@@ -334,7 +331,6 @@ or_state_set(or_state_t *new_state)
       get_circuit_build_times_mutable(),global_state) < 0) {
     ret = -1;
   }
-  netstatus_load_from_state(global_state, time(NULL));
 
   return ret;
 }
@@ -527,7 +523,6 @@ or_state_save(time_t now)
   entry_guards_update_state(global_state);
   rep_hist_update_state(global_state);
   circuit_build_times_update_state(get_circuit_build_times(), global_state);
-  netstatus_flush_to_state(global_state, now);
 
   if (accounting_is_enabled(get_options()))
     accounting_run_housekeeping(now);
diff --git a/src/app/config/statefile.h b/src/app/config/statefile.h
index 515c90a52..60171f8d1 100644
--- a/src/app/config/statefile.h
+++ b/src/app/config/statefile.h
@@ -31,6 +31,8 @@ STATIC struct config_line_t *get_transport_in_state_by_name(
 STATIC void or_state_free_(or_state_t *state);
 #define or_state_free(st) FREE_AND_NULL(or_state_t, or_state_free_, (st))
 STATIC or_state_t *or_state_new(void);
+struct config_mgr_t;
+STATIC const struct config_mgr_t *get_state_mgr(void);
 #endif /* defined(STATEFILE_PRIVATE) */
 
 #endif /* !defined(TOR_STATEFILE_H) */
diff --git a/src/core/include.am b/src/core/include.am
index a69914619..193b10a1c 100644
--- a/src/core/include.am
+++ b/src/core/include.am
@@ -245,6 +245,8 @@ noinst_HEADERS +=					\
 	src/core/mainloop/cpuworker.h			\
 	src/core/mainloop/mainloop.h			\
 	src/core/mainloop/mainloop_pubsub.h		\
+	src/core/mainloop/mainloop_state.inc    	\
+	src/core/mainloop/mainloop_state_st.h    	\
 	src/core/mainloop/mainloop_sys.h		\
 	src/core/mainloop/netstatus.h			\
 	src/core/mainloop/periodic.h			\
diff --git a/src/core/mainloop/.may_include b/src/core/mainloop/.may_include
index 79d6a130a..580e6d0a8 100644
--- a/src/core/mainloop/.may_include
+++ b/src/core/mainloop/.may_include
@@ -2,6 +2,7 @@
 
 orconfig.h
 
+lib/conf/*.h
 lib/container/*.h
 lib/dispatch/*.h
 lib/evloop/*.h
@@ -17,4 +18,5 @@ lib/geoip/*.h
 lib/sandbox/*.h
 lib/compress/*.h
 
-core/mainloop/*.h
\ No newline at end of file
+core/mainloop/*.h
+core/mainloop/*.inc
\ No newline at end of file
diff --git a/src/core/mainloop/mainloop_state.inc b/src/core/mainloop/mainloop_state.inc
new file mode 100644
index 000000000..34a37caaa
--- /dev/null
+++ b/src/core/mainloop/mainloop_state.inc
@@ -0,0 +1,19 @@
+
+/**
+ * @file mainloop_state.inc
+ * @brief Declare configuration options for the crypto_ops module.
+ **/
+
+/** Holds state for the mainloop, corresponding to part of the state
+ * file in Tor's DataDirectory. */
+BEGIN_CONF_STRUCT(mainloop_state_t)
+
+/** Number of minutes since the last user-initiated request (as defined by
+ * the dormant net-status system.) Set to zero if we are dormant. */
+CONF_VAR(MinutesSinceUserActivity, POSINT, 0, NULL)
+
+/** True if we were dormant when we last wrote the file; false if we
+ * weren't.  "auto" on initial startup. */
+CONF_VAR(Dormant, AUTOBOOL, 0, "auto")
+
+END_CONF_STRUCT(mainloop_state_t)
diff --git a/src/core/mainloop/mainloop_state_st.h b/src/core/mainloop/mainloop_state_st.h
new file mode 100644
index 000000000..44c816fba
--- /dev/null
+++ b/src/core/mainloop/mainloop_state_st.h
@@ -0,0 +1,23 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file mainloop_state_st.h
+ * @brief Declare a state structure for mainloop-relevant fields
+ **/
+
+#ifndef TOR_CORE_MAINLOOP_MAINLOOP_STATE_ST_H
+#define TOR_CORE_MAINLOOP_MAINLOOP_STATE_ST_H
+
+#include "lib/conf/confdecl.h"
+
+#define CONF_CONTEXT STRUCT
+#include "core/mainloop/mainloop_state.inc"
+#undef CONF_CONTEXT
+
+typedef struct mainloop_state_t mainloop_state_t;
+
+#endif /* !defined(TOR_CORE_MAINLOOP_MAINLOOP_STATE_ST_H) */
diff --git a/src/core/mainloop/mainloop_sys.c b/src/core/mainloop/mainloop_sys.c
index f14ecb261..7d763866d 100644
--- a/src/core/mainloop/mainloop_sys.c
+++ b/src/core/mainloop/mainloop_sys.c
@@ -12,6 +12,10 @@
 #include "core/or/or.h"
 #include "core/mainloop/mainloop_sys.h"
 #include "core/mainloop/mainloop.h"
+#include "core/mainloop/mainloop_state_st.h"
+#include "core/mainloop/netstatus.h"
+#include "lib/conf/conftypes.h"
+#include "lib/conf/confdecl.h"
 
 #include "lib/subsys/subsys.h"
 
@@ -28,10 +32,58 @@ subsys_mainloop_shutdown(void)
   tor_mainloop_free_all();
 }
 
+/** Declare a list of state variables for mainloop state. */
+#define CONF_CONTEXT TABLE
+#include "core/mainloop/mainloop_state.inc"
+#undef CONF_CONTEXT
+
+/** Magic number for mainloop state objects */
+#define MAINLOOP_STATE_MAGIC 0x59455449
+
+/**
+ * Format object for mainloop state.
+ **/
+static config_format_t mainloop_state_fmt = {
+  .size = sizeof(mainloop_state_t),
+  .magic = { "mainloop_state",
+             MAINLOOP_STATE_MAGIC,
+             offsetof(mainloop_state_t, magic)
+            },
+  .vars = mainloop_state_t_vars,
+};
+
+/**
+ */
+static int
+mainloop_set_state(void *arg)
+{
+  const mainloop_state_t *state = arg;
+  tor_assert(state->magic == MAINLOOP_STATE_MAGIC);
+
+  netstatus_load_from_state(state, approx_time());
+
+  return 0;
+}
+
+static int
+mainloop_flush_state(void *arg)
+{
+  mainloop_state_t *state = arg;
+  tor_assert(state->magic == MAINLOOP_STATE_MAGIC);
+
+  netstatus_flush_to_state(state, approx_time());
+
+  return 0;
+}
+
 const struct subsys_fns_t sys_mainloop = {
   .name = "mainloop",
   .supported = true,
   .level = 5,
   .initialize = subsys_mainloop_initialize,
   .shutdown = subsys_mainloop_shutdown,
+
+  .state_format = &mainloop_state_fmt,
+  .set_state = mainloop_set_state,
+  .flush_state = mainloop_flush_state,
 };
diff --git a/src/core/mainloop/netstatus.c b/src/core/mainloop/netstatus.c
index c34e613d1..a7a1927d8 100644
--- a/src/core/mainloop/netstatus.c
+++ b/src/core/mainloop/netstatus.c
@@ -12,6 +12,7 @@
 #include "core/or/or.h"
 #include "core/mainloop/netstatus.h"
 #include "core/mainloop/mainloop.h"
+#include "core/mainloop/mainloop_state_st.h"
 #include "app/config/config.h"
 #include "feature/hibernate/hibernate.h"
 
@@ -115,7 +116,7 @@ is_participating_on_network(void)
  * Update 'state' with the last time at which we were active on the network.
  **/
 void
-netstatus_flush_to_state(or_state_t *state, time_t now)
+netstatus_flush_to_state(mainloop_state_t *state, time_t now)
 {
   state->Dormant = ! participating_on_network;
   if (participating_on_network) {
@@ -130,7 +131,7 @@ netstatus_flush_to_state(or_state_t *state, time_t now)
  * Update our current view of network participation from an or_state_t object.
  **/
 void
-netstatus_load_from_state(const or_state_t *state, time_t now)
+netstatus_load_from_state(const mainloop_state_t *state, time_t now)
 {
   time_t last_activity;
   if (state->Dormant == -1) { // Initial setup.
diff --git a/src/core/mainloop/netstatus.h b/src/core/mainloop/netstatus.h
index ce3d2e23f..62fd77b42 100644
--- a/src/core/mainloop/netstatus.h
+++ b/src/core/mainloop/netstatus.h
@@ -22,8 +22,11 @@ time_t get_last_user_activity_time(void);
 void set_network_participation(bool participation);
 bool is_participating_on_network(void);
 
-void netstatus_flush_to_state(or_state_t *state, time_t now);
-void netstatus_load_from_state(const or_state_t *state, time_t now);
+struct mainloop_state_t;
+
+void netstatus_flush_to_state(struct mainloop_state_t *state, time_t now);
+void netstatus_load_from_state(const struct mainloop_state_t *state,
+                               time_t now);
 void netstatus_note_clock_jumped(time_t seconds_diff);
 
 #endif /* !defined(TOR_NETSTATUS_H) */
diff --git a/src/test/test_mainloop.c b/src/test/test_mainloop.c
index ed6b8a9b6..e8225db8e 100644
--- a/src/test/test_mainloop.c
+++ b/src/test/test_mainloop.c
@@ -13,9 +13,13 @@
 #include "test/test.h"
 #include "test/log_test_helpers.h"
 
+#include "lib/confmgt/confmgt.h"
+
 #include "core/or/or.h"
 #include "core/mainloop/connection.h"
 #include "core/mainloop/mainloop.h"
+#include "core/mainloop/mainloop_state_st.h"
+#include "core/mainloop/mainloop_sys.h"
 #include "core/mainloop/netstatus.h"
 
 #include "feature/hs/hs_service.h"
@@ -24,6 +28,8 @@
 #include "app/config/statefile.h"
 #include "app/config/or_state_st.h"
 
+#include "app/main/subsysmgr.h"
+
 static const uint64_t BILLION = 1000000000;
 
 static void
@@ -287,7 +293,13 @@ static void
 test_mainloop_dormant_load_state(void *arg)
 {
   (void)arg;
-  or_state_t *state = or_state_new();
+  or_state_t *or_state = or_state_new();
+  mainloop_state_t *state;
+  {
+    int idx = subsystems_get_state_idx(&sys_mainloop);
+    tor_assert(idx >= 0);
+    state = config_mgr_get_obj_mutable(get_state_mgr(), or_state, idx);
+  }
   const time_t start = 1543956575;
 
   reset_user_activity(0);
@@ -326,14 +338,14 @@ test_mainloop_dormant_load_state(void *arg)
   tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
 
  done:
-  or_state_free(state);
+  or_state_free(or_state);
 }
 
 static void
 test_mainloop_dormant_save_state(void *arg)
 {
   (void)arg;
-  or_state_t *state = or_state_new();
+  mainloop_state_t *state = tor_malloc_zero(sizeof(mainloop_state_t));
   const time_t start = 1543956575;
 
   // Can we save a non-dormant state correctly?
@@ -352,7 +364,7 @@ test_mainloop_dormant_save_state(void *arg)
   tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 0);
 
  done:
-  or_state_free(state);
+  tor_free(state);
 }
 
 #define MAINLOOP_TEST(name) \





More information about the tor-commits mailing list