From 03ba24480bb1d603a74913e135cf4640334cf0d9 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Thu, 2 Feb 2023 20:23:53 +0800 Subject: [PATCH] smartdns: support multiline config option and fix timer issue. --- src/dns_client.c | 7 +++++-- src/dns_server.c | 8 +++++--- src/fast_ping.c | 6 ++++-- src/include/conf.h | 2 +- src/lib/conf.c | 16 +++++++++++++--- src/smartdns.c | 1 - 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/dns_client.c b/src/dns_client.c index 9317cd1..820a153 100644 --- a/src/dns_client.c +++ b/src/dns_client.c @@ -3862,10 +3862,12 @@ static void *_dns_client_work(void *arg) sleep_time = 0; } } - last = now; if (now >= expect_time) { - _dns_client_period_run(); + if (last != now) { + _dns_client_period_run(); + } + sleep_time = sleep - (now - expect_time); if (sleep_time < 0) { sleep_time = 0; @@ -3873,6 +3875,7 @@ static void *_dns_client_work(void *arg) } expect_time += sleep; } + last = now; pthread_mutex_lock(&client.domain_map_lock); if (list_empty(&client.dns_request_list) && atomic_read(&client.run_period) == 0) { diff --git a/src/dns_server.c b/src/dns_server.c index 98bf20d..4a84951 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -5137,7 +5137,7 @@ static void _dns_server_period_run(unsigned int msec) struct dns_request *tmp = NULL; LIST_HEAD(check_list); - if (msec % 10 == 0) { + if ((msec % 10) == 0) { _dns_server_period_run_second(); } @@ -5227,11 +5227,12 @@ int dns_server_run(void) expect_time -= cnt * sleep; sleep_time -= cnt * sleep; } - last = now; if (now >= expect_time) { msec++; - _dns_server_period_run(msec); + if (last != now) { + _dns_server_period_run(msec); + } sleep_time = sleep - (now - expect_time); if (sleep_time < 0) { sleep_time = 0; @@ -5250,6 +5251,7 @@ int dns_server_run(void) pthread_mutex_unlock(&server.request_list_lock); expect_time += sleep; } + last = now; num = epoll_wait(server.epoll_fd, events, DNS_MAX_EVENTS, sleep_time); if (num < 0) { diff --git a/src/fast_ping.c b/src/fast_ping.c index 9a8bd84..830eea0 100644 --- a/src/fast_ping.c +++ b/src/fast_ping.c @@ -1780,10 +1780,11 @@ static void *_fast_ping_work(void *arg) sleep_time = 0; } } - last = now; if (now >= expect_time) { - _fast_ping_period_run(); + if (last != now) { + _fast_ping_period_run(); + } sleep_time = sleep - (now - expect_time); if (sleep_time < 0) { sleep_time = 0; @@ -1791,6 +1792,7 @@ static void *_fast_ping_work(void *arg) } expect_time += sleep; } + last = now; pthread_mutex_lock(&ping.map_lock); if (hash_empty(ping.addrmap)) { diff --git a/src/include/conf.h b/src/include/conf.h index 3e96446..91842b1 100644 --- a/src/include/conf.h +++ b/src/include/conf.h @@ -21,7 +21,7 @@ #include -#define MAX_LINE_LEN 1024 +#define MAX_LINE_LEN 8192 #define MAX_KEY_LEN 64 #define CONF_INT_MAX (~(1 << 31)) #define CONF_INT_MIN (1 << 31) diff --git a/src/lib/conf.c b/src/lib/conf.c index 70f6eca..1127aa9 100644 --- a/src/lib/conf.c +++ b/src/lib/conf.c @@ -299,7 +299,7 @@ static int load_conf_printf(const char *file, int lineno, int ret) static int load_conf_file(const char *file, struct config_item *items, conf_error_handler handler) { FILE *fp = NULL; - char line[MAX_LINE_LEN]; + char line[MAX_LINE_LEN + MAX_KEY_LEN]; char key[MAX_KEY_LEN]; char value[MAX_LINE_LEN]; int filed_num = 0; @@ -309,6 +309,8 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro int ret = 0; int call_ret = 0; int line_no = 0; + int line_len = 0; + int read_len = 0; if (handler == NULL) { handler = load_conf_printf; @@ -320,9 +322,17 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro } line_no = 0; - while (fgets(line, MAX_LINE_LEN, fp)) { + while (fgets(line + line_len, MAX_LINE_LEN - line_len, fp)) { line_no++; - filed_num = sscanf(line, "%63s %1023[^\r\n]s", key, value); + read_len = strnlen(line + line_len, sizeof(line)); + if (read_len >= 2 && *(line + line_len + read_len - 2) == '\\') { + line_len += read_len - 2; + line[line_len] = '\0'; + continue; + } + line_len = 0; + + filed_num = sscanf(line, "%63s %8192[^\r\n]s", key, value); if (filed_num <= 0) { continue; } diff --git a/src/smartdns.c b/src/smartdns.c index e96c0e3..65774f3 100644 --- a/src/smartdns.c +++ b/src/smartdns.c @@ -45,7 +45,6 @@ #include #include -#define MAX_LINE_LEN 1024 #define MAX_KEY_LEN 64 #define SMARTDNS_PID_FILE "/var/run/smartdns.pid" #define TMP_BUFF_LEN_32 32