* processes. */
if (running_tor && options->RunAsDaemon) {
/* No need to roll back, since you can't change the value. */
- start_daemon();
+ if (start_daemon())
+ crypto_postfork();
}
#ifdef HAVE_SYSTEMD
/* Finish backgrounding the process */
if (options->RunAsDaemon) {
/* We may be calling this for the n'th time (on SIGHUP), but it's safe. */
- int forked = finish_daemon(options->DataDirectory);
- if (forked)
- crypto_postfork();
+ finish_daemon(options->DataDirectory);
}
/* See whether we need to enable/disable our once-a-second timer. */
/** Start putting the process into daemon mode: fork and drop all resources
* except standard fds. The parent process never returns, but stays around
* 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)
{
pid_t pid;
if (start_daemon_called)
- return;
+ return 0;
start_daemon_called = 1;
if (pipe(daemon_filedes)) {
exit(0); // exit ok: during daemonize, daemonizing.
else
exit(1); /* child reported error. exit ok: daemonize failed. */
+ return 0; // LCOV_EXCL_LINE unreachable
} else { /* Child */
close(daemon_filedes[0]); /* we only write */
}
set_main_thread(); /* We are now the main thread. */
- return;
+ return 1;
}
}