]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bpf-link.c
hwdb: updated Librem 11 accelerometer (#32772)
[thirdparty/systemd.git] / src / shared / bpf-link.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bpf-dlopen.h"
4 #include "bpf-link.h"
5 #include "serialize.h"
6
7 bool bpf_can_link_program(struct bpf_program *prog) {
8 _cleanup_(bpf_link_freep) struct bpf_link *link = NULL;
9
10 assert(prog);
11
12 if (dlopen_bpf() < 0)
13 return false;
14
15 /* Pass invalid cgroup fd intentionally. */
16 link = sym_bpf_program__attach_cgroup(prog, /*cgroup_fd=*/-1);
17
18 /* EBADF indicates that bpf_link is supported by kernel. */
19 return sym_libbpf_get_error(link) == -EBADF;
20 }
21
22 int bpf_serialize_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *link) {
23 assert(key);
24
25 if (!link)
26 return -ENOENT;
27
28 if (sym_libbpf_get_error(link) != 0)
29 return -EINVAL;
30
31 return serialize_fd(f, fds, key, sym_bpf_link__fd(link));
32 }
33
34 struct bpf_link *bpf_link_free(struct bpf_link *link) {
35 /* If libbpf wasn't dlopen()ed, sym_bpf_link__destroy might be unresolved (NULL), so let's not try to
36 * call it if link is NULL. link might also be a non-null "error pointer", but such a value can only
37 * originate from a call to libbpf, but that means that libbpf is available, and we can let
38 * bpf_link__destroy() handle it. */
39 if (link)
40 (void) sym_bpf_link__destroy(link);
41
42 return NULL;
43 }