]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/bpf-link.c
mkosi: update fedora commit reference
[thirdparty/systemd.git] / src / shared / bpf-link.c
index f718cb7d2d39954f463dbd621a161e24da12d644..77f6a4ee9ac8bb484928655b3da686b73141c5e0 100644 (file)
@@ -1,38 +1,43 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include "bpf-dlopen.h"
 #include "bpf-link.h"
 #include "serialize.h"
 
-bool can_link_bpf_program(struct bpf_program *prog) {
+bool bpf_can_link_program(struct bpf_program *prog) {
         _cleanup_(bpf_link_freep) struct bpf_link *link = NULL;
 
         assert(prog);
 
+        if (dlopen_bpf() < 0)
+                return false;
+
         /* Pass invalid cgroup fd intentionally. */
-        link = bpf_program__attach_cgroup(prog, /*cgroup_fd=*/-1);
+        link = sym_bpf_program__attach_cgroup(prog, /*cgroup_fd=*/-1);
 
         /* EBADF indicates that bpf_link is supported by kernel. */
-        return libbpf_get_error(link) == -EBADF;
+        return bpf_get_error_translated(link) == -EBADF;
 }
 
-int serialize_bpf_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *link) {
-        int fd;
-
+int bpf_serialize_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *link) {
         assert(key);
 
         if (!link)
                 return -ENOENT;
 
-        if (libbpf_get_error(link) != 0)
+        if (bpf_get_error_translated(link) != 0)
                 return -EINVAL;
 
-        fd = bpf_link__fd(link);
-        return serialize_fd(f, fds, key, fd);
+        return serialize_fd(f, fds, key, sym_bpf_link__fd(link));
 }
 
 struct bpf_link *bpf_link_free(struct bpf_link *link) {
-        /* bpf_link__destroy handles link == NULL case */
-        (void) bpf_link__destroy(link);
+        /* If libbpf wasn't dlopen()ed, sym_bpf_link__destroy might be unresolved (NULL), so let's not try to
+         * call it if link is NULL. link might also be a non-null "error pointer", but such a value can only
+         * originate from a call to libbpf, but that means that libbpf is available, and we can let
+         * bpf_link__destroy() handle it. */
+        if (link)
+                (void) sym_bpf_link__destroy(link);
 
         return NULL;
 }