From 652dde72f1beed1fb83d137314f47aff15b01036 Mon Sep 17 00:00:00 2001 From: Roger Dingledine Date: Wed, 6 Apr 2005 19:07:38 +0000 Subject: bugfix: chdir to your datadirectory at the *end* of the daemonize process, not the beginning. this was a problem because the first time you run tor, if your datadir isn't there, and you have runasdaemon set to 1, it will try to chdir to it before it tries to create it. oops. svn:r4033 --- src/common/util.c | 26 +++++++++++++------------- src/common/util.h | 4 ++-- src/or/config.c | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/common/util.c b/src/common/util.c index 09409e701..943f912b4 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1334,7 +1334,7 @@ static int daemon_filedes[2]; * until finish_daemon is called. (Note: it's safe to call this more * than once: calls after the first are ignored.) */ -void start_daemon(const char *desired_cwd) +void start_daemon(void) { pid_t pid; @@ -1342,14 +1342,6 @@ void start_daemon(const char *desired_cwd) return; start_daemon_called = 1; - if (!desired_cwd) - desired_cwd = "/"; - /* Don't hold the wrong FS mounted */ - if (chdir(desired_cwd) < 0) { - log_fn(LOG_ERR,"chdir to %s failed. Exiting.",desired_cwd); - exit(1); - } - pipe(daemon_filedes); pid = fork(); if (pid < 0) { @@ -1393,16 +1385,24 @@ void start_daemon(const char *desired_cwd) * calls after the first are ignored. Calls start_daemon first if it hasn't * been called already.) */ -void finish_daemon(void) +void finish_daemon(const char *desired_cwd) { int nullfd; char c = '.'; if (finish_daemon_called) return; if (!start_daemon_called) - start_daemon(NULL); + start_daemon(); finish_daemon_called = 1; + if (!desired_cwd) + desired_cwd = "/"; + /* Don't hold the wrong FS mounted */ + if (chdir(desired_cwd) < 0) { + log_fn(LOG_ERR,"chdir to %s failed. Exiting.",desired_cwd); + exit(1); + } + nullfd = open("/dev/null", O_CREAT | O_RDWR | O_APPEND); if (nullfd < 0) { @@ -1424,8 +1424,8 @@ void finish_daemon(void) } #else /* defined(MS_WINDOWS) */ -void start_daemon(const char *cp) {} -void finish_daemon(void) {} +void start_daemon(void) {} +void finish_daemon(const char *cp) {} #endif /** Write the current process ID, followed by NL, into filename. diff --git a/src/common/util.h b/src/common/util.h index 6f23e219a..b3262d87a 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -132,8 +132,8 @@ int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len); int is_plausible_address(const char *name); /* Process helpers */ -void start_daemon(const char *desired_cwd); -void finish_daemon(void); +void start_daemon(void); +void finish_daemon(const char *desired_cwd); void write_pidfile(char *filename); #endif diff --git a/src/or/config.c b/src/or/config.c index 97039fa96..1d03146cf 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -266,7 +266,7 @@ options_act(void) { * It let us have the parent process stick around until we were sure Tor * was started. Should we make start_daemon get called earlier? -NM */ if (options->RunAsDaemon) { - start_daemon(options->DataDirectory); + start_daemon(); } if (!libevent_initialized) { configure_libevent_logging(); @@ -346,7 +346,7 @@ options_act(void) { /* Finish backgrounding the process */ if (options->RunAsDaemon) { /* We may be calling this for the n'th time (on SIGHUP), but it's safe. */ - finish_daemon(); + finish_daemon(options->DataDirectory); } /* Write our pid to the pid file. If we do not have write permissions we -- cgit v1.2.3