1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include <sys/statfs.h>
14 #include <sys/types.h>
15 #include <sys/xattr.h>
18 #include "alloc-util.h"
19 #include "cgroup-util.h"
21 #include "dirent-util.h"
22 #include "extract-word.h"
25 #include "format-util.h"
28 #include "login-util.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "proc-cmdline.h"
35 #include "process-util.h"
38 #include "stat-util.h"
39 #include "stdio-util.h"
40 #include "string-table.h"
41 #include "string-util.h"
43 #include "unit-name.h"
44 #include "user-util.h"
46 int cg_enumerate_processes(const char *controller
, const char *path
, FILE **_f
) {
47 _cleanup_free_
char *fs
= NULL
;
53 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
65 int cg_read_pid(FILE *f
, pid_t
*_pid
) {
68 /* Note that the cgroup.procs might contain duplicates! See
69 * cgroups.txt for details. */
75 if (fscanf(f
, "%lu", &ul
) != 1) {
80 return errno
> 0 ? -errno
: -EIO
;
91 const char *controller
,
96 _cleanup_free_
char *events
= NULL
, *content
= NULL
;
100 r
= cg_get_path(controller
, path
, "cgroup.events", &events
);
104 r
= read_full_file(events
, &content
, NULL
);
109 while ((line
= strsep(&p
, "\n"))) {
112 key
= strsep(&line
, " ");
116 if (strcmp(key
, event
))
126 bool cg_ns_supported(void) {
127 static thread_local
int enabled
= -1;
132 if (access("/proc/self/ns/cgroup", F_OK
) == 0)
140 int cg_enumerate_subgroups(const char *controller
, const char *path
, DIR **_d
) {
141 _cleanup_free_
char *fs
= NULL
;
147 /* This is not recursive! */
149 r
= cg_get_path(controller
, path
, NULL
, &fs
);
161 int cg_read_subgroup(DIR *d
, char **fn
) {
167 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
170 if (de
->d_type
!= DT_DIR
)
173 if (dot_or_dot_dot(de
->d_name
))
176 b
= strdup(de
->d_name
);
187 int cg_rmdir(const char *controller
, const char *path
) {
188 _cleanup_free_
char *p
= NULL
;
191 r
= cg_get_path(controller
, path
, NULL
, &p
);
196 if (r
< 0 && errno
!= ENOENT
)
199 r
= cg_hybrid_unified();
205 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
206 r
= cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
208 log_warning_errno(r
, "Failed to remove compat systemd cgroup %s: %m", path
);
215 const char *controller
,
220 cg_kill_log_func_t log_kill
,
223 _cleanup_set_free_ Set
*allocated_set
= NULL
;
230 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
231 * SIGCONT on SIGKILL. */
232 if (IN_SET(sig
, SIGCONT
, SIGKILL
))
233 flags
&= ~CGROUP_SIGCONT
;
235 /* This goes through the tasks list and kills them all. This
236 * is repeated until no further processes are added to the
237 * tasks list, to properly handle forking processes */
240 s
= allocated_set
= set_new(NULL
);
245 my_pid
= getpid_cached();
248 _cleanup_fclose_
FILE *f
= NULL
;
252 r
= cg_enumerate_processes(controller
, path
, &f
);
254 if (ret
>= 0 && r
!= -ENOENT
)
260 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
262 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
265 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
269 log_kill(pid
, sig
, userdata
);
271 /* If we haven't killed this process yet, kill
273 if (kill(pid
, sig
) < 0) {
274 if (ret
>= 0 && errno
!= ESRCH
)
277 if (flags
& CGROUP_SIGCONT
)
278 (void) kill(pid
, SIGCONT
);
286 r
= set_put(s
, PID_TO_PTR(pid
));
302 /* To avoid racing against processes which fork
303 * quicker than we can kill them we repeat this until
304 * no new pids need to be killed. */
311 int cg_kill_recursive(
312 const char *controller
,
317 cg_kill_log_func_t log_kill
,
320 _cleanup_set_free_ Set
*allocated_set
= NULL
;
321 _cleanup_closedir_
DIR *d
= NULL
;
329 s
= allocated_set
= set_new(NULL
);
334 ret
= cg_kill(controller
, path
, sig
, flags
, s
, log_kill
, userdata
);
336 r
= cg_enumerate_subgroups(controller
, path
, &d
);
338 if (ret
>= 0 && r
!= -ENOENT
)
344 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
345 _cleanup_free_
char *p
= NULL
;
347 p
= strjoin(path
, "/", fn
);
352 r
= cg_kill_recursive(controller
, p
, sig
, flags
, s
, log_kill
, userdata
);
353 if (r
!= 0 && ret
>= 0)
356 if (ret
>= 0 && r
< 0)
359 if (flags
& CGROUP_REMOVE
) {
360 r
= cg_rmdir(controller
, path
);
361 if (r
< 0 && ret
>= 0 && !IN_SET(r
, -ENOENT
, -EBUSY
))
376 _cleanup_set_free_ Set
*s
= NULL
;
389 my_pid
= getpid_cached();
392 _cleanup_fclose_
FILE *f
= NULL
;
396 r
= cg_enumerate_processes(cfrom
, pfrom
, &f
);
398 if (ret
>= 0 && r
!= -ENOENT
)
404 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
406 /* This might do weird stuff if we aren't a
407 * single-threaded program. However, we
408 * luckily know we are not */
409 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
412 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
415 /* Ignore kernel threads. Since they can only
416 * exist in the root cgroup, we only check for
419 empty_or_root(pfrom
) &&
420 is_kernel_thread(pid
) > 0)
423 r
= cg_attach(cto
, pto
, pid
);
425 if (ret
>= 0 && r
!= -ESRCH
)
432 r
= set_put(s
, PID_TO_PTR(pid
));
452 int cg_migrate_recursive(
459 _cleanup_closedir_
DIR *d
= NULL
;
468 ret
= cg_migrate(cfrom
, pfrom
, cto
, pto
, flags
);
470 r
= cg_enumerate_subgroups(cfrom
, pfrom
, &d
);
472 if (ret
>= 0 && r
!= -ENOENT
)
478 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
479 _cleanup_free_
char *p
= NULL
;
481 p
= strjoin(pfrom
, "/", fn
);
486 r
= cg_migrate_recursive(cfrom
, p
, cto
, pto
, flags
);
487 if (r
!= 0 && ret
>= 0)
491 if (r
< 0 && ret
>= 0)
494 if (flags
& CGROUP_REMOVE
) {
495 r
= cg_rmdir(cfrom
, pfrom
);
496 if (r
< 0 && ret
>= 0 && !IN_SET(r
, -ENOENT
, -EBUSY
))
503 int cg_migrate_recursive_fallback(
517 r
= cg_migrate_recursive(cfrom
, pfrom
, cto
, pto
, flags
);
519 char prefix
[strlen(pto
) + 1];
521 /* This didn't work? Then let's try all prefixes of the destination */
523 PATH_FOREACH_PREFIX(prefix
, pto
) {
526 q
= cg_migrate_recursive(cfrom
, pfrom
, cto
, prefix
, flags
);
535 static const char *controller_to_dirname(const char *controller
) {
540 /* Converts a controller name to the directory name below
541 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
542 * just cuts off the name= prefixed used for named
543 * hierarchies, if it is specified. */
545 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
546 if (cg_hybrid_unified() > 0)
547 controller
= SYSTEMD_CGROUP_CONTROLLER_HYBRID
;
549 controller
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
552 e
= startswith(controller
, "name=");
559 static int join_path_legacy(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
566 dn
= controller_to_dirname(controller
);
568 if (isempty(path
) && isempty(suffix
))
569 t
= strappend("/sys/fs/cgroup/", dn
);
570 else if (isempty(path
))
571 t
= strjoin("/sys/fs/cgroup/", dn
, "/", suffix
);
572 else if (isempty(suffix
))
573 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
);
575 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
, "/", suffix
);
583 static int join_path_unified(const char *path
, const char *suffix
, char **fs
) {
588 if (isempty(path
) && isempty(suffix
))
589 t
= strdup("/sys/fs/cgroup");
590 else if (isempty(path
))
591 t
= strappend("/sys/fs/cgroup/", suffix
);
592 else if (isempty(suffix
))
593 t
= strappend("/sys/fs/cgroup/", path
);
595 t
= strjoin("/sys/fs/cgroup/", path
, "/", suffix
);
603 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
611 /* If no controller is specified, we return the path
612 * *below* the controllers, without any prefix. */
614 if (!path
&& !suffix
)
622 t
= strjoin(path
, "/", suffix
);
626 *fs
= path_simplify(t
, false);
630 if (!cg_controller_is_valid(controller
))
633 r
= cg_all_unified();
637 r
= join_path_unified(path
, suffix
, fs
);
639 r
= join_path_legacy(controller
, path
, suffix
, fs
);
643 path_simplify(*fs
, false);
647 static int controller_is_accessible(const char *controller
) {
652 /* Checks whether a specific controller is accessible,
653 * i.e. its hierarchy mounted. In the unified hierarchy all
654 * controllers are considered accessible, except for the named
657 if (!cg_controller_is_valid(controller
))
660 r
= cg_all_unified();
664 /* We don't support named hierarchies if we are using
665 * the unified hierarchy. */
667 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
670 if (startswith(controller
, "name="))
676 dn
= controller_to_dirname(controller
);
677 cc
= strjoina("/sys/fs/cgroup/", dn
);
679 if (laccess(cc
, F_OK
) < 0)
686 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
692 /* Check if the specified controller is actually accessible */
693 r
= controller_is_accessible(controller
);
697 return cg_get_path(controller
, path
, suffix
, fs
);
700 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
705 if (typeflag
!= FTW_DP
)
708 if (ftwbuf
->level
< 1)
715 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
716 _cleanup_free_
char *fs
= NULL
;
721 r
= cg_get_path(controller
, path
, NULL
, &fs
);
726 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0) {
736 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
740 q
= cg_hybrid_unified();
743 if (q
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
744 q
= cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, delete_root
);
746 log_warning_errno(q
, "Failed to trim compat systemd cgroup %s: %m", path
);
752 /* Create a cgroup in the hierarchy of controller.
753 * Returns 0 if the group already existed, 1 on success, negative otherwise.
755 int cg_create(const char *controller
, const char *path
) {
756 _cleanup_free_
char *fs
= NULL
;
759 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
763 r
= mkdir_parents(fs
, 0755);
767 r
= mkdir_errno_wrapper(fs
, 0755);
773 r
= cg_hybrid_unified();
777 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
778 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
780 log_warning_errno(r
, "Failed to create compat systemd cgroup %s: %m", path
);
786 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
791 r
= cg_create(controller
, path
);
795 q
= cg_attach(controller
, path
, pid
);
799 /* This does not remove the cgroup on failure */
803 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
804 _cleanup_free_
char *fs
= NULL
;
805 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
811 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
816 pid
= getpid_cached();
818 xsprintf(c
, PID_FMT
"\n", pid
);
820 r
= write_string_file(fs
, c
, 0);
824 r
= cg_hybrid_unified();
828 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
829 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, pid
);
831 log_warning_errno(r
, "Failed to attach "PID_FMT
" to compat systemd cgroup %s: %m", pid
, path
);
837 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
844 r
= cg_attach(controller
, path
, pid
);
846 char prefix
[strlen(path
) + 1];
848 /* This didn't work? Then let's try all prefixes of
851 PATH_FOREACH_PREFIX(prefix
, path
) {
854 q
= cg_attach(controller
, prefix
, pid
);
864 const char *controller
,
874 /* cgroupsv1, aka legacy/non-unified */
875 static const struct Attribute legacy_attributes
[] = {
876 { "cgroup.procs", true },
878 { "cgroup.clone_children", false },
882 /* cgroupsv2, aka unified */
883 static const struct Attribute unified_attributes
[] = {
884 { "cgroup.procs", true },
885 { "cgroup.subtree_control", true },
886 { "cgroup.threads", false },
890 static const struct Attribute
* const attributes
[] = {
891 [false] = legacy_attributes
,
892 [true] = unified_attributes
,
895 _cleanup_free_
char *fs
= NULL
;
896 const struct Attribute
*i
;
901 if (uid
== UID_INVALID
&& gid
== GID_INVALID
)
904 unified
= cg_unified_controller(controller
);
908 /* Configure access to the cgroup itself */
909 r
= cg_get_path(controller
, path
, NULL
, &fs
);
913 r
= chmod_and_chown(fs
, 0755, uid
, gid
);
917 /* Configure access to the cgroup's attributes */
918 for (i
= attributes
[unified
]; i
->name
; i
++) {
921 r
= cg_get_path(controller
, path
, i
->name
, &fs
);
925 r
= chmod_and_chown(fs
, 0644, uid
, gid
);
930 log_debug_errno(r
, "Failed to set access on cgroup %s, ignoring: %m", fs
);
934 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
935 r
= cg_hybrid_unified();
939 /* Always propagate access mode from unified to legacy controller */
940 r
= cg_set_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, uid
, gid
);
942 log_debug_errno(r
, "Failed to set access on compatibility systemd cgroup %s, ignoring: %m", path
);
949 int cg_set_xattr(const char *controller
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
) {
950 _cleanup_free_
char *fs
= NULL
;
955 assert(value
|| size
<= 0);
957 r
= cg_get_path(controller
, path
, NULL
, &fs
);
961 if (setxattr(fs
, name
, value
, size
, flags
) < 0)
967 int cg_get_xattr(const char *controller
, const char *path
, const char *name
, void *value
, size_t size
) {
968 _cleanup_free_
char *fs
= NULL
;
975 r
= cg_get_path(controller
, path
, NULL
, &fs
);
979 n
= getxattr(fs
, name
, value
, size
);
986 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
987 _cleanup_fclose_
FILE *f
= NULL
;
989 const char *fs
, *controller_str
;
997 if (!cg_controller_is_valid(controller
))
1000 controller
= SYSTEMD_CGROUP_CONTROLLER
;
1002 unified
= cg_unified_controller(controller
);
1006 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
1007 controller_str
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
1009 controller_str
= controller
;
1011 cs
= strlen(controller_str
);
1014 fs
= procfs_file_alloca(pid
, "cgroup");
1015 f
= fopen(fs
, "re");
1017 return errno
== ENOENT
? -ESRCH
: -errno
;
1019 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1021 FOREACH_LINE(line
, f
, return -errno
) {
1027 e
= startswith(line
, "0:");
1037 const char *word
, *state
;
1040 l
= strchr(line
, ':');
1050 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
)
1051 if (k
== cs
&& memcmp(word
, controller_str
, cs
) == 0) {
1063 /* Truncate suffix indicating the process is a zombie */
1064 e
= endswith(p
, " (deleted)");
1075 int cg_install_release_agent(const char *controller
, const char *agent
) {
1076 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
1082 r
= cg_unified_controller(controller
);
1085 if (r
> 0) /* doesn't apply to unified hierarchy */
1088 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1092 r
= read_one_line_file(fs
, &contents
);
1096 sc
= strstrip(contents
);
1098 r
= write_string_file(fs
, agent
, 0);
1101 } else if (!path_equal(sc
, agent
))
1105 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1109 contents
= mfree(contents
);
1110 r
= read_one_line_file(fs
, &contents
);
1114 sc
= strstrip(contents
);
1115 if (streq(sc
, "0")) {
1116 r
= write_string_file(fs
, "1", 0);
1123 if (!streq(sc
, "1"))
1129 int cg_uninstall_release_agent(const char *controller
) {
1130 _cleanup_free_
char *fs
= NULL
;
1133 r
= cg_unified_controller(controller
);
1136 if (r
> 0) /* Doesn't apply to unified hierarchy */
1139 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1143 r
= write_string_file(fs
, "0", 0);
1149 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1153 r
= write_string_file(fs
, "", 0);
1160 int cg_is_empty(const char *controller
, const char *path
) {
1161 _cleanup_fclose_
FILE *f
= NULL
;
1167 r
= cg_enumerate_processes(controller
, path
, &f
);
1173 r
= cg_read_pid(f
, &pid
);
1180 int cg_is_empty_recursive(const char *controller
, const char *path
) {
1185 /* The root cgroup is always populated */
1186 if (controller
&& empty_or_root(path
))
1189 r
= cg_unified_controller(controller
);
1193 _cleanup_free_
char *t
= NULL
;
1195 /* On the unified hierarchy we can check empty state
1196 * via the "populated" attribute of "cgroup.events". */
1198 r
= cg_read_event(controller
, path
, "populated", &t
);
1202 return streq(t
, "0");
1204 _cleanup_closedir_
DIR *d
= NULL
;
1207 r
= cg_is_empty(controller
, path
);
1211 r
= cg_enumerate_subgroups(controller
, path
, &d
);
1217 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
1218 _cleanup_free_
char *p
= NULL
;
1220 p
= strjoin(path
, "/", fn
);
1225 r
= cg_is_empty_recursive(controller
, p
);
1236 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
1237 char *t
= NULL
, *u
= NULL
;
1243 if (!path_is_normalized(spec
))
1251 *path
= path_simplify(t
, false);
1260 e
= strchr(spec
, ':');
1262 if (!cg_controller_is_valid(spec
))
1279 t
= strndup(spec
, e
-spec
);
1282 if (!cg_controller_is_valid(t
)) {
1296 if (!path_is_normalized(u
) ||
1297 !path_is_absolute(u
)) {
1303 path_simplify(u
, false);
1319 int cg_mangle_path(const char *path
, char **result
) {
1320 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1327 /* First, check if it already is a filesystem path */
1328 if (path_startswith(path
, "/sys/fs/cgroup")) {
1334 *result
= path_simplify(t
, false);
1338 /* Otherwise, treat it as cg spec */
1339 r
= cg_split_spec(path
, &c
, &p
);
1343 return cg_get_path(c
?: SYSTEMD_CGROUP_CONTROLLER
, p
?: "/", NULL
, result
);
1346 int cg_get_root_path(char **path
) {
1352 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1356 e
= endswith(p
, "/" SPECIAL_INIT_SCOPE
);
1358 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
); /* legacy */
1360 e
= endswith(p
, "/system"); /* even more legacy */
1368 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1369 _cleanup_free_
char *rt
= NULL
;
1377 /* If the root was specified let's use that, otherwise
1378 * let's determine it from PID 1 */
1380 r
= cg_get_root_path(&rt
);
1387 p
= path_startswith(cgroup
, root
);
1388 if (p
&& p
> cgroup
)
1396 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1397 _cleanup_free_
char *raw
= NULL
;
1404 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1408 r
= cg_shift_path(raw
, root
, &c
);
1413 *cgroup
= TAKE_PTR(raw
);
1427 int cg_path_decode_unit(const char *cgroup
, char **unit
) {
1434 n
= strcspn(cgroup
, "/");
1438 c
= strndupa(cgroup
, n
);
1441 if (!unit_name_is_valid(c
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1452 static bool valid_slice_name(const char *p
, size_t n
) {
1457 if (n
< STRLEN("x.slice"))
1460 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1466 c
= cg_unescape(buf
);
1468 return unit_name_is_valid(c
, UNIT_NAME_PLAIN
);
1474 static const char *skip_slices(const char *p
) {
1477 /* Skips over all slice assignments */
1482 p
+= strspn(p
, "/");
1484 n
= strcspn(p
, "/");
1485 if (!valid_slice_name(p
, n
))
1492 int cg_path_get_unit(const char *path
, char **ret
) {
1500 e
= skip_slices(path
);
1502 r
= cg_path_decode_unit(e
, &unit
);
1506 /* We skipped over the slices, don't accept any now */
1507 if (endswith(unit
, ".slice")) {
1516 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1517 _cleanup_free_
char *cgroup
= NULL
;
1522 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1526 return cg_path_get_unit(cgroup
, unit
);
1530 * Skip session-*.scope, but require it to be there.
1532 static const char *skip_session(const char *p
) {
1538 p
+= strspn(p
, "/");
1540 n
= strcspn(p
, "/");
1541 if (n
< STRLEN("session-x.scope"))
1544 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1545 char buf
[n
- 8 - 6 + 1];
1547 memcpy(buf
, p
+ 8, n
- 8 - 6);
1550 /* Note that session scopes never need unescaping,
1551 * since they cannot conflict with the kernel's own
1552 * names, hence we don't need to call cg_unescape()
1555 if (!session_id_valid(buf
))
1559 p
+= strspn(p
, "/");
1567 * Skip user@*.service, but require it to be there.
1569 static const char *skip_user_manager(const char *p
) {
1575 p
+= strspn(p
, "/");
1577 n
= strcspn(p
, "/");
1578 if (n
< STRLEN("user@x.service"))
1581 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1582 char buf
[n
- 5 - 8 + 1];
1584 memcpy(buf
, p
+ 5, n
- 5 - 8);
1587 /* Note that user manager services never need unescaping,
1588 * since they cannot conflict with the kernel's own
1589 * names, hence we don't need to call cg_unescape()
1592 if (parse_uid(buf
, NULL
) < 0)
1596 p
+= strspn(p
, "/");
1604 static const char *skip_user_prefix(const char *path
) {
1609 /* Skip slices, if there are any */
1610 e
= skip_slices(path
);
1612 /* Skip the user manager, if it's in the path now... */
1613 t
= skip_user_manager(e
);
1617 /* Alternatively skip the user session if it is in the path... */
1618 return skip_session(e
);
1621 int cg_path_get_user_unit(const char *path
, char **ret
) {
1627 t
= skip_user_prefix(path
);
1631 /* And from here on it looks pretty much the same as for a
1632 * system unit, hence let's use the same parser from here
1634 return cg_path_get_unit(t
, ret
);
1637 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1638 _cleanup_free_
char *cgroup
= NULL
;
1643 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1647 return cg_path_get_user_unit(cgroup
, unit
);
1650 int cg_path_get_machine_name(const char *path
, char **machine
) {
1651 _cleanup_free_
char *u
= NULL
;
1655 r
= cg_path_get_unit(path
, &u
);
1659 sl
= strjoina("/run/systemd/machines/unit:", u
);
1660 return readlink_malloc(sl
, machine
);
1663 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1664 _cleanup_free_
char *cgroup
= NULL
;
1669 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1673 return cg_path_get_machine_name(cgroup
, machine
);
1676 int cg_path_get_session(const char *path
, char **session
) {
1677 _cleanup_free_
char *unit
= NULL
;
1683 r
= cg_path_get_unit(path
, &unit
);
1687 start
= startswith(unit
, "session-");
1690 end
= endswith(start
, ".scope");
1695 if (!session_id_valid(start
))
1711 int cg_pid_get_session(pid_t pid
, char **session
) {
1712 _cleanup_free_
char *cgroup
= NULL
;
1715 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1719 return cg_path_get_session(cgroup
, session
);
1722 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1723 _cleanup_free_
char *slice
= NULL
;
1729 r
= cg_path_get_slice(path
, &slice
);
1733 start
= startswith(slice
, "user-");
1736 end
= endswith(start
, ".slice");
1741 if (parse_uid(start
, uid
) < 0)
1747 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1748 _cleanup_free_
char *cgroup
= NULL
;
1751 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1755 return cg_path_get_owner_uid(cgroup
, uid
);
1758 int cg_path_get_slice(const char *p
, char **slice
) {
1759 const char *e
= NULL
;
1764 /* Finds the right-most slice unit from the beginning, but
1765 * stops before we come to the first non-slice unit. */
1770 p
+= strspn(p
, "/");
1772 n
= strcspn(p
, "/");
1773 if (!valid_slice_name(p
, n
)) {
1778 s
= strdup(SPECIAL_ROOT_SLICE
);
1786 return cg_path_decode_unit(e
, slice
);
1794 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1795 _cleanup_free_
char *cgroup
= NULL
;
1800 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1804 return cg_path_get_slice(cgroup
, slice
);
1807 int cg_path_get_user_slice(const char *p
, char **slice
) {
1812 t
= skip_user_prefix(p
);
1816 /* And now it looks pretty much the same as for a system
1817 * slice, so let's just use the same parser from here on. */
1818 return cg_path_get_slice(t
, slice
);
1821 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1822 _cleanup_free_
char *cgroup
= NULL
;
1827 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1831 return cg_path_get_user_slice(cgroup
, slice
);
1834 char *cg_escape(const char *p
) {
1835 bool need_prefix
= false;
1837 /* This implements very minimal escaping for names to be used
1838 * as file names in the cgroup tree: any name which might
1839 * conflict with a kernel name or is prefixed with '_' is
1840 * prefixed with a '_'. That way, when reading cgroup names it
1841 * is sufficient to remove a single prefixing underscore if
1844 /* The return value of this function (unlike cg_unescape())
1847 if (IN_SET(p
[0], 0, '_', '.') ||
1848 streq(p
, "notify_on_release") ||
1849 streq(p
, "release_agent") ||
1850 streq(p
, "tasks") ||
1851 startswith(p
, "cgroup."))
1856 dot
= strrchr(p
, '.');
1861 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
1864 n
= cgroup_controller_to_string(c
);
1869 if (memcmp(p
, n
, l
) != 0)
1879 return strappend("_", p
);
1884 char *cg_unescape(const char *p
) {
1887 /* The return value of this function (unlike cg_escape())
1888 * doesn't need free()! */
1896 #define CONTROLLER_VALID \
1900 bool cg_controller_is_valid(const char *p
) {
1906 if (streq(p
, SYSTEMD_CGROUP_CONTROLLER
))
1909 s
= startswith(p
, "name=");
1913 if (IN_SET(*p
, 0, '_'))
1916 for (t
= p
; *t
; t
++)
1917 if (!strchr(CONTROLLER_VALID
, *t
))
1920 if (t
- p
> FILENAME_MAX
)
1926 int cg_slice_to_path(const char *unit
, char **ret
) {
1927 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1934 if (streq(unit
, SPECIAL_ROOT_SLICE
)) {
1944 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1947 if (!endswith(unit
, ".slice"))
1950 r
= unit_name_to_prefix(unit
, &p
);
1954 dash
= strchr(p
, '-');
1956 /* Don't allow initial dashes */
1961 _cleanup_free_
char *escaped
= NULL
;
1962 char n
[dash
- p
+ sizeof(".slice")];
1964 #if HAS_FEATURE_MEMORY_SANITIZER
1965 /* msan doesn't instrument stpncpy, so it thinks
1966 * n is later used unitialized:
1967 * https://github.com/google/sanitizers/issues/926
1972 /* Don't allow trailing or double dashes */
1973 if (IN_SET(dash
[1], 0, '-'))
1976 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1977 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1980 escaped
= cg_escape(n
);
1984 if (!strextend(&s
, escaped
, "/", NULL
))
1987 dash
= strchr(dash
+1, '-');
1990 e
= cg_escape(unit
);
1994 if (!strextend(&s
, e
, NULL
))
2002 int cg_set_attribute(const char *controller
, const char *path
, const char *attribute
, const char *value
) {
2003 _cleanup_free_
char *p
= NULL
;
2006 r
= cg_get_path(controller
, path
, attribute
, &p
);
2010 return write_string_file(p
, value
, 0);
2013 int cg_get_attribute(const char *controller
, const char *path
, const char *attribute
, char **ret
) {
2014 _cleanup_free_
char *p
= NULL
;
2017 r
= cg_get_path(controller
, path
, attribute
, &p
);
2021 return read_one_line_file(p
, ret
);
2024 int cg_get_keyed_attribute(
2025 const char *controller
,
2027 const char *attribute
,
2029 char **ret_values
) {
2031 _cleanup_free_
char *filename
= NULL
, *contents
= NULL
;
2033 size_t n
, i
, n_done
= 0;
2037 /* Reads one or more fields of a cgroupsv2 keyed attribute file. The 'keys' parameter should be an strv with
2038 * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of
2039 * entries as 'keys'. On success each entry will be set to the value of the matching key.
2041 * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. */
2043 r
= cg_get_path(controller
, path
, attribute
, &filename
);
2047 r
= read_full_file(filename
, &contents
, NULL
);
2051 n
= strv_length(keys
);
2052 if (n
== 0) /* No keys to retrieve? That's easy, we are done then */
2055 /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */
2056 v
= newa0(char*, n
);
2058 for (p
= contents
; *p
;) {
2059 const char *w
= NULL
;
2061 for (i
= 0; i
< n
; i
++)
2063 w
= first_word(p
, keys
[i
]);
2071 l
= strcspn(w
, NEWLINE
);
2072 v
[i
] = strndup(w
, l
);
2084 p
+= strcspn(p
, NEWLINE
);
2086 p
+= strspn(p
, NEWLINE
);
2092 for (i
= 0; i
< n
; i
++)
2098 memcpy(ret_values
, v
, sizeof(char*) * n
);
2103 int cg_create_everywhere(CGroupMask supported
, CGroupMask mask
, const char *path
) {
2108 /* This one will create a cgroup in our private tree, but also
2109 * duplicate it in the trees specified in mask, and remove it
2112 * Returns 0 if the group already existed in the systemd hierarchy,
2113 * 1 on success, negative otherwise.
2116 /* First create the cgroup in our own hierarchy. */
2117 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
2122 /* If we are in the unified hierarchy, we are done now */
2123 r
= cg_all_unified();
2129 /* Otherwise, do the same in the other hierarchies */
2130 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2131 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2134 n
= cgroup_controller_to_string(c
);
2137 (void) cg_create(n
, path
);
2138 else if (supported
& bit
)
2139 (void) cg_trim(n
, path
, true);
2145 int cg_attach_everywhere(CGroupMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
2149 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
2153 r
= cg_all_unified();
2159 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2160 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2161 const char *p
= NULL
;
2163 if (!(supported
& bit
))
2167 p
= path_callback(bit
, userdata
);
2172 (void) cg_attach_fallback(cgroup_controller_to_string(c
), p
, pid
);
2178 int cg_attach_many_everywhere(CGroupMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
2183 SET_FOREACH(pidp
, pids
, i
) {
2184 pid_t pid
= PTR_TO_PID(pidp
);
2187 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
2188 if (q
< 0 && r
>= 0)
2195 int cg_migrate_everywhere(CGroupMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
2199 if (!path_equal(from
, to
)) {
2200 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, CGROUP_REMOVE
);
2205 q
= cg_all_unified();
2211 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2212 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2213 const char *p
= NULL
;
2215 if (!(supported
& bit
))
2219 p
= to_callback(bit
, userdata
);
2224 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, cgroup_controller_to_string(c
), p
, 0);
2230 int cg_trim_everywhere(CGroupMask supported
, const char *path
, bool delete_root
) {
2234 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
2238 q
= cg_all_unified();
2244 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2245 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2247 if (!(supported
& bit
))
2250 (void) cg_trim(cgroup_controller_to_string(c
), path
, delete_root
);
2256 int cg_mask_to_string(CGroupMask mask
, char **ret
) {
2257 _cleanup_free_
char *s
= NULL
;
2258 size_t n
= 0, allocated
= 0;
2269 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2273 if (!(mask
& CGROUP_CONTROLLER_TO_MASK(c
)))
2276 k
= cgroup_controller_to_string(c
);
2279 if (!GREEDY_REALLOC(s
, allocated
, n
+ space
+ l
+ 1))
2284 memcpy(s
+ n
+ space
, k
, l
);
2298 int cg_mask_from_string(const char *value
, CGroupMask
*mask
) {
2303 _cleanup_free_
char *n
= NULL
;
2307 r
= extract_first_word(&value
, &n
, NULL
, 0);
2313 v
= cgroup_controller_from_string(n
);
2317 *mask
|= CGROUP_CONTROLLER_TO_MASK(v
);
2322 int cg_mask_supported(CGroupMask
*ret
) {
2323 CGroupMask mask
= 0;
2326 /* Determines the mask of supported cgroup controllers. Only
2327 * includes controllers we can make sense of and that are
2328 * actually accessible. */
2330 r
= cg_all_unified();
2334 _cleanup_free_
char *root
= NULL
, *controllers
= NULL
, *path
= NULL
;
2336 /* In the unified hierarchy we can read the supported
2337 * and accessible controllers from a the top-level
2338 * cgroup attribute */
2340 r
= cg_get_root_path(&root
);
2344 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, root
, "cgroup.controllers", &path
);
2348 r
= read_one_line_file(path
, &controllers
);
2352 r
= cg_mask_from_string(controllers
, &mask
);
2356 /* Currently, we support the cpu, memory, io and pids
2357 * controller in the unified hierarchy, mask
2358 * everything else off. */
2359 mask
&= CGROUP_MASK_CPU
| CGROUP_MASK_MEMORY
| CGROUP_MASK_IO
| CGROUP_MASK_PIDS
;
2364 /* In the legacy hierarchy, we check whether which
2365 * hierarchies are mounted. */
2367 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2370 n
= cgroup_controller_to_string(c
);
2371 if (controller_is_accessible(n
) >= 0)
2372 mask
|= CGROUP_CONTROLLER_TO_MASK(c
);
2380 int cg_kernel_controllers(Set
**ret
) {
2381 _cleanup_set_free_free_ Set
*controllers
= NULL
;
2382 _cleanup_fclose_
FILE *f
= NULL
;
2387 /* Determines the full list of kernel-known controllers. Might
2388 * include controllers we don't actually support, arbitrary
2389 * named hierarchies and controllers that aren't currently
2390 * accessible (because not mounted). */
2392 controllers
= set_new(&string_hash_ops
);
2396 f
= fopen("/proc/cgroups", "re");
2398 if (errno
== ENOENT
) {
2406 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
2408 /* Ignore the header line */
2409 (void) read_line(f
, (size_t) -1, NULL
);
2416 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
2421 if (ferror(f
) && errno
> 0)
2432 if (!cg_controller_is_valid(controller
)) {
2437 r
= set_consume(controllers
, controller
);
2442 *ret
= TAKE_PTR(controllers
);
2447 static thread_local CGroupUnified unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2449 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2450 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2451 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2452 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2454 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2455 * process management but disable the compat dual layout, we return %true on
2456 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2458 static thread_local
bool unified_systemd_v232
;
2460 static int cg_unified_update(void) {
2464 /* Checks if we support the unified hierarchy. Returns an
2465 * error when the cgroup hierarchies aren't mounted yet or we
2466 * have any other trouble determining if the unified hierarchy
2469 if (unified_cache
>= CGROUP_UNIFIED_NONE
)
2472 if (statfs("/sys/fs/cgroup/", &fs
) < 0)
2473 return log_debug_errno(errno
, "statfs(\"/sys/fs/cgroup/\") failed: %m");
2475 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2476 log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
2477 unified_cache
= CGROUP_UNIFIED_ALL
;
2478 } else if (F_TYPE_EQUAL(fs
.f_type
, TMPFS_MAGIC
)) {
2479 if (statfs("/sys/fs/cgroup/unified/", &fs
) == 0 &&
2480 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2481 log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller");
2482 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2483 unified_systemd_v232
= false;
2485 if (statfs("/sys/fs/cgroup/systemd/", &fs
) < 0)
2486 return log_debug_errno(errno
, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m");
2488 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2489 log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)");
2490 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2491 unified_systemd_v232
= true;
2492 } else if (F_TYPE_EQUAL(fs
.f_type
, CGROUP_SUPER_MAGIC
)) {
2493 log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy");
2494 unified_cache
= CGROUP_UNIFIED_NONE
;
2496 log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy",
2497 (unsigned long long) fs
.f_type
);
2498 unified_cache
= CGROUP_UNIFIED_NONE
;
2502 log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2503 (unsigned long long) fs
.f_type
);
2510 int cg_unified_controller(const char *controller
) {
2513 r
= cg_unified_update();
2517 if (unified_cache
== CGROUP_UNIFIED_NONE
)
2520 if (unified_cache
>= CGROUP_UNIFIED_ALL
)
2523 return streq_ptr(controller
, SYSTEMD_CGROUP_CONTROLLER
);
2526 int cg_all_unified(void) {
2529 r
= cg_unified_update();
2533 return unified_cache
>= CGROUP_UNIFIED_ALL
;
2536 int cg_hybrid_unified(void) {
2539 r
= cg_unified_update();
2543 return unified_cache
== CGROUP_UNIFIED_SYSTEMD
&& !unified_systemd_v232
;
2546 int cg_unified_flush(void) {
2547 unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2549 return cg_unified_update();
2552 int cg_enable_everywhere(CGroupMask supported
, CGroupMask mask
, const char *p
) {
2553 _cleanup_fclose_
FILE *f
= NULL
;
2554 _cleanup_free_
char *fs
= NULL
;
2563 r
= cg_all_unified();
2566 if (r
== 0) /* on the legacy hiearchy there's no joining of controllers defined */
2569 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, p
, "cgroup.subtree_control", &fs
);
2573 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2574 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2577 if (!(supported
& bit
))
2580 n
= cgroup_controller_to_string(c
);
2582 char s
[1 + strlen(n
) + 1];
2584 s
[0] = mask
& bit
? '+' : '-';
2588 f
= fopen(fs
, "we");
2590 log_debug_errno(errno
, "Failed to open cgroup.subtree_control file of %s: %m", p
);
2595 r
= write_string_stream(f
, s
, 0);
2597 log_debug_errno(r
, "Failed to enable controller %s for %s (%s): %m", n
, p
, fs
);
2606 bool cg_is_unified_wanted(void) {
2607 static thread_local
int wanted
= -1;
2610 const bool is_default
= DEFAULT_HIERARCHY
== CGROUP_UNIFIED_ALL
;
2612 /* If we have a cached value, return that. */
2616 /* If the hierarchy is already mounted, then follow whatever
2617 * was chosen for it. */
2618 if (cg_unified_flush() >= 0)
2619 return (wanted
= unified_cache
>= CGROUP_UNIFIED_ALL
);
2621 /* Otherwise, let's see what the kernel command line has to say.
2622 * Since checking is expensive, cache a non-error result. */
2623 r
= proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b
);
2625 return (wanted
= r
> 0 ? b
: is_default
);
2628 bool cg_is_legacy_wanted(void) {
2629 static thread_local
int wanted
= -1;
2631 /* If we have a cached value, return that. */
2635 /* Check if we have cgroups2 already mounted. */
2636 if (cg_unified_flush() >= 0 &&
2637 unified_cache
== CGROUP_UNIFIED_ALL
)
2638 return (wanted
= false);
2640 /* Otherwise, assume that at least partial legacy is wanted,
2641 * since cgroups2 should already be mounted at this point. */
2642 return (wanted
= true);
2645 bool cg_is_hybrid_wanted(void) {
2646 static thread_local
int wanted
= -1;
2649 const bool is_default
= DEFAULT_HIERARCHY
>= CGROUP_UNIFIED_SYSTEMD
;
2650 /* We default to true if the default is "hybrid", obviously,
2651 * but also when the default is "unified", because if we get
2652 * called, it means that unified hierarchy was not mounted. */
2654 /* If we have a cached value, return that. */
2658 /* If the hierarchy is already mounted, then follow whatever
2659 * was chosen for it. */
2660 if (cg_unified_flush() >= 0 &&
2661 unified_cache
== CGROUP_UNIFIED_ALL
)
2662 return (wanted
= false);
2664 /* Otherwise, let's see what the kernel command line has to say.
2665 * Since checking is expensive, cache a non-error result. */
2666 r
= proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b
);
2668 /* The meaning of the kernel option is reversed wrt. to the return value
2669 * of this function, hence the negation. */
2670 return (wanted
= r
> 0 ? !b
: is_default
);
2673 int cg_weight_parse(const char *s
, uint64_t *ret
) {
2678 *ret
= CGROUP_WEIGHT_INVALID
;
2682 r
= safe_atou64(s
, &u
);
2686 if (u
< CGROUP_WEIGHT_MIN
|| u
> CGROUP_WEIGHT_MAX
)
2693 const uint64_t cgroup_io_limit_defaults
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2694 [CGROUP_IO_RBPS_MAX
] = CGROUP_LIMIT_MAX
,
2695 [CGROUP_IO_WBPS_MAX
] = CGROUP_LIMIT_MAX
,
2696 [CGROUP_IO_RIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2697 [CGROUP_IO_WIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2700 static const char* const cgroup_io_limit_type_table
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2701 [CGROUP_IO_RBPS_MAX
] = "IOReadBandwidthMax",
2702 [CGROUP_IO_WBPS_MAX
] = "IOWriteBandwidthMax",
2703 [CGROUP_IO_RIOPS_MAX
] = "IOReadIOPSMax",
2704 [CGROUP_IO_WIOPS_MAX
] = "IOWriteIOPSMax",
2707 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type
, CGroupIOLimitType
);
2709 int cg_cpu_shares_parse(const char *s
, uint64_t *ret
) {
2714 *ret
= CGROUP_CPU_SHARES_INVALID
;
2718 r
= safe_atou64(s
, &u
);
2722 if (u
< CGROUP_CPU_SHARES_MIN
|| u
> CGROUP_CPU_SHARES_MAX
)
2729 int cg_blkio_weight_parse(const char *s
, uint64_t *ret
) {
2734 *ret
= CGROUP_BLKIO_WEIGHT_INVALID
;
2738 r
= safe_atou64(s
, &u
);
2742 if (u
< CGROUP_BLKIO_WEIGHT_MIN
|| u
> CGROUP_BLKIO_WEIGHT_MAX
)
2749 bool is_cgroup_fs(const struct statfs
*s
) {
2750 return is_fs_type(s
, CGROUP_SUPER_MAGIC
) ||
2751 is_fs_type(s
, CGROUP2_SUPER_MAGIC
);
2754 bool fd_is_cgroup_fs(int fd
) {
2757 if (fstatfs(fd
, &s
) < 0)
2760 return is_cgroup_fs(&s
);
2763 static const char *cgroup_controller_table
[_CGROUP_CONTROLLER_MAX
] = {
2764 [CGROUP_CONTROLLER_CPU
] = "cpu",
2765 [CGROUP_CONTROLLER_CPUACCT
] = "cpuacct",
2766 [CGROUP_CONTROLLER_IO
] = "io",
2767 [CGROUP_CONTROLLER_BLKIO
] = "blkio",
2768 [CGROUP_CONTROLLER_MEMORY
] = "memory",
2769 [CGROUP_CONTROLLER_DEVICES
] = "devices",
2770 [CGROUP_CONTROLLER_PIDS
] = "pids",
2773 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller
, CGroupController
);