From f659cf3725e9f6534c6ad0d63d1c96c8cb704687 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Sun, 28 Aug 2022 10:22:14 +0800 Subject: [PATCH] dns_conf: support relative path for dnsmasq-lease-file --- src/dns_conf.c | 4 ++-- src/include/conf.h | 2 ++ src/lib/conf.c | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/dns_conf.c b/src/dns_conf.c index 7dba2e2..df5a707 100644 --- a/src/dns_conf.c +++ b/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; } diff --git a/src/include/conf.h b/src/include/conf.h index f1863de..9bc8e54 100644 --- a/src/include/conf.h +++ b/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 diff --git a/src/lib/conf.c b/src/lib/conf.c index 63713b2..325cfb4 100644 --- a/src/lib/conf.c +++ b/src/lib/conf.c @@ -18,16 +18,46 @@ #include "conf.h" #include +#include +#include #include #include #include #include -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) {