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 ? -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 /// elogind does not support systemd scopes and slices
1183 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1187 e = endswith(p, "/" SPECIAL_INIT_SCOPE);
1189 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); /* legacy */
1191 e = endswith(p, "/system"); /* even more legacy */
1199 return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path);
1203 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1204 _cleanup_free_ char *rt = NULL;
1212 /* If the root was specified let's use that, otherwise
1213 * let's determine it from PID 1 */
1215 r = cg_get_root_path(&rt);
1220 log_debug_elogind("Determined root path: \"%s\"", root);
1223 p = path_startswith(cgroup, root);
1224 if (p && p[0] && (p > cgroup))
1232 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1233 _cleanup_free_ char *raw = NULL;
1240 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1244 log_debug_elogind("Shifting path: \"%s\" (PID %u, root: \"%s\")",
1245 raw, pid, root ? root : "NULL");
1246 r = cg_shift_path(raw, root, &c);
1262 log_debug_elogind("Resulting cgroup:\"%s\"", *cgroup);
1267 #if 0 /// UNNEEDED by elogind
1268 int cg_path_decode_unit(const char *cgroup, char **unit){
1275 n = strcspn(cgroup, "/");
1279 c = strndupa(cgroup, n);
1282 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1293 static bool valid_slice_name(const char *p, size_t n) {
1298 if (n < strlen("x.slice"))
1301 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1307 c = cg_unescape(buf);
1309 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1315 static const char *skip_slices(const char *p) {
1318 /* Skips over all slice assignments */
1323 p += strspn(p, "/");
1325 n = strcspn(p, "/");
1326 if (!valid_slice_name(p, n))
1333 int cg_path_get_unit(const char *path, char **ret) {
1341 e = skip_slices(path);
1343 r = cg_path_decode_unit(e, &unit);
1347 /* We skipped over the slices, don't accept any now */
1348 if (endswith(unit, ".slice")) {
1357 int cg_pid_get_unit(pid_t pid, char **unit) {
1358 _cleanup_free_ char *cgroup = NULL;
1363 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1367 return cg_path_get_unit(cgroup, unit);
1371 * Skip session-*.scope, but require it to be there.
1373 static const char *skip_session(const char *p) {
1379 p += strspn(p, "/");
1381 n = strcspn(p, "/");
1382 if (n < strlen("session-x.scope"))
1385 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1386 char buf[n - 8 - 6 + 1];
1388 memcpy(buf, p + 8, n - 8 - 6);
1391 /* Note that session scopes never need unescaping,
1392 * since they cannot conflict with the kernel's own
1393 * names, hence we don't need to call cg_unescape()
1396 if (!session_id_valid(buf))
1400 p += strspn(p, "/");
1408 * Skip user@*.service, but require it to be there.
1410 static const char *skip_user_manager(const char *p) {
1416 p += strspn(p, "/");
1418 n = strcspn(p, "/");
1419 if (n < strlen("user@x.service"))
1422 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1423 char buf[n - 5 - 8 + 1];
1425 memcpy(buf, p + 5, n - 5 - 8);
1428 /* Note that user manager services never need unescaping,
1429 * since they cannot conflict with the kernel's own
1430 * names, hence we don't need to call cg_unescape()
1433 if (parse_uid(buf, NULL) < 0)
1437 p += strspn(p, "/");
1445 static const char *skip_user_prefix(const char *path) {
1450 /* Skip slices, if there are any */
1451 e = skip_slices(path);
1453 /* Skip the user manager, if it's in the path now... */
1454 t = skip_user_manager(e);
1458 /* Alternatively skip the user session if it is in the path... */
1459 return skip_session(e);
1462 int cg_path_get_user_unit(const char *path, char **ret) {
1468 t = skip_user_prefix(path);
1472 /* And from here on it looks pretty much the same as for a
1473 * system unit, hence let's use the same parser from here
1475 return cg_path_get_unit(t, ret);
1478 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1479 _cleanup_free_ char *cgroup = NULL;
1484 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1488 return cg_path_get_user_unit(cgroup, unit);
1491 int cg_path_get_machine_name(const char *path, char **machine) {
1492 _cleanup_free_ char *u = NULL;
1496 r = cg_path_get_unit(path, &u);
1500 sl = strjoina("/run/systemd/machines/unit:", u);
1501 return readlink_malloc(sl, machine);
1504 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1505 _cleanup_free_ char *cgroup = NULL;
1510 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1514 return cg_path_get_machine_name(cgroup, machine);
1518 int cg_path_get_session(const char *path, char **session) {
1519 /* Elogind uses a flat hierarchy, just "/SESSION". The only
1520 wrinkle is that SESSION might be escaped. */
1522 _cleanup_free_ char *unit = NULL;
1528 r = cg_path_get_unit(path, &unit);
1532 start = startswith(unit, "session-");
1535 end = endswith(start, ".scope");
1540 if (!session_id_valid(start))
1543 const char *e, *n, *start;
1546 log_debug_elogind("path is \"%s\"", path);
1547 assert(path[0] == '/');
1550 n = strchrnul(e, '/');
1554 start = strndupa(e, n - e);
1555 start = cg_unescape(start);
1564 log_debug_elogind("found session: \"%s\"", start);
1575 int cg_pid_get_session(pid_t pid, char **session) {
1576 _cleanup_free_ char *cgroup = NULL;
1579 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1583 return cg_path_get_session(cgroup, session);
1586 #if 0 /// UNNEEDED by elogind
1587 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1588 _cleanup_free_ char *slice = NULL;
1594 r = cg_path_get_slice(path, &slice);
1598 start = startswith(slice, "user-");
1601 end = endswith(start, ".slice");
1606 if (parse_uid(start, uid) < 0)
1612 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1613 _cleanup_free_ char *cgroup = NULL;
1616 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1620 return cg_path_get_owner_uid(cgroup, uid);
1623 int cg_path_get_slice(const char *p, char **slice) {
1624 const char *e = NULL;
1629 /* Finds the right-most slice unit from the beginning, but
1630 * stops before we come to the first non-slice unit. */
1635 p += strspn(p, "/");
1637 n = strcspn(p, "/");
1638 if (!valid_slice_name(p, n)) {
1643 s = strdup("-.slice");
1651 return cg_path_decode_unit(e, slice);
1659 int cg_pid_get_slice(pid_t pid, char **slice) {
1660 _cleanup_free_ char *cgroup = NULL;
1665 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1669 return cg_path_get_slice(cgroup, slice);
1672 int cg_path_get_user_slice(const char *p, char **slice) {
1677 t = skip_user_prefix(p);
1681 /* And now it looks pretty much the same as for a system
1682 * slice, so let's just use the same parser from here on. */
1683 return cg_path_get_slice(t, slice);
1686 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1687 _cleanup_free_ char *cgroup = NULL;
1692 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1696 return cg_path_get_user_slice(cgroup, slice);
1700 char *cg_escape(const char *p) {
1701 bool need_prefix = false;
1703 /* This implements very minimal escaping for names to be used
1704 * as file names in the cgroup tree: any name which might
1705 * conflict with a kernel name or is prefixed with '_' is
1706 * prefixed with a '_'. That way, when reading cgroup names it
1707 * is sufficient to remove a single prefixing underscore if
1710 /* The return value of this function (unlike cg_unescape())
1716 streq(p, "notify_on_release") ||
1717 streq(p, "release_agent") ||
1718 streq(p, "tasks") ||
1719 startswith(p, "cgroup."))
1724 dot = strrchr(p, '.');
1729 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1732 n = cgroup_controller_to_string(c);
1737 if (memcmp(p, n, l) != 0)
1747 return strappend("_", p);
1752 char *cg_unescape(const char *p) {
1755 /* The return value of this function (unlike cg_escape())
1756 * doesn't need free()! */
1764 #define CONTROLLER_VALID \
1768 bool cg_controller_is_valid(const char *p) {
1774 s = startswith(p, "name=");
1778 if (*p == 0 || *p == '_')
1781 for (t = p; *t; t++)
1782 if (!strchr(CONTROLLER_VALID, *t))
1785 if (t - p > FILENAME_MAX)
1791 #if 0 /// UNNEEDED by elogind
1792 int cg_slice_to_path(const char *unit, char **ret) {
1793 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1800 if (streq(unit, "-.slice")) {
1810 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1813 if (!endswith(unit, ".slice"))
1816 r = unit_name_to_prefix(unit, &p);
1820 dash = strchr(p, '-');
1822 /* Don't allow initial dashes */
1827 _cleanup_free_ char *escaped = NULL;
1828 char n[dash - p + sizeof(".slice")];
1830 /* Don't allow trailing or double dashes */
1831 if (dash[1] == 0 || dash[1] == '-')
1834 strcpy(stpncpy(n, p, dash - p), ".slice");
1835 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1838 escaped = cg_escape(n);
1842 if (!strextend(&s, escaped, "/", NULL))
1845 dash = strchr(dash+1, '-');
1848 e = cg_escape(unit);
1852 if (!strextend(&s, e, NULL))
1862 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1863 _cleanup_free_ char *p = NULL;
1866 r = cg_get_path(controller, path, attribute, &p);
1870 return write_string_file(p, value, 0);
1873 #if 0 /// UNNEEDED by elogind
1874 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1875 _cleanup_free_ char *p = NULL;
1878 r = cg_get_path(controller, path, attribute, &p);
1882 return read_one_line_file(p, ret);
1885 int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path) {
1889 /* This one will create a cgroup in our private tree, but also
1890 * duplicate it in the trees specified in mask, and remove it
1893 /* First create the cgroup in our own hierarchy. */
1894 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1898 /* If we are in the unified hierarchy, we are done now */
1899 unified = cg_unified();
1905 /* Otherwise, do the same in the other hierarchies */
1906 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1907 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1910 n = cgroup_controller_to_string(c);
1913 (void) cg_create(n, path);
1914 else if (supported & bit)
1915 (void) cg_trim(n, path, true);
1921 int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1925 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1929 unified = cg_unified();
1935 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1936 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1937 const char *p = NULL;
1939 if (!(supported & bit))
1943 p = path_callback(bit, userdata);
1948 (void) cg_attach_fallback(cgroup_controller_to_string(c), p, pid);
1954 int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1959 SET_FOREACH(pidp, pids, i) {
1960 pid_t pid = PTR_TO_PID(pidp);
1963 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1964 if (q < 0 && r >= 0)
1971 int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1975 if (!path_equal(from, to)) {
1976 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1981 unified = cg_unified();
1987 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
1988 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
1989 const char *p = NULL;
1991 if (!(supported & bit))
1995 p = to_callback(bit, userdata);
2000 (void) cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, cgroup_controller_to_string(c), p, false, false);
2006 int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root) {
2010 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
2014 unified = cg_unified();
2020 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2021 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2023 if (!(supported & bit))
2026 (void) cg_trim(cgroup_controller_to_string(c), path, delete_root);
2033 int cg_mask_supported(CGroupMask *ret) {
2034 CGroupMask mask = 0;
2037 /* Determines the mask of supported cgroup controllers. Only
2038 * includes controllers we can make sense of and that are
2039 * actually accessible. */
2041 unified = cg_unified();
2045 _cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
2048 /* In the unified hierarchy we can read the supported
2049 * and accessible controllers from a the top-level
2050 * cgroup attribute */
2052 r = cg_get_root_path(&root);
2056 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, root, "cgroup.controllers", &path);
2060 r = read_one_line_file(path, &controllers);
2066 _cleanup_free_ char *n = NULL;
2069 r = extract_first_word(&c, &n, NULL, 0);
2075 v = cgroup_controller_from_string(n);
2079 mask |= CGROUP_CONTROLLER_TO_MASK(v);
2082 /* Currently, we only support the memory and pids
2083 * controller in the unified hierarchy, mask
2084 * everything else off. */
2085 mask &= CGROUP_MASK_MEMORY | CGROUP_MASK_PIDS;
2090 /* In the legacy hierarchy, we check whether which
2091 * hierarchies are mounted. */
2093 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2096 n = cgroup_controller_to_string(c);
2097 if (controller_is_accessible(n) >= 0)
2098 mask |= CGROUP_CONTROLLER_TO_MASK(c);
2106 #if 0 /// UNNEEDED by elogind
2107 int cg_kernel_controllers(Set *controllers) {
2108 _cleanup_fclose_ FILE *f = NULL;
2112 assert(controllers);
2114 /* Determines the full list of kernel-known controllers. Might
2115 * include controllers we don't actually support, arbitrary
2116 * named hierarchies and controllers that aren't currently
2117 * accessible (because not mounted). */
2119 f = fopen("/proc/cgroups", "re");
2121 if (errno == ENOENT)
2126 /* Ignore the header line */
2127 (void) fgets(buf, sizeof(buf), f);
2134 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
2139 if (ferror(f) && errno != 0)
2150 if (!cg_controller_is_valid(controller)) {
2155 r = set_consume(controllers, controller);
2164 static thread_local int unified_cache = -1;
2166 int cg_unified(void) {
2169 /* Checks if we support the unified hierarchy. Returns an
2170 * error when the cgroup hierarchies aren't mounted yet or we
2171 * have any other trouble determining if the unified hierarchy
2174 if (unified_cache >= 0)
2175 return unified_cache;
2177 if (statfs("/sys/fs/cgroup/", &fs) < 0)
2180 /// elogind can not support the unified hierarchy as a controller,
2181 /// so always assume a classical hierarchy.
2182 /// If, ond only *if*, someone really wants to substitute systemd-login
2183 /// in an environment managed by systemd with elogin, we might have to
2184 /// add such a support.
2186 if (F_TYPE_EQUAL(fs.f_type, CGROUP_SUPER_MAGIC))
2187 unified_cache = true;
2188 else if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
2190 if (F_TYPE_EQUAL(fs.f_type, TMPFS_MAGIC))
2192 unified_cache = false;
2196 return unified_cache;
2199 #if 0 /// UNNEEDED by elogind
2200 void cg_unified_flush(void) {
2204 int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
2205 _cleanup_free_ char *fs = NULL;
2214 unified = cg_unified();
2217 if (!unified) /* on the legacy hiearchy there's no joining of controllers defined */
2220 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, p, "cgroup.subtree_control", &fs);
2224 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
2225 CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c);
2228 if (!(supported & bit))
2231 n = cgroup_controller_to_string(c);
2233 char s[1 + strlen(n) + 1];
2235 s[0] = mask & bit ? '+' : '-';
2238 r = write_string_file(fs, s, 0);
2240 log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
2247 bool cg_is_unified_wanted(void) {
2248 static thread_local int wanted = -1;
2251 /* If the hierarchy is already mounted, then follow whatever
2252 * was chosen for it. */
2253 unified = cg_unified();
2257 /* Otherwise, let's see what the kernel command line has to
2258 * say. Since checking that is expensive, let's cache the
2263 r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy", NULL);
2265 return (wanted = true);
2267 _cleanup_free_ char *value = NULL;
2269 r = get_proc_cmdline_key("systemd.unified_cgroup_hierarchy=", &value);
2273 return (wanted = false);
2275 return (wanted = parse_boolean(value) > 0);
2279 bool cg_is_legacy_wanted(void) {
2280 return !cg_is_unified_wanted();
2283 bool cg_is_legacy_wanted(void) {
2288 #if 0 /// UNNEEDED by elogind
2289 int cg_cpu_shares_parse(const char *s, uint64_t *ret) {
2294 *ret = CGROUP_CPU_SHARES_INVALID;
2298 r = safe_atou64(s, &u);
2302 if (u < CGROUP_CPU_SHARES_MIN || u > CGROUP_CPU_SHARES_MAX)
2309 int cg_blkio_weight_parse(const char *s, uint64_t *ret) {
2314 *ret = CGROUP_BLKIO_WEIGHT_INVALID;
2318 r = safe_atou64(s, &u);
2322 if (u < CGROUP_BLKIO_WEIGHT_MIN || u > CGROUP_BLKIO_WEIGHT_MAX)
2330 static const char *cgroup_controller_table[_CGROUP_CONTROLLER_MAX] = {
2331 [CGROUP_CONTROLLER_CPU] = "cpu",
2332 [CGROUP_CONTROLLER_CPUACCT] = "cpuacct",
2333 [CGROUP_CONTROLLER_BLKIO] = "blkio",
2334 [CGROUP_CONTROLLER_MEMORY] = "memory",
2335 [CGROUP_CONTROLLER_DEVICES] = "devices",
2336 [CGROUP_CONTROLLER_PIDS] = "pids",
2337 [CGROUP_CONTROLLER_NET_CLS] = "net_cls",
2340 DEFINE_STRING_TABLE_LOOKUP(cgroup_controller, CGroupController);