]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dlfcn: add new safe_dclose() helper
authorLennart Poettering <lennart@poettering.net>
Wed, 25 Jan 2023 10:54:44 +0000 (11:54 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 25 Jan 2023 14:07:35 +0000 (15:07 +0100)
Let's allow destructing loaded module handles in our usual way that is
fine with NULL handles, and also returns the NULL handle again.

src/shared/dlfcn-util.h
src/shared/tpm2-util.c

index f8a16433fceb01619a413cb461b34f16fe947f5a..ca632f4e1f4b8442aa620b5379c283d5c6c6675a 100644 (file)
@@ -24,3 +24,11 @@ int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, int log_l
 /* libbpf is a bit confused about type-safety and API compatibility. Provide a macro that can tape over that mess. Sad. */
 #define DLSYM_ARG_FORCE(arg) \
         &sym_##arg, STRINGIFY(arg)
+
+static inline void *safe_dlclose(void *p) {
+        if (!p)
+                return NULL;
+
+        assert_se(dlclose(p) == 0);
+        return NULL;
+}
index 3870f0401be73716c487a4707c33ac82aa0fe042..974b14439cf7c8c67b859b18759e39e0d2d058ff 100644 (file)
@@ -111,11 +111,7 @@ void tpm2_context_destroy(struct tpm2_context *c) {
                 sym_Esys_Finalize(&c->esys_context);
 
         c->tcti_context = mfree(c->tcti_context);
-
-        if (c->tcti_dl) {
-                dlclose(c->tcti_dl);
-                c->tcti_dl = NULL;
-        }
+        c->tcti_dl = safe_dlclose(c->tcti_dl);
 }
 
 static inline void Esys_Finalize_wrapper(ESYS_CONTEXT **c) {