]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 | ||
103 | r = read_full_file(events, &content, NULL); | |
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 | |
efdb0237 | 530 | static int controller_is_accessible(const char *controller) { |
b4cccbc1 | 531 | int r; |
37099707 | 532 | |
efdb0237 | 533 | assert(controller); |
37099707 | 534 | |
efdb0237 LP |
535 | /* Checks whether a specific controller is accessible, |
536 | * i.e. its hierarchy mounted. In the unified hierarchy all | |
537 | * controllers are considered accessible, except for the named | |
538 | * hierarchies */ | |
b12afc8c | 539 | |
efdb0237 LP |
540 | if (!cg_controller_is_valid(controller)) |
541 | return -EINVAL; | |
542 | ||
b4cccbc1 LP |
543 | r = cg_all_unified(); |
544 | if (r < 0) | |
545 | return r; | |
546 | if (r > 0) { | |
efdb0237 LP |
547 | /* We don't support named hierarchies if we are using |
548 | * the unified hierarchy. */ | |
549 | ||
550 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) | |
551 | return 0; | |
552 | ||
553 | if (startswith(controller, "name=")) | |
554 | return -EOPNOTSUPP; | |
555 | ||
556 | } else { | |
557 | const char *cc, *dn; | |
558 | ||
559 | dn = controller_to_dirname(controller); | |
560 | cc = strjoina("/sys/fs/cgroup/", dn); | |
561 | ||
562 | if (laccess(cc, F_OK) < 0) | |
563 | return -errno; | |
564 | } | |
37099707 LP |
565 | |
566 | return 0; | |
567 | } | |
568 | ||
3474ae3c | 569 | int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) { |
37099707 | 570 | int r; |
dbd821ac | 571 | |
efdb0237 | 572 | assert(controller); |
3474ae3c | 573 | assert(fs); |
70132bd0 | 574 | |
efdb0237 LP |
575 | /* Check if the specified controller is actually accessible */ |
576 | r = controller_is_accessible(controller); | |
37099707 LP |
577 | if (r < 0) |
578 | return r; | |
3474ae3c | 579 | |
efdb0237 | 580 | return cg_get_path(controller, path, suffix, fs); |
8c6db833 LP |
581 | } |
582 | ||
4b58153d LP |
583 | int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) { |
584 | _cleanup_free_ char *fs = NULL; | |
585 | int r; | |
586 | ||
587 | assert(path); | |
588 | assert(name); | |
589 | assert(value || size <= 0); | |
590 | ||
591 | r = cg_get_path(controller, path, NULL, &fs); | |
592 | if (r < 0) | |
593 | return r; | |
594 | ||
595 | if (setxattr(fs, name, value, size, flags) < 0) | |
596 | return -errno; | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) { | |
602 | _cleanup_free_ char *fs = NULL; | |
603 | ssize_t n; | |
604 | int r; | |
605 | ||
606 | assert(path); | |
607 | assert(name); | |
608 | ||
609 | r = cg_get_path(controller, path, NULL, &fs); | |
610 | if (r < 0) | |
611 | return r; | |
612 | ||
613 | n = getxattr(fs, name, value, size); | |
614 | if (n < 0) | |
615 | return -errno; | |
616 | ||
617 | return (int) n; | |
618 | } | |
619 | ||
baa358df AZ |
620 | int cg_get_xattr_malloc(const char *controller, const char *path, const char *name, char **ret) { |
621 | _cleanup_free_ char *fs = NULL; | |
622 | int r; | |
623 | ||
624 | assert(path); | |
625 | assert(name); | |
626 | ||
627 | r = cg_get_path(controller, path, NULL, &fs); | |
628 | if (r < 0) | |
629 | return r; | |
630 | ||
631 | r = getxattr_malloc(fs, name, ret, false); | |
632 | if (r < 0) | |
633 | return r; | |
634 | ||
635 | return r; | |
636 | } | |
637 | ||
bf25f165 LP |
638 | int cg_remove_xattr(const char *controller, const char *path, const char *name) { |
639 | _cleanup_free_ char *fs = NULL; | |
640 | int r; | |
641 | ||
642 | assert(path); | |
643 | assert(name); | |
644 | ||
645 | r = cg_get_path(controller, path, NULL, &fs); | |
646 | if (r < 0) | |
647 | return r; | |
648 | ||
649 | if (removexattr(fs, name) < 0) | |
650 | return -errno; | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
ae7ef63f | 655 | int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) { |
7027ff61 | 656 | _cleanup_fclose_ FILE *f = NULL; |
b6629c4b | 657 | const char *fs, *controller_str; |
d2b39cb6 | 658 | int unified, r; |
8c6db833 | 659 | |
c6c18be3 | 660 | assert(pid >= 0); |
ae7ef63f | 661 | assert(ret_path); |
8c6db833 | 662 | |
5da38d07 TH |
663 | if (controller) { |
664 | if (!cg_controller_is_valid(controller)) | |
665 | return -EINVAL; | |
666 | } else | |
667 | controller = SYSTEMD_CGROUP_CONTROLLER; | |
668 | ||
c22800e4 | 669 | unified = cg_unified_controller(controller); |
b4cccbc1 LP |
670 | if (unified < 0) |
671 | return unified; | |
672 | if (unified == 0) { | |
b6629c4b TH |
673 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) |
674 | controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY; | |
675 | else | |
676 | controller_str = controller; | |
b6629c4b | 677 | } |
7027ff61 | 678 | |
b68fa010 | 679 | fs = procfs_file_alloca(pid, "cgroup"); |
fdeea3f4 ZJS |
680 | r = fopen_unlocked(fs, "re", &f); |
681 | if (r == -ENOENT) | |
682 | return -ESRCH; | |
683 | if (r < 0) | |
684 | return r; | |
35bbbf85 | 685 | |
d2b39cb6 LP |
686 | for (;;) { |
687 | _cleanup_free_ char *line = NULL; | |
ae7ef63f | 688 | char *e; |
c6c18be3 | 689 | |
d2b39cb6 LP |
690 | r = read_line(f, LONG_LINE_MAX, &line); |
691 | if (r < 0) | |
692 | return r; | |
693 | if (r == 0) | |
ae7ef63f | 694 | return -ENODATA; |
c6c18be3 | 695 | |
efdb0237 LP |
696 | if (unified) { |
697 | e = startswith(line, "0:"); | |
698 | if (!e) | |
699 | continue; | |
c6c18be3 | 700 | |
efdb0237 LP |
701 | e = strchr(e, ':'); |
702 | if (!e) | |
703 | continue; | |
704 | } else { | |
705 | char *l; | |
efdb0237 LP |
706 | |
707 | l = strchr(line, ':'); | |
708 | if (!l) | |
709 | continue; | |
8af8afd6 | 710 | |
efdb0237 LP |
711 | l++; |
712 | e = strchr(l, ':'); | |
713 | if (!e) | |
714 | continue; | |
efdb0237 | 715 | *e = 0; |
ae7ef63f ZJS |
716 | |
717 | r = string_contains_word(l, ",", controller_str); | |
718 | if (r < 0) | |
719 | return r; | |
720 | if (r == 0) | |
efdb0237 | 721 | continue; |
8af8afd6 LP |
722 | } |
723 | ||
ae7ef63f ZJS |
724 | char *path = strdup(e + 1); |
725 | if (!path) | |
7027ff61 | 726 | return -ENOMEM; |
c6c18be3 | 727 | |
5e20b0a4 | 728 | /* Truncate suffix indicating the process is a zombie */ |
ae7ef63f | 729 | e = endswith(path, " (deleted)"); |
5e20b0a4 LP |
730 | if (e) |
731 | *e = 0; | |
732 | ||
ae7ef63f | 733 | *ret_path = path; |
7027ff61 | 734 | return 0; |
c6c18be3 | 735 | } |
8c6db833 LP |
736 | } |
737 | ||
738 | int cg_install_release_agent(const char *controller, const char *agent) { | |
7027ff61 | 739 | _cleanup_free_ char *fs = NULL, *contents = NULL; |
efdb0237 | 740 | const char *sc; |
415fc41c | 741 | int r; |
8c6db833 | 742 | |
8c6db833 LP |
743 | assert(agent); |
744 | ||
c22800e4 | 745 | r = cg_unified_controller(controller); |
b4cccbc1 LP |
746 | if (r < 0) |
747 | return r; | |
748 | if (r > 0) /* doesn't apply to unified hierarchy */ | |
efdb0237 LP |
749 | return -EOPNOTSUPP; |
750 | ||
7027ff61 LP |
751 | r = cg_get_path(controller, NULL, "release_agent", &fs); |
752 | if (r < 0) | |
c6c18be3 | 753 | return r; |
8c6db833 | 754 | |
7027ff61 LP |
755 | r = read_one_line_file(fs, &contents); |
756 | if (r < 0) | |
757 | return r; | |
8c6db833 LP |
758 | |
759 | sc = strstrip(contents); | |
e155a0aa | 760 | if (isempty(sc)) { |
604028de | 761 | r = write_string_file(fs, agent, WRITE_STRING_FILE_DISABLE_BUFFER); |
574d5f2d | 762 | if (r < 0) |
7027ff61 | 763 | return r; |
b8725df8 | 764 | } else if (!path_equal(sc, agent)) |
7027ff61 | 765 | return -EEXIST; |
8c6db833 | 766 | |
0da16248 | 767 | fs = mfree(fs); |
7027ff61 LP |
768 | r = cg_get_path(controller, NULL, "notify_on_release", &fs); |
769 | if (r < 0) | |
770 | return r; | |
8c6db833 | 771 | |
0da16248 | 772 | contents = mfree(contents); |
7027ff61 LP |
773 | r = read_one_line_file(fs, &contents); |
774 | if (r < 0) | |
775 | return r; | |
8c6db833 LP |
776 | |
777 | sc = strstrip(contents); | |
8c6db833 | 778 | if (streq(sc, "0")) { |
604028de | 779 | r = write_string_file(fs, "1", WRITE_STRING_FILE_DISABLE_BUFFER); |
7027ff61 LP |
780 | if (r < 0) |
781 | return r; | |
c6c18be3 | 782 | |
7027ff61 LP |
783 | return 1; |
784 | } | |
8c6db833 | 785 | |
7027ff61 LP |
786 | if (!streq(sc, "1")) |
787 | return -EIO; | |
8c6db833 | 788 | |
7027ff61 | 789 | return 0; |
8c6db833 LP |
790 | } |
791 | ||
ad929bcc KS |
792 | int cg_uninstall_release_agent(const char *controller) { |
793 | _cleanup_free_ char *fs = NULL; | |
415fc41c | 794 | int r; |
efdb0237 | 795 | |
c22800e4 | 796 | r = cg_unified_controller(controller); |
b4cccbc1 LP |
797 | if (r < 0) |
798 | return r; | |
799 | if (r > 0) /* Doesn't apply to unified hierarchy */ | |
efdb0237 | 800 | return -EOPNOTSUPP; |
ad929bcc | 801 | |
ac9ef333 LP |
802 | r = cg_get_path(controller, NULL, "notify_on_release", &fs); |
803 | if (r < 0) | |
804 | return r; | |
805 | ||
604028de | 806 | r = write_string_file(fs, "0", WRITE_STRING_FILE_DISABLE_BUFFER); |
ac9ef333 LP |
807 | if (r < 0) |
808 | return r; | |
809 | ||
0da16248 | 810 | fs = mfree(fs); |
ac9ef333 | 811 | |
ad929bcc KS |
812 | r = cg_get_path(controller, NULL, "release_agent", &fs); |
813 | if (r < 0) | |
814 | return r; | |
815 | ||
604028de | 816 | r = write_string_file(fs, "", WRITE_STRING_FILE_DISABLE_BUFFER); |
ad929bcc KS |
817 | if (r < 0) |
818 | return r; | |
819 | ||
ac9ef333 | 820 | return 0; |
ad929bcc KS |
821 | } |
822 | ||
6f883237 | 823 | int cg_is_empty(const char *controller, const char *path) { |
7027ff61 | 824 | _cleanup_fclose_ FILE *f = NULL; |
efdb0237 | 825 | pid_t pid; |
7027ff61 | 826 | int r; |
8c6db833 | 827 | |
8c6db833 LP |
828 | assert(path); |
829 | ||
b043cd0b | 830 | r = cg_enumerate_processes(controller, path, &f); |
6f883237 | 831 | if (r == -ENOENT) |
1bcf3fc6 | 832 | return true; |
c3175a7f | 833 | if (r < 0) |
6f883237 | 834 | return r; |
8c6db833 | 835 | |
6f883237 | 836 | r = cg_read_pid(f, &pid); |
c6c18be3 LP |
837 | if (r < 0) |
838 | return r; | |
8c6db833 | 839 | |
6f883237 | 840 | return r == 0; |
8c6db833 LP |
841 | } |
842 | ||
6f883237 | 843 | int cg_is_empty_recursive(const char *controller, const char *path) { |
415fc41c | 844 | int r; |
8c6db833 | 845 | |
8c6db833 LP |
846 | assert(path); |
847 | ||
6fd66507 | 848 | /* The root cgroup is always populated */ |
57ea45e1 | 849 | if (controller && empty_or_root(path)) |
efdb0237 | 850 | return false; |
6fd66507 | 851 | |
c22800e4 | 852 | r = cg_unified_controller(controller); |
b4cccbc1 LP |
853 | if (r < 0) |
854 | return r; | |
855 | if (r > 0) { | |
ab2c3861 | 856 | _cleanup_free_ char *t = NULL; |
8c6db833 | 857 | |
efdb0237 | 858 | /* On the unified hierarchy we can check empty state |
ab2c3861 | 859 | * via the "populated" attribute of "cgroup.events". */ |
8c6db833 | 860 | |
ab2c3861 | 861 | r = cg_read_event(controller, path, "populated", &t); |
1bcf3fc6 ZJS |
862 | if (r == -ENOENT) |
863 | return true; | |
efdb0237 LP |
864 | if (r < 0) |
865 | return r; | |
866 | ||
867 | return streq(t, "0"); | |
868 | } else { | |
869 | _cleanup_closedir_ DIR *d = NULL; | |
870 | char *fn; | |
8c6db833 | 871 | |
efdb0237 | 872 | r = cg_is_empty(controller, path); |
35d2e7ec | 873 | if (r <= 0) |
7027ff61 | 874 | return r; |
35d2e7ec | 875 | |
efdb0237 LP |
876 | r = cg_enumerate_subgroups(controller, path, &d); |
877 | if (r == -ENOENT) | |
1bcf3fc6 | 878 | return true; |
efdb0237 LP |
879 | if (r < 0) |
880 | return r; | |
35d2e7ec | 881 | |
efdb0237 LP |
882 | while ((r = cg_read_subgroup(d, &fn)) > 0) { |
883 | _cleanup_free_ char *p = NULL; | |
884 | ||
657ee2d8 | 885 | p = path_join(path, fn); |
efdb0237 LP |
886 | free(fn); |
887 | if (!p) | |
888 | return -ENOMEM; | |
889 | ||
890 | r = cg_is_empty_recursive(controller, p); | |
891 | if (r <= 0) | |
892 | return r; | |
893 | } | |
894 | if (r < 0) | |
895 | return r; | |
896 | ||
897 | return true; | |
898 | } | |
35d2e7ec LP |
899 | } |
900 | ||
2a8020fe ZJS |
901 | int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) { |
902 | _cleanup_free_ char *controller = NULL, *path = NULL; | |
35d2e7ec LP |
903 | |
904 | assert(spec); | |
35d2e7ec LP |
905 | |
906 | if (*spec == '/') { | |
99be45a4 | 907 | if (!path_is_normalized(spec)) |
e884315e | 908 | return -EINVAL; |
35d2e7ec | 909 | |
2a8020fe ZJS |
910 | if (ret_path) { |
911 | path = strdup(spec); | |
912 | if (!path) | |
35d2e7ec LP |
913 | return -ENOMEM; |
914 | ||
2a8020fe | 915 | path_simplify(path, false); |
8c6db833 LP |
916 | } |
917 | ||
2a8020fe ZJS |
918 | } else { |
919 | const char *e; | |
35d2e7ec | 920 | |
2a8020fe ZJS |
921 | e = strchr(spec, ':'); |
922 | if (e) { | |
923 | controller = strndup(spec, e-spec); | |
924 | if (!controller) | |
35d2e7ec | 925 | return -ENOMEM; |
2a8020fe ZJS |
926 | if (!cg_controller_is_valid(controller)) |
927 | return -EINVAL; | |
35d2e7ec | 928 | |
2a8020fe ZJS |
929 | if (!isempty(e + 1)) { |
930 | path = strdup(e+1); | |
931 | if (!path) | |
932 | return -ENOMEM; | |
35d2e7ec | 933 | |
2a8020fe ZJS |
934 | if (!path_is_normalized(path) || |
935 | !path_is_absolute(path)) | |
936 | return -EINVAL; | |
8c6db833 | 937 | |
2a8020fe ZJS |
938 | path_simplify(path, false); |
939 | } | |
246aa6dd | 940 | |
2a8020fe ZJS |
941 | } else { |
942 | if (!cg_controller_is_valid(spec)) | |
943 | return -EINVAL; | |
35d2e7ec | 944 | |
2a8020fe ZJS |
945 | if (ret_controller) { |
946 | controller = strdup(spec); | |
947 | if (!controller) | |
948 | return -ENOMEM; | |
949 | } | |
baa89da4 | 950 | } |
baa89da4 | 951 | } |
5954c074 | 952 | |
2a8020fe ZJS |
953 | if (ret_controller) |
954 | *ret_controller = TAKE_PTR(controller); | |
955 | if (ret_path) | |
956 | *ret_path = TAKE_PTR(path); | |
35d2e7ec | 957 | return 0; |
8c6db833 | 958 | } |
c6c18be3 | 959 | |
7027ff61 | 960 | int cg_mangle_path(const char *path, char **result) { |
78edb35a LP |
961 | _cleanup_free_ char *c = NULL, *p = NULL; |
962 | char *t; | |
35d2e7ec LP |
963 | int r; |
964 | ||
965 | assert(path); | |
966 | assert(result); | |
967 | ||
73e231ab | 968 | /* First, check if it already is a filesystem path */ |
7027ff61 | 969 | if (path_startswith(path, "/sys/fs/cgroup")) { |
35d2e7ec | 970 | |
b69d29ce LP |
971 | t = strdup(path); |
972 | if (!t) | |
35d2e7ec LP |
973 | return -ENOMEM; |
974 | ||
858d36c1 | 975 | *result = path_simplify(t, false); |
35d2e7ec LP |
976 | return 0; |
977 | } | |
978 | ||
73e231ab | 979 | /* Otherwise, treat it as cg spec */ |
b69d29ce LP |
980 | r = cg_split_spec(path, &c, &p); |
981 | if (r < 0) | |
35d2e7ec LP |
982 | return r; |
983 | ||
efdb0237 | 984 | return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result); |
35d2e7ec | 985 | } |
1f73f0f1 | 986 | |
7027ff61 | 987 | int cg_get_root_path(char **path) { |
9444b1f2 | 988 | char *p, *e; |
7027ff61 LP |
989 | int r; |
990 | ||
991 | assert(path); | |
992 | ||
9444b1f2 | 993 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p); |
7027ff61 LP |
994 | if (r < 0) |
995 | return r; | |
996 | ||
efdb0237 LP |
997 | e = endswith(p, "/" SPECIAL_INIT_SCOPE); |
998 | if (!e) | |
999 | e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */ | |
1000 | if (!e) | |
1001 | e = endswith(p, "/system"); /* even more legacy */ | |
9444b1f2 | 1002 | if (e) |
7027ff61 LP |
1003 | *e = 0; |
1004 | ||
1f73f0f1 LP |
1005 | *path = p; |
1006 | return 0; | |
1007 | } | |
b59e2465 | 1008 | |
751bc6ac LP |
1009 | int cg_shift_path(const char *cgroup, const char *root, const char **shifted) { |
1010 | _cleanup_free_ char *rt = NULL; | |
1011 | char *p; | |
ba1261bc LP |
1012 | int r; |
1013 | ||
e9174f29 | 1014 | assert(cgroup); |
751bc6ac | 1015 | assert(shifted); |
e9174f29 LP |
1016 | |
1017 | if (!root) { | |
1018 | /* If the root was specified let's use that, otherwise | |
1019 | * let's determine it from PID 1 */ | |
1020 | ||
751bc6ac | 1021 | r = cg_get_root_path(&rt); |
e9174f29 LP |
1022 | if (r < 0) |
1023 | return r; | |
1024 | ||
751bc6ac | 1025 | root = rt; |
e9174f29 | 1026 | } |
ba1261bc | 1027 | |
751bc6ac | 1028 | p = path_startswith(cgroup, root); |
efdb0237 | 1029 | if (p && p > cgroup) |
751bc6ac LP |
1030 | *shifted = p - 1; |
1031 | else | |
1032 | *shifted = cgroup; | |
1033 | ||
1034 | return 0; | |
1035 | } | |
1036 | ||
1037 | int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { | |
1038 | _cleanup_free_ char *raw = NULL; | |
1039 | const char *c; | |
1040 | int r; | |
1041 | ||
1042 | assert(pid >= 0); | |
1043 | assert(cgroup); | |
1044 | ||
1045 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw); | |
7027ff61 | 1046 | if (r < 0) |
ba1261bc | 1047 | return r; |
ba1261bc | 1048 | |
751bc6ac LP |
1049 | r = cg_shift_path(raw, root, &c); |
1050 | if (r < 0) | |
1051 | return r; | |
ba1261bc | 1052 | |
ae2a15bc LP |
1053 | if (c == raw) |
1054 | *cgroup = TAKE_PTR(raw); | |
1055 | else { | |
751bc6ac | 1056 | char *n; |
ba1261bc | 1057 | |
751bc6ac LP |
1058 | n = strdup(c); |
1059 | if (!n) | |
ba1261bc | 1060 | return -ENOMEM; |
ba1261bc | 1061 | |
751bc6ac LP |
1062 | *cgroup = n; |
1063 | } | |
ba1261bc LP |
1064 | |
1065 | return 0; | |
1066 | } | |
1067 | ||
9ed794a3 | 1068 | int cg_path_decode_unit(const char *cgroup, char **unit) { |
8b0849e9 LP |
1069 | char *c, *s; |
1070 | size_t n; | |
ef1673d1 MT |
1071 | |
1072 | assert(cgroup); | |
6c03089c | 1073 | assert(unit); |
ef1673d1 | 1074 | |
8b0849e9 LP |
1075 | n = strcspn(cgroup, "/"); |
1076 | if (n < 3) | |
1077 | return -ENXIO; | |
1078 | ||
1079 | c = strndupa(cgroup, n); | |
ae018d9b | 1080 | c = cg_unescape(c); |
ef1673d1 | 1081 | |
7410616c | 1082 | if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
cfeaa44a | 1083 | return -ENXIO; |
ef1673d1 | 1084 | |
d7bd3de0 | 1085 | s = strdup(c); |
6c03089c LP |
1086 | if (!s) |
1087 | return -ENOMEM; | |
1088 | ||
1089 | *unit = s; | |
ef1673d1 MT |
1090 | return 0; |
1091 | } | |
1092 | ||
8b0849e9 LP |
1093 | static bool valid_slice_name(const char *p, size_t n) { |
1094 | ||
1095 | if (!p) | |
1096 | return false; | |
1097 | ||
fbd0b64f | 1098 | if (n < STRLEN("x.slice")) |
8b0849e9 LP |
1099 | return false; |
1100 | ||
1101 | if (memcmp(p + n - 6, ".slice", 6) == 0) { | |
1102 | char buf[n+1], *c; | |
1103 | ||
1104 | memcpy(buf, p, n); | |
1105 | buf[n] = 0; | |
1106 | ||
1107 | c = cg_unescape(buf); | |
1108 | ||
7410616c | 1109 | return unit_name_is_valid(c, UNIT_NAME_PLAIN); |
8b0849e9 LP |
1110 | } |
1111 | ||
1112 | return false; | |
1113 | } | |
1114 | ||
9444b1f2 | 1115 | static const char *skip_slices(const char *p) { |
8b0849e9 LP |
1116 | assert(p); |
1117 | ||
9444b1f2 LP |
1118 | /* Skips over all slice assignments */ |
1119 | ||
1120 | for (;;) { | |
1021b21b LP |
1121 | size_t n; |
1122 | ||
9444b1f2 LP |
1123 | p += strspn(p, "/"); |
1124 | ||
1125 | n = strcspn(p, "/"); | |
8b0849e9 | 1126 | if (!valid_slice_name(p, n)) |
9444b1f2 LP |
1127 | return p; |
1128 | ||
1129 | p += n; | |
1130 | } | |
1131 | } | |
1132 | ||
8b0849e9 | 1133 | int cg_path_get_unit(const char *path, char **ret) { |
6c03089c | 1134 | const char *e; |
8b0849e9 LP |
1135 | char *unit; |
1136 | int r; | |
6c03089c LP |
1137 | |
1138 | assert(path); | |
8b0849e9 | 1139 | assert(ret); |
6c03089c | 1140 | |
9444b1f2 | 1141 | e = skip_slices(path); |
6c03089c | 1142 | |
8b0849e9 LP |
1143 | r = cg_path_decode_unit(e, &unit); |
1144 | if (r < 0) | |
1145 | return r; | |
1146 | ||
1147 | /* We skipped over the slices, don't accept any now */ | |
1148 | if (endswith(unit, ".slice")) { | |
1149 | free(unit); | |
1150 | return -ENXIO; | |
1151 | } | |
1152 | ||
1153 | *ret = unit; | |
1154 | return 0; | |
6c03089c LP |
1155 | } |
1156 | ||
1157 | int cg_pid_get_unit(pid_t pid, char **unit) { | |
7fd1b19b | 1158 | _cleanup_free_ char *cgroup = NULL; |
ba1261bc | 1159 | int r; |
ba1261bc | 1160 | |
ef1673d1 MT |
1161 | assert(unit); |
1162 | ||
7027ff61 | 1163 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
ef1673d1 MT |
1164 | if (r < 0) |
1165 | return r; | |
1166 | ||
6c03089c LP |
1167 | return cg_path_get_unit(cgroup, unit); |
1168 | } | |
ef1673d1 | 1169 | |
d4fffc4b ZJS |
1170 | /** |
1171 | * Skip session-*.scope, but require it to be there. | |
1172 | */ | |
9444b1f2 LP |
1173 | static const char *skip_session(const char *p) { |
1174 | size_t n; | |
1175 | ||
8b0849e9 LP |
1176 | if (isempty(p)) |
1177 | return NULL; | |
9444b1f2 LP |
1178 | |
1179 | p += strspn(p, "/"); | |
1180 | ||
1181 | n = strcspn(p, "/"); | |
fbd0b64f | 1182 | if (n < STRLEN("session-x.scope")) |
d4fffc4b ZJS |
1183 | return NULL; |
1184 | ||
8b0849e9 LP |
1185 | if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) { |
1186 | char buf[n - 8 - 6 + 1]; | |
1187 | ||
1188 | memcpy(buf, p + 8, n - 8 - 6); | |
1189 | buf[n - 8 - 6] = 0; | |
d4fffc4b | 1190 | |
8b0849e9 LP |
1191 | /* Note that session scopes never need unescaping, |
1192 | * since they cannot conflict with the kernel's own | |
1193 | * names, hence we don't need to call cg_unescape() | |
1194 | * here. */ | |
1195 | ||
1196 | if (!session_id_valid(buf)) | |
1197 | return false; | |
1198 | ||
1199 | p += n; | |
1200 | p += strspn(p, "/"); | |
1201 | return p; | |
1202 | } | |
1203 | ||
1204 | return NULL; | |
d4fffc4b ZJS |
1205 | } |
1206 | ||
1207 | /** | |
1208 | * Skip user@*.service, but require it to be there. | |
1209 | */ | |
1210 | static const char *skip_user_manager(const char *p) { | |
1211 | size_t n; | |
1212 | ||
8b0849e9 LP |
1213 | if (isempty(p)) |
1214 | return NULL; | |
d4fffc4b ZJS |
1215 | |
1216 | p += strspn(p, "/"); | |
1217 | ||
1218 | n = strcspn(p, "/"); | |
fbd0b64f | 1219 | if (n < STRLEN("user@x.service")) |
6c03089c | 1220 | return NULL; |
ef1673d1 | 1221 | |
8b0849e9 LP |
1222 | if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) { |
1223 | char buf[n - 5 - 8 + 1]; | |
9444b1f2 | 1224 | |
8b0849e9 LP |
1225 | memcpy(buf, p + 5, n - 5 - 8); |
1226 | buf[n - 5 - 8] = 0; | |
1227 | ||
1228 | /* Note that user manager services never need unescaping, | |
1229 | * since they cannot conflict with the kernel's own | |
1230 | * names, hence we don't need to call cg_unescape() | |
1231 | * here. */ | |
1232 | ||
1233 | if (parse_uid(buf, NULL) < 0) | |
1234 | return NULL; | |
1235 | ||
1236 | p += n; | |
1237 | p += strspn(p, "/"); | |
1238 | ||
1239 | return p; | |
1240 | } | |
1241 | ||
1242 | return NULL; | |
9444b1f2 LP |
1243 | } |
1244 | ||
329ac4bc | 1245 | static const char *skip_user_prefix(const char *path) { |
d4fffc4b | 1246 | const char *e, *t; |
ef1673d1 | 1247 | |
6c03089c | 1248 | assert(path); |
ba1261bc | 1249 | |
9444b1f2 LP |
1250 | /* Skip slices, if there are any */ |
1251 | e = skip_slices(path); | |
ba1261bc | 1252 | |
329ac4bc | 1253 | /* Skip the user manager, if it's in the path now... */ |
8b0849e9 | 1254 | t = skip_user_manager(e); |
329ac4bc LP |
1255 | if (t) |
1256 | return t; | |
8b0849e9 | 1257 | |
329ac4bc LP |
1258 | /* Alternatively skip the user session if it is in the path... */ |
1259 | return skip_session(e); | |
1260 | } | |
32081481 | 1261 | |
329ac4bc LP |
1262 | int cg_path_get_user_unit(const char *path, char **ret) { |
1263 | const char *t; | |
6c03089c | 1264 | |
329ac4bc LP |
1265 | assert(path); |
1266 | assert(ret); | |
8b0849e9 | 1267 | |
329ac4bc LP |
1268 | t = skip_user_prefix(path); |
1269 | if (!t) | |
8b0849e9 | 1270 | return -ENXIO; |
8b0849e9 | 1271 | |
bf21be10 LP |
1272 | /* And from here on it looks pretty much the same as for a system unit, hence let's use the same |
1273 | * parser. */ | |
329ac4bc | 1274 | return cg_path_get_unit(t, ret); |
ef1673d1 | 1275 | } |
ba1261bc | 1276 | |
ef1673d1 | 1277 | int cg_pid_get_user_unit(pid_t pid, char **unit) { |
7fd1b19b | 1278 | _cleanup_free_ char *cgroup = NULL; |
6c03089c LP |
1279 | int r; |
1280 | ||
1281 | assert(unit); | |
1282 | ||
7027ff61 | 1283 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
6c03089c LP |
1284 | if (r < 0) |
1285 | return r; | |
1286 | ||
1287 | return cg_path_get_user_unit(cgroup, unit); | |
ba1261bc | 1288 | } |
e884315e | 1289 | |
7027ff61 | 1290 | int cg_path_get_machine_name(const char *path, char **machine) { |
efdb0237 LP |
1291 | _cleanup_free_ char *u = NULL; |
1292 | const char *sl; | |
89f7c846 | 1293 | int r; |
374ec6ab | 1294 | |
89f7c846 LP |
1295 | r = cg_path_get_unit(path, &u); |
1296 | if (r < 0) | |
1297 | return r; | |
7027ff61 | 1298 | |
efdb0237 | 1299 | sl = strjoina("/run/systemd/machines/unit:", u); |
89f7c846 | 1300 | return readlink_malloc(sl, machine); |
7027ff61 LP |
1301 | } |
1302 | ||
1303 | int cg_pid_get_machine_name(pid_t pid, char **machine) { | |
7fd1b19b | 1304 | _cleanup_free_ char *cgroup = NULL; |
7027ff61 LP |
1305 | int r; |
1306 | ||
1307 | assert(machine); | |
1308 | ||
1309 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1310 | if (r < 0) | |
1311 | return r; | |
1312 | ||
1313 | return cg_path_get_machine_name(cgroup, machine); | |
1314 | } | |
1315 | ||
1316 | int cg_path_get_session(const char *path, char **session) { | |
8b0849e9 LP |
1317 | _cleanup_free_ char *unit = NULL; |
1318 | char *start, *end; | |
1319 | int r; | |
7027ff61 LP |
1320 | |
1321 | assert(path); | |
7027ff61 | 1322 | |
8b0849e9 LP |
1323 | r = cg_path_get_unit(path, &unit); |
1324 | if (r < 0) | |
1325 | return r; | |
7027ff61 | 1326 | |
8b0849e9 LP |
1327 | start = startswith(unit, "session-"); |
1328 | if (!start) | |
cfeaa44a | 1329 | return -ENXIO; |
8b0849e9 LP |
1330 | end = endswith(start, ".scope"); |
1331 | if (!end) | |
cfeaa44a | 1332 | return -ENXIO; |
8b0849e9 LP |
1333 | |
1334 | *end = 0; | |
1335 | if (!session_id_valid(start)) | |
cfeaa44a | 1336 | return -ENXIO; |
374ec6ab | 1337 | |
af08d2f9 | 1338 | if (session) { |
8b0849e9 | 1339 | char *rr; |
af08d2f9 | 1340 | |
8b0849e9 LP |
1341 | rr = strdup(start); |
1342 | if (!rr) | |
af08d2f9 LP |
1343 | return -ENOMEM; |
1344 | ||
8b0849e9 | 1345 | *session = rr; |
af08d2f9 | 1346 | } |
7027ff61 | 1347 | |
7027ff61 LP |
1348 | return 0; |
1349 | } | |
1350 | ||
1351 | int cg_pid_get_session(pid_t pid, char **session) { | |
7fd1b19b | 1352 | _cleanup_free_ char *cgroup = NULL; |
7027ff61 LP |
1353 | int r; |
1354 | ||
7027ff61 LP |
1355 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
1356 | if (r < 0) | |
1357 | return r; | |
1358 | ||
1359 | return cg_path_get_session(cgroup, session); | |
1360 | } | |
1361 | ||
ae018d9b | 1362 | int cg_path_get_owner_uid(const char *path, uid_t *uid) { |
374ec6ab | 1363 | _cleanup_free_ char *slice = NULL; |
8b0849e9 | 1364 | char *start, *end; |
374ec6ab | 1365 | int r; |
ae018d9b LP |
1366 | |
1367 | assert(path); | |
ae018d9b | 1368 | |
374ec6ab LP |
1369 | r = cg_path_get_slice(path, &slice); |
1370 | if (r < 0) | |
1371 | return r; | |
ae018d9b | 1372 | |
674eb685 LP |
1373 | start = startswith(slice, "user-"); |
1374 | if (!start) | |
cfeaa44a | 1375 | return -ENXIO; |
8b0849e9 | 1376 | end = endswith(start, ".slice"); |
674eb685 | 1377 | if (!end) |
cfeaa44a | 1378 | return -ENXIO; |
ae018d9b | 1379 | |
8b0849e9 LP |
1380 | *end = 0; |
1381 | if (parse_uid(start, uid) < 0) | |
cfeaa44a | 1382 | return -ENXIO; |
674eb685 | 1383 | |
674eb685 | 1384 | return 0; |
ae018d9b LP |
1385 | } |
1386 | ||
1387 | int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) { | |
1388 | _cleanup_free_ char *cgroup = NULL; | |
1389 | int r; | |
1390 | ||
ae018d9b LP |
1391 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); |
1392 | if (r < 0) | |
1393 | return r; | |
1394 | ||
1395 | return cg_path_get_owner_uid(cgroup, uid); | |
1396 | } | |
1397 | ||
1021b21b LP |
1398 | int cg_path_get_slice(const char *p, char **slice) { |
1399 | const char *e = NULL; | |
1021b21b LP |
1400 | |
1401 | assert(p); | |
1402 | assert(slice); | |
1403 | ||
329ac4bc LP |
1404 | /* Finds the right-most slice unit from the beginning, but |
1405 | * stops before we come to the first non-slice unit. */ | |
1406 | ||
1021b21b LP |
1407 | for (;;) { |
1408 | size_t n; | |
1409 | ||
1410 | p += strspn(p, "/"); | |
1411 | ||
1412 | n = strcspn(p, "/"); | |
8b0849e9 | 1413 | if (!valid_slice_name(p, n)) { |
1021b21b | 1414 | |
8b0849e9 LP |
1415 | if (!e) { |
1416 | char *s; | |
1021b21b | 1417 | |
e5d855d3 | 1418 | s = strdup(SPECIAL_ROOT_SLICE); |
8b0849e9 LP |
1419 | if (!s) |
1420 | return -ENOMEM; | |
1021b21b | 1421 | |
8b0849e9 LP |
1422 | *slice = s; |
1423 | return 0; | |
1424 | } | |
1425 | ||
1426 | return cg_path_decode_unit(e, slice); | |
1021b21b LP |
1427 | } |
1428 | ||
1429 | e = p; | |
1021b21b LP |
1430 | p += n; |
1431 | } | |
1432 | } | |
1433 | ||
1434 | int cg_pid_get_slice(pid_t pid, char **slice) { | |
1435 | _cleanup_free_ char *cgroup = NULL; | |
1436 | int r; | |
1437 | ||
1438 | assert(slice); | |
1439 | ||
1440 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1441 | if (r < 0) | |
1442 | return r; | |
1443 | ||
1444 | return cg_path_get_slice(cgroup, slice); | |
1445 | } | |
1446 | ||
329ac4bc LP |
1447 | int cg_path_get_user_slice(const char *p, char **slice) { |
1448 | const char *t; | |
1449 | assert(p); | |
1450 | assert(slice); | |
1451 | ||
1452 | t = skip_user_prefix(p); | |
1453 | if (!t) | |
1454 | return -ENXIO; | |
1455 | ||
1456 | /* And now it looks pretty much the same as for a system | |
1457 | * slice, so let's just use the same parser from here on. */ | |
1458 | return cg_path_get_slice(t, slice); | |
1459 | } | |
1460 | ||
1461 | int cg_pid_get_user_slice(pid_t pid, char **slice) { | |
1462 | _cleanup_free_ char *cgroup = NULL; | |
1463 | int r; | |
1464 | ||
1465 | assert(slice); | |
1466 | ||
1467 | r = cg_pid_get_path_shifted(pid, NULL, &cgroup); | |
1468 | if (r < 0) | |
1469 | return r; | |
1470 | ||
1471 | return cg_path_get_user_slice(cgroup, slice); | |
1472 | } | |
1473 | ||
ae018d9b LP |
1474 | char *cg_escape(const char *p) { |
1475 | bool need_prefix = false; | |
1476 | ||
1477 | /* This implements very minimal escaping for names to be used | |
1478 | * as file names in the cgroup tree: any name which might | |
1479 | * conflict with a kernel name or is prefixed with '_' is | |
1480 | * prefixed with a '_'. That way, when reading cgroup names it | |
1481 | * is sufficient to remove a single prefixing underscore if | |
1482 | * there is one. */ | |
1483 | ||
1484 | /* The return value of this function (unlike cg_unescape()) | |
1485 | * needs free()! */ | |
1486 | ||
4c701096 | 1487 | if (IN_SET(p[0], 0, '_', '.') || |
0cbd293e | 1488 | STR_IN_SET(p, "notify_on_release", "release_agent", "tasks") || |
efdb0237 | 1489 | startswith(p, "cgroup.")) |
ae018d9b LP |
1490 | need_prefix = true; |
1491 | else { | |
1492 | const char *dot; | |
1493 | ||
1494 | dot = strrchr(p, '.'); | |
1495 | if (dot) { | |
efdb0237 LP |
1496 | CGroupController c; |
1497 | size_t l = dot - p; | |
ae018d9b | 1498 | |
efdb0237 LP |
1499 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { |
1500 | const char *n; | |
1501 | ||
1502 | n = cgroup_controller_to_string(c); | |
ae018d9b | 1503 | |
efdb0237 LP |
1504 | if (l != strlen(n)) |
1505 | continue; | |
ae018d9b | 1506 | |
efdb0237 LP |
1507 | if (memcmp(p, n, l) != 0) |
1508 | continue; | |
1509 | ||
1510 | need_prefix = true; | |
1511 | break; | |
ae018d9b LP |
1512 | } |
1513 | } | |
1514 | } | |
1515 | ||
1516 | if (need_prefix) | |
b910cc72 | 1517 | return strjoin("_", p); |
efdb0237 LP |
1518 | |
1519 | return strdup(p); | |
ae018d9b LP |
1520 | } |
1521 | ||
1522 | char *cg_unescape(const char *p) { | |
1523 | assert(p); | |
1524 | ||
1525 | /* The return value of this function (unlike cg_escape()) | |
1526 | * doesn't need free()! */ | |
1527 | ||
1528 | if (p[0] == '_') | |
1529 | return (char*) p+1; | |
1530 | ||
1531 | return (char*) p; | |
1532 | } | |
78edb35a LP |
1533 | |
1534 | #define CONTROLLER_VALID \ | |
4b549144 | 1535 | DIGITS LETTERS \ |
78edb35a LP |
1536 | "_" |
1537 | ||
185a0874 | 1538 | bool cg_controller_is_valid(const char *p) { |
78edb35a LP |
1539 | const char *t, *s; |
1540 | ||
1541 | if (!p) | |
1542 | return false; | |
1543 | ||
b6629c4b TH |
1544 | if (streq(p, SYSTEMD_CGROUP_CONTROLLER)) |
1545 | return true; | |
1546 | ||
185a0874 DJL |
1547 | s = startswith(p, "name="); |
1548 | if (s) | |
1549 | p = s; | |
78edb35a | 1550 | |
4c701096 | 1551 | if (IN_SET(*p, 0, '_')) |
78edb35a LP |
1552 | return false; |
1553 | ||
1554 | for (t = p; *t; t++) | |
1555 | if (!strchr(CONTROLLER_VALID, *t)) | |
1556 | return false; | |
1557 | ||
1558 | if (t - p > FILENAME_MAX) | |
1559 | return false; | |
1560 | ||
1561 | return true; | |
1562 | } | |
a016b922 LP |
1563 | |
1564 | int cg_slice_to_path(const char *unit, char **ret) { | |
1565 | _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL; | |
1566 | const char *dash; | |
7410616c | 1567 | int r; |
a016b922 LP |
1568 | |
1569 | assert(unit); | |
1570 | assert(ret); | |
1571 | ||
e5d855d3 | 1572 | if (streq(unit, SPECIAL_ROOT_SLICE)) { |
c96cc582 LP |
1573 | char *x; |
1574 | ||
1575 | x = strdup(""); | |
1576 | if (!x) | |
1577 | return -ENOMEM; | |
1578 | *ret = x; | |
1579 | return 0; | |
1580 | } | |
1581 | ||
7410616c | 1582 | if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN)) |
a016b922 LP |
1583 | return -EINVAL; |
1584 | ||
1585 | if (!endswith(unit, ".slice")) | |
1586 | return -EINVAL; | |
1587 | ||
7410616c LP |
1588 | r = unit_name_to_prefix(unit, &p); |
1589 | if (r < 0) | |
1590 | return r; | |
a016b922 LP |
1591 | |
1592 | dash = strchr(p, '-'); | |
e66e5b61 LP |
1593 | |
1594 | /* Don't allow initial dashes */ | |
1595 | if (dash == p) | |
1596 | return -EINVAL; | |
1597 | ||
a016b922 LP |
1598 | while (dash) { |
1599 | _cleanup_free_ char *escaped = NULL; | |
1600 | char n[dash - p + sizeof(".slice")]; | |
1601 | ||
989290db | 1602 | #if HAS_FEATURE_MEMORY_SANITIZER |
1c56d501 | 1603 | /* msan doesn't instrument stpncpy, so it thinks |
5238e957 | 1604 | * n is later used uninitialized: |
1c56d501 ZJS |
1605 | * https://github.com/google/sanitizers/issues/926 |
1606 | */ | |
1607 | zero(n); | |
1608 | #endif | |
1609 | ||
e66e5b61 | 1610 | /* Don't allow trailing or double dashes */ |
4c701096 | 1611 | if (IN_SET(dash[1], 0, '-')) |
c96cc582 | 1612 | return -EINVAL; |
a016b922 | 1613 | |
c96cc582 | 1614 | strcpy(stpncpy(n, p, dash - p), ".slice"); |
7410616c | 1615 | if (!unit_name_is_valid(n, UNIT_NAME_PLAIN)) |
a016b922 LP |
1616 | return -EINVAL; |
1617 | ||
1618 | escaped = cg_escape(n); | |
1619 | if (!escaped) | |
1620 | return -ENOMEM; | |
1621 | ||
1622 | if (!strextend(&s, escaped, "/", NULL)) | |
1623 | return -ENOMEM; | |
1624 | ||
1625 | dash = strchr(dash+1, '-'); | |
1626 | } | |
1627 | ||
1628 | e = cg_escape(unit); | |
1629 | if (!e) | |
1630 | return -ENOMEM; | |
1631 | ||
1632 | if (!strextend(&s, e, NULL)) | |
1633 | return -ENOMEM; | |
1634 | ||
ae2a15bc | 1635 | *ret = TAKE_PTR(s); |
a016b922 LP |
1636 | |
1637 | return 0; | |
1638 | } | |
4ad49000 LP |
1639 | |
1640 | int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) { | |
1641 | _cleanup_free_ char *p = NULL; | |
1642 | int r; | |
1643 | ||
1644 | r = cg_get_path(controller, path, attribute, &p); | |
1645 | if (r < 0) | |
1646 | return r; | |
1647 | ||
604028de | 1648 | return write_string_file(p, value, WRITE_STRING_FILE_DISABLE_BUFFER); |
4ad49000 LP |
1649 | } |
1650 | ||
934277fe LP |
1651 | int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) { |
1652 | _cleanup_free_ char *p = NULL; | |
1653 | int r; | |
1654 | ||
1655 | r = cg_get_path(controller, path, attribute, &p); | |
1656 | if (r < 0) | |
1657 | return r; | |
1658 | ||
1659 | return read_one_line_file(p, ret); | |
1660 | } | |
1661 | ||
613328c3 AZ |
1662 | int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret) { |
1663 | _cleanup_free_ char *value = NULL; | |
1664 | uint64_t v; | |
1665 | int r; | |
1666 | ||
1667 | assert(ret); | |
1668 | ||
1669 | r = cg_get_attribute(controller, path, attribute, &value); | |
1670 | if (r == -ENOENT) | |
1671 | return -ENODATA; | |
1672 | if (r < 0) | |
1673 | return r; | |
1674 | ||
1675 | if (streq(value, "max")) { | |
1676 | *ret = CGROUP_LIMIT_MAX; | |
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | r = safe_atou64(value, &v); | |
1681 | if (r < 0) | |
1682 | return r; | |
1683 | ||
1684 | *ret = v; | |
1685 | return 0; | |
1686 | } | |
1687 | ||
25a1f04c | 1688 | int cg_get_keyed_attribute_full( |
b734a4ff LP |
1689 | const char *controller, |
1690 | const char *path, | |
1691 | const char *attribute, | |
1692 | char **keys, | |
25a1f04c MS |
1693 | char **ret_values, |
1694 | CGroupKeyMode mode) { | |
66ebf6c0 | 1695 | |
b734a4ff | 1696 | _cleanup_free_ char *filename = NULL, *contents = NULL; |
b734a4ff | 1697 | const char *p; |
9177fa9f | 1698 | size_t n, i, n_done = 0; |
b734a4ff LP |
1699 | char **v; |
1700 | int r; | |
1701 | ||
4e1dfa45 | 1702 | /* Reads one or more fields of a cgroup v2 keyed attribute file. The 'keys' parameter should be an strv with |
b734a4ff LP |
1703 | * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of |
1704 | * entries as 'keys'. On success each entry will be set to the value of the matching key. | |
1705 | * | |
d9e45bc3 MS |
1706 | * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. If mode |
1707 | * is set to GG_KEY_MODE_GRACEFUL we ignore missing keys and return those that were parsed successfully. */ | |
66ebf6c0 TH |
1708 | |
1709 | r = cg_get_path(controller, path, attribute, &filename); | |
1710 | if (r < 0) | |
1711 | return r; | |
1712 | ||
b734a4ff | 1713 | r = read_full_file(filename, &contents, NULL); |
66ebf6c0 TH |
1714 | if (r < 0) |
1715 | return r; | |
1716 | ||
b734a4ff LP |
1717 | n = strv_length(keys); |
1718 | if (n == 0) /* No keys to retrieve? That's easy, we are done then */ | |
1719 | return 0; | |
66ebf6c0 | 1720 | |
b734a4ff LP |
1721 | /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */ |
1722 | v = newa0(char*, n); | |
66ebf6c0 | 1723 | |
b734a4ff LP |
1724 | for (p = contents; *p;) { |
1725 | const char *w = NULL; | |
b734a4ff | 1726 | |
9177fa9f ZJS |
1727 | for (i = 0; i < n; i++) |
1728 | if (!v[i]) { | |
b734a4ff LP |
1729 | w = first_word(p, keys[i]); |
1730 | if (w) | |
1731 | break; | |
66ebf6c0 | 1732 | } |
66ebf6c0 | 1733 | |
b734a4ff | 1734 | if (w) { |
b734a4ff LP |
1735 | size_t l; |
1736 | ||
1737 | l = strcspn(w, NEWLINE); | |
9177fa9f ZJS |
1738 | v[i] = strndup(w, l); |
1739 | if (!v[i]) { | |
b734a4ff LP |
1740 | r = -ENOMEM; |
1741 | goto fail; | |
66ebf6c0 | 1742 | } |
b734a4ff | 1743 | |
b734a4ff | 1744 | n_done++; |
b734a4ff LP |
1745 | if (n_done >= n) |
1746 | goto done; | |
1747 | ||
1748 | p = w + l; | |
9177fa9f | 1749 | } else |
b734a4ff | 1750 | p += strcspn(p, NEWLINE); |
b734a4ff LP |
1751 | |
1752 | p += strspn(p, NEWLINE); | |
66ebf6c0 TH |
1753 | } |
1754 | ||
25a1f04c MS |
1755 | if (mode & CG_KEY_MODE_GRACEFUL) |
1756 | goto done; | |
d9e45bc3 MS |
1757 | |
1758 | r = -ENXIO; | |
b734a4ff LP |
1759 | |
1760 | fail: | |
1761 | for (i = 0; i < n; i++) | |
1762 | free(v[i]); | |
1763 | ||
1764 | return r; | |
1765 | ||
1766 | done: | |
1767 | memcpy(ret_values, v, sizeof(char*) * n); | |
25a1f04c MS |
1768 | if (mode & CG_KEY_MODE_GRACEFUL) |
1769 | return n_done; | |
1770 | ||
66ebf6c0 | 1771 | return 0; |
4ad49000 LP |
1772 | } |
1773 | ||
aae7e17f | 1774 | int cg_mask_to_string(CGroupMask mask, char **ret) { |
ec635a2d LP |
1775 | _cleanup_free_ char *s = NULL; |
1776 | size_t n = 0, allocated = 0; | |
1777 | bool space = false; | |
aae7e17f | 1778 | CGroupController c; |
aae7e17f FB |
1779 | |
1780 | assert(ret); | |
1781 | ||
1782 | if (mask == 0) { | |
1783 | *ret = NULL; | |
1784 | return 0; | |
1785 | } | |
1786 | ||
1787 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
ec635a2d LP |
1788 | const char *k; |
1789 | size_t l; | |
aae7e17f | 1790 | |
f99850a0 | 1791 | if (!FLAGS_SET(mask, CGROUP_CONTROLLER_TO_MASK(c))) |
aae7e17f FB |
1792 | continue; |
1793 | ||
ec635a2d LP |
1794 | k = cgroup_controller_to_string(c); |
1795 | l = strlen(k); | |
1796 | ||
1797 | if (!GREEDY_REALLOC(s, allocated, n + space + l + 1)) | |
1798 | return -ENOMEM; | |
1799 | ||
1800 | if (space) | |
1801 | s[n] = ' '; | |
1802 | memcpy(s + n + space, k, l); | |
1803 | n += space + l; | |
1804 | ||
1805 | space = true; | |
aae7e17f FB |
1806 | } |
1807 | ||
ec635a2d | 1808 | assert(s); |
aae7e17f | 1809 | |
ec635a2d | 1810 | s[n] = 0; |
ae2a15bc | 1811 | *ret = TAKE_PTR(s); |
ec635a2d | 1812 | |
aae7e17f FB |
1813 | return 0; |
1814 | } | |
1815 | ||
38a90d45 LP |
1816 | int cg_mask_from_string(const char *value, CGroupMask *ret) { |
1817 | CGroupMask m = 0; | |
1818 | ||
1819 | assert(ret); | |
aae7e17f FB |
1820 | assert(value); |
1821 | ||
1822 | for (;;) { | |
1823 | _cleanup_free_ char *n = NULL; | |
1824 | CGroupController v; | |
1825 | int r; | |
1826 | ||
1827 | r = extract_first_word(&value, &n, NULL, 0); | |
1828 | if (r < 0) | |
1829 | return r; | |
1830 | if (r == 0) | |
1831 | break; | |
1832 | ||
1833 | v = cgroup_controller_from_string(n); | |
1834 | if (v < 0) | |
1835 | continue; | |
1836 | ||
38a90d45 | 1837 | m |= CGROUP_CONTROLLER_TO_MASK(v); |
aae7e17f | 1838 | } |
38a90d45 LP |
1839 | |
1840 | *ret = m; | |
aae7e17f FB |
1841 | return 0; |
1842 | } | |
1843 | ||
efdb0237 | 1844 | int cg_mask_supported(CGroupMask *ret) { |
38a90d45 | 1845 | CGroupMask mask; |
415fc41c | 1846 | int r; |
efdb0237 | 1847 | |
67558d15 LP |
1848 | /* Determines the mask of supported cgroup controllers. Only includes controllers we can make sense of and that |
1849 | * are actually accessible. Only covers real controllers, i.e. not the CGROUP_CONTROLLER_BPF_xyz | |
1850 | * pseudo-controllers. */ | |
4ad49000 | 1851 | |
b4cccbc1 LP |
1852 | r = cg_all_unified(); |
1853 | if (r < 0) | |
1854 | return r; | |
1855 | if (r > 0) { | |
5f4c5fef | 1856 | _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL; |
efdb0237 LP |
1857 | |
1858 | /* In the unified hierarchy we can read the supported | |
1859 | * and accessible controllers from a the top-level | |
1860 | * cgroup attribute */ | |
1861 | ||
5f4c5fef LP |
1862 | r = cg_get_root_path(&root); |
1863 | if (r < 0) | |
1864 | return r; | |
1865 | ||
1866 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path); | |
1867 | if (r < 0) | |
1868 | return r; | |
1869 | ||
1870 | r = read_one_line_file(path, &controllers); | |
efdb0237 LP |
1871 | if (r < 0) |
1872 | return r; | |
4ad49000 | 1873 | |
aae7e17f FB |
1874 | r = cg_mask_from_string(controllers, &mask); |
1875 | if (r < 0) | |
1876 | return r; | |
efdb0237 | 1877 | |
1fbbb526 | 1878 | /* Mask controllers that are not supported in unified hierarchy. */ |
03afd780 | 1879 | mask &= CGROUP_MASK_V2; |
efdb0237 LP |
1880 | |
1881 | } else { | |
1882 | CGroupController c; | |
1883 | ||
03afd780 | 1884 | /* In the legacy hierarchy, we check which hierarchies are mounted. */ |
efdb0237 | 1885 | |
38a90d45 | 1886 | mask = 0; |
efdb0237 | 1887 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { |
03afd780 | 1888 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); |
efdb0237 LP |
1889 | const char *n; |
1890 | ||
03afd780 LP |
1891 | if (!FLAGS_SET(CGROUP_MASK_V1, bit)) |
1892 | continue; | |
1893 | ||
efdb0237 LP |
1894 | n = cgroup_controller_to_string(c); |
1895 | if (controller_is_accessible(n) >= 0) | |
03afd780 | 1896 | mask |= bit; |
efdb0237 | 1897 | } |
4ad49000 LP |
1898 | } |
1899 | ||
efdb0237 LP |
1900 | *ret = mask; |
1901 | return 0; | |
4ad49000 | 1902 | } |
b12afc8c | 1903 | |
6925a0de LP |
1904 | int cg_kernel_controllers(Set **ret) { |
1905 | _cleanup_set_free_free_ Set *controllers = NULL; | |
b12afc8c | 1906 | _cleanup_fclose_ FILE *f = NULL; |
b12afc8c LP |
1907 | int r; |
1908 | ||
6925a0de | 1909 | assert(ret); |
b12afc8c | 1910 | |
f09e86bc LS |
1911 | /* Determines the full list of kernel-known controllers. Might include controllers we don't actually support |
1912 | * and controllers that aren't currently accessible (because not mounted). This does not include "name=" | |
1913 | * pseudo-controllers. */ | |
e155a0aa | 1914 | |
6925a0de LP |
1915 | controllers = set_new(&string_hash_ops); |
1916 | if (!controllers) | |
1917 | return -ENOMEM; | |
1918 | ||
fdeea3f4 ZJS |
1919 | r = fopen_unlocked("/proc/cgroups", "re", &f); |
1920 | if (r == -ENOENT) { | |
1921 | *ret = NULL; | |
1922 | return 0; | |
b12afc8c | 1923 | } |
fdeea3f4 ZJS |
1924 | if (r < 0) |
1925 | return r; | |
35bbbf85 | 1926 | |
b12afc8c | 1927 | /* Ignore the header line */ |
2351e44d | 1928 | (void) read_line(f, (size_t) -1, NULL); |
b12afc8c LP |
1929 | |
1930 | for (;;) { | |
1931 | char *controller; | |
1932 | int enabled = 0; | |
1933 | ||
1934 | errno = 0; | |
1935 | if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) { | |
1936 | ||
1937 | if (feof(f)) | |
1938 | break; | |
1939 | ||
66855de7 LP |
1940 | if (ferror(f)) |
1941 | return errno_or_else(EIO); | |
b12afc8c LP |
1942 | |
1943 | return -EBADMSG; | |
1944 | } | |
1945 | ||
1946 | if (!enabled) { | |
1947 | free(controller); | |
1948 | continue; | |
1949 | } | |
1950 | ||
efdb0237 | 1951 | if (!cg_controller_is_valid(controller)) { |
b12afc8c LP |
1952 | free(controller); |
1953 | return -EBADMSG; | |
1954 | } | |
1955 | ||
1956 | r = set_consume(controllers, controller); | |
1957 | if (r < 0) | |
1958 | return r; | |
1959 | } | |
1960 | ||
1cc6c93a | 1961 | *ret = TAKE_PTR(controllers); |
6925a0de | 1962 | |
b12afc8c LP |
1963 | return 0; |
1964 | } | |
efdb0237 | 1965 | |
d4d99bc6 ZJS |
1966 | /* The hybrid mode was initially implemented in v232 and simply mounted cgroup2 on |
1967 | * /sys/fs/cgroup/systemd. This unfortunately broke other tools (such as docker) which expected the v1 | |
1968 | * "name=systemd" hierarchy on /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mounts v2 on | |
1969 | * /sys/fs/cgroup/unified and maintains "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility | |
1970 | * with other tools. | |
f08e9287 | 1971 | * |
d4d99bc6 ZJS |
1972 | * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep |
1973 | * cgroup v2 process management but disable the compat dual layout, we return true on | |
1974 | * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and false on cg_hybrid_unified(). | |
f08e9287 TH |
1975 | */ |
1976 | static thread_local bool unified_systemd_v232; | |
1977 | ||
d4d99bc6 ZJS |
1978 | int cg_unified_cached(bool flush) { |
1979 | static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN; | |
efdb0237 | 1980 | |
efdb0237 LP |
1981 | struct statfs fs; |
1982 | ||
1983 | /* Checks if we support the unified hierarchy. Returns an | |
1984 | * error when the cgroup hierarchies aren't mounted yet or we | |
1985 | * have any other trouble determining if the unified hierarchy | |
1986 | * is supported. */ | |
1987 | ||
d4d99bc6 ZJS |
1988 | if (flush) |
1989 | unified_cache = CGROUP_UNIFIED_UNKNOWN; | |
1990 | else if (unified_cache >= CGROUP_UNIFIED_NONE) | |
1991 | return unified_cache; | |
efdb0237 LP |
1992 | |
1993 | if (statfs("/sys/fs/cgroup/", &fs) < 0) | |
c028bed1 | 1994 | return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\") failed: %m"); |
efdb0237 | 1995 | |
9aa21133 ZJS |
1996 | if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { |
1997 | log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy"); | |
5da38d07 | 1998 | unified_cache = CGROUP_UNIFIED_ALL; |
9aa21133 | 1999 | } else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) { |
2977724b | 2000 | if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 && |
f08e9287 | 2001 | F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { |
9aa21133 | 2002 | log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller"); |
2977724b | 2003 | unified_cache = CGROUP_UNIFIED_SYSTEMD; |
f08e9287 | 2004 | unified_systemd_v232 = false; |
f08e9287 | 2005 | } else { |
2977724b | 2006 | if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0) |
9aa21133 | 2007 | return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m"); |
5535d8f7 EV |
2008 | |
2009 | if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) { | |
2010 | log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)"); | |
2011 | unified_cache = CGROUP_UNIFIED_SYSTEMD; | |
2012 | unified_systemd_v232 = true; | |
2013 | } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)) { | |
2014 | log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy"); | |
2015 | unified_cache = CGROUP_UNIFIED_NONE; | |
2016 | } else { | |
2017 | log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy", | |
9aa21133 | 2018 | (unsigned long long) fs.f_type); |
5535d8f7 | 2019 | unified_cache = CGROUP_UNIFIED_NONE; |
9aa21133 | 2020 | } |
2977724b | 2021 | } |
0bc5f001 DS |
2022 | } else if (F_TYPE_EQUAL(fs.f_type, SYSFS_MAGIC)) { |
2023 | return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), | |
2024 | "No filesystem is currently mounted on /sys/fs/cgroup."); | |
baaa35ad ZJS |
2025 | } else |
2026 | return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM), | |
2027 | "Unknown filesystem type %llx mounted on /sys/fs/cgroup.", | |
2028 | (unsigned long long)fs.f_type); | |
efdb0237 | 2029 | |
d4d99bc6 | 2030 | return unified_cache; |
5da38d07 TH |
2031 | } |
2032 | ||
c22800e4 | 2033 | int cg_unified_controller(const char *controller) { |
b4cccbc1 | 2034 | int r; |
5da38d07 | 2035 | |
d4d99bc6 | 2036 | r = cg_unified_cached(false); |
b4cccbc1 LP |
2037 | if (r < 0) |
2038 | return r; | |
5da38d07 | 2039 | |
d4d99bc6 | 2040 | if (r == CGROUP_UNIFIED_NONE) |
fc9ae717 LP |
2041 | return false; |
2042 | ||
d4d99bc6 | 2043 | if (r >= CGROUP_UNIFIED_ALL) |
fc9ae717 LP |
2044 | return true; |
2045 | ||
2046 | return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER); | |
5da38d07 TH |
2047 | } |
2048 | ||
b4cccbc1 | 2049 | int cg_all_unified(void) { |
4bb652ac LP |
2050 | int r; |
2051 | ||
d4d99bc6 | 2052 | r = cg_unified_cached(false); |
4bb652ac LP |
2053 | if (r < 0) |
2054 | return r; | |
2055 | ||
d4d99bc6 | 2056 | return r >= CGROUP_UNIFIED_ALL; |
efdb0237 LP |
2057 | } |
2058 | ||
b4cccbc1 LP |
2059 | int cg_hybrid_unified(void) { |
2060 | int r; | |
2977724b | 2061 | |
d4d99bc6 | 2062 | r = cg_unified_cached(false); |
b4cccbc1 LP |
2063 | if (r < 0) |
2064 | return r; | |
2977724b | 2065 | |
d4d99bc6 | 2066 | return r == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232; |
13c31542 TH |
2067 | } |
2068 | ||
9be57249 TH |
2069 | const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = { |
2070 | [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX, | |
2071 | [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX, | |
ac06a0cf TH |
2072 | [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX, |
2073 | [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX, | |
9be57249 TH |
2074 | }; |
2075 | ||
2076 | static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = { | |
2077 | [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax", | |
2078 | [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax", | |
ac06a0cf TH |
2079 | [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax", |
2080 | [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax", | |
9be57249 TH |
2081 | }; |
2082 | ||
2083 | DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType); | |
2084 | ||
f0bef277 EV |
2085 | bool is_cgroup_fs(const struct statfs *s) { |
2086 | return is_fs_type(s, CGROUP_SUPER_MAGIC) || | |
2087 | is_fs_type(s, CGROUP2_SUPER_MAGIC); | |
2088 | } | |
2089 | ||
2090 | bool fd_is_cgroup_fs(int fd) { | |
2091 | struct statfs s; | |
2092 | ||
2093 | if (fstatfs(fd, &s) < 0) | |
2094 | return -errno; | |
2095 | ||
2096 | return is_cgroup_fs(&s); | |
2097 | } | |
2098 | ||
b82f71c7 | 2099 | static const char *const cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = { |
efdb0237 LP |
2100 | [CGROUP_CONTROLLER_CPU] = "cpu", |
2101 | [CGROUP_CONTROLLER_CPUACCT] = "cpuacct", | |
047f5d63 | 2102 | [CGROUP_CONTROLLER_CPUSET] = "cpuset", |
13c31542 | 2103 | [CGROUP_CONTROLLER_IO] = "io", |
efdb0237 LP |
2104 | [CGROUP_CONTROLLER_BLKIO] = "blkio", |
2105 | [CGROUP_CONTROLLER_MEMORY] = "memory", | |
3905f127 | 2106 | [CGROUP_CONTROLLER_DEVICES] = "devices", |
03a7b521 | 2107 | [CGROUP_CONTROLLER_PIDS] = "pids", |
17f14955 | 2108 | [CGROUP_CONTROLLER_BPF_FIREWALL] = "bpf-firewall", |
084c7007 | 2109 | [CGROUP_CONTROLLER_BPF_DEVICES] = "bpf-devices", |
efdb0237 LP |
2110 | }; |
2111 | ||
2112 | DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController); | |
f98c2585 CD |
2113 | |
2114 | CGroupMask get_cpu_accounting_mask(void) { | |
2115 | static CGroupMask needed_mask = (CGroupMask) -1; | |
2116 | ||
2117 | /* On kernel ≥4.15 with unified hierarchy, cpu.stat's usage_usec is | |
2118 | * provided externally from the CPU controller, which means we don't | |
2119 | * need to enable the CPU controller just to get metrics. This is good, | |
2120 | * because enabling the CPU controller comes at a minor performance | |
2121 | * hit, especially when it's propagated deep into large hierarchies. | |
2122 | * There's also no separate CPU accounting controller available within | |
2123 | * a unified hierarchy. | |
2124 | * | |
2125 | * This combination of factors results in the desired cgroup mask to | |
2126 | * enable for CPU accounting varying as follows: | |
2127 | * | |
2128 | * ╔═════════════════════╤═════════════════════╗ | |
2129 | * ║ Linux ≥4.15 │ Linux <4.15 ║ | |
2130 | * ╔═══════════════╬═════════════════════╪═════════════════════╣ | |
2131 | * ║ Unified ║ nothing │ CGROUP_MASK_CPU ║ | |
2132 | * ╟───────────────╫─────────────────────┼─────────────────────╢ | |
2133 | * ║ Hybrid/Legacy ║ CGROUP_MASK_CPUACCT │ CGROUP_MASK_CPUACCT ║ | |
2134 | * ╚═══════════════╩═════════════════════╧═════════════════════╝ | |
2135 | * | |
2136 | * We check kernel version here instead of manually checking whether | |
2137 | * cpu.stat is present for every cgroup, as that check in itself would | |
2138 | * already be fairly expensive. | |
2139 | * | |
2140 | * Kernels where this patch has been backported will therefore have the | |
2141 | * CPU controller enabled unnecessarily. This is more expensive than | |
2142 | * necessary, but harmless. ☺️ | |
2143 | */ | |
2144 | ||
2145 | if (needed_mask == (CGroupMask) -1) { | |
2146 | if (cg_all_unified()) { | |
2147 | struct utsname u; | |
2148 | assert_se(uname(&u) >= 0); | |
2149 | ||
2150 | if (str_verscmp(u.release, "4.15") < 0) | |
2151 | needed_mask = CGROUP_MASK_CPU; | |
2152 | else | |
2153 | needed_mask = 0; | |
2154 | } else | |
2155 | needed_mask = CGROUP_MASK_CPUACCT; | |
2156 | } | |
2157 | ||
2158 | return needed_mask; | |
2159 | } | |
2160 | ||
2161 | bool cpu_accounting_is_cheap(void) { | |
2162 | return get_cpu_accounting_mask() == 0; | |
2163 | } |