1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
14 #include <stdio_ext.h>
18 //#include <sys/statfs.h>
19 #include <sys/types.h>
20 #include <sys/xattr.h>
23 #include "alloc-util.h"
24 #include "cgroup-util.h"
26 #include "dirent-util.h"
27 #include "extract-word.h"
30 #include "format-util.h"
33 #include "login-util.h"
35 //#include "missing.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "proc-cmdline.h"
40 #include "process-util.h"
42 //#include "special.h"
43 #include "stat-util.h"
44 #include "stdio-util.h"
45 #include "string-table.h"
46 #include "string-util.h"
48 #include "unit-name.h"
49 #include "user-util.h"
51 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
52 _cleanup_free_ char *fs = NULL;
58 r = cg_get_path(controller, path, "cgroup.procs", &fs);
70 int cg_read_pid(FILE *f, pid_t *_pid) {
73 /* Note that the cgroup.procs might contain duplicates! See
74 * cgroups.txt for details. */
80 if (fscanf(f, "%lu", &ul) != 1) {
85 return errno > 0 ? -errno : -EIO;
96 const char *controller,
101 _cleanup_free_ char *events = NULL, *content = NULL;
105 r = cg_get_path(controller, path, "cgroup.events", &events);
109 r = read_full_file(events, &content, NULL);
114 while ((line = strsep(&p, "\n"))) {
117 key = strsep(&line, " ");
121 if (strcmp(key, event))
131 #if 0 /// UNNEEDED by elogind
132 bool cg_ns_supported(void) {
133 static thread_local int enabled = -1;
138 if (access("/proc/self/ns/cgroup", F_OK) == 0)
147 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
148 _cleanup_free_ char *fs = NULL;
154 /* This is not recursive! */
156 r = cg_get_path(controller, path, NULL, &fs);
168 int cg_read_subgroup(DIR *d, char **fn) {
174 FOREACH_DIRENT_ALL(de, d, return -errno) {
177 if (de->d_type != DT_DIR)
180 if (dot_or_dot_dot(de->d_name))
183 b = strdup(de->d_name);
194 int cg_rmdir(const char *controller, const char *path) {
195 _cleanup_free_ char *p = NULL;
198 r = cg_get_path(controller, path, NULL, &p);
203 if (r < 0 && errno != ENOENT)
206 r = cg_hybrid_unified();
212 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
213 r = cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
215 log_warning_errno(r, "Failed to remove compat systemd cgroup %s: %m", path);
222 const char *controller,
227 cg_kill_log_func_t log_kill,
230 _cleanup_set_free_ Set *allocated_set = NULL;
237 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
238 * SIGCONT on SIGKILL. */
239 if (IN_SET(sig, SIGCONT, SIGKILL))
240 flags &= ~CGROUP_SIGCONT;
242 /* This goes through the tasks list and kills them all. This
243 * is repeated until no further processes are added to the
244 * tasks list, to properly handle forking processes */
247 s = allocated_set = set_new(NULL);
252 my_pid = getpid_cached();
255 _cleanup_fclose_ FILE *f = NULL;
259 r = cg_enumerate_processes(controller, path, &f);
261 if (ret >= 0 && r != -ENOENT)
267 while ((r = cg_read_pid(f, &pid)) > 0) {
269 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
272 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
276 log_kill(pid, sig, userdata);
278 /* If we haven't killed this process yet, kill
280 if (kill(pid, sig) < 0) {
281 if (ret >= 0 && errno != ESRCH)
284 if (flags & CGROUP_SIGCONT)
285 (void) kill(pid, SIGCONT);
293 r = set_put(s, PID_TO_PTR(pid));
309 /* To avoid racing against processes which fork
310 * quicker than we can kill them we repeat this until
311 * no new pids need to be killed. */
318 int cg_kill_recursive(
319 const char *controller,
324 cg_kill_log_func_t log_kill,
327 _cleanup_set_free_ Set *allocated_set = NULL;
328 _cleanup_closedir_ DIR *d = NULL;
336 s = allocated_set = set_new(NULL);
341 ret = cg_kill(controller, path, sig, flags, s, log_kill, userdata);
343 r = cg_enumerate_subgroups(controller, path, &d);
345 if (ret >= 0 && r != -ENOENT)
351 while ((r = cg_read_subgroup(d, &fn)) > 0) {
352 _cleanup_free_ char *p = NULL;
354 p = strjoin(path, "/", fn);
359 r = cg_kill_recursive(controller, p, sig, flags, s, log_kill, userdata);
360 if (r != 0 && ret >= 0)
363 if (ret >= 0 && r < 0)
366 if (flags & CGROUP_REMOVE) {
367 r = cg_rmdir(controller, path);
368 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
383 _cleanup_set_free_ Set *s = NULL;
396 my_pid = getpid_cached();
398 log_debug_elogind("Migrating \"%s\"/\"%s\" to \"%s\"/\"%s\" (%s)",
399 cfrom, pfrom, cto, pto,
400 (flags & CGROUP_IGNORE_SELF)
401 ? "ignoring self" : "watching self");
403 _cleanup_fclose_ FILE *f = NULL;
407 r = cg_enumerate_processes(cfrom, pfrom, &f);
409 if (ret >= 0 && r != -ENOENT)
415 while ((r = cg_read_pid(f, &pid)) > 0) {
417 /* This might do weird stuff if we aren't a
418 * single-threaded program. However, we
419 * luckily know we are not */
420 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
423 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
426 /* Ignore kernel threads. Since they can only
427 * exist in the root cgroup, we only check for
430 empty_or_root(pfrom) &&
431 is_kernel_thread(pid) > 0)
434 r = cg_attach(cto, pto, pid);
436 if (ret >= 0 && r != -ESRCH)
443 r = set_put(s, PID_TO_PTR(pid));
463 int cg_migrate_recursive(
470 _cleanup_closedir_ DIR *d = NULL;
479 ret = cg_migrate(cfrom, pfrom, cto, pto, flags);
481 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
483 if (ret >= 0 && r != -ENOENT)
489 while ((r = cg_read_subgroup(d, &fn)) > 0) {
490 _cleanup_free_ char *p = NULL;
492 p = strjoin(pfrom, "/", fn);
497 r = cg_migrate_recursive(cfrom, p, cto, pto, flags);
498 if (r != 0 && ret >= 0)
502 if (r < 0 && ret >= 0)
505 if (flags & CGROUP_REMOVE) {
506 r = cg_rmdir(cfrom, pfrom);
507 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
514 int cg_migrate_recursive_fallback(
528 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, flags);
530 char prefix[strlen(pto) + 1];
532 /* This didn't work? Then let's try all prefixes of the destination */
534 PATH_FOREACH_PREFIX(prefix, pto) {
537 q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, flags);
546 static const char *controller_to_dirname(const char *controller) {
551 /* Converts a controller name to the directory name below
552 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
553 * just cuts off the name= prefixed used for named
554 * hierarchies, if it is specified. */
556 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
557 if (cg_hybrid_unified() > 0)
558 controller = SYSTEMD_CGROUP_CONTROLLER_HYBRID;
560 controller = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
563 e = startswith(controller, "name=");
570 static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) {
577 dn = controller_to_dirname(controller);
579 if (isempty(path) && isempty(suffix))
580 t = strappend("/sys/fs/cgroup/", dn);
581 else if (isempty(path))
582 t = strjoin("/sys/fs/cgroup/", dn, "/", suffix);
583 else if (isempty(suffix))
584 t = strjoin("/sys/fs/cgroup/", dn, "/", path);
586 t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix);
594 static int join_path_unified(const char *path, const char *suffix, char **fs) {
599 if (isempty(path) && isempty(suffix))
600 t = strdup("/sys/fs/cgroup");
601 else if (isempty(path))
602 t = strappend("/sys/fs/cgroup/", suffix);
603 else if (isempty(suffix))
604 t = strappend("/sys/fs/cgroup/", path);
606 t = strjoin("/sys/fs/cgroup/", path, "/", suffix);
614 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
622 /* If no controller is specified, we return the path
623 * *below* the controllers, without any prefix. */
625 if (!path && !suffix)
633 t = strjoin(path, "/", suffix);
637 *fs = path_kill_slashes(t);
641 if (!cg_controller_is_valid(controller))
644 r = cg_all_unified();
648 r = join_path_unified(path, suffix, fs);
650 r = join_path_legacy(controller, path, suffix, fs);
654 path_kill_slashes(*fs);
658 static int controller_is_accessible(const char *controller) {
663 /* Checks whether a specific controller is accessible,
664 * i.e. its hierarchy mounted. In the unified hierarchy all
665 * controllers are considered accessible, except for the named
668 if (!cg_controller_is_valid(controller))
671 r = cg_all_unified();
675 /* We don't support named hierarchies if we are using
676 * the unified hierarchy. */
678 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
681 if (startswith(controller, "name="))
687 dn = controller_to_dirname(controller);
688 cc = strjoina("/sys/fs/cgroup/", dn);
690 if (laccess(cc, F_OK) < 0)
697 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
703 /* Check if the specified controller is actually accessible */
704 r = controller_is_accessible(controller);
708 return cg_get_path(controller, path, suffix, fs);
711 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
716 if (typeflag != FTW_DP)
719 if (ftwbuf->level < 1)
726 int cg_trim(const char *controller, const char *path, bool delete_root) {
727 _cleanup_free_ char *fs = NULL;
732 r = cg_get_path(controller, path, NULL, &fs);
737 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
747 if (rmdir(fs) < 0 && errno != ENOENT)
751 q = cg_hybrid_unified();
754 if (q > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
755 q = cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, delete_root);
757 log_warning_errno(q, "Failed to trim compat systemd cgroup %s: %m", path);
763 int cg_create(const char *controller, const char *path) {
764 _cleanup_free_ char *fs = NULL;
767 r = cg_get_path_and_check(controller, path, NULL, &fs);
771 r = mkdir_parents(fs, 0755);
775 r = mkdir_errno_wrapper(fs, 0755);
781 r = cg_hybrid_unified();
785 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
786 r = cg_create(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
788 log_warning_errno(r, "Failed to create compat systemd cgroup %s: %m", path);
794 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
799 r = cg_create(controller, path);
803 q = cg_attach(controller, path, pid);
807 /* This does not remove the cgroup on failure */
811 int cg_attach(const char *controller, const char *path, pid_t pid) {
812 _cleanup_free_ char *fs = NULL;
813 char c[DECIMAL_STR_MAX(pid_t) + 2];
819 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
824 pid = getpid_cached();
826 xsprintf(c, PID_FMT "\n", pid);
828 r = write_string_file(fs, c, 0);
832 r = cg_hybrid_unified();
836 if (r > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
837 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, pid);
839 log_warning_errno(r, "Failed to attach "PID_FMT" to compat systemd cgroup %s: %m", pid, path);
845 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
852 r = cg_attach(controller, path, pid);
854 char prefix[strlen(path) + 1];
856 /* This didn't work? Then let's try all prefixes of
859 PATH_FOREACH_PREFIX(prefix, path) {
862 q = cg_attach(controller, prefix, pid);
871 #if 0 /// UNNEEDED by elogind
873 const char *controller,
883 /* cgroupsv1, aka legacy/non-unified */
884 static const struct Attribute legacy_attributes[] = {
885 { "cgroup.procs", true },
887 { "cgroup.clone_children", false },
891 /* cgroupsv2, aka unified */
892 static const struct Attribute unified_attributes[] = {
893 { "cgroup.procs", true },
894 { "cgroup.subtree_control", true },
895 { "cgroup.threads", false },
899 static const struct Attribute* const attributes[] = {
900 [false] = legacy_attributes,
901 [true] = unified_attributes,
904 _cleanup_free_ char *fs = NULL;
905 const struct Attribute *i;
910 if (uid == UID_INVALID && gid == GID_INVALID)
913 unified = cg_unified_controller(controller);
917 /* Configure access to the cgroup itself */
918 r = cg_get_path(controller, path, NULL, &fs);
922 r = chmod_and_chown(fs, 0755, uid, gid);
926 /* Configure access to the cgroup's attributes */
927 for (i = attributes[unified]; i->name; i++) {
930 r = cg_get_path(controller, path, i->name, &fs);
934 r = chmod_and_chown(fs, 0644, uid, gid);
939 log_debug_errno(r, "Failed to set access on cgroup %s, ignoring: %m", fs);
943 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
944 r = cg_hybrid_unified();
948 /* Always propagate access mode from unified to legacy controller */
949 r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, uid, gid);
951 log_debug_errno(r, "Failed to set access on compatibility elogind cgroup %s, ignoring: %m", path);
958 int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) {
959 _cleanup_free_ char *fs = NULL;
964 assert(value || size <= 0);
966 r = cg_get_path(controller, path, NULL, &fs);
970 if (setxattr(fs, name, value, size, flags) < 0)
976 int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
977 _cleanup_free_ char *fs = NULL;
984 r = cg_get_path(controller, path, NULL, &fs);
988 n = getxattr(fs, name, value, size);
996 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
997 _cleanup_fclose_ FILE *f = NULL;
999 #if 0 /// At elogind we do not want that (false alarm) "maybe uninitialized" warning
1000 const char *fs, *controller_str;
1002 const char *fs, *controller_str = NULL;
1011 if (!cg_controller_is_valid(controller))
1014 controller = SYSTEMD_CGROUP_CONTROLLER;
1016 unified = cg_unified_controller(controller);
1020 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
1021 controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
1023 controller_str = controller;
1025 cs = strlen(controller_str);
1028 fs = procfs_file_alloca(pid, "cgroup");
1029 log_debug_elogind("Searching for PID %u in \"%s\" (controller \"%s\")",
1030 pid, fs, controller);
1031 f = fopen(fs, "re");
1033 return errno == ENOENT ? -ESRCH : -errno;
1035 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
1037 FOREACH_LINE(line, f, return -errno) {
1043 e = startswith(line, "0:");
1053 const char *word, *state;
1056 l = strchr(line, ':');
1066 FOREACH_WORD_SEPARATOR(word, k, l, ",", state)
1067 if (k == cs && memcmp(word, controller_str, cs) == 0) {
1075 log_debug_elogind("Found %s:%s", line, e+1);
1080 /* Truncate suffix indicating the process is a zombie */
1081 e = endswith(p, " (deleted)");
1092 #if 0 /// UNNEEDED by elogind
1093 int cg_install_release_agent(const char *controller, const char *agent) {
1094 _cleanup_free_ char *fs = NULL, *contents = NULL;
1100 r = cg_unified_controller(controller);
1103 if (r > 0) /* doesn't apply to unified hierarchy */
1106 r = cg_get_path(controller, NULL, "release_agent", &fs);
1110 r = read_one_line_file(fs, &contents);
1114 sc = strstrip(contents);
1116 r = write_string_file(fs, agent, 0);
1119 } else if (!path_equal(sc, agent))
1123 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1127 contents = mfree(contents);
1128 r = read_one_line_file(fs, &contents);
1132 sc = strstrip(contents);
1133 if (streq(sc, "0")) {
1134 r = write_string_file(fs, "1", 0);
1141 if (!streq(sc, "1"))
1147 int cg_uninstall_release_agent(const char *controller) {
1148 _cleanup_free_ char *fs = NULL;
1151 r = cg_unified_controller(controller);
1154 if (r > 0) /* Doesn't apply to unified hierarchy */
1157 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1161 r = write_string_file(fs, "0", 0);
1167 r = cg_get_path(controller, NULL, "release_agent", &fs);
1171 r = write_string_file(fs, "", 0);
1179 int cg_is_empty(const char *controller, const char *path) {
1180 _cleanup_fclose_ FILE *f = NULL;
1186 r = cg_enumerate_processes(controller, path, &f);
1192 r = cg_read_pid(f, &pid);
1199 int cg_is_empty_recursive(const char *controller, const char *path) {
1204 /* The root cgroup is always populated */
1205 if (controller && empty_or_root(path))
1208 r = cg_unified_controller(controller);
1212 _cleanup_free_ char *t = NULL;
1214 /* On the unified hierarchy we can check empty state
1215 * via the "populated" attribute of "cgroup.events". */
1217 r = cg_read_event(controller, path, "populated", &t);
1221 return streq(t, "0");
1223 _cleanup_closedir_ DIR *d = NULL;
1226 r = cg_is_empty(controller, path);
1230 r = cg_enumerate_subgroups(controller, path, &d);
1236 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1237 _cleanup_free_ char *p = NULL;
1239 p = strjoin(path, "/", fn);
1244 r = cg_is_empty_recursive(controller, p);
1255 int cg_split_spec(const char *spec, char **controller, char **path) {
1256 char *t = NULL, *u = NULL;
1262 if (!path_is_normalized(spec))
1270 *path = path_kill_slashes(t);
1279 e = strchr(spec, ':');
1281 if (!cg_controller_is_valid(spec))
1298 t = strndup(spec, e-spec);
1301 if (!cg_controller_is_valid(t)) {
1315 if (!path_is_normalized(u) ||
1316 !path_is_absolute(u)) {
1322 path_kill_slashes(u);
1338 int cg_mangle_path(const char *path, char **result) {
1339 _cleanup_free_ char *c = NULL, *p = NULL;
1346 /* First, check if it already is a filesystem path */
1347 if (path_startswith(path, "/sys/fs/cgroup")) {
1353 *result = path_kill_slashes(t);
1357 /* Otherwise, treat it as cg spec */
1358 r = cg_split_spec(path, &c, &p);
1362 return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result);
1365 int cg_get_root_path(char **path) {
1371 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1375 #if 0 /// elogind does not support systemd scopes and slices
1376 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1378 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1380 e = endswith(p, "/system"); /* even more legacy */
1382 e = endswith(p, "/elogind");
1391 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1392 _cleanup_free_ char *rt = NULL;
1400 /* If the root was specified let's use that, otherwise
1401 * let's determine it from PID 1 */
1403 r = cg_get_root_path(&rt);
1408 log_debug_elogind("Determined root path: \"%s\"", root);
1411 p = path_startswith(cgroup, root);
1412 #if 0 /// With other controllers, elogind might end up in /elogind, and *p is 0
1413 if (p && p > cgroup)
1415 if (p && p[0] && (p > cgroup))
1424 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1425 _cleanup_free_ char *raw = NULL;
1432 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1436 log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
1437 raw, pid, root ? root : "NULL");
1438 r = cg_shift_path(raw, root, &c);
1443 *cgroup = TAKE_PTR(raw);
1453 log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
1458 int cg_path_decode_unit(const char *cgroup, char **unit) {
1465 #if 0 /// elogind has a different naming: <controller>:/<session id>. So prefix is always len < 3
1466 n = strcspn(cgroup, "/");
1470 n = strspn(cgroup, "/") + 1;
1473 c = strndupa(cgroup, n);
1476 #if 0 /// elogind session ids are never valid unit names.
1477 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1489 static bool valid_slice_name(const char *p, size_t n) {
1494 if (n < STRLEN("x.slice"))
1497 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1503 c = cg_unescape(buf);
1505 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1511 static const char *skip_slices(const char *p) {
1514 /* Skips over all slice assignments */
1519 p += strspn(p, "/");
1521 n = strcspn(p, "/");
1522 if (!valid_slice_name(p, n))
1529 int cg_path_get_unit(const char *path, char **ret) {
1537 e = skip_slices(path);
1539 r = cg_path_decode_unit(e, &unit);
1543 /* We skipped over the slices, don't accept any now */
1544 if (endswith(unit, ".slice")) {
1553 int cg_pid_get_unit(pid_t pid, char **unit) {
1554 _cleanup_free_ char *cgroup = NULL;
1559 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1563 return cg_path_get_unit(cgroup, unit);
1566 #if 0 /// UNNEEDED by elogind
1568 * Skip session-*.scope, but require it to be there.
1570 static const char *skip_session(const char *p) {
1576 p += strspn(p, "/");
1578 n = strcspn(p, "/");
1579 if (n < STRLEN("session-x.scope"))
1582 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1583 char buf[n - 8 - 6 + 1];
1585 memcpy(buf, p + 8, n - 8 - 6);
1588 /* Note that session scopes never need unescaping,
1589 * since they cannot conflict with the kernel's own
1590 * names, hence we don't need to call cg_unescape()
1593 if (!session_id_valid(buf))
1597 p += strspn(p, "/");
1605 * Skip user@*.service, but require it to be there.
1607 static const char *skip_user_manager(const char *p) {
1613 p += strspn(p, "/");
1615 n = strcspn(p, "/");
1616 if (n < STRLEN("user@x.service"))
1619 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1620 char buf[n - 5 - 8 + 1];
1622 memcpy(buf, p + 5, n - 5 - 8);
1625 /* Note that user manager services never need unescaping,
1626 * since they cannot conflict with the kernel's own
1627 * names, hence we don't need to call cg_unescape()
1630 if (parse_uid(buf, NULL) < 0)
1634 p += strspn(p, "/");
1642 static const char *skip_user_prefix(const char *path) {
1647 /* Skip slices, if there are any */
1648 e = skip_slices(path);
1650 /* Skip the user manager, if it's in the path now... */
1651 t = skip_user_manager(e);
1655 /* Alternatively skip the user session if it is in the path... */
1656 return skip_session(e);
1659 int cg_path_get_user_unit(const char *path, char **ret) {
1665 t = skip_user_prefix(path);
1669 /* And from here on it looks pretty much the same as for a
1670 * system unit, hence let's use the same parser from here
1672 return cg_path_get_unit(t, ret);
1675 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1676 _cleanup_free_ char *cgroup = NULL;
1681 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1685 return cg_path_get_user_unit(cgroup, unit);
1688 int cg_path_get_machine_name(const char *path, char **machine) {
1689 _cleanup_free_ char *u = NULL;
1693 r = cg_path_get_unit(path, &u);
1697 sl = strjoina("/run/systemd/machines/unit:", u);
1698 return readlink_malloc(sl, machine);
1701 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1702 _cleanup_free_ char *cgroup = NULL;
1707 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1711 return cg_path_get_machine_name(cgroup, machine);
1715 int cg_path_get_session(const char *path, char **session) {
1716 #if 0 /// UNNEEDED by elogind
1717 _cleanup_free_ char *unit = NULL;
1723 r = cg_path_get_unit(path, &unit);
1727 start = startswith(unit, "session-");
1730 end = endswith(start, ".scope");
1735 if (!session_id_valid(start))
1738 /* Elogind uses a flat hierarchy, just "/SESSION". The only
1739 wrinkle is that SESSION might be escaped. */
1740 const char *e, *n, *start;
1743 log_debug_elogind("path is \"%s\"", path);
1744 assert(path[0] == '/');
1747 n = strchrnul(e, '/');
1751 start = strndupa(e, n - e);
1752 start = cg_unescape(start);
1761 log_debug_elogind("found session: \"%s\"", start);
1772 int cg_pid_get_session(pid_t pid, char **session) {
1773 _cleanup_free_ char *cgroup = NULL;
1776 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1780 return cg_path_get_session(cgroup, session);
1783 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1784 #if 0 /// elogind needs one more value
1785 _cleanup_free_ char *slice = NULL;
1788 _cleanup_free_ char *slice = NULL, *p = NULL, *s = NULL;
1794 r = cg_path_get_slice(path, &slice);
1798 #if 0 /// elogind does not support systemd slices
1799 start = startswith(slice, "user-");
1802 end = endswith(start, ".slice");
1807 if (parse_uid(start, uid) < 0)
1810 p = strappend("/run/systemd/sessions/", slice);
1812 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
1820 if (parse_uid(s, uid) < 0)
1827 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1828 _cleanup_free_ char *cgroup = NULL;
1831 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1835 return cg_path_get_owner_uid(cgroup, uid);
1838 int cg_path_get_slice(const char *p, char **slice) {
1839 const char *e = NULL;
1844 #if 0 /// elogind does not support systemd slices
1845 /* Finds the right-most slice unit from the beginning, but
1846 * stops before we come to the first non-slice unit. */
1851 p += strspn(p, "/");
1853 n = strcspn(p, "/");
1854 if (!valid_slice_name(p, n)) {
1859 s = strdup(SPECIAL_ROOT_SLICE);
1867 return cg_path_decode_unit(e, slice);
1874 /* In elogind, what is reported here, is the location of
1875 * the session. This is derived from /proc/<self|PID>/cgroup.
1876 * In there we look at the controller, which will look something
1877 * like "1:name=openrc:/3".
1878 * The last part gets extracted (and is now p), which is "/3" in
1879 * this case. The three is the session id, and that can be mapped.
1881 e = startswith(p, "/");
1892 int cg_pid_get_slice(pid_t pid, char **slice) {
1893 _cleanup_free_ char *cgroup = NULL;
1898 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1899 log_debug_elogind("Found cgroup %s for pid %u (result %d)",
1904 return cg_path_get_slice(cgroup, slice);
1907 int cg_path_get_user_slice(const char *p, char **slice) {
1908 #if 0 /// UNNEEDED by elogind
1914 #if 0 /// nothing to skip in elogind
1915 t = skip_user_prefix(p);
1920 #if 0 /// UNNEEDED by elogind
1921 /* And now it looks pretty much the same as for a system
1922 * slice, so let's just use the same parser from here on. */
1923 return cg_path_get_slice(t, slice);
1925 /* In elogind there is nothing to skip, we can use the path
1926 * directly. Generally speaking this is always a session id
1927 * to user mapping. */
1928 return cg_path_get_slice(p, slice);
1932 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1933 _cleanup_free_ char *cgroup = NULL;
1938 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1942 return cg_path_get_user_slice(cgroup, slice);
1945 char *cg_escape(const char *p) {
1946 bool need_prefix = false;
1948 /* This implements very minimal escaping for names to be used
1949 * as file names in the cgroup tree: any name which might
1950 * conflict with a kernel name or is prefixed with '_' is
1951 * prefixed with a '_'. That way, when reading cgroup names it
1952 * is sufficient to remove a single prefixing underscore if
1955 /* The return value of this function (unlike cg_unescape())
1958 if (IN_SET(p[0], 0, '_', '.') ||
1959 streq(p, "notify_on_release") ||
1960 streq(p, "release_agent") ||
1961 streq(p, "tasks") ||
1962 startswith(p, "cgroup."))
1967 dot = strrchr(p, '.');
1972 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1975 n = cgroup_controller_to_string(c);
1980 if (memcmp(p, n, l) != 0)
1990 return strappend("_", p);
1995 char *cg_unescape(const char *p) {
1998 /* The return value of this function (unlike cg_escape())
1999 * doesn't need free()! */
2007 #define CONTROLLER_VALID \
2011 bool cg_controller_is_valid(const char *p) {
2017 if (streq(p, SYSTEMD_CGROUP_CONTROLLER))
2020 s = startswith(p, "name=");
2024 if (IN_SET(*p, 0, '_'))
2027 for (t = p; *t; t++)
2028 if (!strchr(CONTROLLER_VALID, *t))
2031 if (t - p > FILENAME_MAX)
2037 #if 0 /// UNNEEDED by elogind
2038 int cg_slice_to_path(const char *unit, char **ret) {
2039 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
2046 if (streq(unit, SPECIAL_ROOT_SLICE)) {
2056 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
2059 if (!endswith(unit, ".slice"))
2062 r = unit_name_to_prefix(unit, &p);
2066 dash = strchr(p, '-');
2068 /* Don't allow initial dashes */
2073 _cleanup_free_ char *escaped = NULL;
2074 char n[dash - p + sizeof(".slice")];
2076 #if HAS_FEATURE_MEMORY_SANITIZER
2077 /* msan doesn't instrument stpncpy, so it thinks
2078 * n is later used unitialized:
2079 * https://github.com/google/sanitizers/issues/926
2084 /* Don't allow trailing or double dashes */
2085 if (IN_SET(dash[1], 0, '-'))
2088 strcpy(stpncpy(n, p, dash - p), ".slice");
2089 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
2092 escaped = cg_escape(n);
2096 if (!strextend(&s, escaped, "/", NULL))
2099 dash = strchr(dash+1, '-');
2102 e = cg_escape(unit);
2106 if (!strextend(&s, e, NULL))
2115 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
2116 _cleanup_free_ char *p = NULL;
2119 r = cg_get_path(controller, path, attribute, &p);
2123 return write_string_file(p, value, 0);
2126 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
2127 _cleanup_free_ char *p = NULL;
2130 r = cg_get_path(controller, path, attribute, &p);
2134 return read_one_line_file(p, ret);
2137 #if 0 /// UNNEEDED by elogind
2138 int cg_get_keyed_attribute(
2139 const char *controller,
2141 const char *attribute,
2143 char **ret_values) {
2145 _cleanup_free_ char *filename = NULL, *contents = NULL;
2147 size_t n, i, n_done = 0;
2151 /* Reads one or more fields of a cgroupsv2 keyed attribute file. The 'keys' parameter should be an strv with
2152 * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of
2153 * entries as 'keys'. On success each entry will be set to the value of the matching key.
2155 * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. */
2157 r = cg_get_path(controller, path, attribute, &filename);
2161 r = read_full_file(filename, &contents, NULL);
2165 n = strv_length(keys);
2166 if (n == 0) /* No keys to retrieve? That's easy, we are done then */
2169 /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */
2170 v = newa0(char*, n);
2172 for (p = contents; *p;) {
2173 const char *w = NULL;
2175 for (i = 0; i < n; i++)
2177 w = first_word(p, keys[i]);
2185 l = strcspn(w, NEWLINE);
2186 v[i] = strndup(w, l);
2198 p += strcspn(p, NEWLINE);
2200 p += strspn(p, NEWLINE);
2206 for (i = 0; i < n; i++)
2212 memcpy(ret_values, v, sizeof(char*) * n);
2217 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
2221 /* This one will create a cgroup in our private tree, but also
2222 * duplicate it in the trees specified in mask, and remove it
2225 /* First create the cgroup in our own hierarchy. */
2226 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
2230 /* If we are in the unified hierarchy, we are done now */
2231 r = cg_all_unified();
2237 /* Otherwise, do the same in the other hierarchies */
2238 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2239 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2242 n = cgroup_controller_to_string(c);
2245 (void) cg_create(n, path);
2246 else if (supported & bit)
2247 (void) cg_trim(n, path, true);
2253 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
2257 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
2261 r = cg_all_unified();
2267 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2268 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2269 const char *p = NULL;
2271 if (!(supported & bit))
2275 p = path_callback(bit, userdata);
2280 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
2286 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
2291 SET_FOREACH(pidp, pids, i) {
2292 pid_t pid = PTR_TO_PID(pidp);
2295 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
2296 if (q < 0 && r >= 0)
2303 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
2307 if (!path_equal(from, to)) {
2308 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE);
2313 q = cg_all_unified();
2319 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2320 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2321 const char *p = NULL;
2323 if (!(supported & bit))
2327 p = to_callback(bit, userdata);
2332 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0);
2338 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2342 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2346 q = cg_all_unified();
2352 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2353 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2355 if (!(supported & bit))
2358 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2365 int cg_mask_to_string(CGroupMask mask, char **ret) {
2366 _cleanup_free_ char *s = NULL;
2367 size_t n = 0, allocated = 0;
2378 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2382 if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
2385 k = cgroup_controller_to_string(c);
2388 if (!GREEDY_REALLOC(s, allocated, n + space + l + 1))
2393 memcpy(s + n + space, k, l);
2407 int cg_mask_from_string(const char *value, CGroupMask *mask) {
2412 _cleanup_free_ char *n = NULL;
2416 r = extract_first_word(&value, &n, NULL, 0);
2422 v = cgroup_controller_from_string(n);
2426 *mask |= CGROUP_CONTROLLER_TO_MASK(v);
2431 int cg_mask_supported(CGroupMask *ret) {
2432 CGroupMask mask = 0;
2435 /* Determines the mask of supported cgroup controllers. Only
2436 * includes controllers we can make sense of and that are
2437 * actually accessible. */
2439 r = cg_all_unified();
2443 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2445 /* In the unified hierarchy we can read the supported
2446 * and accessible controllers from a the top-level
2447 * cgroup attribute */
2449 r = cg_get_root_path(&root);
2453 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2457 r = read_one_line_file(path, &controllers);
2461 r = cg_mask_from_string(controllers, &mask);
2465 /* Currently, we support the cpu, memory, io and pids
2466 * controller in the unified hierarchy, mask
2467 * everything else off. */
2468 mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS;
2473 /* In the legacy hierarchy, we check whether which
2474 * hierarchies are mounted. */
2476 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2479 n = cgroup_controller_to_string(c);
2480 if (controller_is_accessible(n) >= 0)
2481 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2489 #if 0 /// UNNEEDED by elogind
2490 int cg_kernel_controllers(Set **ret) {
2491 _cleanup_set_free_free_ Set *controllers = NULL;
2492 _cleanup_fclose_ FILE *f = NULL;
2497 /* Determines the full list of kernel-known controllers. Might
2498 * include controllers we don't actually support, arbitrary
2499 * named hierarchies and controllers that aren't currently
2500 * accessible (because not mounted). */
2502 controllers = set_new(&string_hash_ops);
2506 f = fopen("/proc/cgroups", "re");
2508 if (errno == ENOENT) {
2516 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
2518 /* Ignore the header line */
2519 (void) read_line(f, (size_t) -1, NULL);
2526 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2531 if (ferror(f) && errno > 0)
2542 if (!cg_controller_is_valid(controller)) {
2547 r = set_consume(controllers, controller);
2552 *ret = TAKE_PTR(controllers);
2558 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
2560 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2561 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2562 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2563 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2565 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2566 * process management but disable the compat dual layout, we return %true on
2567 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2569 static thread_local bool unified_systemd_v232;
2571 static int cg_unified_update(void) {
2575 /* Checks if we support the unified hierarchy. Returns an
2576 * error when the cgroup hierarchies aren't mounted yet or we
2577 * have any other trouble determining if the unified hierarchy
2580 if (unified_cache >= CGROUP_UNIFIED_NONE)
2583 if (statfs("/sys/fs/cgroup/", &fs) < 0)
2584 return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\") failed: %m");
2586 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2587 log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
2588 unified_cache = CGROUP_UNIFIED_ALL;
2589 #if 0 /// The handling of cgroups is a bit different with elogind
2590 } else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2591 log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller");
2593 } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)
2594 || F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2596 if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 &&
2597 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2598 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2599 unified_systemd_v232 = false;
2601 #if 0 /// There is no sub-grouping within elogind
2602 if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0)
2603 return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m");
2605 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2606 log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)");
2607 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2608 unified_systemd_v232 = true;
2609 } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)) {
2610 log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy");
2611 unified_cache = CGROUP_UNIFIED_NONE;
2613 log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy",
2614 (unsigned long long) fs.f_type);
2615 unified_cache = CGROUP_UNIFIED_NONE;
2618 unified_cache = CGROUP_UNIFIED_NONE;
2622 log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2623 (unsigned long long) fs.f_type);
2630 int cg_unified_controller(const char *controller) {
2633 r = cg_unified_update();
2637 if (unified_cache == CGROUP_UNIFIED_NONE)
2640 if (unified_cache >= CGROUP_UNIFIED_ALL)
2643 #if 0 /// only if elogind is the controller we can use cgroups2 in hybrid mode
2644 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER);
2646 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER_HYBRID);
2650 int cg_all_unified(void) {
2653 r = cg_unified_update();
2657 return unified_cache >= CGROUP_UNIFIED_ALL;
2660 int cg_hybrid_unified(void) {
2663 r = cg_unified_update();
2667 return unified_cache == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232;
2670 int cg_unified_flush(void) {
2671 unified_cache = CGROUP_UNIFIED_UNKNOWN;
2673 return cg_unified_update();
2676 #if 0 /// UNNEEDED by elogind
2677 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2678 _cleanup_fclose_ FILE *f = NULL;
2679 _cleanup_free_ char *fs = NULL;
2688 r = cg_all_unified();
2691 if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */
2694 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2698 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2699 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2702 if (!(supported & bit))
2705 n = cgroup_controller_to_string(c);
2707 char s[1 + strlen(n) + 1];
2709 s[0] = mask & bit ? '+' : '-';
2713 f = fopen(fs, "we");
2715 log_debug_errno(errno, "Failed to open cgroup.subtree_control file of %s: %m", p);
2720 r = write_string_stream(f, s, 0);
2722 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2732 bool cg_is_unified_wanted(void) {
2733 static thread_local int wanted = -1;
2734 #if 0 /// UNNEEDED by elogind
2738 const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL;
2740 /* If we have a cached value, return that. */
2744 /* If the hierarchy is already mounted, then follow whatever
2745 * was chosen for it. */
2746 if (cg_unified_flush() >= 0)
2747 return (wanted = unified_cache >= CGROUP_UNIFIED_ALL);
2749 #if 0 /// elogind is not init and has no business with kernel command line
2750 /* Otherwise, let's see what the kernel command line has to say.
2751 * Since checking is expensive, cache a non-error result. */
2752 r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b);
2754 return (wanted = r > 0 ? b : is_default);
2760 bool cg_is_legacy_wanted(void) {
2761 static thread_local int wanted = -1;
2763 /* If we have a cached value, return that. */
2767 /* Check if we have cgroups2 already mounted. */
2768 if (cg_unified_flush() >= 0 &&
2769 unified_cache == CGROUP_UNIFIED_ALL)
2770 return (wanted = false);
2772 /* Otherwise, assume that at least partial legacy is wanted,
2773 * since cgroups2 should already be mounted at this point. */
2774 return (wanted = true);
2777 bool cg_is_hybrid_wanted(void) {
2778 static thread_local int wanted = -1;
2779 #if 0 /// UNNEEDED by elogind
2783 const bool is_default = DEFAULT_HIERARCHY >= CGROUP_UNIFIED_SYSTEMD;
2784 /* We default to true if the default is "hybrid", obviously,
2785 * but also when the default is "unified", because if we get
2786 * called, it means that unified hierarchy was not mounted. */
2788 /* If we have a cached value, return that. */
2792 /* If the hierarchy is already mounted, then follow whatever
2793 * was chosen for it. */
2794 if (cg_unified_flush() >= 0 &&
2795 unified_cache == CGROUP_UNIFIED_ALL)
2796 return (wanted = false);
2798 #if 0 /// elogind is not init and has no business with kernel command line
2799 /* Otherwise, let's see what the kernel command line has to say.
2800 * Since checking is expensive, cache a non-error result. */
2801 r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b);
2803 /* The meaning of the kernel option is reversed wrt. to the return value
2804 * of this function, hence the negation. */
2805 return (wanted = r > 0 ? !b : is_default);
2811 #if 0 /// UNNEEDED by elogind
2812 int cg_weight_parse(const char *s, uint64_t *ret) {
2817 *ret = CGROUP_WEIGHT_INVALID;
2821 r = safe_atou64(s, &u);
2825 if (u < CGROUP_WEIGHT_MIN || u > CGROUP_WEIGHT_MAX)
2832 const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2833 [CGROUP_IO_RBPS_MAX] = CGROUP_LIMIT_MAX,
2834 [CGROUP_IO_WBPS_MAX] = CGROUP_LIMIT_MAX,
2835 [CGROUP_IO_RIOPS_MAX] = CGROUP_LIMIT_MAX,
2836 [CGROUP_IO_WIOPS_MAX] = CGROUP_LIMIT_MAX,
2839 static const char* const cgroup_io_limit_type_table[_CGROUP_IO_LIMIT_TYPE_MAX] = {
2840 [CGROUP_IO_RBPS_MAX] = "IOReadBandwidthMax",
2841 [CGROUP_IO_WBPS_MAX] = "IOWriteBandwidthMax",
2842 [CGROUP_IO_RIOPS_MAX] = "IOReadIOPSMax",
2843 [CGROUP_IO_WIOPS_MAX] = "IOWriteIOPSMax",
2846 DEFINE_STRING_TABLE_LOOKUP(cgroup_io_limit_type, CGroupIOLimitType);
2848 int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2853 *ret = CGROUP_CPU_SHARES_INVALID;
2857 r = safe_atou64(s, &u);
2861 if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2868 int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2873 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2877 r = safe_atou64(s, &u);
2881 if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2889 bool is_cgroup_fs(const struct statfs *s) {
2890 return is_fs_type(s, CGROUP_SUPER_MAGIC) ||
2891 is_fs_type(s, CGROUP2_SUPER_MAGIC);
2894 bool fd_is_cgroup_fs(int fd) {
2897 if (fstatfs(fd, &s) < 0)
2900 return is_cgroup_fs(&s);
2903 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2904 [CGROUP_CONTROLLER_CPU] = "cpu",
2905 [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
2906 [CGROUP_CONTROLLER_IO] = "io",
2907 [CGROUP_CONTROLLER_BLKIO] = "blkio",
2908 [CGROUP_CONTROLLER_MEMORY] = "memory",
2909 [CGROUP_CONTROLLER_DEVICES] = "devices",
2910 [CGROUP_CONTROLLER_PIDS] = "pids",
2913 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);