mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-11 05:33:47 +01:00
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:
parent
50436ccea4
commit
cad61f0f6d
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user