diff --git a/src/dns.c b/src/dns.c index 8e447a0aa..bae860c25 100644 --- a/src/dns.c +++ b/src/dns.c @@ -39,6 +39,7 @@ extern int dcc_total; extern time_t now; extern Tcl_Interp *interp; #ifdef EGG_TDNS + pthread_attr_t attr; struct dns_thread_node *dns_thread_head; extern int pref_af; #else @@ -48,6 +49,15 @@ extern Tcl_Interp *interp; devent_t *dns_events = NULL; +#ifdef EGG_TDNS +void init_tdns() { + if (pthread_attr_init(&attr)) + fatal("ERROR: init_tnds(): pthread_attr_init()", 0); + dns_thread_head = nmalloc(sizeof(struct dns_thread_node)); + dns_thread_head->next = NULL; +} +#endif + static int ipaddr_equal(const sockname_t *ip, const sockname_t *ip2) { if (!ip || !ip2) { @@ -575,19 +585,13 @@ 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_attr_t attr; - if (pthread_attr_init(&attr)) { - putlog(LOG_MISC, "*", "core_dns_hostbyip(): pthread_attr_init(): 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) { putlog(LOG_MISC, "*", "core_dns_hostbyip(): pipe(): error: %s", strerror(errno)); call_hostbyip(addr, iptostr(&addr->addr.sa), 0); + pthread_mutex_destroy(&dtn->mutex); nfree(dtn); return; } @@ -597,6 +601,7 @@ void core_dns_hostbyip(sockname_t *addr) call_hostbyip(addr, iptostr(&addr->addr.sa), 0); close(dtn->fildes[0]); close(dtn->fildes[1]); + pthread_mutex_destroy(&dtn->mutex); nfree(dtn); return; } @@ -609,7 +614,6 @@ void core_dns_ipbyhost(char *host) { sockname_t addr; struct dns_thread_node *dtn; - pthread_attr_t attr; /* if addr is ip instead of host */ if (setsockname(&addr, host, 0, 0) != AF_UNSPEC) { @@ -617,33 +621,28 @@ void core_dns_ipbyhost(char *host) return; } dtn = nmalloc(sizeof(struct dns_thread_node)); - if (pthread_attr_init(&attr)) { - putlog(LOG_MISC, "*", "core_dns_ipbyhost(): pthread_attr_init(): 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) { putlog(LOG_MISC, "*", "core_dns_ipbyhost(): pipe(): error: %s", strerror(errno)); call_ipbyhost(host, &addr, 0); + pthread_mutex_destroy(&dtn->mutex); nfree(dtn); return; } - dtn->next = dns_thread_head->next; - dns_thread_head->next = dtn; strlcpy(dtn->host, host, sizeof dtn->host); 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]); close(dtn->fildes[1]); - dns_thread_head->next = dtn->next; + pthread_mutex_destroy(&dtn->mutex); nfree(dtn); return; } dtn->type = DTN_TYPE_IPBYHOST; + dtn->next = dns_thread_head->next; + dns_thread_head->next = dtn; } #else /* EGG_TDNS */ /* diff --git a/src/main.c b/src/main.c index 525b0db49..203d48ad0 100644 --- a/src/main.c +++ b/src/main.c @@ -1068,9 +1068,8 @@ int main(int arg_c, char **arg_v) link_statics(); #endif #ifdef EGG_TDNS - /* initialize dns_thread_head before chanprog() */ - dns_thread_head = nmalloc(sizeof(struct dns_thread_node)); - dns_thread_head->next = NULL; + /* initialize attr and dns_thread_head before chanprog() */ + init_tdns(); #endif ctime_r(&now, s); s[24] = 0; diff --git a/src/net.c b/src/net.c index f373a308c..93ccdc563 100644 --- a/src/net.c +++ b/src/net.c @@ -1072,6 +1072,7 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) if (pthread_join(dtn->thread_id, &res)) putlog(LOG_MISC, "*", "sockread(): pthread_join(): error = %s", strerror(errno)); dtn_prev->next = dtn->next; + pthread_mutex_destroy(&dtn->mutex); nfree(dtn); dtn = dtn_prev; } else diff --git a/src/proto.h b/src/proto.h index e4b34a71a..1e9ed1458 100644 --- a/src/proto.h +++ b/src/proto.h @@ -169,6 +169,7 @@ void del_dcc(int); void changeover_dcc(int, struct dcc_table *, int); /* dns.c */ +void init_tdns(); extern void (*dns_hostbyip) (sockname_t *); void core_dns_hostbyip(sockname_t *); void call_hostbyip(sockname_t *, char *, int);