]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Refactor the dns_name API to use ISC_THREAD_LOCAL
authorOndřej Surý <ondrej@isc.org>
Mon, 2 Dec 2019 10:42:50 +0000 (11:42 +0100)
committerOndřej Surý <ondrej@sury.org>
Tue, 3 Dec 2019 15:27:24 +0000 (16:27 +0100)
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.

16 files changed:
bin/check/named-checkconf.c
bin/check/named-checkzone.c
bin/dig/dighost.c
bin/dnssec/dnssec-dsfromkey.c
bin/dnssec/dnssec-importkey.c
bin/dnssec/dnssec-keyfromlabel.c
bin/dnssec/dnssec-keygen.c
bin/dnssec/dnssec-signzone.c
bin/dnssec/dnssec-verify.c
bin/named/main.c
bin/nsupdate/nsupdate.c
bin/rndc/rndc.c
bin/tests/system/rsabigexponent/bigkey.c
lib/dns/include/dns/name.h
lib/dns/name.c
lib/dns/win32/libdns.def.in

index c8b12911bbeed2a15ea3a6f9e6d0aeb2fb3d6035..0e5bde922fede1a2a7ab68ded8efc6876ea39491 100644 (file)
@@ -709,8 +709,6 @@ main(int argc, char **argv) {
 
        cfg_parser_destroy(&parser);
 
-       dns_name_destroy();
-
        isc_log_destroy(&logc);
 
        isc_mem_destroy(&mctx);
index de6bb94b8e9e336d4184aeb2e30bd4a1b7171e4f..67613de41d39e919cfe445442c1cdc9a1cd0237f 100644 (file)
@@ -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 */
index bc712a1ad015886f1f415add888eeb0eb0801a93..b9bc11603bfd9b35cd8d49687b8b3cda8466be87 100644 (file)
@@ -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");
index 4923fe6e8a9e03724960e58726e9df8c2aa8c517..95ba8e9e7e20b6d963f79863028f36175c5d9186 100644 (file)
@@ -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);
        }
index 964efcad0eec52484ea0524d654e591e1a355e97..4a4684d7415e2d98aebb99a38187833074a570f6 100644 (file)
@@ -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);
index e9dfe36f4fe7c9e67cba8aeade5407f3233908f8..4bc653f3a5c3d6eca8b8ed4d13bfbc40b15cf87e 100644 (file)
@@ -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);
index 40cffe2168d6eedc90bc1b74e43963a88b4cf51d..220cbe7a00dfaf96af04a8559db4232096af1ae4 100644 (file)
@@ -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);
index 7682a178e9ec499dcfcfd5cb17cc71d0b60630b2..8008ed7712646599adbf3b5df76ad16aab6c195d 100644 (file)
@@ -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);
index 483577ef4fb7fe70f2d41006957f6e7db22724af..29894c9e81f78449481650a46d3a945d9cec6276 100644 (file)
@@ -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);
index 10e4f536a0c734f6654b979b6860912fbb0d7b0e..f1c14c511d172888ed2dd5eb531a6c28cb4b6969 100644 (file)
@@ -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");
index a272b14a0f86f29d1c92af6f1afc68893bac3d83..4b4ce5cfad0b0556b8fe575b8368e6430ed4c14b 100644 (file)
@@ -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);
 
index d9f32a4507a2c9a721357a9b18aeceede5369f56..cc91793859d74d1a10217d2cea811f1250671813 100644 (file)
@@ -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)
index d01d264832efe98e4e842befd8ea7fcc0c8675eb..a1c4a01c4ff5881140ee7b2aa26eb0ae59058e5f 100644 (file)
@@ -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 */
index 6b504924916429ab15af03844f501363a730b9c5..2c051265fc6750d8ad187fb2904497f63bedfc58 100644 (file)
@@ -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);
 /*%<
index 3bb2e4fcf86758ff33641690b258235085f150e4..82b546429ef00ab7a220138fe01bb129e0788e85 100644 (file)
@@ -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.
  */
index b1a02a0f68f09c7b2b14b94377f2398f34c03630..2eef22497c10bd0939a36a429be587844ffb99c5 100644 (file)
@@ -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