]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/pcre2-dlopen.c
journalctl: make pcre2 a dlopen() dependency
[thirdparty/systemd.git] / src / journal / pcre2-dlopen.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "dlfcn-util.h"
5 #include "pcre2-dlopen.h"
6
7 #if HAVE_PCRE2
8 static void *pcre2_dl = NULL;
9
10 pcre2_match_data* (*sym_pcre2_match_data_create)(uint32_t, pcre2_general_context *);
11 void (*sym_pcre2_match_data_free)(pcre2_match_data *);
12 void (*sym_pcre2_code_free)(pcre2_code *);
13 pcre2_code* (*sym_pcre2_compile)(PCRE2_SPTR, PCRE2_SIZE, uint32_t, int *, PCRE2_SIZE *, pcre2_compile_context *);
14 int (*sym_pcre2_get_error_message)(int, PCRE2_UCHAR *, PCRE2_SIZE);
15 int (*sym_pcre2_match)(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, uint32_t, pcre2_match_data *, pcre2_match_context *);
16 PCRE2_SIZE* (*sym_pcre2_get_ovector_pointer)(pcre2_match_data *);
17
18 int dlopen_pcre2(void) {
19 _cleanup_(dlclosep) void *dl = NULL;
20 int r;
21
22 if (pcre2_dl)
23 return 0; /* Already loaded */
24
25 dl = dlopen("libpcre2-8.so.0", RTLD_LAZY);
26 if (!dl)
27 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
28 "PCRE2 support is not installed: %s", dlerror());
29
30 r = dlsym_many_and_warn(
31 dl,
32 LOG_ERR,
33 &sym_pcre2_match_data_create, "pcre2_match_data_create_8",
34 &sym_pcre2_match_data_free, "pcre2_match_data_free_8",
35 &sym_pcre2_code_free, "pcre2_code_free_8",
36 &sym_pcre2_compile, "pcre2_compile_8",
37 &sym_pcre2_get_error_message, "pcre2_get_error_message_8",
38 &sym_pcre2_match, "pcre2_match_8",
39 &sym_pcre2_get_ovector_pointer, "pcre2_get_ovector_pointer_8",
40 NULL);
41 if (r < 0)
42 return r;
43
44 /* Note that we never release the reference here, because there's no real reason to, after all this
45 * was traditionally a regular shared library dependency which lives forever too. */
46 pcre2_dl = TAKE_PTR(dl);
47
48 return 1;
49 }
50
51 #else
52
53 int dlopen_pcre2(void) {
54 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
55 "PCRE2 support is not compiled in.");
56 }
57 #endif