Commit 6d495b0c4253b6622019ae03a98fb15a8b5555d2

kanoi 2014-07-08T19:22:15

mutex lock getaddrinfo

diff --git a/cgminer.c b/cgminer.c
index b0330b3..c783327 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -306,6 +306,7 @@ static pthread_mutex_t sshare_lock;
 pthread_rwlock_t netacc_lock;
 pthread_rwlock_t mining_thr_lock;
 pthread_rwlock_t devices_lock;
+pthread_mutex_t getaddr_lock;
 
 static pthread_mutex_t lp_lock;
 static pthread_cond_t lp_cond;
@@ -9261,6 +9262,7 @@ int main(int argc, char *argv[])
 	rwlock_init(&netacc_lock);
 	rwlock_init(&mining_thr_lock);
 	rwlock_init(&devices_lock);
+	mutex_init(&getaddr_lock);
 
 	mutex_init(&lp_lock);
 	if (unlikely(pthread_cond_init(&lp_cond, NULL)))
diff --git a/miner.h b/miner.h
index f8899c8..58aba3c 100644
--- a/miner.h
+++ b/miner.h
@@ -1078,6 +1078,7 @@ extern pthread_mutex_t console_lock;
 extern cglock_t ch_lock;
 extern pthread_rwlock_t mining_thr_lock;
 extern pthread_rwlock_t devices_lock;
+extern pthread_mutex_t getaddr_lock;
 
 extern pthread_mutex_t restart_lock;
 extern pthread_cond_t restart_cond;
diff --git a/util.c b/util.c
index 4f3a3d4..634e23c 100644
--- a/util.c
+++ b/util.c
@@ -2240,7 +2240,7 @@ static bool socks4_negotiate(struct pool *pool, int sockd, bool socks4a)
 	unsigned short port;
 	in_addr_t inp;
 	char buf[515];
-	int i, len;
+	int i, len, ret;
 
 	buf[0] = 0x04;
 	buf[1] = 0x01;
@@ -2262,7 +2262,10 @@ static bool socks4_negotiate(struct pool *pool, int sockd, bool socks4a)
 		servinfo = &servinfobase;
 		memset(&hints, 0, sizeof(struct addrinfo));
 		hints.ai_family = AF_INET; /* IPV4 only */
-		if (!getaddrinfo(pool->sockaddr_url, NULL, &hints, &servinfo)) {
+		mutex_lock(&getaddr_lock);
+		ret = getaddrinfo(pool->sockaddr_url, NULL, &hints, &servinfo);
+		mutex_unlock(&getaddr_lock);
+		if (ret == 0) {
 			struct sockaddr_in *saddr_in = (struct sockaddr_in *)servinfo->ai_addr;
 
 			inp = ntohl(saddr_in->sin_addr.s_addr);
@@ -2348,7 +2351,7 @@ static bool setup_stratum_socket(struct pool *pool)
 {
 	struct addrinfo servinfobase, *servinfo, *hints, *p;
 	char *sockaddr_url, *sockaddr_port;
-	int sockd;
+	int sockd, ret;
 
 	mutex_lock(&pool->stratum_lock);
 	pool->stratum_active = false;
@@ -2376,7 +2379,10 @@ static bool setup_stratum_socket(struct pool *pool)
 		sockaddr_url = pool->sockaddr_url;
 		sockaddr_port = pool->stratum_port;
 	}
-	if (getaddrinfo(sockaddr_url, sockaddr_port, hints, &servinfo) != 0) {
+	mutex_lock(&getaddr_lock);
+	ret = getaddrinfo(sockaddr_url, sockaddr_port, hints, &servinfo);
+	mutex_unlock(&getaddr_lock);
+	if (ret != 0) {
 		if (!pool->probed) {
 			applog(LOG_WARNING, "Failed to resolve (?wrong URL) %s:%s",
 			       sockaddr_url, sockaddr_port);