]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Make evloop into a subsystem.
authorNick Mathewson <nickm@torproject.org>
Sat, 8 Jun 2019 00:03:26 +0000 (20:03 -0400)
committerGeorge Kadianakis <desnacked@riseup.net>
Fri, 14 Jun 2019 10:28:10 +0000 (13:28 +0300)
Note that the event base object is _not_ created from the initialize
function, since it is configuration-dependent.  This will wait until
configuration is integrated into subsystems.

Closes ticket 30806.

changes/ticket30806 [new file with mode: 0644]
src/app/main/main.c
src/app/main/shutdown.c
src/app/main/subsystem_list.c
src/lib/evloop/.may_include
src/lib/evloop/evloop_sys.c [new file with mode: 0644]
src/lib/evloop/evloop_sys.h [new file with mode: 0644]
src/lib/evloop/include.am
src/test/test_channelpadding.c
src/test/test_compat_libevent.c

diff --git a/changes/ticket30806 b/changes/ticket30806
new file mode 100644 (file)
index 0000000..4f09ea2
--- /dev/null
@@ -0,0 +1,3 @@
+  o Code simplification and refactoring:
+    - Use the subsystems mechanism to manage the main event loop code.
+      Closes ticket 30806.
index 6e325f0b10dda0ca810d8f8bdb7ba92beff84f4b..17b0000d98cd358606ab03e4c2b14421e91a33c8 100644 (file)
@@ -653,10 +653,6 @@ tor_init(int argc, char *argv[])
     return -1;
   }
 
-  if (tor_init_libevent_rng() < 0) {
-    log_warn(LD_NET, "Problem initializing libevent RNG.");
-  }
-
   /* Scan/clean unparseable descriptors; after reading config */
   routerparse_init();
 
index cc0091a9abcc72c59a35f5fab1fb63da0a297bc6..93d6351d1bbe225d4fcababcbe9463fa90ead77d 100644 (file)
@@ -160,8 +160,6 @@ tor_free_all(int postfork)
 
   subsystems_shutdown();
 
