Commit 1a66aabd authored by Ondřej Surý's avatar Ondřej Surý Committed by Ondřej Surý

Refactor the dns_name API to use ISC_THREAD_LOCAL

Previously, the dns_name API used isc_thread_key API for TLS, which is
fairly complicated and requires initialization of memory contexts, etc.
This part of code was refactored to use a ISC_THREAD_LOCAL pointer which
greatly simplifies the whole code related to storing TLS variables.
parent 01731d4b
......@@ -709,8 +709,6 @@ main(int argc, char **argv) {
cfg_parser_destroy(&parser);
dns_name_destroy();
isc_log_destroy(&logc);
isc_mem_destroy(&mctx);
......
......@@ -85,9 +85,9 @@ usage(void) {
static void
destroy(void) {
if (zone != NULL)
if (zone != NULL) {
dns_zone_detach(&zone);
dns_name_destroy();
}
}
/*% main processing routine */
......
......@@ -4252,7 +4252,6 @@ destroy_libs(void) {
result = dns_name_settotextfilter(NULL);
check_result(result, "dns_name_settotextfilter");
#endif /* HAVE_LIBIDN2 */
dns_name_destroy();
if (commctx != NULL) {
debug("freeing commctx");
......
......@@ -517,7 +517,6 @@ main(int argc, char **argv) {
}
cleanup_logging(&log);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10) {
isc_mem_stats(mctx, stdout);
}
......
......@@ -439,7 +439,6 @@ main(int argc, char **argv) {
dns_rdataset_disassociate(&rdataset);
cleanup_logging(&log);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
......
......@@ -694,7 +694,6 @@ main(int argc, char **argv) {
cleanup_logging(&log);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
isc_mem_free(mctx, label);
......
......@@ -1222,7 +1222,6 @@ main(int argc, char **argv) {
cleanup_logging(&log);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
......
......@@ -3922,7 +3922,6 @@ main(int argc, char *argv[]) {
cleanup_logging(&log);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
......
......@@ -335,7 +335,6 @@ main(int argc, char *argv[]) {
cleanup_logging(&log);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
......
......@@ -1329,8 +1329,6 @@ cleanup(void) {
dlz_dlopen_clear();
#endif
dns_name_destroy();
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
NAMED_LOGMODULE_MAIN,
ISC_LOG_NOTICE, "exiting");
......
......@@ -3210,9 +3210,6 @@ cleanup(void) {
ddebug("Shutting down timer manager");
isc_timermgr_destroy(&timermgr);
ddebug("Destroying name state");
dns_name_destroy();
ddebug("Removing log context");
isc_log_destroy(&glctx);
......
......@@ -1022,8 +1022,6 @@ main(int argc, char **argv) {
isc_mem_put(rndc_mctx, args, argslen);
isccc_ccmsg_invalidate(&ccmsg);
dns_name_destroy();
isc_buffer_free(&databuf);
if (show_final_mem)
......
......@@ -145,7 +145,6 @@ main(int argc, char **argv) {
isc_log_setcontext(NULL);
dns_log_setcontext(NULL);
dst_lib_destroy();
dns_name_destroy();
isc_mem_destroy(&mctx);
return (0);
#else /* !USE_PKCS11 */
......
......@@ -197,8 +197,8 @@ LIBDNS_EXTERNAL_DATA extern const dns_name_t *dns_wildcardname;
* 'target' is the buffer to be converted. The region to be converted
* is from 'buffer'->base + 'used_org' to the end of the used region.
*/
typedef isc_result_t (*dns_name_totextfilter_t)(isc_buffer_t *target,
unsigned int used_org);
typedef isc_result_t (dns_name_totextfilter_t)(isc_buffer_t *target,
unsigned int used_org);
/***
*** Initialization
......@@ -1217,7 +1217,7 @@ dns_name_fromstring2(dns_name_t *target, const char *src,
*/
isc_result_t
dns_name_settotextfilter(dns_name_totextfilter_t proc);
dns_name_settotextfilter(dns_name_totextfilter_t *proc);
/*%<
* Set / clear a thread specific function 'proc' to be called at the
* end of dns_name_totext().
......@@ -1296,18 +1296,6 @@ dns_name_internalwildcard(const dns_name_t *name);
* \li 'name' to be valid.
*/
void
dns_name_destroy(void);
/*%<
* Cleanup dns_name_settotextfilter() / dns_name_totext() state.
*
* This should be called as part of the final cleanup process.
*
* Note: dns_name_settotextfilter(NULL); should be called for all
* threads which have called dns_name_settotextfilter() with a
* non-NULL argument prior to calling dns_name_destroy();
*/
bool
dns_name_isdnssd(const dns_name_t *owner);
/*%<
......
......@@ -163,11 +163,7 @@ LIBDNS_EXTERNAL_DATA const dns_name_t *dns_wildcardname = &wild;
/*
* dns_name_t to text post-conversion procedure.
*/
static int thread_key_initialized = 0;
static isc_mutex_t thread_key_mutex;
static isc_mem_t *thread_key_mctx = NULL;
static isc_thread_key_t totext_filter_proc_key;
static isc_once_t once = ISC_ONCE_INIT;
ISC_THREAD_LOCAL dns_name_totextfilter_t *totext_filter_proc = NULL;
static void
set_offsets(const dns_name_t *name, unsigned char *offsets,
......@@ -1272,52 +1268,6 @@ dns_name_fromtext(dns_name_t *name, isc_buffer_t *source,
return (ISC_R_SUCCESS);
}
static void
free_specific(void *arg) {
dns_name_totextfilter_t *mem = arg;
isc_mem_put(thread_key_mctx, mem, sizeof(*mem));
/* Stop use being called again. */
(void)isc_thread_key_setspecific(totext_filter_proc_key, NULL);
}
static void
thread_key_mutex_init(void) {
isc_mutex_init(&thread_key_mutex);
}
static isc_result_t
totext_filter_proc_key_init(void) {
isc_result_t result;
/*
* We need the call to isc_once_do() to support profiled mutex
* otherwise thread_key_mutex could be initialized at compile time.
*/
result = isc_once_do(&once, thread_key_mutex_init);
if (result != ISC_R_SUCCESS)
return (result);
if (!thread_key_initialized) {
LOCK(&thread_key_mutex);
if (thread_key_mctx == NULL) {
isc_mem_create(&thread_key_mctx);
}
isc_mem_setname(thread_key_mctx, "threadkey", NULL);
isc_mem_setdestroycheck(thread_key_mctx, false);
if (!thread_key_initialized &&
isc_thread_key_create(&totext_filter_proc_key,
free_specific) != 0) {
result = ISC_R_FAILURE;
isc_mem_detach(&thread_key_mctx);
} else {
thread_key_initialized = 1;
}
UNLOCK(&thread_key_mutex);
}
return (result);
}
isc_result_t
dns_name_totext(const dns_name_t *name, bool omit_final_dot,
isc_buffer_t *target)
......@@ -1346,9 +1296,6 @@ dns_name_totext2(const dns_name_t *name, unsigned int options,
unsigned int labels;
bool saw_root = false;
unsigned int oused;
dns_name_totextfilter_t *mem;
dns_name_totextfilter_t totext_filter_proc = NULL;
isc_result_t result;
bool omit_final_dot = ((options & DNS_NAME_OMITFINALDOT) != 0);
/*
......@@ -1360,9 +1307,6 @@ dns_name_totext2(const dns_name_t *name, unsigned int options,
oused = target->used;
result = totext_filter_proc_key_init();
if (result != ISC_R_SUCCESS)
return (result);
ndata = name->ndata;
nlen = name->length;
labels = name->labels;
......@@ -1502,11 +1446,9 @@ dns_name_totext2(const dns_name_t *name, unsigned int options,
}
isc_buffer_add(target, tlen - trem);
mem = isc_thread_key_getspecific(totext_filter_proc_key);
if (mem != NULL)
totext_filter_proc = *mem;
if (totext_filter_proc != NULL)
return ((*totext_filter_proc)(target, oused));
if (totext_filter_proc != NULL) {
return ((totext_filter_proc)(target, oused));
}
return (ISC_R_SUCCESS);
}
......@@ -2320,40 +2262,23 @@ dns_name_print(const dns_name_t *name, FILE *stream) {
}
isc_result_t
dns_name_settotextfilter(dns_name_totextfilter_t proc) {
isc_result_t result;
dns_name_totextfilter_t *mem;
int res;
result = totext_filter_proc_key_init();
if (result != ISC_R_SUCCESS)
return (result);
dns_name_settotextfilter(dns_name_totextfilter_t *proc) {
/*
* If we already have been here set / clear as appropriate.
* Otherwise allocate memory.
*/
mem = isc_thread_key_getspecific(totext_filter_proc_key);
if (mem != NULL && proc != NULL) {
*mem = proc;
return (ISC_R_SUCCESS);
if (totext_filter_proc != NULL && proc != NULL) {
if (totext_filter_proc == proc) {
return (ISC_R_SUCCESS);
}
}
if (proc == NULL) {
if (mem != NULL)
isc_mem_put(thread_key_mctx, mem, sizeof(*mem));
res = isc_thread_key_setspecific(totext_filter_proc_key, NULL);
if (res != 0)
result = ISC_R_UNEXPECTED;
return (result);
if (proc == NULL && totext_filter_proc != NULL) {
totext_filter_proc = NULL;
return (ISC_R_SUCCESS);
}
mem = isc_mem_get(thread_key_mctx, sizeof(*mem));
*mem = proc;
if (isc_thread_key_setspecific(totext_filter_proc_key, mem) != 0) {
isc_mem_put(thread_key_mctx, mem, sizeof(*mem));
result = ISC_R_UNEXPECTED;
}
return (result);
totext_filter_proc = proc;
return (ISC_R_SUCCESS);
}
void
......@@ -2511,21 +2436,6 @@ dns_name_copynf(const dns_name_t *source, dns_name_t *dest)
RUNTIME_CHECK(name_copy(source, dest, dest->buffer) == ISC_R_SUCCESS);
}
void
dns_name_destroy(void) {
RUNTIME_CHECK(isc_once_do(&once, thread_key_mutex_init)
== ISC_R_SUCCESS);
LOCK(&thread_key_mutex);
if (thread_key_initialized) {
isc_mem_detach(&thread_key_mctx);
isc_thread_key_delete(totext_filter_proc_key);
thread_key_initialized = 0;
}
UNLOCK(&thread_key_mutex);
}
/*
* Service Discovery Prefixes RFC 6763.
*/
......
......@@ -584,7 +584,6 @@ dns_name_concatenate
dns_name_copy
dns_name_copynf
dns_name_countlabels
dns_name_destroy
dns_name_digest
dns_name_downcase
dns_name_dup
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment