commit cad61f0f6de48c6eab6e811a081f154b03de57b8 Author: Nick Mathewson nickm@torproject.org Date: Fri Nov 2 18:00:56 2018 -0400
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 7b49387bc..76df7ec67 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 7974f2d23..05803ee94 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 c9b892eee..4b3cad62a 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 cc7865ef7..a03f5eff7 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 25451bc45..b06d67e62 100644 --- a/src/lib/subsys/subsys.h +++ b/src/lib/subsys/subsys.h @@ -54,6 +54,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. * * This function is not allowed to fail. diff --git a/src/test/testing_common.c b/src/test/testing_common.c index d4c563233..1362f2971 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(); }
tor-commits@lists.torproject.org