]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
use uv_dlopen() instead of dlopen() when linking DNSRPZ
authorEvan Hunt <each@isc.org>
Thu, 22 Aug 2024 23:01:50 +0000 (16:01 -0700)
committerOndřej Surý <ondrej@isc.org>
Wed, 18 Sep 2024 15:24:13 +0000 (17:24 +0200)
take advantage of libuv's shared library handling capability
when linking to a DNSRPS library.  (see b396f555861 and 37b9511ce1d
for prior related work.)

bin/tests/system/Makefile.am
bin/tests/system/rpz/testlib/Makefile.am
configure.ac
lib/dns/dnsrps.c
lib/dns/include/dns/librpz.h

index ddda9ac2e3e0973c5862b459594d27d9e2b5d311..346a054ae56594ef4533032b1fb934d37002d0c9 100644 (file)
@@ -67,8 +67,8 @@ rpz_dnsrps_CPPFLAGS =         \
 
 rpz_dnsrps_LDADD =             \
        $(LDADD)                \
-       $(LIBDNS_LIBS)          \
-       $(DLOPEN_LIBS)
+       $(LIBUV_LIBS)           \
+       $(LIBDNS_LIBS)
 
 # Longer running tests are listed (and executed) first to take the most
 # advantage of parallel execution.
index 313a56d3d63789b2a01922964446e98eab44b65e..7dcd9fe71fe176d2ef213c2b99f07119bc1dc342 100644 (file)
@@ -9,4 +9,4 @@ AM_CFLAGS += -Wall -pedantic -Wno-zero-length-array
 noinst_LTLIBRARIES = libdummyrpz.la
 libdummyrpz_la_SOURCES= dummylib.c test-data.c trpz.h test-data.h
 libdummyrpz_la_LDFLAGS = -avoid-version -module -shared -export-dynamic -rpath $(abs_builddir)
-LDADD += -lpthread $(DLOPEN_LIBS)
+LDADD += -lpthread
index 80b98ac94889f0f6ac2c6b61f0bee417a354cf75..7b16b9f85b0bde3b0b00994ed4920ec859b191fd 100644 (file)
@@ -108,9 +108,6 @@ AC_PROG_MKDIR_P
 # Initialize libtool
 LT_INIT([disable-static dlopen pic-only])
 
-DLOPEN_LIBS="$lt_cv_dlopen_libs"
-AC_SUBST(DLOPEN_LIBS)
-
 AS_IF([test "$enable_static" != "no" && test "$enable_developer" != "yes"],
       [AC_MSG_ERROR([Static linking is not supported as it disables dlopen() and certain security features (e.g. RELRO, ASLR)])])
 
index a3b83e11e89d2985ab261a66bdb71958ad10af77..5dcc2fcf65277328ffb9828309b1c3e6c814bbd5 100644 (file)
@@ -35,7 +35,7 @@
 
 librpz_t *librpz = NULL;
 librpz_emsg_t librpz_lib_open_emsg;
-static void *librpz_handle = NULL;
+static uv_lib_t librpz_handle;
 
 #define RPSDB_MAGIC       ISC_MAGIC('R', 'P', 'Z', 'F')
 #define VALID_RPSDB(rpsdb) ((rpsdb)->common.impmagic == RPSDB_MAGIC)
