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
This commit is contained in:
Roger Dingledine 2005-04-06 19:07:38 +00:00
parent 8596947ee2
commit 652dde72f1
3 changed files with 17 additions and 17 deletions

View file

@ -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 <b>filename</b>.

View file

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

View file

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