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 "cgroup-util.h"
36 #include "path-util.h"
37 #include "unit-name.h"
42 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
43 _cleanup_free_ char *fs = NULL;
49 r = cg_get_path(controller, path, "cgroup.procs", &fs);
61 int cg_read_pid(FILE *f, pid_t *_pid) {
64 /* Note that the cgroup.procs might contain duplicates! See
65 * cgroups.txt for details. */
71 if (fscanf(f, "%lu", &ul) != 1) {
76 return errno ? -errno : -EIO;
86 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
87 _cleanup_free_ char *fs = NULL;
93 /* This is not recursive! */
95 r = cg_get_path(controller, path, NULL, &fs);
107 int cg_read_subgroup(DIR *d, char **fn) {
113 FOREACH_DIRENT(de, d, return -errno) {
116 if (de->d_type != DT_DIR)
119 if (streq(de->d_name, ".") ||
120 streq(de->d_name, ".."))
123 b = strdup(de->d_name);
134 int cg_rmdir(const char *controller, const char *path) {
135 _cleanup_free_ char *p = NULL;
138 r = cg_get_path(controller, path, NULL, &p);
143 if (r < 0 && errno != ENOENT)
149 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
150 _cleanup_set_free_ Set *allocated_set = NULL;
157 /* This goes through the tasks list and kills them all. This
158 * is repeated until no further processes are added to the
159 * tasks list, to properly handle forking processes */
162 s = allocated_set = set_new(NULL);
170 _cleanup_fclose_ FILE *f = NULL;
174 r = cg_enumerate_processes(controller, path, &f);
176 if (ret >= 0 && r != -ENOENT)
182 while ((r = cg_read_pid(f, &pid)) > 0) {
184 if (ignore_self && pid == my_pid)
187 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
190 /* If we haven't killed this process yet, kill
192 if (kill(pid, sig) < 0) {
193 if (ret >= 0 && errno != ESRCH)
196 if (sigcont && sig != SIGKILL)
205 r = set_put(s, LONG_TO_PTR(pid));
221 /* To avoid racing against processes which fork
222 * quicker than we can kill them we repeat this until
223 * no new pids need to be killed. */
230 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
231 _cleanup_set_free_ Set *allocated_set = NULL;
232 _cleanup_closedir_ DIR *d = NULL;
240 s = allocated_set = set_new(NULL);
245 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
247 r = cg_enumerate_subgroups(controller, path, &d);
249 if (ret >= 0 && r != -ENOENT)
255 while ((r = cg_read_subgroup(d, &fn)) > 0) {
256 _cleanup_free_ char *p = NULL;
258 p = strjoin(path, "/", fn, NULL);
263 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
264 if (ret >= 0 && r != 0)
268 if (ret >= 0 && r < 0)
272 r = cg_rmdir(controller, path);
273 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
280 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
282 _cleanup_set_free_ Set *s = NULL;
298 _cleanup_fclose_ FILE *f = NULL;
302 r = cg_enumerate_processes(cfrom, pfrom, &f);
304 if (ret >= 0 && r != -ENOENT)
310 while ((r = cg_read_pid(f, &pid)) > 0) {
312 /* This might do weird stuff if we aren't a
313 * single-threaded program. However, we
314 * luckily know we are not */
315 if (ignore_self && pid == my_pid)
318 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
321 r = cg_attach(cto, pto, pid);
323 if (ret >= 0 && r != -ESRCH)
330 r = set_put(s, LONG_TO_PTR(pid));
350 int cg_migrate_recursive(
358 _cleanup_closedir_ DIR *d = NULL;
367 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
369 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
371 if (ret >= 0 && r != -ENOENT)
377 while ((r = cg_read_subgroup(d, &fn)) > 0) {
378 _cleanup_free_ char *p = NULL;
380 p = strjoin(pfrom, "/", fn, NULL);
389 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
390 if (r != 0 && ret >= 0)
394 if (r < 0 && ret >= 0)
398 r = cg_rmdir(cfrom, pfrom);
399 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
406 int cg_migrate_recursive_fallback(
421 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
423 char prefix[strlen(pto) + 1];
425 /* This didn't work? Then let's try all prefixes of the destination */
427 PATH_FOREACH_PREFIX(prefix, pto) {
428 r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
437 static const char *normalize_controller(const char *controller) {
441 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
443 else if (startswith(controller, "name="))
444 return controller + 5;
449 static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
452 if (!isempty(controller)) {
453 if (!isempty(path) && !isempty(suffix))
454 t = strjoin("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
455 else if (!isempty(path))
456 t = strjoin("/sys/fs/cgroup/", controller, "/", path, NULL);
457 else if (!isempty(suffix))
458 t = strjoin("/sys/fs/cgroup/", controller, "/", suffix, NULL);
460 t = strappend("/sys/fs/cgroup/", controller);
462 if (!isempty(path) && !isempty(suffix))
463 t = strjoin(path, "/", suffix, NULL);
464 else if (!isempty(path))
473 *fs = path_kill_slashes(t);
477 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
479 static thread_local bool good = false;
483 if (controller && !cg_controller_is_valid(controller, true))
486 if (_unlikely_(!good)) {
489 r = path_is_mount_point("/sys/fs/cgroup", false);
491 return r < 0 ? r : -ENOENT;
493 /* Cache this to save a few stat()s */
497 p = controller ? normalize_controller(controller) : NULL;
499 return join_path(p, path, suffix, fs);
502 static int check_hierarchy(const char *p) {
507 if (!filename_is_valid(p))
510 /* Check if this controller actually really exists */
511 cc = strjoina("/sys/fs/cgroup/", p);
512 if (laccess(cc, F_OK) < 0)
518 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
524 if (!cg_controller_is_valid(controller, true))
527 /* Normalize the controller syntax */
528 p = normalize_controller(controller);
530 /* Check if this controller actually really exists */
531 r = check_hierarchy(p);
535 return join_path(p, path, suffix, fs);
538 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
543 if (typeflag != FTW_DP)
546 if (ftwbuf->level < 1)
553 int cg_trim(const char *controller, const char *path, bool delete_root) {
554 _cleanup_free_ char *fs = NULL;
559 r = cg_get_path(controller, path, NULL, &fs);
564 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
565 r = errno ? -errno : -EIO;
568 if (rmdir(fs) < 0 && errno != ENOENT)
575 int cg_delete(const char *controller, const char *path) {
576 _cleanup_free_ char *parent = NULL;
581 r = path_get_parent(path, &parent);
585 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
586 return r == -ENOENT ? 0 : r;
589 int cg_create(const char *controller, const char *path) {
590 _cleanup_free_ char *fs = NULL;
593 r = cg_get_path_and_check(controller, path, NULL, &fs);
597 r = mkdir_parents(fs, 0755);
601 if (mkdir(fs, 0755) < 0) {
612 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
617 r = cg_create(controller, path);
621 q = cg_attach(controller, path, pid);
625 /* This does not remove the cgroup on failure */
629 int cg_attach(const char *controller, const char *path, pid_t pid) {
630 _cleanup_free_ char *fs = NULL;
631 char c[DECIMAL_STR_MAX(pid_t) + 2];
637 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
644 snprintf(c, sizeof(c), PID_FMT"\n", pid);
646 return write_string_file_no_create(fs, c);
649 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
656 r = cg_attach(controller, path, pid);
658 char prefix[strlen(path) + 1];
660 /* This didn't work? Then let's try all prefixes of
663 PATH_FOREACH_PREFIX(prefix, path) {
664 r = cg_attach(controller, prefix, pid);
673 int cg_set_group_access(
674 const char *controller,
680 _cleanup_free_ char *fs = NULL;
685 if (mode != MODE_INVALID)
688 r = cg_get_path(controller, path, NULL, &fs);
692 return chmod_and_chown(fs, mode, uid, gid);
695 int cg_set_task_access(
696 const char *controller,
702 _cleanup_free_ char *fs = NULL, *procs = NULL;
707 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
710 if (mode != MODE_INVALID)
713 r = cg_get_path(controller, path, "cgroup.procs", &fs);
717 r = chmod_and_chown(fs, mode, uid, gid);
721 /* Compatibility, Always keep values for "tasks" in sync with
723 r = cg_get_path(controller, path, "tasks", &procs);
727 return chmod_and_chown(procs, mode, uid, gid);
730 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
731 _cleanup_fclose_ FILE *f = NULL;
740 if (!cg_controller_is_valid(controller, true))
743 controller = normalize_controller(controller);
745 controller = SYSTEMD_CGROUP_CONTROLLER;
747 fs = procfs_file_alloca(pid, "cgroup");
751 return errno == ENOENT ? -ESRCH : -errno;
753 cs = strlen(controller);
755 FOREACH_LINE(line, f, return -errno) {
758 const char *word, *state;
763 l = strchr(line, ':');
774 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
776 if (k == cs && memcmp(word, controller, cs) == 0) {
782 memcmp(word, "name=", 5) == 0 &&
783 memcmp(word+5, controller, cs) == 0) {
803 int cg_install_release_agent(const char *controller, const char *agent) {
804 _cleanup_free_ char *fs = NULL, *contents = NULL;
810 r = cg_get_path(controller, NULL, "release_agent", &fs);
814 r = read_one_line_file(fs, &contents);
818 sc = strstrip(contents);
820 r = write_string_file_no_create(fs, agent);
823 } else if (!streq(sc, agent))
828 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
834 r = read_one_line_file(fs, &contents);
838 sc = strstrip(contents);
839 if (streq(sc, "0")) {
840 r = write_string_file_no_create(fs, "1");
853 int cg_uninstall_release_agent(const char *controller) {
854 _cleanup_free_ char *fs = NULL;
857 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
861 r = write_string_file_no_create(fs, "0");
868 r = cg_get_path(controller, NULL, "release_agent", &fs);
872 r = write_string_file_no_create(fs, "");
879 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
880 _cleanup_fclose_ FILE *f = NULL;
881 pid_t pid = 0, self_pid;
887 r = cg_enumerate_processes(controller, path, &f);
889 return r == -ENOENT ? 1 : r;
893 while ((r = cg_read_pid(f, &pid)) > 0) {
895 if (ignore_self && pid == self_pid)
908 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
909 _cleanup_closedir_ DIR *d = NULL;
915 r = cg_is_empty(controller, path, ignore_self);
919 r = cg_enumerate_subgroups(controller, path, &d);
921 return r == -ENOENT ? 1 : r;
923 while ((r = cg_read_subgroup(d, &fn)) > 0) {
924 _cleanup_free_ char *p = NULL;
926 p = strjoin(path, "/", fn, NULL);
931 r = cg_is_empty_recursive(controller, p, ignore_self);
942 int cg_split_spec(const char *spec, char **controller, char **path) {
944 char *t = NULL, *u = NULL;
945 _cleanup_free_ char *v = NULL;
950 if (!path_is_safe(spec))
958 *path = path_kill_slashes(t);
967 e = strchr(spec, ':');
969 if (!cg_controller_is_valid(spec, true))
973 t = strdup(normalize_controller(spec));
986 v = strndup(spec, e-spec);
989 t = strdup(normalize_controller(v));
992 if (!cg_controller_is_valid(t, true)) {
997 if (streq(e+1, "")) {
1010 if (!path_is_safe(u) ||
1011 !path_is_absolute(u)) {
1017 path_kill_slashes(u);
1033 int cg_mangle_path(const char *path, char **result) {
1034 _cleanup_free_ char *c = NULL, *p = NULL;
1041 /* First, check if it already is a filesystem path */
1042 if (path_startswith(path, "/sys/fs/cgroup")) {
1048 *result = path_kill_slashes(t);
1052 /* Otherwise, treat it as cg spec */
1053 r = cg_split_spec(path, &c, &p);
1057 return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1060 int cg_get_root_path(char **path) {
1066 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1070 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1078 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1079 _cleanup_free_ char *rt = NULL;
1087 /* If the root was specified let's use that, otherwise
1088 * let's determine it from PID 1 */
1090 r = cg_get_root_path(&rt);
1097 p = path_startswith(cgroup, root);
1106 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1107 _cleanup_free_ char *raw = NULL;
1114 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1118 r = cg_shift_path(raw, root, &c);
1138 int cg_path_decode_unit(const char *cgroup, char **unit){
1144 e = strchrnul(cgroup, '/');
1145 c = strndupa(cgroup, e - cgroup);
1148 if (!unit_name_is_valid(c, TEMPLATE_INVALID))
1159 static const char *skip_slices(const char *p) {
1160 /* Skips over all slice assignments */
1165 p += strspn(p, "/");
1167 n = strcspn(p, "/");
1168 if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0)
1175 int cg_path_get_unit(const char *path, char **unit) {
1181 e = skip_slices(path);
1183 return cg_path_decode_unit(e, unit);
1186 int cg_pid_get_unit(pid_t pid, char **unit) {
1187 _cleanup_free_ char *cgroup = NULL;
1192 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1196 return cg_path_get_unit(cgroup, unit);
1200 * Skip session-*.scope, but require it to be there.
1202 static const char *skip_session(const char *p) {
1207 p += strspn(p, "/");
1209 n = strcspn(p, "/");
1210 if (n < strlen("session-x.scope") || memcmp(p, "session-", 8) != 0 || memcmp(p + n - 6, ".scope", 6) != 0)
1214 p += strspn(p, "/");
1220 * Skip user@*.service, but require it to be there.
1222 static const char *skip_user_manager(const char *p) {
1227 p += strspn(p, "/");
1229 n = strcspn(p, "/");
1230 if (n < strlen("user@x.service") || memcmp(p, "user@", 5) != 0 || memcmp(p + n - 8, ".service", 8) != 0)
1234 p += strspn(p, "/");
1239 int cg_path_get_user_unit(const char *path, char **unit) {
1245 /* We always have to parse the path from the beginning as unit
1246 * cgroups might have arbitrary child cgroups and we shouldn't get
1247 * confused by those */
1249 /* Skip slices, if there are any */
1250 e = skip_slices(path);
1252 /* Skip the session scope or user manager... */
1253 t = skip_session(e);
1255 t = skip_user_manager(e);
1259 /* ... and skip more slices if there are any */
1262 return cg_path_decode_unit(e, unit);
1265 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1266 _cleanup_free_ char *cgroup = NULL;
1271 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1275 return cg_path_get_user_unit(cgroup, unit);
1278 int cg_path_get_machine_name(const char *path, char **machine) {
1279 _cleanup_free_ char *u = NULL, *sl = NULL;
1282 r = cg_path_get_unit(path, &u);
1286 sl = strjoin("/run/systemd/machines/unit:", u, NULL);
1290 return readlink_malloc(sl, machine);
1293 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1294 _cleanup_free_ char *cgroup = NULL;
1299 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1303 return cg_path_get_machine_name(cgroup, machine);
1306 int cg_path_get_session(const char *path, char **session) {
1307 const char *e, *n, *x, *y;
1312 /* Skip slices, if there are any */
1313 e = skip_slices(path);
1315 n = strchrnul(e, '/');
1319 s = strndupa(e, n - e);
1322 x = startswith(s, "session-");
1325 y = endswith(x, ".scope");
1332 r = strndup(x, y - x);
1342 int cg_pid_get_session(pid_t pid, char **session) {
1343 _cleanup_free_ char *cgroup = NULL;
1346 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1350 return cg_path_get_session(cgroup, session);
1353 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1354 _cleanup_free_ char *slice = NULL;
1355 const char *start, *end;
1362 r = cg_path_get_slice(path, &slice);
1366 start = startswith(slice, "user-");
1369 end = endswith(slice, ".slice");
1373 s = strndupa(start, end - start);
1377 if (parse_uid(s, &u) < 0)
1386 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1387 _cleanup_free_ char *cgroup = NULL;
1390 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1394 return cg_path_get_owner_uid(cgroup, uid);
1397 int cg_path_get_slice(const char *p, char **slice) {
1398 const char *e = NULL;
1407 p += strspn(p, "/");
1409 n = strcspn(p, "/");
1410 if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) {
1431 int cg_pid_get_slice(pid_t pid, char **slice) {
1432 _cleanup_free_ char *cgroup = NULL;
1437 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1441 return cg_path_get_slice(cgroup, slice);
1444 char *cg_escape(const char *p) {
1445 bool need_prefix = false;
1447 /* This implements very minimal escaping for names to be used
1448 * as file names in the cgroup tree: any name which might
1449 * conflict with a kernel name or is prefixed with '_' is
1450 * prefixed with a '_'. That way, when reading cgroup names it
1451 * is sufficient to remove a single prefixing underscore if
1454 /* The return value of this function (unlike cg_unescape())
1460 streq(p, "notify_on_release") ||
1461 streq(p, "release_agent") ||
1467 dot = strrchr(p, '.');
1470 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1475 n = strndupa(p, dot - p);
1477 if (check_hierarchy(n) >= 0)
1484 return strappend("_", p);
1489 char *cg_unescape(const char *p) {
1492 /* The return value of this function (unlike cg_escape())
1493 * doesn't need free()! */
1501 #define CONTROLLER_VALID \
1505 bool cg_controller_is_valid(const char *p, bool allow_named) {
1512 s = startswith(p, "name=");
1517 if (*p == 0 || *p == '_')
1520 for (t = p; *t; t++)
1521 if (!strchr(CONTROLLER_VALID, *t))
1524 if (t - p > FILENAME_MAX)
1530 int cg_slice_to_path(const char *unit, char **ret) {
1531 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1537 if (!unit_name_is_valid(unit, TEMPLATE_INVALID))
1540 if (!endswith(unit, ".slice"))
1543 p = unit_name_to_prefix(unit);
1547 dash = strchr(p, '-');
1549 _cleanup_free_ char *escaped = NULL;
1550 char n[dash - p + sizeof(".slice")];
1552 strcpy(stpncpy(n, p, dash - p), ".slice");
1554 if (!unit_name_is_valid(n, TEMPLATE_INVALID))
1557 escaped = cg_escape(n);
1561 if (!strextend(&s, escaped, "/", NULL))
1564 dash = strchr(dash+1, '-');
1567 e = cg_escape(unit);
1571 if (!strextend(&s, e, NULL))
1580 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1581 _cleanup_free_ char *p = NULL;
1584 r = cg_get_path(controller, path, attribute, &p);
1588 return write_string_file_no_create(p, value);
1591 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1592 _cleanup_free_ char *p = NULL;
1595 r = cg_get_path(controller, path, attribute, &p);
1599 return read_one_line_file(p, ret);
1602 static const char mask_names[] =
1609 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
1610 CGroupControllerMask bit = 1;
1614 /* This one will create a cgroup in our private tree, but also
1615 * duplicate it in the trees specified in mask, and remove it
1618 /* First create the cgroup in our own hierarchy. */
1619 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1623 /* Then, do the same in the other hierarchies */
1624 NULSTR_FOREACH(n, mask_names) {
1627 else if (supported & bit)
1628 cg_trim(n, path, true);
1636 int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1637 CGroupControllerMask bit = 1;
1641 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1645 NULSTR_FOREACH(n, mask_names) {
1647 if (supported & bit) {
1648 const char *p = NULL;
1651 p = path_callback(bit, userdata);
1656 cg_attach_fallback(n, path, pid);
1665 int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1670 SET_FOREACH(pidp, pids, i) {
1671 pid_t pid = PTR_TO_LONG(pidp);
1674 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1682 int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1683 CGroupControllerMask bit = 1;
1687 if (!path_equal(from, to)) {
1688 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1693 NULSTR_FOREACH(n, mask_names) {
1694 if (supported & bit) {
1695 const char *p = NULL;
1698 p = to_callback(bit, userdata);
1703 cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false);
1712 int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
1713 CGroupControllerMask bit = 1;
1717 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
1721 NULSTR_FOREACH(n, mask_names) {
1722 if (supported & bit)
1723 cg_trim(n, path, delete_root);
1731 CGroupControllerMask cg_mask_supported(void) {
1732 CGroupControllerMask bit = 1, mask = 0;
1735 NULSTR_FOREACH(n, mask_names) {
1736 if (check_hierarchy(n) >= 0)
1745 int cg_kernel_controllers(Set *controllers) {
1746 _cleanup_fclose_ FILE *f = NULL;
1750 assert(controllers);
1752 f = fopen("/proc/cgroups", "re");
1754 if (errno == ENOENT)
1759 /* Ignore the header line */
1760 (void) fgets(buf, sizeof(buf), f);
1767 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
1772 if (ferror(f) && errno)
1783 if (!filename_is_valid(controller)) {
1788 r = set_consume(controllers, controller);