]> git.proxmox.com Git - mirror_lxcfs.git/blob - src/cgroups/cgroup2_devices.h
tree-wide: fix dot_or_empty()
[mirror_lxcfs.git] / src / cgroups / cgroup2_devices.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 /* Parts of this taken from systemd's implementation. */
4
5 #ifndef __LXC_CGROUP2_DEVICES_H
6 #define __LXC_CGROUP2_DEVICES_H
7
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif
11
12 #ifndef FUSE_USE_VERSION
13 #define FUSE_USE_VERSION 26
14 #endif
15
16 #define _FILE_OFFSET_BITS 64
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <sys/stat.h>
24 #include <sys/syscall.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
29 #include <linux/bpf.h>
30 #include <linux/filter.h>
31 #endif
32
33 #if !HAVE_BPF
34 #if !(defined __NR_bpf && __NR_bpf > 0)
35 #if defined __NR_bpf
36 #undef __NR_bpf
37 #endif
38 #if defined __i386__
39 #define __NR_bpf 357
40 #elif defined __x86_64__
41 #define __NR_bpf 321
42 #elif defined __aarch64__
43 #define __NR_bpf 280
44 #elif defined __arm__
45 #define __NR_bpf 386
46 #elif defined __sparc__
47 #define __NR_bpf 349
48 #elif defined __s390__
49 #define __NR_bpf 351
50 #elif defined __tilegx__
51 #define __NR_bpf 280
52 #else
53 #warning "__NR_bpf not defined for your architecture"
54 #endif
55 #endif
56
57 union bpf_attr;
58
59 static inline int missing_bpf(int cmd, union bpf_attr *attr, size_t size)
60 {
61 #ifdef __NR_bpf
62 return (int)syscall(__NR_bpf, cmd, attr, size);
63 #else
64 errno = ENOSYS;
65 return -1;
66 #endif
67 }
68
69 #define bpf missing_bpf
70 #endif
71
72 struct bpf_program {
73 int device_list_type;
74 int kernel_fd;
75 uint32_t prog_type;
76
77 size_t n_instructions;
78 #ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
79 struct bpf_insn *instructions;
80 #endif
81
82 char *attached_path;
83 int attached_type;
84 uint32_t attached_flags;
85 };
86
87 #ifdef HAVE_STRUCT_BPF_CGROUP_DEV_CTX
88 struct bpf_program *bpf_program_new(uint32_t prog_type);
89 int bpf_program_init(struct bpf_program *prog);
90 int bpf_program_append_device(struct bpf_program *prog,
91 struct device_item *device);
92 int bpf_program_finalize(struct bpf_program *prog);
93 int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
94 const char *path, uint32_t flags);
95 int bpf_program_cgroup_detach(struct bpf_program *prog);
96 void bpf_program_free(struct bpf_program *prog);
97 bool bpf_devices_cgroup_supported(void);
98 static inline void __auto_bpf_program_free__(struct bpf_program **prog)
99 {
100 if (*prog) {
101 bpf_program_free(*prog);
102 *prog = NULL;
103 }
104 }
105 #else
106 static inline struct bpf_program *bpf_program_new(uint32_t prog_type)
107 {
108 errno = ENOSYS;
109 return NULL;
110 }
111
112 static inline int bpf_program_init(struct bpf_program *prog)
113 {
114 errno = ENOSYS;
115 return -1;
116 }
117
118 static inline int bpf_program_append_device(struct bpf_program *prog, char type,
119 int major, int minor,
120 const char *access, int allow)
121 {
122 errno = ENOSYS;
123 return -1;
124 }
125
126 static inline int bpf_program_finalize(struct bpf_program *prog)
127 {
128 errno = ENOSYS;
129 return -1;
130 }
131
132 static inline int bpf_program_cgroup_attach(struct bpf_program *prog, int type,
133 const char *path, uint32_t flags)
134 {
135 errno = ENOSYS;
136 return -1;
137 }
138
139 static inline int bpf_program_cgroup_detach(struct bpf_program *prog)
140 {
141 errno = ENOSYS;
142 return -1;
143 }
144
145 static inline void bpf_program_free(struct bpf_program *prog)
146 {
147 }
148
149
150 static inline bool bpf_devices_cgroup_supported(void)
151 {
152 return false;
153 }
154
155 static inline void __auto_bpf_program_free__(struct bpf_program **prog)
156 {
157 }
158
159 #endif
160
161 #define __do_bpf_program_free \
162 __attribute__((__cleanup__(__auto_bpf_program_free__)))
163
164 #endif /* __LXC_CGROUP2_DEVICES_H */