1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
14 #include <stdio_ext.h>
18 #include <sys/statfs.h>
19 #include <sys/types.h>
20 #include <sys/xattr.h>
23 #include "alloc-util.h"
24 #include "cgroup-util.h"
26 #include "dirent-util.h"
27 #include "extract-word.h"
30 #include "format-util.h"
33 #include "login-util.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "proc-cmdline.h"
40 #include "process-util.h"
43 #include "stat-util.h"
44 #include "stdio-util.h"
45 #include "string-table.h"
46 #include "string-util.h"
48 #include "unit-name.h"
49 #include "user-util.h"
51 int cg_enumerate_processes(const char *controller
, const char *path
, FILE **_f
) {
52 _cleanup_free_
char *fs
= NULL
;
58 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
70 int cg_read_pid(FILE *f
, pid_t
*_pid
) {
73 /* Note that the cgroup.procs might contain duplicates! See
74 * cgroups.txt for details. */
80 if (fscanf(f
, "%lu", &ul
) != 1) {
85 return errno
> 0 ? -errno
: -EIO
;
96 const char *controller
,
101 _cleanup_free_
char *events
= NULL
, *content
= NULL
;
105 r
= cg_get_path(controller
, path
, "cgroup.events", &events
);
109 r
= read_full_file(events
, &content
, NULL
);
114 while ((line
= strsep(&p
, "\n"))) {
117 key
= strsep(&line
, " ");
121 if (strcmp(key
, event
))
131 bool cg_ns_supported(void) {
132 static thread_local
int enabled
= -1;
137 if (access("/proc/self/ns/cgroup", F_OK
) == 0)
145 int cg_enumerate_subgroups(const char *controller
, const char *path
, DIR **_d
) {
146 _cleanup_free_
char *fs
= NULL
;
152 /* This is not recursive! */
154 r
= cg_get_path(controller
, path
, NULL
, &fs
);
166 int cg_read_subgroup(DIR *d
, char **fn
) {
172 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
175 if (de
->d_type
!= DT_DIR
)
178 if (dot_or_dot_dot(de
->d_name
))
181 b
= strdup(de
->d_name
);
192 int cg_rmdir(const char *controller
, const char *path
) {
193 _cleanup_free_
char *p
= NULL
;
196 r
= cg_get_path(controller
, path
, NULL
, &p
);
201 if (r
< 0 && errno
!= ENOENT
)
204 r
= cg_hybrid_unified();
210 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
211 r
= cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
213 log_warning_errno(r
, "Failed to remove compat systemd cgroup %s: %m", path
);
220 const char *controller
,
225 cg_kill_log_func_t log_kill
,
228 _cleanup_set_free_ Set
*allocated_set
= NULL
;
235 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
236 * SIGCONT on SIGKILL. */
237 if (IN_SET(sig
, SIGCONT
, SIGKILL
))
238 flags
&= ~CGROUP_SIGCONT
;
240 /* This goes through the tasks list and kills them all. This
241 * is repeated until no further processes are added to the
242 * tasks list, to properly handle forking processes */
245 s
= allocated_set
= set_new(NULL
);
250 my_pid
= getpid_cached();
253 _cleanup_fclose_
FILE *f
= NULL
;
257 r
= cg_enumerate_processes(controller
, path
, &f
);
259 if (ret
>= 0 && r
!= -ENOENT
)
265 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
267 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
270 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
274 log_kill(pid
, sig
, userdata
);
276 /* If we haven't killed this process yet, kill
278 if (kill(pid
, sig
) < 0) {
279 if (ret
>= 0 && errno
!= ESRCH
)
282 if (flags
& CGROUP_SIGCONT
)
283 (void) kill(pid
, SIGCONT
);
291 r
= set_put(s
, PID_TO_PTR(pid
));
307 /* To avoid racing against processes which fork
308 * quicker than we can kill them we repeat this until
309 * no new pids need to be killed. */
316 int cg_kill_recursive(
317 const char *controller
,
322 cg_kill_log_func_t log_kill
,
325 _cleanup_set_free_ Set
*allocated_set
= NULL
;
326 _cleanup_closedir_
DIR *d
= NULL
;
334 s
= allocated_set
= set_new(NULL
);
339 ret
= cg_kill(controller
, path
, sig
, flags
, s
, log_kill
, userdata
);
341 r
= cg_enumerate_subgroups(controller
, path
, &d
);
343 if (ret
>= 0 && r
!= -ENOENT
)
349 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
350 _cleanup_free_
char *p
= NULL
;
352 p
= strjoin(path
, "/", fn
);
357 r
= cg_kill_recursive(controller
, p
, sig
, flags
, s
, log_kill
, userdata
);
358 if (r
!= 0 && ret
>= 0)
361 if (ret
>= 0 && r
< 0)
364 if (flags
& CGROUP_REMOVE
) {
365 r
= cg_rmdir(controller
, path
);
366 if (r
< 0 && ret
>= 0 && !IN_SET(r
, -ENOENT
, -EBUSY
))
381 _cleanup_set_free_ Set
*s
= NULL
;
394 my_pid
= getpid_cached();
397 _cleanup_fclose_
FILE *f
= NULL
;
401 r
= cg_enumerate_processes(cfrom
, pfrom
, &f
);
403 if (ret
>= 0 && r
!= -ENOENT
)
409 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
411 /* This might do weird stuff if we aren't a
412 * single-threaded program. However, we
413 * luckily know we are not */
414 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
417 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
420 /* Ignore kernel threads. Since they can only
421 * exist in the root cgroup, we only check for
424 empty_or_root(pfrom
) &&
425 is_kernel_thread(pid
) > 0)
428 r
= cg_attach(cto
, pto
, pid
);
430 if (ret
>= 0 && r
!= -ESRCH
)
437 r
= set_put(s
, PID_TO_PTR(pid
));
457 int cg_migrate_recursive(
464 _cleanup_closedir_
DIR *d
= NULL
;
473 ret
= cg_migrate(cfrom
, pfrom
, cto
, pto
, flags
);
475 r
= cg_enumerate_subgroups(cfrom
, pfrom
, &d
);
477 if (ret
>= 0 && r
!= -ENOENT
)
483 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
484 _cleanup_free_
char *p
= NULL
;
486 p
= strjoin(pfrom
, "/", fn
);
491 r
= cg_migrate_recursive(cfrom
, p
, cto
, pto
, flags
);
492 if (r
!= 0 && ret
>= 0)
496 if (r
< 0 && ret
>= 0)
499 if (flags
& CGROUP_REMOVE
) {
500 r
= cg_rmdir(cfrom
, pfrom
);
501 if (r
< 0 && ret
>= 0 && !IN_SET(r
, -ENOENT
, -EBUSY
))
508 int cg_migrate_recursive_fallback(
522 r
= cg_migrate_recursive(cfrom
, pfrom
, cto
, pto
, flags
);
524 char prefix
[strlen(pto
) + 1];
526 /* This didn't work? Then let's try all prefixes of the destination */
528 PATH_FOREACH_PREFIX(prefix
, pto
) {
531 q
= cg_migrate_recursive(cfrom
, pfrom
, cto
, prefix
, flags
);
540 static const char *controller_to_dirname(const char *controller
) {
545 /* Converts a controller name to the directory name below
546 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
547 * just cuts off the name= prefixed used for named
548 * hierarchies, if it is specified. */
550 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
551 if (cg_hybrid_unified() > 0)
552 controller
= SYSTEMD_CGROUP_CONTROLLER_HYBRID
;
554 controller
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
557 e
= startswith(controller
, "name=");
564 static int join_path_legacy(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
571 dn
= controller_to_dirname(controller
);
573 if (isempty(path
) && isempty(suffix
))
574 t
= strappend("/sys/fs/cgroup/", dn
);
575 else if (isempty(path
))
576 t
= strjoin("/sys/fs/cgroup/", dn
, "/", suffix
);
577 else if (isempty(suffix
))
578 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
);
580 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
, "/", suffix
);
588 static int join_path_unified(const char *path
, const char *suffix
, char **fs
) {
593 if (isempty(path
) && isempty(suffix
))
594 t
= strdup("/sys/fs/cgroup");
595 else if (isempty(path
))
596 t
= strappend("/sys/fs/cgroup/", suffix
);
597 else if (isempty(suffix
))
598 t
= strappend("/sys/fs/cgroup/", path
);
600 t
= strjoin("/sys/fs/cgroup/", path
, "/", suffix
);
608 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
616 /* If no controller is specified, we return the path
617 * *below* the controllers, without any prefix. */
619 if (!path
&& !suffix
)
627 t
= strjoin(path
, "/", suffix
);
631 *fs
= path_kill_slashes(t
);
635 if (!cg_controller_is_valid(controller
))
638 r
= cg_all_unified();
642 r
= join_path_unified(path
, suffix
, fs
);
644 r
= join_path_legacy(controller
, path
, suffix
, fs
);
648 path_kill_slashes(*fs
);
652 static int controller_is_accessible(const char *controller
) {
657 /* Checks whether a specific controller is accessible,
658 * i.e. its hierarchy mounted. In the unified hierarchy all
659 * controllers are considered accessible, except for the named
662 if (!cg_controller_is_valid(controller
))
665 r
= cg_all_unified();
669 /* We don't support named hierarchies if we are using
670 * the unified hierarchy. */
672 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
675 if (startswith(controller
, "name="))
681 dn
= controller_to_dirname(controller
);
682 cc
= strjoina("/sys/fs/cgroup/", dn
);
684 if (laccess(cc
, F_OK
) < 0)
691 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
697 /* Check if the specified controller is actually accessible */
698 r
= controller_is_accessible(controller
);
702 return cg_get_path(controller
, path
, suffix
, fs
);
705 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
710 if (typeflag
!= FTW_DP
)
713 if (ftwbuf
->level
< 1)
720 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
721 _cleanup_free_
char *fs
= NULL
;
726 r
= cg_get_path(controller
, path
, NULL
, &fs
);
731 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0) {
741 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
745 q
= cg_hybrid_unified();
748 if (q
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
749 q
= cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, delete_root
);
751 log_warning_errno(q
, "Failed to trim compat systemd cgroup %s: %m", path
);
757 int cg_create(const char *controller
, const char *path
) {
758 _cleanup_free_
char *fs
= NULL
;
761 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
765 r
= mkdir_parents(fs
, 0755);
769 r
= mkdir_errno_wrapper(fs
, 0755);
775 r
= cg_hybrid_unified();
779 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
780 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
782 log_warning_errno(r
, "Failed to create compat systemd cgroup %s: %m", path
);
788 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
793 r
= cg_create(controller
, path
);
797 q
= cg_attach(controller
, path
, pid
);
801 /* This does not remove the cgroup on failure */
805 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
806 _cleanup_free_
char *fs
= NULL
;
807 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
813 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
818 pid
= getpid_cached();
820 xsprintf(c
, PID_FMT
"\n", pid
);
822 r
= write_string_file(fs
, c
, 0);
826 r
= cg_hybrid_unified();
830 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
831 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, pid
);
833 log_warning_errno(r
, "Failed to attach "PID_FMT
" to compat systemd cgroup %s: %m", pid
, path
);
839 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
846 r
= cg_attach(controller
, path
, pid
);
848 char prefix
[strlen(path
) + 1];
850 /* This didn't work? Then let's try all prefixes of
853 PATH_FOREACH_PREFIX(prefix
, path
) {
856 q
= cg_attach(controller
, prefix
, pid
);
866 const char *controller
,
876 /* cgroupsv1, aka legacy/non-unified */
877 static const struct Attribute legacy_attributes
[] = {
878 { "cgroup.procs", true },
880 { "cgroup.clone_children", false },
884 /* cgroupsv2, aka unified */
885 static const struct Attribute unified_attributes
[] = {
886 { "cgroup.procs", true },
887 { "cgroup.subtree_control", true },
888 { "cgroup.threads", false },
892 static const struct Attribute
* const attributes
[] = {
893 [false] = legacy_attributes
,
894 [true] = unified_attributes
,
897 _cleanup_free_
char *fs
= NULL
;
898 const struct Attribute
*i
;
903 if (uid
== UID_INVALID
&& gid
== GID_INVALID
)
906 unified
= cg_unified_controller(controller
);
910 /* Configure access to the cgroup itself */
911 r
= cg_get_path(controller
, path
, NULL
, &fs
);
915 r
= chmod_and_chown(fs
, 0755, uid
, gid
);
919 /* Configure access to the cgroup's attributes */
920 for (i
= attributes
[unified
]; i
->name
; i
++) {
923 r
= cg_get_path(controller
, path
, i
->name
, &fs
);
927 r
= chmod_and_chown(fs
, 0644, uid
, gid
);
932 log_debug_errno(r
, "Failed to set access on cgroup %s, ignoring: %m", fs
);
936 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
937 r
= cg_hybrid_unified();
941 /* Always propagate access mode from unified to legacy controller */
942 r
= cg_set_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, uid
, gid
);
944 log_debug_errno(r
, "Failed to set access on compatibility systemd cgroup %s, ignoring: %m", path
);
951 int cg_set_xattr(const char *controller
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
) {
952 _cleanup_free_
char *fs
= NULL
;
957 assert(value
|| size
<= 0);
959 r
= cg_get_path(controller
, path
, NULL
, &fs
);
963 if (setxattr(fs
, name
, value
, size
, flags
) < 0)
969 int cg_get_xattr(const char *controller
, const char *path
, const char *name
, void *value
, size_t size
) {
970 _cleanup_free_
char *fs
= NULL
;
977 r
= cg_get_path(controller
, path
, NULL
, &fs
);
981 n
= getxattr(fs
, name
, value
, size
);
988 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
989 _cleanup_fclose_
FILE *f
= NULL
;
991 const char *fs
, *controller_str
;
999 if (!cg_controller_is_valid(controller
))
1002 controller
= SYSTEMD_CGROUP_CONTROLLER
;
1004 unified
= cg_unified_controller(controller
);
1008 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
1009 controller_str
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
1011 controller_str
= controller
;
1013 cs
= strlen(controller_str
);
1016 fs
= procfs_file_alloca(pid
, "cgroup");
1017 f
= fopen(fs
, "re");
1019 return errno
== ENOENT
? -ESRCH
: -errno
;
1021 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1023 FOREACH_LINE(line
, f
, return -errno
) {
1029 e
= startswith(line
, "0:");
1039 const char *word
, *state
;
1042 l
= strchr(line
, ':');
1052 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
)
1053 if (k
== cs
&& memcmp(word
, controller_str
, cs
) == 0) {
1065 /* Truncate suffix indicating the process is a zombie */
1066 e
= endswith(p
, " (deleted)");
1077 int cg_install_release_agent(const char *controller
, const char *agent
) {
1078 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
1084 r
= cg_unified_controller(controller
);
1087 if (r
> 0) /* doesn't apply to unified hierarchy */
1090 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1094 r
= read_one_line_file(fs
, &contents
);
1098 sc
= strstrip(contents
);
1100 r
= write_string_file(fs
, agent
, 0);
1103 } else if (!path_equal(sc
, agent
))
1107 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1111 contents
= mfree(contents
);
1112 r
= read_one_line_file(fs
, &contents
);
1116 sc
= strstrip(contents
);
1117 if (streq(sc
, "0")) {
1118 r
= write_string_file(fs
, "1", 0);
1125 if (!streq(sc
, "1"))
1131 int cg_uninstall_release_agent(const char *controller
) {
1132 _cleanup_free_
char *fs
= NULL
;
1135 r
= cg_unified_controller(controller
);
1138 if (r
> 0) /* Doesn't apply to unified hierarchy */
1141 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1145 r
= write_string_file(fs
, "0", 0);
1151 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1155 r
= write_string_file(fs
, "", 0);
1162 int cg_is_empty(const char *controller
, const char *path
) {
1163 _cleanup_fclose_
FILE *f
= NULL
;
1169 r
= cg_enumerate_processes(controller
, path
, &f
);
1175 r
= cg_read_pid(f
, &pid
);
1182 int cg_is_empty_recursive(const char *controller
, const char *path
) {
1187 /* The root cgroup is always populated */
1188 if (controller
&& empty_or_root(path
))
1191 r
= cg_unified_controller(controller
);
1195 _cleanup_free_
char *t
= NULL
;
1197 /* On the unified hierarchy we can check empty state
1198 * via the "populated" attribute of "cgroup.events". */
1200 r
= cg_read_event(controller
, path
, "populated", &t
);
1204 return streq(t
, "0");
1206 _cleanup_closedir_
DIR *d
= NULL
;
1209 r
= cg_is_empty(controller
, path
);
1213 r
= cg_enumerate_subgroups(controller
, path
, &d
);
1219 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
1220 _cleanup_free_
char *p
= NULL
;
1222 p
= strjoin(path
, "/", fn
);
1227 r
= cg_is_empty_recursive(controller
, p
);
1238 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
1239 char *t
= NULL
, *u
= NULL
;
1245 if (!path_is_normalized(spec
))
1253 *path
= path_kill_slashes(t
);
1262 e
= strchr(spec
, ':');
1264 if (!cg_controller_is_valid(spec
))
1281 t
= strndup(spec
, e
-spec
);
1284 if (!cg_controller_is_valid(t
)) {
1298 if (!path_is_normalized(u
) ||
1299 !path_is_absolute(u
)) {
1305 path_kill_slashes(u
);
1321 int cg_mangle_path(const char *path
, char **result
) {
1322 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1329 /* First, check if it already is a filesystem path */
1330 if (path_startswith(path
, "/sys/fs/cgroup")) {
1336 *result
= path_kill_slashes(t
);
1340 /* Otherwise, treat it as cg spec */
1341 r
= cg_split_spec(path
, &c
, &p
);
1345 return cg_get_path(c
?: SYSTEMD_CGROUP_CONTROLLER
, p
?: "/", NULL
, result
);
1348 int cg_get_root_path(char **path
) {
1354 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1358 e
= endswith(p
, "/" SPECIAL_INIT_SCOPE
);
1360 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
); /* legacy */
1362 e
= endswith(p
, "/system"); /* even more legacy */
1370 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1371 _cleanup_free_
char *rt
= NULL
;
1379 /* If the root was specified let's use that, otherwise
1380 * let's determine it from PID 1 */
1382 r
= cg_get_root_path(&rt
);
1389 p
= path_startswith(cgroup
, root
);
1390 if (p
&& p
> cgroup
)
1398 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1399 _cleanup_free_
char *raw
= NULL
;
1406 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1410 r
= cg_shift_path(raw
, root
, &c
);
1415 *cgroup
= TAKE_PTR(raw
);
1429 int cg_path_decode_unit(const char *cgroup
, char **unit
) {
1436 n
= strcspn(cgroup
, "/");
1440 c
= strndupa(cgroup
, n
);
1443 if (!unit_name_is_valid(c
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1454 static bool valid_slice_name(const char *p
, size_t n
) {
1459 if (n
< STRLEN("x.slice"))
1462 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1468 c
= cg_unescape(buf
);
1470 return unit_name_is_valid(c
, UNIT_NAME_PLAIN
);
1476 static const char *skip_slices(const char *p
) {
1479 /* Skips over all slice assignments */
1484 p
+= strspn(p
, "/");
1486 n
= strcspn(p
, "/");
1487 if (!valid_slice_name(p
, n
))
1494 int cg_path_get_unit(const char *path
, char **ret
) {
1502 e
= skip_slices(path
);
1504 r
= cg_path_decode_unit(e
, &unit
);
1508 /* We skipped over the slices, don't accept any now */
1509 if (endswith(unit
, ".slice")) {
1518 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1519 _cleanup_free_
char *cgroup
= NULL
;
1524 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1528 return cg_path_get_unit(cgroup
, unit
);
1532 * Skip session-*.scope, but require it to be there.
1534 static const char *skip_session(const char *p
) {
1540 p
+= strspn(p
, "/");
1542 n
= strcspn(p
, "/");
1543 if (n
< STRLEN("session-x.scope"))
1546 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1547 char buf
[n
- 8 - 6 + 1];
1549 memcpy(buf
, p
+ 8, n
- 8 - 6);
1552 /* Note that session scopes never need unescaping,
1553 * since they cannot conflict with the kernel's own
1554 * names, hence we don't need to call cg_unescape()
1557 if (!session_id_valid(buf
))
1561 p
+= strspn(p
, "/");
1569 * Skip user@*.service, but require it to be there.
1571 static const char *skip_user_manager(const char *p
) {
1577 p
+= strspn(p
, "/");
1579 n
= strcspn(p
, "/");
1580 if (n
< STRLEN("user@x.service"))
1583 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1584 char buf
[n
- 5 - 8 + 1];
1586 memcpy(buf
, p
+ 5, n
- 5 - 8);
1589 /* Note that user manager services never need unescaping,
1590 * since they cannot conflict with the kernel's own
1591 * names, hence we don't need to call cg_unescape()
1594 if (parse_uid(buf
, NULL
) < 0)
1598 p
+= strspn(p
, "/");
1606 static const char *skip_user_prefix(const char *path
) {
1611 /* Skip slices, if there are any */
1612 e
= skip_slices(path
);
1614 /* Skip the user manager, if it's in the path now... */
1615 t
= skip_user_manager(e
);
1619 /* Alternatively skip the user session if it is in the path... */
1620 return skip_session(e
);
1623 int cg_path_get_user_unit(const char *path
, char **ret
) {
1629 t
= skip_user_prefix(path
);
1633 /* And from here on it looks pretty much the same as for a
1634 * system unit, hence let's use the same parser from here
1636 return cg_path_get_unit(t
, ret
);
1639 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1640 _cleanup_free_
char *cgroup
= NULL
;
1645 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1649 return cg_path_get_user_unit(cgroup
, unit
);
1652 int cg_path_get_machine_name(const char *path
, char **machine
) {
1653 _cleanup_free_
char *u
= NULL
;
1657 r
= cg_path_get_unit(path
, &u
);
1661 sl
= strjoina("/run/systemd/machines/unit:", u
);
1662 return readlink_malloc(sl
, machine
);
1665 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1666 _cleanup_free_
char *cgroup
= NULL
;
1671 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1675 return cg_path_get_machine_name(cgroup
, machine
);
1678 int cg_path_get_session(const char *path
, char **session
) {
1679 _cleanup_free_
char *unit
= NULL
;
1685 r
= cg_path_get_unit(path
, &unit
);
1689 start
= startswith(unit
, "session-");
1692 end
= endswith(start
, ".scope");
1697 if (!session_id_valid(start
))
1713 int cg_pid_get_session(pid_t pid
, char **session
) {
1714 _cleanup_free_
char *cgroup
= NULL
;
1717 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1721 return cg_path_get_session(cgroup
, session
);
1724 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1725 _cleanup_free_
char *slice
= NULL
;
1731 r
= cg_path_get_slice(path
, &slice
);
1735 start
= startswith(slice
, "user-");
1738 end
= endswith(start
, ".slice");
1743 if (parse_uid(start
, uid
) < 0)
1749 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1750 _cleanup_free_
char *cgroup
= NULL
;
1753 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1757 return cg_path_get_owner_uid(cgroup
, uid
);
1760 int cg_path_get_slice(const char *p
, char **slice
) {
1761 const char *e
= NULL
;
1766 /* Finds the right-most slice unit from the beginning, but
1767 * stops before we come to the first non-slice unit. */
1772 p
+= strspn(p
, "/");
1774 n
= strcspn(p
, "/");
1775 if (!valid_slice_name(p
, n
)) {
1780 s
= strdup(SPECIAL_ROOT_SLICE
);
1788 return cg_path_decode_unit(e
, slice
);
1796 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1797 _cleanup_free_
char *cgroup
= NULL
;
1802 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1806 return cg_path_get_slice(cgroup
, slice
);
1809 int cg_path_get_user_slice(const char *p
, char **slice
) {
1814 t
= skip_user_prefix(p
);
1818 /* And now it looks pretty much the same as for a system
1819 * slice, so let's just use the same parser from here on. */
1820 return cg_path_get_slice(t
, slice
);
1823 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1824 _cleanup_free_
char *cgroup
= NULL
;
1829 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1833 return cg_path_get_user_slice(cgroup
, slice
);
1836 char *cg_escape(const char *p
) {
1837 bool need_prefix
= false;
1839 /* This implements very minimal escaping for names to be used
1840 * as file names in the cgroup tree: any name which might
1841 * conflict with a kernel name or is prefixed with '_' is
1842 * prefixed with a '_'. That way, when reading cgroup names it
1843 * is sufficient to remove a single prefixing underscore if
1846 /* The return value of this function (unlike cg_unescape())
1849 if (IN_SET(p
[0], 0, '_', '.') ||
1850 streq(p
, "notify_on_release") ||
1851 streq(p
, "release_agent") ||
1852 streq(p
, "tasks") ||
1853 startswith(p
, "cgroup."))
1858 dot
= strrchr(p
, '.');
1863 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
1866 n
= cgroup_controller_to_string(c
);
1871 if (memcmp(p
, n
, l
) != 0)
1881 return strappend("_", p
);
1886 char *cg_unescape(const char *p
) {
1889 /* The return value of this function (unlike cg_escape())
1890 * doesn't need free()! */
1898 #define CONTROLLER_VALID \
1902 bool cg_controller_is_valid(const char *p
) {
1908 if (streq(p
, SYSTEMD_CGROUP_CONTROLLER
))
1911 s
= startswith(p
, "name=");
1915 if (IN_SET(*p
, 0, '_'))
1918 for (t
= p
; *t
; t
++)
1919 if (!strchr(CONTROLLER_VALID
, *t
))
1922 if (t
- p
> FILENAME_MAX
)
1928 int cg_slice_to_path(const char *unit
, char **ret
) {
1929 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1936 if (streq(unit
, SPECIAL_ROOT_SLICE
)) {
1946 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1949 if (!endswith(unit
, ".slice"))
1952 r
= unit_name_to_prefix(unit
, &p
);
1956 dash
= strchr(p
, '-');
1958 /* Don't allow initial dashes */
1963 _cleanup_free_
char *escaped
= NULL
;
1964 char n
[dash
- p
+ sizeof(".slice")];
1966 #if HAS_FEATURE_MEMORY_SANITIZER
1967 /* msan doesn't instrument stpncpy, so it thinks
1968 * n is later used unitialized:
1969 * https://github.com/google/sanitizers/issues/926
1974 /* Don't allow trailing or double dashes */
1975 if (IN_SET(dash
[1], 0, '-'))
1978 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1979 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1982 escaped
= cg_escape(n
);
1986 if (!strextend(&s
, escaped
, "/", NULL
))
1989 dash
= strchr(dash
+1, '-');
1992 e
= cg_escape(unit
);
1996 if (!strextend(&s
, e
, NULL
))
2004 int cg_set_attribute(const char *controller
, const char *path
, const char *attribute
, const char *value
) {
2005 _cleanup_free_
char *p
= NULL
;
2008 r
= cg_get_path(controller
, path
, attribute
, &p
);
2012 return write_string_file(p
, value
, 0);
2015 int cg_get_attribute(const char *controller
, const char *path
, const char *attribute
, char **ret
) {
2016 _cleanup_free_
char *p
= NULL
;
2019 r
= cg_get_path(controller
, path
, attribute
, &p
);
2023 return read_one_line_file(p
, ret
);
2026 int cg_get_keyed_attribute(
2027 const char *controller
,
2029 const char *attribute
,
2031 char **ret_values
) {
2033 _cleanup_free_
char *filename
= NULL
, *contents
= NULL
;
2035 size_t n
, i
, n_done
= 0;
2039 /* Reads one or more fields of a cgroupsv2 keyed attribute file. The 'keys' parameter should be an strv with
2040 * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of
2041 * entries as 'keys'. On success each entry will be set to the value of the matching key.
2043 * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. */
2045 r
= cg_get_path(controller
, path
, attribute
, &filename
);
2049 r
= read_full_file(filename
, &contents
, NULL
);
2053 n
= strv_length(keys
);
2054 if (n
== 0) /* No keys to retrieve? That's easy, we are done then */
2057 /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */
2058 v
= newa0(char*, n
);
2060 for (p
= contents
; *p
;) {
2061 const char *w
= NULL
;
2063 for (i
= 0; i
< n
; i
++)
2065 w
= first_word(p
, keys
[i
]);
2073 l
= strcspn(w
, NEWLINE
);
2074 v
[i
] = strndup(w
, l
);
2086 p
+= strcspn(p
, NEWLINE
);
2088 p
+= strspn(p
, NEWLINE
);
2094 for (i
= 0; i
< n
; i
++)
2100 memcpy(ret_values
, v
, sizeof(char*) * n
);
2105 int cg_create_everywhere(CGroupMask supported
, CGroupMask mask
, const char *path
) {
2109 /* This one will create a cgroup in our private tree, but also
2110 * duplicate it in the trees specified in mask, and remove it
2113 /* First create the cgroup in our own hierarchy. */
2114 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
2118 /* If we are in the unified hierarchy, we are done now */
2119 r
= cg_all_unified();
2125 /* Otherwise, do the same in the other hierarchies */
2126 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2127 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2130 n
= cgroup_controller_to_string(c
);
2133 (void) cg_create(n
, path
);
2134 else if (supported
& bit
)
2135 (void) cg_trim(n
, path
, true);
2141 int cg_attach_everywhere(CGroupMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
2145 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
2149 r
= cg_all_unified();
2155 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2156 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2157 const char *p
= NULL
;
2159 if (!(supported
& bit
))
2163 p
= path_callback(bit
, userdata
);
2168 (void) cg_attach_fallback(cgroup_controller_to_string(c
), p
, pid
);
2174 int cg_attach_many_everywhere(CGroupMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
2179 SET_FOREACH(pidp
, pids
, i
) {
2180 pid_t pid
= PTR_TO_PID(pidp
);
2183 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
2184 if (q
< 0 && r
>= 0)
2191 int cg_migrate_everywhere(CGroupMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
2195 if (!path_equal(from
, to
)) {
2196 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, CGROUP_REMOVE
);
2201 q
= cg_all_unified();
2207 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2208 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2209 const char *p
= NULL
;
2211 if (!(supported
& bit
))
2215 p
= to_callback(bit
, userdata
);
2220 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, cgroup_controller_to_string(c
), p
, 0);
2226 int cg_trim_everywhere(CGroupMask supported
, const char *path
, bool delete_root
) {
2230 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
2234 q
= cg_all_unified();
2240 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2241 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2243 if (!(supported
& bit
))
2246 (void) cg_trim(cgroup_controller_to_string(c
), path
, delete_root
);
2252 int cg_mask_to_string(CGroupMask mask
, char **ret
) {
2253 _cleanup_free_
char *s
= NULL
;
2254 size_t n
= 0, allocated
= 0;
2265 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2269 if (!(mask
& CGROUP_CONTROLLER_TO_MASK(c
)))
2272 k
= cgroup_controller_to_string(c
);
2275 if (!GREEDY_REALLOC(s
, allocated
, n
+ space
+ l
+ 1))
2280 memcpy(s
+ n
+ space
, k
, l
);
2294 int cg_mask_from_string(const char *value
, CGroupMask
*mask
) {
2299 _cleanup_free_
char *n
= NULL
;
2303 r
= extract_first_word(&value
, &n
, NULL
, 0);
2309 v
= cgroup_controller_from_string(n
);
2313 *mask
|= CGROUP_CONTROLLER_TO_MASK(v
);
2318 int cg_mask_supported(CGroupMask
*ret
) {
2319 CGroupMask mask
= 0;
2322 /* Determines the mask of supported cgroup controllers. Only
2323 * includes controllers we can make sense of and that are
2324 * actually accessible. */
2326 r
= cg_all_unified();
2330 _cleanup_free_
char *root
= NULL
, *controllers
= NULL
, *path
= NULL
;
2332 /* In the unified hierarchy we can read the supported
2333 * and accessible controllers from a the top-level
2334 * cgroup attribute */
2336 r
= cg_get_root_path(&root
);
2340 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, root
, "cgroup.controllers", &path
);
2344 r
= read_one_line_file(path
, &controllers
);
2348 r
= cg_mask_from_string(controllers
, &mask
);
2352 /* Currently, we support the cpu, memory, io and pids
2353 * controller in the unified hierarchy, mask
2354 * everything else off. */
2355 mask
&= CGROUP_MASK_CPU
| CGROUP_MASK_MEMORY
| CGROUP_MASK_IO
| CGROUP_MASK_PIDS
;
2360 /* In the legacy hierarchy, we check whether which
2361 * hierarchies are mounted. */
2363 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2366 n
= cgroup_controller_to_string(c
);
2367 if (controller_is_accessible(n
) >= 0)
2368 mask
|= CGROUP_CONTROLLER_TO_MASK(c
);
2376 int cg_kernel_controllers(Set
**ret
) {
2377 _cleanup_set_free_free_ Set
*controllers
= NULL
;
2378 _cleanup_fclose_
FILE *f
= NULL
;
2383 /* Determines the full list of kernel-known controllers. Might
2384 * include controllers we don't actually support, arbitrary
2385 * named hierarchies and controllers that aren't currently
2386 * accessible (because not mounted). */
2388 controllers
= set_new(&string_hash_ops
);
2392 f
= fopen("/proc/cgroups", "re");
2394 if (errno
== ENOENT
) {
2402 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
2404 /* Ignore the header line */
2405 (void) read_line(f
, (size_t) -1, NULL
);
2412 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
2417 if (ferror(f
) && errno
> 0)
2428 if (!cg_controller_is_valid(controller
)) {
2433 r
= set_consume(controllers
, controller
);
2438 *ret
= TAKE_PTR(controllers
);
2443 static thread_local CGroupUnified unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2445 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2446 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2447 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2448 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2450 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2451 * process management but disable the compat dual layout, we return %true on
2452 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2454 static thread_local
bool unified_systemd_v232
;
2456 static int cg_unified_update(void) {
2460 /* Checks if we support the unified hierarchy. Returns an
2461 * error when the cgroup hierarchies aren't mounted yet or we
2462 * have any other trouble determining if the unified hierarchy
2465 if (unified_cache
>= CGROUP_UNIFIED_NONE
)
2468 if (statfs("/sys/fs/cgroup/", &fs
) < 0)
2469 return log_debug_errno(errno
, "statfs(\"/sys/fs/cgroup/\") failed: %m");
2471 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2472 log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
2473 unified_cache
= CGROUP_UNIFIED_ALL
;
2474 } else if (F_TYPE_EQUAL(fs
.f_type
, TMPFS_MAGIC
)) {
2475 if (statfs("/sys/fs/cgroup/unified/", &fs
) == 0 &&
2476 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2477 log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller");
2478 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2479 unified_systemd_v232
= false;
2481 if (statfs("/sys/fs/cgroup/systemd/", &fs
) < 0)
2482 return log_debug_errno(errno
, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m");
2484 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2485 log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)");
2486 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2487 unified_systemd_v232
= true;
2488 } else if (F_TYPE_EQUAL(fs
.f_type
, CGROUP_SUPER_MAGIC
)) {
2489 log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy");
2490 unified_cache
= CGROUP_UNIFIED_NONE
;
2492 log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy",
2493 (unsigned long long) fs
.f_type
);
2494 unified_cache
= CGROUP_UNIFIED_NONE
;
2498 log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2499 (unsigned long long) fs
.f_type
);
2506 int cg_unified_controller(const char *controller
) {
2509 r
= cg_unified_update();
2513 if (unified_cache
== CGROUP_UNIFIED_NONE
)
2516 if (unified_cache
>= CGROUP_UNIFIED_ALL
)
2519 return streq_ptr(controller
, SYSTEMD_CGROUP_CONTROLLER
);
2522 int cg_all_unified(void) {
2525 r
= cg_unified_update();
2529 return unified_cache
>= CGROUP_UNIFIED_ALL
;
2532 int cg_hybrid_unified(void) {
2535 r
= cg_unified_update();
2539 return unified_cache
== CGROUP_UNIFIED_SYSTEMD
&& !unified_systemd_v232
;
2542 int cg_unified_flush(void) {
2543 unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2545 return cg_unified_update();
2548 int cg_enable_everywhere(CGroupMask supported
, CGroupMask mask
, const char *p
) {
2549 _cleanup_fclose_
FILE *f
= NULL
;
2550 _cleanup_free_
char *fs
= NULL
;
2559 r
= cg_all_unified();
2562 if (r
== 0) /* on the legacy hiearchy there's no joining of controllers defined */
2565 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, p
, "cgroup.subtree_control", &fs
);
2569 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2570 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2573 if (!(supported
& bit
))
2576 n
= cgroup_controller_to_string(c
);
2578 char s
[1 + strlen(n
) + 1];
2580 s
[0] = mask
& bit
? '+' : '-';
2584 f
= fopen(fs
, "we");
2586 log_debug_errno(errno
, "Failed to open cgroup.subtree_control file of %s: %m", p
);
2591 r
= write_string_stream(f
, s
, 0);
2593 log_debug_errno(r
, "Failed to enable controller %s for %s (%s): %m", n
, p
, fs
);
2600 bool cg_is_unified_wanted(void) {
2601 static thread_local
int wanted
= -1;
2604 const bool is_default
= DEFAULT_HIERARCHY
== CGROUP_UNIFIED_ALL
;
2606 /* If we have a cached value, return that. */
2610 /* If the hierarchy is already mounted, then follow whatever
2611 * was chosen for it. */
2612 if (cg_unified_flush() >= 0)
2613 return (wanted
= unified_cache
>= CGROUP_UNIFIED_ALL
);
2615 /* Otherwise, let's see what the kernel command line has to say.
2616 * Since checking is expensive, cache a non-error result. */
2617 r
= proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b
);
2619 return (wanted
= r
> 0 ? b
: is_default
);
2622 bool cg_is_legacy_wanted(void) {
2623 static thread_local
int wanted
= -1;
2625 /* If we have a cached value, return that. */
2629 /* Check if we have cgroups2 already mounted. */
2630 if (cg_unified_flush() >= 0 &&
2631 unified_cache
== CGROUP_UNIFIED_ALL
)
2632 return (wanted
= false);
2634 /* Otherwise, assume that at least partial legacy is wanted,
2635 * since cgroups2 should already be mounted at this point. */
2636 return (wanted
= true);
2639 bool cg_is_hybrid_wanted(void) {
2640 static thread_local
int wanted
= -1;
2643 const bool is_default
= DEFAULT_HIERARCHY
>= CGROUP_UNIFIED_SYSTEMD
;
2644 /* We default to true if the default is "hybrid", obviously,
2645 * but also when the default is "unified", because if we get
2646 * called, it means that unified hierarchy was not mounted. */
2648 /* If we have a cached value, return that. */
2652 /* If the hierarchy is already mounted, then follow whatever
2653 * was chosen for it. */
2654 if (cg_unified_flush() >= 0 &&
2655 unified_cache
== CGROUP_UNIFIED_ALL
)
2656 return (wanted
= false);
2658 /* Otherwise, let's see what the kernel command line has to say.
2659 * Since checking is expensive, cache a non-error result. */
2660 r
= proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b
);
2662 /* The meaning of the kernel option is reversed wrt. to the return value
2663 * of this function, hence the negation. */
2664 return (wanted
= r
> 0 ? !b
: is_default
);
2667 int cg_weight_parse(const char *s
, uint64_t *ret
) {
2672 *ret
= CGROUP_WEIGHT_INVALID
;
2676 r
= safe_atou64(s
, &u
);
2680 if (u
< CGROUP_WEIGHT_MIN
|| u
> CGROUP_WEIGHT_MAX
)
2687 const uint64_t cgroup_io_limit_defaults
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2688 [CGROUP_IO_RBPS_MAX
] = CGROUP_LIMIT_MAX
,
2689 [CGROUP_IO_WBPS_MAX
] = CGROUP_LIMIT_MAX
,
2690 [CGROUP_IO_RIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2691 [CGROUP_IO_WIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2694 static const char* const cgroup_io_limit_type_table
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2695 [CGROUP_IO_RBPS_MAX
] = "IOReadBandwidthMax",
2696 [CGROUP_IO_WBPS_MAX
] = "IOWriteBandwidthMax",
2697 [CGROUP_IO_RIOPS_MAX
] = "IOReadIOPSMax",
2698 [CGROUP_IO_WIOPS_MAX
] = "IOWriteIOPSMax",
2701 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type
, CGroupIOLimitType
);
2703 int cg_cpu_shares_parse(const char *s
, uint64_t *ret
) {
2708 *ret
= CGROUP_CPU_SHARES_INVALID
;
2712 r
= safe_atou64(s
, &u
);
2716 if (u
< CGROUP_CPU_SHARES_MIN
|| u
> CGROUP_CPU_SHARES_MAX
)
2723 int cg_blkio_weight_parse(const char *s
, uint64_t *ret
) {
2728 *ret
= CGROUP_BLKIO_WEIGHT_INVALID
;
2732 r
= safe_atou64(s
, &u
);
2736 if (u
< CGROUP_BLKIO_WEIGHT_MIN
|| u
> CGROUP_BLKIO_WEIGHT_MAX
)
2743 bool is_cgroup_fs(const struct statfs
*s
) {
2744 return is_fs_type(s
, CGROUP_SUPER_MAGIC
) ||
2745 is_fs_type(s
, CGROUP2_SUPER_MAGIC
);
2748 bool fd_is_cgroup_fs(int fd
) {
2751 if (fstatfs(fd
, &s
) < 0)
2754 return is_cgroup_fs(&s
);
2757 static const char *cgroup_controller_table
[_CGROUP_CONTROLLER_MAX
] = {
2758 [CGROUP_CONTROLLER_CPU
] = "cpu",
2759 [CGROUP_CONTROLLER_CPUACCT
] = "cpuacct",
2760 [CGROUP_CONTROLLER_IO
] = "io",
2761 [CGROUP_CONTROLLER_BLKIO
] = "blkio",
2762 [CGROUP_CONTROLLER_MEMORY
] = "memory",
2763 [CGROUP_CONTROLLER_DEVICES
] = "devices",
2764 [CGROUP_CONTROLLER_PIDS
] = "pids",
2767 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller
, CGroupController
);