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"
59 #include "unit-name.h"
60 #include "user-util.h"
62 int cg_enumerate_processes(const char *controller
, const char *path
, FILE **_f
) {
63 _cleanup_free_
char *fs
= NULL
;
69 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
81 int cg_read_pid(FILE *f
, pid_t
*_pid
) {
84 /* Note that the cgroup.procs might contain duplicates! See
85 * cgroups.txt for details. */
91 if (fscanf(f
, "%lu", &ul
) != 1) {
96 return errno
> 0 ? -errno
: -EIO
;
107 const char *controller
,
112 _cleanup_free_
char *events
= NULL
, *content
= NULL
;
116 r
= cg_get_path(controller
, path
, "cgroup.events", &events
);
120 r
= read_full_file(events
, &content
, NULL
);
125 while ((line
= strsep(&p
, "\n"))) {
128 key
= strsep(&line
, " ");
132 if (strcmp(key
, event
))
142 bool cg_ns_supported(void) {
143 static thread_local
int enabled
= -1;
148 if (access("/proc/self/ns/cgroup", F_OK
) == 0)
156 int cg_enumerate_subgroups(const char *controller
, const char *path
, DIR **_d
) {
157 _cleanup_free_
char *fs
= NULL
;
163 /* This is not recursive! */
165 r
= cg_get_path(controller
, path
, NULL
, &fs
);
177 int cg_read_subgroup(DIR *d
, char **fn
) {
183 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
186 if (de
->d_type
!= DT_DIR
)
189 if (dot_or_dot_dot(de
->d_name
))
192 b
= strdup(de
->d_name
);
203 int cg_rmdir(const char *controller
, const char *path
) {
204 _cleanup_free_
char *p
= NULL
;
207 r
= cg_get_path(controller
, path
, NULL
, &p
);
212 if (r
< 0 && errno
!= ENOENT
)
215 r
= cg_hybrid_unified();
221 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
222 r
= cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
224 log_warning_errno(r
, "Failed to remove compat systemd cgroup %s: %m", path
);
231 const char *controller
,
236 cg_kill_log_func_t log_kill
,
239 _cleanup_set_free_ Set
*allocated_set
= NULL
;
246 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
247 * SIGCONT on SIGKILL. */
248 if (IN_SET(sig
, SIGCONT
, SIGKILL
))
249 flags
&= ~CGROUP_SIGCONT
;
251 /* This goes through the tasks list and kills them all. This
252 * is repeated until no further processes are added to the
253 * tasks list, to properly handle forking processes */
256 s
= allocated_set
= set_new(NULL
);
261 my_pid
= getpid_cached();
264 _cleanup_fclose_
FILE *f
= NULL
;
268 r
= cg_enumerate_processes(controller
, path
, &f
);
270 if (ret
>= 0 && r
!= -ENOENT
)
276 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
278 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
281 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
285 log_kill(pid
, sig
, userdata
);
287 /* If we haven't killed this process yet, kill
289 if (kill(pid
, sig
) < 0) {
290 if (ret
>= 0 && errno
!= ESRCH
)
293 if (flags
& CGROUP_SIGCONT
)
294 (void) kill(pid
, SIGCONT
);
302 r
= set_put(s
, PID_TO_PTR(pid
));
318 /* To avoid racing against processes which fork
319 * quicker than we can kill them we repeat this until
320 * no new pids need to be killed. */
327 int cg_kill_recursive(
328 const char *controller
,
333 cg_kill_log_func_t log_kill
,
336 _cleanup_set_free_ Set
*allocated_set
= NULL
;
337 _cleanup_closedir_
DIR *d
= NULL
;
345 s
= allocated_set
= set_new(NULL
);
350 ret
= cg_kill(controller
, path
, sig
, flags
, s
, log_kill
, userdata
);
352 r
= cg_enumerate_subgroups(controller
, path
, &d
);
354 if (ret
>= 0 && r
!= -ENOENT
)
360 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
361 _cleanup_free_
char *p
= NULL
;
363 p
= strjoin(path
, "/", fn
);
368 r
= cg_kill_recursive(controller
, p
, sig
, flags
, s
, log_kill
, userdata
);
369 if (r
!= 0 && ret
>= 0)
372 if (ret
>= 0 && r
< 0)
375 if (flags
& CGROUP_REMOVE
) {
376 r
= cg_rmdir(controller
, path
);
377 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
392 _cleanup_set_free_ Set
*s
= NULL
;
405 my_pid
= getpid_cached();
408 _cleanup_fclose_
FILE *f
= NULL
;
412 r
= cg_enumerate_processes(cfrom
, pfrom
, &f
);
414 if (ret
>= 0 && r
!= -ENOENT
)
420 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
422 /* This might do weird stuff if we aren't a
423 * single-threaded program. However, we
424 * luckily know we are not */
425 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
428 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
431 /* Ignore kernel threads. Since they can only
432 * exist in the root cgroup, we only check for
435 (isempty(pfrom
) || path_equal(pfrom
, "/")) &&
436 is_kernel_thread(pid
) > 0)
439 r
= cg_attach(cto
, pto
, pid
);
441 if (ret
>= 0 && r
!= -ESRCH
)
448 r
= set_put(s
, PID_TO_PTR(pid
));
468 int cg_migrate_recursive(
475 _cleanup_closedir_
DIR *d
= NULL
;
484 ret
= cg_migrate(cfrom
, pfrom
, cto
, pto
, flags
);
486 r
= cg_enumerate_subgroups(cfrom
, pfrom
, &d
);
488 if (ret
>= 0 && r
!= -ENOENT
)
494 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
495 _cleanup_free_
char *p
= NULL
;
497 p
= strjoin(pfrom
, "/", fn
);
502 r
= cg_migrate_recursive(cfrom
, p
, cto
, pto
, flags
);
503 if (r
!= 0 && ret
>= 0)
507 if (r
< 0 && ret
>= 0)
510 if (flags
& CGROUP_REMOVE
) {
511 r
= cg_rmdir(cfrom
, pfrom
);
512 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
519 int cg_migrate_recursive_fallback(
533 r
= cg_migrate_recursive(cfrom
, pfrom
, cto
, pto
, flags
);
535 char prefix
[strlen(pto
) + 1];
537 /* This didn't work? Then let's try all prefixes of the destination */
539 PATH_FOREACH_PREFIX(prefix
, pto
) {
542 q
= cg_migrate_recursive(cfrom
, pfrom
, cto
, prefix
, flags
);
551 static const char *controller_to_dirname(const char *controller
) {
556 /* Converts a controller name to the directory name below
557 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
558 * just cuts off the name= prefixed used for named
559 * hierarchies, if it is specified. */
561 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
562 if (cg_hybrid_unified() > 0)
563 controller
= SYSTEMD_CGROUP_CONTROLLER_HYBRID
;
565 controller
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
568 e
= startswith(controller
, "name=");
575 static int join_path_legacy(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
582 dn
= controller_to_dirname(controller
);
584 if (isempty(path
) && isempty(suffix
))
585 t
= strappend("/sys/fs/cgroup/", dn
);
586 else if (isempty(path
))
587 t
= strjoin("/sys/fs/cgroup/", dn
, "/", suffix
);
588 else if (isempty(suffix
))
589 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
);
591 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
, "/", suffix
);
599 static int join_path_unified(const char *path
, const char *suffix
, char **fs
) {
604 if (isempty(path
) && isempty(suffix
))
605 t
= strdup("/sys/fs/cgroup");
606 else if (isempty(path
))
607 t
= strappend("/sys/fs/cgroup/", suffix
);
608 else if (isempty(suffix
))
609 t
= strappend("/sys/fs/cgroup/", path
);
611 t
= strjoin("/sys/fs/cgroup/", path
, "/", suffix
);
619 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
627 /* If no controller is specified, we return the path
628 * *below* the controllers, without any prefix. */
630 if (!path
&& !suffix
)
638 t
= strjoin(path
, "/", suffix
);
642 *fs
= path_kill_slashes(t
);
646 if (!cg_controller_is_valid(controller
))
649 r
= cg_all_unified();
653 r
= join_path_unified(path
, suffix
, fs
);
655 r
= join_path_legacy(controller
, path
, suffix
, fs
);
659 path_kill_slashes(*fs
);
663 static int controller_is_accessible(const char *controller
) {
668 /* Checks whether a specific controller is accessible,
669 * i.e. its hierarchy mounted. In the unified hierarchy all
670 * controllers are considered accessible, except for the named
673 if (!cg_controller_is_valid(controller
))
676 r
= cg_all_unified();
680 /* We don't support named hierarchies if we are using
681 * the unified hierarchy. */
683 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
686 if (startswith(controller
, "name="))
692 dn
= controller_to_dirname(controller
);
693 cc
= strjoina("/sys/fs/cgroup/", dn
);
695 if (laccess(cc
, F_OK
) < 0)
702 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
708 /* Check if the specified controller is actually accessible */
709 r
= controller_is_accessible(controller
);
713 return cg_get_path(controller
, path
, suffix
, fs
);
716 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
721 if (typeflag
!= FTW_DP
)
724 if (ftwbuf
->level
< 1)
731 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
732 _cleanup_free_
char *fs
= NULL
;
737 r
= cg_get_path(controller
, path
, NULL
, &fs
);
742 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0) {
752 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
756 q
= cg_hybrid_unified();
759 if (q
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
760 q
= cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, delete_root
);
762 log_warning_errno(q
, "Failed to trim compat systemd cgroup %s: %m", path
);
768 int cg_create(const char *controller
, const char *path
) {
769 _cleanup_free_
char *fs
= NULL
;
772 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
776 r
= mkdir_parents(fs
, 0755);
780 if (mkdir(fs
, 0755) < 0) {
788 r
= cg_hybrid_unified();
792 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
793 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
795 log_warning_errno(r
, "Failed to create compat systemd cgroup %s: %m", path
);
801 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
806 r
= cg_create(controller
, path
);
810 q
= cg_attach(controller
, path
, pid
);
814 /* This does not remove the cgroup on failure */
818 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
819 _cleanup_free_
char *fs
= NULL
;
820 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
826 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
831 pid
= getpid_cached();
833 xsprintf(c
, PID_FMT
"\n", pid
);
835 r
= write_string_file(fs
, c
, 0);
839 r
= cg_hybrid_unified();
843 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
844 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, pid
);
846 log_warning_errno(r
, "Failed to attach "PID_FMT
" to compat systemd cgroup %s: %m", pid
, path
);
852 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
859 r
= cg_attach(controller
, path
, pid
);
861 char prefix
[strlen(path
) + 1];
863 /* This didn't work? Then let's try all prefixes of
866 PATH_FOREACH_PREFIX(prefix
, path
) {
869 q
= cg_attach(controller
, prefix
, pid
);
878 int cg_set_group_access(
879 const char *controller
,
885 _cleanup_free_
char *fs
= NULL
;
888 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
891 if (mode
!= MODE_INVALID
)
894 r
= cg_get_path(controller
, path
, NULL
, &fs
);
898 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
902 r
= cg_hybrid_unified();
905 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
906 r
= cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, mode
, uid
, gid
);
908 log_warning_errno(r
, "Failed to set group access on compat systemd cgroup %s: %m", path
);
914 int cg_set_task_access(
915 const char *controller
,
921 _cleanup_free_
char *fs
= NULL
, *procs
= NULL
;
926 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
929 if (mode
!= MODE_INVALID
)
932 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
936 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
940 r
= cg_unified_controller(controller
);
944 /* Compatibility, Always keep values for "tasks" in sync with
946 if (cg_get_path(controller
, path
, "tasks", &procs
) >= 0)
947 (void) chmod_and_chown(procs
, mode
, uid
, gid
);
950 r
= cg_hybrid_unified();
953 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
954 r
= cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, mode
, uid
, gid
);
956 log_warning_errno(r
, "Failed to set task access on compat systemd cgroup %s: %m", path
);
962 int cg_set_xattr(const char *controller
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
) {
963 _cleanup_free_
char *fs
= NULL
;
968 assert(value
|| size
<= 0);
970 r
= cg_get_path(controller
, path
, NULL
, &fs
);
974 if (setxattr(fs
, name
, value
, size
, flags
) < 0)
980 int cg_get_xattr(const char *controller
, const char *path
, const char *name
, void *value
, size_t size
) {
981 _cleanup_free_
char *fs
= NULL
;
988 r
= cg_get_path(controller
, path
, NULL
, &fs
);
992 n
= getxattr(fs
, name
, value
, size
);
999 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
1000 _cleanup_fclose_
FILE *f
= NULL
;
1001 char line
[LINE_MAX
];
1002 const char *fs
, *controller_str
;
1010 if (!cg_controller_is_valid(controller
))
1013 controller
= SYSTEMD_CGROUP_CONTROLLER
;
1015 unified
= cg_unified_controller(controller
);
1019 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
1020 controller_str
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
1022 controller_str
= controller
;
1024 cs
= strlen(controller_str
);
1027 fs
= procfs_file_alloca(pid
, "cgroup");
1028 f
= fopen(fs
, "re");
1030 return errno
== ENOENT
? -ESRCH
: -errno
;
1032 FOREACH_LINE(line
, f
, return -errno
) {
1038 e
= startswith(line
, "0:");
1048 const char *word
, *state
;
1051 l
= strchr(line
, ':');
1061 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
) {
1062 if (k
== cs
&& memcmp(word
, controller_str
, cs
) == 0) {
1083 int cg_install_release_agent(const char *controller
, const char *agent
) {
1084 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
1090 r
= cg_unified_controller(controller
);
1093 if (r
> 0) /* doesn't apply to unified hierarchy */
1096 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1100 r
= read_one_line_file(fs
, &contents
);
1104 sc
= strstrip(contents
);
1106 r
= write_string_file(fs
, agent
, 0);
1109 } else if (!path_equal(sc
, agent
))
1113 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1117 contents
= mfree(contents
);
1118 r
= read_one_line_file(fs
, &contents
);
1122 sc
= strstrip(contents
);
1123 if (streq(sc
, "0")) {
1124 r
= write_string_file(fs
, "1", 0);
1131 if (!streq(sc
, "1"))
1137 int cg_uninstall_release_agent(const char *controller
) {
1138 _cleanup_free_
char *fs
= NULL
;
1141 r
= cg_unified_controller(controller
);
1144 if (r
> 0) /* Doesn't apply to unified hierarchy */
1147 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1151 r
= write_string_file(fs
, "0", 0);
1157 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1161 r
= write_string_file(fs
, "", 0);
1168 int cg_is_empty(const char *controller
, const char *path
) {
1169 _cleanup_fclose_
FILE *f
= NULL
;
1175 r
= cg_enumerate_processes(controller
, path
, &f
);
1181 r
= cg_read_pid(f
, &pid
);
1188 int cg_is_empty_recursive(const char *controller
, const char *path
) {
1193 /* The root cgroup is always populated */
1194 if (controller
&& (isempty(path
) || path_equal(path
, "/")))
1197 r
= cg_unified_controller(controller
);
1201 _cleanup_free_
char *t
= NULL
;
1203 /* On the unified hierarchy we can check empty state
1204 * via the "populated" attribute of "cgroup.events". */
1206 r
= cg_read_event(controller
, path
, "populated", &t
);
1210 return streq(t
, "0");
1212 _cleanup_closedir_
DIR *d
= NULL
;
1215 r
= cg_is_empty(controller
, path
);
1219 r
= cg_enumerate_subgroups(controller
, path
, &d
);
1225 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
1226 _cleanup_free_
char *p
= NULL
;
1228 p
= strjoin(path
, "/", fn
);
1233 r
= cg_is_empty_recursive(controller
, p
);
1244 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
1245 char *t
= NULL
, *u
= NULL
;
1251 if (!path_is_safe(spec
))
1259 *path
= path_kill_slashes(t
);
1268 e
= strchr(spec
, ':');
1270 if (!cg_controller_is_valid(spec
))
1287 t
= strndup(spec
, e
-spec
);
1290 if (!cg_controller_is_valid(t
)) {
1304 if (!path_is_safe(u
) ||
1305 !path_is_absolute(u
)) {
1311 path_kill_slashes(u
);
1327 int cg_mangle_path(const char *path
, char **result
) {
1328 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1335 /* First, check if it already is a filesystem path */
1336 if (path_startswith(path
, "/sys/fs/cgroup")) {
1342 *result
= path_kill_slashes(t
);
1346 /* Otherwise, treat it as cg spec */
1347 r
= cg_split_spec(path
, &c
, &p
);
1351 return cg_get_path(c
?: SYSTEMD_CGROUP_CONTROLLER
, p
?: "/", NULL
, result
);
1354 int cg_get_root_path(char **path
) {
1360 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1364 e
= endswith(p
, "/" SPECIAL_INIT_SCOPE
);
1366 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
); /* legacy */
1368 e
= endswith(p
, "/system"); /* even more legacy */
1376 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1377 _cleanup_free_
char *rt
= NULL
;
1385 /* If the root was specified let's use that, otherwise
1386 * let's determine it from PID 1 */
1388 r
= cg_get_root_path(&rt
);
1395 p
= path_startswith(cgroup
, root
);
1396 if (p
&& p
> cgroup
)
1404 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1405 _cleanup_free_
char *raw
= NULL
;
1412 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1416 r
= cg_shift_path(raw
, root
, &c
);
1436 int cg_path_decode_unit(const char *cgroup
, char **unit
) {
1443 n
= strcspn(cgroup
, "/");
1447 c
= strndupa(cgroup
, n
);
1450 if (!unit_name_is_valid(c
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1461 static bool valid_slice_name(const char *p
, size_t n
) {
1466 if (n
< strlen("x.slice"))
1469 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1475 c
= cg_unescape(buf
);
1477 return unit_name_is_valid(c
, UNIT_NAME_PLAIN
);
1483 static const char *skip_slices(const char *p
) {
1486 /* Skips over all slice assignments */
1491 p
+= strspn(p
, "/");
1493 n
= strcspn(p
, "/");
1494 if (!valid_slice_name(p
, n
))
1501 int cg_path_get_unit(const char *path
, char **ret
) {
1509 e
= skip_slices(path
);
1511 r
= cg_path_decode_unit(e
, &unit
);
1515 /* We skipped over the slices, don't accept any now */
1516 if (endswith(unit
, ".slice")) {
1525 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1526 _cleanup_free_
char *cgroup
= NULL
;
1531 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1535 return cg_path_get_unit(cgroup
, unit
);
1539 * Skip session-*.scope, but require it to be there.
1541 static const char *skip_session(const char *p
) {
1547 p
+= strspn(p
, "/");
1549 n
= strcspn(p
, "/");
1550 if (n
< strlen("session-x.scope"))
1553 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1554 char buf
[n
- 8 - 6 + 1];
1556 memcpy(buf
, p
+ 8, n
- 8 - 6);
1559 /* Note that session scopes never need unescaping,
1560 * since they cannot conflict with the kernel's own
1561 * names, hence we don't need to call cg_unescape()
1564 if (!session_id_valid(buf
))
1568 p
+= strspn(p
, "/");
1576 * Skip user@*.service, but require it to be there.
1578 static const char *skip_user_manager(const char *p
) {
1584 p
+= strspn(p
, "/");
1586 n
= strcspn(p
, "/");
1587 if (n
< strlen("user@x.service"))
1590 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1591 char buf
[n
- 5 - 8 + 1];
1593 memcpy(buf
, p
+ 5, n
- 5 - 8);
1596 /* Note that user manager services never need unescaping,
1597 * since they cannot conflict with the kernel's own
1598 * names, hence we don't need to call cg_unescape()
1601 if (parse_uid(buf
, NULL
) < 0)
1605 p
+= strspn(p
, "/");
1613 static const char *skip_user_prefix(const char *path
) {
1618 /* Skip slices, if there are any */
1619 e
= skip_slices(path
);
1621 /* Skip the user manager, if it's in the path now... */
1622 t
= skip_user_manager(e
);
1626 /* Alternatively skip the user session if it is in the path... */
1627 return skip_session(e
);
1630 int cg_path_get_user_unit(const char *path
, char **ret
) {
1636 t
= skip_user_prefix(path
);
1640 /* And from here on it looks pretty much the same as for a
1641 * system unit, hence let's use the same parser from here
1643 return cg_path_get_unit(t
, ret
);
1646 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1647 _cleanup_free_
char *cgroup
= NULL
;
1652 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1656 return cg_path_get_user_unit(cgroup
, unit
);
1659 int cg_path_get_machine_name(const char *path
, char **machine
) {
1660 _cleanup_free_
char *u
= NULL
;
1664 r
= cg_path_get_unit(path
, &u
);
1668 sl
= strjoina("/run/systemd/machines/unit:", u
);
1669 return readlink_malloc(sl
, machine
);
1672 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1673 _cleanup_free_
char *cgroup
= NULL
;
1678 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1682 return cg_path_get_machine_name(cgroup
, machine
);
1685 int cg_path_get_session(const char *path
, char **session
) {
1686 _cleanup_free_
char *unit
= NULL
;
1692 r
= cg_path_get_unit(path
, &unit
);
1696 start
= startswith(unit
, "session-");
1699 end
= endswith(start
, ".scope");
1704 if (!session_id_valid(start
))
1720 int cg_pid_get_session(pid_t pid
, char **session
) {
1721 _cleanup_free_
char *cgroup
= NULL
;
1724 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1728 return cg_path_get_session(cgroup
, session
);
1731 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1732 _cleanup_free_
char *slice
= NULL
;
1738 r
= cg_path_get_slice(path
, &slice
);
1742 start
= startswith(slice
, "user-");
1745 end
= endswith(start
, ".slice");
1750 if (parse_uid(start
, uid
) < 0)
1756 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1757 _cleanup_free_
char *cgroup
= NULL
;
1760 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1764 return cg_path_get_owner_uid(cgroup
, uid
);
1767 int cg_path_get_slice(const char *p
, char **slice
) {
1768 const char *e
= NULL
;
1773 /* Finds the right-most slice unit from the beginning, but
1774 * stops before we come to the first non-slice unit. */
1779 p
+= strspn(p
, "/");
1781 n
= strcspn(p
, "/");
1782 if (!valid_slice_name(p
, n
)) {
1787 s
= strdup(SPECIAL_ROOT_SLICE
);
1795 return cg_path_decode_unit(e
, slice
);
1803 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1804 _cleanup_free_
char *cgroup
= NULL
;
1809 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1813 return cg_path_get_slice(cgroup
, slice
);
1816 int cg_path_get_user_slice(const char *p
, char **slice
) {
1821 t
= skip_user_prefix(p
);
1825 /* And now it looks pretty much the same as for a system
1826 * slice, so let's just use the same parser from here on. */
1827 return cg_path_get_slice(t
, slice
);
1830 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1831 _cleanup_free_
char *cgroup
= NULL
;
1836 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1840 return cg_path_get_user_slice(cgroup
, slice
);
1843 char *cg_escape(const char *p
) {
1844 bool need_prefix
= false;
1846 /* This implements very minimal escaping for names to be used
1847 * as file names in the cgroup tree: any name which might
1848 * conflict with a kernel name or is prefixed with '_' is
1849 * prefixed with a '_'. That way, when reading cgroup names it
1850 * is sufficient to remove a single prefixing underscore if
1853 /* The return value of this function (unlike cg_unescape())
1859 streq(p
, "notify_on_release") ||
1860 streq(p
, "release_agent") ||
1861 streq(p
, "tasks") ||
1862 startswith(p
, "cgroup."))
1867 dot
= strrchr(p
, '.');
1872 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
1875 n
= cgroup_controller_to_string(c
);
1880 if (memcmp(p
, n
, l
) != 0)
1890 return strappend("_", p
);
1895 char *cg_unescape(const char *p
) {
1898 /* The return value of this function (unlike cg_escape())
1899 * doesn't need free()! */
1907 #define CONTROLLER_VALID \
1911 bool cg_controller_is_valid(const char *p
) {
1917 if (streq(p
, SYSTEMD_CGROUP_CONTROLLER
))
1920 s
= startswith(p
, "name=");
1924 if (*p
== 0 || *p
== '_')
1927 for (t
= p
; *t
; t
++)
1928 if (!strchr(CONTROLLER_VALID
, *t
))
1931 if (t
- p
> FILENAME_MAX
)
1937 int cg_slice_to_path(const char *unit
, char **ret
) {
1938 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1945 if (streq(unit
, SPECIAL_ROOT_SLICE
)) {
1955 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1958 if (!endswith(unit
, ".slice"))
1961 r
= unit_name_to_prefix(unit
, &p
);
1965 dash
= strchr(p
, '-');
1967 /* Don't allow initial dashes */
1972 _cleanup_free_
char *escaped
= NULL
;
1973 char n
[dash
- p
+ sizeof(".slice")];
1975 /* Don't allow trailing or double dashes */
1976 if (dash
[1] == 0 || dash
[1] == '-')
1979 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1980 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1983 escaped
= cg_escape(n
);
1987 if (!strextend(&s
, escaped
, "/", NULL
))
1990 dash
= strchr(dash
+1, '-');
1993 e
= cg_escape(unit
);
1997 if (!strextend(&s
, e
, NULL
))
2006 int cg_set_attribute(const char *controller
, const char *path
, const char *attribute
, const char *value
) {
2007 _cleanup_free_
char *p
= NULL
;
2010 r
= cg_get_path(controller
, path
, attribute
, &p
);
2014 return write_string_file(p
, value
, 0);
2017 int cg_get_attribute(const char *controller
, const char *path
, const char *attribute
, char **ret
) {
2018 _cleanup_free_
char *p
= NULL
;
2021 r
= cg_get_path(controller
, path
, attribute
, &p
);
2025 return read_one_line_file(p
, ret
);
2028 int cg_get_keyed_attribute(const char *controller
, const char *path
, const char *attribute
, const char **keys
, char **values
) {
2029 _cleanup_free_
char *filename
= NULL
, *content
= NULL
;
2033 for (i
= 0; keys
[i
]; i
++)
2036 r
= cg_get_path(controller
, path
, attribute
, &filename
);
2040 r
= read_full_file(filename
, &content
, NULL
);
2045 while ((line
= strsep(&p
, "\n"))) {
2048 key
= strsep(&line
, " ");
2050 for (i
= 0; keys
[i
]; i
++) {
2051 if (streq(key
, keys
[i
])) {
2052 values
[i
] = strdup(line
);
2058 for (i
= 0; keys
[i
]; i
++) {
2060 for (i
= 0; keys
[i
]; i
++) {
2071 int cg_create_everywhere(CGroupMask supported
, CGroupMask mask
, const char *path
) {
2075 /* This one will create a cgroup in our private tree, but also
2076 * duplicate it in the trees specified in mask, and remove it
2079 /* First create the cgroup in our own hierarchy. */
2080 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
2084 /* If we are in the unified hierarchy, we are done now */
2085 r
= cg_all_unified();
2091 /* Otherwise, do the same in the other hierarchies */
2092 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2093 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2096 n
= cgroup_controller_to_string(c
);
2099 (void) cg_create(n
, path
);
2100 else if (supported
& bit
)
2101 (void) cg_trim(n
, path
, true);
2107 int cg_attach_everywhere(CGroupMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
2111 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
2115 r
= cg_all_unified();
2121 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2122 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2123 const char *p
= NULL
;
2125 if (!(supported
& bit
))
2129 p
= path_callback(bit
, userdata
);
2134 (void) cg_attach_fallback(cgroup_controller_to_string(c
), p
, pid
);
2140 int cg_attach_many_everywhere(CGroupMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
2145 SET_FOREACH(pidp
, pids
, i
) {
2146 pid_t pid
= PTR_TO_PID(pidp
);
2149 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
2150 if (q
< 0 && r
>= 0)
2157 int cg_migrate_everywhere(CGroupMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
2161 if (!path_equal(from
, to
)) {
2162 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, CGROUP_REMOVE
);
2167 q
= cg_all_unified();
2173 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2174 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2175 const char *p
= NULL
;
2177 if (!(supported
& bit
))
2181 p
= to_callback(bit
, userdata
);
2186 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, cgroup_controller_to_string(c
), p
, 0);
2192 int cg_trim_everywhere(CGroupMask supported
, const char *path
, bool delete_root
) {
2196 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
2200 q
= cg_all_unified();
2206 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2207 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2209 if (!(supported
& bit
))
2212 (void) cg_trim(cgroup_controller_to_string(c
), path
, delete_root
);
2218 int cg_mask_to_string(CGroupMask mask
, char **ret
) {
2219 const char *controllers
[_CGROUP_CONTROLLER_MAX
+ 1];
2231 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2233 if (!(mask
& CGROUP_CONTROLLER_TO_MASK(c
)))
2236 controllers
[i
++] = cgroup_controller_to_string(c
);
2237 controllers
[i
] = NULL
;
2240 s
= strv_join((char **)controllers
, NULL
);
2248 int cg_mask_from_string(const char *value
, CGroupMask
*mask
) {
2253 _cleanup_free_
char *n
= NULL
;
2257 r
= extract_first_word(&value
, &n
, NULL
, 0);
2263 v
= cgroup_controller_from_string(n
);
2267 *mask
|= CGROUP_CONTROLLER_TO_MASK(v
);
2272 int cg_mask_supported(CGroupMask
*ret
) {
2273 CGroupMask mask
= 0;
2276 /* Determines the mask of supported cgroup controllers. Only
2277 * includes controllers we can make sense of and that are
2278 * actually accessible. */
2280 r
= cg_all_unified();
2284 _cleanup_free_
char *root
= NULL
, *controllers
= NULL
, *path
= NULL
;
2286 /* In the unified hierarchy we can read the supported
2287 * and accessible controllers from a the top-level
2288 * cgroup attribute */
2290 r
= cg_get_root_path(&root
);
2294 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, root
, "cgroup.controllers", &path
);
2298 r
= read_one_line_file(path
, &controllers
);
2302 r
= cg_mask_from_string(controllers
, &mask
);
2306 /* Currently, we support the cpu, memory, io and pids
2307 * controller in the unified hierarchy, mask
2308 * everything else off. */
2309 mask
&= CGROUP_MASK_CPU
| CGROUP_MASK_MEMORY
| CGROUP_MASK_IO
| CGROUP_MASK_PIDS
;
2314 /* In the legacy hierarchy, we check whether which
2315 * hierarchies are mounted. */
2317 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2320 n
= cgroup_controller_to_string(c
);
2321 if (controller_is_accessible(n
) >= 0)
2322 mask
|= CGROUP_CONTROLLER_TO_MASK(c
);
2330 int cg_kernel_controllers(Set
*controllers
) {
2331 _cleanup_fclose_
FILE *f
= NULL
;
2335 assert(controllers
);
2337 /* Determines the full list of kernel-known controllers. Might
2338 * include controllers we don't actually support, arbitrary
2339 * named hierarchies and controllers that aren't currently
2340 * accessible (because not mounted). */
2342 f
= fopen("/proc/cgroups", "re");
2344 if (errno
== ENOENT
)
2349 /* Ignore the header line */
2350 (void) fgets(buf
, sizeof(buf
), f
);
2357 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
2362 if (ferror(f
) && errno
> 0)
2373 if (!cg_controller_is_valid(controller
)) {
2378 r
= set_consume(controllers
, controller
);
2386 static thread_local CGroupUnified unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2388 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2389 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2390 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2391 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2393 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2394 * process management but disable the compat dual layout, we return %true on
2395 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2397 static thread_local
bool unified_systemd_v232
;
2399 static int cg_unified_update(void) {
2403 /* Checks if we support the unified hierarchy. Returns an
2404 * error when the cgroup hierarchies aren't mounted yet or we
2405 * have any other trouble determining if the unified hierarchy
2408 if (unified_cache
>= CGROUP_UNIFIED_NONE
)
2411 if (statfs("/sys/fs/cgroup/", &fs
) < 0)
2414 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
))
2415 unified_cache
= CGROUP_UNIFIED_ALL
;
2416 else if (F_TYPE_EQUAL(fs
.f_type
, TMPFS_MAGIC
)) {
2417 if (statfs("/sys/fs/cgroup/unified/", &fs
) == 0 &&
2418 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2419 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2420 unified_systemd_v232
= false;
2421 } else if (statfs("/sys/fs/cgroup/systemd/", &fs
) == 0 &&
2422 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2423 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2424 unified_systemd_v232
= true;
2426 if (statfs("/sys/fs/cgroup/systemd/", &fs
) < 0)
2428 if (!F_TYPE_EQUAL(fs
.f_type
, CGROUP_SUPER_MAGIC
))
2430 unified_cache
= CGROUP_UNIFIED_NONE
;
2438 int cg_unified_controller(const char *controller
) {
2441 r
= cg_unified_update();
2445 if (unified_cache
== CGROUP_UNIFIED_NONE
)
2448 if (unified_cache
>= CGROUP_UNIFIED_ALL
)
2451 return streq_ptr(controller
, SYSTEMD_CGROUP_CONTROLLER
);
2454 int cg_all_unified(void) {
2457 r
= cg_unified_update();
2461 return unified_cache
>= CGROUP_UNIFIED_ALL
;
2464 int cg_hybrid_unified(void) {
2467 r
= cg_unified_update();
2471 return unified_cache
== CGROUP_UNIFIED_SYSTEMD
&& !unified_systemd_v232
;
2474 int cg_unified_flush(void) {
2475 unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2477 return cg_unified_update();
2480 int cg_enable_everywhere(CGroupMask supported
, CGroupMask mask
, const char *p
) {
2481 _cleanup_free_
char *fs
= NULL
;
2490 r
= cg_all_unified();
2493 if (r
== 0) /* on the legacy hiearchy there's no joining of controllers defined */
2496 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, p
, "cgroup.subtree_control", &fs
);
2500 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2501 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2504 if (!(supported
& bit
))
2507 n
= cgroup_controller_to_string(c
);
2509 char s
[1 + strlen(n
) + 1];
2511 s
[0] = mask
& bit
? '+' : '-';
2514 r
= write_string_file(fs
, s
, 0);
2516 log_debug_errno(r
, "Failed to enable controller %s for %s (%s): %m", n
, p
, fs
);
2523 bool cg_is_unified_wanted(void) {
2524 static thread_local
int wanted
= -1;
2527 const bool is_default
= DEFAULT_HIERARCHY
== CGROUP_UNIFIED_ALL
;
2529 /* If we have a cached value, return that. */
2533 /* If the hierarchy is already mounted, then follow whatever
2534 * was chosen for it. */
2535 if (cg_unified_flush() >= 0)
2536 return (wanted
= unified_cache
>= CGROUP_UNIFIED_ALL
);
2538 /* Otherwise, let's see what the kernel command line has to say.
2539 * Since checking is expensive, cache a non-error result. */
2540 r
= proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b
);
2542 return (wanted
= r
> 0 ? b
: is_default
);
2545 bool cg_is_legacy_wanted(void) {
2546 static thread_local
int wanted
= -1;
2548 /* If we have a cached value, return that. */
2552 /* Check if we have cgroups2 already mounted. */
2553 if (cg_unified_flush() >= 0 &&
2554 unified_cache
== CGROUP_UNIFIED_ALL
)
2555 return (wanted
= false);
2557 /* Otherwise, assume that at least partial legacy is wanted,
2558 * since cgroups2 should already be mounted at this point. */
2559 return (wanted
= true);
2562 bool cg_is_hybrid_wanted(void) {
2563 static thread_local
int wanted
= -1;
2566 const bool is_default
= DEFAULT_HIERARCHY
>= CGROUP_UNIFIED_SYSTEMD
;
2567 /* We default to true if the default is "hybrid", obviously,
2568 * but also when the default is "unified", because if we get
2569 * called, it means that unified hierarchy was not mounted. */
2571 /* If we have a cached value, return that. */
2575 /* If the hierarchy is already mounted, then follow whatever
2576 * was chosen for it. */
2577 if (cg_unified_flush() >= 0 &&
2578 unified_cache
== CGROUP_UNIFIED_ALL
)
2579 return (wanted
= false);
2581 /* Otherwise, let's see what the kernel command line has to say.
2582 * Since checking is expensive, cache a non-error result. */
2583 r
= proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b
);
2585 /* The meaning of the kernel option is reversed wrt. to the return value
2586 * of this function, hence the negation. */
2587 return (wanted
= r
> 0 ? !b
: is_default
);
2590 int cg_weight_parse(const char *s
, uint64_t *ret
) {
2595 *ret
= CGROUP_WEIGHT_INVALID
;
2599 r
= safe_atou64(s
, &u
);
2603 if (u
< CGROUP_WEIGHT_MIN
|| u
> CGROUP_WEIGHT_MAX
)
2610 const uint64_t cgroup_io_limit_defaults
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2611 [CGROUP_IO_RBPS_MAX
] = CGROUP_LIMIT_MAX
,
2612 [CGROUP_IO_WBPS_MAX
] = CGROUP_LIMIT_MAX
,
2613 [CGROUP_IO_RIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2614 [CGROUP_IO_WIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2617 static const char* const cgroup_io_limit_type_table
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2618 [CGROUP_IO_RBPS_MAX
] = "IOReadBandwidthMax",
2619 [CGROUP_IO_WBPS_MAX
] = "IOWriteBandwidthMax",
2620 [CGROUP_IO_RIOPS_MAX
] = "IOReadIOPSMax",
2621 [CGROUP_IO_WIOPS_MAX
] = "IOWriteIOPSMax",
2624 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type
, CGroupIOLimitType
);
2626 int cg_cpu_shares_parse(const char *s
, uint64_t *ret
) {
2631 *ret
= CGROUP_CPU_SHARES_INVALID
;
2635 r
= safe_atou64(s
, &u
);
2639 if (u
< CGROUP_CPU_SHARES_MIN
|| u
> CGROUP_CPU_SHARES_MAX
)
2646 int cg_blkio_weight_parse(const char *s
, uint64_t *ret
) {
2651 *ret
= CGROUP_BLKIO_WEIGHT_INVALID
;
2655 r
= safe_atou64(s
, &u
);
2659 if (u
< CGROUP_BLKIO_WEIGHT_MIN
|| u
> CGROUP_BLKIO_WEIGHT_MAX
)
2666 bool is_cgroup_fs(const struct statfs
*s
) {
2667 return is_fs_type(s
, CGROUP_SUPER_MAGIC
) ||
2668 is_fs_type(s
, CGROUP2_SUPER_MAGIC
);
2671 bool fd_is_cgroup_fs(int fd
) {
2674 if (fstatfs(fd
, &s
) < 0)
2677 return is_cgroup_fs(&s
);
2680 static const char *cgroup_controller_table
[_CGROUP_CONTROLLER_MAX
] = {
2681 [CGROUP_CONTROLLER_CPU
] = "cpu",
2682 [CGROUP_CONTROLLER_CPUACCT
] = "cpuacct",
2683 [CGROUP_CONTROLLER_IO
] = "io",
2684 [CGROUP_CONTROLLER_BLKIO
] = "blkio",
2685 [CGROUP_CONTROLLER_MEMORY
] = "memory",
2686 [CGROUP_CONTROLLER_DEVICES
] = "devices",
2687 [CGROUP_CONTROLLER_PIDS
] = "pids",
2690 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller
, CGroupController
);