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
;
988 const char *fs
, *controller_str
;
996 if (!cg_controller_is_valid(controller
))
999 controller
= SYSTEMD_CGROUP_CONTROLLER
;
1001 unified
= cg_unified_controller(controller
);
1005 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
1006 controller_str
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
1008 controller_str
= controller
;
1010 cs
= strlen(controller_str
);
1013 fs
= procfs_file_alloca(pid
, "cgroup");
1014 f
= fopen(fs
, "re");
1016 return errno
== ENOENT
? -ESRCH
: -errno
;
1018 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1021 _cleanup_free_
char *line
= NULL
;
1024 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1031 e
= startswith(line
, "0:");
1041 const char *word
, *state
;
1044 l
= strchr(line
, ':');
1054 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
)
1055 if (k
== cs
&& memcmp(word
, controller_str
, cs
) == 0) {
1067 /* Truncate suffix indicating the process is a zombie */
1068 e
= endswith(p
, " (deleted)");
1079 int cg_install_release_agent(const char *controller
, const char *agent
) {
1080 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
1086 r
= cg_unified_controller(controller
);
1089 if (r
> 0) /* doesn't apply to unified hierarchy */
1092 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1096 r
= read_one_line_file(fs
, &contents
);
1100 sc
= strstrip(contents
);
1102 r
= write_string_file(fs
, agent
, 0);
1105 } else if (!path_equal(sc
, agent
))
1109 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1113 contents
= mfree(contents
);
1114 r
= read_one_line_file(fs
, &contents
);
1118 sc
= strstrip(contents
);
1119 if (streq(sc
, "0")) {
1120 r
= write_string_file(fs
, "1", 0);
1127 if (!streq(sc
, "1"))
1133 int cg_uninstall_release_agent(const char *controller
) {
1134 _cleanup_free_
char *fs
= NULL
;
1137 r
= cg_unified_controller(controller
);
1140 if (r
> 0) /* Doesn't apply to unified hierarchy */
1143 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1147 r
= write_string_file(fs
, "0", 0);
1153 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1157 r
= write_string_file(fs
, "", 0);
1164 int cg_is_empty(const char *controller
, const char *path
) {
1165 _cleanup_fclose_
FILE *f
= NULL
;
1171 r
= cg_enumerate_processes(controller
, path
, &f
);
1177 r
= cg_read_pid(f
, &pid
);
1184 int cg_is_empty_recursive(const char *controller
, const char *path
) {
1189 /* The root cgroup is always populated */
1190 if (controller
&& empty_or_root(path
))
1193 r
= cg_unified_controller(controller
);
1197 _cleanup_free_
char *t
= NULL
;
1199 /* On the unified hierarchy we can check empty state
1200 * via the "populated" attribute of "cgroup.events". */
1202 r
= cg_read_event(controller
, path
, "populated", &t
);
1208 return streq(t
, "0");
1210 _cleanup_closedir_
DIR *d
= NULL
;
1213 r
= cg_is_empty(controller
, path
);
1217 r
= cg_enumerate_subgroups(controller
, path
, &d
);
1223 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
1224 _cleanup_free_
char *p
= NULL
;
1226 p
= strjoin(path
, "/", fn
);
1231 r
= cg_is_empty_recursive(controller
, p
);
1242 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
1243 char *t
= NULL
, *u
= NULL
;
1249 if (!path_is_normalized(spec
))
1257 *path
= path_simplify(t
, false);
1266 e
= strchr(spec
, ':');
1268 if (!cg_controller_is_valid(spec
))
1285 t
= strndup(spec
, e
-spec
);
1288 if (!cg_controller_is_valid(t
)) {
1302 if (!path_is_normalized(u
) ||
1303 !path_is_absolute(u
)) {
1309 path_simplify(u
, false);
1325 int cg_mangle_path(const char *path
, char **result
) {
1326 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1333 /* First, check if it already is a filesystem path */
1334 if (path_startswith(path
, "/sys/fs/cgroup")) {
1340 *result
= path_simplify(t
, false);
1344 /* Otherwise, treat it as cg spec */
1345 r
= cg_split_spec(path
, &c
, &p
);
1349 return cg_get_path(c
?: SYSTEMD_CGROUP_CONTROLLER
, p
?: "/", NULL
, result
);
1352 int cg_get_root_path(char **path
) {
1358 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1362 e
= endswith(p
, "/" SPECIAL_INIT_SCOPE
);
1364 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
); /* legacy */
1366 e
= endswith(p
, "/system"); /* even more legacy */
1374 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1375 _cleanup_free_
char *rt
= NULL
;
1383 /* If the root was specified let's use that, otherwise
1384 * let's determine it from PID 1 */
1386 r
= cg_get_root_path(&rt
);
1393 p
= path_startswith(cgroup
, root
);
1394 if (p
&& p
> cgroup
)
1402 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1403 _cleanup_free_
char *raw
= NULL
;
1410 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1414 r
= cg_shift_path(raw
, root
, &c
);
1419 *cgroup
= TAKE_PTR(raw
);
1433 int cg_path_decode_unit(const char *cgroup
, char **unit
) {
1440 n
= strcspn(cgroup
, "/");
1444 c
= strndupa(cgroup
, n
);
1447 if (!unit_name_is_valid(c
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1458 static bool valid_slice_name(const char *p
, size_t n
) {
1463 if (n
< STRLEN("x.slice"))
1466 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1472 c
= cg_unescape(buf
);
1474 return unit_name_is_valid(c
, UNIT_NAME_PLAIN
);
1480 static const char *skip_slices(const char *p
) {
1483 /* Skips over all slice assignments */
1488 p
+= strspn(p
, "/");
1490 n
= strcspn(p
, "/");
1491 if (!valid_slice_name(p
, n
))
1498 int cg_path_get_unit(const char *path
, char **ret
) {
1506 e
= skip_slices(path
);
1508 r
= cg_path_decode_unit(e
, &unit
);
1512 /* We skipped over the slices, don't accept any now */
1513 if (endswith(unit
, ".slice")) {
1522 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1523 _cleanup_free_
char *cgroup
= NULL
;
1528 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1532 return cg_path_get_unit(cgroup
, unit
);
1536 * Skip session-*.scope, but require it to be there.
1538 static const char *skip_session(const char *p
) {
1544 p
+= strspn(p
, "/");
1546 n
= strcspn(p
, "/");
1547 if (n
< STRLEN("session-x.scope"))
1550 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1551 char buf
[n
- 8 - 6 + 1];
1553 memcpy(buf
, p
+ 8, n
- 8 - 6);
1556 /* Note that session scopes never need unescaping,
1557 * since they cannot conflict with the kernel's own
1558 * names, hence we don't need to call cg_unescape()
1561 if (!session_id_valid(buf
))
1565 p
+= strspn(p
, "/");
1573 * Skip user@*.service, but require it to be there.
1575 static const char *skip_user_manager(const char *p
) {
1581 p
+= strspn(p
, "/");
1583 n
= strcspn(p
, "/");
1584 if (n
< STRLEN("user@x.service"))
1587 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1588 char buf
[n
- 5 - 8 + 1];
1590 memcpy(buf
, p
+ 5, n
- 5 - 8);
1593 /* Note that user manager services never need unescaping,
1594 * since they cannot conflict with the kernel's own
1595 * names, hence we don't need to call cg_unescape()
1598 if (parse_uid(buf
, NULL
) < 0)
1602 p
+= strspn(p
, "/");
1610 static const char *skip_user_prefix(const char *path
) {
1615 /* Skip slices, if there are any */
1616 e
= skip_slices(path
);
1618 /* Skip the user manager, if it's in the path now... */
1619 t
= skip_user_manager(e
);
1623 /* Alternatively skip the user session if it is in the path... */
1624 return skip_session(e
);
1627 int cg_path_get_user_unit(const char *path
, char **ret
) {
1633 t
= skip_user_prefix(path
);
1637 /* And from here on it looks pretty much the same as for a
1638 * system unit, hence let's use the same parser from here
1640 return cg_path_get_unit(t
, ret
);
1643 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1644 _cleanup_free_
char *cgroup
= NULL
;
1649 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1653 return cg_path_get_user_unit(cgroup
, unit
);
1656 int cg_path_get_machine_name(const char *path
, char **machine
) {
1657 _cleanup_free_
char *u
= NULL
;
1661 r
= cg_path_get_unit(path
, &u
);
1665 sl
= strjoina("/run/systemd/machines/unit:", u
);
1666 return readlink_malloc(sl
, machine
);
1669 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1670 _cleanup_free_
char *cgroup
= NULL
;
1675 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1679 return cg_path_get_machine_name(cgroup
, machine
);
1682 int cg_path_get_session(const char *path
, char **session
) {
1683 _cleanup_free_
char *unit
= NULL
;
1689 r
= cg_path_get_unit(path
, &unit
);
1693 start
= startswith(unit
, "session-");
1696 end
= endswith(start
, ".scope");
1701 if (!session_id_valid(start
))
1717 int cg_pid_get_session(pid_t pid
, char **session
) {
1718 _cleanup_free_
char *cgroup
= NULL
;
1721 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1725 return cg_path_get_session(cgroup
, session
);
1728 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1729 _cleanup_free_
char *slice
= NULL
;
1735 r
= cg_path_get_slice(path
, &slice
);
1739 start
= startswith(slice
, "user-");
1742 end
= endswith(start
, ".slice");
1747 if (parse_uid(start
, uid
) < 0)
1753 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1754 _cleanup_free_
char *cgroup
= NULL
;
1757 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1761 return cg_path_get_owner_uid(cgroup
, uid
);
1764 int cg_path_get_slice(const char *p
, char **slice
) {
1765 const char *e
= NULL
;
1770 /* Finds the right-most slice unit from the beginning, but
1771 * stops before we come to the first non-slice unit. */
1776 p
+= strspn(p
, "/");
1778 n
= strcspn(p
, "/");
1779 if (!valid_slice_name(p
, n
)) {
1784 s
= strdup(SPECIAL_ROOT_SLICE
);
1792 return cg_path_decode_unit(e
, slice
);
1800 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1801 _cleanup_free_
char *cgroup
= NULL
;
1806 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1810 return cg_path_get_slice(cgroup
, slice
);
1813 int cg_path_get_user_slice(const char *p
, char **slice
) {
1818 t
= skip_user_prefix(p
);
1822 /* And now it looks pretty much the same as for a system
1823 * slice, so let's just use the same parser from here on. */
1824 return cg_path_get_slice(t
, slice
);
1827 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1828 _cleanup_free_
char *cgroup
= NULL
;
1833 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1837 return cg_path_get_user_slice(cgroup
, slice
);
1840 char *cg_escape(const char *p
) {
1841 bool need_prefix
= false;
1843 /* This implements very minimal escaping for names to be used
1844 * as file names in the cgroup tree: any name which might
1845 * conflict with a kernel name or is prefixed with '_' is
1846 * prefixed with a '_'. That way, when reading cgroup names it
1847 * is sufficient to remove a single prefixing underscore if
1850 /* The return value of this function (unlike cg_unescape())
1853 if (IN_SET(p
[0], 0, '_', '.') ||
1854 streq(p
, "notify_on_release") ||
1855 streq(p
, "release_agent") ||
1856 streq(p
, "tasks") ||
1857 startswith(p
, "cgroup."))
1862 dot
= strrchr(p
, '.');
1867 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
1870 n
= cgroup_controller_to_string(c
);
1875 if (memcmp(p
, n
, l
) != 0)
1885 return strappend("_", p
);
1890 char *cg_unescape(const char *p
) {
1893 /* The return value of this function (unlike cg_escape())
1894 * doesn't need free()! */
1902 #define CONTROLLER_VALID \
1906 bool cg_controller_is_valid(const char *p
) {
1912 if (streq(p
, SYSTEMD_CGROUP_CONTROLLER
))
1915 s
= startswith(p
, "name=");
1919 if (IN_SET(*p
, 0, '_'))
1922 for (t
= p
; *t
; t
++)
1923 if (!strchr(CONTROLLER_VALID
, *t
))
1926 if (t
- p
> FILENAME_MAX
)
1932 int cg_slice_to_path(const char *unit
, char **ret
) {
1933 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1940 if (streq(unit
, SPECIAL_ROOT_SLICE
)) {
1950 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1953 if (!endswith(unit
, ".slice"))
1956 r
= unit_name_to_prefix(unit
, &p
);
1960 dash
= strchr(p
, '-');
1962 /* Don't allow initial dashes */
1967 _cleanup_free_
char *escaped
= NULL
;
1968 char n
[dash
- p
+ sizeof(".slice")];
1970 #if HAS_FEATURE_MEMORY_SANITIZER
1971 /* msan doesn't instrument stpncpy, so it thinks
1972 * n is later used unitialized:
1973 * https://github.com/google/sanitizers/issues/926
1978 /* Don't allow trailing or double dashes */
1979 if (IN_SET(dash
[1], 0, '-'))
1982 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1983 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1986 escaped
= cg_escape(n
);
1990 if (!strextend(&s
, escaped
, "/", NULL
))
1993 dash
= strchr(dash
+1, '-');
1996 e
= cg_escape(unit
);
2000 if (!strextend(&s
, e
, NULL
))
2008 int cg_set_attribute(const char *controller
, const char *path
, const char *attribute
, const char *value
) {
2009 _cleanup_free_
char *p
= NULL
;
2012 r
= cg_get_path(controller
, path
, attribute
, &p
);
2016 return write_string_file(p
, value
, 0);
2019 int cg_get_attribute(const char *controller
, const char *path
, const char *attribute
, char **ret
) {
2020 _cleanup_free_
char *p
= NULL
;
2023 r
= cg_get_path(controller
, path
, attribute
, &p
);
2027 return read_one_line_file(p
, ret
);
2030 int cg_get_keyed_attribute(
2031 const char *controller
,
2033 const char *attribute
,
2035 char **ret_values
) {
2037 _cleanup_free_
char *filename
= NULL
, *contents
= NULL
;
2039 size_t n
, i
, n_done
= 0;
2043 /* Reads one or more fields of a cgroupsv2 keyed attribute file. The 'keys' parameter should be an strv with
2044 * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of
2045 * entries as 'keys'. On success each entry will be set to the value of the matching key.
2047 * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. */
2049 r
= cg_get_path(controller
, path
, attribute
, &filename
);
2053 r
= read_full_file(filename
, &contents
, NULL
);
2057 n
= strv_length(keys
);
2058 if (n
== 0) /* No keys to retrieve? That's easy, we are done then */
2061 /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */
2062 v
= newa0(char*, n
);
2064 for (p
= contents
; *p
;) {
2065 const char *w
= NULL
;
2067 for (i
= 0; i
< n
; i
++)
2069 w
= first_word(p
, keys
[i
]);
2077 l
= strcspn(w
, NEWLINE
);
2078 v
[i
] = strndup(w
, l
);
2090 p
+= strcspn(p
, NEWLINE
);
2092 p
+= strspn(p
, NEWLINE
);
2098 for (i
= 0; i
< n
; i
++)
2104 memcpy(ret_values
, v
, sizeof(char*) * n
);
2109 int cg_create_everywhere(CGroupMask supported
, CGroupMask mask
, const char *path
) {
2114 /* This one will create a cgroup in our private tree, but also
2115 * duplicate it in the trees specified in mask, and remove it
2118 * Returns 0 if the group already existed in the systemd hierarchy,
2119 * 1 on success, negative otherwise.
2122 /* First create the cgroup in our own hierarchy. */
2123 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
2128 /* If we are in the unified hierarchy, we are done now */
2129 r
= cg_all_unified();
2135 /* Otherwise, do the same in the other hierarchies */
2136 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2137 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2140 n
= cgroup_controller_to_string(c
);
2143 (void) cg_create(n
, path
);
2144 else if (supported
& bit
)
2145 (void) cg_trim(n
, path
, true);
2151 int cg_attach_everywhere(CGroupMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
2155 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
2159 r
= cg_all_unified();
2165 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2166 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2167 const char *p
= NULL
;
2169 if (!(supported
& bit
))
2173 p
= path_callback(bit
, userdata
);
2178 (void) cg_attach_fallback(cgroup_controller_to_string(c
), p
, pid
);
2184 int cg_attach_many_everywhere(CGroupMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
2189 SET_FOREACH(pidp
, pids
, i
) {
2190 pid_t pid
= PTR_TO_PID(pidp
);
2193 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
2194 if (q
< 0 && r
>= 0)
2201 int cg_migrate_everywhere(CGroupMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
2205 if (!path_equal(from
, to
)) {
2206 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, CGROUP_REMOVE
);
2211 q
= cg_all_unified();
2217 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2218 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2219 const char *p
= NULL
;
2221 if (!(supported
& bit
))
2225 p
= to_callback(bit
, userdata
);
2230 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, cgroup_controller_to_string(c
), p
, 0);
2236 int cg_trim_everywhere(CGroupMask supported
, const char *path
, bool delete_root
) {
2240 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
2244 q
= cg_all_unified();
2250 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2251 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2253 if (!(supported
& bit
))
2256 (void) cg_trim(cgroup_controller_to_string(c
), path
, delete_root
);
2262 int cg_mask_to_string(CGroupMask mask
, char **ret
) {
2263 _cleanup_free_
char *s
= NULL
;
2264 size_t n
= 0, allocated
= 0;
2275 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2279 if (!(mask
& CGROUP_CONTROLLER_TO_MASK(c
)))
2282 k
= cgroup_controller_to_string(c
);
2285 if (!GREEDY_REALLOC(s
, allocated
, n
+ space
+ l
+ 1))
2290 memcpy(s
+ n
+ space
, k
, l
);
2304 int cg_mask_from_string(const char *value
, CGroupMask
*mask
) {
2309 _cleanup_free_
char *n
= NULL
;
2313 r
= extract_first_word(&value
, &n
, NULL
, 0);
2319 v
= cgroup_controller_from_string(n
);
2323 *mask
|= CGROUP_CONTROLLER_TO_MASK(v
);
2328 int cg_mask_supported(CGroupMask
*ret
) {
2329 CGroupMask mask
= 0;
2332 /* Determines the mask of supported cgroup controllers. Only
2333 * includes controllers we can make sense of and that are
2334 * actually accessible. */
2336 r
= cg_all_unified();
2340 _cleanup_free_
char *root
= NULL
, *controllers
= NULL
, *path
= NULL
;
2342 /* In the unified hierarchy we can read the supported
2343 * and accessible controllers from a the top-level
2344 * cgroup attribute */
2346 r
= cg_get_root_path(&root
);
2350 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, root
, "cgroup.controllers", &path
);
2354 r
= read_one_line_file(path
, &controllers
);
2358 r
= cg_mask_from_string(controllers
, &mask
);
2362 /* Currently, we support the cpu, memory, io and pids
2363 * controller in the unified hierarchy, mask
2364 * everything else off. */
2365 mask
&= CGROUP_MASK_CPU
| CGROUP_MASK_MEMORY
| CGROUP_MASK_IO
| CGROUP_MASK_PIDS
;
2370 /* In the legacy hierarchy, we check whether which
2371 * hierarchies are mounted. */
2373 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2376 n
= cgroup_controller_to_string(c
);
2377 if (controller_is_accessible(n
) >= 0)
2378 mask
|= CGROUP_CONTROLLER_TO_MASK(c
);
2386 int cg_kernel_controllers(Set
**ret
) {
2387 _cleanup_set_free_free_ Set
*controllers
= NULL
;
2388 _cleanup_fclose_
FILE *f
= NULL
;
2393 /* Determines the full list of kernel-known controllers. Might include controllers we don't actually support
2394 * and controllers that aren't currently accessible (because not mounted). This does not include "name="
2395 * pseudo-controllers. */
2397 controllers
= set_new(&string_hash_ops
);
2401 f
= fopen("/proc/cgroups", "re");
2403 if (errno
== ENOENT
) {
2411 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
2413 /* Ignore the header line */
2414 (void) read_line(f
, (size_t) -1, NULL
);
2421 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
2426 if (ferror(f
) && errno
> 0)
2437 if (!cg_controller_is_valid(controller
)) {
2442 r
= set_consume(controllers
, controller
);
2447 *ret
= TAKE_PTR(controllers
);
2452 static thread_local CGroupUnified unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2454 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2455 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2456 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2457 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2459 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2460 * process management but disable the compat dual layout, we return %true on
2461 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2463 static thread_local
bool unified_systemd_v232
;
2465 static int cg_unified_update(void) {
2469 /* Checks if we support the unified hierarchy. Returns an
2470 * error when the cgroup hierarchies aren't mounted yet or we
2471 * have any other trouble determining if the unified hierarchy
2474 if (unified_cache
>= CGROUP_UNIFIED_NONE
)
2477 if (statfs("/sys/fs/cgroup/", &fs
) < 0)
2478 return log_debug_errno(errno
, "statfs(\"/sys/fs/cgroup/\") failed: %m");
2480 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2481 log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
2482 unified_cache
= CGROUP_UNIFIED_ALL
;
2483 } else if (F_TYPE_EQUAL(fs
.f_type
, TMPFS_MAGIC
)) {
2484 if (statfs("/sys/fs/cgroup/unified/", &fs
) == 0 &&
2485 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2486 log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller");
2487 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2488 unified_systemd_v232
= false;
2490 if (statfs("/sys/fs/cgroup/systemd/", &fs
) < 0)
2491 return log_debug_errno(errno
, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m");
2493 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2494 log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)");
2495 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2496 unified_systemd_v232
= true;
2497 } else if (F_TYPE_EQUAL(fs
.f_type
, CGROUP_SUPER_MAGIC
)) {
2498 log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy");
2499 unified_cache
= CGROUP_UNIFIED_NONE
;
2501 log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy",
2502 (unsigned long long) fs
.f_type
);
2503 unified_cache
= CGROUP_UNIFIED_NONE
;
2507 log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2508 (unsigned long long) fs
.f_type
);
2515 int cg_unified_controller(const char *controller
) {
2518 r
= cg_unified_update();
2522 if (unified_cache
== CGROUP_UNIFIED_NONE
)
2525 if (unified_cache
>= CGROUP_UNIFIED_ALL
)
2528 return streq_ptr(controller
, SYSTEMD_CGROUP_CONTROLLER
);
2531 int cg_all_unified(void) {
2534 r
= cg_unified_update();
2538 return unified_cache
>= CGROUP_UNIFIED_ALL
;
2541 int cg_hybrid_unified(void) {
2544 r
= cg_unified_update();
2548 return unified_cache
== CGROUP_UNIFIED_SYSTEMD
&& !unified_systemd_v232
;
2551 int cg_unified_flush(void) {
2552 unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2554 return cg_unified_update();
2557 int cg_enable_everywhere(CGroupMask supported
, CGroupMask mask
, const char *p
) {
2558 _cleanup_fclose_
FILE *f
= NULL
;
2559 _cleanup_free_
char *fs
= NULL
;
2568 r
= cg_all_unified();
2571 if (r
== 0) /* on the legacy hiearchy there's no joining of controllers defined */
2574 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, p
, "cgroup.subtree_control", &fs
);
2578 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2579 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2582 if (!(supported
& bit
))
2585 n
= cgroup_controller_to_string(c
);
2587 char s
[1 + strlen(n
) + 1];
2589 s
[0] = mask
& bit
? '+' : '-';
2593 f
= fopen(fs
, "we");
2595 log_debug_errno(errno
, "Failed to open cgroup.subtree_control file of %s: %m", p
);
2600 r
= write_string_stream(f
, s
, 0);
2602 log_debug_errno(r
, "Failed to enable controller %s for %s (%s): %m", n
, p
, fs
);
2611 bool cg_is_unified_wanted(void) {
2612 static thread_local
int wanted
= -1;
2615 const bool is_default
= DEFAULT_HIERARCHY
== CGROUP_UNIFIED_ALL
;
2617 /* If we have a cached value, return that. */
2621 /* If the hierarchy is already mounted, then follow whatever
2622 * was chosen for it. */
2623 if (cg_unified_flush() >= 0)
2624 return (wanted
= unified_cache
>= CGROUP_UNIFIED_ALL
);
2626 /* Otherwise, let's see what the kernel command line has to say.
2627 * Since checking is expensive, cache a non-error result. */
2628 r
= proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b
);
2630 return (wanted
= r
> 0 ? b
: is_default
);
2633 bool cg_is_legacy_wanted(void) {
2634 static thread_local
int wanted
= -1;
2636 /* If we have a cached value, return that. */
2640 /* Check if we have cgroups2 already mounted. */
2641 if (cg_unified_flush() >= 0 &&
2642 unified_cache
== CGROUP_UNIFIED_ALL
)
2643 return (wanted
= false);
2645 /* Otherwise, assume that at least partial legacy is wanted,
2646 * since cgroups2 should already be mounted at this point. */
2647 return (wanted
= true);
2650 bool cg_is_hybrid_wanted(void) {
2651 static thread_local
int wanted
= -1;
2654 const bool is_default
= DEFAULT_HIERARCHY
>= CGROUP_UNIFIED_SYSTEMD
;
2655 /* We default to true if the default is "hybrid", obviously,
2656 * but also when the default is "unified", because if we get
2657 * called, it means that unified hierarchy was not mounted. */
2659 /* If we have a cached value, return that. */
2663 /* If the hierarchy is already mounted, then follow whatever
2664 * was chosen for it. */
2665 if (cg_unified_flush() >= 0 &&
2666 unified_cache
== CGROUP_UNIFIED_ALL
)
2667 return (wanted
= false);
2669 /* Otherwise, let's see what the kernel command line has to say.
2670 * Since checking is expensive, cache a non-error result. */
2671 r
= proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b
);
2673 /* The meaning of the kernel option is reversed wrt. to the return value
2674 * of this function, hence the negation. */
2675 return (wanted
= r
> 0 ? !b
: is_default
);
2678 int cg_weight_parse(const char *s
, uint64_t *ret
) {
2683 *ret
= CGROUP_WEIGHT_INVALID
;
2687 r
= safe_atou64(s
, &u
);
2691 if (u
< CGROUP_WEIGHT_MIN
|| u
> CGROUP_WEIGHT_MAX
)
2698 const uint64_t cgroup_io_limit_defaults
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2699 [CGROUP_IO_RBPS_MAX
] = CGROUP_LIMIT_MAX
,
2700 [CGROUP_IO_WBPS_MAX
] = CGROUP_LIMIT_MAX
,
2701 [CGROUP_IO_RIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2702 [CGROUP_IO_WIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2705 static const char* const cgroup_io_limit_type_table
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2706 [CGROUP_IO_RBPS_MAX
] = "IOReadBandwidthMax",
2707 [CGROUP_IO_WBPS_MAX
] = "IOWriteBandwidthMax",
2708 [CGROUP_IO_RIOPS_MAX
] = "IOReadIOPSMax",
2709 [CGROUP_IO_WIOPS_MAX
] = "IOWriteIOPSMax",
2712 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type
, CGroupIOLimitType
);
2714 int cg_cpu_shares_parse(const char *s
, uint64_t *ret
) {
2719 *ret
= CGROUP_CPU_SHARES_INVALID
;
2723 r
= safe_atou64(s
, &u
);
2727 if (u
< CGROUP_CPU_SHARES_MIN
|| u
> CGROUP_CPU_SHARES_MAX
)
2734 int cg_blkio_weight_parse(const char *s
, uint64_t *ret
) {
2739 *ret
= CGROUP_BLKIO_WEIGHT_INVALID
;
2743 r
= safe_atou64(s
, &u
);
2747 if (u
< CGROUP_BLKIO_WEIGHT_MIN
|| u
> CGROUP_BLKIO_WEIGHT_MAX
)
2754 bool is_cgroup_fs(const struct statfs
*s
) {
2755 return is_fs_type(s
, CGROUP_SUPER_MAGIC
) ||
2756 is_fs_type(s
, CGROUP2_SUPER_MAGIC
);
2759 bool fd_is_cgroup_fs(int fd
) {
2762 if (fstatfs(fd
, &s
) < 0)
2765 return is_cgroup_fs(&s
);
2768 static const char *cgroup_controller_table
[_CGROUP_CONTROLLER_MAX
] = {
2769 [CGROUP_CONTROLLER_CPU
] = "cpu",
2770 [CGROUP_CONTROLLER_CPUACCT
] = "cpuacct",
2771 [CGROUP_CONTROLLER_IO
] = "io",
2772 [CGROUP_CONTROLLER_BLKIO
] = "blkio",
2773 [CGROUP_CONTROLLER_MEMORY
] = "memory",
2774 [CGROUP_CONTROLLER_DEVICES
] = "devices",
2775 [CGROUP_CONTROLLER_PIDS
] = "pids",
2776 [CGROUP_CONTROLLER_BPF_FIREWALL
] = "bpf-firewall",
2777 [CGROUP_CONTROLLER_BPF_DEVICES
] = "bpf-devices",
2780 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller
, CGroupController
);