]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Turn 'mainloop' into a subsystem.
authorNick Mathewson <nickm@torproject.org>
Thu, 25 Apr 2019 18:20:41 +0000 (14:20 -0400)
committerNick Mathewson <nickm@torproject.org>
Tue, 30 Apr 2019 15:14:59 +0000 (11:14 -0400)
We need a little refactoring for this to work, since the
initialization code for the periodic events assumes that libevent is
already initialized, which it can't be until it's configured.

This change, combined with the previous ones, lets other subsystems
declare their own periodic events, without mainloop.c having to know
about them.  Implements ticket 30293.

src/app/main/shutdown.c
src/app/main/subsystem_list.c
src/core/include.am
src/core/mainloop/mainloop.c
src/core/mainloop/mainloop.h
src/core/mainloop/mainloop_sys.c [new file with mode: 0644]
src/core/mainloop/mainloop_sys.h [new file with mode: 0644]
src/test/test_periodic_event.c

index 314e33f2280e72daa77b0a4920c5ec31e9b1a09d..2dc18e19a72d78d40d596d99e986945a1ba95500 100644 (file)
@@ -18,7 +18,6 @@
 #include "app/main/shutdown.h"
 #include "app/main/subsysmgr.h"
 #include "core/mainloop/connection.h"
-#include "core/mainloop/mainloop.h"
 #include "core/mainloop/mainloop_pubsub.h"
 #include "core/or/channeltls.h"
 #include "core/or/circuitlist.h"
@@ -176,7 +175,6 @@ tor_free_all(int postfork)
   /* stuff in main.c */
 
   tor_mainloop_disconnect_pubsub();
-  tor_mainloop_free_all();
 
   if (!postfork) {
     release_lockfile();
index 3834176182261aa6980f632aa8bb260f17add4c9..7ffdcc8053b8ff13cf92586dc77efa96f6ee5d89 100644 (file)
@@ -8,6 +8,7 @@
 #include "lib/cc/compat_compiler.h"
 #include "lib/cc/torint.h"
 
+#include "core/mainloop/mainloop_sys.h"
 #include "core/or/ocirc_event_sys.h"
 #include "core/or/orconn_event_sys.h"
 #include "feature/control/btrack_sys.h"
@@ -44,6 +45,8 @@ const subsys_fns_t *tor_subsystems[] = {
   &sys_orconn_event, /* -33 */
   &sys_ocirc_event, /* -32 */
   &sys_btrack, /* -30 */
+
+  &sys_mainloop, /* 5 */
 };
 
 const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems);
index 9824601725051e05ef9c56be0086c771440c3e3f..4645bca76f710372c83aba84a3e05f28c71cced8 100644 (file)
@@ -24,6 +24,7 @@ LIBTOR_APP_A_SOURCES =                                \
        src/core/mainloop/cpuworker.c           \
        src/core/mainloop/mainloop.c            \
        src/core/mainloop/mainloop_pubsub.c     \
+       src/core/mainloop/mainloop_sys.c        \
        src/core/mainloop/netstatus.c           \
        src/core/mainloop/periodic.c            \
        src/core/or/address_set.c               \
@@ -222,6 +223,7 @@ noinst_HEADERS +=                                   \
        src/core/mainloop/cpuworker.h                   \
        src/core/mainloop/mainloop.h                    \
        src/core/mainloop/mainloop_pubsub.h             \
+       src/core/mainloop/mainloop_sys.h                \
        src/core/mainloop/netstatus.h                   \
        src/core/mainloop/periodic.h                    \
        src/core/or/addr_policy_st.h                    \
index ddcc3bcd7689c0f03801b2e2fc9b556704c3a97d..8c90103a7946385ce1716422afd4d076b5d45fdf 100644 (file)
@@ -1550,7 +1550,7 @@ initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
 
 /** Set up all the members of mainloop_periodic_events[], and configure them
  * all to be launched from a callback. */
-STATIC void
+void
 initialize_periodic_events(void)
 {
   if (periodic_events_initialized)
@@ -1563,7 +1563,6 @@ initialize_periodic_events(void)
   }
 
   /* Set up all periodic events. We'll launch them by roles. */
-  periodic_events_setup_all();
 
 #define NAMED_CALLBACK(name) \
   STMT_BEGIN name ## _event = periodic_events_find( #name ); STMT_END
