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.
This commit is contained in:
Nick Mathewson 2018-11-02 18:00:56 -04:00
parent 50436ccea4
commit cad61f0f6d
6 changed files with 85 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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