]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bpf-link.c
Merge pull request #19391 from poettering/dissect-grow
[thirdparty/systemd.git] / src / shared / bpf-link.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "bpf-link.h"
4 #include "serialize.h"
5
6 bool can_link_bpf_program(struct bpf_program *prog) {
7 _cleanup_(bpf_link_freep) struct bpf_link *link = NULL;
8
9 assert(prog);
10
11 /* Pass invalid cgroup fd intentionally. */
12 link = bpf_program__attach_cgroup(prog, /*cgroup_fd=*/-1);
13
14 /* EBADF indicates that bpf_link is supported by kernel. */
15 return libbpf_get_error(link) == -EBADF;
16 }
17
18 int serialize_bpf_link(FILE *f, FDSet *fds, const char *key, struct bpf_link *link) {
19 int fd;
20
21 assert(key);
22
23 if (!link)
24 return -ENOENT;
25
26 if (libbpf_get_error(link) != 0)
27 return -EINVAL;
28
29 fd = bpf_link__fd(link);
30 return serialize_fd(f, fds, key, fd);
31 }
32
33 struct bpf_link *bpf_link_free(struct bpf_link *link) {
34 /* bpf_link__destroy handles link == NULL case */
35 (void) bpf_link__destroy(link);
36
37 return NULL;
38 }