@@ -1575,12 +1574,6 @@ initialize_periodic_events(void)
   NAMED_CALLBACK(launch_descriptor_fetches);
   NAMED_CALLBACK(check_dns_honesty);
   NAMED_CALLBACK(save_state);
-
-  struct timeval one_second = { 1, 0 };
-  initialize_periodic_events_event = tor_evtimer_new(
-                  tor_libevent_get_base(),
-                  initialize_periodic_events_cb, NULL);
-  event_add(initialize_periodic_events_event, &one_second);
 }
 
 STATIC void
@@ -2778,6 +2771,13 @@ do_main_loop(void)
    */
   initialize_periodic_events();
   initialize_mainloop_events();
+  periodic_events_setup_all();
+
+  struct timeval one_second = { 1, 0 };
+  initialize_periodic_events_event = tor_evtimer_new(
+                  tor_libevent_get_base(),
+                  initialize_periodic_events_cb, NULL);
+  event_add(initialize_periodic_events_event, &one_second);
 
 #ifdef HAVE_SYSTEMD_209
   uint64_t watchdog_delay;
index 850918c35ef215054f626ea65122db6eef01c8bc..2dccdf0e1a65c4dc832c5c0e6470bc6814ec6907 100644 (file)
@@ -90,6 +90,7 @@ void mainloop_schedule_shutdown(int delay_sec);
 
 void tor_init_connection_lists(void);
 void initialize_mainloop_events(void);
+void initialize_periodic_events(void);
 void tor_mainloop_free_all(void);
 
 struct token_bucket_rw_t;
@@ -102,7 +103,6 @@ extern struct token_bucket_rw_t global_relayed_bucket;
 #ifdef MAINLOOP_PRIVATE
 STATIC int run_main_loop_until_done(void);
 STATIC void close_closeable_connections(void);
-STATIC void initialize_periodic_events(void);
 STATIC void teardown_periodic_events(void);
 STATIC int get_my_roles(const or_options_t *);
 STATIC int check_network_participation_callback(time_t now,
diff --git a/src/core/mainloop/mainloop_sys.c b/src/core/mainloop/mainloop_sys.c
new file mode 100644 (file)
index 0000000..fbd5a40
--- /dev/null
@@ -0,0 +1,32 @@
+/* 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 */
+
+#include "core/or/or.h"
+#include "core/mainloop/mainloop_sys.h"
+#include "core/mainloop/mainloop.h"
+
+#include "lib/subsys/subsys.h"
+
+static int
+subsys_mainloop_initialize(void)
+{
+  initialize_periodic_events();
+  return 0;
+}
+
+static void
+subsys_mainloop_shutdown(void)
+{
+  tor_mainloop_free_all();
+}
+
+const struct subsys_fns_t sys_mainloop = {
+  .name = "mainloop",
+  .supported = true,
+  .level = 5,
+  .initialize = subsys_mainloop_initialize,
+  .shutdown = subsys_mainloop_shutdown,
+};
diff --git a/src/core/mainloop/mainloop_sys.h b/src/core/mainloop/mainloop_sys.h
new file mode 100644 (file)
index 0000000..14c5672
--- /dev/null
@@ -0,0 +1,12 @@
+/* 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 */
+
+#ifndef MAINLOOP_SYS_H
+#define MAINLOOP_SYS_H
+
+extern const struct subsys_fns_t sys_mainloop;
+
+#endif
index 645274d3715f0dcb6d5a1da39479089da11e85b4..19325ed3b7c29441a42c41a579b4da5b6ac7a113 100644 (file)
@@ -51,6 +51,7 @@ test_pe_initialize(void *arg)
    * need to run the main loop and then wait for a second delaying the unit
    * tests. Instead, we'll test the callback work indepedently elsewhere. */
   initialize_periodic_events();
+  periodic_events_setup_all();
   set_network_participation(false);
   rescan_periodic_events(get_options());
 
@@ -110,6 +111,7 @@ test_pe_launch(void *arg)
 #endif
 
   initialize_periodic_events();
+  periodic_events_setup_all();
 
   /* Now that we've initialized, rescan the list to launch. */
   periodic_events_on_new_options(options);
@@ -300,6 +302,7 @@ test_pe_hs_service(void *arg)
   consider_hibernation(time(NULL));
   /* Initialize the events so we can enable them */
   initialize_periodic_events();
+  periodic_events_setup_all();
 
   /* Hack: We'll set a dumb fn() of each events so they don't get called when
    * dispatching them. We just want to test the state of the callbacks, not