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"
46 //#include "missing.h"
48 #include "parse-util.h"
49 #include "path-util.h"
50 #include "proc-cmdline.h"
51 #include "process-util.h"
53 //#include "special.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 #if 0 /// UNNEEDED by elogind
143 bool cg_ns_supported(void) {
144 static thread_local int enabled = -1;
149 if (access("/proc/self/ns/cgroup", F_OK) == 0)
158 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
159 _cleanup_free_ char *fs = NULL;
165 /* This is not recursive! */
167 r = cg_get_path(controller, path, NULL, &fs);
179 int cg_read_subgroup(DIR *d, char **fn) {
185 FOREACH_DIRENT_ALL(de, d, return -errno) {
188 if (de->d_type != DT_DIR)
191 if (dot_or_dot_dot(de->d_name))
194 b = strdup(de->d_name);
205 int cg_rmdir(const char *controller, const char *path) {
206 _cleanup_free_ char *p = NULL;
209 r = cg_get_path(controller, path, NULL, &p);
214 if (r < 0 && errno != ENOENT)
217 r = cg_hybrid_unified();
223 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
224 r = cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
226 log_warning_errno(r, "Failed to remove compat systemd cgroup %s: %m", path);
233 const char *controller,
238 cg_kill_log_func_t log_kill,
241 _cleanup_set_free_ Set *allocated_set = NULL;
248 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
249 * SIGCONT on SIGKILL. */
250 if (IN_SET(sig, SIGCONT, SIGKILL))
251 flags &= ~CGROUP_SIGCONT;
253 /* This goes through the tasks list and kills them all. This
254 * is repeated until no further processes are added to the
255 * tasks list, to properly handle forking processes */
258 s = allocated_set = set_new(NULL);
263 my_pid = getpid_cached();
266 _cleanup_fclose_ FILE *f = NULL;
270 r = cg_enumerate_processes(controller, path, &f);
272 if (ret >= 0 && r != -ENOENT)
278 while ((r = cg_read_pid(f, &pid)) > 0) {
280 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
283 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
287 log_kill(pid, sig, userdata);
289 /* If we haven't killed this process yet, kill
291 if (kill(pid, sig) < 0) {
292 if (ret >= 0 && errno != ESRCH)
295 if (flags & CGROUP_SIGCONT)
296 (void) kill(pid, SIGCONT);
304 r = set_put(s, PID_TO_PTR(pid));
320 /* To avoid racing against processes which fork
321 * quicker than we can kill them we repeat this until
322 * no new pids need to be killed. */
329 int cg_kill_recursive(
330 const char *controller,
335 cg_kill_log_func_t log_kill,
338 _cleanup_set_free_ Set *allocated_set = NULL;
339 _cleanup_closedir_ DIR *d = NULL;
347 s = allocated_set = set_new(NULL);
352 ret = cg_kill(controller, path, sig, flags, s, log_kill, userdata);
354 r = cg_enumerate_subgroups(controller, path, &d);
356 if (ret >= 0 && r != -ENOENT)
362 while ((r = cg_read_subgroup(d, &fn)) > 0) {
363 _cleanup_free_ char *p = NULL;
365 p = strjoin(path, "/", fn);
370 r = cg_kill_recursive(controller, p, sig, flags, s, log_kill, userdata);
371 if (r != 0 && ret >= 0)
374 if (ret >= 0 && r < 0)
377 if (flags & CGROUP_REMOVE) {
378 r = cg_rmdir(controller, path);
379 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
394 _cleanup_set_free_ Set *s = NULL;
407 my_pid = getpid_cached();
409 log_debug_elogind("Migrating \"%s\"/\"%s\" to \"%s\"/\"%s\" (%s)",
410 cfrom, pfrom, cto, pto,
411 (flags & CGROUP_IGNORE_SELF)
412 ? "ignoring self" : "watching self");
414 _cleanup_fclose_ FILE *f = NULL;
418 r = cg_enumerate_processes(cfrom, pfrom, &f);
420 if (ret >= 0 && r != -ENOENT)
426 while ((r = cg_read_pid(f, &pid)) > 0) {
428 /* This might do weird stuff if we aren't a
429 * single-threaded program. However, we
430 * luckily know we are not */
431 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
434 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
437 /* Ignore kernel threads. Since they can only
438 * exist in the root cgroup, we only check for
441 (isempty(pfrom) || path_equal(pfrom, "/")) &&
442 is_kernel_thread(pid) > 0)
445 r = cg_attach(cto, pto, pid);
447 if (ret >= 0 && r != -ESRCH)
454 r = set_put(s, PID_TO_PTR(pid));
474 int cg_migrate_recursive(
481 _cleanup_closedir_ DIR *d = NULL;
490 ret = cg_migrate(cfrom, pfrom, cto, pto, flags);
492 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
494 if (ret >= 0 && r != -ENOENT)
500 while ((r = cg_read_subgroup(d, &fn)) > 0) {
501 _cleanup_free_ char *p = NULL;
503 p = strjoin(pfrom, "/", fn);
508 r = cg_migrate_recursive(cfrom, p, cto, pto, flags);
509 if (r != 0 && ret >= 0)
513 if (r < 0 && ret >= 0)
516 if (flags & CGROUP_REMOVE) {
517 r = cg_rmdir(cfrom, pfrom);
518 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
525 int cg_migrate_recursive_fallback(
539 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, flags);
541 char prefix[strlen(pto) + 1];
543 /* This didn't work? Then let's try all prefixes of the destination */
545 PATH_FOREACH_PREFIX(prefix, pto) {
548 q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, flags);
557 static const char *controller_to_dirname(const char *controller) {
562 /* Converts a controller name to the directory name below
563 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
564 * just cuts off the name= prefixed used for named
565 * hierarchies, if it is specified. */
567 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
568 if (cg_hybrid_unified() > 0)
569 controller = SYSTEMD_CGROUP_CONTROLLER_HYBRID;
571 controller = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
574 e = startswith(controller, "name=");
581 static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) {
588 dn = controller_to_dirname(controller);
590 if (isempty(path) && isempty(suffix))
591 t = strappend("/sys/fs/cgroup/", dn);
592 else if (isempty(path))
593 t = strjoin("/sys/fs/cgroup/", dn, "/", suffix);
594 else if (isempty(suffix))
595 t = strjoin("/sys/fs/cgroup/", dn, "/", path);
597 t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix);
605 static int join_path_unified(const char *path, const char *suffix, char **fs) {
610 if (isempty(path) && isempty(suffix))
611 t = strdup("/sys/fs/cgroup");
612 else if (isempty(path))
613 t = strappend("/sys/fs/cgroup/", suffix);
614 else if (isempty(suffix))
615 t = strappend("/sys/fs/cgroup/", path);
617 t = strjoin("/sys/fs/cgroup/", path, "/", suffix);
625 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
633 /* If no controller is specified, we return the path
634 * *below* the controllers, without any prefix. */
636 if (!path && !suffix)
644 t = strjoin(path, "/", suffix);
648 *fs = path_kill_slashes(t);
652 if (!cg_controller_is_valid(controller))
655 r = cg_all_unified();
659 r = join_path_unified(path, suffix, fs);
661 r = join_path_legacy(controller, path, suffix, fs);
665 path_kill_slashes(*fs);
669 static int controller_is_accessible(const char *controller) {
674 /* Checks whether a specific controller is accessible,
675 * i.e. its hierarchy mounted. In the unified hierarchy all
676 * controllers are considered accessible, except for the named
679 if (!cg_controller_is_valid(controller))
682 r = cg_all_unified();
686 /* We don't support named hierarchies if we are using
687 * the unified hierarchy. */
689 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
692 if (startswith(controller, "name="))
698 dn = controller_to_dirname(controller);
699 cc = strjoina("/sys/fs/cgroup/", dn);
701 if (laccess(cc, F_OK) < 0)
708 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
714 /* Check if the specified controller is actually accessible */
715 r = controller_is_accessible(controller);
719 return cg_get_path(controller, path, suffix, fs);
722 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
727 if (typeflag != FTW_DP)
730 if (ftwbuf->level < 1)
737 int cg_trim(const char *controller, const char *path, bool delete_root) {
738 _cleanup_free_ char *fs = NULL;
743 r = cg_get_path(controller, path, NULL, &fs);
748 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
758 if (rmdir(fs) < 0 && errno != ENOENT)
762 q = cg_hybrid_unified();
765 if (q > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
766 q = cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, delete_root);
768 log_warning_errno(q, "Failed to trim compat systemd cgroup %s: %m", path);
774 int cg_create(const char *controller, const char *path) {
775 _cleanup_free_ char *fs = NULL;
778 r = cg_get_path_and_check(controller, path, NULL, &fs);
782 r = mkdir_parents(fs, 0755);
786 if (mkdir(fs, 0755) < 0) {
794 r = cg_hybrid_unified();
798 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
799 r = cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
801 log_warning_errno(r, "Failed to create compat systemd cgroup %s: %m", path);
807 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
812 r = cg_create(controller, path);
816 q = cg_attach(controller, path, pid);
820 /* This does not remove the cgroup on failure */
824 int cg_attach(const char *controller, const char *path, pid_t pid) {
825 _cleanup_free_ char *fs = NULL;
826 char c[DECIMAL_STR_MAX(pid_t) + 2];
832 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
837 pid = getpid_cached();
839 xsprintf(c, PID_FMT "\n", pid);
841 r = write_string_file(fs, c, 0);
845 r = cg_hybrid_unified();
849 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
850 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, pid);
852 log_warning_errno(r, "Failed to attach "PID_FMT" to compat systemd cgroup %s: %m", pid, path);
858 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
865 r = cg_attach(controller, path, pid);
867 char prefix[strlen(path) + 1];
869 /* This didn't work? Then let's try all prefixes of
872 PATH_FOREACH_PREFIX(prefix, path) {
875 q = cg_attach(controller, prefix, pid);
884 #if 0 /// UNNEEDED by elogind
885 int cg_set_group_access(
886 const char *controller,
892 _cleanup_free_ char *fs = NULL;
895 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
898 if (mode != MODE_INVALID)
901 r = cg_get_path(controller, path, NULL, &fs);
905 r = chmod_and_chown(fs, mode, uid, gid);
909 r = cg_hybrid_unified();
912 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
913 r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid);
915 log_debug_errno(r, "Failed to set group access on compatibility systemd cgroup %s, ignoring: %m", path);
921 int cg_set_task_access(
922 const char *controller,
928 _cleanup_free_ char *fs = NULL;
933 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
936 if (mode != MODE_INVALID)
939 /* For both the legacy and unified hierarchies, "cgroup.procs" is the main entry point for PIDs */
940 r = cg_get_path(controller, path, "cgroup.procs", &fs);
944 r = chmod_and_chown(fs, mode, uid, gid);
948 r = cg_unified_controller(controller);
954 /* Compatibility: on cgroupsv1 always keep values for the legacy files "tasks" and
955 * "cgroup.clone_children" in sync with "cgroup.procs". Since this is legacy stuff, we don't care if
960 "cgroup.clone_children") {
964 r = cg_get_path(controller, path, fn, &fs);
966 log_debug_errno(r, "Failed to get path for %s of %s, ignoring: %m", fn, path);
968 r = chmod_and_chown(fs, mode, uid, gid);
970 log_debug_errno(r, "Failed to to change ownership/access mode for %s of %s, ignoring: %m", fn, path);
973 /* On the unified controller, we want to permit subtree controllers too. */
976 r = cg_get_path(controller, path, "cgroup.subtree_control", &fs);
980 r = chmod_and_chown(fs, mode, uid, gid);
985 r = cg_hybrid_unified();
988 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
989 /* Always propagate access mode from unified to legacy controller */
991 r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, mode, uid, gid);
993 log_debug_errno(r, "Failed to set task access on compatibility systemd cgroup %s, ignoring: %m", path);
999 int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) {
1000 _cleanup_free_ char *fs = NULL;
1005 assert(value || size <= 0);
1007 r = cg_get_path(controller, path, NULL, &fs);
1011 if (setxattr(fs, name, value, size, flags) < 0)
1017 int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
1018 _cleanup_free_ char *fs = NULL;
1025 r = cg_get_path(controller, path, NULL, &fs);
1029 n = getxattr(fs, name, value, size);
1037 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
1038 _cleanup_fclose_ FILE *f = NULL;
1039 char line[LINE_MAX];
1040 #if 0 /// At elogind we do not want that (false alarm) "maybe uninitialized" warning
1041 const char *fs, *controller_str;
1043 const char *fs, *controller_str = NULL;
1052 if (!cg_controller_is_valid(controller))
1055 controller = SYSTEMD_CGROUP_CONTROLLER;
1057 unified = cg_unified_controller(controller);
1061 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
1062 controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
1064 controller_str = controller;
1066 cs = strlen(controller_str);
1069 fs = procfs_file_alloca(pid, "cgroup");
1070 log_debug_elogind("Searching for PID %u in \"%s\" (controller \"%s\")",
1071 pid, fs, controller);
1072 f = fopen(fs, "re");
1074 return errno == ENOENT ? -ESRCH : -errno;
1076 FOREACH_LINE(line, f, return -errno) {
1082 e = startswith(line, "0:");
1092 const char *word, *state;
1095 l = strchr(line, ':');
1105 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
1106 if (k == cs && memcmp(word, controller_str, cs) == 0) {
1116 log_debug_elogind("Found %s:%s", line, e+1);
1121 /* Truncate suffix indicating the process is a zombie */
1122 e = endswith(p, " (deleted)");
1133 #if 0 /// UNNEEDED by elogind
1134 int cg_install_release_agent(const char *controller, const char *agent) {
1135 _cleanup_free_ char *fs = NULL, *contents = NULL;
1141 r = cg_unified_controller(controller);
1144 if (r > 0) /* doesn't apply to unified hierarchy */
1147 r = cg_get_path(controller, NULL, "release_agent", &fs);
1151 r = read_one_line_file(fs, &contents);
1155 sc = strstrip(contents);
1157 r = write_string_file(fs, agent, 0);
1160 } else if (!path_equal(sc, agent))
1164 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1168 contents = mfree(contents);
1169 r = read_one_line_file(fs, &contents);
1173 sc = strstrip(contents);
1174 if (streq(sc, "0")) {
1175 r = write_string_file(fs, "1", 0);
1182 if (!streq(sc, "1"))
1188 int cg_uninstall_release_agent(const char *controller) {
1189 _cleanup_free_ char *fs = NULL;
1192 r = cg_unified_controller(controller);
1195 if (r > 0) /* Doesn't apply to unified hierarchy */
1198 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1202 r = write_string_file(fs, "0", 0);
1208 r = cg_get_path(controller, NULL, "release_agent", &fs);
1212 r = write_string_file(fs, "", 0);
1220 int cg_is_empty(const char *controller, const char *path) {
1221 _cleanup_fclose_ FILE *f = NULL;
1227 r = cg_enumerate_processes(controller, path, &f);
1233 r = cg_read_pid(f, &pid);
1240 int cg_is_empty_recursive(const char *controller, const char *path) {
1245 /* The root cgroup is always populated */
1246 if (controller && (isempty(path) || path_equal(path, "/")))
1249 r = cg_unified_controller(controller);
1253 _cleanup_free_ char *t = NULL;
1255 /* On the unified hierarchy we can check empty state
1256 * via the "populated" attribute of "cgroup.events". */
1258 r = cg_read_event(controller, path, "populated", &t);
1262 return streq(t, "0");
1264 _cleanup_closedir_ DIR *d = NULL;
1267 r = cg_is_empty(controller, path);
1271 r = cg_enumerate_subgroups(controller, path, &d);
1277 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1278 _cleanup_free_ char *p = NULL;
1280 p = strjoin(path, "/", fn);
1285 r = cg_is_empty_recursive(controller, p);
1296 int cg_split_spec(const char *spec, char **controller, char **path) {
1297 char *t = NULL, *u = NULL;
1303 if (!path_is_safe(spec))
1311 *path = path_kill_slashes(t);
1320 e = strchr(spec, ':');
1322 if (!cg_controller_is_valid(spec))
1339 t = strndup(spec, e-spec);
1342 if (!cg_controller_is_valid(t)) {
1356 if (!path_is_safe(u) ||
1357 !path_is_absolute(u)) {
1363 path_kill_slashes(u);
1379 int cg_mangle_path(const char *path, char **result) {
1380 _cleanup_free_ char *c = NULL, *p = NULL;
1387 /* First, check if it already is a filesystem path */
1388 if (path_startswith(path, "/sys/fs/cgroup")) {
1394 *result = path_kill_slashes(t);
1398 /* Otherwise, treat it as cg spec */
1399 r = cg_split_spec(path, &c, &p);
1403 return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result);
1406 int cg_get_root_path(char **path) {
1412 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1416 #if 0 /// elogind does not support systemd scopes and slices
1417 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1419 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1421 e = endswith(p, "/system"); /* even more legacy */
1423 e = endswith(p, "/elogind");
1432 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1433 _cleanup_free_ char *rt = NULL;
1441 /* If the root was specified let's use that, otherwise
1442 * let's determine it from PID 1 */
1444 r = cg_get_root_path(&rt);
1449 log_debug_elogind("Determined root path: \"%s\"", root);
1452 p = path_startswith(cgroup, root);
1453 #if 0 /// With other controllers, elogind might end up in /elogind, and *p is 0
1454 if (p && p > cgroup)
1456 if (p && p[0] && (p > cgroup))
1465 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1466 _cleanup_free_ char *raw = NULL;
1473 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1477 log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
1478 raw, pid, root ? root : "NULL");
1479 r = cg_shift_path(raw, root, &c);
1495 log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
1500 #if 0 /// UNNEEDED by elogind
1501 int cg_path_decode_unit(const char *cgroup, char **unit) {
1508 n = strcspn(cgroup, "/");
1512 c = strndupa(cgroup, n);
1515 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1526 static bool valid_slice_name(const char *p, size_t n) {
1531 if (n < strlen("x.slice"))
1534 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1540 c = cg_unescape(buf);
1542 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1548 static const char *skip_slices(const char *p) {
1551 /* Skips over all slice assignments */
1556 p += strspn(p, "/");
1558 n = strcspn(p, "/");
1559 if (!valid_slice_name(p, n))
1566 int cg_path_get_unit(const char *path, char **ret) {
1574 e = skip_slices(path);
1576 r = cg_path_decode_unit(e, &unit);
1580 /* We skipped over the slices, don't accept any now */
1581 if (endswith(unit, ".slice")) {
1590 int cg_pid_get_unit(pid_t pid, char **unit) {
1591 _cleanup_free_ char *cgroup = NULL;
1596 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1600 return cg_path_get_unit(cgroup, unit);
1604 * Skip session-*.scope, but require it to be there.
1606 static const char *skip_session(const char *p) {
1612 p += strspn(p, "/");
1614 n = strcspn(p, "/");
1615 if (n < strlen("session-x.scope"))
1618 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1619 char buf[n - 8 - 6 + 1];
1621 memcpy(buf, p + 8, n - 8 - 6);
1624 /* Note that session scopes never need unescaping,
1625 * since they cannot conflict with the kernel's own
1626 * names, hence we don't need to call cg_unescape()
1629 if (!session_id_valid(buf))
1633 p += strspn(p, "/");
1641 * Skip user@*.service, but require it to be there.
1643 static const char *skip_user_manager(const char *p) {
1649 p += strspn(p, "/");
1651 n = strcspn(p, "/");
1652 if (n < strlen("user@x.service"))
1655 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1656 char buf[n - 5 - 8 + 1];
1658 memcpy(buf, p + 5, n - 5 - 8);
1661 /* Note that user manager services never need unescaping,
1662 * since they cannot conflict with the kernel's own
1663 * names, hence we don't need to call cg_unescape()
1666 if (parse_uid(buf, NULL) < 0)
1670 p += strspn(p, "/");
1678 static const char *skip_user_prefix(const char *path) {
1683 /* Skip slices, if there are any */
1684 e = skip_slices(path);
1686 /* Skip the user manager, if it's in the path now... */
1687 t = skip_user_manager(e);
1691 /* Alternatively skip the user session if it is in the path... */
1692 return skip_session(e);
1695 int cg_path_get_user_unit(const char *path, char **ret) {
1701 t = skip_user_prefix(path);
1705 /* And from here on it looks pretty much the same as for a
1706 * system unit, hence let's use the same parser from here
1708 return cg_path_get_unit(t, ret);
1711 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1712 _cleanup_free_ char *cgroup = NULL;
1717 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1721 return cg_path_get_user_unit(cgroup, unit);
1724 int cg_path_get_machine_name(const char *path, char **machine) {
1725 _cleanup_free_ char *u = NULL;
1729 r = cg_path_get_unit(path, &u);
1733 sl = strjoina("/run/systemd/machines/unit:", u);
1734 return readlink_malloc(sl, machine);
1737 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1738 _cleanup_free_ char *cgroup = NULL;
1743 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1747 return cg_path_get_machine_name(cgroup, machine);
1751 int cg_path_get_session(const char *path, char **session) {
1752 #if 0 /// UNNEEDED by elogind
1753 _cleanup_free_ char *unit = NULL;
1759 r = cg_path_get_unit(path, &unit);
1763 start = startswith(unit, "session-");
1766 end = endswith(start, ".scope");
1771 if (!session_id_valid(start))
1774 /* Elogind uses a flat hierarchy, just "/SESSION". The only
1775 wrinkle is that SESSION might be escaped. */
1776 const char *e, *n, *start;
1779 log_debug_elogind("path is \"%s\"", path);
1780 assert(path[0] == '/');
1783 n = strchrnul(e, '/');
1787 start = strndupa(e, n - e);
1788 start = cg_unescape(start);
1797 log_debug_elogind("found session: \"%s\"", start);
1808 int cg_pid_get_session(pid_t pid, char **session) {
1809 _cleanup_free_ char *cgroup = NULL;
1812 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1816 return cg_path_get_session(cgroup, session);
1819 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1820 #if 0 /// elogind needs one more value
1821 _cleanup_free_ char *slice = NULL;
1824 _cleanup_free_ char *slice = NULL, *p = NULL, *s = NULL;
1830 r = cg_path_get_slice(path, &slice);
1834 #if 0 /// elogind does not support systemd slices
1835 start = startswith(slice, "user-");
1838 end = endswith(start, ".slice");
1843 if (parse_uid(start, uid) < 0)
1846 p = strappend("/run/systemd/sessions/", slice);
1848 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
1856 if (parse_uid(s, uid) < 0)
1863 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1864 _cleanup_free_ char *cgroup = NULL;
1867 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1871 return cg_path_get_owner_uid(cgroup, uid);
1874 int cg_path_get_slice(const char *p, char **slice) {
1875 const char *e = NULL;
1880 #if 0 /// elogind does not support systemd slices
1881 /* Finds the right-most slice unit from the beginning, but
1882 * stops before we come to the first non-slice unit. */
1887 p += strspn(p, "/");
1889 n = strcspn(p, "/");
1890 if (!valid_slice_name(p, n)) {
1895 s = strdup(SPECIAL_ROOT_SLICE);
1903 return cg_path_decode_unit(e, slice);
1910 /* In elogind, what is reported here, is the location of
1911 * the session. This is derived from /proc/<self|PID>/cgroup.
1912 * In there we look at the controller, which will look something
1913 * like "1:name=openrc:/3".
1914 * The last part gets extracted (and is now p), which is "/3" in
1915 * this case. The three is the session id, and that can be mapped.
1917 e = startswith(p, "/");
1928 int cg_pid_get_slice(pid_t pid, char **slice) {
1929 _cleanup_free_ char *cgroup = NULL;
1934 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1935 log_debug_elogind("Found cgroup %s for pid %u (result %d)",
1940 return cg_path_get_slice(cgroup, slice);
1943 int cg_path_get_user_slice(const char *p, char **slice) {
1944 #if 0 /// UNNEEDED by elogind
1950 #if 0 /// nothing to skip in elogind
1951 t = skip_user_prefix(p);
1956 #if 0 /// UNNEEDED by elogind
1957 /* And now it looks pretty much the same as for a system
1958 * slice, so let's just use the same parser from here on. */
1959 return cg_path_get_slice(t, slice);
1961 /* In elogind there is nothing to skip, we can use the path
1962 * directly. Generally speaking this is always a session id
1963 * to user mapping. */
1964 return cg_path_get_slice(p, slice);
1968 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1969 _cleanup_free_ char *cgroup = NULL;
1974 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1978 return cg_path_get_user_slice(cgroup, slice);
1981 char *cg_escape(const char *p) {
1982 bool need_prefix = false;
1984 /* This implements very minimal escaping for names to be used
1985 * as file names in the cgroup tree: any name which might
1986 * conflict with a kernel name or is prefixed with '_' is
1987 * prefixed with a '_'. That way, when reading cgroup names it
1988 * is sufficient to remove a single prefixing underscore if
1991 /* The return value of this function (unlike cg_unescape())
1994 if (IN_SET(p[0], 0, '_', '.') ||
1995 streq(p, "notify_on_release") ||
1996 streq(p, "release_agent") ||
1997 streq(p, "tasks") ||
1998 startswith(p, "cgroup."))
2003 dot = strrchr(p, '.');
2008 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2011 n = cgroup_controller_to_string(c);
2016 if (memcmp(p, n, l) != 0)
2026 return strappend("_", p);
2031 char *cg_unescape(const char *p) {
2034 /* The return value of this function (unlike cg_escape())
2035 * doesn't need free()! */
2043 #define CONTROLLER_VALID \
2047 bool cg_controller_is_valid(const char *p) {
2053 if (streq(p, SYSTEMD_CGROUP_CONTROLLER))
2056 s = startswith(p, "name=");
2060 if (IN_SET(*p, 0, '_'))
2063 for (t = p; *t; t++)
2064 if (!strchr(CONTROLLER_VALID, *t))
2067 if (t - p > FILENAME_MAX)
2073 #if 0 /// UNNEEDED by elogind
2074 int cg_slice_to_path(const char *unit, char **ret) {
2075 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
2082 if (streq(unit, SPECIAL_ROOT_SLICE)) {
2092 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
2095 if (!endswith(unit, ".slice"))
2098 r = unit_name_to_prefix(unit, &p);
2102 dash = strchr(p, '-');
2104 /* Don't allow initial dashes */
2109 _cleanup_free_ char *escaped = NULL;
2110 char n[dash - p + sizeof(".slice")];
2112 /* Don't allow trailing or double dashes */
2113 if (IN_SET(dash[1], 0, '-'))
2116 strcpy(stpncpy(n, p, dash - p), ".slice");
2117 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
2120 escaped = cg_escape(n);
2124 if (!strextend(&s, escaped, "/", NULL))
2127 dash = strchr(dash+1, '-');
2130 e = cg_escape(unit);
2134 if (!strextend(&s, e, NULL))
2144 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
2145 _cleanup_free_ char *p = NULL;
2148 r = cg_get_path(controller, path, attribute, &p);
2152 return write_string_file(p, value, 0);
2155 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
2156 _cleanup_free_ char *p = NULL;
2159 r = cg_get_path(controller, path, attribute, &p);
2163 return read_one_line_file(p, ret);
2166 #if 0 /// UNNEEDED by elogind
2167 int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, const char **keys, char **values) {
2168 _cleanup_free_ char *filename = NULL, *content = NULL;
2172 for (i = 0; keys[i]; i++)
2175 r = cg_get_path(controller, path, attribute, &filename);
2179 r = read_full_file(filename, &content, NULL);
2184 while ((line = strsep(&p, "\n"))) {
2187 key = strsep(&line, " ");
2189 for (i = 0; keys[i]; i++) {
2190 if (streq(key, keys[i])) {
2191 values[i] = strdup(line);
2197 for (i = 0; keys[i]; i++) {
2199 for (i = 0; keys[i]; i++) {
2200 values[i] = mfree(values[i]);
2209 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
2213 /* This one will create a cgroup in our private tree, but also
2214 * duplicate it in the trees specified in mask, and remove it
2217 /* First create the cgroup in our own hierarchy. */
2218 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
2222 /* If we are in the unified hierarchy, we are done now */
2223 r = cg_all_unified();
2229 /* Otherwise, do the same in the other hierarchies */
2230 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2231 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2234 n = cgroup_controller_to_string(c);
2237 (void) cg_create(n, path);
2238 else if (supported & bit)
2239 (void) cg_trim(n, path, true);
2245 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
2249 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
2253 r = cg_all_unified();
2259 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2260 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2261 const char *p = NULL;
2263 if (!(supported & bit))
2267 p = path_callback(bit, userdata);
2272 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
2278 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
2283 SET_FOREACH(pidp, pids, i) {
2284 pid_t pid = PTR_TO_PID(pidp);
2287 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
2288 if (q < 0 && r >= 0)
2295 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
2299 if (!path_equal(from, to)) {
2300 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE);
2305 q = cg_all_unified();
2311 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2312 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2313 const char *p = NULL;
2315 if (!(supported & bit))
2319 p = to_callback(bit, userdata);
2324 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0);
2330 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2334 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2338 q = cg_all_unified();
2344 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2345 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2347 if (!(supported & bit))
2350 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2357 int cg_mask_to_string(CGroupMask mask, char **ret) {
2358 const char *controllers[_CGROUP_CONTROLLER_MAX + 1];
2370 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2372 if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
2375 controllers[i++] = cgroup_controller_to_string(c);
2376 controllers[i] = NULL;
2379 s = strv_join((char **)controllers, NULL);
2387 int cg_mask_from_string(const char *value, CGroupMask *mask) {
2392 _cleanup_free_ char *n = NULL;
2396 r = extract_first_word(&value, &n, NULL, 0);
2402 v = cgroup_controller_from_string(n);
2406 *mask |= CGROUP_CONTROLLER_TO_MASK(v);
2411 int cg_mask_supported(CGroupMask *ret) {
2412 CGroupMask mask = 0;
2415 /* Determines the mask of supported cgroup controllers. Only
2416 * includes controllers we can make sense of and that are
2417 * actually accessible. */
2419 r = cg_all_unified();
2423 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2425 /* In the unified hierarchy we can read the supported
2426 * and accessible controllers from a the top-level
2427 * cgroup attribute */
2429 r = cg_get_root_path(&root);
2433 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2437 r = read_one_line_file(path, &controllers);
2441 r = cg_mask_from_string(controllers, &mask);
2445 /* Currently, we support the cpu, memory, io and pids
2446 * controller in the unified hierarchy, mask
2447 * everything else off. */
2448 mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS;
2453 /* In the legacy hierarchy, we check whether which
2454 * hierarchies are mounted. */
2456 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2459 n = cgroup_controller_to_string(c);
2460 if (controller_is_accessible(n) >= 0)
2461 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2469 #if 0 /// UNNEEDED by elogind
2470 int cg_kernel_controllers(Set *controllers) {
2471 _cleanup_fclose_ FILE *f = NULL;
2474 assert(controllers);
2476 /* Determines the full list of kernel-known controllers. Might
2477 * include controllers we don't actually support, arbitrary
2478 * named hierarchies and controllers that aren't currently
2479 * accessible (because not mounted). */
2481 f = fopen("/proc/cgroups", "re");
2483 if (errno == ENOENT)
2488 /* Ignore the header line */
2489 (void) read_line(f, (size_t) -1, NULL);
2496 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2501 if (ferror(f) && errno > 0)
2512 if (!cg_controller_is_valid(controller)) {
2517 r = set_consume(controllers, controller);
2526 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
2528 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2529 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2530 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2531 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2533 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2534 * process management but disable the compat dual layout, we return %true on
2535 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2537 static thread_local bool unified_systemd_v232;
2539 static int cg_unified_update(void) {
2543 /* Checks if we support the unified hierarchy. Returns an
2544 * error when the cgroup hierarchies aren't mounted yet or we
2545 * have any other trouble determining if the unified hierarchy
2548 if (unified_cache >= CGROUP_UNIFIED_NONE)
2551 if (statfs("/sys/fs/cgroup/", &fs) < 0)
2554 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC))
2555 unified_cache = CGROUP_UNIFIED_ALL;
2556 #if 0 /// The handling of cgroups is a bit different with elogind
2557 else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2559 else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)
2560 || F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2562 if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 &&
2563 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2564 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2565 unified_systemd_v232 = false;
2566 #if 0 /// elogind uses its own name
2567 } else if (statfs("/sys/fs/cgroup/systemd/", &fs) == 0 &&
2569 } else if (statfs("/sys/fs/cgroup/elogind/", &fs) == 0 &&
2571 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2572 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2573 unified_systemd_v232 = true;
2575 #if 0 /// There is no sub-grouping within elogind
2576 if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0)
2578 if (!F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC))
2581 unified_cache = CGROUP_UNIFIED_NONE;
2589 int cg_unified_controller(const char *controller) {
2592 r = cg_unified_update();
2596 if (unified_cache == CGROUP_UNIFIED_NONE)
2599 if (unified_cache >= CGROUP_UNIFIED_ALL)
2602 #if 0 /// only if elogind is the controller we can use cgroups2 in hybrid mode
2603 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER);
2605 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER_HYBRID);
2609 int cg_all_unified(void) {
2612 r = cg_unified_update();
2616 return unified_cache >= CGROUP_UNIFIED_ALL;
2619 int cg_hybrid_unified(void) {
2622 r = cg_unified_update();
2626 return unified_cache == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232;
2629 int cg_unified_flush(void) {
2630 unified_cache = CGROUP_UNIFIED_UNKNOWN;
2632 return cg_unified_update();
2635 #if 0 /// UNNEEDED by elogind
2636 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2637 _cleanup_free_ char *fs = NULL;
2646 r = cg_all_unified();
2649 if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */
2652 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2656 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2657 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2660 if (!(supported & bit))
2663 n = cgroup_controller_to_string(c);
2665 char s[1 + strlen(n) + 1];
2667 s[0] = mask & bit ? '+' : '-';
2670 r = write_string_file(fs, s, 0);
2672 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2680 bool cg_is_unified_wanted(void) {
2681 static thread_local int wanted = -1;
2684 const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL;
2686 /* If we have a cached value, return that. */
2690 /* If the hierarchy is already mounted, then follow whatever
2691 * was chosen for it. */
2692 if (cg_unified_flush() >= 0)
2693 return (wanted = unified_cache >= CGROUP_UNIFIED_ALL);
2695 #if 0 /// elogind is not init and has no business with kernel command line
2696 /* Otherwise, let's see what the kernel command line has to say.
2697 * Since checking is expensive, cache a non-error result. */
2698 r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b);
2701 return (wanted = r > 0 ? b : is_default);
2704 bool cg_is_legacy_wanted(void) {
2705 static thread_local int wanted = -1;
2707 /* If we have a cached value, return that. */
2711 /* Check if we have cgroups2 already mounted. */
2712 if (cg_unified_flush() >= 0 &&
2713 unified_cache == CGROUP_UNIFIED_ALL)
2714 return (wanted = false);
2716 /* Otherwise, assume that at least partial legacy is wanted,
2717 * since cgroups2 should already be mounted at this point. */
2718 return (wanted = true);
2721 bool cg_is_hybrid_wanted(void) {
2722 static thread_local int wanted = -1;
2725 const bool is_default = DEFAULT_HIERARCHY >= CGROUP_UNIFIED_SYSTEMD;
2726 /* We default to true if the default is "hybrid", obviously,
2727 * but also when the default is "unified", because if we get
2728 * called, it means that unified hierarchy was not mounted. */
2730 /* If we have a cached value, return that. */
2734 /* If the hierarchy is already mounted, then follow whatever
2735 * was chosen for it. */
2736 if (cg_unified_flush() >= 0 &&
2737 unified_cache == CGROUP_UNIFIED_ALL)
2738 return (wanted = false);
2740 #if 0 /// elogind is not init and has no business with kernel command line
2741 /* Otherwise, let's see what the kernel command line has to say.
2742 * Since checking is expensive, cache a non-error result. */
2743 r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b);
2746 /* The meaning of the kernel option is reversed wrt. to the return value
2747 * of this function, hence the negation. */
2748 return (wanted = r > 0 ? !b : is_default);
2751 #if 0 /// UNNEEDED by elogind
2752 int cg_weight_parse(const char *s, uint64_t *ret) {
2757 *ret = CGROUP_WEIGHT_INVALID;
2761 r = safe_atou64(s, &u);
2765 if (u < CGROUP_WEIGHT_MIN || u > CGROUP_WEIGHT_MAX)
2772 const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2773 [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX,
2774 [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX,
2775 [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX,
2776 [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX,
2779 static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2780 [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax",
2781 [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax",
2782 [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax",
2783 [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax",
2786 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType);
2788 int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2793 *ret = CGROUP_CPU_SHARES_INVALID;
2797 r = safe_atou64(s, &u);
2801 if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2808 int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2813 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2817 r = safe_atou64(s, &u);
2821 if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2829 bool is_cgroup_fs(const struct statfs *s) {
2830 return is_fs_type(s, CGROUP_SUPER_MAGIC) ||
2831 is_fs_type(s, CGROUP2_SUPER_MAGIC);
2834 bool fd_is_cgroup_fs(int fd) {
2837 if (fstatfs(fd, &s) < 0)
2840 return is_cgroup_fs(&s);
2843 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2844 [CGROUP_CONTROLLER_CPU] = "cpu",
2845 [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
2846 [CGROUP_CONTROLLER_IO] = "io",
2847 [CGROUP_CONTROLLER_BLKIO] = "blkio",
2848 [CGROUP_CONTROLLER_MEMORY] = "memory",
2849 [CGROUP_CONTROLLER_DEVICES] = "devices",
2850 [CGROUP_CONTROLLER_PIDS] = "pids",
2853 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);