2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/statfs.h>
30 #include <sys/types.h>
31 #include <sys/xattr.h>
34 #include "alloc-util.h"
35 #include "cgroup-util.h"
37 #include "dirent-util.h"
38 #include "extract-word.h"
41 #include "format-util.h"
44 #include "login-util.h"
48 #include "parse-util.h"
49 #include "path-util.h"
50 #include "proc-cmdline.h"
51 #include "process-util.h"
54 #include "stat-util.h"
55 #include "stdio-util.h"
56 #include "string-table.h"
57 #include "string-util.h"
58 #include "unit-name.h"
59 #include "user-util.h"
61 int cg_enumerate_processes(const char *controller
, const char *path
, FILE **_f
) {
62 _cleanup_free_
char *fs
= NULL
;
68 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
80 int cg_read_pid(FILE *f
, pid_t
*_pid
) {
83 /* Note that the cgroup.procs might contain duplicates! See
84 * cgroups.txt for details. */
90 if (fscanf(f
, "%lu", &ul
) != 1) {
95 return errno
> 0 ? -errno
: -EIO
;
105 int cg_read_event(const char *controller
, const char *path
, const char *event
,
108 _cleanup_free_
char *events
= NULL
, *content
= NULL
;
112 r
= cg_get_path(controller
, path
, "cgroup.events", &events
);
116 r
= read_full_file(events
, &content
, NULL
);
121 while ((line
= strsep(&p
, "\n"))) {
124 key
= strsep(&line
, " ");
128 if (strcmp(key
, event
))
138 bool cg_ns_supported(void) {
139 static thread_local
int enabled
= -1;
144 if (access("/proc/self/ns/cgroup", F_OK
) == 0)
152 int cg_enumerate_subgroups(const char *controller
, const char *path
, DIR **_d
) {
153 _cleanup_free_
char *fs
= NULL
;
159 /* This is not recursive! */
161 r
= cg_get_path(controller
, path
, NULL
, &fs
);
173 int cg_read_subgroup(DIR *d
, char **fn
) {
179 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
182 if (de
->d_type
!= DT_DIR
)
185 if (dot_or_dot_dot(de
->d_name
))
188 b
= strdup(de
->d_name
);
199 int cg_rmdir(const char *controller
, const char *path
) {
200 _cleanup_free_
char *p
= NULL
;
203 r
= cg_get_path(controller
, path
, NULL
, &p
);
208 if (r
< 0 && errno
!= ENOENT
)
211 r
= cg_hybrid_unified();
217 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
218 r
= cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
220 log_warning_errno(r
, "Failed to remove compat systemd cgroup %s: %m", path
);
227 const char *controller
,
232 cg_kill_log_func_t log_kill
,
235 _cleanup_set_free_ Set
*allocated_set
= NULL
;
242 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
243 * SIGCONT on SIGKILL. */
244 if (IN_SET(sig
, SIGCONT
, SIGKILL
))
245 flags
&= ~CGROUP_SIGCONT
;
247 /* This goes through the tasks list and kills them all. This
248 * is repeated until no further processes are added to the
249 * tasks list, to properly handle forking processes */
252 s
= allocated_set
= set_new(NULL
);
260 _cleanup_fclose_
FILE *f
= NULL
;
264 r
= cg_enumerate_processes(controller
, path
, &f
);
266 if (ret
>= 0 && r
!= -ENOENT
)
272 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
274 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
277 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
281 log_kill(pid
, sig
, userdata
);
283 /* If we haven't killed this process yet, kill
285 if (kill(pid
, sig
) < 0) {
286 if (ret
>= 0 && errno
!= ESRCH
)
289 if (flags
& CGROUP_SIGCONT
)
290 (void) kill(pid
, SIGCONT
);
298 r
= set_put(s
, PID_TO_PTR(pid
));
314 /* To avoid racing against processes which fork
315 * quicker than we can kill them we repeat this until
316 * no new pids need to be killed. */
323 int cg_kill_recursive(
324 const char *controller
,
329 cg_kill_log_func_t log_kill
,
332 _cleanup_set_free_ Set
*allocated_set
= NULL
;
333 _cleanup_closedir_
DIR *d
= NULL
;
341 s
= allocated_set
= set_new(NULL
);
346 ret
= cg_kill(controller
, path
, sig
, flags
, s
, log_kill
, userdata
);
348 r
= cg_enumerate_subgroups(controller
, path
, &d
);
350 if (ret
>= 0 && r
!= -ENOENT
)
356 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
357 _cleanup_free_
char *p
= NULL
;
359 p
= strjoin(path
, "/", fn
);
364 r
= cg_kill_recursive(controller
, p
, sig
, flags
, s
, log_kill
, userdata
);
365 if (r
!= 0 && ret
>= 0)
368 if (ret
>= 0 && r
< 0)
371 if (flags
& CGROUP_REMOVE
) {
372 r
= cg_rmdir(controller
, path
);
373 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
388 _cleanup_set_free_ Set
*s
= NULL
;
404 _cleanup_fclose_
FILE *f
= NULL
;
408 r
= cg_enumerate_processes(cfrom
, pfrom
, &f
);
410 if (ret
>= 0 && r
!= -ENOENT
)
416 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
418 /* This might do weird stuff if we aren't a
419 * single-threaded program. However, we
420 * luckily know we are not */
421 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
424 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
427 /* Ignore kernel threads. Since they can only
428 * exist in the root cgroup, we only check for
431 (isempty(pfrom
) || path_equal(pfrom
, "/")) &&
432 is_kernel_thread(pid
) > 0)
435 r
= cg_attach(cto
, pto
, pid
);
437 if (ret
>= 0 && r
!= -ESRCH
)
444 r
= set_put(s
, PID_TO_PTR(pid
));
464 int cg_migrate_recursive(
471 _cleanup_closedir_
DIR *d
= NULL
;
480 ret
= cg_migrate(cfrom
, pfrom
, cto
, pto
, flags
);
482 r
= cg_enumerate_subgroups(cfrom
, pfrom
, &d
);
484 if (ret
>= 0 && r
!= -ENOENT
)
490 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
491 _cleanup_free_
char *p
= NULL
;
493 p
= strjoin(pfrom
, "/", fn
);
498 r
= cg_migrate_recursive(cfrom
, p
, cto
, pto
, flags
);
499 if (r
!= 0 && ret
>= 0)
503 if (r
< 0 && ret
>= 0)
506 if (flags
& CGROUP_REMOVE
) {
507 r
= cg_rmdir(cfrom
, pfrom
);
508 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
515 int cg_migrate_recursive_fallback(
529 r
= cg_migrate_recursive(cfrom
, pfrom
, cto
, pto
, flags
);
531 char prefix
[strlen(pto
) + 1];
533 /* This didn't work? Then let's try all prefixes of the destination */
535 PATH_FOREACH_PREFIX(prefix
, pto
) {
538 q
= cg_migrate_recursive(cfrom
, pfrom
, cto
, prefix
, flags
);
547 static const char *controller_to_dirname(const char *controller
) {
552 /* Converts a controller name to the directory name below
553 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
554 * just cuts off the name= prefixed used for named
555 * hierarchies, if it is specified. */
557 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
558 if (cg_hybrid_unified() > 0)
559 controller
= SYSTEMD_CGROUP_CONTROLLER_HYBRID
;
561 controller
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
564 e
= startswith(controller
, "name=");
571 static int join_path_legacy(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
578 dn
= controller_to_dirname(controller
);
580 if (isempty(path
) && isempty(suffix
))
581 t
= strappend("/sys/fs/cgroup/", dn
);
582 else if (isempty(path
))
583 t
= strjoin("/sys/fs/cgroup/", dn
, "/", suffix
);
584 else if (isempty(suffix
))
585 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
);
587 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
, "/", suffix
);
595 static int join_path_unified(const char *path
, const char *suffix
, char **fs
) {
600 if (isempty(path
) && isempty(suffix
))
601 t
= strdup("/sys/fs/cgroup");
602 else if (isempty(path
))
603 t
= strappend("/sys/fs/cgroup/", suffix
);
604 else if (isempty(suffix
))
605 t
= strappend("/sys/fs/cgroup/", path
);
607 t
= strjoin("/sys/fs/cgroup/", path
, "/", suffix
);
615 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
623 /* If no controller is specified, we return the path
624 * *below* the controllers, without any prefix. */
626 if (!path
&& !suffix
)
634 t
= strjoin(path
, "/", suffix
);
638 *fs
= path_kill_slashes(t
);
642 if (!cg_controller_is_valid(controller
))
645 r
= cg_all_unified();
649 r
= join_path_unified(path
, suffix
, fs
);
651 r
= join_path_legacy(controller
, path
, suffix
, fs
);
655 path_kill_slashes(*fs
);
659 static int controller_is_accessible(const char *controller
) {
664 /* Checks whether a specific controller is accessible,
665 * i.e. its hierarchy mounted. In the unified hierarchy all
666 * controllers are considered accessible, except for the named
669 if (!cg_controller_is_valid(controller
))
672 r
= cg_all_unified();
676 /* We don't support named hierarchies if we are using
677 * the unified hierarchy. */
679 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
682 if (startswith(controller
, "name="))
688 dn
= controller_to_dirname(controller
);
689 cc
= strjoina("/sys/fs/cgroup/", dn
);
691 if (laccess(cc
, F_OK
) < 0)
698 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
704 /* Check if the specified controller is actually accessible */
705 r
= controller_is_accessible(controller
);
709 return cg_get_path(controller
, path
, suffix
, fs
);
712 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
717 if (typeflag
!= FTW_DP
)
720 if (ftwbuf
->level
< 1)
727 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
728 _cleanup_free_
char *fs
= NULL
;
733 r
= cg_get_path(controller
, path
, NULL
, &fs
);
738 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0) {
748 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
752 q
= cg_hybrid_unified();
755 if (q
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
756 q
= cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, delete_root
);
758 log_warning_errno(q
, "Failed to trim compat systemd cgroup %s: %m", path
);
764 int cg_create(const char *controller
, const char *path
) {
765 _cleanup_free_
char *fs
= NULL
;
768 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
772 r
= mkdir_parents(fs
, 0755);
776 if (mkdir(fs
, 0755) < 0) {
784 r
= cg_hybrid_unified();
788 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
789 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
791 log_warning_errno(r
, "Failed to create compat systemd cgroup %s: %m", path
);
797 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
802 r
= cg_create(controller
, path
);
806 q
= cg_attach(controller
, path
, pid
);
810 /* This does not remove the cgroup on failure */
814 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
815 _cleanup_free_
char *fs
= NULL
;
816 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
822 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
829 xsprintf(c
, PID_FMT
"\n", pid
);
831 r
= write_string_file(fs
, c
, 0);
835 r
= cg_hybrid_unified();
839 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
840 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, pid
);
842 log_warning_errno(r
, "Failed to attach %d to compat systemd cgroup %s: %m", pid
, path
);
848 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
855 r
= cg_attach(controller
, path
, pid
);
857 char prefix
[strlen(path
) + 1];
859 /* This didn't work? Then let's try all prefixes of
862 PATH_FOREACH_PREFIX(prefix
, path
) {
865 q
= cg_attach(controller
, prefix
, pid
);
874 int cg_set_group_access(
875 const char *controller
,
881 _cleanup_free_
char *fs
= NULL
;
884 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
887 if (mode
!= MODE_INVALID
)
890 r
= cg_get_path(controller
, path
, NULL
, &fs
);
894 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
898 r
= cg_hybrid_unified();
901 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
902 r
= cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, mode
, uid
, gid
);
904 log_warning_errno(r
, "Failed to set group access on compat systemd cgroup %s: %m", path
);
910 int cg_set_task_access(
911 const char *controller
,
917 _cleanup_free_
char *fs
= NULL
, *procs
= NULL
;
922 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
925 if (mode
!= MODE_INVALID
)
928 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
932 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
936 r
= cg_unified(controller
);
940 /* Compatibility, Always keep values for "tasks" in sync with
942 if (cg_get_path(controller
, path
, "tasks", &procs
) >= 0)
943 (void) chmod_and_chown(procs
, mode
, uid
, gid
);
946 r
= cg_hybrid_unified();
949 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
950 r
= cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, mode
, uid
, gid
);
952 log_warning_errno(r
, "Failed to set task access on compat systemd cgroup %s: %m", path
);
958 int cg_set_xattr(const char *controller
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
) {
959 _cleanup_free_
char *fs
= NULL
;
964 assert(value
|| size
<= 0);
966 r
= cg_get_path(controller
, path
, NULL
, &fs
);
970 if (setxattr(fs
, name
, value
, size
, flags
) < 0)
976 int cg_get_xattr(const char *controller
, const char *path
, const char *name
, void *value
, size_t size
) {
977 _cleanup_free_
char *fs
= NULL
;
984 r
= cg_get_path(controller
, path
, NULL
, &fs
);
988 n
= getxattr(fs
, name
, value
, size
);
995 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
996 _cleanup_fclose_
FILE *f
= NULL
;
998 const char *fs
, *controller_str
;
1006 if (!cg_controller_is_valid(controller
))
1009 controller
= SYSTEMD_CGROUP_CONTROLLER
;
1011 unified
= cg_unified(controller
);
1015 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
1016 controller_str
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
1018 controller_str
= controller
;
1020 cs
= strlen(controller_str
);
1023 fs
= procfs_file_alloca(pid
, "cgroup");
1024 f
= fopen(fs
, "re");
1026 return errno
== ENOENT
? -ESRCH
: -errno
;
1028 FOREACH_LINE(line
, f
, return -errno
) {
1034 e
= startswith(line
, "0:");
1044 const char *word
, *state
;
1047 l
= strchr(line
, ':');
1057 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
) {
1058 if (k
== cs
&& memcmp(word
, controller_str
, cs
) == 0) {
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
);
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
);
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
&& (isempty(path
) || path_equal(path
, "/")))
1193 r
= cg_unified(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
);
1206 return streq(t
, "0");
1208 _cleanup_closedir_
DIR *d
= NULL
;
1211 r
= cg_is_empty(controller
, path
);
1215 r
= cg_enumerate_subgroups(controller
, path
, &d
);
1221 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
1222 _cleanup_free_
char *p
= NULL
;
1224 p
= strjoin(path
, "/", fn
);
1229 r
= cg_is_empty_recursive(controller
, p
);
1240 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
1241 char *t
= NULL
, *u
= NULL
;
1247 if (!path_is_safe(spec
))
1255 *path
= path_kill_slashes(t
);
1264 e
= strchr(spec
, ':');
1266 if (!cg_controller_is_valid(spec
))
1283 t
= strndup(spec
, e
-spec
);
1286 if (!cg_controller_is_valid(t
)) {
1300 if (!path_is_safe(u
) ||
1301 !path_is_absolute(u
)) {
1307 path_kill_slashes(u
);
1323 int cg_mangle_path(const char *path
, char **result
) {
1324 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1331 /* First, check if it already is a filesystem path */
1332 if (path_startswith(path
, "/sys/fs/cgroup")) {
1338 *result
= path_kill_slashes(t
);
1342 /* Otherwise, treat it as cg spec */
1343 r
= cg_split_spec(path
, &c
, &p
);
1347 return cg_get_path(c
?: SYSTEMD_CGROUP_CONTROLLER
, p
?: "/", NULL
, result
);
1350 int cg_get_root_path(char **path
) {
1356 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1360 e
= endswith(p
, "/" SPECIAL_INIT_SCOPE
);
1362 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
); /* legacy */
1364 e
= endswith(p
, "/system"); /* even more legacy */
1372 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1373 _cleanup_free_
char *rt
= NULL
;
1381 /* If the root was specified let's use that, otherwise
1382 * let's determine it from PID 1 */
1384 r
= cg_get_root_path(&rt
);
1391 p
= path_startswith(cgroup
, root
);
1392 if (p
&& p
> cgroup
)
1400 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1401 _cleanup_free_
char *raw
= NULL
;
1408 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1412 r
= cg_shift_path(raw
, root
, &c
);
1432 int cg_path_decode_unit(const char *cgroup
, char **unit
) {
1439 n
= strcspn(cgroup
, "/");
1443 c
= strndupa(cgroup
, n
);
1446 if (!unit_name_is_valid(c
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1457 static bool valid_slice_name(const char *p
, size_t n
) {
1462 if (n
< strlen("x.slice"))
1465 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1471 c
= cg_unescape(buf
);
1473 return unit_name_is_valid(c
, UNIT_NAME_PLAIN
);
1479 static const char *skip_slices(const char *p
) {
1482 /* Skips over all slice assignments */
1487 p
+= strspn(p
, "/");
1489 n
= strcspn(p
, "/");
1490 if (!valid_slice_name(p
, n
))
1497 int cg_path_get_unit(const char *path
, char **ret
) {
1505 e
= skip_slices(path
);
1507 r
= cg_path_decode_unit(e
, &unit
);
1511 /* We skipped over the slices, don't accept any now */
1512 if (endswith(unit
, ".slice")) {
1521 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1522 _cleanup_free_
char *cgroup
= NULL
;
1527 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1531 return cg_path_get_unit(cgroup
, unit
);
1535 * Skip session-*.scope, but require it to be there.
1537 static const char *skip_session(const char *p
) {
1543 p
+= strspn(p
, "/");
1545 n
= strcspn(p
, "/");
1546 if (n
< strlen("session-x.scope"))
1549 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1550 char buf
[n
- 8 - 6 + 1];
1552 memcpy(buf
, p
+ 8, n
- 8 - 6);
1555 /* Note that session scopes never need unescaping,
1556 * since they cannot conflict with the kernel's own
1557 * names, hence we don't need to call cg_unescape()
1560 if (!session_id_valid(buf
))
1564 p
+= strspn(p
, "/");
1572 * Skip user@*.service, but require it to be there.
1574 static const char *skip_user_manager(const char *p
) {
1580 p
+= strspn(p
, "/");
1582 n
= strcspn(p
, "/");
1583 if (n
< strlen("user@x.service"))
1586 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1587 char buf
[n
- 5 - 8 + 1];
1589 memcpy(buf
, p
+ 5, n
- 5 - 8);
1592 /* Note that user manager services never need unescaping,
1593 * since they cannot conflict with the kernel's own
1594 * names, hence we don't need to call cg_unescape()
1597 if (parse_uid(buf
, NULL
) < 0)
1601 p
+= strspn(p
, "/");
1609 static const char *skip_user_prefix(const char *path
) {
1614 /* Skip slices, if there are any */
1615 e
= skip_slices(path
);
1617 /* Skip the user manager, if it's in the path now... */
1618 t
= skip_user_manager(e
);
1622 /* Alternatively skip the user session if it is in the path... */
1623 return skip_session(e
);
1626 int cg_path_get_user_unit(const char *path
, char **ret
) {
1632 t
= skip_user_prefix(path
);
1636 /* And from here on it looks pretty much the same as for a
1637 * system unit, hence let's use the same parser from here
1639 return cg_path_get_unit(t
, ret
);
1642 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1643 _cleanup_free_
char *cgroup
= NULL
;
1648 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1652 return cg_path_get_user_unit(cgroup
, unit
);
1655 int cg_path_get_machine_name(const char *path
, char **machine
) {
1656 _cleanup_free_
char *u
= NULL
;
1660 r
= cg_path_get_unit(path
, &u
);
1664 sl
= strjoina("/run/systemd/machines/unit:", u
);
1665 return readlink_malloc(sl
, machine
);
1668 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1669 _cleanup_free_
char *cgroup
= NULL
;
1674 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1678 return cg_path_get_machine_name(cgroup
, machine
);
1681 int cg_path_get_session(const char *path
, char **session
) {
1682 _cleanup_free_
char *unit
= NULL
;
1688 r
= cg_path_get_unit(path
, &unit
);
1692 start
= startswith(unit
, "session-");
1695 end
= endswith(start
, ".scope");
1700 if (!session_id_valid(start
))
1716 int cg_pid_get_session(pid_t pid
, char **session
) {
1717 _cleanup_free_
char *cgroup
= NULL
;
1720 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1724 return cg_path_get_session(cgroup
, session
);
1727 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1728 _cleanup_free_
char *slice
= NULL
;
1734 r
= cg_path_get_slice(path
, &slice
);
1738 start
= startswith(slice
, "user-");
1741 end
= endswith(start
, ".slice");
1746 if (parse_uid(start
, uid
) < 0)
1752 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1753 _cleanup_free_
char *cgroup
= NULL
;
1756 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1760 return cg_path_get_owner_uid(cgroup
, uid
);
1763 int cg_path_get_slice(const char *p
, char **slice
) {
1764 const char *e
= NULL
;
1769 /* Finds the right-most slice unit from the beginning, but
1770 * stops before we come to the first non-slice unit. */
1775 p
+= strspn(p
, "/");
1777 n
= strcspn(p
, "/");
1778 if (!valid_slice_name(p
, n
)) {
1783 s
= strdup(SPECIAL_ROOT_SLICE
);
1791 return cg_path_decode_unit(e
, slice
);
1799 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1800 _cleanup_free_
char *cgroup
= NULL
;
1805 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1809 return cg_path_get_slice(cgroup
, slice
);
1812 int cg_path_get_user_slice(const char *p
, char **slice
) {
1817 t
= skip_user_prefix(p
);
1821 /* And now it looks pretty much the same as for a system
1822 * slice, so let's just use the same parser from here on. */
1823 return cg_path_get_slice(t
, slice
);
1826 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1827 _cleanup_free_
char *cgroup
= NULL
;
1832 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1836 return cg_path_get_user_slice(cgroup
, slice
);
1839 char *cg_escape(const char *p
) {
1840 bool need_prefix
= false;
1842 /* This implements very minimal escaping for names to be used
1843 * as file names in the cgroup tree: any name which might
1844 * conflict with a kernel name or is prefixed with '_' is
1845 * prefixed with a '_'. That way, when reading cgroup names it
1846 * is sufficient to remove a single prefixing underscore if
1849 /* The return value of this function (unlike cg_unescape())
1855 streq(p
, "notify_on_release") ||
1856 streq(p
, "release_agent") ||
1857 streq(p
, "tasks") ||
1858 startswith(p
, "cgroup."))
1863 dot
= strrchr(p
, '.');
1868 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
1871 n
= cgroup_controller_to_string(c
);
1876 if (memcmp(p
, n
, l
) != 0)
1886 return strappend("_", p
);
1891 char *cg_unescape(const char *p
) {
1894 /* The return value of this function (unlike cg_escape())
1895 * doesn't need free()! */
1903 #define CONTROLLER_VALID \
1907 bool cg_controller_is_valid(const char *p
) {
1913 if (streq(p
, SYSTEMD_CGROUP_CONTROLLER
))
1916 s
= startswith(p
, "name=");
1920 if (*p
== 0 || *p
== '_')
1923 for (t
= p
; *t
; t
++)
1924 if (!strchr(CONTROLLER_VALID
, *t
))
1927 if (t
- p
> FILENAME_MAX
)
1933 int cg_slice_to_path(const char *unit
, char **ret
) {
1934 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1941 if (streq(unit
, SPECIAL_ROOT_SLICE
)) {
1951 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1954 if (!endswith(unit
, ".slice"))
1957 r
= unit_name_to_prefix(unit
, &p
);
1961 dash
= strchr(p
, '-');
1963 /* Don't allow initial dashes */
1968 _cleanup_free_
char *escaped
= NULL
;
1969 char n
[dash
- p
+ sizeof(".slice")];
1971 /* Don't allow trailing or double dashes */
1972 if (dash
[1] == 0 || dash
[1] == '-')
1975 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1976 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1979 escaped
= cg_escape(n
);
1983 if (!strextend(&s
, escaped
, "/", NULL
))
1986 dash
= strchr(dash
+1, '-');
1989 e
= cg_escape(unit
);
1993 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(const char *controller
, const char *path
, const char *attribute
, const char **keys
, char **values
) {
2025 _cleanup_free_
char *filename
= NULL
, *content
= NULL
;
2029 for (i
= 0; keys
[i
]; i
++)
2032 r
= cg_get_path(controller
, path
, attribute
, &filename
);
2036 r
= read_full_file(filename
, &content
, NULL
);
2041 while ((line
= strsep(&p
, "\n"))) {
2044 key
= strsep(&line
, " ");
2046 for (i
= 0; keys
[i
]; i
++) {
2047 if (streq(key
, keys
[i
])) {
2048 values
[i
] = strdup(line
);
2054 for (i
= 0; keys
[i
]; i
++) {
2056 for (i
= 0; keys
[i
]; i
++) {
2067 int cg_create_everywhere(CGroupMask supported
, CGroupMask mask
, const char *path
) {
2071 /* This one will create a cgroup in our private tree, but also
2072 * duplicate it in the trees specified in mask, and remove it
2075 /* First create the cgroup in our own hierarchy. */
2076 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
2080 /* If we are in the unified hierarchy, we are done now */
2081 r
= cg_all_unified();
2087 /* Otherwise, do the same in the other hierarchies */
2088 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2089 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2092 n
= cgroup_controller_to_string(c
);
2095 (void) cg_create(n
, path
);
2096 else if (supported
& bit
)
2097 (void) cg_trim(n
, path
, true);
2103 int cg_attach_everywhere(CGroupMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
2107 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
2111 r
= cg_all_unified();
2117 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2118 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2119 const char *p
= NULL
;
2121 if (!(supported
& bit
))
2125 p
= path_callback(bit
, userdata
);
2130 (void) cg_attach_fallback(cgroup_controller_to_string(c
), p
, pid
);
2136 int cg_attach_many_everywhere(CGroupMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
2141 SET_FOREACH(pidp
, pids
, i
) {
2142 pid_t pid
= PTR_TO_PID(pidp
);
2145 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
2146 if (q
< 0 && r
>= 0)
2153 int cg_migrate_everywhere(CGroupMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
2157 if (!path_equal(from
, to
)) {
2158 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, CGROUP_REMOVE
);
2163 q
= cg_all_unified();
2169 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2170 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2171 const char *p
= NULL
;
2173 if (!(supported
& bit
))
2177 p
= to_callback(bit
, userdata
);
2182 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, cgroup_controller_to_string(c
), p
, 0);
2188 int cg_trim_everywhere(CGroupMask supported
, const char *path
, bool delete_root
) {
2192 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
2196 q
= cg_all_unified();
2202 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2203 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2205 if (!(supported
& bit
))
2208 (void) cg_trim(cgroup_controller_to_string(c
), path
, delete_root
);
2214 int cg_mask_supported(CGroupMask
*ret
) {
2215 CGroupMask mask
= 0;
2218 /* Determines the mask of supported cgroup controllers. Only
2219 * includes controllers we can make sense of and that are
2220 * actually accessible. */
2222 r
= cg_all_unified();
2226 _cleanup_free_
char *root
= NULL
, *controllers
= NULL
, *path
= NULL
;
2229 /* In the unified hierarchy we can read the supported
2230 * and accessible controllers from a the top-level
2231 * cgroup attribute */
2233 r
= cg_get_root_path(&root
);
2237 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, root
, "cgroup.controllers", &path
);
2241 r
= read_one_line_file(path
, &controllers
);
2247 _cleanup_free_
char *n
= NULL
;
2250 r
= extract_first_word(&c
, &n
, NULL
, 0);
2256 v
= cgroup_controller_from_string(n
);
2260 mask
|= CGROUP_CONTROLLER_TO_MASK(v
);
2263 /* Currently, we support the cpu, memory, io and pids
2264 * controller in the unified hierarchy, mask
2265 * everything else off. */
2266 mask
&= CGROUP_MASK_CPU
| CGROUP_MASK_MEMORY
| CGROUP_MASK_IO
| CGROUP_MASK_PIDS
;
2271 /* In the legacy hierarchy, we check whether which
2272 * hierarchies are mounted. */
2274 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2277 n
= cgroup_controller_to_string(c
);
2278 if (controller_is_accessible(n
) >= 0)
2279 mask
|= CGROUP_CONTROLLER_TO_MASK(c
);
2287 int cg_kernel_controllers(Set
*controllers
) {
2288 _cleanup_fclose_
FILE *f
= NULL
;
2292 assert(controllers
);
2294 /* Determines the full list of kernel-known controllers. Might
2295 * include controllers we don't actually support, arbitrary
2296 * named hierarchies and controllers that aren't currently
2297 * accessible (because not mounted). */
2299 f
= fopen("/proc/cgroups", "re");
2301 if (errno
== ENOENT
)
2306 /* Ignore the header line */
2307 (void) fgets(buf
, sizeof(buf
), f
);
2314 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
2319 if (ferror(f
) && errno
> 0)
2330 if (!cg_controller_is_valid(controller
)) {
2335 r
= set_consume(controllers
, controller
);
2343 static thread_local CGroupUnified unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2345 /* The hybrid mode was initially implemented in v232 and simply mounted
2346 * cgroup v2 on /sys/fs/cgroup/systemd. This unfortunately broke other
2347 * tools (such as docker) which expected the v1 "name=systemd" hierarchy
2348 * on /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs
2349 * v2 on /sys/fs/cgroup/unified and maintains "name=systemd" hierarchy
2350 * on /sys/fs/cgroup/systemd for compatibility with other tools.
2352 * To keep live upgrade working, we detect and support v232 layout. When
2353 * v232 layout is detected, to keep cgroup v2 process management but
2354 * disable the compat dual layout, we return %true on
2355 * cg_unified(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2357 static thread_local
bool unified_systemd_v232
;
2359 static int cg_update_unified(void) {
2363 /* Checks if we support the unified hierarchy. Returns an
2364 * error when the cgroup hierarchies aren't mounted yet or we
2365 * have any other trouble determining if the unified hierarchy
2368 if (unified_cache
>= CGROUP_UNIFIED_NONE
)
2371 if (statfs("/sys/fs/cgroup/", &fs
) < 0)
2374 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
))
2375 unified_cache
= CGROUP_UNIFIED_ALL
;
2376 else if (F_TYPE_EQUAL(fs
.f_type
, TMPFS_MAGIC
)) {
2377 if (statfs("/sys/fs/cgroup/unified/", &fs
) == 0 &&
2378 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2379 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2380 unified_systemd_v232
= false;
2381 } else if (statfs("/sys/fs/cgroup/systemd/", &fs
) == 0 &&
2382 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2383 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2384 unified_systemd_v232
= true;
2386 if (statfs("/sys/fs/cgroup/systemd/", &fs
) < 0)
2388 if (!F_TYPE_EQUAL(fs
.f_type
, CGROUP_SUPER_MAGIC
))
2390 unified_cache
= CGROUP_UNIFIED_NONE
;
2398 int cg_unified(const char *controller
) {
2401 r
= cg_update_unified();
2405 if (unified_cache
== CGROUP_UNIFIED_NONE
)
2408 if (unified_cache
>= CGROUP_UNIFIED_ALL
)
2411 return streq_ptr(controller
, SYSTEMD_CGROUP_CONTROLLER
);
2414 int cg_all_unified(void) {
2415 return cg_unified(NULL
);
2418 int cg_hybrid_unified(void) {
2421 r
= cg_update_unified();
2425 return unified_cache
== CGROUP_UNIFIED_SYSTEMD
&& !unified_systemd_v232
;
2428 int cg_unified_flush(void) {
2429 unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2431 return cg_update_unified();
2434 int cg_enable_everywhere(CGroupMask supported
, CGroupMask mask
, const char *p
) {
2435 _cleanup_free_
char *fs
= NULL
;
2444 r
= cg_all_unified();
2447 if (r
== 0) /* on the legacy hiearchy there's no joining of controllers defined */
2450 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, p
, "cgroup.subtree_control", &fs
);
2454 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2455 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2458 if (!(supported
& bit
))
2461 n
= cgroup_controller_to_string(c
);
2463 char s
[1 + strlen(n
) + 1];
2465 s
[0] = mask
& bit
? '+' : '-';
2468 r
= write_string_file(fs
, s
, 0);
2470 log_debug_errno(r
, "Failed to enable controller %s for %s (%s): %m", n
, p
, fs
);
2477 bool cg_is_unified_wanted(void) {
2478 static thread_local
int wanted
= -1;
2481 const bool is_default
= DEFAULT_HIERARCHY
== CGROUP_UNIFIED_ALL
;
2483 /* If we have a cached value, return that. */
2487 /* If the hierarchy is already mounted, then follow whatever
2488 * was chosen for it. */
2489 if (cg_unified_flush() >= 0)
2490 return (wanted
= unified_cache
>= CGROUP_UNIFIED_ALL
);
2492 /* Otherwise, let's see what the kernel command line has to say.
2493 * Since checking is expensive, cache a non-error result. */
2494 r
= proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b
);
2496 return (wanted
= r
> 0 ? b
: is_default
);
2499 bool cg_is_legacy_wanted(void) {
2500 static thread_local
int wanted
= -1;
2502 /* If we have a cached value, return that. */
2506 /* Check if we have cgroups2 already mounted. */
2507 if (cg_unified_flush() >= 0 &&
2508 unified_cache
== CGROUP_UNIFIED_ALL
)
2509 return (wanted
= false);
2511 /* Otherwise, assume that at least partial legacy is wanted,
2512 * since cgroups2 should already be mounted at this point. */
2513 return (wanted
= true);
2516 bool cg_is_hybrid_wanted(void) {
2517 static thread_local
int wanted
= -1;
2520 const bool is_default
= DEFAULT_HIERARCHY
>= CGROUP_UNIFIED_SYSTEMD
;
2521 /* We default to true if the default is "hybrid", obviously,
2522 * but also when the default is "unified", because if we get
2523 * called, it means that unified hierarchy was not mounted. */
2525 /* If we have a cached value, return that. */
2529 /* If the hierarchy is already mounted, then follow whatever
2530 * was chosen for it. */
2531 if (cg_unified_flush() >= 0 &&
2532 unified_cache
== CGROUP_UNIFIED_ALL
)
2533 return (wanted
= false);
2535 /* Otherwise, let's see what the kernel command line has to say.
2536 * Since checking is expensive, cache a non-error result. */
2537 r
= proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b
);
2539 /* The meaning of the kernel option is reversed wrt. to the return value
2540 * of this function, hence the negation. */
2541 return (wanted
= r
> 0 ? !b
: is_default
);
2544 int cg_weight_parse(const char *s
, uint64_t *ret
) {
2549 *ret
= CGROUP_WEIGHT_INVALID
;
2553 r
= safe_atou64(s
, &u
);
2557 if (u
< CGROUP_WEIGHT_MIN
|| u
> CGROUP_WEIGHT_MAX
)
2564 const uint64_t cgroup_io_limit_defaults
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2565 [CGROUP_IO_RBPS_MAX
] = CGROUP_LIMIT_MAX
,
2566 [CGROUP_IO_WBPS_MAX
] = CGROUP_LIMIT_MAX
,
2567 [CGROUP_IO_RIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2568 [CGROUP_IO_WIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2571 static const char* const cgroup_io_limit_type_table
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2572 [CGROUP_IO_RBPS_MAX
] = "IOReadBandwidthMax",
2573 [CGROUP_IO_WBPS_MAX
] = "IOWriteBandwidthMax",
2574 [CGROUP_IO_RIOPS_MAX
] = "IOReadIOPSMax",
2575 [CGROUP_IO_WIOPS_MAX
] = "IOWriteIOPSMax",
2578 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type
, CGroupIOLimitType
);
2580 int cg_cpu_shares_parse(const char *s
, uint64_t *ret
) {
2585 *ret
= CGROUP_CPU_SHARES_INVALID
;
2589 r
= safe_atou64(s
, &u
);
2593 if (u
< CGROUP_CPU_SHARES_MIN
|| u
> CGROUP_CPU_SHARES_MAX
)
2600 int cg_blkio_weight_parse(const char *s
, uint64_t *ret
) {
2605 *ret
= CGROUP_BLKIO_WEIGHT_INVALID
;
2609 r
= safe_atou64(s
, &u
);
2613 if (u
< CGROUP_BLKIO_WEIGHT_MIN
|| u
> CGROUP_BLKIO_WEIGHT_MAX
)
2620 bool is_cgroup_fs(const struct statfs
*s
) {
2621 return is_fs_type(s
, CGROUP_SUPER_MAGIC
) ||
2622 is_fs_type(s
, CGROUP2_SUPER_MAGIC
);
2625 bool fd_is_cgroup_fs(int fd
) {
2628 if (fstatfs(fd
, &s
) < 0)
2631 return is_cgroup_fs(&s
);
2634 static const char *cgroup_controller_table
[_CGROUP_CONTROLLER_MAX
] = {
2635 [CGROUP_CONTROLLER_CPU
] = "cpu",
2636 [CGROUP_CONTROLLER_CPUACCT
] = "cpuacct",
2637 [CGROUP_CONTROLLER_IO
] = "io",
2638 [CGROUP_CONTROLLER_BLKIO
] = "blkio",
2639 [CGROUP_CONTROLLER_MEMORY
] = "memory",
2640 [CGROUP_CONTROLLER_DEVICES
] = "devices",
2641 [CGROUP_CONTROLLER_PIDS
] = "pids",
2644 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller
, CGroupController
);