]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cgroup-util.h
068df102f771efd65e21054e639cf8c493301da9
[thirdparty/systemd.git] / src / basic / cgroup-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <dirent.h>
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <sys/statfs.h>
28 #include <sys/types.h>
29
30 #include "def.h"
31 #include "hashmap.h"
32 #include "macro.h"
33 #include "set.h"
34
35 #define SYSTEMD_CGROUP_CONTROLLER_LEGACY "name=systemd"
36 #define SYSTEMD_CGROUP_CONTROLLER_HYBRID "name=unified"
37 #define SYSTEMD_CGROUP_CONTROLLER "_systemd"
38
39 /* An enum of well known cgroup controllers */
40 typedef enum CGroupController {
41 CGROUP_CONTROLLER_CPU,
42 CGROUP_CONTROLLER_CPUACCT, /* v1 only */
43 CGROUP_CONTROLLER_IO, /* v2 only */
44 CGROUP_CONTROLLER_BLKIO, /* v1 only */
45 CGROUP_CONTROLLER_MEMORY,
46 CGROUP_CONTROLLER_DEVICES, /* v1 only */
47 CGROUP_CONTROLLER_PIDS,
48 _CGROUP_CONTROLLER_MAX,
49 _CGROUP_CONTROLLER_INVALID = -1,
50 } CGroupController;
51
52 #define CGROUP_CONTROLLER_TO_MASK(c) (1 << (c))
53
54 /* A bit mask of well known cgroup controllers */
55 typedef enum CGroupMask {
56 CGROUP_MASK_CPU = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPU),
57 CGROUP_MASK_CPUACCT = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPUACCT),
58 CGROUP_MASK_IO = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_IO),
59 CGROUP_MASK_BLKIO = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BLKIO),
60 CGROUP_MASK_MEMORY = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_MEMORY),
61 CGROUP_MASK_DEVICES = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_DEVICES),
62 CGROUP_MASK_PIDS = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_PIDS),
63 _CGROUP_MASK_ALL = CGROUP_CONTROLLER_TO_MASK(_CGROUP_CONTROLLER_MAX) - 1
64 } CGroupMask;
65
66 /* Special values for all weight knobs on unified hierarchy */
67 #define CGROUP_WEIGHT_INVALID ((uint64_t) -1)
68 #define CGROUP_WEIGHT_MIN UINT64_C(1)
69 #define CGROUP_WEIGHT_MAX UINT64_C(10000)
70 #define CGROUP_WEIGHT_DEFAULT UINT64_C(100)
71
72 #define CGROUP_LIMIT_MIN UINT64_C(0)
73 #define CGROUP_LIMIT_MAX ((uint64_t) -1)
74
75 static inline bool CGROUP_WEIGHT_IS_OK(uint64_t x) {
76 return
77 x == CGROUP_WEIGHT_INVALID ||
78 (x >= CGROUP_WEIGHT_MIN && x <= CGROUP_WEIGHT_MAX);
79 }
80
81 /* IO limits on unified hierarchy */
82 typedef enum CGroupIOLimitType {
83 CGROUP_IO_RBPS_MAX,
84 CGROUP_IO_WBPS_MAX,
85 CGROUP_IO_RIOPS_MAX,
86 CGROUP_IO_WIOPS_MAX,
87
88 _CGROUP_IO_LIMIT_TYPE_MAX,
89 _CGROUP_IO_LIMIT_TYPE_INVALID = -1
90 } CGroupIOLimitType;
91
92 extern const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX];
93
94 const char* cgroup_io_limit_type_to_string(CGroupIOLimitType t) _const_;
95 CGroupIOLimitType cgroup_io_limit_type_from_string(const char *s) _pure_;
96
97 /* Special values for the cpu.shares attribute */
98 #define CGROUP_CPU_SHARES_INVALID ((uint64_t) -1)
99 #define CGROUP_CPU_SHARES_MIN UINT64_C(2)
100 #define CGROUP_CPU_SHARES_MAX UINT64_C(262144)
101 #define CGROUP_CPU_SHARES_DEFAULT UINT64_C(1024)
102
103 static inline bool CGROUP_CPU_SHARES_IS_OK(uint64_t x) {
104 return
105 x == CGROUP_CPU_SHARES_INVALID ||
106 (x >= CGROUP_CPU_SHARES_MIN && x <= CGROUP_CPU_SHARES_MAX);
107 }
108
109 /* Special values for the blkio.weight attribute */
110 #define CGROUP_BLKIO_WEIGHT_INVALID ((uint64_t) -1)
111 #define CGROUP_BLKIO_WEIGHT_MIN UINT64_C(10)
112 #define CGROUP_BLKIO_WEIGHT_MAX UINT64_C(1000)
113 #define CGROUP_BLKIO_WEIGHT_DEFAULT UINT64_C(500)
114
115 static inline bool CGROUP_BLKIO_WEIGHT_IS_OK(uint64_t x) {
116 return
117 x == CGROUP_BLKIO_WEIGHT_INVALID ||
118 (x >= CGROUP_BLKIO_WEIGHT_MIN && x <= CGROUP_BLKIO_WEIGHT_MAX);
119 }
120
121 /* Default resource limits */
122 #define DEFAULT_TASKS_MAX_PERCENTAGE 15U /* 15% of PIDs, 4915 on default settings */
123 #define DEFAULT_USER_TASKS_MAX_PERCENTAGE 33U /* 33% of PIDs, 10813 on default settings */
124
125 typedef enum CGroupUnified {
126 CGROUP_UNIFIED_UNKNOWN = -1,
127 CGROUP_UNIFIED_NONE = 0, /* Both systemd and controllers on legacy */
128 CGROUP_UNIFIED_SYSTEMD = 1, /* Only systemd on unified */
129 CGROUP_UNIFIED_ALL = 2, /* Both systemd and controllers on unified */
130 } CGroupUnified;
131
132 /*
133 * General rules:
134 *
135 * We accept named hierarchies in the syntax "foo" and "name=foo".
136 *
137 * We expect that named hierarchies do not conflict in name with a
138 * kernel hierarchy, modulo the "name=" prefix.
139 *
140 * We always generate "normalized" controller names, i.e. without the
141 * "name=" prefix.
142 *
143 * We require absolute cgroup paths. When returning, we will always
144 * generate paths with multiple adjacent / removed.
145 */
146
147 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f);
148 int cg_read_pid(FILE *f, pid_t *_pid);
149 int cg_read_event(const char *controller, const char *path, const char *event,
150 char **val);
151
152 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d);
153 int cg_read_subgroup(DIR *d, char **fn);
154
155 typedef enum CGroupFlags {
156 CGROUP_SIGCONT = 1,
157 CGROUP_IGNORE_SELF = 2,
158 CGROUP_REMOVE = 4,
159 } CGroupFlags;
160
161 typedef void (*cg_kill_log_func_t)(pid_t pid, int sig, void *userdata);
162
163 int cg_kill(const char *controller, const char *path, int sig, CGroupFlags flags, Set *s, cg_kill_log_func_t kill_log, void *userdata);
164 int cg_kill_recursive(const char *controller, const char *path, int sig, CGroupFlags flags, Set *s, cg_kill_log_func_t kill_log, void *userdata);
165
166 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
167 int cg_migrate_recursive(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
168 int cg_migrate_recursive_fallback(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
169
170 int cg_split_spec(const char *spec, char **controller, char **path);
171 int cg_mangle_path(const char *path, char **result);
172
173 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs);
174 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs);
175
176 int cg_pid_get_path(const char *controller, pid_t pid, char **path);
177
178 int cg_trim(const char *controller, const char *path, bool delete_root);
179
180 int cg_rmdir(const char *controller, const char *path);
181
182 int cg_create(const char *controller, const char *path);
183 int cg_attach(const char *controller, const char *path, pid_t pid);
184 int cg_attach_fallback(const char *controller, const char *path, pid_t pid);
185 int cg_create_and_attach(const char *controller, const char *path, pid_t pid);
186
187 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value);
188 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
189 int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, char **keys, char **values);
190
191 int cg_set_access(const char *controller, const char *path, uid_t uid, gid_t gid);
192
193 int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags);
194 int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size);
195
196 int cg_install_release_agent(const char *controller, const char *agent);
197 int cg_uninstall_release_agent(const char *controller);
198
199 int cg_is_empty(const char *controller, const char *path);
200 int cg_is_empty_recursive(const char *controller, const char *path);
201
202 int cg_get_root_path(char **path);
203
204 int cg_path_get_session(const char *path, char **session);
205 int cg_path_get_owner_uid(const char *path, uid_t *uid);
206 int cg_path_get_unit(const char *path, char **unit);
207 int cg_path_get_user_unit(const char *path, char **unit);
208 int cg_path_get_machine_name(const char *path, char **machine);
209 int cg_path_get_slice(const char *path, char **slice);
210 int cg_path_get_user_slice(const char *path, char **slice);
211
212 int cg_shift_path(const char *cgroup, const char *cached_root, const char **shifted);
213 int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **cgroup);
214
215 int cg_pid_get_session(pid_t pid, char **session);
216 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
217 int cg_pid_get_unit(pid_t pid, char **unit);
218 int cg_pid_get_user_unit(pid_t pid, char **unit);
219 int cg_pid_get_machine_name(pid_t pid, char **machine);
220 int cg_pid_get_slice(pid_t pid, char **slice);
221 int cg_pid_get_user_slice(pid_t pid, char **slice);
222
223 int cg_path_decode_unit(const char *cgroup, char **unit);
224
225 char *cg_escape(const char *p);
226 char *cg_unescape(const char *p) _pure_;
227
228 bool cg_controller_is_valid(const char *p);
229
230 int cg_slice_to_path(const char *unit, char **ret);
231
232 typedef const char* (*cg_migrate_callback_t)(CGroupMask mask, void *userdata);
233
234 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path);
235 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t callback, void *userdata);
236 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t callback, void *userdata);
237 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t callback, void *userdata);
238 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root);
239 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p);
240
241 int cg_mask_supported(CGroupMask *ret);
242 int cg_mask_from_string(const char *s, CGroupMask *ret);
243 int cg_mask_to_string(CGroupMask mask, char **ret);
244
245 int cg_kernel_controllers(Set **controllers);
246
247 bool cg_ns_supported(void);
248
249 int cg_all_unified(void);
250 int cg_hybrid_unified(void);
251 int cg_unified_controller(const char *controller);
252 int cg_unified_flush(void);
253
254 bool cg_is_unified_wanted(void);
255 bool cg_is_legacy_wanted(void);
256 bool cg_is_hybrid_wanted(void);
257
258 const char* cgroup_controller_to_string(CGroupController c) _const_;
259 CGroupController cgroup_controller_from_string(const char *s) _pure_;
260
261 int cg_weight_parse(const char *s, uint64_t *ret);
262 int cg_cpu_shares_parse(const char *s, uint64_t *ret);
263 int cg_blkio_weight_parse(const char *s, uint64_t *ret);
264
265 bool is_cgroup_fs(const struct statfs *s);
266 bool fd_is_cgroup_fs(int fd);