core: replace cgroup_bpf_supported() with dlopen_bpf_full()

After 3988e2489a, the function is a simple
wrapper of bpf_dlopen() with logging. Let's introduce dlopen_bpf_full()
that takes log level, and replace cgroup_bpf_supported() with it.
This commit is contained in:
Yu Watanabe
2025-05-10 00:17:52 +09:00
parent e06e34a3b6
commit 4d2b9abbe1
8 changed files with 24 additions and 51 deletions
+1 -2
View File
@@ -27,7 +27,6 @@
/* libbpf, clang and llc compile time dependencies are satisfied */
#include "bpf-dlopen.h"
#include "bpf-link.h"
#include "bpf-util.h"
#include "bpf/restrict_fs/restrict-fs-skel.h"
#define CGROUP_HASH_SIZE_MAX 2048
@@ -102,7 +101,7 @@ bool bpf_restrict_fs_supported(bool initialize) {
if (!initialize)
return false;
if (!cgroup_bpf_supported())
if (dlopen_bpf_full(LOG_WARNING) < 0)
return (supported = false);
r = lsm_supported("bpf");
+1 -2
View File
@@ -10,7 +10,6 @@
#include "bpf-dlopen.h"
#include "bpf-link.h"
#include "bpf-util.h"
#include "bpf/restrict_ifaces/restrict-ifaces-skel.h"
static struct restrict_ifaces_bpf *restrict_ifaces_bpf_free(struct restrict_ifaces_bpf *obj) {
@@ -81,7 +80,7 @@ int bpf_restrict_ifaces_supported(void) {
if (supported >= 0)
return supported;
if (!cgroup_bpf_supported())
if (dlopen_bpf_full(LOG_WARNING) < 0)
return (supported = false);
if (!compat_libbpf_probe_bpf_prog_type(BPF_PROG_TYPE_CGROUP_SKB, /*opts=*/NULL)) {
+1 -2
View File
@@ -12,7 +12,6 @@
/* libbpf, clang, llvm and bpftool compile time dependencies are satisfied */
#include "bpf-dlopen.h"
#include "bpf-link.h"
#include "bpf-util.h"
#include "bpf/socket_bind/socket-bind-api.bpf.h"
#include "bpf/socket_bind/socket-bind-skel.h"
@@ -127,7 +126,7 @@ int bpf_socket_bind_supported(void) {
_cleanup_(socket_bind_bpf_freep) struct socket_bind_bpf *obj = NULL;
int r;
if (!cgroup_bpf_supported())
if (dlopen_bpf_full(LOG_WARNING) < 0)
return false;
if (!compat_libbpf_probe_bpf_prog_type(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, /*opts=*/NULL)) {
-24
View File
@@ -1,24 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "bpf-dlopen.h"
#include "bpf-util.h"
#include "cgroup-util.h"
#include "initrd-util.h"
#include "log.h"
bool cgroup_bpf_supported(void) {
static int supported = -1;
int r;
if (supported >= 0)
return supported;
r = dlopen_bpf();
if (r < 0) {
log_full_errno(in_initrd() ? LOG_DEBUG : LOG_INFO,
r, "Failed to open libbpf, cgroup BPF features disabled: %m");
return (supported = false);
}
return (supported = true);
}
-5
View File
@@ -1,5 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdbool.h>
bool cgroup_bpf_supported(void);
-6
View File
@@ -65,12 +65,6 @@ libcore_sources = files(
'varlink.c',
)
if conf.get('BPF_FRAMEWORK') == 1
libcore_sources += files(
'bpf-util.c',
)
endif
subdir('bpf/socket_bind')
subdir('bpf/restrict_fs')
subdir('bpf/restrict_ifaces')
+14 -9
View File
@@ -72,10 +72,14 @@ static int bpf_print_func(enum libbpf_print_level level, const char *fmt, va_lis
return log_internalv(LOG_DEBUG, errno, NULL, 0, NULL, fmt, ap);
}
int dlopen_bpf(void) {
int dlopen_bpf_full(int log_level) {
static int cached = 0;
void *dl;
int r;
if (cached != 0)
return cached;
ELF_NOTE_DLOPEN("bpf",
"Support firewalling and sandboxing with BPF",
ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
@@ -91,8 +95,9 @@ int dlopen_bpf(void) {
* list for both files, and when we assume 1.0+ is present we can remove this dlopen */
dl = dlopen("libbpf.so.0", RTLD_NOW|RTLD_NODELETE);
if (!dl)
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"neither libbpf.so.1 nor libbpf.so.0 are installed: %s", dlerror());
return cached = log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
"Neither libbpf.so.1 nor libbpf.so.0 are installed, cgroup BPF features disabled: %s",
dlerror());
log_debug("Loaded 'libbpf.so.0' via dlopen()");
@@ -129,7 +134,7 @@ int dlopen_bpf(void) {
);
}
if (r < 0)
return r;
return cached = log_full_errno(log_level, r, "Failed to load libbpf symbols, cgroup BPF features disabled: %m");
r = dlsym_many_or_warn(
dl, LOG_DEBUG,
@@ -171,14 +176,14 @@ int dlopen_bpf(void) {
DLSYM_ARG(ring_buffer__new),
DLSYM_ARG(ring_buffer__poll));
if (r < 0)
return r;
return cached = log_full_errno(log_level, r, "Failed to load libbpf symbols, cgroup BPF features disabled: %m");
/* We set the print helper unconditionally. Otherwise libbpf will emit not useful log messages. */
(void) sym_libbpf_set_print(bpf_print_func);
REENABLE_WARNING;
return r;
return cached = true;
}
int bpf_get_error_translated(const void *ptr) {
@@ -200,8 +205,8 @@ int bpf_get_error_translated(const void *ptr) {
#else
int dlopen_bpf(void) {
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"libbpf support is not compiled in.");
int dlopen_bpf_full(int log_level) {
return log_once_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
"libbpf support is not compiled in, cgroup BPF features disabled.");
}
#endif
+7 -1
View File
@@ -1,6 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdbool.h>
#include <syslog.h>
#if HAVE_LIBBPF
#include <bpf/bpf.h>
@@ -48,4 +51,7 @@ int bpf_get_error_translated(const void *ptr);
#endif
int dlopen_bpf(void);
int dlopen_bpf_full(int log_level);
static inline int dlopen_bpf(void) {
return dlopen_bpf_full(LOG_DEBUG);
}