-  tor_libevent_free_all();
-
   /* Stuff in util.c and address.c*/
   if (!postfork) {
     esc_router_info(NULL);
index f595796232c0d4461dd2bc9ecb96767aa521475a..95d96f78d2165ac6141a6e27bb6638a293a8ebfc 100644 (file)
@@ -25,6 +25,7 @@
 #include "lib/time/time_sys.h"
 #include "lib/tls/tortls_sys.h"
 #include "lib/wallclock/wallclock_sys.h"
+#include "lib/evloop/evloop_sys.h"
 
 #include "feature/dirauth/dirauth_sys.h"
 
@@ -50,6 +51,8 @@ const subsys_fns_t *tor_subsystems[] = {
   &sys_ocirc_event, /* -32 */
   &sys_btrack, /* -30 */
 
+  &sys_evloop, /* -20 */
+
   &sys_mainloop, /* 5 */
   &sys_or, /* 20 */
 
index 273de7bb9434e7acb43d764f3b0557d3723f0258..54aa75fbff27969e1eb7ed16aef2854e6a9f887d 100644 (file)
@@ -8,6 +8,7 @@ lib/log/*.h
 lib/malloc/*.h
 lib/net/*.h
 lib/string/*.h
+lib/subsys/*.h
 lib/testsupport/*.h
 lib/thread/*.h
 lib/time/*.h
diff --git a/src/lib/evloop/evloop_sys.c b/src/lib/evloop/evloop_sys.c
new file mode 100644 (file)
index 0000000..56641a3
--- /dev/null
@@ -0,0 +1,49 @@
+/* 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 evloop_sys.c
+ * @brief Subsystem definition for the event loop module
+ **/
+
+#include "orconfig.h"
+#include "lib/subsys/subsys.h"
+#include "lib/evloop/compat_libevent.h"
+#include "lib/evloop/evloop_sys.h"
+#include "lib/log/log.h"
+
+static int
+subsys_evloop_initialize(void)
+{
+  if (tor_init_libevent_rng() < 0) {
+    log_warn(LD_NET, "Problem initializing libevent RNG.");
+    return -1;
+  }
+  return 0;
+}
+
+static void
+subsys_evloop_postfork(void)
+{
+#ifdef TOR_UNIT_TESTS
+  tor_libevent_postfork();
+#endif
+}
+
+static void
+subsys_evloop_shutdown(void)
+{
+  tor_libevent_free_all();
+}
+
+const struct subsys_fns_t sys_evloop = {
+  .name = "evloop",
+  .supported = true,
+  .level = -20,
+  .initialize = subsys_evloop_initialize,
+  .shutdown = subsys_evloop_shutdown,
+  .postfork = subsys_evloop_postfork,
+};
diff --git a/src/lib/evloop/evloop_sys.h b/src/lib/evloop/evloop_sys.h
new file mode 100644 (file)
index 0000000..e6155c2
--- /dev/null
@@ -0,0 +1,17 @@
+/* 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 evloop_sys.h
+ * @brief Declare subsystem object for the event loop module.
+ **/
+
+#ifndef TOR_LIB_EVLOOP_EVLOOP_SYS_H
+#define TOR_LIB_EVLOOP_EVLOOP_SYS_H
+
+extern const struct subsys_fns_t sys_evloop;
+
+#endif /* !defined(TOR_LIB_EVLOOP_EVLOOP_SYS_H) */
index 6595b3a34b5f3a4f1846954adc8df4f1b4901aa7..41cd2f45c5fd56e89f969398dad0baceb9573a57 100644 (file)
@@ -8,6 +8,7 @@ endif
 # ADD_C_FILE: INSERT SOURCES HERE.
 src_lib_libtor_evloop_a_SOURCES =                      \
        src/lib/evloop/compat_libevent.c                \
+       src/lib/evloop/evloop_sys.c                     \
        src/lib/evloop/procmon.c                        \
        src/lib/evloop/timers.c                         \
        src/lib/evloop/token_bucket.c                   \
@@ -21,6 +22,7 @@ src_lib_libtor_evloop_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
 # ADD_C_FILE: INSERT HEADERS HERE.
 noinst_HEADERS +=                                      \
        src/lib/evloop/compat_libevent.h                \
+       src/lib/evloop/evloop_sys.h                     \
        src/lib/evloop/procmon.h                        \
        src/lib/evloop/timers.h                         \
        src/lib/evloop/token_bucket.h                   \
index 5d012e462bbabf4f963345f4209a7ba26fdf2b6c..885246628e4a82705cc45c2a2933a92b8b57c64a 100644 (file)
@@ -289,8 +289,6 @@ test_channelpadding_timers(void *arg)
   channel_t *chans[CHANNELS_TO_TEST];
   (void)arg;
 
-  tor_libevent_postfork();
-
   if (!connection_array)
     connection_array = smartlist_new();
 
@@ -393,7 +391,6 @@ test_channelpadding_killonehop(void *arg)
   channelpadding_decision_t decision;
   int64_t new_time;
   (void)arg;
-  tor_libevent_postfork();
 
   routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t));
   monotime_init();
@@ -502,8 +499,6 @@ test_channelpadding_consensus(void *arg)
   int64_t new_time;
   (void)arg;
 
-  tor_libevent_postfork();
-
   /*
    * Params tested:
    *   nf_pad_before_usage
@@ -898,8 +893,6 @@ test_channelpadding_decide_to_pad_channel(void *arg)
     connection_array = smartlist_new();
   (void)arg;
 
-  tor_libevent_postfork();
-
   monotime_init();
   monotime_enable_test_mocking();
   monotime_set_mock_time_nsec(1);
index 5d625483dae5d89ac18a441feb04af3c84d5b002..ecd97e34744cd61dd0ce6d029ce52849885ffc55 100644 (file)
@@ -151,8 +151,6 @@ test_compat_libevent_postloop_events(void *arg)
   mainloop_event_t *a = NULL, *b = NULL;
   periodic_timer_t *timed = NULL;
 
-  tor_libevent_postfork();
-
   /* If postloop events don't work, then these events will activate one
    * another ad infinitum and, and the periodic event will never occur. */
   b = mainloop_event_postloop_new(activate_event_cb, &a);