diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-11-02 18:00:56 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-11-05 09:22:02 -0500 |
commit | cad61f0f6de48c6eab6e811a081f154b03de57b8 (patch) | |
tree | 9a3e3cb610f7a0315e5f3c56795176eba9ed1065 /src | |
parent | 50436ccea4bd200e45196ccce7acff28f293a4de (diff) | |
download | tor-cad61f0f6de48c6eab6e811a081f154b03de57b8.tar.gz tor-cad61f0f6de48c6eab6e811a081f154b03de57b8.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/app/config/config.c | 5 | ||||
-rw-r--r-- | src/app/main/subsysmgr.c | 57 | ||||
-rw-r--r-- | src/app/main/subsysmgr.h | 4 | ||||
-rw-r--r-- | src/lib/crypt_ops/crypto_init.c | 3 | ||||
-rw-r--r-- | src/lib/subsys/subsys.h | 16 | ||||
-rw-r--r-- | 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 @@ -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 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(); } |