From c62ea93ba61f048360b7f6aeba18dc43687cf638 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Fri, 1 Mar 2019 00:35:45 +0800 Subject: [PATCH] Support relative conf-path --- src/dns_conf.c | 14 +++++++++++++- src/include/conf.h | 2 ++ src/lib/conf.c | 8 ++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/dns_conf.c b/src/dns_conf.c index b5d671d..ceaea6e 100644 --- a/src/dns_conf.c +++ b/src/dns_conf.c @@ -9,6 +9,7 @@ #include #include #include + #include #define DEFAULT_DNS_CACHE_SIZE 512 @@ -999,10 +1000,21 @@ static int _conf_printf(const char *file, int lineno, int ret) int config_addtional_file(void *data, int argc, char *argv[]) { - char *file_path = argv[1]; + char *conf_file = argv[1]; + char file_path[DNS_MAX_PATH]; + char file_path_dir[DNS_MAX_PATH]; + + if (conf_file[0] != '/') { + strncpy(file_path_dir, conf_get_conf_file(), DNS_MAX_PATH); + dirname(file_path_dir); + snprintf(file_path, DNS_MAX_PATH, "%s/%s", file_path_dir, conf_file); + } else { + strncpy(file_path, conf_file, DNS_MAX_PATH); + } if (access(file_path, R_OK) != 0) { tlog(TLOG_WARN, "conf file %s is not readable.", file_path); + syslog(LOG_NOTICE, "conf file %s is not readable.", file_path); return 0; } diff --git a/src/include/conf.h b/src/include/conf.h index 0236134..48e4edc 100644 --- a/src/include/conf.h +++ b/src/include/conf.h @@ -118,4 +118,6 @@ int load_conf(const char *file, struct config_item items[], conf_error_handler h void load_exit(void); +const char *conf_get_conf_file(void); + #endif // !_GENERIC_CONF_H diff --git a/src/lib/conf.c b/src/lib/conf.c index b65d8a6..ed88e00 100644 --- a/src/lib/conf.c +++ b/src/lib/conf.c @@ -5,6 +5,13 @@ #include #include +static const char *currrent_conf_file = NULL; + +const char *conf_get_conf_file(void) +{ + return currrent_conf_file; +} + int conf_custom(const char *item, void *data, int argc, char *argv[]) { struct config_item_custom *item_custom = data; @@ -255,6 +262,7 @@ int load_conf_file(const char *file, struct config_item *items, conf_error_handl conf_getopt_reset(); /* call item function */ + currrent_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) {