Browse Source

dns_conf: support relative path for dnsmasq-lease-file

Nick Peng 3 years ago
parent
commit
f659cf3725
3 changed files with 37 additions and 5 deletions
  1. 2 2
      src/dns_conf.c
  2. 2 0
      src/include/conf.h
  3. 33 3
      src/lib/conf.c

+ 2 - 2
src/dns_conf.c

@@ -1750,8 +1750,8 @@ static int _conf_dhcp_lease_dnsmasq_file(void *data, int argc, char *argv[])
 		return -1;
 	}
 
-	safe_strncpy(dns_conf_dnsmasq_lease_file, argv[1], DNS_MAX_PATH);
-	if (_conf_dhcp_lease_dnsmasq_add(argv[1]) != 0) {
+	conf_get_conf_fullpath(argv[1], dns_conf_dnsmasq_lease_file, sizeof(dns_conf_dnsmasq_lease_file));
+	if (_conf_dhcp_lease_dnsmasq_add(dns_conf_dnsmasq_lease_file) != 0) {
 		return -1;
 	}
 

+ 2 - 0
src/include/conf.h

@@ -160,4 +160,6 @@ void load_exit(void);
 
 const char *conf_get_conf_file(void);
 
+const char *conf_get_conf_fullpath(const char *path, char *fullpath, size_t path_len);
+
 #endif // !_GENERIC_CONF_H

+ 33 - 3
src/lib/conf.c

@@ -18,16 +18,46 @@
 
 #include "conf.h"
 #include <getopt.h>
+#include <libgen.h>
+#include <linux/limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-static const char *currrent_conf_file = NULL;
+static const char *current_conf_file = NULL;
 
 const char *conf_get_conf_file(void)
 {
-	return currrent_conf_file;
+	return current_conf_file;
+}
+
+const char *conf_get_conf_fullpath(const char *path, char *fullpath, size_t path_len)
+{
+	char file_path_dir[PATH_MAX];
+
+	if (path_len < 1) {
+		return NULL;
+	}
+
+	if (path[0] == '/') {
+		strncpy(fullpath, path, path_len);
+		return fullpath;
+	}
+
+	strncpy(file_path_dir, conf_get_conf_file(), PATH_MAX - 1);
+	file_path_dir[PATH_MAX - 1] = 0;
+	dirname(file_path_dir);
+	if (file_path_dir[0] == '\0') {
+		strncpy(fullpath, path, path_len);
+		return fullpath;
+	}
+
+	if (snprintf(fullpath, PATH_MAX, "%s/%s", file_path_dir, path) < 0) {
+		return NULL;
+	}
+
+	return fullpath;
 }
 
 int conf_custom(const char *item, void *data, int argc, char *argv[])
@@ -303,7 +333,7 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro
 
 			conf_getopt_reset();
 			/* call item function */
-			currrent_conf_file = file;
+			current_conf_file = file;
 			call_ret = items[i].item_func(items[i].item, items[i].data, argc, argv);
 			ret = handler(file, line_no, call_ret);
 			if (ret != 0) {