Просмотр исходного кода

smartdns: enhance reliability during initialization.

Nick Peng 2 лет назад
Родитель
Сommit
568f95dbdf
9 измененных файлов с 85 добавлено и 12 удалено
  1. 13 1
      src/dns_cache.c
  2. 12 1
      src/dns_client.c
  3. 16 6
      src/dns_server.c
  4. 12 0
      src/fast_ping.c
  5. 14 2
      src/proxy.c
  6. 1 1
      src/proxy.h
  7. 2 1
      src/smartdns.c
  8. 5 0
      src/tlog.c
  9. 10 0
      src/util.c

+ 13 - 1
src/dns_cache.c

@@ -44,11 +44,16 @@ struct dns_cache_head {
 
 typedef int (*dns_cache_read_callback)(struct dns_cache_record *cache_record, struct dns_cache_data *cache_data);
 
+static int is_cache_init;
 static struct dns_cache_head dns_cache_head;
 
 int dns_cache_init(int size, dns_cache_callback timeout_callback)
 {
 	int bits = 0;
+	if (is_cache_init == 1) {
+		return -1;
+	}
+
 	INIT_LIST_HEAD(&dns_cache_head.cache_list);
 
 	bits = ilog2(size) - 1;
@@ -64,6 +69,7 @@ int dns_cache_init(int size, dns_cache_callback timeout_callback)
 	dns_cache_head.timeout_callback = timeout_callback;
 	pthread_mutex_init(&dns_cache_head.lock, NULL);
 
+	is_cache_init = 1;
 	return 0;
 }
 
@@ -779,15 +785,21 @@ void dns_cache_destroy(void)
 	struct dns_cache *dns_cache = NULL;
 	struct dns_cache *tmp = NULL;
 
+	if (is_cache_init == 0) {
+		return;
+	}
+
 	pthread_mutex_lock(&dns_cache_head.lock);
 	list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list)
 	{
-		_dns_cache_delete(dns_cache);
+		_dns_cache_remove(dns_cache);
 	}
 	pthread_mutex_unlock(&dns_cache_head.lock);
 
 	pthread_mutex_destroy(&dns_cache_head.lock);
 	hash_table_free(dns_cache_head.cache_hash, free);
+
+	is_cache_init = 0;
 }
 
 const char *dns_cache_file_version(void)

+ 12 - 1
src/dns_client.c

@@ -267,7 +267,7 @@ struct dns_query_struct {
 	/* replied hash table */
 	DECLARE_HASHTABLE(replied_map, 4);
 };
-
+static int is_client_init;
 static struct dns_client client;
 static LIST_HEAD(pending_servers);
 static pthread_mutex_t pending_server_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -4362,6 +4362,10 @@ int dns_client_init(void)
 	int fd_wakeup = -1;
 	int ret = 0;
 
+	if (is_client_init == 1) {
+		return -1;
+	}
+
 	if (client.epoll_fd > 0) {
 		return -1;
 	}
@@ -4411,6 +4415,7 @@ int dns_client_init(void)
 	}
 
 	client.fd_wakeup = fd_wakeup;
+	is_client_init = 1;
 
 	return 0;
 errout:
@@ -4437,6 +4442,10 @@ errout:
 
 void dns_client_exit(void)
 {
+	if (is_client_init == 0) {
+		return;
+	}
+
 	if (client.tid) {
 		void *ret = NULL;
 		atomic_set(&client.run, 0);
@@ -4458,4 +4467,6 @@ void dns_client_exit(void)
 		SSL_CTX_free(client.ssl_ctx);
 		client.ssl_ctx = NULL;
 	}
+
+	is_client_init = 0;
 }

+ 16 - 6
src/dns_server.c

@@ -348,6 +348,7 @@ struct dns_server {
 	pthread_mutex_t request_pending_lock;
 };
 
+static int is_server_init;
 static struct dns_server server;
 
 static tlog_log *dns_audit;
@@ -7445,13 +7446,12 @@ int dns_server_init(void)
 
 	_dns_server_check_need_exit();
 
