1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
32 #include "alloc-util.h"
33 #include "cgroup-util.h"
34 #include "dirent-util.h"
35 #include "extract-word.h"
38 #include "formats-util.h"
40 #include "login-util.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "proc-cmdline.h"
46 #include "process-util.h"
48 //#include "special.h"
49 #include "stat-util.h"
50 #include "string-table.h"
51 #include "string-util.h"
52 #include "unit-name.h"
53 #include "user-util.h"
56 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
57 _cleanup_free_ char *fs = NULL;
63 r = cg_get_path(controller, path, "cgroup.procs", &fs);
75 int cg_read_pid(FILE *f, pid_t *_pid) {
78 /* Note that the cgroup.procs might contain duplicates! See
79 * cgroups.txt for details. */
85 if (fscanf(f, "%lu", &ul) != 1) {
90 return errno > 0 ? -errno : -EIO;
100 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
101 _cleanup_free_ char *fs = NULL;
107 /* This is not recursive! */
109 r = cg_get_path(controller, path, NULL, &fs);
121 int cg_read_subgroup(DIR *d, char **fn) {
127 FOREACH_DIRENT_ALL(de, d, return -errno) {
130 if (de->d_type != DT_DIR)
133 if (streq(de->d_name, ".") ||
134 streq(de->d_name, ".."))
137 b = strdup(de->d_name);
148 int cg_rmdir(const char *controller, const char *path) {
149 _cleanup_free_ char *p = NULL;
152 r = cg_get_path(controller, path, NULL, &p);
157 if (r < 0 && errno != ENOENT)
163 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
164 _cleanup_set_free_ Set *allocated_set = NULL;
171 /* This goes through the tasks list and kills them all. This
172 * is repeated until no further processes are added to the
173 * tasks list, to properly handle forking processes */
176 s = allocated_set = set_new(NULL);
184 _cleanup_fclose_ FILE *f = NULL;
188 r = cg_enumerate_processes(controller, path, &f);
190 if (ret >= 0 && r != -ENOENT)
196 while ((r = cg_read_pid(f, &pid)) > 0) {
198 if (ignore_self && pid == my_pid)
201 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
204 /* If we haven't killed this process yet, kill
206 if (kill(pid, sig) < 0) {
207 if (ret >= 0 && errno != ESRCH)
210 if (sigcont && sig != SIGKILL)
211 (void) kill(pid, SIGCONT);
219 r = set_put(s, PID_TO_PTR(pid));
235 /* To avoid racing against processes which fork
236 * quicker than we can kill them we repeat this until
237 * no new pids need to be killed. */
244 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
245 _cleanup_set_free_ Set *allocated_set = NULL;
246 _cleanup_closedir_ DIR *d = NULL;
254 s = allocated_set = set_new(NULL);
259 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
261 r = cg_enumerate_subgroups(controller, path, &d);
263 if (ret >= 0 && r != -ENOENT)
269 while ((r = cg_read_subgroup(d, &fn)) > 0) {
270 _cleanup_free_ char *p = NULL;
272 p = strjoin(path, "/", fn, NULL);
277 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
278 if (r != 0 && ret >= 0)
282 if (ret >= 0 && r < 0)
286 r = cg_rmdir(controller, path);
287 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
294 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
296 _cleanup_set_free_ Set *s = NULL;
311 log_debug_elogind("Migrating \"%s\"/\"%s\" to \"%s\"/\"%s\" (%s)",
312 cfrom, pfrom, cto, pto,
313 ignore_self ? "ignoring self" : "watching self");
316 _cleanup_fclose_ FILE *f = NULL;
320 r = cg_enumerate_processes(cfrom, pfrom, &f);
322 if (ret >= 0 && r != -ENOENT)
328 while ((r = cg_read_pid(f, &pid)) > 0) {
330 /* This might do weird stuff if we aren't a
331 * single-threaded program. However, we
332 * luckily know we are not */
333 if (ignore_self && pid == my_pid)
336 if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
339 /* Ignore kernel threads. Since they can only
340 * exist in the root cgroup, we only check for
343 (isempty(pfrom) || path_equal(pfrom, "/")) &&
344 is_kernel_thread(pid) > 0)
347 r = cg_attach(cto, pto, pid);
349 if (ret >= 0 && r != -ESRCH)
356 r = set_put(s, PID_TO_PTR(pid));
376 int cg_migrate_recursive(
384 _cleanup_closedir_ DIR *d = NULL;
393 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
395 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
397 if (ret >= 0 && r != -ENOENT)
403 while ((r = cg_read_subgroup(d, &fn)) > 0) {
404 _cleanup_free_ char *p = NULL;
406 p = strjoin(pfrom, "/", fn, NULL);
411 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
412 if (r != 0 && ret >= 0)
416 if (r < 0 && ret >= 0)
420 r = cg_rmdir(cfrom, pfrom);
421 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
428 int cg_migrate_recursive_fallback(
443 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
445 char prefix[strlen(pto) + 1];
447 /* This didn't work? Then let's try all prefixes of the destination */
449 PATH_FOREACH_PREFIX(prefix, pto) {
452 q = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
461 static const char *controller_to_dirname(const char *controller) {
466 /* Converts a controller name to the directory name below
467 * /sys/fs/cgroup/ we want to mount it to. Effectively, this
468 * just cuts off the name= prefixed used for named
469 * hierarchies, if it is specified. */
471 e = startswith(controller, "name=");
478 static int join_path_legacy(const char *controller, const char *path, const char *suffix, char **fs) {
485 dn = controller_to_dirname(controller);
487 if (isempty(path) && isempty(suffix))
488 t = strappend("/sys/fs/cgroup/", dn);
489 else if (isempty(path))
490 t = strjoin("/sys/fs/cgroup/", dn, "/", suffix, NULL);
491 else if (isempty(suffix))
492 t = strjoin("/sys/fs/cgroup/", dn, "/", path, NULL);
494 t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix, NULL);
502 static int join_path_unified(const char *path, const char *suffix, char **fs) {
507 if (isempty(path) && isempty(suffix))
508 t = strdup("/sys/fs/cgroup");
509 else if (isempty(path))
510 t = strappend("/sys/fs/cgroup/", suffix);
511 else if (isempty(suffix))
512 t = strappend("/sys/fs/cgroup/", path);
514 t = strjoin("/sys/fs/cgroup/", path, "/", suffix, NULL);
522 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
530 /* If no controller is specified, we return the path
531 * *below* the controllers, without any prefix. */
533 if (!path && !suffix)
541 t = strjoin(path, "/", suffix, NULL);
545 *fs = path_kill_slashes(t);
549 if (!cg_controller_is_valid(controller))
552 unified = cg_unified();
557 r = join_path_unified(path, suffix, fs);
559 r = join_path_legacy(controller, path, suffix, fs);
564 path_kill_slashes(*fs);
568 static int controller_is_accessible(const char *controller) {
573 /* Checks whether a specific controller is accessible,
574 * i.e. its hierarchy mounted. In the unified hierarchy all
575 * controllers are considered accessible, except for the named
578 if (!cg_controller_is_valid(controller))
581 unified = cg_unified();
585 /* We don't support named hierarchies if we are using
586 * the unified hierarchy. */
588 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
591 if (startswith(controller, "name="))
597 dn = controller_to_dirname(controller);
598 cc = strjoina("/sys/fs/cgroup/", dn);
600 if (laccess(cc, F_OK) < 0)
607 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
613 /* Check if the specified controller is actually accessible */
614 r = controller_is_accessible(controller);
618 return cg_get_path(controller, path, suffix, fs);
621 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
626 if (typeflag != FTW_DP)
629 if (ftwbuf->level < 1)
636 int cg_trim(const char *controller, const char *path, bool delete_root) {
637 _cleanup_free_ char *fs = NULL;
642 r = cg_get_path(controller, path, NULL, &fs);
647 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0) {
657 if (rmdir(fs) < 0 && errno != ENOENT)
664 int cg_create(const char *controller, const char *path) {
665 _cleanup_free_ char *fs = NULL;
668 r = cg_get_path_and_check(controller, path, NULL, &fs);
672 r = mkdir_parents(fs, 0755);
676 if (mkdir(fs, 0755) < 0) {
687 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
692 r = cg_create(controller, path);
696 q = cg_attach(controller, path, pid);
700 /* This does not remove the cgroup on failure */
704 int cg_attach(const char *controller, const char *path, pid_t pid) {
705 _cleanup_free_ char *fs = NULL;
706 char c[DECIMAL_STR_MAX(pid_t) + 2];
712 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
719 snprintf(c, sizeof(c), PID_FMT"\n", pid);
721 return write_string_file(fs, c, 0);
724 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
731 r = cg_attach(controller, path, pid);
733 char prefix[strlen(path) + 1];
735 /* This didn't work? Then let's try all prefixes of
738 PATH_FOREACH_PREFIX(prefix, path) {
741 q = cg_attach(controller, prefix, pid);
750 #if 0 /// UNNEEDED by elogind
751 int cg_set_group_access(
752 const char *controller,
758 _cleanup_free_ char *fs = NULL;
761 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
764 if (mode != MODE_INVALID)
767 r = cg_get_path(controller, path, NULL, &fs);
771 return chmod_and_chown(fs, mode, uid, gid);
774 int cg_set_task_access(
775 const char *controller,
781 _cleanup_free_ char *fs = NULL, *procs = NULL;
786 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
789 if (mode != MODE_INVALID)
792 r = cg_get_path(controller, path, "cgroup.procs", &fs);
796 r = chmod_and_chown(fs, mode, uid, gid);
800 unified = cg_unified();
806 /* Compatibility, Always keep values for "tasks" in sync with
808 if (cg_get_path(controller, path, "tasks", &procs) >= 0)
809 (void) chmod_and_chown(procs, mode, uid, gid);
815 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
816 _cleanup_fclose_ FILE *f = NULL;
825 unified = cg_unified();
830 if (!cg_controller_is_valid(controller))
833 controller = SYSTEMD_CGROUP_CONTROLLER;
835 cs = strlen(controller);
838 fs = procfs_file_alloca(pid, "cgroup");
839 log_debug_elogind("Searching for PID %u in \"%s\" (controller \"%s\")",
840 pid, fs, controller);
843 return errno == ENOENT ? -ESRCH : -errno;
845 FOREACH_LINE(line, f, return -errno) {
851 e = startswith(line, "0:");
861 const char *word, *state;
864 l = strchr(line, ':');
874 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
875 if (k == cs && memcmp(word, controller, cs) == 0) {
885 log_debug_elogind("Found %s:%s", line, e+1);
897 int cg_install_release_agent(const char *controller, const char *agent) {
898 _cleanup_free_ char *fs = NULL, *contents = NULL;
904 unified = cg_unified();
907 if (unified) /* doesn't apply to unified hierarchy */
910 r = cg_get_path(controller, NULL, "release_agent", &fs);
914 r = read_one_line_file(fs, &contents);
918 sc = strstrip(contents);
920 r = write_string_file(fs, agent, 0);
923 } else if (!path_equal(sc, agent))
927 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
931 contents = mfree(contents);
932 r = read_one_line_file(fs, &contents);
936 sc = strstrip(contents);
937 if (streq(sc, "0")) {
938 r = write_string_file(fs, "1", 0);
951 int cg_uninstall_release_agent(const char *controller) {
952 _cleanup_free_ char *fs = NULL;
955 unified = cg_unified();
958 if (unified) /* Doesn't apply to unified hierarchy */
961 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
965 r = write_string_file(fs, "0", 0);
971 r = cg_get_path(controller, NULL, "release_agent", &fs);
975 r = write_string_file(fs, "", 0);
982 int cg_is_empty(const char *controller, const char *path) {
983 _cleanup_fclose_ FILE *f = NULL;
989 r = cg_enumerate_processes(controller, path, &f);
995 r = cg_read_pid(f, &pid);
1002 int cg_is_empty_recursive(const char *controller, const char *path) {
1007 /* The root cgroup is always populated */
1008 if (controller && (isempty(path) || path_equal(path, "/")))
1011 unified = cg_unified();
1016 _cleanup_free_ char *populated = NULL, *t = NULL;
1018 /* On the unified hierarchy we can check empty state
1019 * via the "cgroup.populated" attribute. */
1021 r = cg_get_path(controller, path, "cgroup.populated", &populated);
1025 r = read_one_line_file(populated, &t);
1031 return streq(t, "0");
1033 _cleanup_closedir_ DIR *d = NULL;
1036 r = cg_is_empty(controller, path);
1040 r = cg_enumerate_subgroups(controller, path, &d);
1046 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1047 _cleanup_free_ char *p = NULL;
1049 p = strjoin(path, "/", fn, NULL);
1054 r = cg_is_empty_recursive(controller, p);
1065 int cg_split_spec(const char *spec, char **controller, char **path) {
1066 char *t = NULL, *u = NULL;
1072 if (!path_is_safe(spec))
1080 *path = path_kill_slashes(t);
1089 e = strchr(spec, ':');
1091 if (!cg_controller_is_valid(spec))
1108 t = strndup(spec, e-spec);
1111 if (!cg_controller_is_valid(t)) {
1125 if (!path_is_safe(u) ||
1126 !path_is_absolute(u)) {
1132 path_kill_slashes(u);
1148 int cg_mangle_path(const char *path, char **result) {
1149 _cleanup_free_ char *c = NULL, *p = NULL;
1156 /* First, check if it already is a filesystem path */
1157 if (path_startswith(path, "/sys/fs/cgroup")) {
1163 *result = path_kill_slashes(t);
1167 /* Otherwise, treat it as cg spec */
1168 r = cg_split_spec(path, &c, &p);
1172 return cg_get_path(c ?: SYSTEMD_CGROUP_CONTROLLER, p ?: "/", NULL, result);
1175 int cg_get_root_path(char **path) {
1176 #if 0 /// elogind does not support systemd scopes and slices
1182 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1186 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1188 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1190 e = endswith(p, "/system"); /* even more legacy */
1198 return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path);
1202 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1203 _cleanup_free_ char *rt = NULL;
1211 /* If the root was specified let's use that, otherwise
1212 * let's determine it from PID 1 */
1214 r = cg_get_root_path(&rt);
1219 log_debug_elogind("Determined root path: \"%s\"", root);
1222 p = path_startswith(cgroup, root);
1223 if (p && p[0] && (p > cgroup))
1231 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1232 _cleanup_free_ char *raw = NULL;
1239 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1243 log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
1244 raw, pid, root ? root : "NULL");
1245 r = cg_shift_path(raw, root, &c);
1261 log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
1266 #if 0 /// UNNEEDED by elogind
1267 int cg_path_decode_unit(const char *cgroup, char **unit){
1274 n = strcspn(cgroup, "/");
1278 c = strndupa(cgroup, n);
1281 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1292 static bool valid_slice_name(const char *p, size_t n) {
1297 if (n < strlen("x.slice"))
1300 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1306 c = cg_unescape(buf);
1308 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1314 static const char *skip_slices(const char *p) {
1317 /* Skips over all slice assignments */
1322 p += strspn(p, "/");
1324 n = strcspn(p, "/");
1325 if (!valid_slice_name(p, n))
1332 int cg_path_get_unit(const char *path, char **ret) {
1340 e = skip_slices(path);
1342 r = cg_path_decode_unit(e, &unit);
1346 /* We skipped over the slices, don't accept any now */
1347 if (endswith(unit, ".slice")) {
1356 int cg_pid_get_unit(pid_t pid, char **unit) {
1357 _cleanup_free_ char *cgroup = NULL;
1362 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1366 return cg_path_get_unit(cgroup, unit);
1370 * Skip session-*.scope, but require it to be there.
1372 static const char *skip_session(const char *p) {
1378 p += strspn(p, "/");
1380 n = strcspn(p, "/");
1381 if (n < strlen("session-x.scope"))
1384 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1385 char buf[n - 8 - 6 + 1];
1387 memcpy(buf, p + 8, n - 8 - 6);
1390 /* Note that session scopes never need unescaping,
1391 * since they cannot conflict with the kernel's own
1392 * names, hence we don't need to call cg_unescape()
1395 if (!session_id_valid(buf))
1399 p += strspn(p, "/");
1407 * Skip user@*.service, but require it to be there.
1409 static const char *skip_user_manager(const char *p) {
1415 p += strspn(p, "/");
1417 n = strcspn(p, "/");
1418 if (n < strlen("user@x.service"))
1421 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1422 char buf[n - 5 - 8 + 1];
1424 memcpy(buf, p + 5, n - 5 - 8);
1427 /* Note that user manager services never need unescaping,
1428 * since they cannot conflict with the kernel's own
1429 * names, hence we don't need to call cg_unescape()
1432 if (parse_uid(buf, NULL) < 0)
1436 p += strspn(p, "/");
1444 static const char *skip_user_prefix(const char *path) {
1449 /* Skip slices, if there are any */
1450 e = skip_slices(path);
1452 /* Skip the user manager, if it's in the path now... */
1453 t = skip_user_manager(e);
1457 /* Alternatively skip the user session if it is in the path... */
1458 return skip_session(e);
1461 int cg_path_get_user_unit(const char *path, char **ret) {
1467 t = skip_user_prefix(path);
1471 /* And from here on it looks pretty much the same as for a
1472 * system unit, hence let's use the same parser from here
1474 return cg_path_get_unit(t, ret);
1477 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1478 _cleanup_free_ char *cgroup = NULL;
1483 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1487 return cg_path_get_user_unit(cgroup, unit);
1490 int cg_path_get_machine_name(const char *path, char **machine) {
1491 _cleanup_free_ char *u = NULL;
1495 r = cg_path_get_unit(path, &u);
1499 sl = strjoina("/run/systemd/machines/unit:", u);
1500 return readlink_malloc(sl, machine);
1503 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1504 _cleanup_free_ char *cgroup = NULL;
1509 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1513 return cg_path_get_machine_name(cgroup, machine);
1517 int cg_path_get_session(const char *path, char **session) {
1518 /* Elogind uses a flat hierarchy, just "/SESSION". The only
1519 wrinkle is that SESSION might be escaped. */
1521 _cleanup_free_ char *unit = NULL;
1527 r = cg_path_get_unit(path, &unit);
1531 start = startswith(unit, "session-");
1534 end = endswith(start, ".scope");
1539 if (!session_id_valid(start))
1542 const char *e, *n, *start;
1545 log_debug_elogind("path is \"%s\"", path);
1546 assert(path[0] == '/');
1549 n = strchrnul(e, '/');
1553 start = strndupa(e, n - e);
1554 start = cg_unescape(start);
1563 log_debug_elogind("found session: \"%s\"", start);
1574 int cg_pid_get_session(pid_t pid, char **session) {
1575 _cleanup_free_ char *cgroup = NULL;
1578 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1582 return cg_path_get_session(cgroup, session);
1585 #if 0 /// UNNEEDED by elogind
1586 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1587 _cleanup_free_ char *slice = NULL;
1593 r = cg_path_get_slice(path, &slice);
1597 start = startswith(slice, "user-");
1600 end = endswith(start, ".slice");
1605 if (parse_uid(start, uid) < 0)
1611 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1612 _cleanup_free_ char *cgroup = NULL;
1615 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1619 return cg_path_get_owner_uid(cgroup, uid);
1622 int cg_path_get_slice(const char *p, char **slice) {
1623 const char *e = NULL;
1628 /* Finds the right-most slice unit from the beginning, but
1629 * stops before we come to the first non-slice unit. */
1634 p += strspn(p, "/");
1636 n = strcspn(p, "/");
1637 if (!valid_slice_name(p, n)) {
1642 s = strdup("-.slice");
1650 return cg_path_decode_unit(e, slice);
1658 int cg_pid_get_slice(pid_t pid, char **slice) {
1659 _cleanup_free_ char *cgroup = NULL;
1664 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1668 return cg_path_get_slice(cgroup, slice);
1671 int cg_path_get_user_slice(const char *p, char **slice) {
1676 t = skip_user_prefix(p);
1680 /* And now it looks pretty much the same as for a system
1681 * slice, so let's just use the same parser from here on. */
1682 return cg_path_get_slice(t, slice);
1685 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1686 _cleanup_free_ char *cgroup = NULL;
1691 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1695 return cg_path_get_user_slice(cgroup, slice);
1699 char *cg_escape(const char *p) {
1700 bool need_prefix = false;
1702 /* This implements very minimal escaping for names to be used
1703 * as file names in the cgroup tree: any name which might
1704 * conflict with a kernel name or is prefixed with '_' is
1705 * prefixed with a '_'. That way, when reading cgroup names it
1706 * is sufficient to remove a single prefixing underscore if
1709 /* The return value of this function (unlike cg_unescape())
1715 streq(p, "notify_on_release") ||
1716 streq(p, "release_agent") ||
1717 streq(p, "tasks") ||
1718 startswith(p, "cgroup."))
1723 dot = strrchr(p, '.');
1728 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1731 n = cgroup_controller_to_string(c);
1736 if (memcmp(p, n, l) != 0)
1746 return strappend("_", p);
1751 char *cg_unescape(const char *p) {
1754 /* The return value of this function (unlike cg_escape())
1755 * doesn't need free()! */
1763 #define CONTROLLER_VALID \
1767 bool cg_controller_is_valid(const char *p) {
1773 s = startswith(p, "name=");
1777 if (*p == 0 || *p == '_')
1780 for (t = p; *t; t++)
1781 if (!strchr(CONTROLLER_VALID, *t))
1784 if (t - p > FILENAME_MAX)
1790 #if 0 /// UNNEEDED by elogind
1791 int cg_slice_to_path(const char *unit, char **ret) {
1792 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1799 if (streq(unit, "-.slice")) {
1809 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1812 if (!endswith(unit, ".slice"))
1815 r = unit_name_to_prefix(unit, &p);
1819 dash = strchr(p, '-');
1821 /* Don't allow initial dashes */
1826 _cleanup_free_ char *escaped = NULL;
1827 char n[dash - p + sizeof(".slice")];
1829 /* Don't allow trailing or double dashes */
1830 if (dash[1] == 0 || dash[1] == '-')
1833 strcpy(stpncpy(n, p, dash - p), ".slice");
1834 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1837 escaped = cg_escape(n);
1841 if (!strextend(&s, escaped, "/", NULL))
1844 dash = strchr(dash+1, '-');
1847 e = cg_escape(unit);
1851 if (!strextend(&s, e, NULL))
1861 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1862 _cleanup_free_ char *p = NULL;
1865 r = cg_get_path(controller, path, attribute, &p);
1869 return write_string_file(p, value, 0);
1872 #if 0 /// UNNEEDED by elogind
1873 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1874 _cleanup_free_ char *p = NULL;
1877 r = cg_get_path(controller, path, attribute, &p);
1881 return read_one_line_file(p, ret);
1884 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
1888 /* This one will create a cgroup in our private tree, but also
1889 * duplicate it in the trees specified in mask, and remove it
1892 /* First create the cgroup in our own hierarchy. */
1893 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1897 /* If we are in the unified hierarchy, we are done now */
1898 unified = cg_unified();
1904 /* Otherwise, do the same in the other hierarchies */
1905 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1906 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1909 n = cgroup_controller_to_string(c);
1912 (void) cg_create(n, path);
1913 else if (supported & bit)
1914 (void) cg_trim(n, path, true);
1920 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1924 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1928 unified = cg_unified();
1934 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1935 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1936 const char *p = NULL;
1938 if (!(supported & bit))
1942 p = path_callback(bit, userdata);
1947 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
1953 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1958 SET_FOREACH(pidp, pids, i) {
1959 pid_t pid = PTR_TO_PID(pidp);
1962 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1963 if (q < 0 && r >= 0)
1970 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1974 if (!path_equal(from, to)) {
1975 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1980 unified = cg_unified();
1986 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1987 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1988 const char *p = NULL;
1990 if (!(supported & bit))
1994 p = to_callback(bit, userdata);
1999 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, false, false);
2005 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2009 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2013 unified = cg_unified();
2019 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2020 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2022 if (!(supported & bit))
2025 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2032 int cg_mask_supported(CGroupMask *ret) {
2033 CGroupMask mask = 0;
2036 /* Determines the mask of supported cgroup controllers. Only
2037 * includes controllers we can make sense of and that are
2038 * actually accessible. */
2040 unified = cg_unified();
2044 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2047 /* In the unified hierarchy we can read the supported
2048 * and accessible controllers from a the top-level
2049 * cgroup attribute */
2051 r = cg_get_root_path(&root);
2055 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2059 r = read_one_line_file(path, &controllers);
2065 _cleanup_free_ char *n = NULL;
2068 r = extract_first_word(&c, &n, NULL, 0);
2074 v = cgroup_controller_from_string(n);
2078 mask |= CGROUP_CONTROLLER_TO_MASK(v);
2081 /* Currently, we only support the memory and pids
2082 * controller in the unified hierarchy, mask
2083 * everything else off. */
2084 mask &= CGROUP_MASK_MEMORY | CGROUP_MASK_PIDS;
2089 /* In the legacy hierarchy, we check whether which
2090 * hierarchies are mounted. */
2092 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2095 n = cgroup_controller_to_string(c);
2096 if (controller_is_accessible(n) >= 0)
2097 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2105 #if 0 /// UNNEEDED by elogind
2106 int cg_kernel_controllers(Set *controllers) {
2107 _cleanup_fclose_ FILE *f = NULL;
2111 assert(controllers);
2113 /* Determines the full list of kernel-known controllers. Might
2114 * include controllers we don't actually support, arbitrary
2115 * named hierarchies and controllers that aren't currently
2116 * accessible (because not mounted). */
2118 f = fopen("/proc/cgroups", "re");
2120 if (errno == ENOENT)
2125 /* Ignore the header line */
2126 (void) fgets(buf, sizeof(buf), f);
2133 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2138 if (ferror(f) && errno != 0)
2149 if (!cg_controller_is_valid(controller)) {
2154 r = set_consume(controllers, controller);
2163 static thread_local int unified_cache = -1;
2165 int cg_unified(void) {
2168 /* Checks if we support the unified hierarchy. Returns an
2169 * error when the cgroup hierarchies aren't mounted yet or we
2170 * have any other trouble determining if the unified hierarchy
2173 if (unified_cache >= 0)
2174 return unified_cache;
2176 if (statfs("/sys/fs/cgroup/", &fs) < 0)
2179 /// elogind can not support the unified hierarchy as a controller,
2180 /// so always assume a classical hierarchy.
2181 /// If, ond only *if*, someone really wants to substitute systemd-login
2182 /// in an environment managed by systemd with elogin, we might have to
2183 /// add such a support.
2185 if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC))
2186 unified_cache = true;
2187 else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
2189 if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
2191 unified_cache = false;
2195 return unified_cache;
2198 #if 0 /// UNNEEDED by elogind
2199 void cg_unified_flush(void) {
2203 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2204 _cleanup_free_ char *fs = NULL;
2213 unified = cg_unified();
2216 if (!unified) /* on the legacy hiearchy there's no joining of controllers defined */
2219 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2223 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2224 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2227 if (!(supported & bit))
2230 n = cgroup_controller_to_string(c);
2232 char s[1 + strlen(n) + 1];
2234 s[0] = mask & bit ? '+' : '-';
2237 r = write_string_file(fs, s, 0);
2239 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2246 bool cg_is_unified_wanted(void) {
2247 static thread_local int wanted = -1;
2250 /* If the hierarchy is already mounted, then follow whatever
2251 * was chosen for it. */
2252 unified = cg_unified();
2256 /* Otherwise, let's see what the kernel command line has to
2257 * say. Since checking that is expensive, let's cache the
2262 r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy", NULL);
2264 return (wanted = true);
2266 _cleanup_free_ char *value = NULL;
2268 r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy=", &value);
2272 return (wanted = false);
2274 return (wanted = parse_boolean(value) > 0);
2278 bool cg_is_legacy_wanted(void) {
2279 return !cg_is_unified_wanted();
2282 bool cg_is_legacy_wanted(void) {
2287 #if 0 /// UNNEEDED by elogind
2288 int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2293 *ret = CGROUP_CPU_SHARES_INVALID;
2297 r = safe_atou64(s, &u);
2301 if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2308 int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2313 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2317 r = safe_atou64(s, &u);
2321 if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2329 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2330 [CGROUP_CONTROLLER_CPU] = "cpu",
2331 [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
2332 [CGROUP_CONTROLLER_BLKIO] = "blkio",
2333 [CGROUP_CONTROLLER_MEMORY] = "memory",
2334 [CGROUP_CONTROLLER_DEVICES] = "devices",
2335 [CGROUP_CONTROLLER_PIDS] = "pids",
2336 [CGROUP_CONTROLLER_NET_CLS] = "net_cls",
2339 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);