[tor-commits] [tor/master] Move networking startup/cleanup logic into a subsystem.

nickm at torproject.org nickm at torproject.org
Fri Nov 9 20:01:54 UTC 2018


commit cfe5b35edb38cef6312ef0b4ae44fb0e20342706
Author: Nick Mathewson <nickm at torproject.org>
Date:   Fri Nov 2 11:11:21 2018 -0400

    Move networking startup/cleanup logic into a subsystem.
---
 src/app/main/main.c           | 19 -------------------
 src/app/main/subsystem_list.c |  2 ++
 src/lib/net/.may_include      |  1 +
 src/lib/net/include.am        |  2 ++
 src/lib/net/network_sys.c     | 44 +++++++++++++++++++++++++++++++++++++++++++
 src/lib/net/network_sys.h     | 14 ++++++++++++++
 src/test/testing_common.c     |  2 --
 7 files changed, 63 insertions(+), 21 deletions(-)

diff --git a/src/app/main/main.c b/src/app/main/main.c
index 5740efb0b..3e80725b9 100644
--- a/src/app/main/main.c
+++ b/src/app/main/main.c
@@ -427,18 +427,6 @@ dumpstats(int severity)
   rend_service_dump_stats(severity);
 }
 
-/** Called by exit() as we shut down the process.
- */
-static void
-exit_function(void)
-{
-  /* NOTE: If we ever daemonize, this gets called immediately.  That's
-   * okay for now, because we only use this on Windows.  */
-#ifdef _WIN32
-  WSACleanup();
-#endif
-}
-
 #ifdef _WIN32
 #define UNIX_ONLY 0
 #else
@@ -632,12 +620,6 @@ tor_init(int argc, char *argv[])
   rust_log_welcome_string();
 #endif /* defined(HAVE_RUST) */
 
-  if (network_init()<0) {
-    log_err(LD_BUG,"Error initializing network; exiting.");
-    return -1;
-  }
-  atexit(exit_function);
-
   int init_rv = options_init_from_torrc(argc,argv);
   if (init_rv < 0) {
     log_err(LD_CONFIG,"Reading config failed--see warnings above.");
@@ -784,7 +766,6 @@ tor_free_all(int postfork)
   routerparse_free_all();
   ext_orport_free_all();
   control_free_all();
-  tor_free_getaddrinfo_cache();
   protover_free_all();
   bridges_free_all();
   consdiffmgr_free_all();
diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c
index 3d03a9a4d..cb186c14d 100644
--- a/src/app/main/subsystem_list.c
+++ b/src/app/main/subsystem_list.c
@@ -10,6 +10,7 @@
 
 #include "lib/err/torerr_sys.h"
 #include "lib/log/log_sys.h"
+#include "lib/net/network_sys.h"
 #include "lib/process/winprocess_sys.h"
 #include "lib/thread/thread_sys.h"
 #include "lib/wallclock/wallclock_sys.h"
@@ -25,6 +26,7 @@ const subsys_fns_t *tor_subsystems[] = {
   &sys_wallclock,
   &sys_threads,
   &sys_logging,
+  &sys_network,
 };
 
 const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems);
diff --git a/src/lib/net/.may_include b/src/lib/net/.may_include
index 13b209bbe..f93f0e155 100644
--- a/src/lib/net/.may_include
+++ b/src/lib/net/.may_include
@@ -11,5 +11,6 @@ lib/lock/*.h
 lib/log/*.h
 lib/net/*.h
 lib/string/*.h
+lib/subsys/*.h
 lib/testsupport/*.h
 lib/malloc/*.h
\ No newline at end of file
diff --git a/src/lib/net/include.am b/src/lib/net/include.am
index ff0967e78..8a88f0f2a 100644
--- a/src/lib/net/include.am
+++ b/src/lib/net/include.am
@@ -11,6 +11,7 @@ src_lib_libtor_net_a_SOURCES =			\
 	src/lib/net/buffers_net.c		\
 	src/lib/net/gethostname.c		\
 	src/lib/net/inaddr.c			\
+	src/lib/net/network_sys.c		\
 	src/lib/net/resolve.c			\
 	src/lib/net/socket.c			\
 	src/lib/net/socketpair.c
@@ -28,6 +29,7 @@ noinst_HEADERS +=				\
 	src/lib/net/inaddr.h			\
 	src/lib/net/inaddr_st.h			\
 	src/lib/net/nettypes.h			\
+	src/lib/net/network_sys.h		\
 	src/lib/net/resolve.h			\
 	src/lib/net/socket.h			\
 	src/lib/net/socketpair.h		\
diff --git a/src/lib/net/network_sys.c b/src/lib/net/network_sys.c
new file mode 100644
index 000000000..c9d33a94d
--- /dev/null
+++ b/src/lib/net/network_sys.c
@@ -0,0 +1,44 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file network_sys.c
+ * \brief Subsystem object for networking setup.
+ **/
+
+#include "orconfig.h"
+#include "lib/subsys/subsys.h"
+#include "lib/net/network_sys.h"
+#include "lib/net/resolve.h"
+#include "lib/net/socket.h"
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <windows.h>
+#endif
+
+static int
+init_network_sys(void)
+{
+  if (network_init() < 0)
+    return -1;
+
+  return 0;
+}
+
+static void
+shutdown_network_sys(void)
+{
+#ifdef _WIN32
+  WSACleanup();
+#endif
+  tor_free_getaddrinfo_cache();
+}
+
+const subsys_fns_t sys_network = {
+  .name = "network",
+  .level = -90,
+  .supported = true,
+  .initialize = init_network_sys,
+  .shutdown = shutdown_network_sys,
+};
diff --git a/src/lib/net/network_sys.h b/src/lib/net/network_sys.h
new file mode 100644
index 000000000..62b778bb6
--- /dev/null
+++ b/src/lib/net/network_sys.h
@@ -0,0 +1,14 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file log_network.h
+ * \brief Declare subsystem object for the network module.
+ **/
+
+#ifndef TOR_NETWORK_SYS_H
+#define TOR_NETWORK_SYS_H
+
+extern const struct subsys_fns_t sys_network;
+
+#endif /* !defined(TOR_NETWORK_SYS_H) */
diff --git a/src/test/testing_common.c b/src/test/testing_common.c
index eef393d3a..818bb58c9 100644
--- a/src/test/testing_common.c
+++ b/src/test/testing_common.c
@@ -257,8 +257,6 @@ main(int c, const char **v)
   options = options_new();
   tor_compress_init();
 
-  network_init();
-
   monotime_init();
 
   struct tor_libevent_cfg cfg;





More information about the tor-commits mailing list