apparmor: move dlopen() into mac_apparmor_use() check

This mirrors what we do for mac_selinux_use(), which also loads
libselinux.
This commit is contained in:
Lennart Poettering
2025-11-20 14:19:56 +01:00
parent 8137c6bf2d
commit c3b3eea2e5
3 changed files with 23 additions and 19 deletions
+21 -6
View File
@@ -5,6 +5,7 @@
#include "alloc-util.h"
#include "apparmor-util.h"
#include "fileio.h"
#include "log.h"
#include "parse-util.h"
#if HAVE_APPARMOR
@@ -42,14 +43,28 @@ int dlopen_libapparmor(void) {
bool mac_apparmor_use(void) {
static int cached_use = -1;
int r;
if (cached_use < 0) {
_cleanup_free_ char *p = NULL;
if (cached_use >= 0)
return cached_use;
cached_use =
read_one_line_file("/sys/module/apparmor/parameters/enabled", &p) >= 0 &&
parse_boolean(p) > 0;
_cleanup_free_ char *p = NULL;
r = read_one_line_file("/sys/module/apparmor/parameters/enabled", &p);
if (r < 0) {
if (r != -ENOENT)
log_debug_errno(r, "Failed to read /sys/module/apparmor/parameters/enabled, assuming AppArmor is not available: %m");
return (cached_use = false);
}
return cached_use;
r = parse_boolean(p);
if (r <= 0) {
if (r < 0)
log_debug_errno(r, "Failed to parse /sys/module/apparmor/parameters/enabled, assuming AppArmor is not available: %m");
return (cached_use = false);
}
if (dlopen_libapparmor() < 0)
return (cached_use = false);
return (cached_use = true);
}