Move networking startup/cleanup logic into a subsystem.

This commit is contained in:
Nick Mathewson 2018-11-02 11:11:21 -04:00
parent 05b54f6a6a
commit cfe5b35edb
7 changed files with 63 additions and 21 deletions

View File

@ -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();

View File

@ -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);

View File

@ -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

View File

@ -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 \

44
src/lib/net/network_sys.c Normal file
View File

@ -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,
};

14
src/lib/net/network_sys.h Normal file
View File

@ -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) */

View File

@ -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;