]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
various: convert to the new dlopen_or_warn() helper
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 22 Jun 2021 07:24:02 +0000 (09:24 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 24 Jun 2021 08:16:46 +0000 (10:16 +0200)
src/journal/pcre2-dlopen.c
src/shared/bpf-dlopen.c
src/shared/cryptsetup-util.c
src/shared/idn-util.c
src/shared/libfido2-util.c
src/shared/pwquality-util.c
src/shared/qrcode-util.c

index 210e39a0aed0e23e9a36a6085c5eb8ef91c3c889..475d7eb26da27d6d6ee778191d945b12c54d308f 100644 (file)
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include "alloc-util.h"
 #include "dlfcn-util.h"
+#include "log.h"
 #include "pcre2-dlopen.h"
 
 #if HAVE_PCRE2
@@ -16,17 +16,6 @@ int (*sym_pcre2_match)(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, u
 PCRE2_SIZE* (*sym_pcre2_get_ovector_pointer)(pcre2_match_data *);
 
 int dlopen_pcre2(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
-        int r;
-
-        if (pcre2_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libpcre2-8.so.0", RTLD_LAZY);
-        if (!dl)
-                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "PCRE2 support is not installed: %s", dlerror());
-
         /* So here's something weird: PCRE2 actually renames the symbols exported by the library via C
          * macros, so that the exported symbols carry a suffix "_8" but when used from C the suffix is
          * gone. In the argument list below we ignore this mangling. Surprisingly (at least to me), we
@@ -35,25 +24,15 @@ int dlopen_pcre2(void) {
          * string actually contains the "_8" suffix already due to that and we don't have to append it
          * manually anymore. C is weird. 🤯 */
 
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_ERR,
+        return dlopen_many_sym_or_warn(
+                        &pcre2_dl, "libpcre2-8.so.0", LOG_ERR,
                         DLSYM_ARG(pcre2_match_data_create),
                         DLSYM_ARG(pcre2_match_data_free),
                         DLSYM_ARG(pcre2_code_free),
                         DLSYM_ARG(pcre2_compile),
                         DLSYM_ARG(pcre2_get_error_message),
                         DLSYM_ARG(pcre2_match),
-                        DLSYM_ARG(pcre2_get_ovector_pointer),
-                        NULL);
-        if (r < 0)
-                return r;
-
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        pcre2_dl = TAKE_PTR(dl);
-
-        return 1;
+                        DLSYM_ARG(pcre2_get_ovector_pointer));
 }
 
 #else
index 0d222ae19e93a04298f6f046c014af2136c81fd3..45ab986ab25913d2d9beda9d1cef55d50df65202 100644 (file)
@@ -1,8 +1,8 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include "alloc-util.h"
 #include "dlfcn-util.h"
 #include "bpf-dlopen.h"
+#include "log.h"
 
 #if HAVE_LIBBPF
 static void *bpf_dl = NULL;
@@ -24,20 +24,8 @@ bool (*sym_bpf_probe_prog_type)(enum bpf_prog_type, __u32);
 const char* (*sym_bpf_program__name)(const struct bpf_program *);
 
 int dlopen_bpf(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
-        int r;
-
-        if (bpf_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libbpf.so.0", RTLD_LAZY);
-        if (!dl)
-                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "libbpf is not installed: %s", dlerror());
-
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_DEBUG,
+        return dlopen_many_sym_or_warn(
+                        &bpf_dl, "libbpf.so.0", LOG_DEBUG,
                         DLSYM_ARG(bpf_link__destroy),
                         DLSYM_ARG(bpf_link__fd),
                         DLSYM_ARG(bpf_map__fd),
@@ -52,15 +40,7 @@ int dlopen_bpf(void) {
                         DLSYM_ARG(bpf_probe_prog_type),
                         DLSYM_ARG(bpf_program__attach_cgroup),
                         DLSYM_ARG(bpf_program__name),
-                        DLSYM_ARG(libbpf_get_error),
-                        NULL);
-        if (r < 0)
-                return r;
-
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        bpf_dl = TAKE_PTR(dl);
-        return 1;
+                        DLSYM_ARG(libbpf_get_error));
 }
 
 #else
index 99c78f68588a8f1e579aa4d818b09577b41eb4d7..b93f702aff994959d2c604b357d38ec2bc1d6a81 100644 (file)
@@ -51,20 +51,10 @@ crypt_token_info (*sym_crypt_token_status)(struct crypt_device *cd, int token, c
 int (*sym_crypt_volume_key_get)(struct crypt_device *cd, int keyslot, char *volume_key, size_t *volume_key_size, const char *passphrase, size_t passphrase_size);
 
 int dlopen_cryptsetup(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
         int r;
 
-        if (cryptsetup_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libcryptsetup.so.12", RTLD_LAZY);
-        if (!dl)
-                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "libcryptsetup support is not installed: %s", dlerror());
-
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_DEBUG,
+        r = dlopen_many_sym_or_warn(
+                        &cryptsetup_dl, "libcryptsetup.so.12", LOG_DEBUG,
                         DLSYM_ARG(crypt_activate_by_passphrase),
 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
                         DLSYM_ARG(crypt_activate_by_signed_key),
@@ -104,15 +94,10 @@ int dlopen_cryptsetup(void) {
                         DLSYM_ARG(crypt_token_max),
 #endif
                         DLSYM_ARG(crypt_token_status),
-                        DLSYM_ARG(crypt_volume_key_get),
-                        NULL);
-        if (r < 0)
+                        DLSYM_ARG(crypt_volume_key_get));
+        if (r <= 0)
                 return r;
 
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        cryptsetup_dl = TAKE_PTR(dl);
-
         /* Redirect the default logging calls of libcryptsetup to our own logging infra. (Note that
          * libcryptsetup also maintains per-"struct crypt_device" log functions, which we'll also set
          * whenever allocating a "struct crypt_device" context. Why set both? To be defensive: maybe some
index 63a9ebf99dd61bc229433f29b9fe549e67bd0d4c..6dda3af54c34f0e97a2c6f301cd28692d6f228cf 100644 (file)
@@ -21,32 +21,11 @@ const char *(*sym_idn2_strerror)(int rc) = NULL;
 int (*sym_idn2_to_unicode_8z8z)(const char * input, char ** output, int flags) = NULL;
 
 int dlopen_idn(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
-        int r;
-
-        if (idn_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libidn2.so.0", RTLD_LAZY);
-        if (!dl)
-                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "libidn2 support is not installed: %s", dlerror());
-
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_DEBUG,
+        return dlopen_many_sym_or_warn(
+                        &idn_dl, "libidn2.so.0", LOG_DEBUG,
                         DLSYM_ARG(idn2_lookup_u8),
                         DLSYM_ARG(idn2_strerror),
-                        DLSYM_ARG(idn2_to_unicode_8z8z),
-                        NULL);
-        if (r < 0)
-                return r;
-
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        idn_dl = TAKE_PTR(dl);
-
-        return 1;
+                        DLSYM_ARG(idn2_to_unicode_8z8z));
 }
 #endif
 
index c69943262df7e21ebd7c8ce00b8820dceedca6a1..12c644dcfcce05f3896766be190bb23b131cbce7 100644 (file)
@@ -61,20 +61,8 @@ int (*sym_fido_dev_open)(fido_dev_t *, const char *) = NULL;
 const char* (*sym_fido_strerr)(int) = NULL;
 
 int dlopen_libfido2(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
-        int r;
-
-        if (libfido2_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libfido2.so.1", RTLD_LAZY);
-        if (!dl)
-                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "libfido2 support is not installed: %s", dlerror());
-
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_DEBUG,
+        return dlopen_many_sym_or_warn(
+                        &libfido2_dl, "libfido2.so.1", LOG_DEBUG,
                         DLSYM_ARG(fido_assert_allow_cred),
                         DLSYM_ARG(fido_assert_free),
                         DLSYM_ARG(fido_assert_hmac_secret_len),
@@ -118,15 +106,7 @@ int dlopen_libfido2(void) {
                         DLSYM_ARG(fido_dev_make_cred),
                         DLSYM_ARG(fido_dev_new),
                         DLSYM_ARG(fido_dev_open),
-                        DLSYM_ARG(fido_strerr),
-                        NULL);
-        if (r < 0)
-                return r;
-
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        libfido2_dl = TAKE_PTR(dl);
-        return 1;
+                        DLSYM_ARG(fido_strerr));
 }
 
 static int verify_features(
index d7776e7eff207e9f191e4f47fbc39b12cc356ddc..12efe626081e86b8ac8315e51bef9dc2503184d7 100644 (file)
@@ -24,20 +24,8 @@ int (*sym_pwquality_set_int_value)(pwquality_settings_t *pwq, int setting, int v
 const char* (*sym_pwquality_strerror)(char *buf, size_t len, int errcode, void *auxerror);
 
 int dlopen_pwquality(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
-        int r;
-
-        if (pwquality_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libpwquality.so.1", RTLD_LAZY);
-        if (!dl)
-                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "libpwquality support is not installed: %s", dlerror());
-
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_DEBUG,
+        return dlopen_many_sym_or_warn(
+                        &pwquality_dl, "libpwquality.so.1", LOG_DEBUG,
                         DLSYM_ARG(pwquality_check),
                         DLSYM_ARG(pwquality_default_settings),
                         DLSYM_ARG(pwquality_free_settings),
@@ -45,15 +33,7 @@ int dlopen_pwquality(void) {
                         DLSYM_ARG(pwquality_get_str_value),
                         DLSYM_ARG(pwquality_read_config),
                         DLSYM_ARG(pwquality_set_int_value),
-                        DLSYM_ARG(pwquality_strerror),
-                        NULL);
-        if (r < 0)
-                return r;
-
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        pwquality_dl = TAKE_PTR(dl);
-        return 1;
+                        DLSYM_ARG(pwquality_strerror));
 }
 
 void pwq_maybe_disable_dictionary(pwquality_settings_t *pwq) {
index 5345b7288bc8aa33bae876b3b6126e8bac8896ff..db48c73610504a456675d908e135c711617eb9d5 100644 (file)
@@ -5,9 +5,9 @@
 #if HAVE_QRENCODE
 #include <qrencode.h>
 
-#include "alloc-util.h"
 #include "dlfcn-util.h"
 #include "locale-util.h"
+#include "log.h"
 #include "terminal-util.h"
 
 #define ANSI_WHITE_ON_BLACK "\033[40;37;1m"
@@ -18,30 +18,10 @@ static QRcode* (*sym_QRcode_encodeString)(const char *string, int version, QRecL
 static void (*sym_QRcode_free)(QRcode *qrcode) = NULL;
 
 int dlopen_qrencode(void) {
-        _cleanup_(dlclosep) void *dl = NULL;
-        int r;
-
-        if (qrcode_dl)
-                return 0; /* Already loaded */
-
-        dl = dlopen("libqrencode.so.4", RTLD_LAZY);
-        if (!dl)
-                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                                       "libqrcode support is not installed: %s", dlerror());
-
-        r = dlsym_many_or_warn(
-                        dl,
-                        LOG_DEBUG,
+        return dlopen_many_sym_or_warn(
+                        &qrcode_dl, "libqrencode.so.4", LOG_DEBUG,
                         DLSYM_ARG(QRcode_encodeString),
-                        DLSYM_ARG(QRcode_free),
-                        NULL);
-        if (r < 0)
-                return r;
-
-        /* Note that we never release the reference here, because there's no real reason to, after all this
-         * was traditionally a regular shared library dependency which lives forever too. */
-        qrcode_dl = TAKE_PTR(dl);
-        return 1;
+                        DLSYM_ARG(QRcode_free));
 }
 
 static void print_border(FILE *output, unsigned width) {