]>
Commit | Line | Data |
---|---|---|
5fbea8a6 CB |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #ifndef __LXC_CGROUP_H | |
4 | #define __LXC_CGROUP_H | |
5 | ||
d511b24d | 6 | #include "config.h" |
1f5596dd | 7 | |
d298bba1 | 8 | #include <errno.h> |
5fbea8a6 CB |
9 | #include <stdbool.h> |
10 | #include <stddef.h> | |
11 | #include <sys/types.h> | |
12 | ||
1f5596dd CB |
13 | #include "../macro.h" |
14 | #include "../memory_utils.h" | |
5fbea8a6 CB |
15 | |
16 | #define DEFAULT_CGROUP_MOUNTPOINT "/sys/fs/cgroup" | |
17 | ||
18 | typedef enum { | |
19 | CGROUP_LAYOUT_UNKNOWN = -1, | |
20 | CGROUP_LAYOUT_LEGACY = 0, | |
21 | CGROUP_LAYOUT_HYBRID = 1, | |
22 | CGROUP_LAYOUT_UNIFIED = 2, | |
23 | } cgroup_layout_t; | |
24 | ||
25 | /* A descriptor for a mounted hierarchy | |
26 | * | |
27 | * @controllers | |
28 | * - legacy hierarchy | |
29 | * Either NULL, or a null-terminated list of all the co-mounted controllers. | |
30 | * - unified hierarchy | |
31 | * Either NULL, or a null-terminated list of all enabled controllers. | |
32 | * | |
33 | * @mountpoint | |
34 | * - The mountpoint we will use. | |
35 | * - legacy hierarchy | |
36 | * It will be either /sys/fs/cgroup/controller or | |
37 | * /sys/fs/cgroup/controllerlist. | |
38 | * - unified hierarchy | |
39 | * It will either be /sys/fs/cgroup or /sys/fs/cgroup/<mountpoint-name> | |
40 | * depending on whether this is a hybrid cgroup layout (mix of legacy and | |
41 | * unified hierarchies) or a pure unified cgroup layout. | |
42 | * | |
43 | * @container_base_path | |
44 | * - The cgroup under which the container cgroup path | |
45 | * is created. This will be either the caller's cgroup (if not root), or | |
46 | * init's cgroup (if root). | |
47 | * | |
48 | * @container_full_path | |
49 | * - The full path to the containers cgroup. | |
50 | * | |
51 | * @monitor_full_path | |
52 | * - The full path to the monitor's cgroup. | |
53 | * | |
54 | * @version | |
55 | * - legacy hierarchy | |
56 | * If the hierarchy is a legacy hierarchy this will be set to | |
57 | * CGROUP_SUPER_MAGIC. | |
58 | * - unified hierarchy | |
59 | * If the hierarchy is a unified hierarchy this will be set to | |
60 | * CGROUP2_SUPER_MAGIC. | |
61 | */ | |
62 | struct hierarchy { | |
63 | /* | |
64 | * cgroup2 only: what files need to be chowned to delegate a cgroup to | |
65 | * an unprivileged user. | |
66 | */ | |
67 | char **controllers; | |
68 | char *__controllers; | |
69 | char *mountpoint; | |
0fd1b770 | 70 | char *base_path; |
5fbea8a6 CB |
71 | int version; |
72 | ||
73 | /* cgroup2 only */ | |
74 | unsigned int bpf_device_controller:1; | |
75 | int fd; | |
76 | }; | |
77 | ||
78 | struct cgroup_ops { | |
0646f250 CB |
79 | /* |
80 | * File descriptor of the mount namespace the cgroup hierarchies are | |
81 | * mounted in. | |
82 | */ | |
83 | int mntns_fd; | |
84 | ||
114eb8b8 CB |
85 | /* |
86 | * A file descriptor to the root of the cgroup tree if we're on a | |
87 | * cgroup2 only system. | |
88 | */ | |
89 | int cgroup2_root_fd; | |
90 | ||
5fbea8a6 CB |
91 | /* string constant */ |
92 | const char *driver; | |
93 | ||
94 | /* string constant */ | |
95 | const char *version; | |
96 | ||
5fbea8a6 CB |
97 | /* @hierarchies |
98 | * - A NULL-terminated array of struct hierarchy, one per legacy | |
99 | * hierarchy. No duplicates. First sufficient, writeable mounted | |
100 | * hierarchy wins. | |
101 | */ | |
102 | struct hierarchy **hierarchies; | |
103 | /* Pointer to the unified hierarchy. Do not free! */ | |
104 | struct hierarchy *unified; | |
105 | ||
106 | /* | |
107 | * @cgroup_layout | |
108 | * - What cgroup layout the container is running with. | |
109 | * - CGROUP_LAYOUT_UNKNOWN | |
110 | * The cgroup layout could not be determined. This should be treated | |
111 | * as an error condition. | |
112 | * - CGROUP_LAYOUT_LEGACY | |
113 | * The container is running with all controllers mounted into legacy | |
114 | * cgroup hierarchies. | |
115 | * - CGROUP_LAYOUT_HYBRID | |
116 | * The container is running with at least one controller mounted | |
117 | * into a legacy cgroup hierarchy and a mountpoint for the unified | |
118 | * hierarchy. The unified hierarchy can be empty (no controllers | |
119 | * enabled) or non-empty (controllers enabled). | |
120 | * - CGROUP_LAYOUT_UNIFIED | |
121 | * The container is running on a pure unified cgroup hierarchy. The | |
122 | * unified hierarchy can be empty (no controllers enabled) or | |
123 | * non-empty (controllers enabled). | |
124 | */ | |
125 | cgroup_layout_t cgroup_layout; | |
126 | ||
127 | int (*num_hierarchies)(struct cgroup_ops *ops); | |
128 | bool (*get_hierarchies)(struct cgroup_ops *ops, int n, char ***out); | |
129 | bool (*mount)(struct cgroup_ops *ops, const char *root); | |
5fbea8a6 CB |
130 | struct hierarchy *(*get_hierarchy)(struct cgroup_ops *ops, |
131 | const char *controller); | |
1ca6a467 CB |
132 | bool (*get)(struct cgroup_ops *ops, const char *controller, |
133 | const char *cgroup, const char *file, char **value); | |
66c5e848 CB |
134 | |
135 | /* memory */ | |
acff9786 | 136 | int (*get_memory_stats_fd)(struct cgroup_ops *ops, const char *cgroup); |
66c5e848 CB |
137 | int (*get_memory_stats)(struct cgroup_ops *ops, const char *cgroup, |
138 | char **value); | |
139 | int (*get_memory_current)(struct cgroup_ops *ops, const char *cgroup, | |
140 | char **value); | |
141 | int (*get_memory_swap_current)(struct cgroup_ops *ops, | |
142 | const char *cgroup, char **value); | |
143 | int (*get_memory_max)(struct cgroup_ops *ops, const char *cgroup, | |
144 | char **value); | |
4032b735 | 145 | int (*get_memory_swappiness)(struct cgroup_ops *ops, const char *cgroup, |
6cc153e6 | 146 | char **value); |
66c5e848 CB |
147 | int (*get_memory_swap_max)(struct cgroup_ops *ops, const char *cgroup, |
148 | char **value); | |
6cc153e6 FS |
149 | int (*get_memory_slabinfo_fd)(struct cgroup_ops *ops, |
150 | const char *cgroup); | |
c6805016 | 151 | bool (*can_use_swap)(struct cgroup_ops *ops); |
2c3bcd9e CB |
152 | |
153 | /* cpuset */ | |
154 | int (*get_cpuset_cpus)(struct cgroup_ops *ops, const char *cgroup, | |
155 | char **value); | |
77f4399a | 156 | bool (*can_use_cpuview)(struct cgroup_ops *ops); |
9a9484ab CB |
157 | |
158 | /* io */ | |
159 | int (*get_io_service_bytes)(struct cgroup_ops *ops, const char *cgroup, | |
160 | char **value); | |
161 | int (*get_io_service_time)(struct cgroup_ops *ops, const char *cgroup, | |
162 | char **value); | |
163 | int (*get_io_serviced)(struct cgroup_ops *ops, const char *cgroup, | |
164 | char **value); | |
165 | int (*get_io_merged)(struct cgroup_ops *ops, const char *cgroup, | |
166 | char **value); | |
167 | int (*get_io_wait_time)(struct cgroup_ops *ops, const char *cgroup, | |
168 | char **value); | |
5fbea8a6 CB |
169 | }; |
170 | ||
77f4399a CB |
171 | extern struct cgroup_ops *cgroup_ops; |
172 | ||
5fbea8a6 CB |
173 | extern struct cgroup_ops *cgroup_init(void); |
174 | extern void cgroup_exit(struct cgroup_ops *ops); | |
175 | ||
176 | extern void prune_init_scope(char *cg); | |
177 | ||
178 | static inline void __auto_cgroup_exit__(struct cgroup_ops **ops) | |
179 | { | |
180 | if (*ops) | |
181 | cgroup_exit(*ops); | |
182 | } | |
183 | ||
184 | extern int cgroup_attach(const char *name, const char *lxcpath, int64_t pid); | |
185 | ||
186 | #define __do_cgroup_exit __attribute__((__cleanup__(__auto_cgroup_exit__))) | |
187 | ||
188 | static inline bool pure_unified_layout(const struct cgroup_ops *ops) | |
189 | { | |
190 | return ops->cgroup_layout == CGROUP_LAYOUT_UNIFIED; | |
191 | } | |
192 | ||
193 | static inline bool is_unified_hierarchy(const struct hierarchy *h) | |
194 | { | |
195 | return h->version == CGROUP2_SUPER_MAGIC; | |
196 | } | |
197 | ||
66c5e848 CB |
198 | static inline bool is_unified_controller(int version) |
199 | { | |
200 | return version == CGROUP2_SUPER_MAGIC; | |
201 | } | |
202 | ||
d298bba1 CB |
203 | static inline int get_cgroup_fd(const char *controller) |
204 | { | |
205 | struct hierarchy *h; | |
206 | ||
207 | h = cgroup_ops->get_hierarchy(cgroup_ops, controller); | |
208 | return h ? h->fd : -EBADF; | |
209 | } | |
210 | ||
1f5596dd CB |
211 | extern char *get_pid_cgroup(pid_t pid, const char *contrl); |
212 | ||
213 | extern char *get_cpuset(const char *cg); | |
214 | ||
5fbea8a6 | 215 | #endif |