]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bpf-link.c
Merge pull request #19854 from poettering/journal-enum-uniq-fix
[thirdparty/systemd.git] / src / shared / bpf-link.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
36 /* Avoid a useless dlopen() if link == NULL */
37 if (!link)
38 return NULL;
39
40 (void) sym_bpf_link__destroy(link);
41
42 return NULL;
43 }