From afef79bee7eb86e7f395fc2b38f9e6cf9a4fc269 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:07:12 +0100 Subject: [PATCH] Join threads instead of detach Found by: michaelortmann Patch by: michaelortmann --- src/dns.c | 18 ++---------------- src/eggdrop.h | 1 + src/net.c | 3 +++ 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/dns.c b/src/dns.c index ab1fdbeae..2e5035b88 100644 --- a/src/dns.c +++ b/src/dns.c @@ -568,7 +568,6 @@ void *thread_dns_ipbyhost(void *arg) void core_dns_hostbyip(sockname_t *addr) { struct dns_thread_node *dtn = nmalloc(sizeof(struct dns_thread_node)); - pthread_t thread; /* only used by pthread_create(), no need to save */ pthread_attr_t attr; if (pthread_attr_init(&attr)) { @@ -577,12 +576,6 @@ void core_dns_hostbyip(sockname_t *addr) nfree(dtn); return; } - if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { - putlog(LOG_MISC, "*", "core_dns_hostbyip(): pthread_attr_setdetachstate(): error = %s", strerror(errno)); - call_hostbyip(addr, iptostr(&addr->addr.sa), 0); - nfree(dtn); - return; - } if (pthread_mutex_init(&dtn->mutex, NULL)) fatal("ERROR: core_dns_hostbyip(): pthread_mutex_init() failed", 0); if (pipe(dtn->fildes) < 0) { @@ -592,7 +585,7 @@ void core_dns_hostbyip(sockname_t *addr) return; } memcpy(&dtn->addr, addr, sizeof *addr); - if (pthread_create(&thread, &attr, thread_dns_hostbyip, (void *) dtn)) { + if (pthread_create(&(dtn->thread_id), &attr, thread_dns_hostbyip, (void *) dtn)) { putlog(LOG_MISC, "*", "core_dns_hostbyip(): pthread_create(): error = %s", strerror(errno)); call_hostbyip(addr, iptostr(&addr->addr.sa), 0); close(dtn->fildes[0]); @@ -609,7 +602,6 @@ void core_dns_ipbyhost(char *host) { sockname_t addr; struct dns_thread_node *dtn; - pthread_t thread; /* only used by pthread_create(), no need to save */ pthread_attr_t attr; /* if addr is ip instead of host */ @@ -624,12 +616,6 @@ void core_dns_ipbyhost(char *host) nfree(dtn); return; } - if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { - putlog(LOG_MISC, "*", "core_dns_ipbyhost(): pthread_attr_setdetachstate(): error = %s", strerror(errno)); - call_ipbyhost(host, &addr, 0); - nfree(dtn); - return; - } if (pthread_mutex_init(&dtn->mutex, NULL)) fatal("ERROR: core_dns_ipbyhost(): pthread_mutex_init() failed", 0); if (pipe(dtn->fildes) < 0) { @@ -641,7 +627,7 @@ void core_dns_ipbyhost(char *host) dtn->next = dns_thread_head->next; dns_thread_head->next = dtn; strlcpy(dtn->host, host, sizeof dtn->host); - if (pthread_create(&thread, &attr, thread_dns_ipbyhost, (void *) dtn)) { + if (pthread_create(&(dtn->thread_id), &attr, thread_dns_ipbyhost, (void *) dtn)) { putlog(LOG_MISC, "*", "core_dns_ipbyhost(): pthread_create(): error = %s", strerror(errno)); call_ipbyhost(host, &addr, 0); close(dtn->fildes[0]); diff --git a/src/eggdrop.h b/src/eggdrop.h index a7774e294..076a16252 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -751,6 +751,7 @@ enum { /* linked list instead of array because of multi threading */ struct dns_thread_node { + pthread_t thread_id; pthread_mutex_t mutex; int fildes[2]; int type; diff --git a/src/net.c b/src/net.c index 9ea150019..c8aa62cd5 100644 --- a/src/net.c +++ b/src/net.c @@ -899,6 +899,7 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) #ifdef EGG_TDNS int fd; struct dns_thread_node *dtn, *dtn_prev; + void *res; #endif maxfd_r = preparefdset(&fdr, slist, slistmax, tclonly, TCL_READABLE); @@ -1065,6 +1066,8 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) pthread_mutex_unlock(&dtn->mutex); } close(dtn->fildes[0]); + 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;