From b94a5591286b999ae863639aaac4c8703d2d808c Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Sat, 29 Jun 2024 13:28:04 +0200 Subject: [PATCH] Fix locking proper, thank you thommey and valgrind --- src/dns.c | 4 ++-- src/net.c | 17 +++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/dns.c b/src/dns.c index 32bc53849..7d6658954 100644 --- a/src/dns.c +++ b/src/dns.c @@ -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 { @@ -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; @@ -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 @@ -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; diff --git a/src/net.c b/src/net.c index 3e23acb97..ce310d73c 100644 --- a/src/net.c +++ b/src/net.c @@ -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