From: Evan Hunt Date: Thu, 22 Aug 2024 23:01:50 +0000 (-0700) Subject: use uv_dlopen() instead of dlopen() when linking DNSRPZ X-Git-Tag: v9.21.2~32^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc133339579c05e277357a5ba61344c8a41d012f;p=thirdparty%2Fbind9.git use uv_dlopen() instead of dlopen() when linking DNSRPZ take advantage of libuv's shared library handling capability when linking to a DNSRPS library. (see b396f555861 and 37b9511ce1d for prior related work.) --- diff --git a/bin/tests/system/Makefile.am b/bin/tests/system/Makefile.am index ddda9ac2e3e..346a054ae56 100644 --- a/bin/tests/system/Makefile.am +++ b/bin/tests/system/Makefile.am @@ -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. diff --git a/bin/tests/system/rpz/testlib/Makefile.am b/bin/tests/system/rpz/testlib/Makefile.am index 313a56d3d63..7dcd9fe71fe 100644 --- a/bin/tests/system/rpz/testlib/Makefile.am +++ b/bin/tests/system/rpz/testlib/Makefile.am @@ -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 diff --git a/configure.ac b/configure.ac index 80b98ac9488..7b16b9f85b0 100644 --- a/configure.ac +++ b/configure.ac @@ -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)])]) diff --git a/lib/dns/dnsrps.c b/lib/dns/dnsrps.c index a3b83e11e89..5dcc2fcf652 100644 --- a/lib/dns/dnsrps.c +++ b/lib/dns/dnsrps.c @@ -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 } diff --git a/lib/dns/include/dns/librpz.h b/lib/dns/include/dns/librpz.h index aee8d0fbf51..662545670b8 100644 --- a/lib/dns/include/dns/librpz.h +++ b/lib/dns/include/dns/librpz.h @@ -43,6 +43,7 @@ #include #include #include +#include /* * 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) /*