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
;
106 int cg_read_event(const char *controller
, const char *path
, const char *event
,
109 _cleanup_free_
char *events
= NULL
, *content
= NULL
;
113 r
= cg_get_path(controller
, path
, "cgroup.events", &events
);
117 r
= read_full_file(events
, &content
, NULL
);
122 while ((line
= strsep(&p
, "\n"))) {
125 key
= strsep(&line
, " ");
129 if (strcmp(key
, event
))
139 bool cg_ns_supported(void) {
140 static thread_local
int enabled
= -1;
145 if (access("/proc/self/ns/cgroup", F_OK
) == 0)
153 int cg_enumerate_subgroups(const char *controller
, const char *path
, DIR **_d
) {
154 _cleanup_free_
char *fs
= NULL
;
160 /* This is not recursive! */
162 r
= cg_get_path(controller
, path
, NULL
, &fs
);
174 int cg_read_subgroup(DIR *d
, char **fn
) {
180 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
183 if (de
->d_type
!= DT_DIR
)
186 if (dot_or_dot_dot(de
->d_name
))
189 b
= strdup(de
->d_name
);
200 int cg_rmdir(const char *controller
, const char *path
) {
201 _cleanup_free_
char *p
= NULL
;
204 r
= cg_get_path(controller
, path
, NULL
, &p
);
209 if (r
< 0 && errno
!= ENOENT
)
212 r
= cg_hybrid_unified();
218 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
219 r
= cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
221 log_warning_errno(r
, "Failed to remove compat systemd cgroup %s: %m", path
);
228 const char *controller
,
233 cg_kill_log_func_t log_kill
,
236 _cleanup_set_free_ Set
*allocated_set
= NULL
;
243 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
244 * SIGCONT on SIGKILL. */
245 if (IN_SET(sig
, SIGCONT
, SIGKILL
))
246 flags
&= ~CGROUP_SIGCONT
;
248 /* This goes through the tasks list and kills them all. This
249 * is repeated until no further processes are added to the
250 * tasks list, to properly handle forking processes */
253 s
= allocated_set
= set_new(NULL
);
258 my_pid
= getpid_cached();
261 _cleanup_fclose_
FILE *f
= NULL
;
265 r
= cg_enumerate_processes(controller
, path
, &f
);
267 if (ret
>= 0 && r
!= -ENOENT
)
273 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
275 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
278 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
282 log_kill(pid
, sig
, userdata
);
284 /* If we haven't killed this process yet, kill
286 if (kill(pid
, sig
) < 0) {
287 if (ret
>= 0 && errno
!= ESRCH
)
290 if (flags
& CGROUP_SIGCONT
)
291 (void) kill(pid
, SIGCONT
);
299 r
= set_put(s
, PID_TO_PTR(pid
));
315 /* To avoid racing against processes which fork
316 * quicker than we can kill them we repeat this until
317 * no new pids need to be killed. */
324 int cg_kill_recursive(
325 const char *controller
,
330 cg_kill_log_func_t log_kill
,
333 _cleanup_set_free_ Set
*allocated_set
= NULL
;
334 _cleanup_closedir_
DIR *d
= NULL
;
342 s
= allocated_set
= set_new(NULL
);
347 ret
= cg_kill(controller
, path
, sig
, flags
, s
, log_kill
, userdata
);
349 r
= cg_enumerate_subgroups(controller
, path
, &d
);
351 if (ret
>= 0 && r
!= -ENOENT
)
357 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
358 _cleanup_free_
char *p
= NULL
;
360 p
= strjoin(path
, "/", fn
);
365 r
= cg_kill_recursive(controller
, p
, sig
, flags
, s
, log_kill
, userdata
);
366 if (r
!= 0 && ret
>= 0)
369 if (ret
>= 0 && r
< 0)
372 if (flags
& CGROUP_REMOVE
) {
373 r
= cg_rmdir(controller
, path
);
374 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
389 _cleanup_set_free_ Set
*s
= NULL
;
402 my_pid
= getpid_cached();
405 _cleanup_fclose_
FILE *f
= NULL
;
409 r
= cg_enumerate_processes(cfrom
, pfrom
, &f
);
411 if (ret
>= 0 && r
!= -ENOENT
)
417 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
419 /* This might do weird stuff if we aren't a
420 * single-threaded program. However, we
421 * luckily know we are not */
422 if ((flags
& CGROUP_IGNORE_SELF
) && pid
== my_pid
)
425 if (set_get(s
, PID_TO_PTR(pid
)) == PID_TO_PTR(pid
))
428 /* Ignore kernel threads. Since they can only
429 * exist in the root cgroup, we only check for
432 (isempty(pfrom
) || path_equal(pfrom
, "/")) &&
433 is_kernel_thread(pid
) > 0)
436 r
= cg_attach(cto
, pto
, pid
);
438 if (ret
>= 0 && r
!= -ESRCH
)
445 r
= set_put(s
, PID_TO_PTR(pid
));
465 int cg_migrate_recursive(
472 _cleanup_closedir_
DIR *d
= NULL
;
481 ret
= cg_migrate(cfrom
, pfrom
, cto
, pto
, flags
);
483 r
= cg_enumerate_subgroups(cfrom
, pfrom
, &d
);
485 if (ret
>= 0 && r
!= -ENOENT
)
491 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
492 _cleanup_free_
char *p
= NULL
;
494 p
= strjoin(pfrom
, "/", fn
);
499 r
= cg_migrate_recursive(cfrom
, p
, cto
, pto
, flags
);
500 if (r
!= 0 && ret
>= 0)
504 if (r
< 0 && ret
>= 0)
507 if (flags
& CGROUP_REMOVE
) {
508 r
= cg_rmdir(cfrom
, pfrom
);
509 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
516 int cg_migrate_recursive_fallback(
530 r
= cg_migrate_recursive(cfrom
, pfrom
, cto
, pto
, flags
);
532 char prefix
[strlen(pto
) + 1];
534 /* This didn't work? Then let's try all prefixes of the destination */
536 PATH_FOREACH_PREFIX(prefix
, pto
) {
539 q
= cg_migrate_recursive(cfrom
, pfrom
, cto
, prefix
, flags
);
548 static const char *controller_to_dirname(const char *controller
) {
553 /* Converts a controller name to the directory name below
554 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
555 * just cuts off the name= prefixed used for named
556 * hierarchies, if it is specified. */
558 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
559 if (cg_hybrid_unified() > 0)
560 controller
= SYSTEMD_CGROUP_CONTROLLER_HYBRID
;
562 controller
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
565 e
= startswith(controller
, "name=");
572 static int join_path_legacy(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
579 dn
= controller_to_dirname(controller
);
581 if (isempty(path
) && isempty(suffix
))
582 t
= strappend("/sys/fs/cgroup/", dn
);
583 else if (isempty(path
))
584 t
= strjoin("/sys/fs/cgroup/", dn
, "/", suffix
);
585 else if (isempty(suffix
))
586 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
);
588 t
= strjoin("/sys/fs/cgroup/", dn
, "/", path
, "/", suffix
);
596 static int join_path_unified(const char *path
, const char *suffix
, char **fs
) {
601 if (isempty(path
) && isempty(suffix
))
602 t
= strdup("/sys/fs/cgroup");
603 else if (isempty(path
))
604 t
= strappend("/sys/fs/cgroup/", suffix
);
605 else if (isempty(suffix
))
606 t
= strappend("/sys/fs/cgroup/", path
);
608 t
= strjoin("/sys/fs/cgroup/", path
, "/", suffix
);
616 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
624 /* If no controller is specified, we return the path
625 * *below* the controllers, without any prefix. */
627 if (!path
&& !suffix
)
635 t
= strjoin(path
, "/", suffix
);
639 *fs
= path_kill_slashes(t
);
643 if (!cg_controller_is_valid(controller
))
646 r
= cg_all_unified();
650 r
= join_path_unified(path
, suffix
, fs
);
652 r
= join_path_legacy(controller
, path
, suffix
, fs
);
656 path_kill_slashes(*fs
);
660 static int controller_is_accessible(const char *controller
) {
665 /* Checks whether a specific controller is accessible,
666 * i.e. its hierarchy mounted. In the unified hierarchy all
667 * controllers are considered accessible, except for the named
670 if (!cg_controller_is_valid(controller
))
673 r
= cg_all_unified();
677 /* We don't support named hierarchies if we are using
678 * the unified hierarchy. */
680 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
683 if (startswith(controller
, "name="))
689 dn
= controller_to_dirname(controller
);
690 cc
= strjoina("/sys/fs/cgroup/", dn
);
692 if (laccess(cc
, F_OK
) < 0)
699 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
705 /* Check if the specified controller is actually accessible */
706 r
= controller_is_accessible(controller
);
710 return cg_get_path(controller
, path
, suffix
, fs
);
713 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
718 if (typeflag
!= FTW_DP
)
721 if (ftwbuf
->level
< 1)
728 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
729 _cleanup_free_
char *fs
= NULL
;
734 r
= cg_get_path(controller
, path
, NULL
, &fs
);
739 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0) {
749 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
753 q
= cg_hybrid_unified();
756 if (q
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
757 q
= cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, delete_root
);
759 log_warning_errno(q
, "Failed to trim compat systemd cgroup %s: %m", path
);
765 int cg_create(const char *controller
, const char *path
) {
766 _cleanup_free_
char *fs
= NULL
;
769 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
773 r
= mkdir_parents(fs
, 0755);
777 if (mkdir(fs
, 0755) < 0) {
785 r
= cg_hybrid_unified();
789 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
790 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
);
792 log_warning_errno(r
, "Failed to create compat systemd cgroup %s: %m", path
);
798 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
803 r
= cg_create(controller
, path
);
807 q
= cg_attach(controller
, path
, pid
);
811 /* This does not remove the cgroup on failure */
815 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
816 _cleanup_free_
char *fs
= NULL
;
817 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
823 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
828 pid
= getpid_cached();
830 xsprintf(c
, PID_FMT
"\n", pid
);
832 r
= write_string_file(fs
, c
, 0);
836 r
= cg_hybrid_unified();
840 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
841 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, pid
);
843 log_warning_errno(r
, "Failed to attach "PID_FMT
" to compat systemd cgroup %s: %m", pid
, path
);
849 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
856 r
= cg_attach(controller
, path
, pid
);
858 char prefix
[strlen(path
) + 1];
860 /* This didn't work? Then let's try all prefixes of
863 PATH_FOREACH_PREFIX(prefix
, path
) {
866 q
= cg_attach(controller
, prefix
, pid
);
875 int cg_set_group_access(
876 const char *controller
,
882 _cleanup_free_
char *fs
= NULL
;
885 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
888 if (mode
!= MODE_INVALID
)
891 r
= cg_get_path(controller
, path
, NULL
, &fs
);
895 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
899 r
= cg_hybrid_unified();
902 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
903 r
= cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, mode
, uid
, gid
);
905 log_warning_errno(r
, "Failed to set group access on compat systemd cgroup %s: %m", path
);
911 int cg_set_task_access(
912 const char *controller
,
918 _cleanup_free_
char *fs
= NULL
, *procs
= NULL
;
923 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
926 if (mode
!= MODE_INVALID
)
929 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
933 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
937 r
= cg_unified_controller(controller
);
941 /* Compatibility, Always keep values for "tasks" in sync with
943 if (cg_get_path(controller
, path
, "tasks", &procs
) >= 0)
944 (void) chmod_and_chown(procs
, mode
, uid
, gid
);
947 r
= cg_hybrid_unified();
950 if (r
> 0 && streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
951 r
= cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, mode
, uid
, gid
);
953 log_warning_errno(r
, "Failed to set task access on compat systemd cgroup %s: %m", path
);
959 int cg_set_xattr(const char *controller
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
) {
960 _cleanup_free_
char *fs
= NULL
;
965 assert(value
|| size
<= 0);
967 r
= cg_get_path(controller
, path
, NULL
, &fs
);
971 if (setxattr(fs
, name
, value
, size
, flags
) < 0)
977 int cg_get_xattr(const char *controller
, const char *path
, const char *name
, void *value
, size_t size
) {
978 _cleanup_free_
char *fs
= NULL
;
985 r
= cg_get_path(controller
, path
, NULL
, &fs
);
989 n
= getxattr(fs
, name
, value
, size
);
996 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
997 _cleanup_fclose_
FILE *f
= NULL
;
999 const char *fs
, *controller_str
;
1007 if (!cg_controller_is_valid(controller
))
1010 controller
= SYSTEMD_CGROUP_CONTROLLER
;
1012 unified
= cg_unified_controller(controller
);
1016 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
1017 controller_str
= SYSTEMD_CGROUP_CONTROLLER_LEGACY
;
1019 controller_str
= controller
;
1021 cs
= strlen(controller_str
);
1024 fs
= procfs_file_alloca(pid
, "cgroup");
1025 f
= fopen(fs
, "re");
1027 return errno
== ENOENT
? -ESRCH
: -errno
;
1029 FOREACH_LINE(line
, f
, return -errno
) {
1035 e
= startswith(line
, "0:");
1045 const char *word
, *state
;
1048 l
= strchr(line
, ':');
1058 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
) {
1059 if (k
== cs
&& memcmp(word
, controller_str
, cs
) == 0) {
1080 int cg_install_release_agent(const char *controller
, const char *agent
) {
1081 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
1087 r
= cg_unified_controller(controller
);
1090 if (r
> 0) /* doesn't apply to unified hierarchy */
1093 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1097 r
= read_one_line_file(fs
, &contents
);
1101 sc
= strstrip(contents
);
1103 r
= write_string_file(fs
, agent
, 0);
1106 } else if (!path_equal(sc
, agent
))
1110 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1114 contents
= mfree(contents
);
1115 r
= read_one_line_file(fs
, &contents
);
1119 sc
= strstrip(contents
);
1120 if (streq(sc
, "0")) {
1121 r
= write_string_file(fs
, "1", 0);
1128 if (!streq(sc
, "1"))
1134 int cg_uninstall_release_agent(const char *controller
) {
1135 _cleanup_free_
char *fs
= NULL
;
1138 r
= cg_unified_controller(controller
);
1141 if (r
> 0) /* Doesn't apply to unified hierarchy */
1144 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
1148 r
= write_string_file(fs
, "0", 0);
1154 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
1158 r
= write_string_file(fs
, "", 0);
1165 int cg_is_empty(const char *controller
, const char *path
) {
1166 _cleanup_fclose_
FILE *f
= NULL
;
1172 r
= cg_enumerate_processes(controller
, path
, &f
);
1178 r
= cg_read_pid(f
, &pid
);
1185 int cg_is_empty_recursive(const char *controller
, const char *path
) {
1190 /* The root cgroup is always populated */
1191 if (controller
&& (isempty(path
) || path_equal(path
, "/")))
1194 r
= cg_unified_controller(controller
);
1198 _cleanup_free_
char *t
= NULL
;
1200 /* On the unified hierarchy we can check empty state
1201 * via the "populated" attribute of "cgroup.events". */
1203 r
= cg_read_event(controller
, path
, "populated", &t
);
1207 return streq(t
, "0");
1209 _cleanup_closedir_
DIR *d
= NULL
;
1212 r
= cg_is_empty(controller
, path
);
1216 r
= cg_enumerate_subgroups(controller
, path
, &d
);
1222 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
1223 _cleanup_free_
char *p
= NULL
;
1225 p
= strjoin(path
, "/", fn
);
1230 r
= cg_is_empty_recursive(controller
, p
);
1241 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
1242 char *t
= NULL
, *u
= NULL
;
1248 if (!path_is_safe(spec
))
1256 *path
= path_kill_slashes(t
);
1265 e
= strchr(spec
, ':');
1267 if (!cg_controller_is_valid(spec
))
1284 t
= strndup(spec
, e
-spec
);
1287 if (!cg_controller_is_valid(t
)) {
1301 if (!path_is_safe(u
) ||
1302 !path_is_absolute(u
)) {
1308 path_kill_slashes(u
);
1324 int cg_mangle_path(const char *path
, char **result
) {
1325 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1332 /* First, check if it already is a filesystem path */
1333 if (path_startswith(path
, "/sys/fs/cgroup")) {
1339 *result
= path_kill_slashes(t
);
1343 /* Otherwise, treat it as cg spec */
1344 r
= cg_split_spec(path
, &c
, &p
);
1348 return cg_get_path(c
?: SYSTEMD_CGROUP_CONTROLLER
, p
?: "/", NULL
, result
);
1351 int cg_get_root_path(char **path
) {
1357 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1361 e
= endswith(p
, "/" SPECIAL_INIT_SCOPE
);
1363 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
); /* legacy */
1365 e
= endswith(p
, "/system"); /* even more legacy */
1373 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1374 _cleanup_free_
char *rt
= NULL
;
1382 /* If the root was specified let's use that, otherwise
1383 * let's determine it from PID 1 */
1385 r
= cg_get_root_path(&rt
);
1392 p
= path_startswith(cgroup
, root
);
1393 if (p
&& p
> cgroup
)
1401 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1402 _cleanup_free_
char *raw
= NULL
;
1409 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1413 r
= cg_shift_path(raw
, root
, &c
);
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())
1856 streq(p
, "notify_on_release") ||
1857 streq(p
, "release_agent") ||
1858 streq(p
, "tasks") ||
1859 startswith(p
, "cgroup."))
1864 dot
= strrchr(p
, '.');
1869 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
1872 n
= cgroup_controller_to_string(c
);
1877 if (memcmp(p
, n
, l
) != 0)
1887 return strappend("_", p
);
1892 char *cg_unescape(const char *p
) {
1895 /* The return value of this function (unlike cg_escape())
1896 * doesn't need free()! */
1904 #define CONTROLLER_VALID \
1908 bool cg_controller_is_valid(const char *p
) {
1914 if (streq(p
, SYSTEMD_CGROUP_CONTROLLER
))
1917 s
= startswith(p
, "name=");
1921 if (*p
== 0 || *p
== '_')
1924 for (t
= p
; *t
; t
++)
1925 if (!strchr(CONTROLLER_VALID
, *t
))
1928 if (t
- p
> FILENAME_MAX
)
1934 int cg_slice_to_path(const char *unit
, char **ret
) {
1935 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1942 if (streq(unit
, SPECIAL_ROOT_SLICE
)) {
1952 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1955 if (!endswith(unit
, ".slice"))
1958 r
= unit_name_to_prefix(unit
, &p
);
1962 dash
= strchr(p
, '-');
1964 /* Don't allow initial dashes */
1969 _cleanup_free_
char *escaped
= NULL
;
1970 char n
[dash
- p
+ sizeof(".slice")];
1972 /* Don't allow trailing or double dashes */
1973 if (dash
[1] == 0 || dash
[1] == '-')
1976 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1977 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1980 escaped
= cg_escape(n
);
1984 if (!strextend(&s
, escaped
, "/", NULL
))
1987 dash
= strchr(dash
+1, '-');
1990 e
= cg_escape(unit
);
1994 if (!strextend(&s
, e
, NULL
))
2003 int cg_set_attribute(const char *controller
, const char *path
, const char *attribute
, const char *value
) {
2004 _cleanup_free_
char *p
= NULL
;
2007 r
= cg_get_path(controller
, path
, attribute
, &p
);
2011 return write_string_file(p
, value
, 0);
2014 int cg_get_attribute(const char *controller
, const char *path
, const char *attribute
, char **ret
) {
2015 _cleanup_free_
char *p
= NULL
;
2018 r
= cg_get_path(controller
, path
, attribute
, &p
);
2022 return read_one_line_file(p
, ret
);
2025 int cg_get_keyed_attribute(const char *controller
, const char *path
, const char *attribute
, const char **keys
, char **values
) {
2026 _cleanup_free_
char *filename
= NULL
, *content
= NULL
;
2030 for (i
= 0; keys
[i
]; i
++)
2033 r
= cg_get_path(controller
, path
, attribute
, &filename
);
2037 r
= read_full_file(filename
, &content
, NULL
);
2042 while ((line
= strsep(&p
, "\n"))) {
2045 key
= strsep(&line
, " ");
2047 for (i
= 0; keys
[i
]; i
++) {
2048 if (streq(key
, keys
[i
])) {
2049 values
[i
] = strdup(line
);
2055 for (i
= 0; keys
[i
]; i
++) {
2057 for (i
= 0; keys
[i
]; i
++) {
2068 int cg_create_everywhere(CGroupMask supported
, CGroupMask mask
, const char *path
) {
2072 /* This one will create a cgroup in our private tree, but also
2073 * duplicate it in the trees specified in mask, and remove it
2076 /* First create the cgroup in our own hierarchy. */
2077 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
2081 /* If we are in the unified hierarchy, we are done now */
2082 r
= cg_all_unified();
2088 /* Otherwise, do the same in the other hierarchies */
2089 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2090 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2093 n
= cgroup_controller_to_string(c
);
2096 (void) cg_create(n
, path
);
2097 else if (supported
& bit
)
2098 (void) cg_trim(n
, path
, true);
2104 int cg_attach_everywhere(CGroupMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
2108 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
2112 r
= cg_all_unified();
2118 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2119 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2120 const char *p
= NULL
;
2122 if (!(supported
& bit
))
2126 p
= path_callback(bit
, userdata
);
2131 (void) cg_attach_fallback(cgroup_controller_to_string(c
), p
, pid
);
2137 int cg_attach_many_everywhere(CGroupMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
2142 SET_FOREACH(pidp
, pids
, i
) {
2143 pid_t pid
= PTR_TO_PID(pidp
);
2146 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
2147 if (q
< 0 && r
>= 0)
2154 int cg_migrate_everywhere(CGroupMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
2158 if (!path_equal(from
, to
)) {
2159 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, CGROUP_REMOVE
);
2164 q
= cg_all_unified();
2170 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2171 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2172 const char *p
= NULL
;
2174 if (!(supported
& bit
))
2178 p
= to_callback(bit
, userdata
);
2183 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, cgroup_controller_to_string(c
), p
, 0);
2189 int cg_trim_everywhere(CGroupMask supported
, const char *path
, bool delete_root
) {
2193 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
2197 q
= cg_all_unified();
2203 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2204 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2206 if (!(supported
& bit
))
2209 (void) cg_trim(cgroup_controller_to_string(c
), path
, delete_root
);
2215 int cg_mask_to_string(CGroupMask mask
, char **ret
) {
2216 const char *controllers
[_CGROUP_CONTROLLER_MAX
+ 1];
2228 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2230 if (!(mask
& CGROUP_CONTROLLER_TO_MASK(c
)))
2233 controllers
[i
++] = cgroup_controller_to_string(c
);
2234 controllers
[i
] = NULL
;
2237 s
= strv_join((char **)controllers
, NULL
);
2245 int cg_mask_from_string(const char *value
, CGroupMask
*mask
) {
2250 _cleanup_free_
char *n
= NULL
;
2254 r
= extract_first_word(&value
, &n
, NULL
, 0);
2260 v
= cgroup_controller_from_string(n
);
2264 *mask
|= CGROUP_CONTROLLER_TO_MASK(v
);
2269 int cg_mask_supported(CGroupMask
*ret
) {
2270 CGroupMask mask
= 0;
2273 /* Determines the mask of supported cgroup controllers. Only
2274 * includes controllers we can make sense of and that are
2275 * actually accessible. */
2277 r
= cg_all_unified();
2281 _cleanup_free_
char *root
= NULL
, *controllers
= NULL
, *path
= NULL
;
2283 /* In the unified hierarchy we can read the supported
2284 * and accessible controllers from a the top-level
2285 * cgroup attribute */
2287 r
= cg_get_root_path(&root
);
2291 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, root
, "cgroup.controllers", &path
);
2295 r
= read_one_line_file(path
, &controllers
);
2299 r
= cg_mask_from_string(controllers
, &mask
);
2303 /* Currently, we support the cpu, memory, io and pids
2304 * controller in the unified hierarchy, mask
2305 * everything else off. */
2306 mask
&= CGROUP_MASK_CPU
| CGROUP_MASK_MEMORY
| CGROUP_MASK_IO
| CGROUP_MASK_PIDS
;
2311 /* In the legacy hierarchy, we check whether which
2312 * hierarchies are mounted. */
2314 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2317 n
= cgroup_controller_to_string(c
);
2318 if (controller_is_accessible(n
) >= 0)
2319 mask
|= CGROUP_CONTROLLER_TO_MASK(c
);
2327 int cg_kernel_controllers(Set
*controllers
) {
2328 _cleanup_fclose_
FILE *f
= NULL
;
2331 assert(controllers
);
2333 /* Determines the full list of kernel-known controllers. Might
2334 * include controllers we don't actually support, arbitrary
2335 * named hierarchies and controllers that aren't currently
2336 * accessible (because not mounted). */
2338 f
= fopen("/proc/cgroups", "re");
2340 if (errno
== ENOENT
)
2345 /* Ignore the header line */
2346 (void) read_line(f
, (size_t) -1, NULL
);
2353 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
2358 if (ferror(f
) && errno
> 0)
2369 if (!cg_controller_is_valid(controller
)) {
2374 r
= set_consume(controllers
, controller
);
2382 static thread_local CGroupUnified unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2384 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2385 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2386 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2387 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2389 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2390 * process management but disable the compat dual layout, we return %true on
2391 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2393 static thread_local
bool unified_systemd_v232
;
2395 static int cg_unified_update(void) {
2399 /* Checks if we support the unified hierarchy. Returns an
2400 * error when the cgroup hierarchies aren't mounted yet or we
2401 * have any other trouble determining if the unified hierarchy
2404 if (unified_cache
>= CGROUP_UNIFIED_NONE
)
2407 if (statfs("/sys/fs/cgroup/", &fs
) < 0)
2410 if (F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
))
2411 unified_cache
= CGROUP_UNIFIED_ALL
;
2412 else if (F_TYPE_EQUAL(fs
.f_type
, TMPFS_MAGIC
)) {
2413 if (statfs("/sys/fs/cgroup/unified/", &fs
) == 0 &&
2414 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2415 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2416 unified_systemd_v232
= false;
2417 } else if (statfs("/sys/fs/cgroup/systemd/", &fs
) == 0 &&
2418 F_TYPE_EQUAL(fs
.f_type
, CGROUP2_SUPER_MAGIC
)) {
2419 unified_cache
= CGROUP_UNIFIED_SYSTEMD
;
2420 unified_systemd_v232
= true;
2422 if (statfs("/sys/fs/cgroup/systemd/", &fs
) < 0)
2424 if (!F_TYPE_EQUAL(fs
.f_type
, CGROUP_SUPER_MAGIC
))
2426 unified_cache
= CGROUP_UNIFIED_NONE
;
2434 int cg_unified_controller(const char *controller
) {
2437 r
= cg_unified_update();
2441 if (unified_cache
== CGROUP_UNIFIED_NONE
)
2444 if (unified_cache
>= CGROUP_UNIFIED_ALL
)
2447 return streq_ptr(controller
, SYSTEMD_CGROUP_CONTROLLER
);
2450 int cg_all_unified(void) {
2453 r
= cg_unified_update();
2457 return unified_cache
>= CGROUP_UNIFIED_ALL
;
2460 int cg_hybrid_unified(void) {
2463 r
= cg_unified_update();
2467 return unified_cache
== CGROUP_UNIFIED_SYSTEMD
&& !unified_systemd_v232
;
2470 int cg_unified_flush(void) {
2471 unified_cache
= CGROUP_UNIFIED_UNKNOWN
;
2473 return cg_unified_update();
2476 int cg_enable_everywhere(CGroupMask supported
, CGroupMask mask
, const char *p
) {
2477 _cleanup_free_
char *fs
= NULL
;
2486 r
= cg_all_unified();
2489 if (r
== 0) /* on the legacy hiearchy there's no joining of controllers defined */
2492 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, p
, "cgroup.subtree_control", &fs
);
2496 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++) {
2497 CGroupMask bit
= CGROUP_CONTROLLER_TO_MASK(c
);
2500 if (!(supported
& bit
))
2503 n
= cgroup_controller_to_string(c
);
2505 char s
[1 + strlen(n
) + 1];
2507 s
[0] = mask
& bit
? '+' : '-';
2510 r
= write_string_file(fs
, s
, 0);
2512 log_debug_errno(r
, "Failed to enable controller %s for %s (%s): %m", n
, p
, fs
);
2519 bool cg_is_unified_wanted(void) {
2520 static thread_local
int wanted
= -1;
2523 const bool is_default
= DEFAULT_HIERARCHY
== CGROUP_UNIFIED_ALL
;
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 return (wanted
= unified_cache
>= CGROUP_UNIFIED_ALL
);
2534 /* Otherwise, let's see what the kernel command line has to say.
2535 * Since checking is expensive, cache a non-error result. */
2536 r
= proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b
);
2538 return (wanted
= r
> 0 ? b
: is_default
);
2541 bool cg_is_legacy_wanted(void) {
2542 static thread_local
int wanted
= -1;
2544 /* If we have a cached value, return that. */
2548 /* Check if we have cgroups2 already mounted. */
2549 if (cg_unified_flush() >= 0 &&
2550 unified_cache
== CGROUP_UNIFIED_ALL
)
2551 return (wanted
= false);
2553 /* Otherwise, assume that at least partial legacy is wanted,
2554 * since cgroups2 should already be mounted at this point. */
2555 return (wanted
= true);
2558 bool cg_is_hybrid_wanted(void) {
2559 static thread_local
int wanted
= -1;
2562 const bool is_default
= DEFAULT_HIERARCHY
>= CGROUP_UNIFIED_SYSTEMD
;
2563 /* We default to true if the default is "hybrid", obviously,
2564 * but also when the default is "unified", because if we get
2565 * called, it means that unified hierarchy was not mounted. */
2567 /* If we have a cached value, return that. */
2571 /* If the hierarchy is already mounted, then follow whatever
2572 * was chosen for it. */
2573 if (cg_unified_flush() >= 0 &&
2574 unified_cache
== CGROUP_UNIFIED_ALL
)
2575 return (wanted
= false);
2577 /* Otherwise, let's see what the kernel command line has to say.
2578 * Since checking is expensive, cache a non-error result. */
2579 r
= proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b
);
2581 /* The meaning of the kernel option is reversed wrt. to the return value
2582 * of this function, hence the negation. */
2583 return (wanted
= r
> 0 ? !b
: is_default
);
2586 int cg_weight_parse(const char *s
, uint64_t *ret
) {
2591 *ret
= CGROUP_WEIGHT_INVALID
;
2595 r
= safe_atou64(s
, &u
);
2599 if (u
< CGROUP_WEIGHT_MIN
|| u
> CGROUP_WEIGHT_MAX
)
2606 const uint64_t cgroup_io_limit_defaults
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2607 [CGROUP_IO_RBPS_MAX
] = CGROUP_LIMIT_MAX
,
2608 [CGROUP_IO_WBPS_MAX
] = CGROUP_LIMIT_MAX
,
2609 [CGROUP_IO_RIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2610 [CGROUP_IO_WIOPS_MAX
] = CGROUP_LIMIT_MAX
,
2613 static const char* const cgroup_io_limit_type_table
[_CGROUP_IO_LIMIT_TYPE_MAX
] = {
2614 [CGROUP_IO_RBPS_MAX
] = "IOReadBandwidthMax",
2615 [CGROUP_IO_WBPS_MAX
] = "IOWriteBandwidthMax",
2616 [CGROUP_IO_RIOPS_MAX
] = "IOReadIOPSMax",
2617 [CGROUP_IO_WIOPS_MAX
] = "IOWriteIOPSMax",
2620 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type
, CGroupIOLimitType
);
2622 int cg_cpu_shares_parse(const char *s
, uint64_t *ret
) {
2627 *ret
= CGROUP_CPU_SHARES_INVALID
;
2631 r
= safe_atou64(s
, &u
);
2635 if (u
< CGROUP_CPU_SHARES_MIN
|| u
> CGROUP_CPU_SHARES_MAX
)
2642 int cg_blkio_weight_parse(const char *s
, uint64_t *ret
) {
2647 *ret
= CGROUP_BLKIO_WEIGHT_INVALID
;
2651 r
= safe_atou64(s
, &u
);
2655 if (u
< CGROUP_BLKIO_WEIGHT_MIN
|| u
> CGROUP_BLKIO_WEIGHT_MAX
)
2662 bool is_cgroup_fs(const struct statfs
*s
) {
2663 return is_fs_type(s
, CGROUP_SUPER_MAGIC
) ||
2664 is_fs_type(s
, CGROUP2_SUPER_MAGIC
);
2667 bool fd_is_cgroup_fs(int fd
) {
2670 if (fstatfs(fd
, &s
) < 0)
2673 return is_cgroup_fs(&s
);
2676 static const char *cgroup_controller_table
[_CGROUP_CONTROLLER_MAX
] = {
2677 [CGROUP_CONTROLLER_CPU
] = "cpu",
2678 [CGROUP_CONTROLLER_CPUACCT
] = "cpuacct",
2679 [CGROUP_CONTROLLER_IO
] = "io",
2680 [CGROUP_CONTROLLER_BLKIO
] = "blkio",
2681 [CGROUP_CONTROLLER_MEMORY
] = "memory",
2682 [CGROUP_CONTROLLER_DEVICES
] = "devices",
2683 [CGROUP_CONTROLLER_PIDS
] = "pids",
2686 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller
, CGroupController
);