From 0ce1f2d46646fd73abee56888650288055f16a53 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 08:18:29 -0400 Subject: [PATCH 01/22] Declare the subsystem structure. --- src/include.am | 1 + src/lib/subsys/.may_include | 1 + src/lib/subsys/include.am | 3 ++ src/lib/subsys/subsys.h | 63 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 src/lib/subsys/.may_include create mode 100644 src/lib/subsys/include.am create mode 100644 src/lib/subsys/subsys.h diff --git a/src/include.am b/src/include.am index d2f83da814..247b0db8da 100644 --- a/src/include.am +++ b/src/include.am @@ -25,6 +25,7 @@ include src/lib/osinfo/include.am include src/lib/process/include.am include src/lib/sandbox/include.am include src/lib/string/include.am +include src/lib/subsys/include.am include src/lib/smartlist_core/include.am include src/lib/term/include.am include src/lib/testsupport/include.am diff --git a/src/lib/subsys/.may_include b/src/lib/subsys/.may_include new file mode 100644 index 0000000000..2b06e8519c --- /dev/null +++ b/src/lib/subsys/.may_include @@ -0,0 +1 @@ +orconfig.h diff --git a/src/lib/subsys/include.am b/src/lib/subsys/include.am new file mode 100644 index 0000000000..4741126b14 --- /dev/null +++ b/src/lib/subsys/include.am @@ -0,0 +1,3 @@ + +noinst_HEADERS += \ + src/lib/subsys/subsys.h diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h new file mode 100644 index 0000000000..7e4fe53636 --- /dev/null +++ b/src/lib/subsys/subsys.h @@ -0,0 +1,63 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#ifndef TOR_SUBSYS_T +#define TOR_SUBSYS_T + +#include + +struct dispatch_connector_t; + +/** + * A subsystem is a part of Tor that is initialized, shut down, configured, + * and connected to other parts of Tor. + * + * Subsystems + **/ +typedef struct subsys_fns_t { + /** + * The name of this subsystem. It should be a programmer-readable + * identifier. + **/ + const char *name; + + /** + * Whether this subsystem is supported -- that is, whether it is compiled + * into Tor. For most subsystems, this should be true. + **/ + bool supported; + + /** + * The 'initialization level' for the subsystem. It should run from -100 + * through +100. The subsystems are initialized from lowest level to + * highest, and shut down from highest level to lowest. + **/ + int level; + + /** + * Initialize any global components of this subsystem. + * + * This function MAY rely on any lower-level subsystem being initialized. + * + * This function MUST NOT rely on any runtime configuration information; + * it is only for global state or pre-configuration state. + **/ + int (*initialize)(void); + + /** + * Connect a subsystem to the message dispatch system. + **/ + int (*add_pubsub)(struct dispatch_connector_t *); + + /** + * Free all resources held by this subsystem. + * + * This function is not allowed to fail. + **/ + void (*shutdown)(void); + +} subsys_fns_t; + +#endif From 7bb76b24cf755799b7950ef078ac5ccf4d6e3a8a Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 11:51:33 -0400 Subject: [PATCH 02/22] Code to manage the list of subsystems. --- src/app/main/main.c | 6 ++ src/app/main/subsysmgr.c | 130 ++++++++++++++++++++++++++++++++++ src/app/main/subsysmgr.h | 20 ++++++ src/app/main/subsystem_list.c | 20 ++++++ src/core/include.am | 3 + src/lib/subsys/subsys.h | 5 ++ 6 files changed, 184 insertions(+) create mode 100644 src/app/main/subsysmgr.c create mode 100644 src/app/main/subsysmgr.h create mode 100644 src/app/main/subsystem_list.c diff --git a/src/app/main/main.c b/src/app/main/main.c index ae87add67d..444d6ea7ec 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -15,6 +15,7 @@ #include "app/config/statefile.h" #include "app/main/main.h" #include "app/main/ntmain.h" +#include "app/main/subsysmgr.h" #include "core/mainloop/connection.h" #include "core/mainloop/cpuworker.h" #include "core/mainloop/mainloop.h" @@ -813,6 +814,9 @@ tor_free_all(int postfork) release_lockfile(); } tor_libevent_free_all(); + + subsystems_shutdown(); + /* Stuff in util.c and address.c*/ if (!postfork) { escaped(NULL); @@ -1426,6 +1430,8 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_); #endif + subsystems_init(); + init_protocol_warning_severity_level(); update_approx_time(time(NULL)); diff --git a/src/app/main/subsysmgr.c b/src/app/main/subsysmgr.c new file mode 100644 index 0000000000..7974f2d238 --- /dev/null +++ b/src/app/main/subsysmgr.c @@ -0,0 +1,130 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "orconfig.h" +#include "app/main/subsysmgr.h" +#include "lib/err/torerr.h" + +#include +#include +#include + +/** + * True iff we have checked tor_subsystems for consistency. + **/ +static bool subsystem_array_validated = false; + +/** + * True if a given subsystem is initialized. Expand this array if there + * are more than this number of subsystems. (We'd rather not + * dynamically allocate in this module.) + **/ +static bool sys_initialized[128]; + +/** + * Exit with a raw assertion if the subsystems list is inconsistent; + * initialize the subsystem_initialized array. + **/ +static void +check_and_setup(void) +{ + if (subsystem_array_validated) + return; + + raw_assert(ARRAY_LENGTH(sys_initialized) >= n_tor_subsystems); + memset(sys_initialized, 0, sizeof(sys_initialized)); + + int last_level = MIN_SUBSYS_LEVEL; + + for (unsigned i = 0; i < n_tor_subsystems; ++i) { + const subsys_fns_t *sys = tor_subsystems[i]; + if (sys->level < MIN_SUBSYS_LEVEL || sys->level > MAX_SUBSYS_LEVEL) { + fprintf(stderr, "BUG: Subsystem %s (at %u) has an invalid level %d. " + "It is supposed to be between %d and %d (inclusive).\n", + sys->name, i, sys->level, MIN_SUBSYS_LEVEL, MAX_SUBSYS_LEVEL); + raw_assert_unreached_msg("There is a bug in subsystem_list.c"); + } + if (sys->level < last_level) { + fprintf(stderr, "BUG: Subsystem %s (at #%u) is in the wrong position. " + "Its level is %d; but the previous subsystem's level was %d.\n", + sys->name, i, sys->level, last_level); + raw_assert_unreached_msg("There is a bug in subsystem_list.c"); + } + last_level = sys->level; + } + + subsystem_array_validated = true; +} + +/** + * Initialize all the subsystems; exit on failure. + **/ +int +subsystems_init(void) +{ + return subsystems_init_upto(MAX_SUBSYS_LEVEL); +} + +/** + * Initialize all the subsystems whose level is less than or equal to + * target_level; exit on failure. + **/ +int +subsystems_init_upto(int target_level) +{ + check_and_setup(); + + for (unsigned i = 0; i < n_tor_subsystems; ++i) { + const subsys_fns_t *sys = tor_subsystems[i]; + if (!sys->supported) + continue; + if (sys->level > target_level) + break; + if (sys_initialized[i]) + continue; + int r = 0; + if (sys->initialize) + r = sys->initialize(); + if (r < 0) { + fprintf(stderr, "BUG: subsystem %s (at %u) initialization failed.\n", + sys->name, i); + raw_assert_unreached_msg("A subsystem couldn't be initialized."); + } + sys_initialized[i] = true; + } + + return 0; +} + +/** + * Shut down all the subsystems. + **/ +void +subsystems_shutdown(void) +{ + subsystems_shutdown_downto(MIN_SUBSYS_LEVEL - 1); +} + +/** + * Shut down all the subsystems whose level is above target_level. + **/ +void +subsystems_shutdown_downto(int target_level) +{ + check_and_setup(); + + for (int i = (int)n_tor_subsystems - 1; i >= 0; --i) { + const subsys_fns_t *sys = tor_subsystems[i]; + if (!sys->supported) + continue; + if (sys->level <= target_level) + break; + if (! sys_initialized[i]) + continue; + if (sys->shutdown) + sys->shutdown(); + sys_initialized[i] = false; + } +} diff --git a/src/app/main/subsysmgr.h b/src/app/main/subsysmgr.h new file mode 100644 index 0000000000..c9b892eee4 --- /dev/null +++ b/src/app/main/subsysmgr.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#ifndef TOR_SUBSYSMGR_T +#define TOR_SUBSYSMGR_T + +#include "lib/subsys/subsys.h" + +extern const struct subsys_fns_t *tor_subsystems[]; +extern const unsigned n_tor_subsystems; + +int subsystems_init(void); +int subsystems_init_upto(int level); + +void subsystems_shutdown(void); +void subsystems_shutdown_downto(int level); + +#endif diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c new file mode 100644 index 0000000000..fc1249e1c6 --- /dev/null +++ b/src/app/main/subsystem_list.c @@ -0,0 +1,20 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "orconfig.h" +#include "app/main/subsysmgr.h" +#include "lib/cc/compat_compiler.h" +#include "lib/cc/torint.h" + +#include + +/** + * Global list of the subsystems in Tor, in the order of their initialization. + **/ +const subsys_fns_t *tor_subsystems[] = { + NULL // placeholder. +}; + +const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems); diff --git a/src/core/include.am b/src/core/include.am index 1b8ef2ac58..d3fce54285 100644 --- a/src/core/include.am +++ b/src/core/include.am @@ -11,6 +11,8 @@ LIBTOR_APP_A_SOURCES = \ src/app/config/confparse.c \ src/app/config/statefile.c \ src/app/main/main.c \ + src/app/main/subsystem_list.c \ + src/app/main/subsysmgr.c \ src/core/crypto/hs_ntor.c \ src/core/crypto/onion_crypto.c \ src/core/crypto/onion_fast.c \ @@ -191,6 +193,7 @@ noinst_HEADERS += \ src/app/config/statefile.h \ src/app/main/main.h \ src/app/main/ntmain.h \ + src/app/main/subsysmgr.h \ src/core/crypto/hs_ntor.h \ src/core/crypto/onion_crypto.h \ src/core/crypto/onion_fast.h \ diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h index 7e4fe53636..012b218da7 100644 --- a/src/lib/subsys/subsys.h +++ b/src/lib/subsys/subsys.h @@ -43,6 +43,8 @@ typedef struct subsys_fns_t { * * This function MUST NOT rely on any runtime configuration information; * it is only for global state or pre-configuration state. + * + * This function MUST NOT have any parts that can fail. **/ int (*initialize)(void); @@ -60,4 +62,7 @@ typedef struct subsys_fns_t { } subsys_fns_t; +#define MIN_SUBSYS_LEVEL -100 +#define MAX_SUBSYS_LEVEL 100 + #endif From 6e7ff8cba0efaf803e3ef5b5aba4123633fe0658 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 12:33:22 -0400 Subject: [PATCH 03/22] Move the code that knows our tor version into a lowest-level lib --- .gitignore | 2 + Makefile.am | 2 + src/app/config/config.c | 41 +------------------ src/app/config/config.h | 2 - src/app/config/statefile.c | 1 + src/app/main/main.c | 1 + src/feature/control/control.c | 1 + src/feature/dirauth/shared_random_state.c | 1 + src/feature/relay/router.c | 1 + src/include.am | 1 + src/lib/log/.may_include | 3 +- src/lib/log/include.am | 8 ---- src/lib/log/log.c | 2 +- src/lib/version/.may_include | 3 ++ src/lib/{log => version}/git_revision.c | 2 +- src/lib/{log => version}/git_revision.h | 0 src/lib/version/include.am | 25 ++++++++++++ src/lib/version/torversion.h | 12 ++++++ src/lib/version/version.c | 50 +++++++++++++++++++++++ src/rust/build.rs | 1 + src/test/fuzz/fuzzing_common.c | 1 + src/test/testing_common.c | 1 + 22 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 src/lib/version/.may_include rename src/lib/{log => version}/git_revision.c (94%) rename src/lib/{log => version}/git_revision.h (100%) create mode 100644 src/lib/version/include.am create mode 100644 src/lib/version/torversion.h create mode 100644 src/lib/version/version.c diff --git a/.gitignore b/.gitignore index cedff8fb37..ee2de376a6 100644 --- a/.gitignore +++ b/.gitignore @@ -210,6 +210,8 @@ uptime-*.json /src/lib/libtor-tls.a /src/lib/libtor-tls-testing.a /src/lib/libtor-trace.a +/src/lib/libtor-version.a +/src/lib/libtor-version-testing.a /src/lib/libtor-wallclock.a /src/lib/libtor-wallclock-testing.a diff --git a/Makefile.am b/Makefile.am index e5c1be31b5..cb76edfa2f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,6 +62,7 @@ TOR_UTIL_LIBS = \ src/lib/libtor-malloc.a \ src/lib/libtor-wallclock.a \ src/lib/libtor-err.a \ + src/lib/libtor-version.a \ src/lib/libtor-intmath.a \ src/lib/libtor-ctime.a @@ -91,6 +92,7 @@ TOR_UTIL_TESTING_LIBS = \ src/lib/libtor-malloc-testing.a \ src/lib/libtor-wallclock-testing.a \ src/lib/libtor-err-testing.a \ + src/lib/libtor-version-testing.a \ src/lib/libtor-intmath.a \ src/lib/libtor-ctime-testing.a endif diff --git a/src/app/config/config.c b/src/app/config/config.c index 6e7e131055..7b49387bcf 100644 --- a/src/app/config/config.c +++ b/src/app/config/config.c @@ -112,9 +112,9 @@ #include "lib/crypt_ops/crypto_rand.h" #include "lib/crypt_ops/crypto_util.h" #include "lib/encoding/confline.h" -#include "lib/log/git_revision.h" #include "lib/net/resolve.h" #include "lib/sandbox/sandbox.h" +#include "lib/version/torversion.h" #ifdef ENABLE_NSS #include "lib/crypt_ops/crypto_nss_mgt.h" @@ -972,42 +972,6 @@ set_options(or_options_t *new_val, char **msg) return 0; } -/** The version of this Tor process, as parsed. */ -static char *the_tor_version = NULL; -/** A shorter version of this Tor process's version, for export in our router - * descriptor. (Does not include the git version, if any.) */ -static char *the_short_tor_version = NULL; - -/** Return the current Tor version. */ -const char * -get_version(void) -{ - if (the_tor_version == NULL) { - if (strlen(tor_git_revision)) { - tor_asprintf(&the_tor_version, "%s (git-%s)", get_short_version(), - tor_git_revision); - } else { - the_tor_version = tor_strdup(get_short_version()); - } - } - return the_tor_version; -} - -/** Return the current Tor version, without any git tag. */ -const char * -get_short_version(void) -{ - - if (the_short_tor_version == NULL) { -#ifdef TOR_BUILD_TAG - tor_asprintf(&the_short_tor_version, "%s (%s)", VERSION, TOR_BUILD_TAG); -#else - the_short_tor_version = tor_strdup(VERSION); -#endif - } - return the_short_tor_version; -} - /** Release additional memory allocated in options */ STATIC void @@ -1067,9 +1031,6 @@ config_free_all(void) tor_free(torrc_defaults_fname); tor_free(global_dirfrontpagecontents); - tor_free(the_short_tor_version); - tor_free(the_tor_version); - cleanup_protocol_warning_severity_level(); have_parsed_cmdline = 0; diff --git a/src/app/config/config.h b/src/app/config/config.h index a169cfd451..4c497b83a6 100644 --- a/src/app/config/config.h +++ b/src/app/config/config.h @@ -41,8 +41,6 @@ const char *escaped_safe_str_client(const char *address); const char *escaped_safe_str(const char *address); void init_protocol_warning_severity_level(void); int get_protocol_warning_severity_level(void); -const char *get_version(void); -const char *get_short_version(void); /** An error from options_trial_assign() or options_init_from_string(). */ typedef enum setopt_err_t { diff --git a/src/app/config/statefile.c b/src/app/config/statefile.c index 8a8b7ced01..4ba7be1519 100644 --- a/src/app/config/statefile.c +++ b/src/app/config/statefile.c @@ -45,6 +45,7 @@ #include "app/config/statefile.h" #include "lib/encoding/confline.h" #include "lib/net/resolve.h" +#include "lib/version/torversion.h" #include "app/config/or_state_st.h" diff --git a/src/app/main/main.c b/src/app/main/main.c index 444d6ea7ec..031f570097 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -84,6 +84,7 @@ #include "lib/encoding/confline.h" #include "lib/evloop/timers.h" #include "lib/crypt_ops/crypto_init.h" +#include "lib/version/torversion.h" #include diff --git a/src/feature/control/control.c b/src/feature/control/control.c index 3fa47747eb..b31b448e96 100644 --- a/src/feature/control/control.c +++ b/src/feature/control/control.c @@ -92,6 +92,7 @@ #include "lib/crypt_ops/crypto_util.h" #include "lib/encoding/confline.h" #include "lib/evloop/compat_libevent.h" +#include "lib/version/torversion.h" #include "feature/dircache/cached_dir_st.h" #include "feature/control/control_connection_st.h" diff --git a/src/feature/dirauth/shared_random_state.c b/src/feature/dirauth/shared_random_state.c index 38c7fd76d0..1ce06744d4 100644 --- a/src/feature/dirauth/shared_random_state.c +++ b/src/feature/dirauth/shared_random_state.c @@ -22,6 +22,7 @@ #include "feature/dirauth/shared_random_state.h" #include "feature/dircommon/voting_schedule.h" #include "lib/encoding/confline.h" +#include "lib/version/torversion.h" #include "app/config/or_state_st.h" diff --git a/src/feature/relay/router.c b/src/feature/relay/router.c index 3a819f592c..9d61ced11c 100644 --- a/src/feature/relay/router.c +++ b/src/feature/relay/router.c @@ -49,6 +49,7 @@ #include "lib/encoding/confline.h" #include "lib/osinfo/uname.h" #include "lib/tls/tortls.h" +#include "lib/version/torversion.h" #include "feature/dirauth/authmode.h" diff --git a/src/include.am b/src/include.am index 247b0db8da..8279499936 100644 --- a/src/include.am +++ b/src/include.am @@ -33,6 +33,7 @@ include src/lib/thread/include.am include src/lib/time/include.am include src/lib/tls/include.am include src/lib/trace/include.am +include src/lib/version/include.am include src/lib/wallclock/include.am include src/trunnel/include.am diff --git a/src/lib/log/.may_include b/src/lib/log/.may_include index 852173aab3..7ca1863a52 100644 --- a/src/lib/log/.may_include +++ b/src/lib/log/.may_include @@ -10,6 +10,5 @@ lib/log/*.h lib/malloc/*.h lib/string/*.h lib/testsupport/*.h +lib/version/*.h lib/wallclock/*.h - -micro-revision.i \ No newline at end of file diff --git a/src/lib/log/include.am b/src/lib/log/include.am index 4a6c9b3686..c6f404e269 100644 --- a/src/lib/log/include.am +++ b/src/lib/log/include.am @@ -7,7 +7,6 @@ endif src_lib_libtor_log_a_SOURCES = \ src/lib/log/escape.c \ - src/lib/log/git_revision.c \ src/lib/log/ratelim.c \ src/lib/log/log.c \ src/lib/log/util_bug.c @@ -21,15 +20,8 @@ src_lib_libtor_log_testing_a_SOURCES = \ src_lib_libtor_log_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS) src_lib_libtor_log_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) -# Declare that these object files depend on micro-revision.i. Without this -# rule, we could try to build them before micro-revision.i was created. -src/lib/log/git_revision.$(OBJEXT) \ - src/lib/log/src_lib_libtor_log_testing_a-git_revision.$(OBJEXT): \ - micro-revision.i - noinst_HEADERS += \ src/lib/log/escape.h \ - src/lib/log/git_revision.h \ src/lib/log/ratelim.h \ src/lib/log/log.h \ src/lib/log/util_bug.h \ diff --git a/src/lib/log/log.c b/src/lib/log/log.c index d60ce6308a..bc7b36dcb9 100644 --- a/src/lib/log/log.c +++ b/src/lib/log/log.c @@ -32,7 +32,7 @@ #define LOG_PRIVATE #include "lib/log/log.h" -#include "lib/log/git_revision.h" +#include "lib/version/git_revision.h" #include "lib/log/ratelim.h" #include "lib/lock/compat_mutex.h" #include "lib/smartlist_core/smartlist_core.h" diff --git a/src/lib/version/.may_include b/src/lib/version/.may_include new file mode 100644 index 0000000000..d159ceb41f --- /dev/null +++ b/src/lib/version/.may_include @@ -0,0 +1,3 @@ +orconfig.h +micro-revision.i +lib/version/*.h \ No newline at end of file diff --git a/src/lib/log/git_revision.c b/src/lib/version/git_revision.c similarity index 94% rename from src/lib/log/git_revision.c rename to src/lib/version/git_revision.c index 9d29ecd2a2..e5b2ff534e 100644 --- a/src/lib/log/git_revision.c +++ b/src/lib/version/git_revision.c @@ -4,7 +4,7 @@ /* See LICENSE for licensing information */ #include "orconfig.h" -#include "lib/log/git_revision.h" +#include "lib/version/git_revision.h" /** String describing which Tor Git repository version the source was * built from. This string is generated by a bit of shell kludging in diff --git a/src/lib/log/git_revision.h b/src/lib/version/git_revision.h similarity index 100% rename from src/lib/log/git_revision.h rename to src/lib/version/git_revision.h diff --git a/src/lib/version/include.am b/src/lib/version/include.am new file mode 100644 index 0000000000..6944eb05e3 --- /dev/null +++ b/src/lib/version/include.am @@ -0,0 +1,25 @@ + +noinst_LIBRARIES += src/lib/libtor-version.a + +if UNITTESTS_ENABLED +noinst_LIBRARIES += src/lib/libtor-version-testing.a +endif + +src_lib_libtor_version_a_SOURCES = \ + src/lib/version/git_revision.c \ + src/lib/version/version.c + +src_lib_libtor_version_testing_a_SOURCES = \ + $(src_lib_libtor_version_a_SOURCES) +src_lib_libtor_version_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS) +src_lib_libtor_version_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) + +# Declare that these object files depend on micro-revision.i. Without this +# rule, we could try to build them before micro-revision.i was created. +src/lib/version/git_revision.$(OBJEXT) \ + src/lib/version/src_lib_libtor_version_testing_a-git_revision.$(OBJEXT): \ + micro-revision.i + +noinst_HEADERS += \ + src/lib/version/git_revision.h \ + src/lib/version/torversion.h diff --git a/src/lib/version/torversion.h b/src/lib/version/torversion.h new file mode 100644 index 0000000000..761d6f25ab --- /dev/null +++ b/src/lib/version/torversion.h @@ -0,0 +1,12 @@ +/* Copyright 2001-2004 Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#ifndef TOR_VERSION_H +#define TOR_VERSION_H + +const char *get_version(void); +const char *get_short_version(void); + +#endif /* !defined(TOR_VERSION_H) */ diff --git a/src/lib/version/version.c b/src/lib/version/version.c new file mode 100644 index 0000000000..29ada39c9d --- /dev/null +++ b/src/lib/version/version.c @@ -0,0 +1,50 @@ +/* Copyright 2001-2004 Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "orconfig.h" +#include "lib/version/torversion.h" +#include "lib/version/git_revision.h" + +#include +#include + +/** A shorter version of this Tor process's version, for export in our router + * descriptor. (Does not include the git version, if any.) */ +static const char the_short_tor_version[] = + VERSION +#ifdef TOR_BUILD_TAG + " ("TOR_BUILD_TAG")" +#endif + ""; + +#define MAX_VERSION_LEN 128 + +/** The version of this Tor process, possibly including git version */ +static char the_tor_version[MAX_VERSION_LEN] = ""; + +/** Return the current Tor version. */ +const char * +get_version(void) +{ + if (the_tor_version[0] == 0) { + if (strlen(tor_git_revision)) { + snprintf(the_tor_version, sizeof(the_tor_version), + "%s (git-%s)", the_short_tor_version, tor_git_revision); + } else { + snprintf(the_tor_version, sizeof(the_tor_version), + "%s", the_short_tor_version); + } + the_tor_version[sizeof(the_tor_version)-1] = 0; + } + + return the_tor_version; +} + +/** Return the current Tor version, without any git tag. */ +const char * +get_short_version(void) +{ + return the_short_tor_version; +} diff --git a/src/rust/build.rs b/src/rust/build.rs index 123d5c0682..bf566c56bf 100644 --- a/src/rust/build.rs +++ b/src/rust/build.rs @@ -162,6 +162,7 @@ pub fn main() { cfg.component("tor-malloc"); cfg.component("tor-wallclock"); cfg.component("tor-err-testing"); + cfg.component("tor-version-testing"); cfg.component("tor-intmath-testing"); cfg.component("tor-ctime-testing"); cfg.component("curve25519_donna"); diff --git a/src/test/fuzz/fuzzing_common.c b/src/test/fuzz/fuzzing_common.c index 1401e4c28d..879f9e74dc 100644 --- a/src/test/fuzz/fuzzing_common.c +++ b/src/test/fuzz/fuzzing_common.c @@ -9,6 +9,7 @@ #include "lib/compress/compress.h" #include "lib/crypt_ops/crypto_ed25519.h" #include "lib/crypt_ops/crypto_init.h" +#include "lib/version/torversion.h" static or_options_t *mock_options = NULL; static const or_options_t * diff --git a/src/test/testing_common.c b/src/test/testing_common.c index c52683afca..8d648ee175 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -25,6 +25,7 @@ #include "lib/compress/compress.h" #include "lib/evloop/compat_libevent.h" #include "lib/crypt_ops/crypto_init.h" +#include "lib/version/torversion.h" #include #ifdef HAVE_FCNTL_H From 175153a3290b3987faacac9d5390e87e1ad4a457 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 12:40:55 -0400 Subject: [PATCH 04/22] Make initialization for the "err" library into a subsystem. --- src/app/main/main.c | 10 --------- src/app/main/subsystem_list.c | 4 +++- src/lib/err/.may_include | 2 ++ src/lib/err/include.am | 8 ++++--- src/lib/err/torerr.c | 10 +++++++++ src/lib/err/torerr.h | 1 + src/lib/err/torerr_sys.c | 39 +++++++++++++++++++++++++++++++++++ src/lib/err/torerr_sys.h | 14 +++++++++++++ 8 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 src/lib/err/torerr_sys.c create mode 100644 src/lib/err/torerr_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 031f570097..e3d7610c82 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -69,7 +69,6 @@ #include "lib/container/buffers.h" #include "lib/crypt_ops/crypto_rand.h" #include "lib/crypt_ops/crypto_s2k.h" -#include "lib/err/backtrace.h" #include "lib/geoip/geoip.h" #include "lib/process/waitpid.h" @@ -822,7 +821,6 @@ tor_free_all(int postfork) if (!postfork) { escaped(NULL); esc_router_info(NULL); - clean_up_backtrace_handler(); logs_free_all(); /* free log strings. do this last so logs keep working. */ } } @@ -1419,14 +1417,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) #endif /* !defined(_WIN64) */ #endif /* defined(_WIN32) */ - { - int bt_err = configure_backtrace_handler(get_version()); - if (bt_err < 0) { - log_warn(LD_BUG, "Unable to install backtrace handler: %s", - strerror(-bt_err)); - } - } - #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_); #endif diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index fc1249e1c6..244dbadbd9 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -8,13 +8,15 @@ #include "lib/cc/compat_compiler.h" #include "lib/cc/torint.h" +#include "lib/err/torerr_sys.h" + #include /** * Global list of the subsystems in Tor, in the order of their initialization. **/ const subsys_fns_t *tor_subsystems[] = { - NULL // placeholder. + &sys_torerr, }; const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems); diff --git a/src/lib/err/.may_include b/src/lib/err/.may_include index 48cc0ef088..daa1b6e4ca 100644 --- a/src/lib/err/.may_include +++ b/src/lib/err/.may_include @@ -1,3 +1,5 @@ orconfig.h lib/cc/*.h lib/err/*.h +lib/subsys/*.h +lib/version/*.h \ No newline at end of file diff --git a/src/lib/err/include.am b/src/lib/err/include.am index f2a409c51e..43adcd2694 100644 --- a/src/lib/err/include.am +++ b/src/lib/err/include.am @@ -6,8 +6,9 @@ noinst_LIBRARIES += src/lib/libtor-err-testing.a endif src_lib_libtor_err_a_SOURCES = \ - src/lib/err/backtrace.c \ - src/lib/err/torerr.c + src/lib/err/backtrace.c \ + src/lib/err/torerr.c \ + src/lib/err/torerr_sys.c src_lib_libtor_err_testing_a_SOURCES = \ $(src_lib_libtor_err_a_SOURCES) @@ -16,4 +17,5 @@ src_lib_libtor_err_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) noinst_HEADERS += \ src/lib/err/backtrace.h \ - src/lib/err/torerr.h + src/lib/err/torerr.h \ + src/lib/err/torerr_sys.h diff --git a/src/lib/err/torerr.c b/src/lib/err/torerr.c index f9e139f967..e9de86837f 100644 --- a/src/lib/err/torerr.c +++ b/src/lib/err/torerr.c @@ -122,6 +122,16 @@ tor_log_set_sigsafe_err_fds(const int *fds, int n) n_sigsafe_log_fds = n; } +/** + * Reset the list of emergency error fds to its default. + */ +void +tor_log_reset_sigsafe_err_fds(void) +{ + int fds[] = { STDERR_FILENO }; + tor_log_set_sigsafe_err_fds(fds, 1); +} + /** * Set the granularity (in ms) to use when reporting fatal errors outside * the logging system. diff --git a/src/lib/err/torerr.h b/src/lib/err/torerr.h index d4bba6916f..b415ef73ef 100644 --- a/src/lib/err/torerr.h +++ b/src/lib/err/torerr.h @@ -39,6 +39,7 @@ void tor_raw_assertion_failed_msg_(const char *file, int line, void tor_log_err_sigsafe(const char *m, ...); int tor_log_get_sigsafe_err_fds(const int **out); void tor_log_set_sigsafe_err_fds(const int *fds, int n); +void tor_log_reset_sigsafe_err_fds(void); void tor_log_sigsafe_err_set_granularity(int ms); int format_hex_number_sigsafe(unsigned long x, char *buf, int max_len); diff --git a/src/lib/err/torerr_sys.c b/src/lib/err/torerr_sys.c new file mode 100644 index 0000000000..54666f4106 --- /dev/null +++ b/src/lib/err/torerr_sys.c @@ -0,0 +1,39 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file torerr_sys.c + * \brief Subsystem object for the error handling subsystem. + **/ + +#include "orconfig.h" +#include "lib/err/backtrace.h" +#include "lib/err/torerr.h" +#include "lib/err/torerr_sys.h" +#include "lib/subsys/subsys.h" +#include "lib/version/torversion.h" + +#include + +static int +torerr_subsys_init(void) +{ + configure_backtrace_handler(get_version()); + tor_log_reset_sigsafe_err_fds(); + + return 0; +} +static void +torerr_subsys_shutdown(void) +{ + tor_log_reset_sigsafe_err_fds(); + clean_up_backtrace_handler(); +} + +const subsys_fns_t sys_torerr = { + .name = "err", + .level = -100, + .supported = true, + .initialize = torerr_subsys_init, + .shutdown = torerr_subsys_shutdown +}; diff --git a/src/lib/err/torerr_sys.h b/src/lib/err/torerr_sys.h new file mode 100644 index 0000000000..b56270d538 --- /dev/null +++ b/src/lib/err/torerr_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file torerr_sys.h + * \brief Declare subsystem object for torerr.c + **/ + +#ifndef TOR_TORERR_SYS_H +#define TOR_TORERR_SYS_H + +extern const struct subsys_fns_t sys_torerr; + +#endif /* !defined(TOR_TORERR_SYS_H) */ From 178c1821b2115972ce3c3f194d1fcbd0d75ca364 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 12:55:10 -0400 Subject: [PATCH 05/22] Make the windows process parameter initialization a subsystem Also, move it from "main" into lib/process --- src/app/main/main.c | 29 --------------- src/app/main/subsystem_list.c | 2 + src/lib/process/.may_include | 1 + src/lib/process/include.am | 6 ++- src/lib/process/winprocess_sys.c | 64 ++++++++++++++++++++++++++++++++ src/lib/process/winprocess_sys.h | 14 +++++++ 6 files changed, 85 insertions(+), 31 deletions(-) create mode 100644 src/lib/process/winprocess_sys.c create mode 100644 src/lib/process/winprocess_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index e3d7610c82..1e4cd37feb 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1388,35 +1388,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) { int result = 0; -#ifdef _WIN32 -#ifndef HeapEnableTerminationOnCorruption -#define HeapEnableTerminationOnCorruption 1 -#endif - /* On heap corruption, just give up; don't try to play along. */ - HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); - - /* SetProcessDEPPolicy is only supported on 32-bit Windows. - * (On 64-bit Windows it always fails, and some compilers don't like the - * PSETDEP cast.) - * 32-bit Windows defines _WIN32. - * 64-bit Windows defines _WIN32 and _WIN64. */ -#ifndef _WIN64 - /* Call SetProcessDEPPolicy to permanently enable DEP. - The function will not resolve on earlier versions of Windows, - and failure is not dangerous. */ - HMODULE hMod = GetModuleHandleA("Kernel32.dll"); - if (hMod) { - typedef BOOL (WINAPI *PSETDEP)(DWORD); - PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod, - "SetProcessDEPPolicy"); - if (setdeppolicy) { - /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */ - setdeppolicy(3); - } - } -#endif /* !defined(_WIN64) */ -#endif /* defined(_WIN32) */ - #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_); #endif diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index 244dbadbd9..0f7d5d2ccc 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -9,6 +9,7 @@ #include "lib/cc/torint.h" #include "lib/err/torerr_sys.h" +#include "lib/process/winprocess_sys.h" #include @@ -16,6 +17,7 @@ * Global list of the subsystems in Tor, in the order of their initialization. **/ const subsys_fns_t *tor_subsystems[] = { + &sys_winprocess, &sys_torerr, }; diff --git a/src/lib/process/.may_include b/src/lib/process/.may_include index 05414d2a96..a2d57a52f3 100644 --- a/src/lib/process/.may_include +++ b/src/lib/process/.may_include @@ -11,6 +11,7 @@ lib/malloc/*.h lib/net/*.h lib/process/*.h lib/string/*.h +lib/subsys/*.h lib/testsupport/*.h lib/thread/*.h diff --git a/src/lib/process/include.am b/src/lib/process/include.am index c6cc3a6699..2aa30cc3c1 100644 --- a/src/lib/process/include.am +++ b/src/lib/process/include.am @@ -12,7 +12,8 @@ src_lib_libtor_process_a_SOURCES = \ src/lib/process/restrict.c \ src/lib/process/setuid.c \ src/lib/process/subprocess.c \ - src/lib/process/waitpid.c + src/lib/process/waitpid.c \ + src/lib/process/winprocess_sys.c src_lib_libtor_process_testing_a_SOURCES = \ $(src_lib_libtor_process_a_SOURCES) @@ -26,4 +27,5 @@ noinst_HEADERS += \ src/lib/process/restrict.h \ src/lib/process/setuid.h \ src/lib/process/subprocess.h \ - src/lib/process/waitpid.h + src/lib/process/waitpid.h \ + src/lib/process/winprocess_sys.h diff --git a/src/lib/process/winprocess_sys.c b/src/lib/process/winprocess_sys.c new file mode 100644 index 0000000000..e00f94c915 --- /dev/null +++ b/src/lib/process/winprocess_sys.c @@ -0,0 +1,64 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file winprocess_sys.c + * \brief Subsystem object for windows process setup. + **/ + +#include "orconfig.h" +#include "lib/subsys/subsys.h" +#include "lib/process/winprocess_sys.h" + +#include +#include + +#ifdef _WIN32 +#include + +#define WINPROCESS_SYS_ENABLED true + +static int +init_windows_process_params(void) +{ +#ifndef HeapEnableTerminationOnCorruption +#define HeapEnableTerminationOnCorruption 1 +#endif + + /* On heap corruption, just give up; don't try to play along. */ + HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); + + /* SetProcessDEPPolicy is only supported on 32-bit Windows. + * (On 64-bit Windows it always fails, and some compilers don't like the + * PSETDEP cast.) + * 32-bit Windows defines _WIN32. + * 64-bit Windows defines _WIN32 and _WIN64. */ +#ifndef _WIN64 + /* Call SetProcessDEPPolicy to permanently enable DEP. + The function will not resolve on earlier versions of Windows, + and failure is not dangerous. */ + HMODULE hMod = GetModuleHandleA("Kernel32.dll"); + if (hMod) { + typedef BOOL (WINAPI *PSETDEP)(DWORD); + PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod, + "SetProcessDEPPolicy"); + if (setdeppolicy) { + /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */ + setdeppolicy(3); + } + } +#endif /* !defined(_WIN64) */ + + return 0; +} +#else /* !defined(_WIN32) */ +#define WINPROCESS_SYS_ENABLED false +#define init_windows_process_params NULL +#endif /* defined(_WIN32) */ + +const subsys_fns_t sys_winprocess = { + .name = "winprocess", + .level = -100, + .supported = WINPROCESS_SYS_ENABLED, + .initialize = init_windows_process_params, +}; diff --git a/src/lib/process/winprocess_sys.h b/src/lib/process/winprocess_sys.h new file mode 100644 index 0000000000..cb096e0c92 --- /dev/null +++ b/src/lib/process/winprocess_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file winprocess_sys.h + * \brief Declare subsystem object for winprocess.c + **/ + +#ifndef TOR_WINPROCESS_SYS_H +#define TOR_WINPROCESS_SYS_H + +extern const struct subsys_fns_t sys_winprocess; + +#endif /* !defined(TOR_WINPROCESS_SYS_H) */ From b8c50eabfee1bd9f5ed03f8ec569cc53b980f1d1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 13:14:43 -0400 Subject: [PATCH 06/22] Add a subsystem for our threading support --- src/app/main/main.c | 1 - src/app/main/subsystem_list.c | 2 ++ src/lib/thread/.may_include | 1 + src/lib/thread/compat_threads.c | 16 ++++++++++++++++ src/lib/thread/include.am | 5 +++-- src/lib/thread/thread_sys.h | 14 ++++++++++++++ 6 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/lib/thread/thread_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 1e4cd37feb..21a2832781 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1397,7 +1397,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) init_protocol_warning_severity_level(); update_approx_time(time(NULL)); - tor_threads_init(); tor_compress_init(); init_logging(0); monotime_init(); diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index 0f7d5d2ccc..c3b731ca39 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/process/winprocess_sys.h" +#include "lib/thread/thread_sys.h" #include @@ -19,6 +20,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_winprocess, &sys_torerr, + &sys_threads, }; const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems); diff --git a/src/lib/thread/.may_include b/src/lib/thread/.may_include index 93ad0cd734..c26a426923 100644 --- a/src/lib/thread/.may_include +++ b/src/lib/thread/.may_include @@ -2,5 +2,6 @@ orconfig.h lib/cc/*.h lib/lock/*.h lib/log/*.h +lib/subsys/*.h lib/testsupport/*.h lib/thread/*.h diff --git a/src/lib/thread/compat_threads.c b/src/lib/thread/compat_threads.c index 7f1970af45..3d41faa8ce 100644 --- a/src/lib/thread/compat_threads.c +++ b/src/lib/thread/compat_threads.c @@ -14,9 +14,11 @@ #include "orconfig.h" #include #include "lib/thread/threads.h" +#include "lib/thread/thread_sys.h" #include "lib/log/log.h" #include "lib/log/util_bug.h" +#include "lib/subsys/subsys.h" #include @@ -109,3 +111,17 @@ atomic_counter_exchange(atomic_counter_t *counter, size_t newval) return oldval; } #endif /* !defined(HAVE_WORKING_STDATOMIC) */ + +static int +sys_threads_initialize(void) +{ + tor_threads_init(); + return 0; +} + +const subsys_fns_t sys_threads = { + .name = "threads", + .supported = true, + .level = -95, + .initialize = sys_threads_initialize, +}; diff --git a/src/lib/thread/include.am b/src/lib/thread/include.am index 9ec23d166e..695795a2c8 100644 --- a/src/lib/thread/include.am +++ b/src/lib/thread/include.am @@ -23,5 +23,6 @@ src_lib_libtor_thread_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS) src_lib_libtor_thread_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) noinst_HEADERS += \ - src/lib/thread/threads.h \ - src/lib/thread/numcpus.h + src/lib/thread/numcpus.h \ + src/lib/thread/thread_sys.h \ + src/lib/thread/threads.h diff --git a/src/lib/thread/thread_sys.h b/src/lib/thread/thread_sys.h new file mode 100644 index 0000000000..984abe88e8 --- /dev/null +++ b/src/lib/thread/thread_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file threads_sys.h + * \brief Declare subsystem object for threads library + **/ + +#ifndef TOR_THREADS_SYS_H +#define TOR_THREADS_SYS_H + +extern const struct subsys_fns_t sys_threads; + +#endif /* !defined(TOR_THREADS_SYS_H) */ From d3e4afcc9b835e0f862207ef16d7e706ceea9ce1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 13:26:33 -0400 Subject: [PATCH 07/22] Turn the logging code into a subsystem --- src/app/main/main.c | 3 --- src/app/main/subsystem_list.c | 2 ++ src/lib/log/.may_include | 1 + src/lib/log/include.am | 2 ++ src/lib/log/log.c | 1 + src/lib/log/log_sys.c | 35 +++++++++++++++++++++++++++++++++++ src/lib/log/log_sys.h | 14 ++++++++++++++ 7 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/lib/log/log_sys.c create mode 100644 src/lib/log/log_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 21a2832781..f44f3475dd 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -819,9 +819,7 @@ tor_free_all(int postfork) /* Stuff in util.c and address.c*/ if (!postfork) { - escaped(NULL); esc_router_info(NULL); - logs_free_all(); /* free log strings. do this last so logs keep working. */ } } @@ -1398,7 +1396,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) update_approx_time(time(NULL)); tor_compress_init(); - init_logging(0); monotime_init(); int argc = tor_cfg->argc + tor_cfg->argc_owned; diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index c3b731ca39..4a2994ec49 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -11,6 +11,7 @@ #include "lib/err/torerr_sys.h" #include "lib/process/winprocess_sys.h" #include "lib/thread/thread_sys.h" +#include "lib/log/log_sys.h" #include @@ -21,6 +22,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_winprocess, &sys_torerr, &sys_threads, + &sys_logging, }; const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems); diff --git a/src/lib/log/.may_include b/src/lib/log/.may_include index 7ca1863a52..11c87f0a0d 100644 --- a/src/lib/log/.may_include +++ b/src/lib/log/.may_include @@ -9,6 +9,7 @@ lib/lock/*.h lib/log/*.h lib/malloc/*.h lib/string/*.h +lib/subsys/*.h lib/testsupport/*.h lib/version/*.h lib/wallclock/*.h diff --git a/src/lib/log/include.am b/src/lib/log/include.am index c6f404e269..9d3dbe3104 100644 --- a/src/lib/log/include.am +++ b/src/lib/log/include.am @@ -9,6 +9,7 @@ src_lib_libtor_log_a_SOURCES = \ src/lib/log/escape.c \ src/lib/log/ratelim.c \ src/lib/log/log.c \ + src/lib/log/log_sys.c \ src/lib/log/util_bug.c if WIN32 @@ -24,5 +25,6 @@ noinst_HEADERS += \ src/lib/log/escape.h \ src/lib/log/ratelim.h \ src/lib/log/log.h \ + src/lib/log/log_sys.h \ src/lib/log/util_bug.h \ src/lib/log/win32err.h diff --git a/src/lib/log/log.c b/src/lib/log/log.c index bc7b36dcb9..46107fe848 100644 --- a/src/lib/log/log.c +++ b/src/lib/log/log.c @@ -32,6 +32,7 @@ #define LOG_PRIVATE #include "lib/log/log.h" +#include "lib/log/log_sys.h" #include "lib/version/git_revision.h" #include "lib/log/ratelim.h" #include "lib/lock/compat_mutex.h" diff --git a/src/lib/log/log_sys.c b/src/lib/log/log_sys.c new file mode 100644 index 0000000000..94ec97fdc1 --- /dev/null +++ b/src/lib/log/log_sys.c @@ -0,0 +1,35 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file log_sys.c + * \brief Setup and tear down the logging module. + **/ + +#include "orconfig.h" +#include "lib/subsys/subsys.h" +#include "lib/log/escape.h" +#include "lib/log/log.h" +#include "lib/log/log_sys.h" + +static int +init_logging_subsys(void) +{ + init_logging(0); + return 0; +} + +static void +shutdown_logging_subsys(void) +{ + logs_free_all(); + escaped(NULL); +} + +const subsys_fns_t sys_logging = { + .name = "log", + .supported = true, + .level = -90, + .initialize = init_logging_subsys, + .shutdown = shutdown_logging_subsys, +}; diff --git a/src/lib/log/log_sys.h b/src/lib/log/log_sys.h new file mode 100644 index 0000000000..f7afbb279d --- /dev/null +++ b/src/lib/log/log_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file log_sys.h + * \brief Declare subsystem object for the logging module. + **/ + +#ifndef TOR_LOG_SYS_H +#define TOR_LOG_SYS_H + +extern const struct subsys_fns_t sys_logging; + +#endif /* !defined(TOR_LOG_SYS_H) */ From a0ee54549fec3ae710ab5e3623d707bd08adcafe Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 13:34:07 -0400 Subject: [PATCH 08/22] Turn the wallclock module into a subsystem. (This may be slightly gratuitous.) --- src/app/main/main.c | 1 - src/app/main/subsystem_list.c | 4 +++- src/lib/wallclock/.may_include | 1 + src/lib/wallclock/approx_time.c | 16 ++++++++++++++++ src/lib/wallclock/include.am | 3 ++- src/lib/wallclock/wallclock_sys.h | 14 ++++++++++++++ 6 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/lib/wallclock/wallclock_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index f44f3475dd..5740efb0b6 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1394,7 +1394,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) init_protocol_warning_severity_level(); - update_approx_time(time(NULL)); tor_compress_init(); monotime_init(); diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index 4a2994ec49..3d03a9a4df 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -9,9 +9,10 @@ #include "lib/cc/torint.h" #include "lib/err/torerr_sys.h" +#include "lib/log/log_sys.h" #include "lib/process/winprocess_sys.h" #include "lib/thread/thread_sys.h" -#include "lib/log/log_sys.h" +#include "lib/wallclock/wallclock_sys.h" #include @@ -21,6 +22,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_winprocess, &sys_torerr, + &sys_wallclock, &sys_threads, &sys_logging, }; diff --git a/src/lib/wallclock/.may_include b/src/lib/wallclock/.may_include index dc010da063..ce7a26472b 100644 --- a/src/lib/wallclock/.may_include +++ b/src/lib/wallclock/.may_include @@ -3,4 +3,5 @@ lib/cc/*.h lib/err/*.h lib/wallclock/*.h lib/string/*.h +lib/subsys/*.h lib/testsupport/*.h diff --git a/src/lib/wallclock/approx_time.c b/src/lib/wallclock/approx_time.c index bb9a292369..c7a7ae9bd7 100644 --- a/src/lib/wallclock/approx_time.c +++ b/src/lib/wallclock/approx_time.c @@ -9,7 +9,9 @@ **/ #include "orconfig.h" +#include "lib/subsys/subsys.h" #include "lib/wallclock/approx_time.h" +#include "lib/wallclock/wallclock_sys.h" /* ===== * Cached time @@ -41,3 +43,17 @@ update_approx_time(time_t now) cached_approx_time = now; } #endif /* !defined(TIME_IS_FAST) */ + +static int +init_wallclock_subsys(void) +{ + update_approx_time(time(NULL)); + return 0; +} + +const subsys_fns_t sys_wallclock = { + .name = "wallclock", + .supported = true, + .level = -99, + .initialize = init_wallclock_subsys, +}; diff --git a/src/lib/wallclock/include.am b/src/lib/wallclock/include.am index 1961639bd7..2351252e0c 100644 --- a/src/lib/wallclock/include.am +++ b/src/lib/wallclock/include.am @@ -19,4 +19,5 @@ noinst_HEADERS += \ src/lib/wallclock/approx_time.h \ src/lib/wallclock/timeval.h \ src/lib/wallclock/time_to_tm.h \ - src/lib/wallclock/tor_gettimeofday.h + src/lib/wallclock/tor_gettimeofday.h \ + src/lib/wallclock/wallclock_sys.h diff --git a/src/lib/wallclock/wallclock_sys.h b/src/lib/wallclock/wallclock_sys.h new file mode 100644 index 0000000000..e009578a83 --- /dev/null +++ b/src/lib/wallclock/wallclock_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file wallclock_sys.h + * \brief Declare subsystem object for the wallclock module. + **/ + +#ifndef TOR_WALLCLOCK_SYS_H +#define TOR_WALLCLOCK_SYS_H + +extern const struct subsys_fns_t sys_wallclock; + +#endif /* !defined(TOR_WALLCLOCK_SYS_H) */ From 05b54f6a6a24ebdb47de4f7e41cf94f2f6be93bd Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 1 Nov 2018 14:13:36 -0400 Subject: [PATCH 09/22] Use subsystems manager for subsystems used in tests. --- src/lib/subsys/subsys.h | 4 ++++ src/test/bench.c | 7 ++++--- src/test/fuzz/fuzzing_common.c | 9 ++++----- src/test/testing_common.c | 8 +++----- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h index 012b218da7..25451bc450 100644 --- a/src/lib/subsys/subsys.h +++ b/src/lib/subsys/subsys.h @@ -65,4 +65,8 @@ typedef struct subsys_fns_t { #define MIN_SUBSYS_LEVEL -100 #define MAX_SUBSYS_LEVEL 100 +/* All tor "libraries" (in src/libs) should have a subsystem level equal to or + * less than this value. */ +#define SUBSYS_LEVEL_LIBS -10 + #endif diff --git a/src/test/bench.c b/src/test/bench.c index 9da1b46a1b..ff8707d41c 100644 --- a/src/test/bench.c +++ b/src/test/bench.c @@ -24,6 +24,7 @@ #include "core/or/circuitlist.h" #include "app/config/config.h" +#include "app/main/subsysmgr.h" #include "lib/crypt_ops/crypto_curve25519.h" #include "lib/crypt_ops/crypto_dh.h" #include "core/crypto/onion_ntor.h" @@ -690,9 +691,10 @@ main(int argc, const char **argv) char *errmsg; or_options_t *options; - tor_threads_init(); + subsystems_init_upto(SUBSYS_LEVEL_LIBS); + flush_log_messages_from_startup(); + tor_compress_init(); - init_logging(1); if (argc == 4 && !strcmp(argv[1], "diff")) { const int N = 200; @@ -739,7 +741,6 @@ main(int argc, const char **argv) init_protocol_warning_severity_level(); options = options_new(); - init_logging(1); options->command = CMD_RUN_UNITTESTS; options->DataDirectory = tor_strdup(""); options->KeyDirectory = tor_strdup(""); diff --git a/src/test/fuzz/fuzzing_common.c b/src/test/fuzz/fuzzing_common.c index 879f9e74dc..21aa07cfe2 100644 --- a/src/test/fuzz/fuzzing_common.c +++ b/src/test/fuzz/fuzzing_common.c @@ -3,6 +3,7 @@ #define CRYPTO_ED25519_PRIVATE #include "orconfig.h" #include "core/or/or.h" +#include "app/main/subsysmgr.h" #include "lib/err/backtrace.h" #include "app/config/config.h" #include "test/fuzz/fuzzing.h" @@ -95,12 +96,10 @@ disable_signature_checking(void) static void global_init(void) { - tor_threads_init(); - tor_compress_init(); + subsystems_init_upto(SUBSYS_LEVEL_LIBS); + flush_log_messages_from_startup(); - /* Initialise logging first */ - init_logging(1); - configure_backtrace_handler(get_version()); + tor_compress_init(); if (crypto_global_init(0, NULL, NULL) < 0) abort(); diff --git a/src/test/testing_common.c b/src/test/testing_common.c index 8d648ee175..eef393d3a8 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -26,6 +26,7 @@ #include "lib/evloop/compat_libevent.h" #include "lib/crypt_ops/crypto_init.h" #include "lib/version/torversion.h" +#include "app/main/subsysmgr.h" #include #ifdef HAVE_FCNTL_H @@ -251,12 +252,9 @@ main(int c, const char **v) int loglevel = LOG_ERR; int accel_crypto = 0; - /* We must initialise logs before we call tor_assert() */ - init_logging(1); + subsystems_init_upto(SUBSYS_LEVEL_LIBS); - update_approx_time(time(NULL)); options = options_new(); - tor_threads_init(); tor_compress_init(); network_init(); @@ -268,7 +266,6 @@ main(int c, const char **v) tor_libevent_initialize(&cfg); control_initialize_event_queue(); - configure_backtrace_handler(get_version()); for (i_out = i = 1; i < c; ++i) { if (!strcmp(v[i], "--warn")) { @@ -295,6 +292,7 @@ main(int c, const char **v) s.masks[LOG_WARN-LOG_ERR] |= LD_BUG; add_stream_log(&s, "", fileno(stdout)); } + flush_log_messages_from_startup(); init_protocol_warning_severity_level(); options->command = CMD_RUN_UNITTESTS; From cfe5b35edb38cef6312ef0b4ae44fb0e20342706 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 Nov 2018 11:11:21 -0400 Subject: [PATCH 10/22] 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(-) create mode 100644 src/lib/net/network_sys.c create mode 100644 src/lib/net/network_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 5740efb0b6..3e80725b9a 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 3d03a9a4df..cb186c14d9 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 13b209bbed..f93f0e1552 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 ff0967e786..8a88f0f2ae 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 0000000000..c9d33a94d3 --- /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 +#include +#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 0000000000..62b778bb66 --- /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 eef393d3a8..818bb58c9a 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; From 50436ccea4bd200e45196ccce7acff28f293a4de Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 Nov 2018 11:21:06 -0400 Subject: [PATCH 11/22] Add crypto module as a subsystem. --- src/app/main/main.c | 7 ------- src/app/main/subsystem_list.c | 2 ++ src/lib/crypt_ops/.may_include | 1 + src/lib/crypt_ops/crypto_init.c | 26 ++++++++++++++++++++++++++ src/lib/crypt_ops/crypto_sys.h | 14 ++++++++++++++ src/lib/crypt_ops/include.am | 1 + src/test/testing_common.c | 2 -- 7 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/lib/crypt_ops/crypto_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 3e80725b9a..74c3c41e5b 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -535,12 +535,6 @@ tor_init(int argc, char *argv[]) tor_snprintf(progname, sizeof(progname), "Tor %s", get_version()); log_set_application_name(progname); - /* Set up the crypto nice and early */ - if (crypto_early_init() < 0) { - log_err(LD_GENERAL, "Unable to initialize the crypto subsystem!"); - return -1; - } - /* Initialize the history structures. */ rep_hist_init(); /* Initialize the service cache. */ @@ -859,7 +853,6 @@ tor_cleanup(void) later, if it makes shutdown unacceptably slow. But for now, leave it here: it's helped us catch bugs in the past. */ - crypto_global_cleanup(); } /** Read/create keys as needed, and echo our fingerprint to stdout. */ diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index cb186c14d9..dd64568226 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -8,6 +8,7 @@ #include "lib/cc/compat_compiler.h" #include "lib/cc/torint.h" +#include "lib/crypt_ops/crypto_sys.h" #include "lib/err/torerr_sys.h" #include "lib/log/log_sys.h" #include "lib/net/network_sys.h" @@ -27,6 +28,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_threads, &sys_logging, &sys_network, + &sys_crypto, }; const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems); diff --git a/src/lib/crypt_ops/.may_include b/src/lib/crypt_ops/.may_include index a0fa4ec05c..352fde858c 100644 --- a/src/lib/crypt_ops/.may_include +++ b/src/lib/crypt_ops/.may_include @@ -12,6 +12,7 @@ lib/malloc/*.h lib/intmath/*.h lib/sandbox/*.h lib/string/*.h +lib/subsys/*.h lib/testsupport/testsupport.h lib/thread/*.h lib/log/*.h diff --git a/src/lib/crypt_ops/crypto_init.c b/src/lib/crypt_ops/crypto_init.c index 9d6e2da0d0..cc7865ef72 100644 --- a/src/lib/crypt_ops/crypto_init.c +++ b/src/lib/crypt_ops/crypto_init.c @@ -20,6 +20,9 @@ #include "lib/crypt_ops/crypto_openssl_mgt.h" #include "lib/crypt_ops/crypto_nss_mgt.h" #include "lib/crypt_ops/crypto_rand.h" +#include "lib/crypt_ops/crypto_sys.h" + +#include "lib/subsys/subsys.h" #include "siphash.h" @@ -202,3 +205,26 @@ tor_is_using_nss(void) return 0; #endif } + +static int +init_crypto_sys(void) +{ + if (crypto_early_init() < 0) + return -1; + crypto_dh_init(); + return 0; +} + +static void +shutdown_crypto_sys(void) +{ + crypto_global_cleanup(); +} + +const struct subsys_fns_t sys_crypto = { + .name = "crypto", + .supported = true, + .level = -60, + .initialize = init_crypto_sys, + .shutdown = shutdown_crypto_sys, +}; diff --git a/src/lib/crypt_ops/crypto_sys.h b/src/lib/crypt_ops/crypto_sys.h new file mode 100644 index 0000000000..31644d088b --- /dev/null +++ b/src/lib/crypt_ops/crypto_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file log_crypto.h + * \brief Declare subsystem object for the crypto module. + **/ + +#ifndef TOR_CRYPTO_SYS_H +#define TOR_CRYPTO_SYS_H + +extern const struct subsys_fns_t sys_crypto; + +#endif /* !defined(TOR_CRYPTO_SYS_H) */ diff --git a/src/lib/crypt_ops/include.am b/src/lib/crypt_ops/include.am index 1022096fdc..d0ccc13bff 100644 --- a/src/lib/crypt_ops/include.am +++ b/src/lib/crypt_ops/include.am @@ -66,5 +66,6 @@ noinst_HEADERS += \ src/lib/crypt_ops/crypto_rand.h \ src/lib/crypt_ops/crypto_rsa.h \ src/lib/crypt_ops/crypto_s2k.h \ + src/lib/crypt_ops/crypto_sys.h \ src/lib/crypt_ops/crypto_util.h \ src/lib/crypt_ops/digestset.h diff --git a/src/test/testing_common.c b/src/test/testing_common.c index 818bb58c9a..d4c5632334 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -331,8 +331,6 @@ main(int c, const char **v) free_pregenerated_keys(); - crypto_global_cleanup(); - if (have_failed) return 1; else From cad61f0f6de48c6eab6e811a081f154b03de57b8 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 Nov 2018 18:00:56 -0400 Subject: [PATCH 12/22] Move prefork, postfork, and thread-exit hooks into subsys So far, crypto is the only module that uses them, but others are likely to do so in the future. --- src/app/config/config.c | 5 +-- src/app/main/subsysmgr.c | 57 +++++++++++++++++++++++++++++++++ src/app/main/subsysmgr.h | 4 +++ src/lib/crypt_ops/crypto_init.c | 3 ++ src/lib/subsys/subsys.h | 16 +++++++++ src/test/testing_common.c | 4 +-- 6 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/app/config/config.c b/src/app/config/config.c index 7b49387bcf..76df7ec67e 100644 --- a/src/app/config/config.c +++ b/src/app/config/config.c @@ -64,6 +64,7 @@ #include "app/config/confparse.h" #include "app/config/statefile.h" #include "app/main/main.h" +#include "app/main/subsysmgr.h" #include "core/mainloop/connection.h" #include "core/mainloop/cpuworker.h" #include "core/mainloop/mainloop.h" @@ -1393,10 +1394,10 @@ options_act_reversible(const or_options_t *old_options, char **msg) * processes. */ if (running_tor && options->RunAsDaemon) { if (! start_daemon_has_been_called()) - crypto_prefork(); + subsystems_prefork(); /* No need to roll back, since you can't change the value. */ if (start_daemon()) - crypto_postfork(); + subsystems_postfork(); } #ifdef HAVE_SYSTEMD diff --git a/src/app/main/subsysmgr.c b/src/app/main/subsysmgr.c index 7974f2d238..05803ee946 100644 --- a/src/app/main/subsysmgr.c +++ b/src/app/main/subsysmgr.c @@ -128,3 +128,60 @@ subsystems_shutdown_downto(int target_level) sys_initialized[i] = false; } } + +/** + * Run pre-fork code on all subsystems that declare any + **/ +void +subsystems_prefork(void) +{ + check_and_setup(); + + for (int i = (int)n_tor_subsystems - 1; i >= 0; --i) { + const subsys_fns_t *sys = tor_subsystems[i]; + if (!sys->supported) + continue; + if (! sys_initialized[i]) + continue; + if (sys->prefork) + sys->prefork(); + } +} + +/** + * Run post-fork code on all subsystems that declare any + **/ +void +subsystems_postfork(void) +{ + check_and_setup(); + + for (unsigned i = 0; i < n_tor_subsystems; ++i) { + const subsys_fns_t *sys = tor_subsystems[i]; + if (!sys->supported) + continue; + if (! sys_initialized[i]) + continue; + if (sys->postfork) + sys->postfork(); + } +} + +/** + * Run thread-clanup code on all subsystems that declare any + **/ +void +subsystems_thread_cleanup(void) +{ + check_and_setup(); + + for (int i = (int)n_tor_subsystems - 1; i >= 0; --i) { + const subsys_fns_t *sys = tor_subsystems[i]; + if (!sys->supported) + continue; + if (! sys_initialized[i]) + continue; + if (sys->thread_cleanup) + sys->thread_cleanup(); + } +} diff --git a/src/app/main/subsysmgr.h b/src/app/main/subsysmgr.h index c9b892eee4..4b3cad62ad 100644 --- a/src/app/main/subsysmgr.h +++ b/src/app/main/subsysmgr.h @@ -17,4 +17,8 @@ int subsystems_init_upto(int level); void subsystems_shutdown(void); void subsystems_shutdown_downto(int level); +void subsystems_prefork(void); +void subsystems_postfork(void); +void subsystems_thread_cleanup(void); + #endif diff --git a/src/lib/crypt_ops/crypto_init.c b/src/lib/crypt_ops/crypto_init.c index cc7865ef72..a03f5eff7c 100644 --- a/src/lib/crypt_ops/crypto_init.c +++ b/src/lib/crypt_ops/crypto_init.c @@ -227,4 +227,7 @@ const struct subsys_fns_t sys_crypto = { .level = -60, .initialize = init_crypto_sys, .shutdown = shutdown_crypto_sys, + .prefork = crypto_prefork, + .postfork = crypto_postfork, + .thread_cleanup = crypto_thread_cleanup, }; diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h index 25451bc450..b06d67e624 100644 --- a/src/lib/subsys/subsys.h +++ b/src/lib/subsys/subsys.h @@ -53,6 +53,22 @@ typedef struct subsys_fns_t { **/ int (*add_pubsub)(struct dispatch_connector_t *); + /** + * Perform any necessary pre-fork cleanup. This function may not fail. + */ + void (*prefork)(void); + + /** + * Perform any necessary post-fork setup. This function may not fail. + */ + void (*postfork)(void); + + /** + * Free any thread-local resources held by this subsystem. Called before + * the thread exits. + */ + void (*thread_cleanup)(void); + /** * Free all resources held by this subsystem. * diff --git a/src/test/testing_common.c b/src/test/testing_common.c index d4c5632334..1362f29711 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -232,12 +232,12 @@ void tinytest_prefork(void) { free_pregenerated_keys(); - crypto_prefork(); + subsystems_prefork(); } void tinytest_postfork(void) { - crypto_postfork(); + subsystems_postfork(); init_pregenerated_keys(); } From 207253df8d7c040840c7f4305534e6979bfc7bf7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 Nov 2018 18:09:44 -0400 Subject: [PATCH 13/22] Move monotonic time setup into a subsystem --- src/app/main/main.c | 2 -- src/app/main/subsystem_list.c | 2 ++ src/lib/time/.may_include | 1 + src/lib/time/include.am | 2 ++ src/lib/time/time_sys.c | 26 ++++++++++++++++++++++++++ src/lib/time/time_sys.h | 14 ++++++++++++++ src/test/testing_common.c | 2 -- 7 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/lib/time/time_sys.c create mode 100644 src/lib/time/time_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 74c3c41e5b..bb2e9f5cdb 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1248,7 +1248,6 @@ static int run_tor_main_loop(void) { handle_signals(); - monotime_init(); timers_initialize(); initialize_mainloop_events(); @@ -1369,7 +1368,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) init_protocol_warning_severity_level(); tor_compress_init(); - monotime_init(); int argc = tor_cfg->argc + tor_cfg->argc_owned; char **argv = tor_calloc(argc, sizeof(char*)); diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index dd64568226..a9189b9941 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -14,6 +14,7 @@ #include "lib/net/network_sys.h" #include "lib/process/winprocess_sys.h" #include "lib/thread/thread_sys.h" +#include "lib/time/time_sys.h" #include "lib/wallclock/wallclock_sys.h" #include @@ -27,6 +28,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_wallclock, &sys_threads, &sys_logging, + &sys_time, &sys_network, &sys_crypto, }; diff --git a/src/lib/time/.may_include b/src/lib/time/.may_include index 2c7e37a836..40a18805ac 100644 --- a/src/lib/time/.may_include +++ b/src/lib/time/.may_include @@ -4,6 +4,7 @@ lib/cc/*.h lib/err/*.h lib/intmath/*.h lib/log/*.h +lib/subsys/*.h lib/time/*.h lib/wallclock/*.h diff --git a/src/lib/time/include.am b/src/lib/time/include.am index a3f93a3744..dae16f49ac 100644 --- a/src/lib/time/include.am +++ b/src/lib/time/include.am @@ -7,6 +7,7 @@ endif src_lib_libtor_time_a_SOURCES = \ src/lib/time/compat_time.c \ + src/lib/time/time_sys.c \ src/lib/time/tvdiff.c src_lib_libtor_time_testing_a_SOURCES = \ @@ -16,4 +17,5 @@ src_lib_libtor_time_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) noinst_HEADERS += \ src/lib/time/compat_time.h \ + src/lib/time/time_sys.h \ src/lib/time/tvdiff.h diff --git a/src/lib/time/time_sys.c b/src/lib/time/time_sys.c new file mode 100644 index 0000000000..2303874f29 --- /dev/null +++ b/src/lib/time/time_sys.c @@ -0,0 +1,26 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file time_sys.c + * \brief Subsystem object for monotime setup. + **/ + +#include "orconfig.h" +#include "lib/subsys/subsys.h" +#include "lib/time/time_sys.h" +#include "lib/time/compat_time.h" + +static int +init_time_sys(void) +{ + monotime_init(); + return 0; +} + +const subsys_fns_t sys_time = { + .name = "time", + .level = -90, + .supported = true, + .initialize = init_time_sys, +}; diff --git a/src/lib/time/time_sys.h b/src/lib/time/time_sys.h new file mode 100644 index 0000000000..0f1aebc268 --- /dev/null +++ b/src/lib/time/time_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file log_time.h + * \brief Declare subsystem object for the time module. + **/ + +#ifndef TOR_TIME_SYS_H +#define TOR_TIME_SYS_H + +extern const struct subsys_fns_t sys_time; + +#endif /* !defined(TOR_TIME_SYS_H) */ diff --git a/src/test/testing_common.c b/src/test/testing_common.c index 1362f29711..333dbc436f 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(); - monotime_init(); - struct tor_libevent_cfg cfg; memset(&cfg, 0, sizeof(cfg)); tor_libevent_initialize(&cfg); From 019a044e5e6586fb42a171cb98aea15a72bd5a13 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 Nov 2018 18:34:56 -0400 Subject: [PATCH 14/22] Turn "compress" into a subsystem. --- src/app/main/main.c | 2 -- src/app/main/subsystem_list.c | 2 ++ src/lib/compress/.may_include | 1 + src/lib/compress/compress.c | 15 ++++++++++++++- src/lib/compress/compress.h | 2 +- src/lib/compress/compress_sys.h | 14 ++++++++++++++ src/lib/compress/include.am | 1 + src/test/testing_common.c | 1 - 8 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/lib/compress/compress_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index bb2e9f5cdb..6240609ee6 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1367,8 +1367,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) init_protocol_warning_severity_level(); - tor_compress_init(); - int argc = tor_cfg->argc + tor_cfg->argc_owned; char **argv = tor_calloc(argc, sizeof(char*)); memcpy(argv, tor_cfg->argv, tor_cfg->argc*sizeof(char*)); diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index a9189b9941..e47b05da15 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -8,6 +8,7 @@ #include "lib/cc/compat_compiler.h" #include "lib/cc/torint.h" +#include "lib/compress/compress_sys.h" #include "lib/crypt_ops/crypto_sys.h" #include "lib/err/torerr_sys.h" #include "lib/log/log_sys.h" @@ -30,6 +31,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_logging, &sys_time, &sys_network, + &sys_compress, &sys_crypto, }; diff --git a/src/lib/compress/.may_include b/src/lib/compress/.may_include index 68fe9f1c54..4870259ec9 100644 --- a/src/lib/compress/.may_include +++ b/src/lib/compress/.may_include @@ -8,5 +8,6 @@ lib/intmath/*.h lib/log/*.h lib/malloc/*.h lib/string/*.h +lib/subsys/*.h lib/testsupport/*.h lib/thread/*.h diff --git a/src/lib/compress/compress.c b/src/lib/compress/compress.c index 2ad9b15b2e..0d134fd1be 100644 --- a/src/lib/compress/compress.c +++ b/src/lib/compress/compress.c @@ -29,10 +29,12 @@ #include "lib/compress/compress.h" #include "lib/compress/compress_lzma.h" #include "lib/compress/compress_none.h" +#include "lib/compress/compress_sys.h" #include "lib/compress/compress_zlib.h" #include "lib/compress/compress_zstd.h" #include "lib/intmath/cmp.h" #include "lib/malloc/malloc.h" +#include "lib/subsys/subsys.h" #include "lib/thread/threads.h" /** Total number of bytes allocated for compression state overhead. */ @@ -660,7 +662,7 @@ tor_compress_state_size(const tor_compress_state_t *state) } /** Initialize all compression modules. */ -void +int tor_compress_init(void) { atomic_counter_init(&total_compress_allocation); @@ -668,6 +670,8 @@ tor_compress_init(void) tor_zlib_init(); tor_lzma_init(); tor_zstd_init(); + + return 0; } /** Warn if we had any problems while setting up our compression libraries. @@ -677,5 +681,14 @@ tor_compress_init(void) void tor_compress_log_init_warnings(void) { + // XXXX can we move this into tor_compress_init() after all? log.c queues + // XXXX log messages at startup. tor_zstd_warn_if_version_mismatched(); } + +const subsys_fns_t sys_compress = { + .name = "compress", + .supported = true, + .level = -70, + .initialize = tor_compress_init, +}; diff --git a/src/lib/compress/compress.h b/src/lib/compress/compress.h index 4466e27c4d..4dd6506238 100644 --- a/src/lib/compress/compress.h +++ b/src/lib/compress/compress.h @@ -89,7 +89,7 @@ void tor_compress_free_(tor_compress_state_t *state); size_t tor_compress_state_size(const tor_compress_state_t *state); -void tor_compress_init(void); +int tor_compress_init(void); void tor_compress_log_init_warnings(void); struct buf_t; diff --git a/src/lib/compress/compress_sys.h b/src/lib/compress/compress_sys.h new file mode 100644 index 0000000000..a162140cfb --- /dev/null +++ b/src/lib/compress/compress_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file compress_sys.h + * \brief Declare subsystem object for the compress module + **/ + +#ifndef TOR_COMPRESS_SYS_H +#define TOR_COMPRESS_SYS_H + +extern const struct subsys_fns_t sys_compress; + +#endif /* !defined(TOR_COMPRESS_SYS_H) */ diff --git a/src/lib/compress/include.am b/src/lib/compress/include.am index 75c9032bd2..b952779578 100644 --- a/src/lib/compress/include.am +++ b/src/lib/compress/include.am @@ -22,5 +22,6 @@ noinst_HEADERS += \ src/lib/compress/compress.h \ src/lib/compress/compress_lzma.h \ src/lib/compress/compress_none.h \ + src/lib/compress/compress_sys.h \ src/lib/compress/compress_zlib.h \ src/lib/compress/compress_zstd.h diff --git a/src/test/testing_common.c b/src/test/testing_common.c index 333dbc436f..6d2db28f15 100644 --- a/src/test/testing_common.c +++ b/src/test/testing_common.c @@ -255,7 +255,6 @@ main(int c, const char **v) subsystems_init_upto(SUBSYS_LEVEL_LIBS); options = options_new(); - tor_compress_init(); struct tor_libevent_cfg cfg; memset(&cfg, 0, sizeof(cfg)); From 32b23a4c40880591ecadab59f932f4a4c1e7560a Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 Nov 2018 18:46:35 -0400 Subject: [PATCH 15/22] Make tortls use the subsystems interface This one only needs a shutdown right now. --- src/app/main/main.c | 1 - src/app/main/subsystem_list.c | 2 ++ src/lib/tls/.may_include | 1 + src/lib/tls/include.am | 1 + src/lib/tls/tortls.c | 8 ++++++++ src/lib/tls/tortls_sys.h | 14 ++++++++++++++ 6 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/lib/tls/tortls_sys.h diff --git a/src/app/main/main.c b/src/app/main/main.c index 6240609ee6..4dedae9c0c 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -776,7 +776,6 @@ tor_free_all(int postfork) policies_free_all(); } if (!postfork) { - tor_tls_free_all(); #ifndef _WIN32 tor_getpwnam(NULL); #endif diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index e47b05da15..62c87005c6 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -16,6 +16,7 @@ #include "lib/process/winprocess_sys.h" #include "lib/thread/thread_sys.h" #include "lib/time/time_sys.h" +#include "lib/tls/tortls_sys.h" #include "lib/wallclock/wallclock_sys.h" #include @@ -33,6 +34,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_network, &sys_compress, &sys_crypto, + &sys_tortls, }; const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems); diff --git a/src/lib/tls/.may_include b/src/lib/tls/.may_include index 2840e590b8..79301bc318 100644 --- a/src/lib/tls/.may_include +++ b/src/lib/tls/.may_include @@ -11,6 +11,7 @@ lib/log/*.h lib/malloc/*.h lib/net/*.h lib/string/*.h +lib/subsys/*.h lib/testsupport/testsupport.h lib/tls/*.h diff --git a/src/lib/tls/include.am b/src/lib/tls/include.am index a664b29fb2..1817739eef 100644 --- a/src/lib/tls/include.am +++ b/src/lib/tls/include.am @@ -36,5 +36,6 @@ noinst_HEADERS += \ src/lib/tls/tortls.h \ src/lib/tls/tortls_internal.h \ src/lib/tls/tortls_st.h \ + src/lib/tls/tortls_sys.h \ src/lib/tls/x509.h \ src/lib/tls/x509_internal.h diff --git a/src/lib/tls/tortls.c b/src/lib/tls/tortls.c index 56f70bc371..fdeea9e0d4 100644 --- a/src/lib/tls/tortls.c +++ b/src/lib/tls/tortls.c @@ -7,6 +7,7 @@ #define TOR_X509_PRIVATE #include "lib/tls/x509.h" #include "lib/tls/x509_internal.h" +#include "lib/tls/tortls_sys.h" #include "lib/tls/tortls.h" #include "lib/tls/tortls_st.h" #include "lib/tls/tortls_internal.h" @@ -15,6 +16,7 @@ #include "lib/crypt_ops/crypto_rsa.h" #include "lib/crypt_ops/crypto_rand.h" #include "lib/net/socket.h" +#include "lib/subsys/subsys.h" #ifdef _WIN32 #include @@ -440,3 +442,9 @@ tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity) return rv; } + +const subsys_fns_t sys_tortls = { + .name = "tortls", + .level = -50, + .shutdown = tor_tls_free_all +}; diff --git a/src/lib/tls/tortls_sys.h b/src/lib/tls/tortls_sys.h new file mode 100644 index 0000000000..fd909f6019 --- /dev/null +++ b/src/lib/tls/tortls_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file tortls_sys.h + * \brief Declare subsystem object for the tortls module + **/ + +#ifndef TOR_TORTLS_SYS_H +#define TOR_TORTLS_SYS_H + +extern const struct subsys_fns_t sys_tortls; + +#endif /* !defined(TOR_TORTLS_SYS_H) */ From adecda753996611e9a5b82c5fa87ea78ec683806 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 5 Nov 2018 09:42:16 -0500 Subject: [PATCH 16/22] changes file for subsystems api (28330) --- changes/subsystems | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changes/subsystems diff --git a/changes/subsystems b/changes/subsystems new file mode 100644 index 0000000000..a51fb8e2b1 --- /dev/null +++ b/changes/subsystems @@ -0,0 +1,6 @@ + o Major features (refactoring): + - Tor now uses an explicit list of its own subsystems when initializing + and shutting down. Previously, these systems were managed implicitly + though various places throughout the codebase. (There still some + subsystems using the old system.) + Closes ticket 28330. From 4fe4bcf8a10967a668895e962099f50635ba9e4b Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Nov 2018 10:55:18 -0500 Subject: [PATCH 17/22] Explain that configuration should happen elsewhere, but not init. --- src/lib/subsys/subsys.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h index b06d67e624..462314567e 100644 --- a/src/lib/subsys/subsys.h +++ b/src/lib/subsys/subsys.h @@ -44,6 +44,9 @@ typedef struct subsys_fns_t { * This function MUST NOT rely on any runtime configuration information; * it is only for global state or pre-configuration state. * + * (If you need to do any setup that depends on configuration, you'll need + * to declare a configuration callback. (Not yet designed)) + * * This function MUST NOT have any parts that can fail. **/ int (*initialize)(void); From 61695e3d622dfcc196b8b829842f2b12fecebeab Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Nov 2018 10:58:20 -0500 Subject: [PATCH 18/22] Document that subsystem callbacks are optional. --- src/lib/subsys/subsys.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h index 462314567e..2452ec6e2f 100644 --- a/src/lib/subsys/subsys.h +++ b/src/lib/subsys/subsys.h @@ -14,7 +14,11 @@ struct dispatch_connector_t; * A subsystem is a part of Tor that is initialized, shut down, configured, * and connected to other parts of Tor. * - * Subsystems + * All callbacks are optional -- if a callback is set to NULL, the subsystem + * manager will treat it as a no-op. + * + * You should use c99 named-field initializers with this structure: we + * will be adding more fields, often in the middle of the structure. **/ typedef struct subsys_fns_t { /** From e80595f562e199049a41fdf1f3e12baced7e74d5 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Nov 2018 11:00:31 -0500 Subject: [PATCH 19/22] fixup! Make initialization for the "err" library into a subsystem. Check for failure to install backtrace handler. --- src/lib/err/torerr_sys.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/err/torerr_sys.c b/src/lib/err/torerr_sys.c index 54666f4106..2f9e33e233 100644 --- a/src/lib/err/torerr_sys.c +++ b/src/lib/err/torerr_sys.c @@ -18,7 +18,8 @@ static int torerr_subsys_init(void) { - configure_backtrace_handler(get_version()); + if (configure_backtrace_handler(get_version()) < 0) + return -1; tor_log_reset_sigsafe_err_fds(); return 0; From c6336727cac937b4b5ca38c9b49ed3a66ce0b579 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Nov 2018 11:12:12 -0500 Subject: [PATCH 20/22] Rename subsystem callback functions to make them consistent --- src/lib/compress/compress.c | 8 +++++++- src/lib/crypt_ops/crypto_init.c | 32 +++++++++++++++++++++++++------- src/lib/err/torerr_sys.c | 8 ++++---- src/lib/log/log_sys.c | 8 ++++---- src/lib/net/network_sys.c | 8 ++++---- src/lib/process/winprocess_sys.c | 6 +++--- src/lib/thread/compat_threads.c | 4 ++-- src/lib/time/time_sys.c | 4 ++-- src/lib/tls/tortls.c | 8 +++++++- src/lib/wallclock/approx_time.c | 4 ++-- 10 files changed, 60 insertions(+), 30 deletions(-) diff --git a/src/lib/compress/compress.c b/src/lib/compress/compress.c index 0d134fd1be..6cb9bd492b 100644 --- a/src/lib/compress/compress.c +++ b/src/lib/compress/compress.c @@ -686,9 +686,15 @@ tor_compress_log_init_warnings(void) tor_zstd_warn_if_version_mismatched(); } +static int +subsys_compress_initialize(void) +{ + return tor_compress_init(); +} + const subsys_fns_t sys_compress = { .name = "compress", .supported = true, .level = -70, - .initialize = tor_compress_init, + .initialize = subsys_compress_initialize, }; diff --git a/src/lib/crypt_ops/crypto_init.c b/src/lib/crypt_ops/crypto_init.c index a03f5eff7c..4c4cc3e43b 100644 --- a/src/lib/crypt_ops/crypto_init.c +++ b/src/lib/crypt_ops/crypto_init.c @@ -207,7 +207,7 @@ tor_is_using_nss(void) } static int -init_crypto_sys(void) +subsys_crypto_initialize(void) { if (crypto_early_init() < 0) return -1; @@ -216,18 +216,36 @@ init_crypto_sys(void) } static void -shutdown_crypto_sys(void) +subsys_crypto_shutdown(void) { crypto_global_cleanup(); } +static void +subsys_crypto_prefork(void) +{ + crypto_prefork(); +} + +static void +subsys_crypto_postfork(void) +{ + crypto_postfork(); +} + +static void +subsys_crypto_thread_cleanup(void) +{ + crypto_thread_cleanup(); +} + const struct subsys_fns_t sys_crypto = { .name = "crypto", .supported = true, .level = -60, - .initialize = init_crypto_sys, - .shutdown = shutdown_crypto_sys, - .prefork = crypto_prefork, - .postfork = crypto_postfork, - .thread_cleanup = crypto_thread_cleanup, + .initialize = subsys_crypto_initialize, + .shutdown = subsys_crypto_shutdown, + .prefork = subsys_crypto_prefork, + .postfork = subsys_crypto_postfork, + .thread_cleanup = subsys_crypto_thread_cleanup, }; diff --git a/src/lib/err/torerr_sys.c b/src/lib/err/torerr_sys.c index 2f9e33e233..96bb1308a4 100644 --- a/src/lib/err/torerr_sys.c +++ b/src/lib/err/torerr_sys.c @@ -16,7 +16,7 @@ #include static int -torerr_subsys_init(void) +subsys_torerr_initialize(void) { if (configure_backtrace_handler(get_version()) < 0) return -1; @@ -25,7 +25,7 @@ torerr_subsys_init(void) return 0; } static void -torerr_subsys_shutdown(void) +subsys_torerr_shutdown(void) { tor_log_reset_sigsafe_err_fds(); clean_up_backtrace_handler(); @@ -35,6 +35,6 @@ const subsys_fns_t sys_torerr = { .name = "err", .level = -100, .supported = true, - .initialize = torerr_subsys_init, - .shutdown = torerr_subsys_shutdown + .initialize = subsys_torerr_initialize, + .shutdown = subsys_torerr_shutdown }; diff --git a/src/lib/log/log_sys.c b/src/lib/log/log_sys.c index 94ec97fdc1..e20f3156ca 100644 --- a/src/lib/log/log_sys.c +++ b/src/lib/log/log_sys.c @@ -13,14 +13,14 @@ #include "lib/log/log_sys.h" static int -init_logging_subsys(void) +subsys_logging_initialize(void) { init_logging(0); return 0; } static void -shutdown_logging_subsys(void) +subsys_logging_shutdown(void) { logs_free_all(); escaped(NULL); @@ -30,6 +30,6 @@ const subsys_fns_t sys_logging = { .name = "log", .supported = true, .level = -90, - .initialize = init_logging_subsys, - .shutdown = shutdown_logging_subsys, + .initialize = subsys_logging_initialize, + .shutdown = subsys_logging_shutdown, }; diff --git a/src/lib/net/network_sys.c b/src/lib/net/network_sys.c index c9d33a94d3..ac49288ee6 100644 --- a/src/lib/net/network_sys.c +++ b/src/lib/net/network_sys.c @@ -18,7 +18,7 @@ #endif static int -init_network_sys(void) +subsys_network_initialize(void) { if (network_init() < 0) return -1; @@ -27,7 +27,7 @@ init_network_sys(void) } static void -shutdown_network_sys(void) +subsys_network_shutdown(void) { #ifdef _WIN32 WSACleanup(); @@ -39,6 +39,6 @@ const subsys_fns_t sys_network = { .name = "network", .level = -90, .supported = true, - .initialize = init_network_sys, - .shutdown = shutdown_network_sys, + .initialize = subsys_network_initialize, + .shutdown = subsys_network_shutdown, }; diff --git a/src/lib/process/winprocess_sys.c b/src/lib/process/winprocess_sys.c index e00f94c915..ef66f8bfb1 100644 --- a/src/lib/process/winprocess_sys.c +++ b/src/lib/process/winprocess_sys.c @@ -19,7 +19,7 @@ #define WINPROCESS_SYS_ENABLED true static int -init_windows_process_params(void) +subsys_winprocess_initialize(void) { #ifndef HeapEnableTerminationOnCorruption #define HeapEnableTerminationOnCorruption 1 @@ -53,12 +53,12 @@ init_windows_process_params(void) } #else /* !defined(_WIN32) */ #define WINPROCESS_SYS_ENABLED false -#define init_windows_process_params NULL +#define subsys_winprocess_initialize NULL #endif /* defined(_WIN32) */ const subsys_fns_t sys_winprocess = { .name = "winprocess", .level = -100, .supported = WINPROCESS_SYS_ENABLED, - .initialize = init_windows_process_params, + .initialize = subsys_winprocess_initialize, }; diff --git a/src/lib/thread/compat_threads.c b/src/lib/thread/compat_threads.c index 3d41faa8ce..0b466da212 100644 --- a/src/lib/thread/compat_threads.c +++ b/src/lib/thread/compat_threads.c @@ -113,7 +113,7 @@ atomic_counter_exchange(atomic_counter_t *counter, size_t newval) #endif /* !defined(HAVE_WORKING_STDATOMIC) */ static int -sys_threads_initialize(void) +subsys_threads_initialize(void) { tor_threads_init(); return 0; @@ -123,5 +123,5 @@ const subsys_fns_t sys_threads = { .name = "threads", .supported = true, .level = -95, - .initialize = sys_threads_initialize, + .initialize = subsys_threads_initialize, }; diff --git a/src/lib/time/time_sys.c b/src/lib/time/time_sys.c index 2303874f29..b29ca35e69 100644 --- a/src/lib/time/time_sys.c +++ b/src/lib/time/time_sys.c @@ -12,7 +12,7 @@ #include "lib/time/compat_time.h" static int -init_time_sys(void) +subsys_time_initialize(void) { monotime_init(); return 0; @@ -22,5 +22,5 @@ const subsys_fns_t sys_time = { .name = "time", .level = -90, .supported = true, - .initialize = init_time_sys, + .initialize = subsys_time_initialize, }; diff --git a/src/lib/tls/tortls.c b/src/lib/tls/tortls.c index fdeea9e0d4..654cacacf7 100644 --- a/src/lib/tls/tortls.c +++ b/src/lib/tls/tortls.c @@ -443,8 +443,14 @@ tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity) return rv; } +static void +subsys_tortls_shutdown(void) +{ + tor_tls_free_all(); +} + const subsys_fns_t sys_tortls = { .name = "tortls", .level = -50, - .shutdown = tor_tls_free_all + .shutdown = subsys_tortls_shutdown }; diff --git a/src/lib/wallclock/approx_time.c b/src/lib/wallclock/approx_time.c index c7a7ae9bd7..0b0ef382c2 100644 --- a/src/lib/wallclock/approx_time.c +++ b/src/lib/wallclock/approx_time.c @@ -45,7 +45,7 @@ update_approx_time(time_t now) #endif /* !defined(TIME_IS_FAST) */ static int -init_wallclock_subsys(void) +subsys_wallclock_initialize(void) { update_approx_time(time(NULL)); return 0; @@ -55,5 +55,5 @@ const subsys_fns_t sys_wallclock = { .name = "wallclock", .supported = true, .level = -99, - .initialize = init_wallclock_subsys, + .initialize = subsys_wallclock_initialize, }; From ba722e47995e106b46d848263638fa3009687cd9 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Nov 2018 11:15:27 -0500 Subject: [PATCH 21/22] Add list of levels in subsystem_list.c --- src/app/main/subsystem_list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index 62c87005c6..190e6579d8 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -25,7 +25,7 @@ * Global list of the subsystems in Tor, in the order of their initialization. **/ const subsys_fns_t *tor_subsystems[] = { - &sys_winprocess, + &sys_winprocess, /* -100 */ &sys_torerr, &sys_wallclock, &sys_threads, From 60d10812368458cb88aa9f9d628c49766d4bb490 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 9 Nov 2018 11:56:26 -0500 Subject: [PATCH 22/22] Log before performing a subsystem operation --- src/app/main/subsysmgr.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/app/main/subsysmgr.c b/src/app/main/subsysmgr.c index 05803ee946..abd2edd10b 100644 --- a/src/app/main/subsysmgr.c +++ b/src/app/main/subsysmgr.c @@ -7,6 +7,8 @@ #include "app/main/subsysmgr.h" #include "lib/err/torerr.h" +#include "lib/log/log.h" + #include #include #include @@ -85,8 +87,13 @@ subsystems_init_upto(int target_level) if (sys_initialized[i]) continue; int r = 0; - if (sys->initialize) + if (sys->initialize) { + // Note that the logging subsystem is designed so that it does no harm + // to log a message in an uninitialized state. These messages will be + // discarded for now, however. + log_debug(LD_GENERAL, "Initializing %s", sys->name); r = sys->initialize(); + } if (r < 0) { fprintf(stderr, "BUG: subsystem %s (at %u) initialization failed.\n", sys->name, i); @@ -123,8 +130,10 @@ subsystems_shutdown_downto(int target_level) break; if (! sys_initialized[i]) continue; - if (sys->shutdown) + if (sys->shutdown) { + log_debug(LD_GENERAL, "Shutting down %s", sys->name); sys->shutdown(); + } sys_initialized[i] = false; } } @@ -143,8 +152,10 @@ subsystems_prefork(void) continue; if (! sys_initialized[i]) continue; - if (sys->prefork) + if (sys->prefork) { + log_debug(LD_GENERAL, "Pre-fork: %s", sys->name); sys->prefork(); + } } } @@ -162,13 +173,15 @@ subsystems_postfork(void) continue; if (! sys_initialized[i]) continue; - if (sys->postfork) + if (sys->postfork) { + log_debug(LD_GENERAL, "Post-fork: %s", sys->name); sys->postfork(); + } } } /** - * Run thread-clanup code on all subsystems that declare any + * Run thread-cleanup code on all subsystems that declare any **/ void subsystems_thread_cleanup(void) @@ -181,7 +194,9 @@ subsystems_thread_cleanup(void) continue; if (! sys_initialized[i]) continue; - if (sys->thread_cleanup) + if (sys->thread_cleanup) { + log_debug(LD_GENERAL, "Thread cleanup: %s", sys->name); sys->thread_cleanup(); + } } }