Skip to content

Commit 36e0034

Browse files
Fix locking/race for tdns error logging
Patch by: michaelortmann
1 parent 49e5a00 commit 36e0034

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/dns.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ void *thread_dns_hostbyip(void *arg)
504504

505505
i = getnameinfo((const struct sockaddr *) &addr->addr.sa, addr->addrlen,
506506
dtn->host, sizeof dtn->host, NULL, 0, 0);
507+
pthread_mutex_lock(&dtn->mutex);
507508
if (!i)
508509
*dtn->strerror = 0;
509510
else {
@@ -515,7 +516,6 @@ void *thread_dns_hostbyip(void *arg)
515516
#endif
516517
inet_ntop(AF_INET, &addr->addr.s4.sin_addr.s_addr, dtn->host, sizeof dtn->host);
517518
}
518-
pthread_mutex_lock(&dtn->mutex);
519519
close(dtn->fildes[1]);
520520
pthread_mutex_unlock(&dtn->mutex);
521521
return NULL;
@@ -530,6 +530,7 @@ void *thread_dns_ipbyhost(void *arg)
530530

531531
error = getaddrinfo(dtn->host, NULL, NULL, &res0);
532532
memset(addr, 0, sizeof *addr);
533+
pthread_mutex_lock(&dtn->mutex);
533534
if (!error) {
534535
*dtn->strerror = 0;
535536
#ifdef IPV6
@@ -563,11 +564,13 @@ void *thread_dns_ipbyhost(void *arg)
563564
}
564565
else if (error == EAI_NONAME)
565566
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): not known");
566-
else if (error == EAI_SYSTEM)
567-
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s: %s", gai_strerror(error), strerror(errno));
568-
else
567+
else if (error == EAI_SYSTEM) {
568+
char ebuf[2048];
569+
if (strerror_r(errno, ebuf, sizeof ebuf))
570+
strcpy(ebuf, "strerror_r()");
571+
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s: %s", gai_strerror(error), ebuf);
572+
} else
569573
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s", gai_strerror(error));
570-
pthread_mutex_lock(&dtn->mutex);
571574
close(dtn->fildes[1]);
572575
pthread_mutex_unlock(&dtn->mutex);
573576
return NULL;

src/net.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -1061,27 +1061,24 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly)
10611061
#ifdef EGG_TDNS
10621062
dtn_prev = dns_thread_head;
10631063
for (dtn = dtn_prev->next; dtn; dtn = dtn->next) {
1064+
pthread_mutex_lock(&dtn->mutex);
10641065
if (*dtn->strerror)
10651066
debug2("%s: hostname %s", dtn->strerror, dtn->host);
10661067
fd = dtn->fildes[0];
10671068
if (FD_ISSET(fd, &fdr)) {
1068-
if (dtn->type == DTN_TYPE_HOSTBYIP) {
1069-
pthread_mutex_lock(&dtn->mutex);
1069+
if (dtn->type == DTN_TYPE_HOSTBYIP)
10701070
call_hostbyip(&dtn->addr, dtn->host, !*dtn->strerror);
1071-
pthread_mutex_unlock(&dtn->mutex);
1072-
}
1073-
else {
1074-
pthread_mutex_lock(&dtn->mutex);
1071+
else
10751072
call_ipbyhost(dtn->host, &dtn->addr, !*dtn->strerror);
1076-
pthread_mutex_unlock(&dtn->mutex);
1077-
}
1078-
close(dtn->fildes[0]);
1073+
pthread_mutex_unlock(&dtn->mutex);
1074+
close(fd);
10791075
if (pthread_join(dtn->thread_id, &res))
10801076
putlog(LOG_MISC, "*", "sockread(): pthread_join(): error = %s", strerror(errno));
10811077
dtn_prev->next = dtn->next;
10821078
nfree(dtn);
10831079
dtn = dtn_prev;
1084-
}
1080+
} else
1081+
pthread_mutex_unlock(&dtn->mutex);
10851082
dtn_prev = dtn;
10861083
}
10871084
#endif

0 commit comments

Comments
 (0)