journal,homectl: unify implementations of libqrencode loading and fss key printing

We had two of each: both homectl and journalctl had the whole dlopen()
wrapper, and journalctl had two implementations (slightly different) of the
code to print the fss:// pattern.

print_qrcode() now returns -EOPNOTSUPP when compiled with qrcode support. Both
callers ignore the return value, so this changes nothing.

No functional change.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-10-27 18:33:29 +01:00
parent c9e9a57fce
commit f1b823596f
8 changed files with 123 additions and 185 deletions
+2 -7
View File
@@ -195,6 +195,8 @@ shared_sources = files('''
ptyfwd.h
pwquality-util.c
pwquality-util.h
qrcode-util.c
qrcode-util.h
reboot-util.c
reboot-util.h
resize-fs.c
@@ -304,13 +306,6 @@ if conf.get('HAVE_PAM') == 1
'''.split())
endif
if conf.get('HAVE_QRENCODE') == 1
shared_sources += files('''
qrcode-util.c
qrcode-util.h
'''.split())
endif
generate_ip_protocol_list = find_program('generate-ip-protocol-list.sh')
ip_protocol_list_txt = custom_target(
'ip-protocol-list.txt',
+49 -1
View File
@@ -1,4 +1,10 @@
#include "qrcode-util.h"
#if HAVE_QRENCODE
#include <qrencode.h>
#include "dlfcn-util.h"
#include "locale-util.h"
#include "terminal-util.h"
#define ANSI_WHITE_ON_BLACK "\033[40;37;1m"
@@ -15,7 +21,7 @@ static void print_border(FILE *output, unsigned width) {
}
}
void write_qrcode(FILE *output, QRcode *qr) {
static void write_qrcode(FILE *output, QRcode *qr) {
assert(qr);
if (!output)
@@ -55,3 +61,45 @@ void write_qrcode(FILE *output, QRcode *qr) {
print_border(output, qr->width);
fflush(output);
}
int print_qrcode(FILE *out, const char *header, const char *string) {
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;
QRcode* qr;
int r;
/* If this is not an UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
* codes */
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;
qr = sym_QRcode_encodeString(string, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
if (!qr)
return -ENOMEM;
if (header)
fprintf(out, "\n%s:\n\n", header);
write_qrcode(out, qr);
fputc('\n', out);
sym_QRcode_free(qr);
return 0;
}
#endif
+8 -4
View File
@@ -1,9 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include <stdio.h>
#include <errno.h>
#if HAVE_QRENCODE
#include <qrencode.h>
#include <stdio.h>
void write_qrcode(FILE *output, QRcode *qr);
int print_qrcode(FILE *out, const char *header, const char *string);
#else
static inline int print_qrcode(FILE *out, const char *header, const char *string) {
return -EOPNOTSUPP;
}
#endif