Call crypto_postfork on start_daemon() instead.

This commit is contained in:
Nick Mathewson 2018-08-08 17:32:26 -04:00
parent 622a2c6bee
commit a57c27a1c7
3 changed files with 10 additions and 9 deletions

View File

@ -1411,7 +1411,8 @@ options_act_reversible(const or_options_t *old_options, char **msg)
* processes. */ * processes. */
if (running_tor && options->RunAsDaemon) { if (running_tor && options->RunAsDaemon) {
/* No need to roll back, since you can't change the value. */ /* No need to roll back, since you can't change the value. */
start_daemon(); if (start_daemon())
crypto_postfork();
} }
#ifdef HAVE_SYSTEMD #ifdef HAVE_SYSTEMD
@ -2027,9 +2028,7 @@ options_act(const or_options_t *old_options)
/* Finish backgrounding the process */ /* Finish backgrounding the process */
if (options->RunAsDaemon) { if (options->RunAsDaemon) {
/* We may be calling this for the n'th time (on SIGHUP), but it's safe. */ /* We may be calling this for the n'th time (on SIGHUP), but it's safe. */
int forked = finish_daemon(options->DataDirectory); finish_daemon(options->DataDirectory);
if (forked)
crypto_postfork();
} }
/* See whether we need to enable/disable our once-a-second timer. */ /* See whether we need to enable/disable our once-a-second timer. */

View File

@ -41,15 +41,16 @@ static int daemon_filedes[2];
/** Start putting the process into daemon mode: fork and drop all resources /** Start putting the process into daemon mode: fork and drop all resources
* except standard fds. The parent process never returns, but stays around * except standard fds. The parent process never returns, but stays around
* until finish_daemon is called. (Note: it's safe to call this more * until finish_daemon is called. (Note: it's safe to call this more
* than once: calls after the first are ignored.) * than once: calls after the first are ignored.) Return true if we actually
* forked and this is the child; false otherwise.
*/ */
void int
start_daemon(void) start_daemon(void)
{ {
pid_t pid; pid_t pid;
if (start_daemon_called) if (start_daemon_called)
return; return 0;
start_daemon_called = 1; start_daemon_called = 1;
if (pipe(daemon_filedes)) { if (pipe(daemon_filedes)) {
@ -80,6 +81,7 @@ start_daemon(void)
exit(0); // exit ok: during daemonize, daemonizing. exit(0); // exit ok: during daemonize, daemonizing.
else else
exit(1); /* child reported error. exit ok: daemonize failed. */ exit(1); /* child reported error. exit ok: daemonize failed. */
return 0; // LCOV_EXCL_LINE unreachable
} else { /* Child */ } else { /* Child */
close(daemon_filedes[0]); /* we only write */ close(daemon_filedes[0]); /* we only write */
@ -95,7 +97,7 @@ start_daemon(void)
} }
set_main_thread(); /* We are now the main thread. */ set_main_thread(); /* We are now the main thread. */
return; return 1;
} }
} }

View File

@ -11,7 +11,7 @@
#ifndef TOR_DAEMON_H #ifndef TOR_DAEMON_H
#define TOR_DAEMON_H #define TOR_DAEMON_H
void start_daemon(void); int start_daemon(void);
int finish_daemon(const char *desired_cwd); int finish_daemon(const char *desired_cwd);
#endif #endif