]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8c6db833 LP |
2 | |
3 | #include <errno.h> | |
84ac7bea | 4 | #include <ftw.h> |
11c3a366 | 5 | #include <limits.h> |
8c6db833 | 6 | #include <signal.h> |
11c3a366 | 7 | #include <stddef.h> |
8c6db833 | 8 | #include <stdlib.h> |
672c48cc | 9 | #include <sys/types.h> |
f98c2585 | 10 | #include <sys/utsname.h> |
4b58153d | 11 | #include <sys/xattr.h> |
84ac7bea | 12 | #include <unistd.h> |
8c6db833 | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
3ffd4af2 | 15 | #include "cgroup-util.h" |
93cc7779 | 16 | #include "def.h" |
a0956174 | 17 | #include "dirent-util.h" |
84ac7bea | 18 | #include "extract-word.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
84ac7bea | 20 | #include "fileio.h" |
f97b34a6 | 21 | #include "format-util.h" |
f4f15635 | 22 | #include "fs-util.h" |
93cc7779 | 23 | #include "log.h" |
84ac7bea LP |
24 | #include "login-util.h" |
25 | #include "macro.h" | |
f5947a5e | 26 | #include "missing_magic.h" |
84ac7bea | 27 | #include "mkdir.h" |
6bedfcbb | 28 | #include "parse-util.h" |
9eb977db | 29 | #include "path-util.h" |
84ac7bea LP |
30 | #include "process-util.h" |
31 | #include "set.h" | |
9444b1f2 | 32 | #include "special.h" |
872a590e | 33 | #include "stat-util.h" |
d054f0a4 | 34 | #include "stdio-util.h" |
8b43440b | 35 | #include "string-table.h" |
07630cea | 36 | #include "string-util.h" |
aae7e17f | 37 | #include "strv.h" |
84ac7bea | 38 | #include "unit-name.h" |
b1d4f8e1 | 39 | #include "user-util.h" |
baa358df | 40 | #include "xattr-util.h" |
8c6db833 | 41 | |
e48fcfef | 42 | static int cg_enumerate_items(const char *controller, const char *path, FILE **_f, const char *item) { |
7027ff61 | 43 | _cleanup_free_ char *fs = NULL; |
c6c18be3 | 44 | FILE *f; |
7027ff61 | 45 | int r; |
c6c18be3 | 46 | |
c6c18be3 LP |
47 | assert(_f); |
48 | ||
e48fcfef | 49 | r = cg_get_path(controller, path, item, &fs); |
c3175a7f | 50 | if (r < 0) |
c6c18be3 LP |
51 | return r; |
52 | ||
53 | f = fopen(fs, "re"); | |
c6c18be3 LP |
54 | if (!f) |
55 | return -errno; | |
56 | ||
57 | *_f = f; | |
58 | return 0; | |
59 | } | |
60 | ||
e48fcfef TM |
61 | int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { |
62 | return cg_enumerate_items(controller, path, _f, "cgroup.procs"); | |
63 | } | |
64 | ||
c6c18be3 LP |
65 | int cg_read_pid(FILE *f, pid_t *_pid) { |
66 | unsigned long ul; | |
67 | ||
68 | /* Note that the cgroup.procs might contain duplicates! See | |
69 | * cgroups.txt for details. */ | |
70 | ||
7027ff61 LP |
71 | assert(f); |
72 | assert(_pid); | |
73 | ||
c6c18be3 LP |
74 | errno = 0; |
75 | if (fscanf(f, "%lu", &ul) != 1) { | |
76 | ||
77 | if (feof(f)) | |
78 | return 0; | |
79 | ||
66855de7 | 80 | return errno_or_else(EIO); |
c6c18be3 LP |
81 | } |
82 | ||
83 | if (ul <= 0) | |
84 | return -EIO; | |
85 | ||
86 | *_pid = (pid_t) ul; | |
87 | return 1; | |
88 | } | |
89 | ||
8b238b13 LP |
90 | int cg_read_event( |
91 | const char *controller, | |
92 | const char *path, | |
93 | const char *event, | |
31a9be23 | 94 | char **ret) { |
8b238b13 | 95 | |
ab2c3861 | 96 | _cleanup_free_ char *events = NULL, *content = NULL; |
ab2c3861 TH |
97 | int r; |
98 | ||
99 | r = cg_get_path(controller, path, "cgroup.events", &events); | |
100 | if (r < 0) | |
101 | return r; | |
102 | ||
627055ce | 103 | r = read_full_virtual_file(events, &content, NULL); |
ab2c3861 TH |
104 | if (r < 0) |
105 | return r; | |
106 | ||
31a9be23 YW |
107 | for (const char *p = content;;) { |
108 | _cleanup_free_ char *line = NULL, *key = NULL, *val = NULL; | |
109 | const char *q; | |
110 | ||
111 | r = extract_first_word(&p, &line, "\n", 0); | |
112 | if (r < 0) | |
113 | return r; | |
114 | if (r == 0) | |
115 | return -ENOENT; | |
116 | ||
117 | q = line; | |
118 | r = extract_first_word(&q, &key, " ", 0); | |
119 | if (r < 0) | |
120 | return r; | |
121 | if (r == 0) | |
ab2c3861 TH |
122 | return -EINVAL; |
123 | ||
31a9be23 | 124 | if (!streq(key, event)) |
ab2c3861 TH |
125 | continue; |
126 | ||
31a9be23 YW |
127 | val = strdup(q); |
128 | if (!val) | |
129 | return -ENOMEM; | |
130 | ||
131 | *ret = TAKE_PTR(val); | |
ab2c3861 TH |
132 | return 0; |
133 | } | |
ab2c3861 TH |
134 | } |
135 | ||
3228995c CB |
136 | bool cg_ns_supported(void) { |
137 | static thread_local int enabled = -1; | |
138 | ||
139 | if (enabled >= 0) | |
140 | return enabled; | |
141 | ||
0887fa71 LP |
142 | if (access("/proc/self/ns/cgroup", F_OK) < 0) { |
143 | if (errno != ENOENT) | |
144 | log_debug_errno(errno, "Failed to check whether /proc/self/ns/cgroup is available, assuming not: %m"); | |
145 | enabled = false; | |
146 | } else | |
147 | enabled = true; | |
3228995c CB |
148 | |
149 | return enabled; | |
150 | } | |
151 | ||
d9e45bc3 MS |
152 | bool cg_freezer_supported(void) { |
153 | static thread_local int supported = -1; | |
154 | ||
155 | if (supported >= 0) | |
156 | return supported; | |
157 | ||
158 | supported = cg_all_unified() > 0 && access("/sys/fs/cgroup/init.scope/cgroup.freeze", F_OK) == 0; | |
159 | ||
160 | return supported; | |
161 | } | |
162 | ||
35d2e7ec | 163 | int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) { |
7027ff61 | 164 | _cleanup_free_ char *fs = NULL; |
35d2e7ec LP |
165 | int r; |
166 | DIR *d; | |
167 | ||
35d2e7ec LP |
168 | assert(_d); |
169 | ||
170 | /* This is not recursive! */ | |
171 | ||
c3175a7f LP |
172 | r = cg_get_path(controller, path, NULL, &fs); |
173 | if (r < 0) | |
35d2e7ec LP |
174 | return r; |
175 | ||
176 | d = opendir(fs); | |
35d2e7ec LP |
177 | if (!d) |
178 | return -errno; | |
179 | ||
180 | *_d = d; | |
181 | return 0; | |
182 | } | |
183 | ||
184 | int cg_read_subgroup(DIR *d, char **fn) { | |
185 | struct dirent *de; | |
186 | ||
187 | assert(d); | |
7027ff61 | 188 | assert(fn); |
35d2e7ec | 189 | |
f01327ad | 190 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
35d2e7ec LP |
191 | char *b; |
192 | ||
193 | if (de->d_type != DT_DIR) | |
194 | continue; | |
195 | ||
49bfc877 | 196 | if (dot_or_dot_dot(de->d_name)) |
35d2e7ec LP |
197 | continue; |
198 | ||
7027ff61 LP |
199 | b = strdup(de->d_name); |
200 | if (!b) | |
35d2e7ec LP |
201 | return -ENOMEM; |
202 | ||
203 | *fn = b; | |
204 | return 1; | |
205 | } | |
206 | ||
35d2e7ec LP |
207 | return 0; |
208 | } | |
209 | ||
4ad49000 | 210 | int cg_rmdir(const char *controller, const char *path) { |
7027ff61 | 211 | _cleanup_free_ char *p = NULL; |
35d2e7ec LP |
212 | int r; |
213 | ||
ad293f5a LP |
214 | r = cg_get_path(controller, path, NULL, &p); |
215 | if (r < 0) | |
35d2e7ec LP |
216 | return r; |
217 | ||
218 | r = rmdir(p); | |
7027ff61 LP |
219 | if (r < 0 && errno != ENOENT) |
220 | return -errno; | |
35d2e7ec | 221 | |
b4cccbc1 | 222 | r = cg_hybrid_unified(); |
f20db199 | 223 | if (r <= 0) |
b4cccbc1 | 224 | return r; |
b4cccbc1 LP |
225 | |
226 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { | |
2977724b TH |
227 | r = cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path); |
228 | if (r < 0) | |
229 | log_warning_errno(r, "Failed to remove compat systemd cgroup %s: %m", path); | |
230 | } | |
231 | ||
7027ff61 | 232 | return 0; |
35d2e7ec LP |
233 | } |
234 | ||
e48fcfef | 235 | static int cg_kill_items( |
1d98fef1 LP |
236 | const char *controller, |
237 | const char *path, | |
238 | int sig, | |
239 | CGroupFlags flags, | |
240 | Set *s, | |
241 | cg_kill_log_func_t log_kill, | |
e48fcfef TM |
242 | void *userdata, |
243 | const char *item) { | |
1d98fef1 | 244 | |
7027ff61 | 245 | _cleanup_set_free_ Set *allocated_set = NULL; |
35d2e7ec | 246 | bool done = false; |
c53d2d54 | 247 | int r, ret = 0, ret_log_kill = 0; |
35d2e7ec | 248 | pid_t my_pid; |
8c6db833 | 249 | |
8c6db833 LP |
250 | assert(sig >= 0); |
251 | ||
0d5b4810 LP |
252 | /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send |
253 | * SIGCONT on SIGKILL. */ | |
254 | if (IN_SET(sig, SIGCONT, SIGKILL)) | |
255 | flags &= ~CGROUP_SIGCONT; | |
256 | ||
8c6db833 LP |
257 | /* This goes through the tasks list and kills them all. This |
258 | * is repeated until no further processes are added to the | |
259 | * tasks list, to properly handle forking processes */ | |
260 | ||
7027ff61 | 261 | if (!s) { |
d5099efc | 262 | s = allocated_set = set_new(NULL); |
7027ff61 | 263 | if (!s) |
ca949c9d | 264 | return -ENOMEM; |
7027ff61 | 265 | } |
8c6db833 | 266 | |
df0ff127 | 267 | my_pid = getpid_cached(); |
8c6db833 LP |
268 | |
269 | do { | |
7027ff61 | 270 | _cleanup_fclose_ FILE *f = NULL; |
0b172489 | 271 | pid_t pid = 0; |
8c6db833 LP |
272 | done = true; |
273 | ||
e48fcfef | 274 | r = cg_enumerate_items(controller, path, &f, item); |
7027ff61 | 275 | if (r < 0) { |
4c633005 | 276 | if (ret >= 0 && r != -ENOENT) |
7027ff61 | 277 | return r; |
35d2e7ec | 278 | |
7027ff61 | 279 | return ret; |
35d2e7ec | 280 | } |
c6c18be3 LP |
281 | |
282 | while ((r = cg_read_pid(f, &pid)) > 0) { | |
8c6db833 | 283 | |
1d98fef1 | 284 | if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid) |
c6c18be3 | 285 | continue; |
8c6db833 | 286 | |
fea72cc0 | 287 | if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid)) |
c6c18be3 | 288 | continue; |
8c6db833 | 289 | |
1d98fef1 | 290 | if (log_kill) |
c53d2d54 | 291 | ret_log_kill = log_kill(pid, sig, userdata); |
1d98fef1 | 292 | |
8c6db833 LP |
293 | /* If we haven't killed this process yet, kill |
294 | * it */ | |
4c633005 LP |
295 | if (kill(pid, sig) < 0) { |
296 | if (ret >= 0 && errno != ESRCH) | |
8c6db833 | 297 | ret = -errno; |
6e8314c4 | 298 | } else { |
1d98fef1 | 299 | if (flags & CGROUP_SIGCONT) |
e155a0aa | 300 | (void) kill(pid, SIGCONT); |
430c18ed | 301 | |
c53d2d54 DB |
302 | if (ret == 0) { |
303 | if (log_kill) | |
304 | ret = ret_log_kill; | |
305 | else | |
306 | ret = 1; | |
307 | } | |
430c18ed | 308 | } |
8c6db833 | 309 | |
8c6db833 LP |
310 | done = false; |
311 | ||
fea72cc0 | 312 | r = set_put(s, PID_TO_PTR(pid)); |
7027ff61 | 313 | if (r < 0) { |
35d2e7ec | 314 | if (ret >= 0) |
7027ff61 | 315 | return r; |
35d2e7ec | 316 | |
7027ff61 | 317 | return ret; |
35d2e7ec LP |
318 | } |
319 | } | |
320 | ||
321 | if (r < 0) { | |
322 | if (ret >= 0) | |
7027ff61 | 323 | return r; |
35d2e7ec | 324 | |
7027ff61 | 325 | return ret; |
8c6db833 LP |
326 | } |
327 | ||
8c6db833 LP |
328 | /* To avoid racing against processes which fork |
329 | * quicker than we can kill them we repeat this until | |
330 | * no new pids need to be killed. */ | |
331 | ||
35d2e7ec | 332 | } while (!done); |
8c6db833 | 333 | |
35d2e7ec | 334 | return ret; |
8c6db833 LP |
335 | } |
336 | ||
e48fcfef TM |
337 | int cg_kill( |
338 | const char *controller, | |
339 | const char *path, | |
340 | int sig, | |
341 | CGroupFlags flags, | |
342 | Set *s, | |
343 | cg_kill_log_func_t log_kill, | |
344 | void *userdata) { | |
345 | int r; | |
346 | ||
347 | r = cg_kill_items(controller, path, sig, flags, s, log_kill, userdata, "cgroup.procs"); | |
348 | if (r < 0 || sig != SIGKILL) | |
349 | return r; | |
350 | ||
351 | /* Only in case of killing with SIGKILL and when using cgroupsv2, kill remaining threads manually as | |
cda5ccdb TM |
352 | a workaround for kernel bug. It was fixed in 5.2-rc5 (c03cd7738a83), backported to 4.19.66 |
353 | (4340d175b898) and 4.14.138 (feb6b123b7dd). */ | |
e48fcfef | 354 | r = cg_unified_controller(controller); |
38288f0b | 355 | if (r <= 0) |
e48fcfef | 356 | return r; |
e48fcfef TM |
357 | |
358 | return cg_kill_items(controller, path, sig, flags, s, log_kill, userdata, "cgroup.threads"); | |
359 | } | |
360 | ||
1d98fef1 LP |
361 | int cg_kill_recursive( |
362 | const char *controller, | |
363 | const char *path, | |
364 | int sig, | |
365 | CGroupFlags flags, | |
366 | Set *s, | |
367 | cg_kill_log_func_t log_kill, | |
368 | void *userdata) { | |
369 | ||
7027ff61 LP |
370 | _cleanup_set_free_ Set *allocated_set = NULL; |
371 | _cleanup_closedir_ DIR *d = NULL; | |
e155a0aa | 372 | int r, ret; |
35d2e7ec | 373 | char *fn; |
8c6db833 LP |
374 | |
375 | assert(path); | |
8c6db833 LP |
376 | assert(sig >= 0); |
377 | ||
7027ff61 | 378 | if (!s) { |
d5099efc | 379 | s = allocated_set = set_new(NULL); |
7027ff61 | 380 | if (!s) |
ca949c9d | 381 | return -ENOMEM; |
7027ff61 | 382 | } |
ca949c9d | 383 | |
1d98fef1 | 384 | ret = cg_kill(controller, path, sig, flags, s, log_kill, userdata); |
8c6db833 | 385 | |
7027ff61 LP |
386 | r = cg_enumerate_subgroups(controller, path, &d); |
387 | if (r < 0) { | |
4c633005 | 388 | if (ret >= 0 && r != -ENOENT) |
7027ff61 | 389 | return r; |
8c6db833 | 390 | |
7027ff61 | 391 | return ret; |
35d2e7ec | 392 | } |
8c6db833 | 393 | |
35d2e7ec | 394 | while ((r = cg_read_subgroup(d, &fn)) > 0) { |
7027ff61 | 395 | _cleanup_free_ char *p = NULL; |
8c6db833 | 396 | |
95b21cff | 397 | p = path_join(empty_to_root(path), fn); |
35d2e7ec | 398 | free(fn); |
7027ff61 LP |
399 | if (!p) |
400 | return -ENOMEM; | |
8c6db833 | 401 | |
1d98fef1 | 402 | r = cg_kill_recursive(controller, p, sig, flags, s, log_kill, userdata); |
e155a0aa | 403 | if (r != 0 && ret >= 0) |
35d2e7ec | 404 | ret = r; |
8c6db833 | 405 | } |
7027ff61 | 406 | if (ret >= 0 && r < 0) |
35d2e7ec LP |
407 | ret = r; |
408 | ||
1d98fef1 | 409 | if (flags & CGROUP_REMOVE) { |
4ad49000 | 410 | r = cg_rmdir(controller, path); |
4c701096 | 411 | if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY)) |
7027ff61 LP |
412 | return r; |
413 | } | |
ca949c9d | 414 | |
8c6db833 LP |
415 | return ret; |
416 | } | |
417 | ||
efdb0237 LP |
418 | static const char *controller_to_dirname(const char *controller) { |
419 | const char *e; | |
3474ae3c | 420 | |
7027ff61 LP |
421 | assert(controller); |
422 | ||
efdb0237 LP |
423 | /* Converts a controller name to the directory name below |
424 | * /sys/fs/cgroup/ we want to mount it to. Effectively, this | |
425 | * just cuts off the name= prefixed used for named | |
426 | * hierarchies, if it is specified. */ | |
427 | ||
2977724b | 428 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { |
b4cccbc1 | 429 | if (cg_hybrid_unified() > 0) |
2977724b TH |
430 | controller = SYSTEMD_CGROUP_CONTROLLER_HYBRID; |
431 | else | |
432 | controller = SYSTEMD_CGROUP_CONTROLLER_LEGACY; | |
433 | } | |
b6629c4b | 434 | |
efdb0237 LP |
435 | e = startswith(controller, "name="); |
436 | if (e) | |
437 | return e; | |
438 | ||
439 | return controller; | |
3474ae3c LP |
440 | } |
441 | ||
569b19d8 LP |
442 | static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) { |
443 | const char *dn; | |
018ef268 | 444 | char *t = NULL; |
3474ae3c | 445 | |
efdb0237 | 446 | assert(fs); |
569b19d8 LP |
447 | assert(controller); |
448 | ||
449 | dn = controller_to_dirname(controller); | |
efdb0237 LP |
450 | |
451 | if (isempty(path) && isempty(suffix)) | |
657ee2d8 | 452 | t = path_join("/sys/fs/cgroup", dn); |
efdb0237 | 453 | else if (isempty(path)) |
657ee2d8 | 454 | t = path_join("/sys/fs/cgroup", dn, suffix); |
efdb0237 | 455 | else if (isempty(suffix)) |
657ee2d8 | 456 | t = path_join("/sys/fs/cgroup", dn, path); |
efdb0237 | 457 | else |
657ee2d8 | 458 | t = path_join("/sys/fs/cgroup", dn, path, suffix); |
efdb0237 LP |
459 | if (!t) |
460 | return -ENOMEM; | |
3474ae3c | 461 | |
efdb0237 LP |
462 | *fs = t; |
463 | return 0; | |
464 | } | |
465 | ||
466 | static int join_path_unified(const char *path, const char *suffix, char **fs) { | |
467 | char *t; | |
468 | ||
469 | assert(fs); | |
470 | ||
471 | if (isempty(path) && isempty(suffix)) | |
472 | t = strdup("/sys/fs/cgroup"); | |
473 | else if (isempty(path)) | |
657ee2d8 | 474 | t = path_join("/sys/fs/cgroup", suffix); |
efdb0237 | 475 | else if (isempty(suffix)) |
657ee2d8 | 476 | t = path_join("/sys/fs/cgroup", path); |
efdb0237 | 477 | else |
657ee2d8 | 478 | t = path_join("/sys/fs/cgroup", path, suffix); |
3474ae3c LP |
479 | if (!t) |
480 | return -ENOMEM; | |
481 | ||
efdb0237 | 482 | *fs = t; |
3474ae3c LP |
483 | return 0; |
484 | } | |
485 | ||
8c6db833 | 486 | int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) { |
415fc41c | 487 | int r; |
8c6db833 | 488 | |
dbd821ac LP |
489 | assert(fs); |
490 | ||
efdb0237 LP |
491 | if (!controller) { |
492 | char *t; | |
493 | ||
569b19d8 LP |
494 | /* If no controller is specified, we return the path |
495 | * *below* the controllers, without any prefix. */ | |
efdb0237 LP |
496 | |
497 | if (!path && !suffix) | |
498 | return -EINVAL; | |
499 | ||
989189ea | 500 | if (!suffix) |
efdb0237 | 501 | t = strdup(path); |
989189ea | 502 | else if (!path) |
efdb0237 LP |
503 | t = strdup(suffix); |
504 | else | |
657ee2d8 | 505 | t = path_join(path, suffix); |
efdb0237 LP |
506 | if (!t) |
507 | return -ENOMEM; | |
508 | ||
858d36c1 | 509 | *fs = path_simplify(t, false); |
efdb0237 LP |
510 | return 0; |
511 | } | |
512 | ||
513 | if (!cg_controller_is_valid(controller)) | |
78edb35a LP |
514 | return -EINVAL; |
515 | ||
b4cccbc1 LP |
516 | r = cg_all_unified(); |
517 | if (r < 0) | |
518 | return r; | |
519 | if (r > 0) | |
efdb0237 | 520 | r = join_path_unified(path, suffix, fs); |
569b19d8 LP |
521 | else |
522 | r = join_path_legacy(controller, path, suffix, fs); | |
efdb0237 LP |
523 | if (r < 0) |
524 | return r; | |
7027ff61 | 525 | |
858d36c1 | 526 | path_simplify(*fs, false); |
efdb0237 | 527 | return 0; |
3474ae3c | 528 | } |
dbd821ac | 529 | |
0fa7b500 MK |
530 | static int controller_is_v1_accessible(const char *root, const char *controller) { |
531 | const char *cpath, *dn; | |
37099707 | 532 | |
efdb0237 | 533 | assert(controller); |
37099707 | 534 | |
81504017 | 535 | dn = controller_to_dirname(controller); |
0fa7b500 | 536 | |
e4645ca5 ZJS |
537 | /* If root if specified, we check that: |
538 | * - possible subcgroup is created at root, | |
539 | * - we can modify the hierarchy. */ | |
540 | ||
541 | cpath = strjoina("/sys/fs/cgroup/", dn, root, root ? "/cgroup.procs" : NULL); | |
0fa7b500 | 542 | if (laccess(cpath, root ? W_OK : F_OK) < 0) |
81504017 | 543 | return -errno; |
37099707 LP |
544 | |
545 | return 0; | |
546 | } | |
547 | ||
3474ae3c | 548 | int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) { |
37099707 | 549 | int r; |
dbd821ac | 550 | |
efdb0237 | 551 | assert(controller); |
3474ae3c | 552 | assert(fs); |
70132bd0 | 553 | |
81504017 MK |
554 | if (!cg_controller_is_valid(controller)) |
555 | return -EINVAL; | |
556 | ||
557 | r = cg_all_unified(); | |
37099707 LP |
558 | if (r < 0) |
559 | return r; | |
81504017 MK |
560 | if (r > 0) { |
561 | /* In the unified hierarchy all controllers are considered accessible, | |
562 | * except for the named hierarchies */ | |
563 | if (startswith(controller, "name=")) | |
564 | return -EOPNOTSUPP; | |
565 | } else { | |
566 | /* Check if the specified controller is actually accessible */ | |
0fa7b500 | 567 | r = controller_is_v1_accessible(NULL, controller); |
81504017 MK |
568 | if (r < 0) |
569 | return r; | |
570 | } | |
3474ae3c | 571 | |
efdb0237 | 572 | return cg_get_path(controller, path, suffix, fs); |
8c6db833 LP |
573 | } |
574 | ||
4b58153d LP |
575 | int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) { |
576 | _cleanup_free_ char *fs = NULL; | |
577 | int r; | |
578 | ||
579 | assert(path); | |
580 | assert(name); | |
581 | assert(value || size <= 0); | |
582 | ||
583 | r = cg_get_path(controller, path, NULL, &fs); | |
584 | if (r < 0) | |
585 | return r; | |
586 | ||
587 | if (setxattr(fs, name, value, size, flags) < 0) | |
588 | return -errno; | |
589 | ||
590 | return 0; | |
591 | } | |
592 | ||
593 | int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) { | |
594 | _cleanup_free_ char *fs = NULL; | |
595 | ssize_t n; | |
596 | int r; | |
597 | ||
598 | assert(path); | |
599 | assert(name); | |
600 | ||
601 | r = cg_get_path(controller, path, NULL, &fs); | |
602 | if (r < 0) | |
603 | return r; | |
604 | ||
605 | n = getxattr(fs, name, value, size); | |
606 | if (n < 0) | |
607 | return -errno; | |
608 | ||
609 | return (int) n; | |
610 | } | |
611 | ||
baa358df AZ |
612 | int cg_get_xattr_malloc(const char *controller, const char *path, const char *name, char **ret) { |
613 | _cleanup_free_ char *fs = NULL; | |
614 | int r; | |
615 | ||
616 | assert(path); | |
617 | assert(name); | |
618 | ||
619 | r = cg_get_path(controller, path, NULL, &fs); | |
620 | if (r < 0) | |
621 | return r; | |
622 | ||
623 | r = getxattr_malloc(fs, name, ret, false); | |
624 | if (r < 0) | |
625 | return r; | |
626 | ||
627 | return r; | |
628 | } | |
629 | ||
59331b8e AZ |
630 | int cg_get_xattr_bool(const char *controller, const char *path, const char *name) { |
631 | _cleanup_free_ char *val = NULL; | |
632 | int r; | |
633 | ||
634 | assert(path); | |
635 | assert(name); | |
636 | ||
637 | r = cg_get_xattr_malloc(controller, path, name, &val); | |
638 | if (r < 0) | |
639 | return r; | |
640 | ||
641 | return parse_boolean(val); | |
642 | } | |
643 | ||
bf25f165 LP |
644 | int cg_remove_xattr(const char *controller, const char *path, const char *name) { |
645 | _cleanup_free_ char *fs = NULL; | |
646 | int r; | |
647 | ||
648 | assert(path); | |
649 | assert(name); | |
650 | ||
651 | r = cg_get_path(controller, path, NULL, &fs); | |
652 | if (r < 0) | |
653 | return r; | |
654 | ||
655 | if (removexattr(fs, name) < 0) | |
656 | return -errno; | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
ae7ef63f | 661 | int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) { |
7027ff61 | 662 | _cleanup_fclose_ FILE *f = NULL; |
b6629c4b | 663 | const char *fs, *controller_str; |
d2b39cb6 | 664 | int unified, r; |
8c6db833 | 665 | |
c6c18be3 | 666 | assert(pid >= 0); |
ae7ef63f | 667 | assert(ret_path); |
8c6db833 | 668 | |
5da38d07 TH |
669 | if (controller) { |
670 | if (!cg_controller_is_valid(controller)) | |
671 | return -EINVAL; | |
672 | } else | |
673 | controller = SYSTEMD_CGROUP_CONTROLLER; | |
674 | ||
c22800e4 | 675 | unified = cg_unified_controller(controller); |
b4cccbc1 LP |
676 | if (unified < 0) |
677 | return unified; | |
678 | if (unified == 0) { | |
b6629c4b TH |
679 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) |
680 | controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY; | |
681 | else | |
682 | controller_str = controller; | |
b6629c4b | 683 | } |
7027ff61 | 684 | |
b68fa010 | 685 | fs = procfs_file_alloca(pid, "cgroup"); |
fdeea3f4 ZJS |
686 | r = fopen_unlocked(fs, "re", &f); |
687 | if (r == -ENOENT) | |
688 | return -ESRCH; | |
689 | if (r < 0) | |
690 | return r; | |
35bbbf85 | 691 | |
d2b39cb6 LP |
692 | for (;;) { |
693 | _cleanup_free_ char *line = NULL; | |
ae7ef63f | 694 | char *e; |
c6c18be3 | 695 | |
d2b39cb6 LP |
696 | r = read_line(f, LONG_LINE_MAX, &line); |
697 | if (r < 0) | |
698 | return r; | |
699 | if (r == 0) | |
ae7ef63f | 700 | return -ENODATA; |
c6c18be3 | 701 | |
efdb0237 LP |
702 | if (unified) { |
703 | e = startswith(line, "0:"); | |
704 | if (!e) | |
705 | continue; | |
c6c18be3 | 706 | |
efdb0237 LP |
707 | e = strchr(e, ':'); |
708 | if (!e) | |
709 | continue; | |
710 | } else { | |
711 | char *l; | |
efdb0237 LP |
712 | |
713 | l = strchr(line, ':'); | |
714 | if (!l) | |
715 | continue; | |
8af8afd6 | 716 | |
efdb0237 LP |
717 | l++; |
718 | e = strchr(l, ':'); | |
719 | if (!e) | |
720 | continue; | |
efdb0237 | 721 | *e = 0; |
ae7ef63f ZJS |
722 | |
723 | r = string_contains_word(l, ",", controller_str); | |
724 | if (r < 0) | |
725 | return r; | |
726 | if (r == 0) | |
efdb0237 | 727 | continue; |
8af8afd6 LP |
728 | } |
729 | ||
ae7ef63f ZJS |
730 | char *path = strdup(e + 1); |
731 | if (!path) | |
7027ff61 | 732 | return -ENOMEM; |
c6c18be3 | 733 | |
5e20b0a4 | 734 | /* Truncate suffix indicating the process is a zombie */ |
ae7ef63f | 735 | e = endswith(path, " (deleted)"); |
5e20b0a4 LP |
736 | if (e) |
737 | *e = 0; | |
738 | ||
ae7ef63f | 739 | *ret_path = path; |
7027ff61 | 740 | return 0; |
c6c18be3 | 741 | } |
8c6db833 LP |
742 | } |
743 | ||
744 | int cg_install_release_agent(const char *controller, const char *agent) { | |
7027ff61 | 745 | _cleanup_free_ char *fs = NULL, *contents = NULL; |
efdb0237 | 746 | const char *sc; |
415fc41c | 747 | int r; |
8c6db833 | 748 | |
8c6db833 LP |
749 | assert(agent); |
750 | ||
c22800e4 | 751 | r = cg_unified_controller(controller); |
b4cccbc1 LP |
752 | if (r < 0) |
753 | return r; | |
754 | if (r > 0) /* doesn't apply to unified hierarchy */ | |
efdb0237 LP |
755 | return -EOPNOTSUPP; |
756 | ||
7027ff61 LP |
757 | r = cg_get_path(controller, NULL, "release_agent", &fs); |
758 | if (r < 0) | |
c6c18be3 | 759 | return r; |
8c6db833 | 760 | |
7027ff61 LP |
761 | r = read_one_line_file(fs, &contents); |
762 | if (r < 0) | |
763 | return r; | |
8c6db833 LP |
764 | |
765 | sc = strstrip(contents); | |
e155a0aa | 766 | if (isempty(sc)) { |
604028de | 767 | r = write_string_file(fs, agent, WRITE_STRING_FILE_DISABLE_BUFFER); |
574d5f2d | 768 | if (r < 0) |
7027ff61 | 769 | return r; |
b8725df8 | 770 | } else if (!path_equal(sc, agent)) |
7027ff61 | 771 | return -EEXIST; |
8c6db833 | 772 | |
0da16248 | 773 | fs = mfree(fs); |
7027ff61 LP |
774 | r = cg_get_path(controller, NULL, "notify_on_release", &fs); |
775 | if (r < 0) | |
776 | return r; | |
8c6db833 | 777 | |
0da16248 | 778 | contents = mfree(contents); |
7027ff61 LP |
779 | r = read_one_line_file(fs, &contents); |
780 | if (r < 0) | |
781 | return r; | |
8c6db833 LP |
782 | |
783 | sc = strstrip(contents); | |
8c6db833 | 784 | if (streq(sc, "0")) { |
604028de | 785 | r = write_string_file(fs, "1", WRITE_STRING_FILE_DISABLE_BUFFER); |
7027ff61 LP |
786 | if (r < 0) |
787 | return r; | |
c6c18be3 | 788 | |
7027ff61 LP |
789 | return 1; |
790 | } | |
8c6db833 | 791 | |
7027ff61 LP |
792 | if (!streq(sc, "1")) |
793 | return -EIO; | |
8c6db833 | 794 | |
7027ff61 | 795 | return 0; |
8c6db833 LP |
796 | } |
797 | ||
ad929bcc KS |
798 | int cg_uninstall_release_agent(const char *controller) { |
799 | _cleanup_free_ char *fs = NULL; | |
415fc41c | 800 | int r; |
efdb0237 | 801 | |
c22800e4 | 802 | r = cg_unified_controller(controller); |
b4cccbc1 LP |
803 | if (r < 0) |
804 | return r; | |
805 | if (r > 0) /* Doesn't apply to unified hierarchy */ | |
efdb0237 | 806 | return -EOPNOTSUPP; |
ad929bcc | 807 | |
ac9ef333 LP |
808 | r = cg_get_path(controller, NULL, "notify_on_release", &fs); |
809 | if (r < 0) | |
810 | return r; | |
811 | ||
604028de | 812 | r = write_string_file(fs, "0", WRITE_STRING_FILE_DISABLE_BUFFER); |
ac9ef333 LP |
813 | if (r < 0) |
814 | return r; | |
815 | ||
0da16248 | 816 | fs = mfree(fs); |
ac9ef333 | 817 | |
ad929bcc KS |
818 | r = cg_get_path(controller, NULL, "release_agent", &fs); |
819 | if (r < 0) | |
820 | return r; | |
821 | ||
604028de | 822 | r = write_string_file(fs, "", WRITE_STRING_FILE_DISABLE_BUFFER); |
ad929bcc KS |
823 | if (r < 0) |
824 | return r; | |
825 | ||
ac9ef333 | 826 | return 0; |
ad929bcc KS |
827 | } |
828 | ||
6f883237 | 829 | int cg_is_empty(const char *controller, const char *path) { |
7027ff61 | 830 | _cleanup_fclose_ FILE *f = NULL; |
efdb0237 | 831 | pid_t pid; |
7027ff61 | 832 | int r; |
8c6db833 | 833 | |
8c6db833 LP |
834 | assert(path); |
835 | ||
b043cd0b | 836 | r = cg_enumerate_processes(controller, path, &f); |
6f883237 | 837 | if (r == -ENOENT) |
1bcf3fc6 | 838 | return true; |
c3175a7f | 839 | if (r < 0) |
6f883237 | 840 | return r; |
8c6db833 | 841 | |
6f883237 | 842 | r = cg_read_pid(f, &pid); |
c6c18be3 LP |
843 | if (r < 0) |
844 | return r; | |
8c6db833 | 845 | |
6f883237 | 846 | return r == 0; |
8c6db833 LP |
847 | } |
848 | ||
6f883237 | 849 | int cg_is_empty_recursive(const char *controller, const char *path) { |
415fc41c | 850 | int r; |
8c6db833 | 851 | |
8c6db833 LP |
852 | assert(path); |
853 | ||
6fd66507 | 854 | /* The root cgroup is always populated */ |
57ea45e1 | 855 | if (controller && empty_or_root(path)) |
efdb0237 | 856 | return false; |
6fd66507 | 857 | |
c22800e4 | 858 | r = cg_unified_controller(controller); |
b4cccbc1 LP |
859 | if (r < 0) |
860 | return r; | |
861 | if (r > 0) { | |
ab2c3861 | 862 | _cleanup_free_ char *t = NULL; |
8c6db833 | 863 | |
efdb0237 | 864 | /* On the unified hierarchy we can check empty state |
ab2c3861 | 865 | * via the "populated" attribute of "cgroup.events". */ |
8c6db833 | 866 | |
ab2c3861 | 867 | r = cg_read_event(controller, path, "populated", &t); |
1bcf3fc6 ZJS |
868 | if (r == -ENOENT) |
869 | return true; | |
efdb0237 LP |
870 | if (r < 0) |
871 | return r; | |
872 | ||
873 | return streq(t, "0"); | |
874 | } else { | |
875 | _cleanup_closedir_ DIR *d = NULL; | |
876 | char *fn; | |
8c6db833 | 877 | |
efdb0237 | 878 | r = cg_is_empty(controller, path); |
35d2e7ec | 879 | if (r <= 0) |
7027ff61 | 880 | return r; |
35d2e7ec | 881 | |
efdb0237 LP |
882 | r = cg_enumerate_subgroups(controller, path, &d); |
883 | if (r == -ENOENT) | |
1bcf3fc6 | 884 | return true; |
efdb0237 LP |
885 | if (r < 0) |
886 | return r; | |
35d2e7ec | 887 | |
efdb0237 LP |
888 | while ((r = cg_read_subgroup(d, &fn)) > 0) { |
889 | _cleanup_free_ char *p = NULL; | |
890 | ||
657ee2d8 | 891 | p = path_join(path, fn); |
efdb0237 LP |
892 | free(fn); |
893 | if (!p) | |
894 | return -ENOMEM; | |
895 | ||
896 | r = cg_is_empty_recursive(controller, p); | |
897 | if (r <= 0) | |
898 | return r; | |
899 | } | |
900 | if (r < 0) | |
901 | return r; | |
902 | ||
903 | return true; | |
904 | } | |
35d2e7ec LP |
905 | } |
906 | ||
2a8020fe ZJS |
907 | int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) { |
908 | _cleanup_free_ char *controller = NULL, *path = NULL; | |
35d2e7ec LP |
909 | |
910 | assert(spec); | |
35d2e7ec LP |
911 | |
912 | if (*spec == '/') { | |
99be45a4 | 913 | if (!path_is_normalized(spec)) |
e884315e | 914 | return -EINVAL; |
35d2e7ec | 915 | |
2a8020fe ZJS |
916 | if (ret_path) { |
917 | path = strdup(spec); | |
918 | if (!path) | |
35d2e7ec LP |
919 | return -ENOMEM; |
920 | ||
2a8020fe | 921 | path_simplify(path, false); |
8c6db833 LP |
922 | } |
923 | ||
2a8020fe ZJS |
924 | } else { |
925 | const char *e; | |
35d2e7ec | 926 | |
2a8020fe ZJS |
927 | e = strchr(spec, ':'); |
928 | if (e) { | |
929 | controller = strndup(spec, e-spec); | |
930 | if (!controller) | |
35d2e7ec | 931 | return -ENOMEM; |
2a8020fe ZJS |
932 | if (!cg_controller_is_valid(controller)) |
933 | return -EINVAL; | |
35d2e7ec | 934 | |
2a8020fe ZJS |
935 | if (!isempty(e + 1)) { |
936 | path = strdup(e+1); | |
937 | if (!path) | |
938 | return -ENOMEM; | |
35d2e7ec | 939 | |
2a8020fe ZJS |
940 | if (!path_is_normalized(path) || |
941 | !path_is_absolute(path)) | |
942 | return -EINVAL; | |
8c6db833 | 943 | |
2a8020fe ZJS |
944 | path_simplify(path, false); |
945 | } | |
246aa6dd | 946 | |
2a8020fe ZJS |
947 | } else { |
948 | if (!cg_controller_is_valid(spec)) | |
949 | return -EINVAL; | |
35d2e7ec | 950 | |
2a8020fe ZJS |
951 | if (ret_controller) { |
952 | controller = strdup(spec); | |
953 | if (!controller) | |
954 | return -ENOMEM; | |
955 | } | |
baa89da4 | 956 | } |
baa89da4 | 957 | } |
5954c074 | 958 | |
2a8020fe ZJS |
959 | if (ret_controller) |
960 | *ret_controller = TAKE_PTR(controller); | |
961 | if (ret_path) | |
962 | *ret_path = TAKE_PTR(path); | |
35d2e7ec | 963 | return 0; |
8c6db833 | 964 | } |
c6c18be3 | 965 | |
7027ff61 | 966 | int cg_mangle_path(const char *path, char **result) { |
78edb35a LP |
967 | _cleanup_free_ char *c = NULL, *p = NULL; |
968 | char *t; | |
35d2e7ec LP |
969 | int r; |
970 | ||
971 | assert(path); | |
972 | assert(result); | |
973 | ||
73e231ab | 974 | /* First, check if it already is a filesystem path */ |
7027ff61 | 975 | if (path_startswith(path, "/sys/fs/cgroup")) { |
35d2e7ec | 976 | |
b69d29ce LP |
977 | t = strdup(path); |
978 | if (!t) | |
35d2e7ec LP |
979 | return -ENOMEM; |
980 | ||
858d36c1 | 981 | *result = path_simplify(t, false); |
35d2e7ec LP |
982 | return 0; |
983 | } | |
984 | ||
73e231ab | 985 | /* Otherwise, treat it as cg spec */ |
b69d29ce LP |
986 | r = cg_split_spec(path, &c, &p); |
987 | if (r < 0) | |
35d2e7ec LP |
988 | return r; |
989 | ||
efdb0237 | 990 | return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result); |
35d2e7ec | 991 | } |
1f73f0f1 | 992 | |
7027ff61 | 993 | int cg_get_root_path(char **path) { |
9444b1f2 | 994 | char *p, *e; |
7027ff61 LP |
995 | int r; |
996 | ||
997 | assert(path); | |
998 | ||
9444b1f2 | 999 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p); |
7027ff61 LP |
1000 | if (r < 0) |
1001 | return r; | |
1002 | ||
efdb0237 LP |
1003 | e = endswith(p, "/" SPECIAL_INIT_SCOPE); |
1004 | if (!e) | |
1005 | e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */ | |
1006 | if (!e) | |
1007 | e = endswith(p, "/system"); /* even more legacy */ | |
9444b1f2 | 1008 | if (e) |
7027ff61 LP |
1009 | *e = 0; |
1010 | ||
1f73f0f1 LP |
1011 | *path = p; |
1012 | return 0; | |
1013 | } | |
b59e2465 | 1014 | |
751bc6ac LP |
1015 | int cg_shift_path(const char *cgroup, const char *root, const char **shifted) { |
1016 | _cleanup_free_ char *rt = NULL; | |
1017 | char *p; | |
ba1261bc LP |
1018 | int r; |
1019 | ||
e9174f29 | 1020 | assert(cgroup); |
751bc6ac | 1021 | assert(shifted); |
e9174f29 LP |
1022 | |
1023 | if (!root) { | |
1024 | /* If the root was specified let's use that, otherwise | |
1025 | * let's determine it from PID 1 */ | |
1026 | ||
751bc6ac | 1027 | r = cg_get_root_path(&rt); |
e9174f29 LP |
1028 | if (r < 0) |
1029 | return r; | |
1030 | ||
751bc6ac | 1031 | root = rt; |
e9174f29 | 1032 | } |
ba1261bc | 1033 | |
751bc6ac | 1034 | p = path_startswith(cgroup, root); |
efdb0237 | 1035 | if (p && p > cgroup) |
751bc6ac LP |
1036 | *shifted = p - 1; |
1037 | else | |
1038 | *shifted = cgroup; | |
1039 | ||
1040 | return 0; | |
1041 | } | |
1042 | ||
1043 | int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { | |
1044 | _cleanup_free_ char *raw = NULL; | |
1045 | const char *c; | |
1046 | int r; | |
1047 | ||
1048 | assert(pid >= 0); | |
1049 | assert(cgroup); | |
1050 | ||
1051 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw); | |
7027ff61 | 1052 | if (r < 0) |
ba1261bc | 1053 | return r; |
ba1261bc | 1054 | |
751bc6ac LP |
1055 | r = cg_shift_path(raw, root, &c); |
1056 | if (r < 0) | |
1057 | return r; | |
ba1261bc | 1058 | |
ae2a15bc LP |
1059 | if (c == raw) |
1060 | *cgroup = TAKE_PTR(raw); | |
1061 | else { | |
751bc6ac | 1062 | char *n; |
ba1261bc | 1063 | |
751bc6ac LP |
1064 | n = strdup(c); |
1065 | if (!n) | |
ba1261bc | 1066 | return -ENOMEM; |
ba1261bc | 1067 | |
751bc6ac LP |
1068 | *cgroup = n; |
1069 | } | |
ba1261bc LP |
1070 | |
1071 | return 0; | |
1072 | } | |
1073 | ||
9ed794a3 | 1074 | int cg_path_decode_unit(const char *cgroup, char **unit) { |
8b0849e9 LP |
1075 | char *c, *s; |
1076 | size_t n; | |
ef1673d1 MT |
1077 | |
1078 | assert(cgroup); | |
6c03089c | 1079 | assert(unit); |
ef1673d1 | 1080 | |
8b0849e9 LP |
1081 | n = strcspn(cgroup, "/"); |
1082 | if (n < 3) | |
1083 | return -ENXIO; | |
1084 | ||
1085 | c = strndupa(cgroup, n); | |
ae018d9b | 1086 | c = cg_unescape(c); |
ef1673d1 | 1087 | |
7410616c | 1088 | if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
cfeaa44a | 1089 | return -ENXIO; |
ef1673d1 | 1090 | |
d7bd3de0 | 1091 | s = strdup(c); |
6c03089c LP |
1092 | if (!s) |
1093 | return -ENOMEM; | |
1094 | ||
1095 | *unit = s; | |
ef1673d1 MT |
1096 | return 0; |
1097 | } | |
1098 | ||
8b0849e9 LP |
1099 | static bool valid_slice_name(const char *p, size_t n) { |
1100 | ||
1101 | if (!p) | |
1102 | return false; | |
1103 | ||
fbd0b64f | 1104 | if (n < STRLEN("x.slice")) |
8b0849e9 LP |
1105 | return false; |
1106 | ||
1107 | if (memcmp(p + n - 6, ".slice", 6) == 0) { | |
1108 | char buf[n+1], *c; | |
1109 | ||
1110 | memcpy(buf, p, n); | |
1111 | buf[n] = 0; | |
1112 | ||
1113 | c = cg_unescape(buf); | |
1114 | ||
7410616c | 1115 | return unit_name_is_valid(c, UNIT_NAME_PLAIN); |
8b0849e9 LP |
1116 | } |
1117 | ||
1118 | return false; | |
1119 | } | |
1120 | ||
9444b1f2 | 1121 | static const char *skip_slices(const char *p) { |
8b0849e9 LP |
1122 | assert(p); |
1123 | ||
9444b1f2 LP |
1124 | /* Skips over all slice assignments */ |
1125 | ||
1126 | for (;;) { | |
1021b21b LP |
1127 | size_t n; |
1128 | ||
9444b1f2 LP |
1129 | p += strspn(p, "/"); |
1130 | ||
1131 | n = strcspn(p, "/"); | |
8b0849e9 | 1132 | if (!valid_slice_name(p, n)) |
9444b1f2 LP |
1133 | return p; |
1134 | ||
1135 | p += n; | |
1136 | } | |
1137 | } | |
1138 | ||
8b0849e9 | 1139 | int cg_path_get_unit(const char *path, char **ret) { |
fe96c0f8 | 1140 | _cleanup_free_ char *unit = NULL; |
6c03089c | 1141 | const char *e; |
8b0849e9 | 1142 | int r; |
6c03089c LP |
1143 | |
1144 | assert(path); | |
8b0849e9 | 1145 | assert(ret); |
6c03089c | 1146 | |
9444b1f2 | 1147 | e = skip_slices(path); |
6c03089c | 1148 | |
8b0849e9 LP |
1149 | r = cg_path_decode_unit(e, &unit); |
1150 | if (r < 0) | |
1151 | return r; | |
1152 | ||
1153 | /* We skipped over the slices, don't accept any now */ | |
fe96c0f8 | 1154 | if (endswith(unit, ".slice")) |
8b0849e9 | 1155 | return -ENXIO; |
8b0849e9 | 1156 | |
fe96c0f8 | 1157 | *ret = TAKE_PTR(unit); |
8b0849e9 | 1158 | return 0; |
6c03089c LP |
1159 | } |
1160 | ||
1161 | int cg_pid_get_unit(pid_t pid, char **unit) { | |
7fd1b19b | 1162 | _cleanup_free_ char *cgroup = NULL; |
ba1261bc | 1163 | int r; |
ba1261bc | 1164 | |
ef1673d1 MT |
1165 | assert(unit); |
1166 | ||
7027ff61 | 1167 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
ef1673d1 MT |
1168 | if (r < 0) |
1169 | return r; | |
1170 | ||
6c03089c LP |
1171 | return cg_path_get_unit(cgroup, unit); |
1172 | } | |
ef1673d1 | 1173 | |
d4fffc4b ZJS |
1174 | /** |
1175 | * Skip session-*.scope, but require it to be there. | |
1176 | */ | |
9444b1f2 LP |
1177 | static const char *skip_session(const char *p) { |
1178 | size_t n; | |
1179 | ||
8b0849e9 LP |
1180 | if (isempty(p)) |
1181 | return NULL; | |
9444b1f2 LP |
1182 | |
1183 | p += strspn(p, "/"); | |
1184 | ||
1185 | n = strcspn(p, "/"); | |
fbd0b64f | 1186 | if (n < STRLEN("session-x.scope")) |
d4fffc4b ZJS |
1187 | return NULL; |
1188 | ||
8b0849e9 LP |
1189 | if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) { |
1190 | char buf[n - 8 - 6 + 1]; | |
1191 | ||
1192 | memcpy(buf, p + 8, n - 8 - 6); | |
1193 | buf[n - 8 - 6] = 0; | |
d4fffc4b | 1194 | |
8b0849e9 LP |
1195 | /* Note that session scopes never need unescaping, |
1196 | * since they cannot conflict with the kernel's own | |
1197 | * names, hence we don't need to call cg_unescape() | |
1198 | * here. */ | |
1199 | ||
1200 | if (!session_id_valid(buf)) | |
1201 | return false; | |
1202 | ||
1203 | p += n; | |
1204 | p += strspn(p, "/"); | |
1205 | return p; | |
1206 | } | |
1207 | ||
1208 | return NULL; | |
d4fffc4b ZJS |
1209 | } |
1210 | ||
1211 | /** | |
1212 | * Skip user@*.service, but require it to be there. | |
1213 | */ | |
1214 | static const char *skip_user_manager(const char *p) { | |
1215 | size_t n; | |
1216 | ||
8b0849e9 LP |
1217 | if (isempty(p)) |
1218 | return NULL; | |
d4fffc4b ZJS |
1219 | |
1220 | p += strspn(p, "/"); | |
1221 | ||
1222 | n = strcspn(p, "/"); | |
fbd0b64f | 1223 | if (n < STRLEN("user@x.service")) |
6c03089c | 1224 | return NULL; |
ef1673d1 | 1225 | |
8b0849e9 LP |
1226 | if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) { |
1227 | char buf[n - 5 - 8 + 1]; | |
9444b1f2 | 1228 | |
8b0849e9 LP |
1229 | memcpy(buf, p + 5, n - 5 - 8); |
1230 | buf[n - 5 - 8] = 0; | |
1231 | ||
1232 | /* Note that user manager services never need unescaping, | |
1233 | * since they cannot conflict with the kernel's own | |
1234 | * names, hence we don't need to call cg_unescape() | |
1235 | * here. */ | |
1236 | ||
1237 | if (parse_uid(buf, NULL) < 0) | |
1238 | return NULL; | |
1239 | ||
1240 | p += n; | |
1241 | p += strspn(p, "/"); | |
1242 | ||
1243 | return p; | |
1244 | } | |
1245 | ||
1246 | return NULL; | |
9444b1f2 LP |
1247 | } |
1248 | ||
329ac4bc | 1249 | static const char *skip_user_prefix(const char *path) { |
d4fffc4b | 1250 | const char *e, *t; |
ef1673d1 | 1251 | |
6c03089c | 1252 | assert(path); |
ba1261bc | 1253 | |
9444b1f2 LP |
1254 | /* Skip slices, if there are any */ |
1255 | e = skip_slices(path); | |
ba1261bc | 1256 | |
329ac4bc | 1257 | /* Skip the user manager, if it's in the path now... */ |
8b0849e9 | 1258 | t = skip_user_manager(e); |
329ac4bc LP |
1259 | if (t) |
1260 | return t; | |
8b0849e9 | 1261 | |
329ac4bc LP |
1262 | /* Alternatively skip the user session if it is in the path... */ |
1263 | return skip_session(e); | |
1264 | } | |
32081481 | 1265 | |
329ac4bc LP |
1266 | int cg_path_get_user_unit(const char *path, char **ret) { |
1267 | const char *t; | |
6c03089c | 1268 | |
329ac4bc LP |
1269 | assert(path); |
1270 | assert(ret); | |
8b0849e9 | 1271 | |
329ac4bc LP |
1272 | t = skip_user_prefix(path); |
1273 | if (!t) | |
8b0849e9 | 1274 | return -ENXIO; |
8b0849e9 | 1275 | |
bf21be10 LP |
1276 | /* And from here on it looks pretty much the same as for a system unit, hence let's use the same |
1277 | * parser. */ | |
329ac4bc | 1278 | return cg_path_get_unit(t, ret); |
ef1673d1 | 1279 | } |
ba1261bc | 1280 | |
ef1673d1 | 1281 | int cg_pid_get_user_unit(pid_t pid, char **unit) { |
7fd1b19b | 1282 | _cleanup_free_ char *cgroup = NULL; |
6c03089c LP |
1283 | int r; |
1284 | ||
1285 | assert(unit); | |
1286 | ||
7027ff61 | 1287 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
6c03089c LP |
1288 | if (r < 0) |
1289 | return r; | |
1290 | ||
1291 | return cg_path_get_user_unit(cgroup, unit); | |
ba1261bc | 1292 | } |
e884315e | 1293 | |
7027ff61 | 1294 | int cg_path_get_machine_name(const char *path, char **machine) { |
efdb0237 LP |
1295 | _cleanup_free_ char *u = NULL; |
1296 | const char *sl; | |
89f7c846 | 1297 | int r; |
374ec6ab | 1298 | |
89f7c846 LP |
1299 | r = cg_path_get_unit(path, &u); |
1300 | if (r < 0) | |
1301 | return r; | |
7027ff61 | 1302 | |
efdb0237 | 1303 | sl = strjoina("/run/systemd/machines/unit:", u); |
89f7c846 | 1304 | return readlink_malloc(sl, machine); |
7027ff61 LP |
1305 | } |
1306 | ||
1307 | int cg_pid_get_machine_name(pid_t pid, char **machine) { | |
7fd1b19b | 1308 | _cleanup_free_ char *cgroup = NULL; |
7027ff61 LP |
1309 | int r; |
1310 | ||
1311 | assert(machine); | |
1312 | ||
1313 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1314 | if (r < 0) | |
1315 | return r; | |
1316 | ||
1317 | return cg_path_get_machine_name(cgroup, machine); | |
1318 | } | |
1319 | ||
1320 | int cg_path_get_session(const char *path, char **session) { | |
8b0849e9 LP |
1321 | _cleanup_free_ char *unit = NULL; |
1322 | char *start, *end; | |
1323 | int r; | |
7027ff61 LP |
1324 | |
1325 | assert(path); | |
7027ff61 | 1326 | |
8b0849e9 LP |
1327 | r = cg_path_get_unit(path, &unit); |
1328 | if (r < 0) | |
1329 | return r; | |
7027ff61 | 1330 | |
8b0849e9 LP |
1331 | start = startswith(unit, "session-"); |
1332 | if (!start) | |
cfeaa44a | 1333 | return -ENXIO; |
8b0849e9 LP |
1334 | end = endswith(start, ".scope"); |
1335 | if (!end) | |
cfeaa44a | 1336 | return -ENXIO; |
8b0849e9 LP |
1337 | |
1338 | *end = 0; | |
1339 | if (!session_id_valid(start)) | |
cfeaa44a | 1340 | return -ENXIO; |
374ec6ab | 1341 | |
af08d2f9 | 1342 | if (session) { |
8b0849e9 | 1343 | char *rr; |
af08d2f9 | 1344 | |
8b0849e9 LP |
1345 | rr = strdup(start); |
1346 | if (!rr) | |
af08d2f9 LP |
1347 | return -ENOMEM; |
1348 | ||
8b0849e9 | 1349 | *session = rr; |
af08d2f9 | 1350 | } |
7027ff61 | 1351 | |
7027ff61 LP |
1352 | return 0; |
1353 | } | |
1354 | ||
1355 | int cg_pid_get_session(pid_t pid, char **session) { | |
7fd1b19b | 1356 | _cleanup_free_ char *cgroup = NULL; |
7027ff61 LP |
1357 | int r; |
1358 | ||
7027ff61 LP |
1359 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
1360 | if (r < 0) | |
1361 | return r; | |
1362 | ||
1363 | return cg_path_get_session(cgroup, session); | |
1364 | } | |
1365 | ||
ae018d9b | 1366 | int cg_path_get_owner_uid(const char *path, uid_t *uid) { |
374ec6ab | 1367 | _cleanup_free_ char *slice = NULL; |
8b0849e9 | 1368 | char *start, *end; |
374ec6ab | 1369 | int r; |
ae018d9b LP |
1370 | |
1371 | assert(path); | |
ae018d9b | 1372 | |
374ec6ab LP |
1373 | r = cg_path_get_slice(path, &slice); |
1374 | if (r < 0) | |
1375 | return r; | |
ae018d9b | 1376 | |
674eb685 LP |
1377 | start = startswith(slice, "user-"); |
1378 | if (!start) | |
cfeaa44a | 1379 | return -ENXIO; |
8b0849e9 | 1380 | end = endswith(start, ".slice"); |
674eb685 | 1381 | if (!end) |
cfeaa44a | 1382 | return -ENXIO; |
ae018d9b | 1383 | |
8b0849e9 LP |
1384 | *end = 0; |
1385 | if (parse_uid(start, uid) < 0) | |
cfeaa44a | 1386 | return -ENXIO; |
674eb685 | 1387 | |
674eb685 | 1388 | return 0; |
ae018d9b LP |
1389 | } |
1390 | ||
1391 | int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) { | |
1392 | _cleanup_free_ char *cgroup = NULL; | |
1393 | int r; | |
1394 | ||
ae018d9b LP |
1395 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
1396 | if (r < 0) | |
1397 | return r; | |
1398 | ||
1399 | return cg_path_get_owner_uid(cgroup, uid); | |
1400 | } | |
1401 | ||
1021b21b LP |
1402 | int cg_path_get_slice(const char *p, char **slice) { |
1403 | const char *e = NULL; | |
1021b21b LP |
1404 | |
1405 | assert(p); | |
1406 | assert(slice); | |
1407 | ||
329ac4bc LP |
1408 | /* Finds the right-most slice unit from the beginning, but |
1409 | * stops before we come to the first non-slice unit. */ | |
1410 | ||
1021b21b LP |
1411 | for (;;) { |
1412 | size_t n; | |
1413 | ||
1414 | p += strspn(p, "/"); | |
1415 | ||
1416 | n = strcspn(p, "/"); | |
8b0849e9 | 1417 | if (!valid_slice_name(p, n)) { |
1021b21b | 1418 | |
8b0849e9 LP |
1419 | if (!e) { |
1420 | char *s; | |
1021b21b | 1421 | |
e5d855d3 | 1422 | s = strdup(SPECIAL_ROOT_SLICE); |
8b0849e9 LP |
1423 | if (!s) |
1424 | return -ENOMEM; | |
1021b21b | 1425 | |
8b0849e9 LP |
1426 | *slice = s; |
1427 | return 0; | |
1428 | } | |
1429 | ||
1430 | return cg_path_decode_unit(e, slice); | |
1021b21b LP |
1431 | } |
1432 | ||
1433 | e = p; | |
1021b21b LP |
1434 | p += n; |
1435 | } | |
1436 | } | |
1437 | ||
1438 | int cg_pid_get_slice(pid_t pid, char **slice) { | |
1439 | _cleanup_free_ char *cgroup = NULL; | |
1440 | int r; | |
1441 | ||
1442 | assert(slice); | |
1443 | ||
1444 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1445 | if (r < 0) | |
1446 | return r; | |
1447 | ||
1448 | return cg_path_get_slice(cgroup, slice); | |
1449 | } | |
1450 | ||
329ac4bc LP |
1451 | int cg_path_get_user_slice(const char *p, char **slice) { |
1452 | const char *t; | |
1453 | assert(p); | |
1454 | assert(slice); | |
1455 | ||
1456 | t = skip_user_prefix(p); | |
1457 | if (!t) | |
1458 | return -ENXIO; | |
1459 | ||
1460 | /* And now it looks pretty much the same as for a system | |
1461 | * slice, so let's just use the same parser from here on. */ | |
1462 | return cg_path_get_slice(t, slice); | |
1463 | } | |
1464 | ||
1465 | int cg_pid_get_user_slice(pid_t pid, char **slice) { | |
1466 | _cleanup_free_ char *cgroup = NULL; | |
1467 | int r; | |
1468 | ||
1469 | assert(slice); | |
1470 | ||
1471 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1472 | if (r < 0) | |
1473 | return r; | |
1474 | ||
1475 | return cg_path_get_user_slice(cgroup, slice); | |
1476 | } | |
1477 | ||
ae018d9b LP |
1478 | char *cg_escape(const char *p) { |
1479 | bool need_prefix = false; | |
1480 | ||
1481 | /* This implements very minimal escaping for names to be used | |
1482 | * as file names in the cgroup tree: any name which might | |
1483 | * conflict with a kernel name or is prefixed with '_' is | |
1484 | * prefixed with a '_'. That way, when reading cgroup names it | |
1485 | * is sufficient to remove a single prefixing underscore if | |
1486 | * there is one. */ | |
1487 | ||
1488 | /* The return value of this function (unlike cg_unescape()) | |
1489 | * needs free()! */ | |
1490 | ||
4c701096 | 1491 | if (IN_SET(p[0], 0, '_', '.') || |
0cbd293e | 1492 | STR_IN_SET(p, "notify_on_release", "release_agent", "tasks") || |
efdb0237 | 1493 | startswith(p, "cgroup.")) |
ae018d9b LP |
1494 | need_prefix = true; |
1495 | else { | |
1496 | const char *dot; | |
1497 | ||
1498 | dot = strrchr(p, '.'); | |
1499 | if (dot) { | |
efdb0237 LP |
1500 | CGroupController c; |
1501 | size_t l = dot - p; | |
ae018d9b | 1502 | |
efdb0237 LP |
1503 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { |
1504 | const char *n; | |
1505 | ||
1506 | n = cgroup_controller_to_string(c); | |
ae018d9b | 1507 | |
efdb0237 LP |
1508 | if (l != strlen(n)) |
1509 | continue; | |
ae018d9b | 1510 | |
efdb0237 LP |
1511 | if (memcmp(p, n, l) != 0) |
1512 | continue; | |
1513 | ||
1514 | need_prefix = true; | |
1515 | break; | |
ae018d9b LP |
1516 | } |
1517 | } | |
1518 | } | |
1519 | ||
1520 | if (need_prefix) | |
b910cc72 | 1521 | return strjoin("_", p); |
efdb0237 LP |
1522 | |
1523 | return strdup(p); | |
ae018d9b LP |
1524 | } |
1525 | ||
1526 | char *cg_unescape(const char *p) { | |
1527 | assert(p); | |
1528 | ||
1529 | /* The return value of this function (unlike cg_escape()) | |
1530 | * doesn't need free()! */ | |
1531 | ||
1532 | if (p[0] == '_') | |
1533 | return (char*) p+1; | |
1534 | ||
1535 | return (char*) p; | |
1536 | } | |
78edb35a LP |
1537 | |
1538 | #define CONTROLLER_VALID \ | |
4b549144 | 1539 | DIGITS LETTERS \ |
78edb35a LP |
1540 | "_" |
1541 | ||
185a0874 | 1542 | bool cg_controller_is_valid(const char *p) { |
78edb35a LP |
1543 | const char *t, *s; |
1544 | ||
1545 | if (!p) | |
1546 | return false; | |
1547 | ||
b6629c4b TH |
1548 | if (streq(p, SYSTEMD_CGROUP_CONTROLLER)) |
1549 | return true; | |
1550 | ||
185a0874 DJL |
1551 | s = startswith(p, "name="); |
1552 | if (s) | |
1553 | p = s; | |
78edb35a | 1554 | |
4c701096 | 1555 | if (IN_SET(*p, 0, '_')) |
78edb35a LP |
1556 | return false; |
1557 | ||
1558 | for (t = p; *t; t++) | |
1559 | if (!strchr(CONTROLLER_VALID, *t)) | |
1560 | return false; | |
1561 | ||
8ca94009 | 1562 | if (t - p > NAME_MAX) |
78edb35a LP |
1563 | return false; |
1564 | ||
1565 | return true; | |
1566 | } | |
a016b922 LP |
1567 | |
1568 | int cg_slice_to_path(const char *unit, char **ret) { | |
1569 | _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL; | |
1570 | const char *dash; | |
7410616c | 1571 | int r; |
a016b922 LP |
1572 | |
1573 | assert(unit); | |
1574 | assert(ret); | |
1575 | ||
e5d855d3 | 1576 | if (streq(unit, SPECIAL_ROOT_SLICE)) { |
c96cc582 LP |
1577 | char *x; |
1578 | ||
1579 | x = strdup(""); | |
1580 | if (!x) | |
1581 | return -ENOMEM; | |
1582 | *ret = x; | |
1583 | return 0; | |
1584 | } | |
1585 | ||
7410616c | 1586 | if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN)) |
a016b922 LP |
1587 | return -EINVAL; |
1588 | ||
1589 | if (!endswith(unit, ".slice")) | |
1590 | return -EINVAL; | |
1591 | ||
7410616c LP |
1592 | r = unit_name_to_prefix(unit, &p); |
1593 | if (r < 0) | |
1594 | return r; | |
a016b922 LP |
1595 | |
1596 | dash = strchr(p, '-'); | |
e66e5b61 LP |
1597 | |
1598 | /* Don't allow initial dashes */ | |
1599 | if (dash == p) | |
1600 | return -EINVAL; | |
1601 | ||
a016b922 LP |
1602 | while (dash) { |
1603 | _cleanup_free_ char *escaped = NULL; | |
1604 | char n[dash - p + sizeof(".slice")]; | |
1605 | ||
989290db | 1606 | #if HAS_FEATURE_MEMORY_SANITIZER |
1c56d501 | 1607 | /* msan doesn't instrument stpncpy, so it thinks |
5238e957 | 1608 | * n is later used uninitialized: |
1c56d501 ZJS |
1609 | * https://github.com/google/sanitizers/issues/926 |
1610 | */ | |
1611 | zero(n); | |
1612 | #endif | |
1613 | ||
e66e5b61 | 1614 | /* Don't allow trailing or double dashes */ |
4c701096 | 1615 | if (IN_SET(dash[1], 0, '-')) |
c96cc582 | 1616 | return -EINVAL; |
a016b922 | 1617 | |
c96cc582 | 1618 | strcpy(stpncpy(n, p, dash - p), ".slice"); |
7410616c | 1619 | if (!unit_name_is_valid(n, UNIT_NAME_PLAIN)) |
a016b922 LP |
1620 | return -EINVAL; |
1621 | ||
1622 | escaped = cg_escape(n); | |
1623 | if (!escaped) | |
1624 | return -ENOMEM; | |
1625 | ||
c2bc710b | 1626 | if (!strextend(&s, escaped, "/")) |
a016b922 LP |
1627 | return -ENOMEM; |
1628 | ||
1629 | dash = strchr(dash+1, '-'); | |
1630 | } | |
1631 | ||
1632 | e = cg_escape(unit); | |
1633 | if (!e) | |
1634 | return -ENOMEM; | |
1635 | ||
c2bc710b | 1636 | if (!strextend(&s, e)) |
a016b922 LP |
1637 | return -ENOMEM; |
1638 | ||
ae2a15bc | 1639 | *ret = TAKE_PTR(s); |
a016b922 LP |
1640 | |
1641 | return 0; | |
1642 | } | |
4ad49000 LP |
1643 | |
1644 | int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) { | |
1645 | _cleanup_free_ char *p = NULL; | |
1646 | int r; | |
1647 | ||
1648 | r = cg_get_path(controller, path, attribute, &p); | |
1649 | if (r < 0) | |
1650 | return r; | |
1651 | ||
604028de | 1652 | return write_string_file(p, value, WRITE_STRING_FILE_DISABLE_BUFFER); |
4ad49000 LP |
1653 | } |
1654 | ||
934277fe LP |
1655 | int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) { |
1656 | _cleanup_free_ char *p = NULL; | |
1657 | int r; | |
1658 | ||
1659 | r = cg_get_path(controller, path, attribute, &p); | |
1660 | if (r < 0) | |
1661 | return r; | |
1662 | ||
1663 | return read_one_line_file(p, ret); | |
1664 | } | |
1665 | ||
613328c3 AZ |
1666 | int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret) { |
1667 | _cleanup_free_ char *value = NULL; | |
1668 | uint64_t v; | |
1669 | int r; | |
1670 | ||
1671 | assert(ret); | |
1672 | ||
1673 | r = cg_get_attribute(controller, path, attribute, &value); | |
1674 | if (r == -ENOENT) | |
1675 | return -ENODATA; | |
1676 | if (r < 0) | |
1677 | return r; | |
1678 | ||
1679 | if (streq(value, "max")) { | |
1680 | *ret = CGROUP_LIMIT_MAX; | |
1681 | return 0; | |
1682 | } | |
1683 | ||
1684 | r = safe_atou64(value, &v); | |
1685 | if (r < 0) | |
1686 | return r; | |
1687 | ||
1688 | *ret = v; | |
1689 | return 0; | |
1690 | } | |
1691 | ||
b41dcc51 AZ |
1692 | int cg_get_attribute_as_bool(const char *controller, const char *path, const char *attribute, bool *ret) { |
1693 | _cleanup_free_ char *value = NULL; | |
1694 | int r; | |
1695 | ||
1696 | assert(ret); | |
1697 | ||
1698 | r = cg_get_attribute(controller, path, attribute, &value); | |
1699 | if (r == -ENOENT) | |
1700 | return -ENODATA; | |
1701 | if (r < 0) | |
1702 | return r; | |
1703 | ||
1704 | r = parse_boolean(value); | |
1705 | if (r < 0) | |
1706 | return r; | |
1707 | ||
1708 | *ret = r; | |
1709 | return 0; | |
1710 | } | |
1711 | ||
59331b8e AZ |
1712 | int cg_get_owner(const char *controller, const char *path, uid_t *ret_uid) { |
1713 | _cleanup_free_ char *f = NULL; | |
1714 | struct stat stats; | |
1715 | int r; | |
1716 | ||
1717 | assert(ret_uid); | |
1718 | ||
1719 | r = cg_get_path(controller, path, NULL, &f); | |
1720 | if (r < 0) | |
1721 | return r; | |
1722 | ||
1723 | r = stat(f, &stats); | |
1724 | if (r < 0) | |
1725 | return -errno; | |
1726 | ||
1727 | *ret_uid = stats.st_uid; | |
1728 | return 0; | |
1729 | } | |
1730 | ||
25a1f04c | 1731 | int cg_get_keyed_attribute_full( |
b734a4ff LP |
1732 | const char *controller, |
1733 | const char *path, | |
1734 | const char *attribute, | |
1735 | char **keys, | |
25a1f04c MS |
1736 | char **ret_values, |
1737 | CGroupKeyMode mode) { | |
66ebf6c0 | 1738 | |
b734a4ff | 1739 | _cleanup_free_ char *filename = NULL, *contents = NULL; |
b734a4ff | 1740 | const char *p; |
9177fa9f | 1741 | size_t n, i, n_done = 0; |
b734a4ff LP |
1742 | char **v; |
1743 | int r; | |
1744 | ||
4e1dfa45 | 1745 | /* Reads one or more fields of a cgroup v2 keyed attribute file. The 'keys' parameter should be an strv with |
b734a4ff LP |
1746 | * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of |
1747 | * entries as 'keys'. On success each entry will be set to the value of the matching key. | |
1748 | * | |
d9e45bc3 MS |
1749 | * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. If mode |
1750 | * is set to GG_KEY_MODE_GRACEFUL we ignore missing keys and return those that were parsed successfully. */ | |
66ebf6c0 TH |
1751 | |
1752 | r = cg_get_path(controller, path, attribute, &filename); | |
1753 | if (r < 0) | |
1754 | return r; | |
1755 | ||
b734a4ff | 1756 | r = read_full_file(filename, &contents, NULL); |
66ebf6c0 TH |
1757 | if (r < 0) |
1758 | return r; | |
1759 | ||
b734a4ff LP |
1760 | n = strv_length(keys); |
1761 | if (n == 0) /* No keys to retrieve? That's easy, we are done then */ | |
1762 | return 0; | |
66ebf6c0 | 1763 | |
b734a4ff LP |
1764 | /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */ |
1765 | v = newa0(char*, n); | |
66ebf6c0 | 1766 | |
b734a4ff LP |
1767 | for (p = contents; *p;) { |
1768 | const char *w = NULL; | |
b734a4ff | 1769 | |
9177fa9f ZJS |
1770 | for (i = 0; i < n; i++) |
1771 | if (!v[i]) { | |
b734a4ff LP |
1772 | w = first_word(p, keys[i]); |
1773 | if (w) | |
1774 | break; | |
66ebf6c0 | 1775 | } |
66ebf6c0 | 1776 | |
b734a4ff | 1777 | if (w) { |
b734a4ff LP |
1778 | size_t l; |
1779 | ||
1780 | l = strcspn(w, NEWLINE); | |
9177fa9f ZJS |
1781 | v[i] = strndup(w, l); |
1782 | if (!v[i]) { | |
b734a4ff LP |
1783 | r = -ENOMEM; |
1784 | goto fail; | |
66ebf6c0 | 1785 | } |
b734a4ff | 1786 | |
b734a4ff | 1787 | n_done++; |
b734a4ff LP |
1788 | if (n_done >= n) |
1789 | goto done; | |
1790 | ||
1791 | p = w + l; | |
9177fa9f | 1792 | } else |
b734a4ff | 1793 | p += strcspn(p, NEWLINE); |
b734a4ff LP |
1794 | |
1795 | p += strspn(p, NEWLINE); | |
66ebf6c0 TH |
1796 | } |
1797 | ||
25a1f04c MS |
1798 | if (mode & CG_KEY_MODE_GRACEFUL) |
1799 | goto done; | |
d9e45bc3 MS |
1800 | |
1801 | r = -ENXIO; | |
b734a4ff LP |
1802 | |
1803 | fail: | |
1804 | for (i = 0; i < n; i++) | |
1805 | free(v[i]); | |
1806 | ||
1807 | return r; | |
1808 | ||
1809 | done: | |
1810 | memcpy(ret_values, v, sizeof(char*) * n); | |
25a1f04c MS |
1811 | if (mode & CG_KEY_MODE_GRACEFUL) |
1812 | return n_done; | |
1813 | ||
66ebf6c0 | 1814 | return 0; |
4ad49000 LP |
1815 | } |
1816 | ||
aae7e17f | 1817 | int cg_mask_to_string(CGroupMask mask, char **ret) { |
ec635a2d LP |
1818 | _cleanup_free_ char *s = NULL; |
1819 | size_t n = 0, allocated = 0; | |
1820 | bool space = false; | |
aae7e17f | 1821 | CGroupController c; |
aae7e17f FB |
1822 | |
1823 | assert(ret); | |
1824 | ||
1825 | if (mask == 0) { | |
1826 | *ret = NULL; | |
1827 | return 0; | |
1828 | } | |
1829 | ||
1830 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
ec635a2d LP |
1831 | const char *k; |
1832 | size_t l; | |
aae7e17f | 1833 | |
f99850a0 | 1834 | if (!FLAGS_SET(mask, CGROUP_CONTROLLER_TO_MASK(c))) |
aae7e17f FB |
1835 | continue; |
1836 | ||
ec635a2d LP |
1837 | k = cgroup_controller_to_string(c); |
1838 | l = strlen(k); | |
1839 | ||
1840 | if (!GREEDY_REALLOC(s, allocated, n + space + l + 1)) | |
1841 | return -ENOMEM; | |
1842 | ||
1843 | if (space) | |
1844 | s[n] = ' '; | |
1845 | memcpy(s + n + space, k, l); | |
1846 | n += space + l; | |
1847 | ||
1848 | space = true; | |
aae7e17f FB |
1849 | } |
1850 | ||
ec635a2d | 1851 | assert(s); |
aae7e17f | 1852 | |
ec635a2d | 1853 | s[n] = 0; |
ae2a15bc | 1854 | *ret = TAKE_PTR(s); |
ec635a2d | 1855 | |
aae7e17f FB |
1856 | return 0; |
1857 | } | |
1858 | ||
38a90d45 LP |
1859 | int cg_mask_from_string(const char *value, CGroupMask *ret) { |
1860 | CGroupMask m = 0; | |
1861 | ||
1862 | assert(ret); | |
aae7e17f FB |
1863 | assert(value); |
1864 | ||
1865 | for (;;) { | |
1866 | _cleanup_free_ char *n = NULL; | |
1867 | CGroupController v; | |
1868 | int r; | |
1869 | ||
1870 | r = extract_first_word(&value, &n, NULL, 0); | |
1871 | if (r < 0) | |
1872 | return r; | |
1873 | if (r == 0) | |
1874 | break; | |
1875 | ||
1876 | v = cgroup_controller_from_string(n); | |
1877 | if (v < 0) | |
1878 | continue; | |
1879 | ||
38a90d45 | 1880 | m |= CGROUP_CONTROLLER_TO_MASK(v); |
aae7e17f | 1881 | } |
38a90d45 LP |
1882 | |
1883 | *ret = m; | |
aae7e17f FB |
1884 | return 0; |
1885 | } | |
1886 | ||
0fa7b500 | 1887 | int cg_mask_supported_subtree(const char *root, CGroupMask *ret) { |
38a90d45 | 1888 | CGroupMask mask; |
415fc41c | 1889 | int r; |
efdb0237 | 1890 | |
67558d15 LP |
1891 | /* Determines the mask of supported cgroup controllers. Only includes controllers we can make sense of and that |
1892 | * are actually accessible. Only covers real controllers, i.e. not the CGROUP_CONTROLLER_BPF_xyz | |
1893 | * pseudo-controllers. */ | |
4ad49000 | 1894 | |
b4cccbc1 LP |
1895 | r = cg_all_unified(); |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | if (r > 0) { | |
0fa7b500 | 1899 | _cleanup_free_ char *controllers = NULL, *path = NULL; |
efdb0237 | 1900 | |
d51c4fca YW |
1901 | /* In the unified hierarchy we can read the supported and accessible controllers from |
1902 | * the top-level cgroup attribute */ | |
efdb0237 | 1903 | |
5f4c5fef LP |
1904 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path); |
1905 | if (r < 0) | |
1906 | return r; | |
1907 | ||
1908 | r = read_one_line_file(path, &controllers); | |
efdb0237 LP |
1909 | if (r < 0) |
1910 | return r; | |
4ad49000 | 1911 | |
aae7e17f FB |
1912 | r = cg_mask_from_string(controllers, &mask); |
1913 | if (r < 0) | |
1914 | return r; | |
efdb0237 | 1915 | |
1fbbb526 | 1916 | /* Mask controllers that are not supported in unified hierarchy. */ |
03afd780 | 1917 | mask &= CGROUP_MASK_V2; |
efdb0237 LP |
1918 | |
1919 | } else { | |
1920 | CGroupController c; | |
1921 | ||
0fa7b500 | 1922 | /* In the legacy hierarchy, we check which hierarchies are accessible. */ |
efdb0237 | 1923 | |
38a90d45 | 1924 | mask = 0; |
efdb0237 | 1925 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { |
03afd780 | 1926 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); |
efdb0237 LP |
1927 | const char *n; |
1928 | ||
03afd780 LP |
1929 | if (!FLAGS_SET(CGROUP_MASK_V1, bit)) |
1930 | continue; | |
1931 | ||
efdb0237 | 1932 | n = cgroup_controller_to_string(c); |
0fa7b500 | 1933 | if (controller_is_v1_accessible(root, n) >= 0) |
03afd780 | 1934 | mask |= bit; |
efdb0237 | 1935 | } |
4ad49000 LP |
1936 | } |
1937 | ||
efdb0237 LP |
1938 | *ret = mask; |
1939 | return 0; | |
4ad49000 | 1940 | } |
b12afc8c | 1941 | |
0fa7b500 MK |
1942 | int cg_mask_supported(CGroupMask *ret) { |
1943 | _cleanup_free_ char *root = NULL; | |
1944 | int r; | |
1945 | ||
1946 | r = cg_get_root_path(&root); | |
1947 | if (r < 0) | |
1948 | return r; | |
1949 | ||
1950 | return cg_mask_supported_subtree(root, ret); | |
1951 | } | |
1952 | ||
6925a0de LP |
1953 | int cg_kernel_controllers(Set **ret) { |
1954 | _cleanup_set_free_free_ Set *controllers = NULL; | |
b12afc8c | 1955 | _cleanup_fclose_ FILE *f = NULL; |
b12afc8c LP |
1956 | int r; |
1957 | ||
6925a0de | 1958 | assert(ret); |
b12afc8c | 1959 | |
f09e86bc LS |
1960 | /* Determines the full list of kernel-known controllers. Might include controllers we don't actually support |
1961 | * and controllers that aren't currently accessible (because not mounted). This does not include "name=" | |
1962 | * pseudo-controllers. */ | |
e155a0aa | 1963 | |
6925a0de LP |
1964 | controllers = set_new(&string_hash_ops); |
1965 | if (!controllers) | |
1966 | return -ENOMEM; | |
1967 | ||
fdeea3f4 ZJS |
1968 | r = fopen_unlocked("/proc/cgroups", "re", &f); |
1969 | if (r == -ENOENT) { | |
1970 | *ret = NULL; | |
1971 | return 0; | |
b12afc8c | 1972 | } |
fdeea3f4 ZJS |
1973 | if (r < 0) |
1974 | return r; | |
35bbbf85 | 1975 | |
b12afc8c | 1976 | /* Ignore the header line */ |
f5fbe71d | 1977 | (void) read_line(f, SIZE_MAX, NULL); |
b12afc8c LP |
1978 | |
1979 | for (;;) { | |
1980 | char *controller; | |
1981 | int enabled = 0; | |
1982 | ||
1983 | errno = 0; | |
1984 | if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) { | |
1985 | ||
1986 | if (feof(f)) | |
1987 | break; | |
1988 | ||
66855de7 LP |
1989 | if (ferror(f)) |
1990 | return errno_or_else(EIO); | |
b12afc8c LP |
1991 | |
1992 | return -EBADMSG; | |
1993 | } | |
1994 | ||
1995 | if (!enabled) { | |
1996 | free(controller); | |
1997 | continue; | |
1998 | } | |
1999 | ||
efdb0237 | 2000 | if (!cg_controller_is_valid(controller)) { |
b12afc8c LP |
2001 | free(controller); |
2002 | return -EBADMSG; | |
2003 | } | |
2004 | ||
2005 | r = set_consume(controllers, controller); | |
2006 | if (r < 0) | |
2007 | return r; | |
2008 | } | |
2009 | ||
1cc6c93a | 2010 | *ret = TAKE_PTR(controllers); |
6925a0de | 2011 | |
b12afc8c LP |
2012 | return 0; |
2013 | } | |
efdb0237 | 2014 | |
d4d99bc6 ZJS |
2015 | /* The hybrid mode was initially implemented in v232 and simply mounted cgroup2 on |
2016 | * /sys/fs/cgroup/systemd. This unfortunately broke other tools (such as docker) which expected the v1 | |
2017 | * "name=systemd" hierarchy on /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mounts v2 on | |
2018 | * /sys/fs/cgroup/unified and maintains "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility | |
2019 | * with other tools. | |
f08e9287 | 2020 | * |
d4d99bc6 ZJS |
2021 | * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep |
2022 | * cgroup v2 process management but disable the compat dual layout, we return true on | |
2023 | * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and false on cg_hybrid_unified(). | |
f08e9287 TH |
2024 | */ |
2025 | static thread_local bool unified_systemd_v232; | |
2026 | ||
d4d99bc6 ZJS |
2027 | int cg_unified_cached(bool flush) { |
2028 | static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN; | |
efdb0237 | 2029 | |
efdb0237 LP |
2030 | struct statfs fs; |
2031 | ||
2032 | /* Checks if we support the unified hierarchy. Returns an | |
2033 | * error when the cgroup hierarchies aren't mounted yet or we | |
2034 | * have any other trouble determining if the unified hierarchy | |
2035 | * is supported. */ | |
2036 | ||
d4d99bc6 ZJS |
2037 | if (flush) |
2038 | unified_cache = CGROUP_UNIFIED_UNKNOWN; | |
2039 | else if (unified_cache >= CGROUP_UNIFIED_NONE) | |
2040 | return unified_cache; | |
efdb0237 LP |
2041 | |
2042 | if (statfs("/sys/fs/cgroup/", &fs) < 0) | |
c028bed1 | 2043 | return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\") failed: %m"); |
efdb0237 | 2044 | |
9aa21133 ZJS |
2045 | if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { |
2046 | log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy"); | |
5da38d07 | 2047 | unified_cache = CGROUP_UNIFIED_ALL; |
9aa21133 | 2048 | } else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) { |
2977724b | 2049 | if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 && |
f08e9287 | 2050 | F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { |
9aa21133 | 2051 | log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller"); |
2977724b | 2052 | unified_cache = CGROUP_UNIFIED_SYSTEMD; |
f08e9287 | 2053 | unified_systemd_v232 = false; |
f08e9287 | 2054 | } else { |
2156061f MG |
2055 | if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0) { |
2056 | if (errno == ENOENT) { | |
2057 | /* Some other software may have set up /sys/fs/cgroup in a configuration we do not recognize. */ | |
2058 | log_debug_errno(errno, "Unsupported cgroupsv1 setup detected: name=systemd hierarchy not found."); | |
2059 | return -ENOMEDIUM; | |
2060 | } | |
9aa21133 | 2061 | return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m"); |
2156061f | 2062 | } |
5535d8f7 EV |
2063 | |
2064 | if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { | |
2065 | log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)"); | |
2066 | unified_cache = CGROUP_UNIFIED_SYSTEMD; | |
2067 | unified_systemd_v232 = true; | |
2068 | } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)) { | |
2069 | log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy"); | |
2070 | unified_cache = CGROUP_UNIFIED_NONE; | |
2071 | } else { | |
2072 | log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy", | |
9aa21133 | 2073 | (unsigned long long) fs.f_type); |
5535d8f7 | 2074 | unified_cache = CGROUP_UNIFIED_NONE; |
9aa21133 | 2075 | } |
2977724b | 2076 | } |
0bc5f001 DS |
2077 | } else if (F_TYPE_EQUAL(fs.f_type, SYSFS_MAGIC)) { |
2078 | return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), | |
2079 | "No filesystem is currently mounted on /sys/fs/cgroup."); | |
baaa35ad ZJS |
2080 | } else |
2081 | return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), | |
2082 | "Unknown filesystem type %llx mounted on /sys/fs/cgroup.", | |
2083 | (unsigned long long)fs.f_type); | |
efdb0237 | 2084 | |
d4d99bc6 | 2085 | return unified_cache; |
5da38d07 TH |
2086 | } |
2087 | ||
c22800e4 | 2088 | int cg_unified_controller(const char *controller) { |
b4cccbc1 | 2089 | int r; |
5da38d07 | 2090 | |
d4d99bc6 | 2091 | r = cg_unified_cached(false); |
b4cccbc1 LP |
2092 | if (r < 0) |
2093 | return r; | |
5da38d07 | 2094 | |
d4d99bc6 | 2095 | if (r == CGROUP_UNIFIED_NONE) |
fc9ae717 LP |
2096 | return false; |
2097 | ||
d4d99bc6 | 2098 | if (r >= CGROUP_UNIFIED_ALL) |
fc9ae717 LP |
2099 | return true; |
2100 | ||
2101 | return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER); | |
5da38d07 TH |
2102 | } |
2103 | ||
b4cccbc1 | 2104 | int cg_all_unified(void) { |
4bb652ac LP |
2105 | int r; |
2106 | ||
d4d99bc6 | 2107 | r = cg_unified_cached(false); |
4bb652ac LP |
2108 | if (r < 0) |
2109 | return r; | |
2110 | ||
d4d99bc6 | 2111 | return r >= CGROUP_UNIFIED_ALL; |
efdb0237 LP |
2112 | } |
2113 | ||
b4cccbc1 LP |
2114 | int cg_hybrid_unified(void) { |
2115 | int r; | |
2977724b | 2116 | |
d4d99bc6 | 2117 | r = cg_unified_cached(false); |
b4cccbc1 LP |
2118 | if (r < 0) |
2119 | return r; | |
2977724b | 2120 | |
d4d99bc6 | 2121 | return r == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232; |
13c31542 TH |
2122 | } |
2123 | ||
9be57249 TH |
2124 | const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = { |
2125 | [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX, | |
2126 | [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX, | |
ac06a0cf TH |
2127 | [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX, |
2128 | [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX, | |
9be57249 TH |
2129 | }; |
2130 | ||
2131 | static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = { | |
2132 | [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax", | |
2133 | [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax", | |
ac06a0cf TH |
2134 | [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax", |
2135 | [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax", | |
9be57249 TH |
2136 | }; |
2137 | ||
2138 | DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType); | |
2139 | ||
f0bef277 EV |
2140 | bool is_cgroup_fs(const struct statfs *s) { |
2141 | return is_fs_type(s, CGROUP_SUPER_MAGIC) || | |
2142 | is_fs_type(s, CGROUP2_SUPER_MAGIC); | |
2143 | } | |
2144 | ||
2145 | bool fd_is_cgroup_fs(int fd) { | |
2146 | struct statfs s; | |
2147 | ||
2148 | if (fstatfs(fd, &s) < 0) | |
2149 | return -errno; | |
2150 | ||
2151 | return is_cgroup_fs(&s); | |
2152 | } | |
2153 | ||
b82f71c7 | 2154 | static const char *const cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = { |
efdb0237 LP |
2155 | [CGROUP_CONTROLLER_CPU] = "cpu", |
2156 | [CGROUP_CONTROLLER_CPUACCT] = "cpuacct", | |
047f5d63 | 2157 | [CGROUP_CONTROLLER_CPUSET] = "cpuset", |
13c31542 | 2158 | [CGROUP_CONTROLLER_IO] = "io", |
efdb0237 LP |
2159 | [CGROUP_CONTROLLER_BLKIO] = "blkio", |
2160 | [CGROUP_CONTROLLER_MEMORY] = "memory", | |
3905f127 | 2161 | [CGROUP_CONTROLLER_DEVICES] = "devices", |
03a7b521 | 2162 | [CGROUP_CONTROLLER_PIDS] = "pids", |
17f14955 | 2163 | [CGROUP_CONTROLLER_BPF_FIREWALL] = "bpf-firewall", |
084c7007 | 2164 | [CGROUP_CONTROLLER_BPF_DEVICES] = "bpf-devices", |
efdb0237 LP |
2165 | }; |
2166 | ||
2167 | DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController); | |
f98c2585 CD |
2168 | |
2169 | CGroupMask get_cpu_accounting_mask(void) { | |
2170 | static CGroupMask needed_mask = (CGroupMask) -1; | |
2171 | ||
2172 | /* On kernel ≥4.15 with unified hierarchy, cpu.stat's usage_usec is | |
2173 | * provided externally from the CPU controller, which means we don't | |
2174 | * need to enable the CPU controller just to get metrics. This is good, | |
2175 | * because enabling the CPU controller comes at a minor performance | |
2176 | * hit, especially when it's propagated deep into large hierarchies. | |
2177 | * There's also no separate CPU accounting controller available within | |
2178 | * a unified hierarchy. | |
2179 | * | |
2180 | * This combination of factors results in the desired cgroup mask to | |
2181 | * enable for CPU accounting varying as follows: | |
2182 | * | |
2183 | * ╔═════════════════════╤═════════════════════╗ | |
2184 | * ║ Linux ≥4.15 │ Linux <4.15 ║ | |
2185 | * ╔═══════════════╬═════════════════════╪═════════════════════╣ | |
2186 | * ║ Unified ║ nothing │ CGROUP_MASK_CPU ║ | |
2187 | * ╟───────────────╫─────────────────────┼─────────────────────╢ | |
2188 | * ║ Hybrid/Legacy ║ CGROUP_MASK_CPUACCT │ CGROUP_MASK_CPUACCT ║ | |
2189 | * ╚═══════════════╩═════════════════════╧═════════════════════╝ | |
2190 | * | |
2191 | * We check kernel version here instead of manually checking whether | |
2192 | * cpu.stat is present for every cgroup, as that check in itself would | |
2193 | * already be fairly expensive. | |
2194 | * | |
2195 | * Kernels where this patch has been backported will therefore have the | |
2196 | * CPU controller enabled unnecessarily. This is more expensive than | |
2197 | * necessary, but harmless. ☺️ | |
2198 | */ | |
2199 | ||
2200 | if (needed_mask == (CGroupMask) -1) { | |
2201 | if (cg_all_unified()) { | |
2202 | struct utsname u; | |
2203 | assert_se(uname(&u) >= 0); | |
2204 | ||
8087644a | 2205 | if (strverscmp_improved(u.release, "4.15") < 0) |
f98c2585 CD |
2206 | needed_mask = CGROUP_MASK_CPU; |
2207 | else | |
2208 | needed_mask = 0; | |
2209 | } else | |
2210 | needed_mask = CGROUP_MASK_CPUACCT; | |
2211 | } | |
2212 | ||
2213 | return needed_mask; | |
2214 | } | |
2215 | ||
2216 | bool cpu_accounting_is_cheap(void) { | |
2217 | return get_cpu_accounting_mask() == 0; | |
2218 | } | |
4d824a4e AZ |
2219 | |
2220 | static const char* const managed_oom_mode_table[_MANAGED_OOM_MODE_MAX] = { | |
2221 | [MANAGED_OOM_AUTO] = "auto", | |
2222 | [MANAGED_OOM_KILL] = "kill", | |
2223 | }; | |
2224 | ||
2225 | DEFINE_STRING_TABLE_LOOKUP(managed_oom_mode, ManagedOOMMode); | |
242d75bd AZ |
2226 | |
2227 | static const char* const managed_oom_preference_table[_MANAGED_OOM_PREFERENCE_MAX] = { | |
2228 | [MANAGED_OOM_PREFERENCE_NONE] = "none", | |
2229 | [MANAGED_OOM_PREFERENCE_AVOID] = "avoid", | |
2230 | [MANAGED_OOM_PREFERENCE_OMIT] = "omit", | |
2231 | }; | |
2232 | ||
2233 | DEFINE_STRING_TABLE_LOOKUP(managed_oom_preference, ManagedOOMPreference); |