From 7c6b83b224d74491f0d47760dcdd47f39df3751a Mon Sep 17 00:00:00 2001
From: Andre Noll <maan@congo.fml.local>
Date: Fri, 28 Mar 2008 15:01:09 +0100
Subject: [PATCH] Fix a serious bug in deamon mode.

In daemon mode, we changed to dest_dir _before_ calling daemon_init(),
which changes the cwd to /.

Fix this rather embarrassing bug by changing cwd later. To this aim,
introduce change_to_dest_dir() which also logs some messages.
---
 dss.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/dss.c b/dss.c
index 02d0c8b..413f88d 100644
--- a/dss.c
+++ b/dss.c
@@ -629,7 +629,6 @@ static void parse_config_file(int override)
 	}
 	DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
 //	cmdline_parser_dump(logfile? logfile : stdout, &conf);
-	ret = dss_chdir(conf.dest_dir_arg);
 out:
 	free(config_file);
 	if (ret >= 0)
@@ -638,10 +637,23 @@ out:
 	exit(EXIT_FAILURE);
 }
 
+static void change_to_dest_dir(void)
+{
+	int ret;
+
+	DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
+	ret = dss_chdir(conf.dest_dir_arg);
+	if (ret >= 0)
+		return;
+	DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
+	exit(EXIT_FAILURE);
+}
+
 static void handle_sighup(void)
 {
 	DSS_NOTICE_LOG("SIGHUP\n");
 	parse_config_file(1);
+	change_to_dest_dir();
 }
 
 static void handle_signal(void)
@@ -965,6 +977,7 @@ int main(int argc, char **argv)
 
 	if (conf.daemon_given)
 		daemon_init();
+	change_to_dest_dir();
 	setup_signal_handling();
 	ret = call_command_handler();
 	if (ret < 0)
-- 
2.39.5