-	if (server.epoll_fd > 0) {
+	if (is_server_init == 1) {
 		return -1;
 	}
 
-	if (_dns_server_cache_init() != 0) {
-		tlog(TLOG_ERROR, "init dns cache filed.");
-		goto errout;
+	if (server.epoll_fd > 0) {
+		return -1;
 	}
 
 	if (_dns_server_audit_init() != 0) {
@@ -7495,6 +7495,12 @@ int dns_server_init(void)
 		goto errout;
 	}
 
+	if (_dns_server_cache_init() != 0) {
+		tlog(TLOG_ERROR, "init dns cache filed.");
+		goto errout;
+	}
+
+	is_server_init = 1;
 	return 0;
 errout:
 	atomic_set(&server.run, 0);
@@ -7506,8 +7512,6 @@ errout:
 	_dns_server_close_socket();
 	pthread_mutex_destroy(&server.request_list_lock);
 
-	dns_cache_destroy();
-
 	return -1;
 }
 
@@ -7519,6 +7523,10 @@ void dns_server_stop(void)
 
 void dns_server_exit(void)
 {
+	if (is_server_init == 0) {
+		return;
+	}
+
 	if (server.event_fd > 0) {
 		close(server.event_fd);
 		server.event_fd = -1;
@@ -7534,4 +7542,6 @@ void dns_server_exit(void)
 	_dns_server_request_remove_all();
 	pthread_mutex_destroy(&server.request_list_lock);
 	dns_cache_destroy();
+
+	is_server_init = 0;
 }

+ 12 - 0
src/fast_ping.c

@@ -183,6 +183,7 @@ struct fast_ping_struct {
 	int fake_ip_num;
 };
 
+static int is_fast_ping_init;
 static struct fast_ping_struct ping;
 static atomic_t ping_sid = ATOMIC_INIT(0);
 static int bool_print_log = 1;
@@ -2187,6 +2188,10 @@ int fast_ping_init(void)
 	int ret = 0;
 	bool_print_log = 1;
 
+	if (is_fast_ping_init == 1) {
+		return -1;
+	}
+
 	if (ping.epoll_fd > 0) {
 		return -1;
 	}
@@ -2232,6 +2237,7 @@ int fast_ping_init(void)
 		goto errout;
 	}
 
+	is_fast_ping_init = 1;
 	return 0;
 errout:
 	if (ping.notify_tid) {
@@ -2294,6 +2300,10 @@ static void _fast_ping_close_fds(void)
 
 void fast_ping_exit(void)
 {
+	if (is_fast_ping_init == 0) {
+		return;
+	}
+
 	if (ping.notify_tid) {
 		void *retval = NULL;
 		atomic_set(&ping.run, 0);
@@ -2324,4 +2334,6 @@ void fast_ping_exit(void)
 	pthread_mutex_destroy(&ping.notify_lock);
 	pthread_mutex_destroy(&ping.lock);
 	pthread_mutex_destroy(&ping.map_lock);
+
+	is_fast_ping_init = 0;
 }

+ 14 - 2
src/proxy.c

@@ -95,6 +95,7 @@ struct proxy_struct {
 };
 
 static struct proxy_struct proxy;
+static int is_proxy_init;
 
 static const char *proxy_socks5_status_code[] = {
 	"success",
@@ -1045,13 +1046,24 @@ int proxy_conn_is_udp(struct proxy_conn *proxy_conn)
 
 int proxy_init(void)
 {
+	if (is_proxy_init == 1) {
+		return -1;
+	}
+
 	memset(&proxy, 0, sizeof(proxy));
 	hash_init(proxy.proxy_server);
+	is_proxy_init = 1;
 	return 0;
 }
 
-int proxy_exit(void)
+void proxy_exit(void)
 {
+	if (is_proxy_init == 0) {
+		return;
+	}
 	_proxy_remove_all();
-	return 0;
+
+	is_proxy_init = 0;
+	
+	return ;
 }

+ 1 - 1
src/proxy.h

@@ -56,7 +56,7 @@ struct proxy_conn;
 
 int proxy_init(void);
 
-int proxy_exit(void);
+void proxy_exit(void);
 
 int proxy_add(const char *proxy_name, struct proxy_info *info);
 

+ 2 - 1
src/smartdns.c

@@ -573,7 +573,6 @@ static int _smartdns_run(void)
 
 static void _smartdns_exit(void)
 {
-	tlog(TLOG_INFO, "smartdns exit...");
 	dns_client_exit();
 	proxy_exit();
 	fast_ping_exit();
@@ -889,6 +888,7 @@ int main(int argc, char *argv[])
 
 	smartdns_test_notify(1);
 	ret = _smartdns_run();
+	tlog(TLOG_INFO, "smartdns exit...");
 	_smartdns_exit();
 	return ret;
 errout:
@@ -896,5 +896,6 @@ errout:
 		daemon_kickoff(ret, dns_conf_log_console | verbose_screen);
 	}
 	smartdns_test_notify(2);
+	_smartdns_exit();
 	return 1;
 }

+ 5 - 0
src/tlog.c

@@ -1970,6 +1970,7 @@ errout:
     pthread_mutex_destroy(&tlog.lock);
     tlog.run = 0;
     tlog.root = NULL;
+    tlog_format = NULL;
 
     _tlog_close(log, 1);
 
@@ -1978,6 +1979,10 @@ errout:
 
 void tlog_exit(void)
 {
+    if (tlog_format == NULL) {
+        return;
+    }
+
     if (tlog.tid) {
         void *ret = NULL;
         tlog.run = 0;

+ 10 - 0
src/util.c

@@ -1065,6 +1065,10 @@ void SSL_CRYPTO_thread_setup(void)
 {
 	int i = 0;
 
+	if (lock_cs != NULL) {
+		return;
+	}
+
 	lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
 	lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
 	if (!lock_cs || !lock_count) {
@@ -1094,12 +1098,18 @@ void SSL_CRYPTO_thread_cleanup(void)
 {
 	int i = 0;
 
+	if (lock_cs == NULL) {
+		return;
+	}
+
 	CRYPTO_set_locking_callback(NULL);
 	for (i = 0; i < CRYPTO_num_locks(); i++) {
 		pthread_mutex_destroy(&(lock_cs[i]));
 	}
 	OPENSSL_free(lock_cs);
 	OPENSSL_free(lock_count);
+	lock_cs = NULL;
+	lock_count = NULL;
 }
 #endif