]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/cgroup-util.c
cgroup: assume the use of v1 when all the preceding checks fail (#7366)
[thirdparty/systemd.git] / src / basic / cgroup-util.c
CommitLineData
8c6db833
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
8c6db833
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
8c6db833 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
8c6db833
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
84ac7bea 20#include <dirent.h>
8c6db833 21#include <errno.h>
84ac7bea 22#include <ftw.h>
11c3a366 23#include <limits.h>
8c6db833 24#include <signal.h>
11c3a366 25#include <stddef.h>
8c6db833 26#include <stdlib.h>
84ac7bea 27#include <string.h>
672c48cc 28#include <sys/stat.h>
11c3a366 29#include <sys/statfs.h>
672c48cc 30#include <sys/types.h>
4b58153d 31#include <sys/xattr.h>
84ac7bea 32#include <unistd.h>
8c6db833 33
b5efdb8a 34#include "alloc-util.h"
3ffd4af2 35#include "cgroup-util.h"
93cc7779 36#include "def.h"
a0956174 37#include "dirent-util.h"
84ac7bea 38#include "extract-word.h"
3ffd4af2 39#include "fd-util.h"
84ac7bea 40#include "fileio.h"
f97b34a6 41#include "format-util.h"
f4f15635 42#include "fs-util.h"
93cc7779 43#include "log.h"
84ac7bea
LP
44#include "login-util.h"
45#include "macro.h"
93cc7779 46#include "missing.h"
84ac7bea 47#include "mkdir.h"
6bedfcbb 48#include "parse-util.h"
9eb977db 49#include "path-util.h"
872a590e 50#include "proc-cmdline.h"
84ac7bea
LP
51#include "process-util.h"
52#include "set.h"
9444b1f2 53#include "special.h"
872a590e 54#include "stat-util.h"
d054f0a4 55#include "stdio-util.h"
8b43440b 56#include "string-table.h"
07630cea 57#include "string-util.h"
aae7e17f 58#include "strv.h"
84ac7bea 59#include "unit-name.h"
b1d4f8e1 60#include "user-util.h"
8c6db833 61
c6c18be3 62int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
7027ff61 63 _cleanup_free_ char *fs = NULL;
c6c18be3 64 FILE *f;
7027ff61 65 int r;
c6c18be3 66
c6c18be3
LP
67 assert(_f);
68
c3175a7f
LP
69 r = cg_get_path(controller, path, "cgroup.procs", &fs);
70 if (r < 0)
c6c18be3
LP
71 return r;
72
73 f = fopen(fs, "re");
c6c18be3
LP
74 if (!f)
75 return -errno;
76
77 *_f = f;
78 return 0;
79}
80
c6c18be3
LP
81int cg_read_pid(FILE *f, pid_t *_pid) {
82 unsigned long ul;
83
84 /* Note that the cgroup.procs might contain duplicates! See
85 * cgroups.txt for details. */
86
7027ff61
LP
87 assert(f);
88 assert(_pid);
89
c6c18be3
LP
90 errno = 0;
91 if (fscanf(f, "%lu", &ul) != 1) {
92
93 if (feof(f))
94 return 0;
95
f5e5c28f 96 return errno > 0 ? -errno : -EIO;
c6c18be3
LP
97 }
98
99 if (ul <= 0)
100 return -EIO;
101
102 *_pid = (pid_t) ul;
103 return 1;
104}
105
8b238b13
LP
106int cg_read_event(
107 const char *controller,
108 const char *path,
109 const char *event,
110 char **val) {
111
ab2c3861
TH
112 _cleanup_free_ char *events = NULL, *content = NULL;
113 char *p, *line;
114 int r;
115
116 r = cg_get_path(controller, path, "cgroup.events", &events);
117 if (r < 0)
118 return r;
119
120 r = read_full_file(events, &content, NULL);
121 if (r < 0)
122 return r;
123
124 p = content;
125 while ((line = strsep(&p, "\n"))) {
126 char *key;
127
128 key = strsep(&line, " ");
129 if (!key || !line)
130 return -EINVAL;
131
132 if (strcmp(key, event))
133 continue;
134
135 *val = strdup(line);
136 return 0;
137 }
138
139 return -ENOENT;
140}
141
3228995c
CB
142bool cg_ns_supported(void) {
143 static thread_local int enabled = -1;
144
145 if (enabled >= 0)
146 return enabled;
147
148 if (access("/proc/self/ns/cgroup", F_OK) == 0)
149 enabled = 1;
150 else
151 enabled = 0;
152
153 return enabled;
154}
155
35d2e7ec 156int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
7027ff61 157 _cleanup_free_ char *fs = NULL;
35d2e7ec
LP
158 int r;
159 DIR *d;
160
35d2e7ec
LP
161 assert(_d);
162
163 /* This is not recursive! */
164
c3175a7f
LP
165 r = cg_get_path(controller, path, NULL, &fs);
166 if (r < 0)
35d2e7ec
LP
167 return r;
168
169 d = opendir(fs);
35d2e7ec
LP
170 if (!d)
171 return -errno;
172
173 *_d = d;
174 return 0;
175}
176
177int cg_read_subgroup(DIR *d, char **fn) {
178 struct dirent *de;
179
180 assert(d);
7027ff61 181 assert(fn);
35d2e7ec 182
f01327ad 183 FOREACH_DIRENT_ALL(de, d, return -errno) {
35d2e7ec
LP
184 char *b;
185
186 if (de->d_type != DT_DIR)
187 continue;
188
49bfc877 189 if (dot_or_dot_dot(de->d_name))
35d2e7ec
LP
190 continue;
191
7027ff61
LP
192 b = strdup(de->d_name);
193 if (!b)
35d2e7ec
LP
194 return -ENOMEM;
195
196 *fn = b;
197 return 1;
198 }
199
35d2e7ec
LP
200 return 0;
201}
202
4ad49000 203int cg_rmdir(const char *controller, const char *path) {
7027ff61 204 _cleanup_free_ char *p = NULL;
35d2e7ec
LP
205 int r;
206
ad293f5a
LP
207 r = cg_get_path(controller, path, NULL, &p);
208 if (r < 0)
35d2e7ec
LP
209 return r;
210
211 r = rmdir(p);
7027ff61
LP
212 if (r < 0 && errno != ENOENT)
213 return -errno;
35d2e7ec 214
b4cccbc1
LP
215 r = cg_hybrid_unified();
216 if (r < 0)
217 return r;
218 if (r == 0)
219 return 0;
220
221 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
2977724b
TH
222 r = cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
223 if (r < 0)
224 log_warning_errno(r, "Failed to remove compat systemd cgroup %s: %m", path);
225 }
226
7027ff61 227 return 0;
35d2e7ec
LP
228}
229
1d98fef1
LP
230int cg_kill(
231 const char *controller,
232 const char *path,
233 int sig,
234 CGroupFlags flags,
235 Set *s,
236 cg_kill_log_func_t log_kill,
237 void *userdata) {
238
7027ff61 239 _cleanup_set_free_ Set *allocated_set = NULL;
35d2e7ec 240 bool done = false;
8c6db833 241 int r, ret = 0;
35d2e7ec 242 pid_t my_pid;
8c6db833 243
8c6db833
LP
244 assert(sig >= 0);
245
0d5b4810
LP
246 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
247 * SIGCONT on SIGKILL. */
248 if (IN_SET(sig, SIGCONT, SIGKILL))
249 flags &= ~CGROUP_SIGCONT;
250
8c6db833
LP
251 /* This goes through the tasks list and kills them all. This
252 * is repeated until no further processes are added to the
253 * tasks list, to properly handle forking processes */
254
7027ff61 255 if (!s) {
d5099efc 256 s = allocated_set = set_new(NULL);
7027ff61 257 if (!s)
ca949c9d 258 return -ENOMEM;
7027ff61 259 }
8c6db833 260
df0ff127 261 my_pid = getpid_cached();
8c6db833
LP
262
263 do {
7027ff61 264 _cleanup_fclose_ FILE *f = NULL;
0b172489 265 pid_t pid = 0;
8c6db833
LP
266 done = true;
267
7027ff61
LP
268 r = cg_enumerate_processes(controller, path, &f);
269 if (r < 0) {
4c633005 270 if (ret >= 0 && r != -ENOENT)
7027ff61 271 return r;
35d2e7ec 272
7027ff61 273 return ret;
35d2e7ec 274 }
c6c18be3
LP
275
276 while ((r = cg_read_pid(f, &pid)) > 0) {
8c6db833 277
1d98fef1 278 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
c6c18be3 279 continue;
8c6db833 280
fea72cc0 281 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
c6c18be3 282 continue;
8c6db833 283
1d98fef1
LP
284 if (log_kill)
285 log_kill(pid, sig, userdata);
286
8c6db833
LP
287 /* If we haven't killed this process yet, kill
288 * it */
4c633005
LP
289 if (kill(pid, sig) < 0) {
290 if (ret >= 0 && errno != ESRCH)
8c6db833 291 ret = -errno;
6e8314c4 292 } else {
1d98fef1 293 if (flags & CGROUP_SIGCONT)
e155a0aa 294 (void) kill(pid, SIGCONT);
430c18ed 295
6e8314c4
LP
296 if (ret == 0)
297 ret = 1;
430c18ed 298 }
8c6db833 299
8c6db833
LP
300 done = false;
301
fea72cc0 302 r = set_put(s, PID_TO_PTR(pid));
7027ff61 303 if (r < 0) {
35d2e7ec 304 if (ret >= 0)
7027ff61 305 return r;
35d2e7ec 306
7027ff61 307 return ret;
35d2e7ec
LP
308 }
309 }
310
311 if (r < 0) {
312 if (ret >= 0)
7027ff61 313 return r;
35d2e7ec 314
7027ff61 315 return ret;
8c6db833
LP
316 }
317
8c6db833
LP
318 /* To avoid racing against processes which fork
319 * quicker than we can kill them we repeat this until
320 * no new pids need to be killed. */
321
35d2e7ec 322 } while (!done);
8c6db833 323
35d2e7ec 324 return ret;
8c6db833
LP
325}
326
1d98fef1
LP
327int cg_kill_recursive(
328 const char *controller,
329 const char *path,
330 int sig,
331 CGroupFlags flags,
332 Set *s,
333 cg_kill_log_func_t log_kill,
334 void *userdata) {
335
7027ff61
LP
336 _cleanup_set_free_ Set *allocated_set = NULL;
337 _cleanup_closedir_ DIR *d = NULL;
e155a0aa 338 int r, ret;
35d2e7ec 339 char *fn;
8c6db833
LP
340
341 assert(path);
8c6db833
LP
342 assert(sig >= 0);
343
7027ff61 344 if (!s) {
d5099efc 345 s = allocated_set = set_new(NULL);
7027ff61 346 if (!s)
ca949c9d 347 return -ENOMEM;
7027ff61 348 }
ca949c9d 349
1d98fef1 350 ret = cg_kill(controller, path, sig, flags, s, log_kill, userdata);
8c6db833 351
7027ff61
LP
352 r = cg_enumerate_subgroups(controller, path, &d);
353 if (r < 0) {
4c633005 354 if (ret >= 0 && r != -ENOENT)
7027ff61 355 return r;
8c6db833 356
7027ff61 357 return ret;
35d2e7ec 358 }
8c6db833 359
35d2e7ec 360 while ((r = cg_read_subgroup(d, &fn)) > 0) {
7027ff61 361 _cleanup_free_ char *p = NULL;
8c6db833 362
605405c6 363 p = strjoin(path, "/", fn);
35d2e7ec 364 free(fn);
7027ff61
LP
365 if (!p)
366 return -ENOMEM;
8c6db833 367
1d98fef1 368 r = cg_kill_recursive(controller, p, sig, flags, s, log_kill, userdata);
e155a0aa 369 if (r != 0 && ret >= 0)
35d2e7ec 370 ret = r;
8c6db833 371 }
7027ff61 372 if (ret >= 0 && r < 0)
35d2e7ec
LP
373 ret = r;
374
1d98fef1 375 if (flags & CGROUP_REMOVE) {
4ad49000 376 r = cg_rmdir(controller, path);
4c701096 377 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
7027ff61
LP
378 return r;
379 }
ca949c9d 380
8c6db833
LP
381 return ret;
382}
383
1d98fef1
LP
384int cg_migrate(
385 const char *cfrom,
386 const char *pfrom,
387 const char *cto,
388 const char *pto,
389 CGroupFlags flags) {
390
35d2e7ec 391 bool done = false;
246aa6dd 392 _cleanup_set_free_ Set *s = NULL;
8c6db833
LP
393 int r, ret = 0;
394 pid_t my_pid;
395
246aa6dd
LP
396 assert(cfrom);
397 assert(pfrom);
398 assert(cto);
399 assert(pto);
8c6db833 400
d5099efc 401 s = set_new(NULL);
246aa6dd 402 if (!s)
35d2e7ec
LP
403 return -ENOMEM;
404
df0ff127 405 my_pid = getpid_cached();
8c6db833
LP
406
407 do {
7027ff61 408 _cleanup_fclose_ FILE *f = NULL;
0b172489 409 pid_t pid = 0;
8c6db833
LP
410 done = true;
411
b043cd0b 412 r = cg_enumerate_processes(cfrom, pfrom, &f);
246aa6dd 413 if (r < 0) {
4c633005 414 if (ret >= 0 && r != -ENOENT)
7027ff61 415 return r;
35d2e7ec 416
246aa6dd 417 return ret;
35d2e7ec 418 }
c6c18be3
LP
419
420 while ((r = cg_read_pid(f, &pid)) > 0) {
8c6db833 421
35d2e7ec
LP
422 /* This might do weird stuff if we aren't a
423 * single-threaded program. However, we
424 * luckily know we are not */
1d98fef1 425 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
c6c18be3 426 continue;
8c6db833 427
fea72cc0 428 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
35d2e7ec
LP
429 continue;
430
9b84c7f9
LP
431 /* Ignore kernel threads. Since they can only
432 * exist in the root cgroup, we only check for
433 * them there. */
434 if (cfrom &&
435 (isempty(pfrom) || path_equal(pfrom, "/")) &&
436 is_kernel_thread(pid) > 0)
437 continue;
438
246aa6dd
LP
439 r = cg_attach(cto, pto, pid);
440 if (r < 0) {
4c633005 441 if (ret >= 0 && r != -ESRCH)
35d2e7ec
LP
442 ret = r;
443 } else if (ret == 0)
444 ret = 1;
8c6db833 445
8c6db833 446 done = false;
35d2e7ec 447
fea72cc0 448 r = set_put(s, PID_TO_PTR(pid));
246aa6dd 449 if (r < 0) {
35d2e7ec 450 if (ret >= 0)
7027ff61 451 return r;
35d2e7ec 452
246aa6dd 453 return ret;
35d2e7ec
LP
454 }
455 }
456
457 if (r < 0) {
458 if (ret >= 0)
7027ff61 459 return r;
35d2e7ec 460
246aa6dd 461 return ret;
8c6db833 462 }
35d2e7ec 463 } while (!done);
8c6db833 464
35d2e7ec 465 return ret;
8c6db833
LP
466}
467
4ad49000
LP
468int cg_migrate_recursive(
469 const char *cfrom,
470 const char *pfrom,
471 const char *cto,
472 const char *pto,
1d98fef1 473 CGroupFlags flags) {
4ad49000 474
246aa6dd 475 _cleanup_closedir_ DIR *d = NULL;
7027ff61 476 int r, ret = 0;
35d2e7ec 477 char *fn;
8c6db833 478
246aa6dd
LP
479 assert(cfrom);
480 assert(pfrom);
481 assert(cto);
482 assert(pto);
8c6db833 483
1d98fef1 484 ret = cg_migrate(cfrom, pfrom, cto, pto, flags);
8c6db833 485
246aa6dd
LP
486 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
487 if (r < 0) {
4c633005 488 if (ret >= 0 && r != -ENOENT)
7027ff61
LP
489 return r;
490
246aa6dd 491 return ret;
35d2e7ec
LP
492 }
493
494 while ((r = cg_read_subgroup(d, &fn)) > 0) {
246aa6dd 495 _cleanup_free_ char *p = NULL;
8c6db833 496
605405c6 497 p = strjoin(pfrom, "/", fn);
35d2e7ec 498 free(fn);
e155a0aa
LP
499 if (!p)
500 return -ENOMEM;
8c6db833 501
1d98fef1 502 r = cg_migrate_recursive(cfrom, p, cto, pto, flags);
35d2e7ec
LP
503 if (r != 0 && ret >= 0)
504 ret = r;
8c6db833
LP
505 }
506
35d2e7ec
LP
507 if (r < 0 && ret >= 0)
508 ret = r;
509
1d98fef1 510 if (flags & CGROUP_REMOVE) {
4ad49000 511 r = cg_rmdir(cfrom, pfrom);
4c701096 512 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
246aa6dd
LP
513 return r;
514 }
8c6db833
LP
515
516 return ret;
517}
518
13b84ec7
LP
519int cg_migrate_recursive_fallback(
520 const char *cfrom,
521 const char *pfrom,
522 const char *cto,
523 const char *pto,
1d98fef1 524 CGroupFlags flags) {
13b84ec7
LP
525
526 int r;
527
528 assert(cfrom);
529 assert(pfrom);
530 assert(cto);
531 assert(pto);
532
1d98fef1 533 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, flags);
13b84ec7
LP
534 if (r < 0) {
535 char prefix[strlen(pto) + 1];
536
537 /* This didn't work? Then let's try all prefixes of the destination */
538
fecffe5d 539 PATH_FOREACH_PREFIX(prefix, pto) {
e155a0aa
LP
540 int q;
541
1d98fef1 542 q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, flags);
e155a0aa
LP
543 if (q >= 0)
544 return q;
13b84ec7
LP
545 }
546 }
547
e155a0aa 548 return r;
13b84ec7
LP
549}
550
efdb0237
LP
551static const char *controller_to_dirname(const char *controller) {
552 const char *e;
3474ae3c 553
7027ff61
LP
554 assert(controller);
555
efdb0237
LP
556 /* Converts a controller name to the directory name below
557 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
558 * just cuts off the name= prefixed used for named
559 * hierarchies, if it is specified. */
560
2977724b 561 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
b4cccbc1 562 if (cg_hybrid_unified() > 0)
2977724b
TH
563 controller = SYSTEMD_CGROUP_CONTROLLER_HYBRID;
564 else
565 controller = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
566 }
b6629c4b 567
efdb0237
LP
568 e = startswith(controller, "name=");
569 if (e)
570 return e;
571
572 return controller;
3474ae3c
LP
573}
574
569b19d8
LP
575static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) {
576 const char *dn;
018ef268 577 char *t = NULL;
3474ae3c 578
efdb0237 579 assert(fs);
569b19d8
LP
580 assert(controller);
581
582 dn = controller_to_dirname(controller);
efdb0237
LP
583
584 if (isempty(path) && isempty(suffix))
569b19d8 585 t = strappend("/sys/fs/cgroup/", dn);
efdb0237 586 else if (isempty(path))
605405c6 587 t = strjoin("/sys/fs/cgroup/", dn, "/", suffix);
efdb0237 588 else if (isempty(suffix))
605405c6 589 t = strjoin("/sys/fs/cgroup/", dn, "/", path);
efdb0237 590 else
605405c6 591 t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix);
efdb0237
LP
592 if (!t)
593 return -ENOMEM;
3474ae3c 594
efdb0237
LP
595 *fs = t;
596 return 0;
597}
598
599static int join_path_unified(const char *path, const char *suffix, char **fs) {
600 char *t;
601
602 assert(fs);
603
604 if (isempty(path) && isempty(suffix))
605 t = strdup("/sys/fs/cgroup");
606 else if (isempty(path))
607 t = strappend("/sys/fs/cgroup/", suffix);
608 else if (isempty(suffix))
609 t = strappend("/sys/fs/cgroup/", path);
610 else
605405c6 611 t = strjoin("/sys/fs/cgroup/", path, "/", suffix);
3474ae3c
LP
612 if (!t)
613 return -ENOMEM;
614
efdb0237 615 *fs = t;
3474ae3c
LP
616 return 0;
617}
618
8c6db833 619int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
415fc41c 620 int r;
8c6db833 621
dbd821ac
LP
622 assert(fs);
623
efdb0237
LP
624 if (!controller) {
625 char *t;
626
569b19d8
LP
627 /* If no controller is specified, we return the path
628 * *below* the controllers, without any prefix. */
efdb0237
LP
629
630 if (!path && !suffix)
631 return -EINVAL;
632
989189ea 633 if (!suffix)
efdb0237 634 t = strdup(path);
989189ea 635 else if (!path)
efdb0237
LP
636 t = strdup(suffix);
637 else
605405c6 638 t = strjoin(path, "/", suffix);
efdb0237
LP
639 if (!t)
640 return -ENOMEM;
641
642 *fs = path_kill_slashes(t);
643 return 0;
644 }
645
646 if (!cg_controller_is_valid(controller))
78edb35a
LP
647 return -EINVAL;
648
b4cccbc1
LP
649 r = cg_all_unified();
650 if (r < 0)
651 return r;
652 if (r > 0)
efdb0237 653 r = join_path_unified(path, suffix, fs);
569b19d8
LP
654 else
655 r = join_path_legacy(controller, path, suffix, fs);
efdb0237
LP
656 if (r < 0)
657 return r;
7027ff61 658
efdb0237
LP
659 path_kill_slashes(*fs);
660 return 0;
3474ae3c 661}
dbd821ac 662
efdb0237 663static int controller_is_accessible(const char *controller) {
b4cccbc1 664 int r;
37099707 665
efdb0237 666 assert(controller);
37099707 667
efdb0237
LP
668 /* Checks whether a specific controller is accessible,
669 * i.e. its hierarchy mounted. In the unified hierarchy all
670 * controllers are considered accessible, except for the named
671 * hierarchies */
b12afc8c 672
efdb0237
LP
673 if (!cg_controller_is_valid(controller))
674 return -EINVAL;
675
b4cccbc1
LP
676 r = cg_all_unified();
677 if (r < 0)
678 return r;
679 if (r > 0) {
efdb0237
LP
680 /* We don't support named hierarchies if we are using
681 * the unified hierarchy. */
682
683 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
684 return 0;
685
686 if (startswith(controller, "name="))
687 return -EOPNOTSUPP;
688
689 } else {
690 const char *cc, *dn;
691
692 dn = controller_to_dirname(controller);
693 cc = strjoina("/sys/fs/cgroup/", dn);
694
695 if (laccess(cc, F_OK) < 0)
696 return -errno;
697 }
37099707
LP
698
699 return 0;
700}
701
3474ae3c 702int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
37099707 703 int r;
dbd821ac 704
efdb0237 705 assert(controller);
3474ae3c 706 assert(fs);
70132bd0 707
efdb0237
LP
708 /* Check if the specified controller is actually accessible */
709 r = controller_is_accessible(controller);
37099707
LP
710 if (r < 0)
711 return r;
3474ae3c 712
efdb0237 713 return cg_get_path(controller, path, suffix, fs);
8c6db833
LP
714}
715
e27796a0 716static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
4ad49000
LP
717 assert(path);
718 assert(sb);
719 assert(ftwbuf);
e27796a0
LP
720
721 if (typeflag != FTW_DP)
722 return 0;
723
724 if (ftwbuf->level < 1)
725 return 0;
726
e155a0aa 727 (void) rmdir(path);
e27796a0
LP
728 return 0;
729}
730
8c6db833 731int cg_trim(const char *controller, const char *path, bool delete_root) {
7027ff61 732 _cleanup_free_ char *fs = NULL;
2977724b 733 int r = 0, q;
8c6db833 734
8c6db833
LP
735 assert(path);
736
e27796a0
LP
737 r = cg_get_path(controller, path, NULL, &fs);
738 if (r < 0)
8c6db833
LP
739 return r;
740
e27796a0 741 errno = 0;
e155a0aa
LP
742 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
743 if (errno == ENOENT)
744 r = 0;
b3267152 745 else if (errno > 0)
e155a0aa
LP
746 r = -errno;
747 else
748 r = -EIO;
749 }
e27796a0
LP
750
751 if (delete_root) {
4ad49000
LP
752 if (rmdir(fs) < 0 && errno != ENOENT)
753 return -errno;
e27796a0
LP
754 }
755
b4cccbc1
LP
756 q = cg_hybrid_unified();
757 if (q < 0)
758 return q;
759 if (q > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
2977724b
TH
760 q = cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, delete_root);
761 if (q < 0)
762 log_warning_errno(q, "Failed to trim compat systemd cgroup %s: %m", path);
763 }
764
e27796a0 765 return r;
8c6db833
LP
766}
767
1434ae6f
LP
768int cg_create(const char *controller, const char *path) {
769 _cleanup_free_ char *fs = NULL;
770 int r;
771
772 r = cg_get_path_and_check(controller, path, NULL, &fs);
773 if (r < 0)
774 return r;
775
776 r = mkdir_parents(fs, 0755);
777 if (r < 0)
778 return r;
779
780 if (mkdir(fs, 0755) < 0) {
781
782 if (errno == EEXIST)
783 return 0;
784
785 return -errno;
786 }
787
b4cccbc1
LP
788 r = cg_hybrid_unified();
789 if (r < 0)
790 return r;
791
792 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
2977724b
TH
793 r = cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
794 if (r < 0)
795 log_warning_errno(r, "Failed to create compat systemd cgroup %s: %m", path);
796 }
797
1434ae6f
LP
798 return 1;
799}
800
801int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
802 int r, q;
803
804 assert(pid >= 0);
805
806 r = cg_create(controller, path);
807 if (r < 0)
808 return r;
809
810 q = cg_attach(controller, path, pid);
811 if (q < 0)
812 return q;
813
814 /* This does not remove the cgroup on failure */
815 return r;
816}
817
8c6db833 818int cg_attach(const char *controller, const char *path, pid_t pid) {
574d5f2d
LP
819 _cleanup_free_ char *fs = NULL;
820 char c[DECIMAL_STR_MAX(pid_t) + 2];
8c6db833
LP
821 int r;
822
8c6db833
LP
823 assert(path);
824 assert(pid >= 0);
825
b043cd0b 826 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
3474ae3c 827 if (r < 0)
c6c18be3 828 return r;
8c6db833
LP
829
830 if (pid == 0)
df0ff127 831 pid = getpid_cached();
8c6db833 832
d054f0a4 833 xsprintf(c, PID_FMT "\n", pid);
8c6db833 834
2977724b
TH
835 r = write_string_file(fs, c, 0);
836 if (r < 0)
837 return r;
838
b4cccbc1
LP
839 r = cg_hybrid_unified();
840 if (r < 0)
841 return r;
842
843 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
2977724b
TH
844 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, pid);
845 if (r < 0)
bd68e99b 846 log_warning_errno(r, "Failed to attach "PID_FMT" to compat systemd cgroup %s: %m", pid, path);
2977724b
TH
847 }
848
849 return 0;
8c6db833
LP
850}
851
13b84ec7
LP
852int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
853 int r;
854
855 assert(controller);
856 assert(path);
857 assert(pid >= 0);
858
859 r = cg_attach(controller, path, pid);
860 if (r < 0) {
861 char prefix[strlen(path) + 1];
862
863 /* This didn't work? Then let's try all prefixes of
864 * the destination */
865
fecffe5d 866 PATH_FOREACH_PREFIX(prefix, path) {
e155a0aa
LP
867 int q;
868
869 q = cg_attach(controller, prefix, pid);
870 if (q >= 0)
871 return q;
13b84ec7
LP
872 }
873 }
874
e155a0aa 875 return r;
13b84ec7
LP
876}
877
2d76d14e
LP
878int cg_set_group_access(
879 const char *controller,
880 const char *path,
881 mode_t mode,
882 uid_t uid,
883 gid_t gid) {
884
574d5f2d 885 _cleanup_free_ char *fs = NULL;
8c6db833
LP
886 int r;
887
e155a0aa
LP
888 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
889 return 0;
8c6db833 890
fed1e721 891 if (mode != MODE_INVALID)
8d53b453
LP
892 mode &= 0777;
893
894 r = cg_get_path(controller, path, NULL, &fs);
895 if (r < 0)
8c6db833
LP
896 return r;
897
2977724b
TH
898 r = chmod_and_chown(fs, mode, uid, gid);
899 if (r < 0)
900 return r;
901
b4cccbc1
LP
902 r = cg_hybrid_unified();
903 if (r < 0)
904 return r;
905 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
2977724b
TH
906 r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid);
907 if (r < 0)
5beac75e 908 log_debug_errno(r, "Failed to set group access on compatibility systemd cgroup %s, ignoring: %m", path);
2977724b
TH
909 }
910
911 return 0;
8c6db833
LP
912}
913
974efc46
LP
914int cg_set_task_access(
915 const char *controller,
916 const char *path,
917 mode_t mode,
918 uid_t uid,
4ad49000 919 gid_t gid) {
974efc46 920
40853aa5 921 _cleanup_free_ char *fs = NULL;
415fc41c 922 int r;
8c6db833 923
8c6db833
LP
924 assert(path);
925
fed1e721 926 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
8d53b453
LP
927 return 0;
928
fed1e721 929 if (mode != MODE_INVALID)
8d53b453
LP
930 mode &= 0666;
931
40853aa5 932 /* For both the legacy and unified hierarchies, "cgroup.procs" is the main entry point for PIDs */
b043cd0b 933 r = cg_get_path(controller, path, "cgroup.procs", &fs);
8d53b453 934 if (r < 0)
8c6db833
LP
935 return r;
936
937 r = chmod_and_chown(fs, mode, uid, gid);
974efc46
LP
938 if (r < 0)
939 return r;
8c6db833 940
c22800e4 941 r = cg_unified_controller(controller);
b4cccbc1
LP
942 if (r < 0)
943 return r;
944 if (r == 0) {
40853aa5
LP
945 const char *fn;
946
947 /* Compatibility: on cgroupsv1 always keep values for the legacy files "tasks" and
948 * "cgroup.clone_children" in sync with "cgroup.procs". Since this is legacy stuff, we don't care if
949 * this fails. */
950
951 FOREACH_STRING(fn,
952 "tasks",
953 "cgroup.clone_children") {
954
955 fs = mfree(fs);
956
957 r = cg_get_path(controller, path, fn, &fs);
958 if (r < 0)
959 log_debug_errno(r, "Failed to get path for %s of %s, ignoring: %m", fn, path);
960
961 r = chmod_and_chown(fs, mode, uid, gid);
962 if (r < 0)
963 log_debug_errno(r, "Failed to to change ownership/access mode for %s of %s, ignoring: %m", fn, path);
964 }
965 } else {
966 /* On the unified controller, we want to permit subtree controllers too. */
967
968 fs = mfree(fs);
969 r = cg_get_path(controller, path, "cgroup.subtree_control", &fs);
970 if (r < 0)
971 return r;
972
973 r = chmod_and_chown(fs, mode, uid, gid);
974 if (r < 0)
975 return r;
2977724b 976 }
efdb0237 977
b4cccbc1
LP
978 r = cg_hybrid_unified();
979 if (r < 0)
980 return r;
981 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
5beac75e
LP
982 /* Always propagate access mode from unified to legacy controller */
983
2977724b
TH
984 r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid);
985 if (r < 0)
5beac75e 986 log_debug_errno(r, "Failed to set task access on compatibility systemd cgroup %s, ignoring: %m", path);
2977724b 987 }
974efc46 988
efdb0237 989 return 0;
8c6db833
LP
990}
991
4b58153d
LP
992int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) {
993 _cleanup_free_ char *fs = NULL;
994 int r;
995
996 assert(path);
997 assert(name);
998 assert(value || size <= 0);
999
1000 r = cg_get_path(controller, path, NULL, &fs);
1001 if (r < 0)
1002 return r;
1003
1004 if (setxattr(fs, name, value, size, flags) < 0)
1005 return -errno;
1006
1007 return 0;
1008}
1009
1010int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
1011 _cleanup_free_ char *fs = NULL;
1012 ssize_t n;
1013 int r;
1014
1015 assert(path);
1016 assert(name);
1017
1018 r = cg_get_path(controller, path, NULL, &fs);
1019 if (r < 0)
1020 return r;
1021
1022 n = getxattr(fs, name, value, size);
1023 if (n < 0)
1024 return -errno;
1025
1026 return (int) n;
1027}
1028
7027ff61 1029int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
7027ff61
LP
1030 _cleanup_fclose_ FILE *f = NULL;
1031 char line[LINE_MAX];
b6629c4b 1032 const char *fs, *controller_str;
efdb0237 1033 size_t cs = 0;
b4cccbc1 1034 int unified;
8c6db833 1035
8c6db833 1036 assert(path);
c6c18be3 1037 assert(pid >= 0);
8c6db833 1038
5da38d07
TH
1039 if (controller) {
1040 if (!cg_controller_is_valid(controller))
1041 return -EINVAL;
1042 } else
1043 controller = SYSTEMD_CGROUP_CONTROLLER;
1044
c22800e4 1045 unified = cg_unified_controller(controller);
b4cccbc1
LP
1046 if (unified < 0)
1047 return unified;
1048 if (unified == 0) {
b6629c4b
TH
1049 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
1050 controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
1051 else
1052 controller_str = controller;
1053
1054 cs = strlen(controller_str);
1055 }
7027ff61 1056
b68fa010 1057 fs = procfs_file_alloca(pid, "cgroup");
c6c18be3 1058 f = fopen(fs, "re");
4c633005
LP
1059 if (!f)
1060 return errno == ENOENT ? -ESRCH : -errno;
1061
7027ff61 1062 FOREACH_LINE(line, f, return -errno) {
efdb0237 1063 char *e, *p;
c6c18be3
LP
1064
1065 truncate_nl(line);
1066
efdb0237
LP
1067 if (unified) {
1068 e = startswith(line, "0:");
1069 if (!e)
1070 continue;
c6c18be3 1071
efdb0237
LP
1072 e = strchr(e, ':');
1073 if (!e)
1074 continue;
1075 } else {
1076 char *l;
1077 size_t k;
1078 const char *word, *state;
1079 bool found = false;
1080
1081 l = strchr(line, ':');
1082 if (!l)
1083 continue;
8af8afd6 1084
efdb0237
LP
1085 l++;
1086 e = strchr(l, ':');
1087 if (!e)
1088 continue;
8af8afd6 1089
efdb0237
LP
1090 *e = 0;
1091 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
b6629c4b 1092 if (k == cs && memcmp(word, controller_str, cs) == 0) {
efdb0237
LP
1093 found = true;
1094 break;
1095 }
8af8afd6
LP
1096 }
1097
efdb0237
LP
1098 if (!found)
1099 continue;
8af8afd6
LP
1100 }
1101
8af8afd6 1102 p = strdup(e + 1);
7027ff61
LP
1103 if (!p)
1104 return -ENOMEM;
c6c18be3
LP
1105
1106 *path = p;
7027ff61 1107 return 0;
c6c18be3
LP
1108 }
1109
1c80e425 1110 return -ENODATA;
8c6db833
LP
1111}
1112
1113int cg_install_release_agent(const char *controller, const char *agent) {
7027ff61 1114 _cleanup_free_ char *fs = NULL, *contents = NULL;
efdb0237 1115 const char *sc;
415fc41c 1116 int r;
8c6db833 1117
8c6db833
LP
1118 assert(agent);
1119
c22800e4 1120 r = cg_unified_controller(controller);
b4cccbc1
LP
1121 if (r < 0)
1122 return r;
1123 if (r > 0) /* doesn't apply to unified hierarchy */
efdb0237
LP
1124 return -EOPNOTSUPP;
1125
7027ff61
LP
1126 r = cg_get_path(controller, NULL, "release_agent", &fs);
1127 if (r < 0)
c6c18be3 1128 return r;
8c6db833 1129
7027ff61
LP
1130 r = read_one_line_file(fs, &contents);
1131 if (r < 0)
1132 return r;
8c6db833
LP
1133
1134 sc = strstrip(contents);
e155a0aa 1135 if (isempty(sc)) {
4c1fc3e4 1136 r = write_string_file(fs, agent, 0);
574d5f2d 1137 if (r < 0)
7027ff61 1138 return r;
b8725df8 1139 } else if (!path_equal(sc, agent))
7027ff61 1140 return -EEXIST;
8c6db833 1141
0da16248 1142 fs = mfree(fs);
7027ff61
LP
1143 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1144 if (r < 0)
1145 return r;
8c6db833 1146
0da16248 1147 contents = mfree(contents);
7027ff61
LP
1148 r = read_one_line_file(fs, &contents);
1149 if (r < 0)
1150 return r;
8c6db833
LP
1151
1152 sc = strstrip(contents);
8c6db833 1153 if (streq(sc, "0")) {
4c1fc3e4 1154 r = write_string_file(fs, "1", 0);
7027ff61
LP
1155 if (r < 0)
1156 return r;
c6c18be3 1157
7027ff61
LP
1158 return 1;
1159 }
8c6db833 1160
7027ff61
LP
1161 if (!streq(sc, "1"))
1162 return -EIO;
8c6db833 1163
7027ff61 1164 return 0;
8c6db833
LP
1165}
1166
ad929bcc
KS
1167int cg_uninstall_release_agent(const char *controller) {
1168 _cleanup_free_ char *fs = NULL;
415fc41c 1169 int r;
efdb0237 1170
c22800e4 1171 r = cg_unified_controller(controller);
b4cccbc1
LP
1172 if (r < 0)
1173 return r;
1174 if (r > 0) /* Doesn't apply to unified hierarchy */
efdb0237 1175 return -EOPNOTSUPP;
ad929bcc 1176
ac9ef333
LP
1177 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1178 if (r < 0)
1179 return r;
1180
4c1fc3e4 1181 r = write_string_file(fs, "0", 0);
ac9ef333
LP
1182 if (r < 0)
1183 return r;
1184
0da16248 1185 fs = mfree(fs);
ac9ef333 1186
ad929bcc
KS
1187 r = cg_get_path(controller, NULL, "release_agent", &fs);
1188 if (r < 0)
1189 return r;
1190
4c1fc3e4 1191 r = write_string_file(fs, "", 0);
ad929bcc
KS
1192 if (r < 0)
1193 return r;
1194
ac9ef333 1195 return 0;
ad929bcc
KS
1196}
1197
6f883237 1198int cg_is_empty(const char *controller, const char *path) {
7027ff61 1199 _cleanup_fclose_ FILE *f = NULL;
efdb0237 1200 pid_t pid;
7027ff61 1201 int r;
8c6db833 1202
8c6db833
LP
1203 assert(path);
1204
b043cd0b 1205 r = cg_enumerate_processes(controller, path, &f);
6f883237
LP
1206 if (r == -ENOENT)
1207 return 1;
c3175a7f 1208 if (r < 0)
6f883237 1209 return r;
8c6db833 1210
6f883237 1211 r = cg_read_pid(f, &pid);
c6c18be3
LP
1212 if (r < 0)
1213 return r;
8c6db833 1214
6f883237 1215 return r == 0;
8c6db833
LP
1216}
1217
6f883237 1218int cg_is_empty_recursive(const char *controller, const char *path) {
415fc41c 1219 int r;
8c6db833 1220
8c6db833
LP
1221 assert(path);
1222
6fd66507
LP
1223 /* The root cgroup is always populated */
1224 if (controller && (isempty(path) || path_equal(path, "/")))
efdb0237 1225 return false;
6fd66507 1226
c22800e4 1227 r = cg_unified_controller(controller);
b4cccbc1
LP
1228 if (r < 0)
1229 return r;
1230 if (r > 0) {
ab2c3861 1231 _cleanup_free_ char *t = NULL;
8c6db833 1232
efdb0237 1233 /* On the unified hierarchy we can check empty state
ab2c3861 1234 * via the "populated" attribute of "cgroup.events". */
8c6db833 1235
ab2c3861 1236 r = cg_read_event(controller, path, "populated", &t);
efdb0237
LP
1237 if (r < 0)
1238 return r;
1239
1240 return streq(t, "0");
1241 } else {
1242 _cleanup_closedir_ DIR *d = NULL;
1243 char *fn;
8c6db833 1244
efdb0237 1245 r = cg_is_empty(controller, path);
35d2e7ec 1246 if (r <= 0)
7027ff61 1247 return r;
35d2e7ec 1248
efdb0237
LP
1249 r = cg_enumerate_subgroups(controller, path, &d);
1250 if (r == -ENOENT)
1251 return 1;
1252 if (r < 0)
1253 return r;
35d2e7ec 1254
efdb0237
LP
1255 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1256 _cleanup_free_ char *p = NULL;
1257
605405c6 1258 p = strjoin(path, "/", fn);
efdb0237
LP
1259 free(fn);
1260 if (!p)
1261 return -ENOMEM;
1262
1263 r = cg_is_empty_recursive(controller, p);
1264 if (r <= 0)
1265 return r;
1266 }
1267 if (r < 0)
1268 return r;
1269
1270 return true;
1271 }
35d2e7ec
LP
1272}
1273
1274int cg_split_spec(const char *spec, char **controller, char **path) {
35d2e7ec 1275 char *t = NULL, *u = NULL;
efdb0237 1276 const char *e;
35d2e7ec
LP
1277
1278 assert(spec);
35d2e7ec
LP
1279
1280 if (*spec == '/') {
e884315e
LP
1281 if (!path_is_safe(spec))
1282 return -EINVAL;
35d2e7ec
LP
1283
1284 if (path) {
246aa6dd
LP
1285 t = strdup(spec);
1286 if (!t)
35d2e7ec
LP
1287 return -ENOMEM;
1288
dbb9401d 1289 *path = path_kill_slashes(t);
8c6db833
LP
1290 }
1291
35d2e7ec
LP
1292 if (controller)
1293 *controller = NULL;
1294
1295 return 0;
8c6db833
LP
1296 }
1297
246aa6dd
LP
1298 e = strchr(spec, ':');
1299 if (!e) {
185a0874 1300 if (!cg_controller_is_valid(spec))
35d2e7ec
LP
1301 return -EINVAL;
1302
1303 if (controller) {
efdb0237 1304 t = strdup(spec);
246aa6dd 1305 if (!t)
35d2e7ec
LP
1306 return -ENOMEM;
1307
1308 *controller = t;
1309 }
1310
1311 if (path)
1312 *path = NULL;
1313
1314 return 0;
8c6db833
LP
1315 }
1316
efdb0237 1317 t = strndup(spec, e-spec);
e884315e
LP
1318 if (!t)
1319 return -ENOMEM;
185a0874 1320 if (!cg_controller_is_valid(t)) {
e884315e 1321 free(t);
35d2e7ec 1322 return -EINVAL;
246aa6dd
LP
1323 }
1324
efdb0237
LP
1325 if (isempty(e+1))
1326 u = NULL;
1327 else {
baa89da4
LP
1328 u = strdup(e+1);
1329 if (!u) {
1330 free(t);
1331 return -ENOMEM;
1332 }
35d2e7ec 1333
baa89da4
LP
1334 if (!path_is_safe(u) ||
1335 !path_is_absolute(u)) {
1336 free(t);
1337 free(u);
1338 return -EINVAL;
1339 }
1340
1341 path_kill_slashes(u);
1342 }
5954c074 1343
35d2e7ec
LP
1344 if (controller)
1345 *controller = t;
e884315e
LP
1346 else
1347 free(t);
35d2e7ec
LP
1348
1349 if (path)
1350 *path = u;
e884315e
LP
1351 else
1352 free(u);
35d2e7ec
LP
1353
1354 return 0;
8c6db833 1355}
c6c18be3 1356
7027ff61 1357int cg_mangle_path(const char *path, char **result) {
78edb35a
LP
1358 _cleanup_free_ char *c = NULL, *p = NULL;
1359 char *t;
35d2e7ec
LP
1360 int r;
1361
1362 assert(path);
1363 assert(result);
1364
73e231ab 1365 /* First, check if it already is a filesystem path */
7027ff61 1366 if (path_startswith(path, "/sys/fs/cgroup")) {
35d2e7ec 1367
b69d29ce
LP
1368 t = strdup(path);
1369 if (!t)
35d2e7ec
LP
1370 return -ENOMEM;
1371
dbb9401d 1372 *result = path_kill_slashes(t);
35d2e7ec
LP
1373 return 0;
1374 }
1375
73e231ab 1376 /* Otherwise, treat it as cg spec */
b69d29ce
LP
1377 r = cg_split_spec(path, &c, &p);
1378 if (r < 0)
35d2e7ec
LP
1379 return r;
1380
efdb0237 1381 return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result);
35d2e7ec 1382}
1f73f0f1 1383
7027ff61 1384int cg_get_root_path(char **path) {
9444b1f2 1385 char *p, *e;
7027ff61
LP
1386 int r;
1387
1388 assert(path);
1389
9444b1f2 1390 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
7027ff61
LP
1391 if (r < 0)
1392 return r;
1393
efdb0237
LP
1394 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1395 if (!e)
1396 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1397 if (!e)
1398 e = endswith(p, "/system"); /* even more legacy */
9444b1f2 1399 if (e)
7027ff61
LP
1400 *e = 0;
1401
1f73f0f1
LP
1402 *path = p;
1403 return 0;
1404}
b59e2465 1405
751bc6ac
LP
1406int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1407 _cleanup_free_ char *rt = NULL;
1408 char *p;
ba1261bc
LP
1409 int r;
1410
e9174f29 1411 assert(cgroup);
751bc6ac 1412 assert(shifted);
e9174f29
LP
1413
1414 if (!root) {
1415 /* If the root was specified let's use that, otherwise
1416 * let's determine it from PID 1 */
1417
751bc6ac 1418 r = cg_get_root_path(&rt);
e9174f29
LP
1419 if (r < 0)
1420 return r;
1421
751bc6ac 1422 root = rt;
e9174f29 1423 }
ba1261bc 1424
751bc6ac 1425 p = path_startswith(cgroup, root);
efdb0237 1426 if (p && p > cgroup)
751bc6ac
LP
1427 *shifted = p - 1;
1428 else
1429 *shifted = cgroup;
1430
1431 return 0;
1432}
1433
1434int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1435 _cleanup_free_ char *raw = NULL;
1436 const char *c;
1437 int r;
1438
1439 assert(pid >= 0);
1440 assert(cgroup);
1441
1442 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
7027ff61 1443 if (r < 0)
ba1261bc 1444 return r;
ba1261bc 1445
751bc6ac
LP
1446 r = cg_shift_path(raw, root, &c);
1447 if (r < 0)
1448 return r;
ba1261bc 1449
751bc6ac
LP
1450 if (c == raw) {
1451 *cgroup = raw;
1452 raw = NULL;
1453 } else {
1454 char *n;
ba1261bc 1455
751bc6ac
LP
1456 n = strdup(c);
1457 if (!n)
ba1261bc 1458 return -ENOMEM;
ba1261bc 1459
751bc6ac
LP
1460 *cgroup = n;
1461 }
ba1261bc
LP
1462
1463 return 0;
1464}
1465
9ed794a3 1466int cg_path_decode_unit(const char *cgroup, char **unit) {
8b0849e9
LP
1467 char *c, *s;
1468 size_t n;
ef1673d1
MT
1469
1470 assert(cgroup);
6c03089c 1471 assert(unit);
ef1673d1 1472
8b0849e9
LP
1473 n = strcspn(cgroup, "/");
1474 if (n < 3)
1475 return -ENXIO;
1476
1477 c = strndupa(cgroup, n);
ae018d9b 1478 c = cg_unescape(c);
ef1673d1 1479
7410616c 1480 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
cfeaa44a 1481 return -ENXIO;
ef1673d1 1482
d7bd3de0 1483 s = strdup(c);
6c03089c
LP
1484 if (!s)
1485 return -ENOMEM;
1486
1487 *unit = s;
ef1673d1
MT
1488 return 0;
1489}
1490
8b0849e9
LP
1491static bool valid_slice_name(const char *p, size_t n) {
1492
1493 if (!p)
1494 return false;
1495
1496 if (n < strlen("x.slice"))
1497 return false;
1498
1499 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1500 char buf[n+1], *c;
1501
1502 memcpy(buf, p, n);
1503 buf[n] = 0;
1504
1505 c = cg_unescape(buf);
1506
7410616c 1507 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
8b0849e9
LP
1508 }
1509
1510 return false;
1511}
1512
9444b1f2 1513static const char *skip_slices(const char *p) {
8b0849e9
LP
1514 assert(p);
1515
9444b1f2
LP
1516 /* Skips over all slice assignments */
1517
1518 for (;;) {
1021b21b
LP
1519 size_t n;
1520
9444b1f2
LP
1521 p += strspn(p, "/");
1522
1523 n = strcspn(p, "/");
8b0849e9 1524 if (!valid_slice_name(p, n))
9444b1f2
LP
1525 return p;
1526
1527 p += n;
1528 }
1529}
1530
8b0849e9 1531int cg_path_get_unit(const char *path, char **ret) {
6c03089c 1532 const char *e;
8b0849e9
LP
1533 char *unit;
1534 int r;
6c03089c
LP
1535
1536 assert(path);
8b0849e9 1537 assert(ret);
6c03089c 1538
9444b1f2 1539 e = skip_slices(path);
6c03089c 1540
8b0849e9
LP
1541 r = cg_path_decode_unit(e, &unit);
1542 if (r < 0)
1543 return r;
1544
1545 /* We skipped over the slices, don't accept any now */
1546 if (endswith(unit, ".slice")) {
1547 free(unit);
1548 return -ENXIO;
1549 }
1550
1551 *ret = unit;
1552 return 0;
6c03089c
LP
1553}
1554
1555int cg_pid_get_unit(pid_t pid, char **unit) {
7fd1b19b 1556 _cleanup_free_ char *cgroup = NULL;
ba1261bc 1557 int r;
ba1261bc 1558
ef1673d1
MT
1559 assert(unit);
1560
7027ff61 1561 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
ef1673d1
MT
1562 if (r < 0)
1563 return r;
1564
6c03089c
LP
1565 return cg_path_get_unit(cgroup, unit);
1566}
ef1673d1 1567
d4fffc4b
ZJS
1568/**
1569 * Skip session-*.scope, but require it to be there.
1570 */
9444b1f2
LP
1571static const char *skip_session(const char *p) {
1572 size_t n;
1573
8b0849e9
LP
1574 if (isempty(p))
1575 return NULL;
9444b1f2
LP
1576
1577 p += strspn(p, "/");
1578
1579 n = strcspn(p, "/");
8b0849e9 1580 if (n < strlen("session-x.scope"))
d4fffc4b
ZJS
1581 return NULL;
1582
8b0849e9
LP
1583 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1584 char buf[n - 8 - 6 + 1];
1585
1586 memcpy(buf, p + 8, n - 8 - 6);
1587 buf[n - 8 - 6] = 0;
d4fffc4b 1588
8b0849e9
LP
1589 /* Note that session scopes never need unescaping,
1590 * since they cannot conflict with the kernel's own
1591 * names, hence we don't need to call cg_unescape()
1592 * here. */
1593
1594 if (!session_id_valid(buf))
1595 return false;
1596
1597 p += n;
1598 p += strspn(p, "/");
1599 return p;
1600 }
1601
1602 return NULL;
d4fffc4b
ZJS
1603}
1604
1605/**
1606 * Skip user@*.service, but require it to be there.
1607 */
1608static const char *skip_user_manager(const char *p) {
1609 size_t n;
1610
8b0849e9
LP
1611 if (isempty(p))
1612 return NULL;
d4fffc4b
ZJS
1613
1614 p += strspn(p, "/");
1615
1616 n = strcspn(p, "/");
8b0849e9 1617 if (n < strlen("user@x.service"))
6c03089c 1618 return NULL;
ef1673d1 1619
8b0849e9
LP
1620 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1621 char buf[n - 5 - 8 + 1];
9444b1f2 1622
8b0849e9
LP
1623 memcpy(buf, p + 5, n - 5 - 8);
1624 buf[n - 5 - 8] = 0;
1625
1626 /* Note that user manager services never need unescaping,
1627 * since they cannot conflict with the kernel's own
1628 * names, hence we don't need to call cg_unescape()
1629 * here. */
1630
1631 if (parse_uid(buf, NULL) < 0)
1632 return NULL;
1633
1634 p += n;
1635 p += strspn(p, "/");
1636
1637 return p;
1638 }
1639
1640 return NULL;
9444b1f2
LP
1641}
1642
329ac4bc 1643static const char *skip_user_prefix(const char *path) {
d4fffc4b 1644 const char *e, *t;
ef1673d1 1645
6c03089c 1646 assert(path);
ba1261bc 1647
9444b1f2
LP
1648 /* Skip slices, if there are any */
1649 e = skip_slices(path);
ba1261bc 1650
329ac4bc 1651 /* Skip the user manager, if it's in the path now... */
8b0849e9 1652 t = skip_user_manager(e);
329ac4bc
LP
1653 if (t)
1654 return t;
8b0849e9 1655
329ac4bc
LP
1656 /* Alternatively skip the user session if it is in the path... */
1657 return skip_session(e);
1658}
32081481 1659
329ac4bc
LP
1660int cg_path_get_user_unit(const char *path, char **ret) {
1661 const char *t;
6c03089c 1662
329ac4bc
LP
1663 assert(path);
1664 assert(ret);
8b0849e9 1665
329ac4bc
LP
1666 t = skip_user_prefix(path);
1667 if (!t)
8b0849e9 1668 return -ENXIO;
8b0849e9 1669
329ac4bc
LP
1670 /* And from here on it looks pretty much the same as for a
1671 * system unit, hence let's use the same parser from here
1672 * on. */
1673 return cg_path_get_unit(t, ret);
ef1673d1 1674}
ba1261bc 1675
ef1673d1 1676int cg_pid_get_user_unit(pid_t pid, char **unit) {
7fd1b19b 1677 _cleanup_free_ char *cgroup = NULL;
6c03089c
LP
1678 int r;
1679
1680 assert(unit);
1681
7027ff61 1682 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
6c03089c
LP
1683 if (r < 0)
1684 return r;
1685
1686 return cg_path_get_user_unit(cgroup, unit);
ba1261bc 1687}
e884315e 1688
7027ff61 1689int cg_path_get_machine_name(const char *path, char **machine) {
efdb0237
LP
1690 _cleanup_free_ char *u = NULL;
1691 const char *sl;
89f7c846 1692 int r;
374ec6ab 1693
89f7c846
LP
1694 r = cg_path_get_unit(path, &u);
1695 if (r < 0)
1696 return r;
7027ff61 1697
efdb0237 1698 sl = strjoina("/run/systemd/machines/unit:", u);
89f7c846 1699 return readlink_malloc(sl, machine);
7027ff61
LP
1700}
1701
1702int cg_pid_get_machine_name(pid_t pid, char **machine) {
7fd1b19b 1703 _cleanup_free_ char *cgroup = NULL;
7027ff61
LP
1704 int r;
1705
1706 assert(machine);
1707
1708 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1709 if (r < 0)
1710 return r;
1711
1712 return cg_path_get_machine_name(cgroup, machine);
1713}
1714
1715int cg_path_get_session(const char *path, char **session) {
8b0849e9
LP
1716 _cleanup_free_ char *unit = NULL;
1717 char *start, *end;
1718 int r;
7027ff61
LP
1719
1720 assert(path);
7027ff61 1721
8b0849e9
LP
1722 r = cg_path_get_unit(path, &unit);
1723 if (r < 0)
1724 return r;
7027ff61 1725
8b0849e9
LP
1726 start = startswith(unit, "session-");
1727 if (!start)
cfeaa44a 1728 return -ENXIO;
8b0849e9
LP
1729 end = endswith(start, ".scope");
1730 if (!end)
cfeaa44a 1731 return -ENXIO;
8b0849e9
LP
1732
1733 *end = 0;
1734 if (!session_id_valid(start))
cfeaa44a 1735 return -ENXIO;
374ec6ab 1736
af08d2f9 1737 if (session) {
8b0849e9 1738 char *rr;
af08d2f9 1739
8b0849e9
LP
1740 rr = strdup(start);
1741 if (!rr)
af08d2f9
LP
1742 return -ENOMEM;
1743
8b0849e9 1744 *session = rr;
af08d2f9 1745 }
7027ff61 1746
7027ff61
LP
1747 return 0;
1748}
1749
1750int cg_pid_get_session(pid_t pid, char **session) {
7fd1b19b 1751 _cleanup_free_ char *cgroup = NULL;
7027ff61
LP
1752 int r;
1753
7027ff61
LP
1754 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1755 if (r < 0)
1756 return r;
1757
1758 return cg_path_get_session(cgroup, session);
1759}
1760
ae018d9b 1761int cg_path_get_owner_uid(const char *path, uid_t *uid) {
374ec6ab 1762 _cleanup_free_ char *slice = NULL;
8b0849e9 1763 char *start, *end;
374ec6ab 1764 int r;
ae018d9b
LP
1765
1766 assert(path);
ae018d9b 1767
374ec6ab
LP
1768 r = cg_path_get_slice(path, &slice);
1769 if (r < 0)
1770 return r;
ae018d9b 1771
674eb685
LP
1772 start = startswith(slice, "user-");
1773 if (!start)
cfeaa44a 1774 return -ENXIO;
8b0849e9 1775 end = endswith(start, ".slice");
674eb685 1776 if (!end)
cfeaa44a 1777 return -ENXIO;
ae018d9b 1778
8b0849e9
LP
1779 *end = 0;
1780 if (parse_uid(start, uid) < 0)
cfeaa44a 1781 return -ENXIO;
674eb685 1782
674eb685 1783 return 0;
ae018d9b
LP
1784}
1785
1786int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1787 _cleanup_free_ char *cgroup = NULL;
1788 int r;
1789
ae018d9b
LP
1790 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1791 if (r < 0)
1792 return r;
1793
1794 return cg_path_get_owner_uid(cgroup, uid);
1795}
1796
1021b21b
LP
1797int cg_path_get_slice(const char *p, char **slice) {
1798 const char *e = NULL;
1021b21b
LP
1799
1800 assert(p);
1801 assert(slice);
1802
329ac4bc
LP
1803 /* Finds the right-most slice unit from the beginning, but
1804 * stops before we come to the first non-slice unit. */
1805
1021b21b
LP
1806 for (;;) {
1807 size_t n;
1808
1809 p += strspn(p, "/");
1810
1811 n = strcspn(p, "/");
8b0849e9 1812 if (!valid_slice_name(p, n)) {
1021b21b 1813
8b0849e9
LP
1814 if (!e) {
1815 char *s;
1021b21b 1816
e5d855d3 1817 s = strdup(SPECIAL_ROOT_SLICE);
8b0849e9
LP
1818 if (!s)
1819 return -ENOMEM;
1021b21b 1820
8b0849e9
LP
1821 *slice = s;
1822 return 0;
1823 }
1824
1825 return cg_path_decode_unit(e, slice);
1021b21b
LP
1826 }
1827
1828 e = p;
1021b21b
LP
1829 p += n;
1830 }
1831}
1832
1833int cg_pid_get_slice(pid_t pid, char **slice) {
1834 _cleanup_free_ char *cgroup = NULL;
1835 int r;
1836
1837 assert(slice);
1838
1839 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1840 if (r < 0)
1841 return r;
1842
1843 return cg_path_get_slice(cgroup, slice);
1844}
1845
329ac4bc
LP
1846int cg_path_get_user_slice(const char *p, char **slice) {
1847 const char *t;
1848 assert(p);
1849 assert(slice);
1850
1851 t = skip_user_prefix(p);
1852 if (!t)
1853 return -ENXIO;
1854
1855 /* And now it looks pretty much the same as for a system
1856 * slice, so let's just use the same parser from here on. */
1857 return cg_path_get_slice(t, slice);
1858}
1859
1860int cg_pid_get_user_slice(pid_t pid, char **slice) {
1861 _cleanup_free_ char *cgroup = NULL;
1862 int r;
1863
1864 assert(slice);
1865
1866 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1867 if (r < 0)
1868 return r;
1869
1870 return cg_path_get_user_slice(cgroup, slice);
1871}
1872
ae018d9b
LP
1873char *cg_escape(const char *p) {
1874 bool need_prefix = false;
1875
1876 /* This implements very minimal escaping for names to be used
1877 * as file names in the cgroup tree: any name which might
1878 * conflict with a kernel name or is prefixed with '_' is
1879 * prefixed with a '_'. That way, when reading cgroup names it
1880 * is sufficient to remove a single prefixing underscore if
1881 * there is one. */
1882
1883 /* The return value of this function (unlike cg_unescape())
1884 * needs free()! */
1885
4c701096 1886 if (IN_SET(p[0], 0, '_', '.') ||
a0ab5665
LP
1887 streq(p, "notify_on_release") ||
1888 streq(p, "release_agent") ||
efdb0237
LP
1889 streq(p, "tasks") ||
1890 startswith(p, "cgroup."))
ae018d9b
LP
1891 need_prefix = true;
1892 else {
1893 const char *dot;
1894
1895 dot = strrchr(p, '.');
1896 if (dot) {
efdb0237
LP
1897 CGroupController c;
1898 size_t l = dot - p;
ae018d9b 1899
efdb0237
LP
1900 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1901 const char *n;
1902
1903 n = cgroup_controller_to_string(c);
ae018d9b 1904
efdb0237
LP
1905 if (l != strlen(n))
1906 continue;
ae018d9b 1907
efdb0237
LP
1908 if (memcmp(p, n, l) != 0)
1909 continue;
1910
1911 need_prefix = true;
1912 break;
ae018d9b
LP
1913 }
1914 }
1915 }
1916
1917 if (need_prefix)
1918 return strappend("_", p);
efdb0237
LP
1919
1920 return strdup(p);
ae018d9b
LP
1921}
1922
1923char *cg_unescape(const char *p) {
1924 assert(p);
1925
1926 /* The return value of this function (unlike cg_escape())
1927 * doesn't need free()! */
1928
1929 if (p[0] == '_')
1930 return (char*) p+1;
1931
1932 return (char*) p;
1933}
78edb35a
LP
1934
1935#define CONTROLLER_VALID \
4b549144 1936 DIGITS LETTERS \
78edb35a
LP
1937 "_"
1938
185a0874 1939bool cg_controller_is_valid(const char *p) {
78edb35a
LP
1940 const char *t, *s;
1941
1942 if (!p)
1943 return false;
1944
b6629c4b
TH
1945 if (streq(p, SYSTEMD_CGROUP_CONTROLLER))
1946 return true;
1947
185a0874
DJL
1948 s = startswith(p, "name=");
1949 if (s)
1950 p = s;
78edb35a 1951
4c701096 1952 if (IN_SET(*p, 0, '_'))
78edb35a
LP
1953 return false;
1954
1955 for (t = p; *t; t++)
1956 if (!strchr(CONTROLLER_VALID, *t))
1957 return false;
1958
1959 if (t - p > FILENAME_MAX)
1960 return false;
1961
1962 return true;
1963}
a016b922
LP
1964
1965int cg_slice_to_path(const char *unit, char **ret) {
1966 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1967 const char *dash;
7410616c 1968 int r;
a016b922
LP
1969
1970 assert(unit);
1971 assert(ret);
1972
e5d855d3 1973 if (streq(unit, SPECIAL_ROOT_SLICE)) {
c96cc582
LP
1974 char *x;
1975
1976 x = strdup("");
1977 if (!x)
1978 return -ENOMEM;
1979 *ret = x;
1980 return 0;
1981 }
1982
7410616c 1983 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
a016b922
LP
1984 return -EINVAL;
1985
1986 if (!endswith(unit, ".slice"))
1987 return -EINVAL;
1988
7410616c
LP
1989 r = unit_name_to_prefix(unit, &p);
1990 if (r < 0)
1991 return r;
a016b922
LP
1992
1993 dash = strchr(p, '-');
e66e5b61
LP
1994
1995 /* Don't allow initial dashes */
1996 if (dash == p)
1997 return -EINVAL;
1998
a016b922
LP
1999 while (dash) {
2000 _cleanup_free_ char *escaped = NULL;
2001 char n[dash - p + sizeof(".slice")];
2002
e66e5b61 2003 /* Don't allow trailing or double dashes */
4c701096 2004 if (IN_SET(dash[1], 0, '-'))
c96cc582 2005 return -EINVAL;
a016b922 2006
c96cc582 2007 strcpy(stpncpy(n, p, dash - p), ".slice");
7410616c 2008 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
a016b922
LP
2009 return -EINVAL;
2010
2011 escaped = cg_escape(n);
2012 if (!escaped)
2013 return -ENOMEM;
2014
2015 if (!strextend(&s, escaped, "/", NULL))
2016 return -ENOMEM;
2017
2018 dash = strchr(dash+1, '-');
2019 }
2020
2021 e = cg_escape(unit);
2022 if (!e)
2023 return -ENOMEM;
2024
2025 if (!strextend(&s, e, NULL))
2026 return -ENOMEM;
2027
2028 *ret = s;
2029 s = NULL;
2030
2031 return 0;
2032}
4ad49000
LP
2033
2034int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
2035 _cleanup_free_ char *p = NULL;
2036 int r;
2037
2038 r = cg_get_path(controller, path, attribute, &p);
2039 if (r < 0)
2040 return r;
2041
4c1fc3e4 2042 return write_string_file(p, value, 0);
4ad49000
LP
2043}
2044
934277fe
LP
2045int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
2046 _cleanup_free_ char *p = NULL;
2047 int r;
2048
2049 r = cg_get_path(controller, path, attribute, &p);
2050 if (r < 0)
2051 return r;
2052
2053 return read_one_line_file(p, ret);
2054}
2055
66ebf6c0
TH
2056int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, const char **keys, char **values) {
2057 _cleanup_free_ char *filename = NULL, *content = NULL;
2058 char *line, *p;
2059 int i, r;
2060
2061 for (i = 0; keys[i]; i++)
2062 values[i] = NULL;
2063
2064 r = cg_get_path(controller, path, attribute, &filename);
2065 if (r < 0)
2066 return r;
2067
2068 r = read_full_file(filename, &content, NULL);
2069 if (r < 0)
2070 return r;
2071
2072 p = content;
2073 while ((line = strsep(&p, "\n"))) {
2074 char *key;
2075
2076 key = strsep(&line, " ");
2077
2078 for (i = 0; keys[i]; i++) {
2079 if (streq(key, keys[i])) {
2080 values[i] = strdup(line);
2081 break;
2082 }
2083 }
2084 }
2085
2086 for (i = 0; keys[i]; i++) {
2087 if (!values[i]) {
2088 for (i = 0; keys[i]; i++) {
2089 free(values[i]);
2090 values[i] = NULL;
2091 }
2092 return -ENOENT;
2093 }
2094 }
2095
2096 return 0;
2097}
2098
efdb0237
LP
2099int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
2100 CGroupController c;
415fc41c 2101 int r;
4ad49000
LP
2102
2103 /* This one will create a cgroup in our private tree, but also
2104 * duplicate it in the trees specified in mask, and remove it
2105 * in all others */
2106
2107 /* First create the cgroup in our own hierarchy. */
2108 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
2109 if (r < 0)
2110 return r;
2111
efdb0237 2112 /* If we are in the unified hierarchy, we are done now */
b4cccbc1
LP
2113 r = cg_all_unified();
2114 if (r < 0)
2115 return r;
2116 if (r > 0)
efdb0237
LP
2117 return 0;
2118
2119 /* Otherwise, do the same in the other hierarchies */
2120 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2121 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2122 const char *n;
2123
2124 n = cgroup_controller_to_string(c);
2125
13b84ec7 2126 if (mask & bit)
efdb0237 2127 (void) cg_create(n, path);
13b84ec7 2128 else if (supported & bit)
efdb0237 2129 (void) cg_trim(n, path, true);
4ad49000
LP
2130 }
2131
13b84ec7 2132 return 0;
4ad49000
LP
2133}
2134
efdb0237
LP
2135int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
2136 CGroupController c;
415fc41c 2137 int r;
4ad49000
LP
2138
2139 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
13b84ec7
LP
2140 if (r < 0)
2141 return r;
4ad49000 2142
b4cccbc1
LP
2143 r = cg_all_unified();
2144 if (r < 0)
2145 return r;
2146 if (r > 0)
efdb0237 2147 return 0;
7b3fd631 2148
efdb0237
LP
2149 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2150 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2151 const char *p = NULL;
7b3fd631 2152
efdb0237
LP
2153 if (!(supported & bit))
2154 continue;
7b3fd631 2155
efdb0237
LP
2156 if (path_callback)
2157 p = path_callback(bit, userdata);
7b3fd631 2158
efdb0237
LP
2159 if (!p)
2160 p = path;
4ad49000 2161
efdb0237 2162 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
4ad49000
LP
2163 }
2164
13b84ec7 2165 return 0;
4ad49000
LP
2166}
2167
efdb0237 2168int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
6c12b52e
LP
2169 Iterator i;
2170 void *pidp;
2171 int r = 0;
2172
2173 SET_FOREACH(pidp, pids, i) {
fea72cc0 2174 pid_t pid = PTR_TO_PID(pidp);
13b84ec7 2175 int q;
6c12b52e 2176
7b3fd631 2177 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
efdb0237 2178 if (q < 0 && r >= 0)
13b84ec7 2179 r = q;
6c12b52e
LP
2180 }
2181
2182 return r;
2183}
2184
efdb0237 2185int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
b3c5bad3 2186 CGroupController c;
b4cccbc1 2187 int r = 0, q;
4ad49000 2188
13b84ec7 2189 if (!path_equal(from, to)) {
1d98fef1 2190 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE);
13b84ec7
LP
2191 if (r < 0)
2192 return r;
2193 }
4ad49000 2194
b4cccbc1
LP
2195 q = cg_all_unified();
2196 if (q < 0)
2197 return q;
2198 if (q > 0)
efdb0237 2199 return r;
03b90d4b 2200
efdb0237
LP
2201 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2202 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2203 const char *p = NULL;
03b90d4b 2204
efdb0237
LP
2205 if (!(supported & bit))
2206 continue;
03b90d4b 2207
efdb0237
LP
2208 if (to_callback)
2209 p = to_callback(bit, userdata);
4ad49000 2210
efdb0237
LP
2211 if (!p)
2212 p = to;
2213
1d98fef1 2214 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0);
4ad49000
LP
2215 }
2216
13b84ec7 2217 return 0;
4ad49000
LP
2218}
2219
efdb0237
LP
2220int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2221 CGroupController c;
b4cccbc1 2222 int r, q;
4ad49000
LP
2223
2224 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2225 if (r < 0)
2226 return r;
2227
b4cccbc1
LP
2228 q = cg_all_unified();
2229 if (q < 0)
2230 return q;
2231 if (q > 0)
efdb0237
LP
2232 return r;
2233
2234 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2235 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2236
2237 if (!(supported & bit))
2238 continue;
4ad49000 2239
efdb0237 2240 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
4ad49000
LP
2241 }
2242
13b84ec7 2243 return 0;
4ad49000
LP
2244}
2245
aae7e17f 2246int cg_mask_to_string(CGroupMask mask, char **ret) {
ec635a2d
LP
2247 _cleanup_free_ char *s = NULL;
2248 size_t n = 0, allocated = 0;
2249 bool space = false;
aae7e17f 2250 CGroupController c;
aae7e17f
FB
2251
2252 assert(ret);
2253
2254 if (mask == 0) {
2255 *ret = NULL;
2256 return 0;
2257 }
2258
2259 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
ec635a2d
LP
2260 const char *k;
2261 size_t l;
aae7e17f
FB
2262
2263 if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
2264 continue;
2265
ec635a2d
LP
2266 k = cgroup_controller_to_string(c);
2267 l = strlen(k);
2268
2269 if (!GREEDY_REALLOC(s, allocated, n + space + l + 1))
2270 return -ENOMEM;
2271
2272 if (space)
2273 s[n] = ' ';
2274 memcpy(s + n + space, k, l);
2275 n += space + l;
2276
2277 space = true;
aae7e17f
FB
2278 }
2279
ec635a2d 2280 assert(s);
aae7e17f 2281
ec635a2d 2282 s[n] = 0;
aae7e17f 2283 *ret = s;
ec635a2d
LP
2284 s = NULL;
2285
aae7e17f
FB
2286 return 0;
2287}
2288
2289int cg_mask_from_string(const char *value, CGroupMask *mask) {
2290 assert(mask);
2291 assert(value);
2292
2293 for (;;) {
2294 _cleanup_free_ char *n = NULL;
2295 CGroupController v;
2296 int r;
2297
2298 r = extract_first_word(&value, &n, NULL, 0);
2299 if (r < 0)
2300 return r;
2301 if (r == 0)
2302 break;
2303
2304 v = cgroup_controller_from_string(n);
2305 if (v < 0)
2306 continue;
2307
2308 *mask |= CGROUP_CONTROLLER_TO_MASK(v);
2309 }
2310 return 0;
2311}
2312
efdb0237
LP
2313int cg_mask_supported(CGroupMask *ret) {
2314 CGroupMask mask = 0;
415fc41c 2315 int r;
efdb0237
LP
2316
2317 /* Determines the mask of supported cgroup controllers. Only
2318 * includes controllers we can make sense of and that are
2319 * actually accessible. */
4ad49000 2320
b4cccbc1
LP
2321 r = cg_all_unified();
2322 if (r < 0)
2323 return r;
2324 if (r > 0) {
5f4c5fef 2325 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
efdb0237
LP
2326
2327 /* In the unified hierarchy we can read the supported
2328 * and accessible controllers from a the top-level
2329 * cgroup attribute */
2330
5f4c5fef
LP
2331 r = cg_get_root_path(&root);
2332 if (r < 0)
2333 return r;
2334
2335 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2336 if (r < 0)
2337 return r;
2338
2339 r = read_one_line_file(path, &controllers);
efdb0237
LP
2340 if (r < 0)
2341 return r;
4ad49000 2342
aae7e17f
FB
2343 r = cg_mask_from_string(controllers, &mask);
2344 if (r < 0)
2345 return r;
efdb0237 2346
66ebf6c0 2347 /* Currently, we support the cpu, memory, io and pids
03a7b521
LP
2348 * controller in the unified hierarchy, mask
2349 * everything else off. */
66ebf6c0 2350 mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS;
efdb0237
LP
2351
2352 } else {
2353 CGroupController c;
2354
2355 /* In the legacy hierarchy, we check whether which
2356 * hierarchies are mounted. */
2357
2358 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2359 const char *n;
2360
2361 n = cgroup_controller_to_string(c);
2362 if (controller_is_accessible(n) >= 0)
2363 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2364 }
4ad49000
LP
2365 }
2366
efdb0237
LP
2367 *ret = mask;
2368 return 0;
4ad49000 2369}
b12afc8c
LP
2370
2371int cg_kernel_controllers(Set *controllers) {
2372 _cleanup_fclose_ FILE *f = NULL;
b12afc8c
LP
2373 int r;
2374
2375 assert(controllers);
2376
e155a0aa
LP
2377 /* Determines the full list of kernel-known controllers. Might
2378 * include controllers we don't actually support, arbitrary
2379 * named hierarchies and controllers that aren't currently
2380 * accessible (because not mounted). */
2381
b12afc8c
LP
2382 f = fopen("/proc/cgroups", "re");
2383 if (!f) {
2384 if (errno == ENOENT)
2385 return 0;
2386 return -errno;
2387 }
2388
2389 /* Ignore the header line */
2351e44d 2390 (void) read_line(f, (size_t) -1, NULL);
b12afc8c
LP
2391
2392 for (;;) {
2393 char *controller;
2394 int enabled = 0;
2395
2396 errno = 0;
2397 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2398
2399 if (feof(f))
2400 break;
2401
b3267152 2402 if (ferror(f) && errno > 0)
b12afc8c
LP
2403 return -errno;
2404
2405 return -EBADMSG;
2406 }
2407
2408 if (!enabled) {
2409 free(controller);
2410 continue;
2411 }
2412
efdb0237 2413 if (!cg_controller_is_valid(controller)) {
b12afc8c
LP
2414 free(controller);
2415 return -EBADMSG;
2416 }
2417
2418 r = set_consume(controllers, controller);
2419 if (r < 0)
2420 return r;
2421 }
2422
2423 return 0;
2424}
efdb0237 2425
5da38d07
TH
2426static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
2427
c22800e4
LP
2428/* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2429 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2430 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2431 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
f08e9287 2432 *
c22800e4
LP
2433 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2434 * process management but disable the compat dual layout, we return %true on
2435 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
f08e9287
TH
2436 */
2437static thread_local bool unified_systemd_v232;
2438
1fcca10e 2439static int cg_unified_update(void) {
efdb0237 2440
efdb0237
LP
2441 struct statfs fs;
2442
2443 /* Checks if we support the unified hierarchy. Returns an
2444 * error when the cgroup hierarchies aren't mounted yet or we
2445 * have any other trouble determining if the unified hierarchy
2446 * is supported. */
2447
5da38d07
TH
2448 if (unified_cache >= CGROUP_UNIFIED_NONE)
2449 return 0;
efdb0237
LP
2450
2451 if (statfs("/sys/fs/cgroup/", &fs) < 0)
9aa21133 2452 return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\" failed: %m");
efdb0237 2453
9aa21133
ZJS
2454 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2455 log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
5da38d07 2456 unified_cache = CGROUP_UNIFIED_ALL;
9aa21133 2457 } else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2977724b 2458 if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 &&
f08e9287 2459 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
9aa21133 2460 log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller");
2977724b 2461 unified_cache = CGROUP_UNIFIED_SYSTEMD;
f08e9287 2462 unified_systemd_v232 = false;
f08e9287 2463 } else {
2977724b 2464 if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0)
9aa21133 2465 return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m");
5535d8f7
EV
2466
2467 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2468 log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)");
2469 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2470 unified_systemd_v232 = true;
2471 } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)) {
2472 log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy");
2473 unified_cache = CGROUP_UNIFIED_NONE;
2474 } else {
2475 log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy",
9aa21133 2476 (unsigned long long) fs.f_type);
5535d8f7 2477 unified_cache = CGROUP_UNIFIED_NONE;
9aa21133 2478 }
2977724b 2479 }
651d47d1
ZJS
2480 } else {
2481 log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2482 (unsigned long long) fs.f_type);
8b3aa503 2483 return -ENOMEDIUM;
651d47d1 2484 }
efdb0237 2485
5da38d07
TH
2486 return 0;
2487}
2488
c22800e4 2489int cg_unified_controller(const char *controller) {
b4cccbc1 2490 int r;
5da38d07 2491
1fcca10e 2492 r = cg_unified_update();
b4cccbc1
LP
2493 if (r < 0)
2494 return r;
5da38d07 2495
fc9ae717
LP
2496 if (unified_cache == CGROUP_UNIFIED_NONE)
2497 return false;
2498
2499 if (unified_cache >= CGROUP_UNIFIED_ALL)
2500 return true;
2501
2502 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER);
5da38d07
TH
2503}
2504
b4cccbc1 2505int cg_all_unified(void) {
4bb652ac
LP
2506 int r;
2507
2508 r = cg_unified_update();
2509 if (r < 0)
2510 return r;
2511
2512 return unified_cache >= CGROUP_UNIFIED_ALL;
efdb0237
LP
2513}
2514
b4cccbc1
LP
2515int cg_hybrid_unified(void) {
2516 int r;
2977724b 2517
1fcca10e 2518 r = cg_unified_update();
b4cccbc1
LP
2519 if (r < 0)
2520 return r;
2977724b 2521
f08e9287 2522 return unified_cache == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232;
2977724b
TH
2523}
2524
415fc41c 2525int cg_unified_flush(void) {
5da38d07 2526 unified_cache = CGROUP_UNIFIED_UNKNOWN;
415fc41c 2527
1fcca10e 2528 return cg_unified_update();
efdb0237
LP
2529}
2530
2531int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2532 _cleanup_free_ char *fs = NULL;
2533 CGroupController c;
415fc41c 2534 int r;
efdb0237
LP
2535
2536 assert(p);
2537
2538 if (supported == 0)
2539 return 0;
2540
b4cccbc1
LP
2541 r = cg_all_unified();
2542 if (r < 0)
2543 return r;
2544 if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */
efdb0237
LP
2545 return 0;
2546
2547 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2548 if (r < 0)
2549 return r;
2550
2551 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2552 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2553 const char *n;
2554
2555 if (!(supported & bit))
2556 continue;
2557
2558 n = cgroup_controller_to_string(c);
2559 {
2560 char s[1 + strlen(n) + 1];
2561
2562 s[0] = mask & bit ? '+' : '-';
2563 strcpy(s + 1, n);
2564
2565 r = write_string_file(fs, s, 0);
2566 if (r < 0)
98e4d8d7 2567 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
efdb0237
LP
2568 }
2569 }
2570
2571 return 0;
2572}
2573
2574bool cg_is_unified_wanted(void) {
2575 static thread_local int wanted = -1;
415fc41c 2576 int r;
1d84ad94 2577 bool b;
77fab2a9 2578 const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL;
efdb0237 2579
77fab2a9 2580 /* If we have a cached value, return that. */
efdb0237
LP
2581 if (wanted >= 0)
2582 return wanted;
2583
239a3d09
ZJS
2584 /* If the hierarchy is already mounted, then follow whatever
2585 * was chosen for it. */
2586 if (cg_unified_flush() >= 0)
b4cccbc1 2587 return (wanted = unified_cache >= CGROUP_UNIFIED_ALL);
239a3d09 2588
77fab2a9
ZJS
2589 /* Otherwise, let's see what the kernel command line has to say.
2590 * Since checking is expensive, cache a non-error result. */
1d84ad94 2591 r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b);
efdb0237 2592
77fab2a9 2593 return (wanted = r > 0 ? b : is_default);
efdb0237
LP
2594}
2595
2596bool cg_is_legacy_wanted(void) {
239a3d09
ZJS
2597 static thread_local int wanted = -1;
2598
2599 /* If we have a cached value, return that. */
2600 if (wanted >= 0)
2601 return wanted;
2602
1b59cf04
ZJS
2603 /* Check if we have cgroups2 already mounted. */
2604 if (cg_unified_flush() >= 0 &&
2605 unified_cache == CGROUP_UNIFIED_ALL)
239a3d09 2606 return (wanted = false);
1b59cf04
ZJS
2607
2608 /* Otherwise, assume that at least partial legacy is wanted,
2609 * since cgroups2 should already be mounted at this point. */
239a3d09 2610 return (wanted = true);
efdb0237
LP
2611}
2612
a4464b95 2613bool cg_is_hybrid_wanted(void) {
5da38d07 2614 static thread_local int wanted = -1;
415fc41c 2615 int r;
1d84ad94 2616 bool b;
c19739db
ZJS
2617 const bool is_default = DEFAULT_HIERARCHY >= CGROUP_UNIFIED_SYSTEMD;
2618 /* We default to true if the default is "hybrid", obviously,
2619 * but also when the default is "unified", because if we get
2620 * called, it means that unified hierarchy was not mounted. */
5da38d07 2621
77fab2a9 2622 /* If we have a cached value, return that. */
5da38d07
TH
2623 if (wanted >= 0)
2624 return wanted;
2625
239a3d09
ZJS
2626 /* If the hierarchy is already mounted, then follow whatever
2627 * was chosen for it. */
2628 if (cg_unified_flush() >= 0 &&
2629 unified_cache == CGROUP_UNIFIED_ALL)
2630 return (wanted = false);
2631
77fab2a9
ZJS
2632 /* Otherwise, let's see what the kernel command line has to say.
2633 * Since checking is expensive, cache a non-error result. */
1d84ad94 2634 r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b);
5da38d07 2635
2dcb526d
ZJS
2636 /* The meaning of the kernel option is reversed wrt. to the return value
2637 * of this function, hence the negation. */
77fab2a9 2638 return (wanted = r > 0 ? !b : is_default);
5da38d07
TH
2639}
2640
13c31542
TH
2641int cg_weight_parse(const char *s, uint64_t *ret) {
2642 uint64_t u;
2643 int r;
2644
2645 if (isempty(s)) {
2646 *ret = CGROUP_WEIGHT_INVALID;
2647 return 0;
2648 }
2649
2650 r = safe_atou64(s, &u);
2651 if (r < 0)
2652 return r;
2653
2654 if (u < CGROUP_WEIGHT_MIN || u > CGROUP_WEIGHT_MAX)
2655 return -ERANGE;
2656
2657 *ret = u;
2658 return 0;
2659}
2660
9be57249
TH
2661const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2662 [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX,
2663 [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX,
ac06a0cf
TH
2664 [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX,
2665 [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX,
9be57249
TH
2666};
2667
2668static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2669 [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax",
2670 [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax",
ac06a0cf
TH
2671 [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax",
2672 [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax",
9be57249
TH
2673};
2674
2675DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType);
2676
d53d9474
LP
2677int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2678 uint64_t u;
2679 int r;
2680
2681 if (isempty(s)) {
2682 *ret = CGROUP_CPU_SHARES_INVALID;
2683 return 0;
2684 }
2685
2686 r = safe_atou64(s, &u);
2687 if (r < 0)
2688 return r;
2689
2690 if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2691 return -ERANGE;
2692
2693 *ret = u;
2694 return 0;
2695}
2696
2697int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2698 uint64_t u;
2699 int r;
2700
2701 if (isempty(s)) {
2702 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2703 return 0;
2704 }
2705
2706 r = safe_atou64(s, &u);
2707 if (r < 0)
2708 return r;
2709
2710 if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2711 return -ERANGE;
2712
2713 *ret = u;
2714 return 0;
2715}
2716
f0bef277
EV
2717bool is_cgroup_fs(const struct statfs *s) {
2718 return is_fs_type(s, CGROUP_SUPER_MAGIC) ||
2719 is_fs_type(s, CGROUP2_SUPER_MAGIC);
2720}
2721
2722bool fd_is_cgroup_fs(int fd) {
2723 struct statfs s;
2724
2725 if (fstatfs(fd, &s) < 0)
2726 return -errno;
2727
2728 return is_cgroup_fs(&s);
2729}
2730
efdb0237
LP
2731static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2732 [CGROUP_CONTROLLER_CPU] = "cpu",
2733 [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
13c31542 2734 [CGROUP_CONTROLLER_IO] = "io",
efdb0237
LP
2735 [CGROUP_CONTROLLER_BLKIO] = "blkio",
2736 [CGROUP_CONTROLLER_MEMORY] = "memory",
3905f127 2737 [CGROUP_CONTROLLER_DEVICES] = "devices",
03a7b521 2738 [CGROUP_CONTROLLER_PIDS] = "pids",
efdb0237
LP
2739};
2740
2741DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);