From e44b5004840c80610a732e014ae8672277f16ee2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 11 Jun 2020 13:16:53 +0200 Subject: [PATCH] journalctl: make libqrencode a weak dependency This way journalctl can make use of libqrencode if it's there, but will quietly not use it if it isn't. This means libqrencode remains a build-time dep, but not a strict runtime dependency. I figure we should do something similar for a bunch of other "leaf" libraries we only use few symbols of. Specifically the following are probably good candidates: * pcre2 * libpwquality * p11kit * elfutils and possibly: * libcryptsetup (only in some parts. i.e. building systemd-cryptsetup without it makes no sense. However building the dissect option with libcryptsetup as optional dep does make sense) * possibly the compression libraries (at least the ones we never use for compression, but only as alternative ones for decompression) Already covered like this is: * libxkcommon --- meson.build | 2 +- src/journal/journal-qrcode.c | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/meson.build b/meson.build index 8f1d1b5897..3a86967938 100644 --- a/meson.build +++ b/meson.build @@ -1780,7 +1780,7 @@ public_programs += executable( include_directories : includes, link_with : [libshared], dependencies : [threads, - libqrencode, + libdl, libxz, liblz4, libpcre2, diff --git a/src/journal/journal-qrcode.c b/src/journal/journal-qrcode.c index 706d33c38a..dddbd7b381 100644 --- a/src/journal/journal-qrcode.c +++ b/src/journal/journal-qrcode.c @@ -7,6 +7,7 @@ #include #include "alloc-util.h" +#include "dlfcn-util.h" #include "fd-util.h" #include "fileio.h" #include "journal-qrcode.h" @@ -40,6 +41,9 @@ int print_qr_code( const char *hn, sd_id128_t machine) { + QRcode* (*sym_QRcode_encodeString)(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive); + void (*sym_QRcode_free)(QRcode *qrcode); + _cleanup_(dlclosep) void *dl = NULL; _cleanup_free_ char *url = NULL; _cleanup_fclose_ FILE *f = NULL; size_t url_size = 0, i; @@ -55,6 +59,20 @@ int print_qr_code( if (!is_locale_utf8() || !colors_enabled()) return -EOPNOTSUPP; + dl = dlopen("libqrencode.so.4", RTLD_LAZY); + if (!dl) + return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "QRCODE support is not installed: %s", dlerror()); + + r = dlsym_many_and_warn( + dl, + LOG_DEBUG, + &sym_QRcode_encodeString, "QRcode_encodeString", + &sym_QRcode_free, "QRcode_free", + NULL); + if (r < 0) + return r; + f = open_memstream_unlocked(&url, &url_size); if (!f) return -ENOMEM; @@ -81,7 +99,7 @@ int print_qr_code( f = safe_fclose(f); - qr = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1); + qr = sym_QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (!qr) return -ENOMEM; @@ -123,6 +141,6 @@ int print_qr_code( print_border(output, qr->width); - QRcode_free(qr); + sym_QRcode_free(qr); return 0; }