From fe064cae22030045af77772f36dff8bdb1753775 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eckl=2C=20M=C3=A1t=C3=A9?= <ecklm94@gmail.com>
Date: Fri, 27 May 2016 02:58:29 +0200
Subject: [PATCH] =?UTF-8?q?Kult=C3=BAr=C3=A1lt=20working=20dir=20kezel?=
 =?UTF-8?q?=C3=A9s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 main.cpp | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/main.cpp b/main.cpp
index 813eb2c..da36dbd 100644
--- a/main.cpp
+++ b/main.cpp
@@ -12,6 +12,7 @@
 #include <sys/types.h>
 #include <pwd.h>
 #include <signal.h>
+#include <linux/limits.h>
 
 #include "include/general_sockaddr_wp.h"
 #include "include/dns_server.h"
@@ -20,7 +21,6 @@
 #include "include/IPC_US_communicator.h"
 #include "include/command.h"
 
-#define WORKING_DIR "/home/mate/Dokumentumok/Egyetem/Tárgyak/2016 tavasz/Linux programozás/HF/DNS_server"
 #define COMM_SOCK_PATH "/var/run/ecklm-dns/"
 
 logger log_on_demand;
@@ -32,9 +32,11 @@ int main(int argc, char *argv[])
 {
 	int cli_option;
 	int thread_num = 1;
+	char working_dir[PATH_MAX] = { 0 };
+	getcwd(working_dir, PATH_MAX-1 );
 	bool run_as_daemon = false;
 	std::vector<command> command_list;
-	while((cli_option = getopt (argc, argv, "dmskvht:")) != -1)
+	while((cli_option = getopt (argc, argv, "dskvht:w:")) != -1)
 	{
 		int ti = 0;
 		switch(cli_option)
@@ -56,6 +58,9 @@ int main(int argc, char *argv[])
 				if(ti>0)
 					thread_num = ti;
 				break;
+			case 'w':
+				strncpy(working_dir, optarg, PATH_MAX-1);
+				break;
 			case 'h':
 				print_help();
 				exit(EXIT_SUCCESS);
@@ -89,14 +94,17 @@ int main(int argc, char *argv[])
 	}
 
 	log_on_demand(LOG_INFO, "Server started on pid %d", getpid()); // Enélkül nem hajlandó sudo után logolni.
+	if(verbose)
+			log_on_demand(LOG_DEBUG, "Current working directory is %s.", working_dir);
+
 	struct passwd *new_uid = getpwnam("dnsuser");
 
-	if(chroot(WORKING_DIR) < 0)
+	if(chroot(working_dir) < 0)
 	{
 		PERROR_ON_DEMAND("chroot");
 		exit(EXIT_SUCCESS);
 	}
-	log_on_demand(LOG_INFO, "Chrooted to \"%s\"", WORKING_DIR);
+	log_on_demand(LOG_INFO, "Chrooted to \"%s\"", working_dir);
 	if(chdir("/") < 0)
 	{
 		PERROR_ON_DEMAND("chdir");
@@ -179,9 +187,11 @@ For more info look at the man page.
 
 Options:
 	-d Run as daemon
-	-t Number of threads to start. Only makes sense with -d.
+	-t threads Number of threads to start. Only makes sense with -d.
 	-s Statistics from daemon
 	-k Kill daemon
+	-w workind_dir Set working directory. Absolute path needed.
+		If not the actual PWD will be used.
 	-v Verbose
 	-h help
 )");
-- 
GitLab