1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <stdio_ext.h>
31 //#include <sys/statfs.h>
32 #include <sys/types.h>
33 #include <sys/xattr.h>
36 #include "alloc-util.h"
37 #include "cgroup-util.h"
39 #include "dirent-util.h"
40 #include "extract-word.h"
43 #include "format-util.h"
46 #include "login-util.h"
48 //#include "missing.h"
50 #include "parse-util.h"
51 #include "path-util.h"
52 #include "proc-cmdline.h"
53 #include "process-util.h"
55 //#include "special.h"
56 #include "stat-util.h"
57 #include "stdio-util.h"
58 #include "string-table.h"
59 #include "string-util.h"
61 #include "unit-name.h"
62 #include "user-util.h"
64 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
65 _cleanup_free_ char *fs = NULL;
71 r = cg_get_path(controller, path, "cgroup.procs", &fs);
83 int cg_read_pid(FILE *f, pid_t *_pid) {
86 /* Note that the cgroup.procs might contain duplicates! See
87 * cgroups.txt for details. */
93 if (fscanf(f, "%lu", &ul) != 1) {
98 return errno > 0 ? -errno : -EIO;
109 const char *controller,
114 _cleanup_free_ char *events = NULL, *content = NULL;
118 r = cg_get_path(controller, path, "cgroup.events", &events);
122 r = read_full_file(events, &content, NULL);
127 while ((line = strsep(&p, "\n"))) {
130 key = strsep(&line, " ");
134 if (strcmp(key, event))
144 #if 0 /// UNNEEDED by elogind
145 bool cg_ns_supported(void) {
146 static thread_local int enabled = -1;
151 if (access("/proc/self/ns/cgroup", F_OK) == 0)
160 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
161 _cleanup_free_ char *fs = NULL;
167 /* This is not recursive! */
169 r = cg_get_path(controller, path, NULL, &fs);
181 int cg_read_subgroup(DIR *d, char **fn) {
187 FOREACH_DIRENT_ALL(de, d, return -errno) {
190 if (de->d_type != DT_DIR)
193 if (dot_or_dot_dot(de->d_name))
196 b = strdup(de->d_name);
207 int cg_rmdir(const char *controller, const char *path) {
208 _cleanup_free_ char *p = NULL;
211 r = cg_get_path(controller, path, NULL, &p);
216 if (r < 0 && errno != ENOENT)
219 r = cg_hybrid_unified();
225 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
226 r = cg_rmdir(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path);
228 log_warning_errno(r, "Failed to remove compat systemd cgroup %s: %m", path);
235 const char *controller,
240 cg_kill_log_func_t log_kill,
243 _cleanup_set_free_ Set *allocated_set = NULL;
250 /* Don't send SIGCONT twice. Also, SIGKILL always works even when process is suspended, hence don't send
251 * SIGCONT on SIGKILL. */
252 if (IN_SET(sig, SIGCONT, SIGKILL))
253 flags &= ~CGROUP_SIGCONT;
255 /* This goes through the tasks list and kills them all. This
256 * is repeated until no further processes are added to the
257 * tasks list, to properly handle forking processes */
260 s = allocated_set = set_new(NULL);
265 my_pid = getpid_cached();
268 _cleanup_fclose_ FILE *f = NULL;
272 r = cg_enumerate_processes(controller, path, &f);
274 if (ret >= 0 && r != -ENOENT)
280 while ((r = cg_read_pid(f, &pid)) > 0) {
282 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
285 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
289 log_kill(pid, sig, userdata);
291 /* If we haven't killed this process yet, kill
293 if (kill(pid, sig) < 0) {
294 if (ret >= 0 && errno != ESRCH)
297 if (flags & CGROUP_SIGCONT)
298 (void) kill(pid, SIGCONT);
306 r = set_put(s, PID_TO_PTR(pid));
322 /* To avoid racing against processes which fork
323 * quicker than we can kill them we repeat this until
324 * no new pids need to be killed. */
331 int cg_kill_recursive(
332 const char *controller,
337 cg_kill_log_func_t log_kill,
340 _cleanup_set_free_ Set *allocated_set = NULL;
341 _cleanup_closedir_ DIR *d = NULL;
349 s = allocated_set = set_new(NULL);
354 ret = cg_kill(controller, path, sig, flags, s, log_kill, userdata);
356 r = cg_enumerate_subgroups(controller, path, &d);
358 if (ret >= 0 && r != -ENOENT)
364 while ((r = cg_read_subgroup(d, &fn)) > 0) {
365 _cleanup_free_ char *p = NULL;
367 p = strjoin(path, "/", fn);
372 r = cg_kill_recursive(controller, p, sig, flags, s, log_kill, userdata);
373 if (r != 0 && ret >= 0)
376 if (ret >= 0 && r < 0)
379 if (flags & CGROUP_REMOVE) {
380 r = cg_rmdir(controller, path);
381 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
396 _cleanup_set_free_ Set *s = NULL;
409 my_pid = getpid_cached();
411 log_debug_elogind("Migrating \"%s\"/\"%s\" to \"%s\"/\"%s\" (%s)",
412 cfrom, pfrom, cto, pto,
413 (flags & CGROUP_IGNORE_SELF)
414 ? "ignoring self" : "watching self");
416 _cleanup_fclose_ FILE *f = NULL;
420 r = cg_enumerate_processes(cfrom, pfrom, &f);
422 if (ret >= 0 && r != -ENOENT)
428 while ((r = cg_read_pid(f, &pid)) > 0) {
430 /* This might do weird stuff if we aren't a
431 * single-threaded program. However, we
432 * luckily know we are not */
433 if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
436 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
439 /* Ignore kernel threads. Since they can only
440 * exist in the root cgroup, we only check for
443 (isempty(pfrom) || path_equal(pfrom, "/")) &&
444 is_kernel_thread(pid) > 0)
447 r = cg_attach(cto, pto, pid);
449 if (ret >= 0 && r != -ESRCH)
456 r = set_put(s, PID_TO_PTR(pid));
476 int cg_migrate_recursive(
483 _cleanup_closedir_ DIR *d = NULL;
492 ret = cg_migrate(cfrom, pfrom, cto, pto, flags);
494 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
496 if (ret >= 0 && r != -ENOENT)
502 while ((r = cg_read_subgroup(d, &fn)) > 0) {
503 _cleanup_free_ char *p = NULL;
505 p = strjoin(pfrom, "/", fn);
510 r = cg_migrate_recursive(cfrom, p, cto, pto, flags);
511 if (r != 0 && ret >= 0)
515 if (r < 0 && ret >= 0)
518 if (flags & CGROUP_REMOVE) {
519 r = cg_rmdir(cfrom, pfrom);
520 if (r < 0 && ret >= 0 && !IN_SET(r, -ENOENT, -EBUSY))
527 int cg_migrate_recursive_fallback(
541 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, flags);
543 char prefix[strlen(pto) + 1];
545 /* This didn't work? Then let's try all prefixes of the destination */
547 PATH_FOREACH_PREFIX(prefix, pto) {
550 q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, flags);
559 static const char *controller_to_dirname(const char *controller) {
564 /* Converts a controller name to the directory name below
565 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
566 * just cuts off the name= prefixed used for named
567 * hierarchies, if it is specified. */
569 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
570 if (cg_hybrid_unified() > 0)
571 controller = SYSTEMD_CGROUP_CONTROLLER_HYBRID;
573 controller = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
576 e = startswith(controller, "name=");
583 static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) {
590 dn = controller_to_dirname(controller);
592 if (isempty(path) && isempty(suffix))
593 t = strappend("/sys/fs/cgroup/", dn);
594 else if (isempty(path))
595 t = strjoin("/sys/fs/cgroup/", dn, "/", suffix);
596 else if (isempty(suffix))
597 t = strjoin("/sys/fs/cgroup/", dn, "/", path);
599 t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix);
607 static int join_path_unified(const char *path, const char *suffix, char **fs) {
612 if (isempty(path) && isempty(suffix))
613 t = strdup("/sys/fs/cgroup");
614 else if (isempty(path))
615 t = strappend("/sys/fs/cgroup/", suffix);
616 else if (isempty(suffix))
617 t = strappend("/sys/fs/cgroup/", path);
619 t = strjoin("/sys/fs/cgroup/", path, "/", suffix);
627 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
635 /* If no controller is specified, we return the path
636 * *below* the controllers, without any prefix. */
638 if (!path && !suffix)
646 t = strjoin(path, "/", suffix);
650 *fs = path_kill_slashes(t);
654 if (!cg_controller_is_valid(controller))
657 r = cg_all_unified();
661 r = join_path_unified(path, suffix, fs);
663 r = join_path_legacy(controller, path, suffix, fs);
667 path_kill_slashes(*fs);
671 static int controller_is_accessible(const char *controller) {
676 /* Checks whether a specific controller is accessible,
677 * i.e. its hierarchy mounted. In the unified hierarchy all
678 * controllers are considered accessible, except for the named
681 if (!cg_controller_is_valid(controller))
684 r = cg_all_unified();
688 /* We don't support named hierarchies if we are using
689 * the unified hierarchy. */
691 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
694 if (startswith(controller, "name="))
700 dn = controller_to_dirname(controller);
701 cc = strjoina("/sys/fs/cgroup/", dn);
703 if (laccess(cc, F_OK) < 0)
710 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
716 /* Check if the specified controller is actually accessible */
717 r = controller_is_accessible(controller);
721 return cg_get_path(controller, path, suffix, fs);
724 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
729 if (typeflag != FTW_DP)
732 if (ftwbuf->level < 1)
739 int cg_trim(const char *controller, const char *path, bool delete_root) {
740 _cleanup_free_ char *fs = NULL;
745 r = cg_get_path(controller, path, NULL, &fs);
750 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
760 if (rmdir(fs) < 0 && errno != ENOENT)
764 q = cg_hybrid_unified();
767 if (q > 0 && streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
768 q = cg_trim(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, delete_root);
770 log_warning_errno(q, "Failed to trim compat systemd cgroup %s: %m", path);
776 int cg_create(const char *controller, const char *path) {
777 _cleanup_free_ char *fs = NULL;
780 r = cg_get_path_and_check(controller, path, NULL, &fs);
784 r = mkdir_parents(fs, 0755);
788 r = mkdir_errno_wrapper(fs, 0755);
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
886 const char *controller,
896 /* cgroupsv1, aka legacy/non-unified */
897 static const struct Attribute legacy_attributes[] = {
898 { "cgroup.procs", true },
900 { "cgroup.clone_children", false },
904 /* cgroupsv2, aka unified */
905 static const struct Attribute unified_attributes[] = {
906 { "cgroup.procs", true },
907 { "cgroup.subtree_control", true },
908 { "cgroup.threads", false },
912 static const struct Attribute* const attributes[] = {
913 [false] = legacy_attributes,
914 [true] = unified_attributes,
917 _cleanup_free_ char *fs = NULL;
918 const struct Attribute *i;
923 if (uid == UID_INVALID && gid == GID_INVALID)
926 unified = cg_unified_controller(controller);
930 /* Configure access to the cgroup itself */
931 r = cg_get_path(controller, path, NULL, &fs);
935 r = chmod_and_chown(fs, 0755, uid, gid);
939 /* Configure access to the cgroup's attributes */
940 for (i = attributes[unified]; i->name; i++) {
943 r = cg_get_path(controller, path, i->name, &fs);
947 r = chmod_and_chown(fs, 0644, uid, gid);
952 log_debug_errno(r, "Failed to set access on cgroup %s, ignoring: %m", fs);
956 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) {
957 r = cg_hybrid_unified();
961 /* Always propagate access mode from unified to legacy controller */
962 r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER_LEGACY, path, uid, gid);
964 log_debug_errno(r, "Failed to set access on compatibility elogind cgroup %s, ignoring: %m", path);
971 int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags) {
972 _cleanup_free_ char *fs = NULL;
977 assert(value || size <= 0);
979 r = cg_get_path(controller, path, NULL, &fs);
983 if (setxattr(fs, name, value, size, flags) < 0)
989 int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
990 _cleanup_free_ char *fs = NULL;
997 r = cg_get_path(controller, path, NULL, &fs);
1001 n = getxattr(fs, name, value, size);
1009 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
1010 _cleanup_fclose_ FILE *f = NULL;
1011 char line[LINE_MAX];
1012 #if 0 /// At elogind we do not want that (false alarm) "maybe uninitialized" warning
1013 const char *fs, *controller_str;
1015 const char *fs, *controller_str = NULL;
1024 if (!cg_controller_is_valid(controller))
1027 controller = SYSTEMD_CGROUP_CONTROLLER;
1029 unified = cg_unified_controller(controller);
1033 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
1034 controller_str = SYSTEMD_CGROUP_CONTROLLER_LEGACY;
1036 controller_str = controller;
1038 cs = strlen(controller_str);
1041 fs = procfs_file_alloca(pid, "cgroup");
1042 log_debug_elogind("Searching for PID %u in \"%s\" (controller \"%s\")",
1043 pid, fs, controller);
1044 f = fopen(fs, "re");
1046 return errno == ENOENT ? -ESRCH : -errno;
1048 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
1050 FOREACH_LINE(line, f, return -errno) {
1056 e = startswith(line, "0:");
1066 const char *word, *state;
1069 l = strchr(line, ':');
1079 FOREACH_WORD_SEPARATOR(word, k, l, ",", state)
1080 if (k == cs && memcmp(word, controller_str, cs) == 0) {
1088 log_debug_elogind("Found %s:%s", line, e+1);
1093 /* Truncate suffix indicating the process is a zombie */
1094 e = endswith(p, " (deleted)");
1105 #if 0 /// UNNEEDED by elogind
1106 int cg_install_release_agent(const char *controller, const char *agent) {
1107 _cleanup_free_ char *fs = NULL, *contents = NULL;
1113 r = cg_unified_controller(controller);
1116 if (r > 0) /* doesn't apply to unified hierarchy */
1119 r = cg_get_path(controller, NULL, "release_agent", &fs);
1123 r = read_one_line_file(fs, &contents);
1127 sc = strstrip(contents);
1129 r = write_string_file(fs, agent, 0);
1132 } else if (!path_equal(sc, agent))
1136 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1140 contents = mfree(contents);
1141 r = read_one_line_file(fs, &contents);
1145 sc = strstrip(contents);
1146 if (streq(sc, "0")) {
1147 r = write_string_file(fs, "1", 0);
1154 if (!streq(sc, "1"))
1160 int cg_uninstall_release_agent(const char *controller) {
1161 _cleanup_free_ char *fs = NULL;
1164 r = cg_unified_controller(controller);
1167 if (r > 0) /* Doesn't apply to unified hierarchy */
1170 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
1174 r = write_string_file(fs, "0", 0);
1180 r = cg_get_path(controller, NULL, "release_agent", &fs);
1184 r = write_string_file(fs, "", 0);
1192 int cg_is_empty(const char *controller, const char *path) {
1193 _cleanup_fclose_ FILE *f = NULL;
1199 r = cg_enumerate_processes(controller, path, &f);
1205 r = cg_read_pid(f, &pid);
1212 int cg_is_empty_recursive(const char *controller, const char *path) {
1217 /* The root cgroup is always populated */
1218 if (controller && (isempty(path) || path_equal(path, "/")))
1221 r = cg_unified_controller(controller);
1225 _cleanup_free_ char *t = NULL;
1227 /* On the unified hierarchy we can check empty state
1228 * via the "populated" attribute of "cgroup.events". */
1230 r = cg_read_event(controller, path, "populated", &t);
1234 return streq(t, "0");
1236 _cleanup_closedir_ DIR *d = NULL;
1239 r = cg_is_empty(controller, path);
1243 r = cg_enumerate_subgroups(controller, path, &d);
1249 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1250 _cleanup_free_ char *p = NULL;
1252 p = strjoin(path, "/", fn);
1257 r = cg_is_empty_recursive(controller, p);
1268 int cg_split_spec(const char *spec, char **controller, char **path) {
1269 char *t = NULL, *u = NULL;
1275 if (!path_is_normalized(spec))
1283 *path = path_kill_slashes(t);
1292 e = strchr(spec, ':');
1294 if (!cg_controller_is_valid(spec))
1311 t = strndup(spec, e-spec);
1314 if (!cg_controller_is_valid(t)) {
1328 if (!path_is_normalized(u) ||
1329 !path_is_absolute(u)) {
1335 path_kill_slashes(u);
1351 int cg_mangle_path(const char *path, char **result) {
1352 _cleanup_free_ char *c = NULL, *p = NULL;
1359 /* First, check if it already is a filesystem path */
1360 if (path_startswith(path, "/sys/fs/cgroup")) {
1366 *result = path_kill_slashes(t);
1370 /* Otherwise, treat it as cg spec */
1371 r = cg_split_spec(path, &c, &p);
1375 return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result);
1378 int cg_get_root_path(char **path) {
1384 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1388 #if 0 /// elogind does not support systemd scopes and slices
1389 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1391 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1393 e = endswith(p, "/system"); /* even more legacy */
1395 e = endswith(p, "/elogind");
1404 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1405 _cleanup_free_ char *rt = NULL;
1413 /* If the root was specified let's use that, otherwise
1414 * let's determine it from PID 1 */
1416 r = cg_get_root_path(&rt);
1421 log_debug_elogind("Determined root path: \"%s\"", root);
1424 p = path_startswith(cgroup, root);
1425 #if 0 /// With other controllers, elogind might end up in /elogind, and *p is 0
1426 if (p && p > cgroup)
1428 if (p && p[0] && (p > cgroup))
1437 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1438 _cleanup_free_ char *raw = NULL;
1445 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1449 log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
1450 raw, pid, root ? root : "NULL");
1451 r = cg_shift_path(raw, root, &c);
1467 log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
1472 int cg_path_decode_unit(const char *cgroup, char **unit) {
1479 #if 0 /// elogind has a different naming: <controller>:/<session id>. So prefix is always len < 3
1480 n = strcspn(cgroup, "/");
1484 n = strspn(cgroup, "/") + 1;
1487 c = strndupa(cgroup, n);
1490 #if 0 /// elogind session ids are never valid unit names.
1491 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1503 static bool valid_slice_name(const char *p, size_t n) {
1508 if (n < STRLEN("x.slice"))
1511 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1517 c = cg_unescape(buf);
1519 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1525 static const char *skip_slices(const char *p) {
1528 /* Skips over all slice assignments */
1533 p += strspn(p, "/");
1535 n = strcspn(p, "/");
1536 if (!valid_slice_name(p, n))
1543 int cg_path_get_unit(const char *path, char **ret) {
1551 e = skip_slices(path);
1553 r = cg_path_decode_unit(e, &unit);
1557 /* We skipped over the slices, don't accept any now */
1558 if (endswith(unit, ".slice")) {
1567 int cg_pid_get_unit(pid_t pid, char **unit) {
1568 _cleanup_free_ char *cgroup = NULL;
1573 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1577 return cg_path_get_unit(cgroup, unit);
1580 #if 0 /// UNNEEDED by elogind
1582 * Skip session-*.scope, but require it to be there.
1584 static const char *skip_session(const char *p) {
1590 p += strspn(p, "/");
1592 n = strcspn(p, "/");
1593 if (n < STRLEN("session-x.scope"))
1596 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1597 char buf[n - 8 - 6 + 1];
1599 memcpy(buf, p + 8, n - 8 - 6);
1602 /* Note that session scopes never need unescaping,
1603 * since they cannot conflict with the kernel's own
1604 * names, hence we don't need to call cg_unescape()
1607 if (!session_id_valid(buf))
1611 p += strspn(p, "/");
1619 * Skip user@*.service, but require it to be there.
1621 static const char *skip_user_manager(const char *p) {
1627 p += strspn(p, "/");
1629 n = strcspn(p, "/");
1630 if (n < STRLEN("user@x.service"))
1633 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1634 char buf[n - 5 - 8 + 1];
1636 memcpy(buf, p + 5, n - 5 - 8);
1639 /* Note that user manager services never need unescaping,
1640 * since they cannot conflict with the kernel's own
1641 * names, hence we don't need to call cg_unescape()
1644 if (parse_uid(buf, NULL) < 0)
1648 p += strspn(p, "/");
1656 static const char *skip_user_prefix(const char *path) {
1661 /* Skip slices, if there are any */
1662 e = skip_slices(path);
1664 /* Skip the user manager, if it's in the path now... */
1665 t = skip_user_manager(e);
1669 /* Alternatively skip the user session if it is in the path... */
1670 return skip_session(e);
1673 int cg_path_get_user_unit(const char *path, char **ret) {
1679 t = skip_user_prefix(path);
1683 /* And from here on it looks pretty much the same as for a
1684 * system unit, hence let's use the same parser from here
1686 return cg_path_get_unit(t, ret);
1689 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1690 _cleanup_free_ char *cgroup = NULL;
1695 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1699 return cg_path_get_user_unit(cgroup, unit);
1702 int cg_path_get_machine_name(const char *path, char **machine) {
1703 _cleanup_free_ char *u = NULL;
1707 r = cg_path_get_unit(path, &u);
1711 sl = strjoina("/run/systemd/machines/unit:", u);
1712 return readlink_malloc(sl, machine);
1715 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1716 _cleanup_free_ char *cgroup = NULL;
1721 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1725 return cg_path_get_machine_name(cgroup, machine);
1729 int cg_path_get_session(const char *path, char **session) {
1730 #if 0 /// UNNEEDED by elogind
1731 _cleanup_free_ char *unit = NULL;
1737 r = cg_path_get_unit(path, &unit);
1741 start = startswith(unit, "session-");
1744 end = endswith(start, ".scope");
1749 if (!session_id_valid(start))
1752 /* Elogind uses a flat hierarchy, just "/SESSION". The only
1753 wrinkle is that SESSION might be escaped. */
1754 const char *e, *n, *start;
1757 log_debug_elogind("path is \"%s\"", path);
1758 assert(path[0] == '/');
1761 n = strchrnul(e, '/');
1765 start = strndupa(e, n - e);
1766 start = cg_unescape(start);
1775 log_debug_elogind("found session: \"%s\"", start);
1786 int cg_pid_get_session(pid_t pid, char **session) {
1787 _cleanup_free_ char *cgroup = NULL;
1790 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1794 return cg_path_get_session(cgroup, session);
1797 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1798 #if 0 /// elogind needs one more value
1799 _cleanup_free_ char *slice = NULL;
1802 _cleanup_free_ char *slice = NULL, *p = NULL, *s = NULL;
1808 r = cg_path_get_slice(path, &slice);
1812 #if 0 /// elogind does not support systemd slices
1813 start = startswith(slice, "user-");
1816 end = endswith(start, ".slice");
1821 if (parse_uid(start, uid) < 0)
1824 p = strappend("/run/systemd/sessions/", slice);
1826 r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
1834 if (parse_uid(s, uid) < 0)
1841 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1842 _cleanup_free_ char *cgroup = NULL;
1845 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1849 return cg_path_get_owner_uid(cgroup, uid);
1852 int cg_path_get_slice(const char *p, char **slice) {
1853 const char *e = NULL;
1858 #if 0 /// elogind does not support systemd slices
1859 /* Finds the right-most slice unit from the beginning, but
1860 * stops before we come to the first non-slice unit. */
1865 p += strspn(p, "/");
1867 n = strcspn(p, "/");
1868 if (!valid_slice_name(p, n)) {
1873 s = strdup(SPECIAL_ROOT_SLICE);
1881 return cg_path_decode_unit(e, slice);
1888 /* In elogind, what is reported here, is the location of
1889 * the session. This is derived from /proc/<self|PID>/cgroup.
1890 * In there we look at the controller, which will look something
1891 * like "1:name=openrc:/3".
1892 * The last part gets extracted (and is now p), which is "/3" in
1893 * this case. The three is the session id, and that can be mapped.
1895 e = startswith(p, "/");
1906 int cg_pid_get_slice(pid_t pid, char **slice) {
1907 _cleanup_free_ char *cgroup = NULL;
1912 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1913 log_debug_elogind("Found cgroup %s for pid %u (result %d)",
1918 return cg_path_get_slice(cgroup, slice);
1921 int cg_path_get_user_slice(const char *p, char **slice) {
1922 #if 0 /// UNNEEDED by elogind
1928 #if 0 /// nothing to skip in elogind
1929 t = skip_user_prefix(p);
1934 #if 0 /// UNNEEDED by elogind
1935 /* And now it looks pretty much the same as for a system
1936 * slice, so let's just use the same parser from here on. */
1937 return cg_path_get_slice(t, slice);
1939 /* In elogind there is nothing to skip, we can use the path
1940 * directly. Generally speaking this is always a session id
1941 * to user mapping. */
1942 return cg_path_get_slice(p, slice);
1946 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1947 _cleanup_free_ char *cgroup = NULL;
1952 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1956 return cg_path_get_user_slice(cgroup, slice);
1959 char *cg_escape(const char *p) {
1960 bool need_prefix = false;
1962 /* This implements very minimal escaping for names to be used
1963 * as file names in the cgroup tree: any name which might
1964 * conflict with a kernel name or is prefixed with '_' is
1965 * prefixed with a '_'. That way, when reading cgroup names it
1966 * is sufficient to remove a single prefixing underscore if
1969 /* The return value of this function (unlike cg_unescape())
1972 if (IN_SET(p[0], 0, '_', '.') ||
1973 streq(p, "notify_on_release") ||
1974 streq(p, "release_agent") ||
1975 streq(p, "tasks") ||
1976 startswith(p, "cgroup."))
1981 dot = strrchr(p, '.');
1986 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1989 n = cgroup_controller_to_string(c);
1994 if (memcmp(p, n, l) != 0)
2004 return strappend("_", p);
2009 char *cg_unescape(const char *p) {
2012 /* The return value of this function (unlike cg_escape())
2013 * doesn't need free()! */
2021 #define CONTROLLER_VALID \
2025 bool cg_controller_is_valid(const char *p) {
2031 if (streq(p, SYSTEMD_CGROUP_CONTROLLER))
2034 s = startswith(p, "name=");
2038 if (IN_SET(*p, 0, '_'))
2041 for (t = p; *t; t++)
2042 if (!strchr(CONTROLLER_VALID, *t))
2045 if (t - p > FILENAME_MAX)
2051 #if 0 /// UNNEEDED by elogind
2052 int cg_slice_to_path(const char *unit, char **ret) {
2053 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
2060 if (streq(unit, SPECIAL_ROOT_SLICE)) {
2070 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
2073 if (!endswith(unit, ".slice"))
2076 r = unit_name_to_prefix(unit, &p);
2080 dash = strchr(p, '-');
2082 /* Don't allow initial dashes */
2087 _cleanup_free_ char *escaped = NULL;
2088 char n[dash - p + sizeof(".slice")];
2090 /* Don't allow trailing or double dashes */
2091 if (IN_SET(dash[1], 0, '-'))
2094 strcpy(stpncpy(n, p, dash - p), ".slice");
2095 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
2098 escaped = cg_escape(n);
2102 if (!strextend(&s, escaped, "/", NULL))
2105 dash = strchr(dash+1, '-');
2108 e = cg_escape(unit);
2112 if (!strextend(&s, e, NULL))
2122 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
2123 _cleanup_free_ char *p = NULL;
2126 r = cg_get_path(controller, path, attribute, &p);
2130 return write_string_file(p, value, 0);
2133 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
2134 _cleanup_free_ char *p = NULL;
2137 r = cg_get_path(controller, path, attribute, &p);
2141 return read_one_line_file(p, ret);
2144 #if 0 /// UNNEEDED by elogind
2145 int cg_get_keyed_attribute(
2146 const char *controller,
2148 const char *attribute,
2150 char **ret_values) {
2152 _cleanup_free_ char *filename = NULL, *contents = NULL;
2153 _cleanup_fclose_ FILE *f = NULL;
2155 size_t n, i, n_done = 0;
2159 /* Reads one or more fields of a cgroupsv2 keyed attribute file. The 'keys' parameter should be an strv with
2160 * all keys to retrieve. The 'ret_values' parameter should be passed as string size with the same number of
2161 * entries as 'keys'. On success each entry will be set to the value of the matching key.
2163 * If the attribute file doesn't exist at all returns ENOENT, if any key is not found returns ENXIO. */
2165 r = cg_get_path(controller, path, attribute, &filename);
2169 r = read_full_file(filename, &contents, NULL);
2173 n = strv_length(keys);
2174 if (n == 0) /* No keys to retrieve? That's easy, we are done then */
2177 /* Let's build this up in a temporary array for now in order not to clobber the return parameter on failure */
2178 v = newa0(char*, n);
2180 for (p = contents; *p;) {
2181 const char *w = NULL;
2183 for (i = 0; i < n; i++)
2185 w = first_word(p, keys[i]);
2193 l = strcspn(w, NEWLINE);
2194 v[i] = strndup(w, l);
2206 p += strcspn(p, NEWLINE);
2208 p += strspn(p, NEWLINE);
2214 for (i = 0; i < n; i++)
2220 memcpy(ret_values, v, sizeof(char*) * n);
2225 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
2229 /* This one will create a cgroup in our private tree, but also
2230 * duplicate it in the trees specified in mask, and remove it
2233 /* First create the cgroup in our own hierarchy. */
2234 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
2238 /* If we are in the unified hierarchy, we are done now */
2239 r = cg_all_unified();
2245 /* Otherwise, do the same in the other hierarchies */
2246 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2247 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2250 n = cgroup_controller_to_string(c);
2253 (void) cg_create(n, path);
2254 else if (supported & bit)
2255 (void) cg_trim(n, path, true);
2261 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
2265 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
2269 r = cg_all_unified();
2275 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2276 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2277 const char *p = NULL;
2279 if (!(supported & bit))
2283 p = path_callback(bit, userdata);
2288 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
2294 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
2299 SET_FOREACH(pidp, pids, i) {
2300 pid_t pid = PTR_TO_PID(pidp);
2303 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
2304 if (q < 0 && r >= 0)
2311 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
2315 if (!path_equal(from, to)) {
2316 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, CGROUP_REMOVE);
2321 q = cg_all_unified();
2327 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2328 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2329 const char *p = NULL;
2331 if (!(supported & bit))
2335 p = to_callback(bit, userdata);
2340 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, 0);
2346 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2350 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2354 q = cg_all_unified();
2360 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2361 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2363 if (!(supported & bit))
2366 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2373 int cg_mask_to_string(CGroupMask mask, char **ret) {
2374 _cleanup_free_ char *s = NULL;
2375 size_t n = 0, allocated = 0;
2386 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2390 if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
2393 k = cgroup_controller_to_string(c);
2396 if (!GREEDY_REALLOC(s, allocated, n + space + l + 1))
2401 memcpy(s + n + space, k, l);
2416 int cg_mask_from_string(const char *value, CGroupMask *mask) {
2421 _cleanup_free_ char *n = NULL;
2425 r = extract_first_word(&value, &n, NULL, 0);
2431 v = cgroup_controller_from_string(n);
2435 *mask |= CGROUP_CONTROLLER_TO_MASK(v);
2440 int cg_mask_supported(CGroupMask *ret) {
2441 CGroupMask mask = 0;
2444 /* Determines the mask of supported cgroup controllers. Only
2445 * includes controllers we can make sense of and that are
2446 * actually accessible. */
2448 r = cg_all_unified();
2452 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2454 /* In the unified hierarchy we can read the supported
2455 * and accessible controllers from a the top-level
2456 * cgroup attribute */
2458 r = cg_get_root_path(&root);
2462 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2466 r = read_one_line_file(path, &controllers);
2470 r = cg_mask_from_string(controllers, &mask);
2474 /* Currently, we support the cpu, memory, io and pids
2475 * controller in the unified hierarchy, mask
2476 * everything else off. */
2477 mask &= CGROUP_MASK_CPU | CGROUP_MASK_MEMORY | CGROUP_MASK_IO | CGROUP_MASK_PIDS;
2482 /* In the legacy hierarchy, we check whether which
2483 * hierarchies are mounted. */
2485 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2488 n = cgroup_controller_to_string(c);
2489 if (controller_is_accessible(n) >= 0)
2490 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2498 #if 0 /// UNNEEDED by elogind
2499 int cg_kernel_controllers(Set **ret) {
2500 _cleanup_set_free_free_ Set *controllers = NULL;
2501 _cleanup_fclose_ FILE *f = NULL;
2506 /* Determines the full list of kernel-known controllers. Might
2507 * include controllers we don't actually support, arbitrary
2508 * named hierarchies and controllers that aren't currently
2509 * accessible (because not mounted). */
2511 controllers = set_new(&string_hash_ops);
2515 f = fopen("/proc/cgroups", "re");
2517 if (errno == ENOENT) {
2525 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
2527 /* Ignore the header line */
2528 (void) read_line(f, (size_t) -1, NULL);
2535 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2540 if (ferror(f) && errno > 0)
2551 if (!cg_controller_is_valid(controller)) {
2556 r = set_consume(controllers, controller);
2568 static thread_local CGroupUnified unified_cache = CGROUP_UNIFIED_UNKNOWN;
2570 /* The hybrid mode was initially implemented in v232 and simply mounted cgroup v2 on /sys/fs/cgroup/systemd. This
2571 * unfortunately broke other tools (such as docker) which expected the v1 "name=systemd" hierarchy on
2572 * /sys/fs/cgroup/systemd. From v233 and on, the hybrid mode mountnbs v2 on /sys/fs/cgroup/unified and maintains
2573 * "name=systemd" hierarchy on /sys/fs/cgroup/systemd for compatibility with other tools.
2575 * To keep live upgrade working, we detect and support v232 layout. When v232 layout is detected, to keep cgroup v2
2576 * process management but disable the compat dual layout, we return %true on
2577 * cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) and %false on cg_hybrid_unified().
2579 static thread_local bool unified_systemd_v232;
2581 static int cg_unified_update(void) {
2585 /* Checks if we support the unified hierarchy. Returns an
2586 * error when the cgroup hierarchies aren't mounted yet or we
2587 * have any other trouble determining if the unified hierarchy
2590 if (unified_cache >= CGROUP_UNIFIED_NONE)
2593 if (statfs("/sys/fs/cgroup/", &fs) < 0)
2594 return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\" failed: %m");
2596 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2597 log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
2598 unified_cache = CGROUP_UNIFIED_ALL;
2599 #if 0 /// The handling of cgroups is a bit different with elogind
2600 } else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2601 log_debug("Found cgroup2 on /sys/fs/cgroup/unified, unified hierarchy for systemd controller");
2603 } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)
2604 || F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC)) {
2606 if (statfs("/sys/fs/cgroup/unified/", &fs) == 0 &&
2607 F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2608 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2609 unified_systemd_v232 = false;
2611 #if 0 /// There is no sub-grouping within elogind
2612 if (statfs("/sys/fs/cgroup/systemd/", &fs) < 0)
2613 return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/systemd\" failed: %m");
2615 if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
2616 log_debug("Found cgroup2 on /sys/fs/cgroup/systemd, unified hierarchy for systemd controller (v232 variant)");
2617 unified_cache = CGROUP_UNIFIED_SYSTEMD;
2618 unified_systemd_v232 = true;
2619 } else if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC)) {
2620 log_debug("Found cgroup on /sys/fs/cgroup/systemd, legacy hierarchy");
2621 unified_cache = CGROUP_UNIFIED_NONE;
2623 log_debug("Unexpected filesystem type %llx mounted on /sys/fs/cgroup/systemd, assuming legacy hierarchy",
2624 (unsigned long long) fs.f_type);
2625 unified_cache = CGROUP_UNIFIED_NONE;
2628 unified_cache = CGROUP_UNIFIED_NONE;
2632 log_debug("Unknown filesystem type %llx mounted on /sys/fs/cgroup.",
2633 (unsigned long long) fs.f_type);
2640 int cg_unified_controller(const char *controller) {
2643 r = cg_unified_update();
2647 if (unified_cache == CGROUP_UNIFIED_NONE)
2650 if (unified_cache >= CGROUP_UNIFIED_ALL)
2653 #if 0 /// only if elogind is the controller we can use cgroups2 in hybrid mode
2654 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER);
2656 return streq_ptr(controller, SYSTEMD_CGROUP_CONTROLLER_HYBRID);
2660 int cg_all_unified(void) {
2663 r = cg_unified_update();
2667 return unified_cache >= CGROUP_UNIFIED_ALL;
2670 int cg_hybrid_unified(void) {
2673 r = cg_unified_update();
2677 return unified_cache == CGROUP_UNIFIED_SYSTEMD && !unified_systemd_v232;
2680 int cg_unified_flush(void) {
2681 unified_cache = CGROUP_UNIFIED_UNKNOWN;
2683 return cg_unified_update();
2686 #if 0 /// UNNEEDED by elogind
2687 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2688 _cleanup_fclose_ FILE *f = NULL;
2689 _cleanup_free_ char *fs = NULL;
2698 r = cg_all_unified();
2701 if (r == 0) /* on the legacy hiearchy there's no joining of controllers defined */
2704 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2708 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2709 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2712 if (!(supported & bit))
2715 n = cgroup_controller_to_string(c);
2717 char s[1 + strlen(n) + 1];
2719 s[0] = mask & bit ? '+' : '-';
2723 f = fopen(fs, "we");
2725 log_debug_errno(errno, "Failed to open cgroup.subtree_control file of %s: %m", p);
2730 r = write_string_stream(f, s, 0);
2732 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2740 bool cg_is_unified_wanted(void) {
2741 static thread_local int wanted = -1;
2744 const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL;
2746 /* If we have a cached value, return that. */
2750 /* If the hierarchy is already mounted, then follow whatever
2751 * was chosen for it. */
2752 if (cg_unified_flush() >= 0)
2753 return (wanted = unified_cache >= CGROUP_UNIFIED_ALL);
2755 #if 0 /// elogind is not init and has no business with kernel command line
2756 /* Otherwise, let's see what the kernel command line has to say.
2757 * Since checking is expensive, cache a non-error result. */
2758 r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b);
2761 return (wanted = r > 0 ? b : is_default);
2764 bool cg_is_legacy_wanted(void) {
2765 static thread_local int wanted = -1;
2767 /* If we have a cached value, return that. */
2771 /* Check if we have cgroups2 already mounted. */
2772 if (cg_unified_flush() >= 0 &&
2773 unified_cache == CGROUP_UNIFIED_ALL)
2774 return (wanted = false);
2776 /* Otherwise, assume that at least partial legacy is wanted,
2777 * since cgroups2 should already be mounted at this point. */
2778 return (wanted = true);
2781 bool cg_is_hybrid_wanted(void) {
2782 static thread_local int wanted = -1;
2785 const bool is_default = DEFAULT_HIERARCHY >= CGROUP_UNIFIED_SYSTEMD;
2786 /* We default to true if the default is "hybrid", obviously,
2787 * but also when the default is "unified", because if we get
2788 * called, it means that unified hierarchy was not mounted. */
2790 /* If we have a cached value, return that. */
2794 /* If the hierarchy is already mounted, then follow whatever
2795 * was chosen for it. */
2796 if (cg_unified_flush() >= 0 &&
2797 unified_cache == CGROUP_UNIFIED_ALL)
2798 return (wanted = false);
2800 #if 0 /// elogind is not init and has no business with kernel command line
2801 /* Otherwise, let's see what the kernel command line has to say.
2802 * Since checking is expensive, cache a non-error result. */
2803 r = proc_cmdline_get_bool("systemd.legacy_systemd_cgroup_controller", &b);
2806 /* The meaning of the kernel option is reversed wrt. to the return value
2807 * of this function, hence the negation. */
2808 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);