]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dlfcn-util: add dlsym_many_and_warn() helper
authorLennart Poettering <lennart@poettering.net>
Wed, 24 Jun 2020 12:47:39 +0000 (14:47 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 21 Jul 2020 08:36:38 +0000 (10:36 +0200)
When we dlopen() some library we most likely will resolve more than a
few symbols, hence add a helper that makes it easy.

src/basic/dlfcn-util.c [new file with mode: 0644]
src/basic/dlfcn-util.h
src/basic/meson.build

diff --git a/src/basic/dlfcn-util.c b/src/basic/dlfcn-util.c
new file mode 100644 (file)
index 0000000..08ded96
--- /dev/null
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include "dlfcn-util.h"
+
+int dlsym_many_and_warn(void *dl, int level, ...) {
+        va_list ap;
+        int r;
+
+        /* Tries to resolve a bunch of function symbols, and logs errors about the ones it cannot
+         * resolve. Note that this function possibly modifies the supplied function pointers if the whole
+         * operation fails */
+
+        va_start(ap, level);
+
+        for (;;) {
+                void (**fn)(void);
+                void (*tfn)(void);
+                const char *symbol;
+
+                fn = va_arg(ap, typeof(fn));
+                if (!fn)
+                        break;
+
+                symbol = va_arg(ap, typeof(symbol));
+
+                tfn = (typeof(tfn)) dlsym(dl, symbol);
+                if (!tfn) {
+                        r = log_full_errno(level,
+                                           SYNTHETIC_ERRNO(ELIBBAD),
+                                           "Can't find symbol %s: %s", symbol, dlerror());
+                        va_end(ap);
+                        return r;
+                }
+
+                *fn = tfn;
+        }
+
+        va_end(ap);
+        return 0;
+}
index d254afb68b5d1d4138420f245fb12e5c562be8a9..df66cdfd38f8a57bbe9424dd2fbe499fb52db424 100644 (file)
@@ -6,3 +6,5 @@
 #include "macro.h"
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(void*, dlclose);
+
+int dlsym_many_and_warn(void *dl, int level, ...);
index 90924d6cb895525e1becade481fff0eb5a6600b6..42d0754d6d1891e29a88f3e943f34e43605586ad 100644 (file)
@@ -39,6 +39,7 @@ basic_sources = files('''
         device-nodes.h
         dirent-util.c
         dirent-util.h
+        dlfcn-util.c
         dlfcn-util.h
         efivars.c
         efivars.h