Skip to content

Commit

Permalink
Join threads instead of detach
Browse files Browse the repository at this point in the history
Found by: michaelortmann
Patch by: michaelortmann
  • Loading branch information
michaelortmann committed Feb 18, 2024
1 parent 1c5ea70 commit afef79b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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]);
Expand All @@ -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 */
Expand All @@ -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) {
Expand All @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions src/eggdrop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit afef79b

Please sign in to comment.