Skip to content

Commit

Permalink
Fix locking proper, thank you thommey and valgrind
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelortmann committed Jun 29, 2024
1 parent af0afc0 commit b94a559
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ void *thread_dns_hostbyip(void *arg)

i = getnameinfo((const struct sockaddr *) &addr->addr.sa, addr->addrlen,
dtn->host, sizeof dtn->host, NULL, 0, 0);
pthread_mutex_lock(&dtn->mutex);
if (!i)
*dtn->strerror = 0;
else {
Expand All @@ -515,7 +516,6 @@ void *thread_dns_hostbyip(void *arg)
#endif
inet_ntop(AF_INET, &addr->addr.s4.sin_addr.s_addr, dtn->host, sizeof dtn->host);
}
pthread_mutex_lock(&dtn->mutex);
close(dtn->fildes[1]);
pthread_mutex_unlock(&dtn->mutex);
return NULL;
Expand All @@ -530,6 +530,7 @@ void *thread_dns_ipbyhost(void *arg)

error = getaddrinfo(dtn->host, NULL, NULL, &res0);
memset(addr, 0, sizeof *addr);
pthread_mutex_lock(&dtn->mutex);
if (!error) {
*dtn->strerror = 0;
#ifdef IPV6
Expand Down Expand Up @@ -570,7 +571,6 @@ void *thread_dns_ipbyhost(void *arg)
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s: %s", gai_strerror(error), ebuf);
} else
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s", gai_strerror(error));
pthread_mutex_lock(&dtn->mutex);
close(dtn->fildes[1]);
pthread_mutex_unlock(&dtn->mutex);
return NULL;
Expand Down
17 changes: 7 additions & 10 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,27 +1053,24 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly)
#ifdef EGG_TDNS
dtn_prev = dns_thread_head;
for (dtn = dtn_prev->next; dtn; dtn = dtn->next) {
pthread_mutex_lock(&dtn->mutex);
if (*dtn->strerror)
debug2("%s: hostname %s", dtn->strerror, dtn->host);
fd = dtn->fildes[0];
if (FD_ISSET(fd, &fdr)) {
if (dtn->type == DTN_TYPE_HOSTBYIP) {
pthread_mutex_lock(&dtn->mutex);
if (dtn->type == DTN_TYPE_HOSTBYIP)
call_hostbyip(&dtn->addr, dtn->host, !*dtn->strerror);
pthread_mutex_unlock(&dtn->mutex);
}
else {
pthread_mutex_lock(&dtn->mutex);
else
call_ipbyhost(dtn->host, &dtn->addr, !*dtn->strerror);
pthread_mutex_unlock(&dtn->mutex);
}
close(dtn->fildes[0]);
pthread_mutex_unlock(&dtn->mutex);
close(fd);
if (pthread_join(dtn->thread_id, &res))
putlog(LOG_MISC, "*", "sockread(): pthread_join(): error = %s", strerror(errno));
dtn_prev->next = dtn->next;
nfree(dtn);
dtn = dtn_prev;
}
} else
pthread_mutex_unlock(&dtn->mutex);
dtn_prev = dtn;
}
#endif
Expand Down

0 comments on commit b94a559

Please sign in to comment.