@@ -134,7 +134,6 @@ dns_dnsrps_server_create(const char *librpz_path) {
 
        INSIST(clist == NULL);
        INSIST(librpz == NULL);
-       INSIST(librpz_handle == NULL);
 
        /*
         * Notice if librpz is available.
@@ -171,14 +170,7 @@ dns_dnsrps_server_destroy(void) {
 
 #if DNSRPS_LIB_OPEN == 2
        if (librpz != NULL) {
-               INSIST(librpz_handle != NULL);
-               if (dlclose(librpz_handle) != 0) {
-                       isc_log_write(DNS_LOGCATEGORY_RPZ, DNS_LOGMODULE_RBTDB,
-                                     DNS_RPZ_ERROR_LEVEL,
-                                     "dnsrps: dlclose(): %s", dlerror());
-               }
-               librpz_handle = NULL;
-               librpz = NULL;
+               librpz_lib_close(&librpz, &librpz_handle);
        }
 #endif
 }
index aee8d0fbf51acb3632ba3c1284251d3ace100f5e..662545670b886933cf2254ee6b3e194d6c173a81 100644 (file)
@@ -43,6 +43,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <sys/types.h>
+#include <uv.h>
 
 /*
  * Allow either ordinary or dlopen() linking.
@@ -852,71 +853,48 @@ extern librpz_t     *librpz;
  * @return address of interface structure or NULL on failure
  */
 static inline librpz_t *
-librpz_lib_open(librpz_emsg_t *emsg, void **dl_handle, const char *path) {
-       void     *handle;
-       librpz_t *new_librpz;
+librpz_lib_open(librpz_emsg_t *emsg, uv_lib_t *lib, const char *path) {
+       librpz_t *new_librpz = NULL;
+       int       r;
 
        emsg->c[0] = '\0';
 
-       /*
-        * Close a previously opened handle on librpz.so.
-        */
-       if (dl_handle != NULL && *dl_handle != NULL) {
-               if (dlclose(*dl_handle) != 0) {
-                       snprintf(emsg->c, sizeof(librpz_emsg_t),
-                                "dlopen(NULL): %s", dlerror());
-                       return (NULL);
-               }
-               *dl_handle = NULL;
-       }
-
-       /*
-        * First try the main executable of the process in case it was
-        * linked to librpz.
-        * Do not worry if we cannot search the main executable of the process.
-        */
-       handle = dlopen(NULL, RTLD_NOW | RTLD_LOCAL);
-       if (handle != NULL) {
-               new_librpz = dlsym(handle, LIBRPZ_DEF_STR);
-               if (new_librpz != NULL) {
-                       if (dl_handle != NULL) {
-                               *dl_handle = handle;
-                               handle = NULL;
-                       }
-                       return (new_librpz);
-               }
-               if (dlclose(handle) != 0) {
-                       snprintf(emsg->c, sizeof(librpz_emsg_t),
-                                "dlsym(NULL, " LIBRPZ_DEF_STR "): %s",
-                                dlerror());
-                       return (NULL);
-               }
-       }
-
        if (path == NULL || path[0] == '\0') {
                snprintf(emsg->c, sizeof(librpz_emsg_t),
-                        "librpz not linked and no dlopen() path provided");
+                        "path to librpz not provided");
                return (NULL);
        }
 
-       handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
-       if (handle == NULL) {
-               snprintf(emsg->c, sizeof(librpz_emsg_t), "dlopen(%s): %s", path,
-                        dlerror());
+       r = uv_dlopen(path, lib);
+       if (r != 0) {
+               const char *errmsg = uv_dlerror(lib);
+               if (errmsg == NULL) {
+                       errmsg = "unknown error";
+               }
+               snprintf(emsg->c, sizeof(librpz_emsg_t), "uv_dlopen(%s): %s",
+                        path, errmsg);
+               uv_dlclose(lib);
                return (NULL);
        }
-       new_librpz = dlsym(handle, LIBRPZ_DEF_STR);
-       if (new_librpz != NULL) {
-               if (dl_handle != NULL) {
-                       *dl_handle = handle;
-                       handle = NULL;
+       r = uv_dlsym(lib, LIBRPZ_DEF_STR, (void **)&new_librpz);
+       if (r != 0) {
+               const char *errmsg = uv_dlerror(lib);
+               if (errmsg == NULL) {
+                       errmsg = "returned function pointer is NULL";
                }
-               return (new_librpz);
+               uv_dlclose(lib);
+               snprintf(emsg->c, sizeof(librpz_emsg_t),
+                        "dlsym(%s, " LIBRPZ_DEF_STR "): %s", path, errmsg);
+               return (NULL);
        }
-       snprintf(emsg->c, sizeof(librpz_emsg_t),
-                "dlsym(%s, " LIBRPZ_DEF_STR "): %s", path, dlerror());
-       dlclose(handle);
-       return (NULL);
+
+       return (new_librpz);
+}
+
+static inline void
+librpz_lib_close(librpz_t **rpz, uv_lib_t *lib) {
+       *rpz = NULL;
+       uv_dlclose(lib);
 }
 #elif defined(LIBRPZ_LIB_OPEN)
 /*