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"
37 #include "path-util.h"
39 #include "unit-name.h"
43 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
44 _cleanup_free_ char *fs = NULL;
50 r = cg_get_path(controller, path, "cgroup.procs", &fs);
62 int cg_read_pid(FILE *f, pid_t *_pid) {
65 /* Note that the cgroup.procs might contain duplicates! See
66 * cgroups.txt for details. */
72 if (fscanf(f, "%lu", &ul) != 1) {
77 return errno ? -errno : -EIO;
87 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
88 _cleanup_free_ char *fs = NULL;
94 /* This is not recursive! */
96 r = cg_get_path(controller, path, NULL, &fs);
108 int cg_read_subgroup(DIR *d, char **fn) {
114 FOREACH_DIRENT(de, d, return -errno) {
117 if (de->d_type != DT_DIR)
120 if (streq(de->d_name, ".") ||
121 streq(de->d_name, ".."))
124 b = strdup(de->d_name);
135 int cg_rmdir(const char *controller, const char *path) {
136 _cleanup_free_ char *p = NULL;
139 r = cg_get_path(controller, path, NULL, &p);
144 if (r < 0 && errno != ENOENT)
150 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
151 _cleanup_set_free_ Set *allocated_set = NULL;
158 /* This goes through the tasks list and kills them all. This
159 * is repeated until no further processes are added to the
160 * tasks list, to properly handle forking processes */
163 s = allocated_set = set_new(trivial_hash_func, trivial_compare_func);
171 _cleanup_fclose_ FILE *f = NULL;
175 r = cg_enumerate_processes(controller, path, &f);
177 if (ret >= 0 && r != -ENOENT)
183 while ((r = cg_read_pid(f, &pid)) > 0) {
185 if (ignore_self && pid == my_pid)
188 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
191 /* If we haven't killed this process yet, kill
193 if (kill(pid, sig) < 0) {
194 if (ret >= 0 && errno != ESRCH)
196 } else if (ret == 0) {
206 r = set_put(s, LONG_TO_PTR(pid));
222 /* To avoid racing against processes which fork
223 * quicker than we can kill them we repeat this until
224 * no new pids need to be killed. */
231 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
232 _cleanup_set_free_ Set *allocated_set = NULL;
233 _cleanup_closedir_ DIR *d = NULL;
241 s = allocated_set = set_new(trivial_hash_func, trivial_compare_func);
246 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
248 r = cg_enumerate_subgroups(controller, path, &d);
250 if (ret >= 0 && r != -ENOENT)
256 while ((r = cg_read_subgroup(d, &fn)) > 0) {
257 _cleanup_free_ char *p = NULL;
259 p = strjoin(path, "/", fn, NULL);
264 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
265 if (ret >= 0 && r != 0)
269 if (ret >= 0 && r < 0)
273 r = cg_rmdir(controller, path);
274 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
281 int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) {
286 /* This safely kills all processes; first it sends a SIGTERM,
287 * then checks 8 times after 200ms whether the group is now
288 * empty, then kills everything that is left with SIGKILL and
289 * finally checks 5 times after 200ms each whether the group
290 * is finally empty. */
292 for (i = 0; i < 15; i++) {
302 r = cg_kill_recursive(controller, path, sig, true, true, rem, NULL);
306 usleep(200 * USEC_PER_MSEC);
312 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
314 _cleanup_set_free_ Set *s = NULL;
323 s = set_new(trivial_hash_func, trivial_compare_func);
330 _cleanup_fclose_ FILE *f = NULL;
334 r = cg_enumerate_processes(cfrom, pfrom, &f);
336 if (ret >= 0 && r != -ENOENT)
342 while ((r = cg_read_pid(f, &pid)) > 0) {
344 /* This might do weird stuff if we aren't a
345 * single-threaded program. However, we
346 * luckily know we are not */
347 if (ignore_self && pid == my_pid)
350 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
353 r = cg_attach(cto, pto, pid);
355 if (ret >= 0 && r != -ESRCH)
362 r = set_put(s, LONG_TO_PTR(pid));
382 int cg_migrate_recursive(
390 _cleanup_closedir_ DIR *d = NULL;
399 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
401 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
403 if (ret >= 0 && r != -ENOENT)
409 while ((r = cg_read_subgroup(d, &fn)) > 0) {
410 _cleanup_free_ char *p = NULL;
412 p = strjoin(pfrom, "/", fn, NULL);
421 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
422 if (r != 0 && ret >= 0)
426 if (r < 0 && ret >= 0)
430 r = cg_rmdir(cfrom, pfrom);
431 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
438 static const char *normalize_controller(const char *controller) {
442 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
444 else if (startswith(controller, "name="))
445 return controller + 5;
450 static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
453 if (!isempty(controller)) {
454 if (!isempty(path) && !isempty(suffix))
455 t = strjoin("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
456 else if (!isempty(path))
457 t = strjoin("/sys/fs/cgroup/", controller, "/", path, NULL);
458 else if (!isempty(suffix))
459 t = strjoin("/sys/fs/cgroup/", controller, "/", suffix, NULL);
461 t = strappend("/sys/fs/cgroup/", controller);
463 if (!isempty(path) && !isempty(suffix))
464 t = strjoin(path, "/", suffix, NULL);
465 else if (!isempty(path))
474 path_kill_slashes(t);
480 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
482 static __thread bool good = false;
486 if (controller && !cg_controller_is_valid(controller, true))
489 if (_unlikely_(!good)) {
492 r = path_is_mount_point("/sys/fs/cgroup", false);
494 return r < 0 ? r : -ENOENT;
496 /* Cache this to save a few stat()s */
500 p = controller ? normalize_controller(controller) : NULL;
502 return join_path(p, path, suffix, fs);
505 static int check_hierarchy(const char *p) {
510 /* Check if this controller actually really exists */
511 cc = alloca(sizeof("/sys/fs/cgroup/") + strlen(p));
512 strcpy(stpcpy(cc, "/sys/fs/cgroup/"), p);
513 if (access(cc, F_OK) < 0)
519 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
525 if (!cg_controller_is_valid(controller, true))
528 /* Normalize the controller syntax */
529 p = normalize_controller(controller);
531 /* Check if this controller actually really exists */
532 r = check_hierarchy(p);
536 return join_path(p, path, suffix, fs);
539 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
544 if (typeflag != FTW_DP)
547 if (ftwbuf->level < 1)
554 int cg_trim(const char *controller, const char *path, bool delete_root) {
555 _cleanup_free_ char *fs = NULL;
560 r = cg_get_path(controller, path, NULL, &fs);
565 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
566 r = errno ? -errno : -EIO;
569 if (rmdir(fs) < 0 && errno != ENOENT)
576 int cg_delete(const char *controller, const char *path) {
577 _cleanup_free_ char *parent = NULL;
582 r = path_get_parent(path, &parent);
586 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
587 return r == -ENOENT ? 0 : r;
590 int cg_attach(const char *controller, const char *path, pid_t pid) {
591 _cleanup_free_ char *fs = NULL;
592 char c[DECIMAL_STR_MAX(pid_t) + 2];
598 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
605 snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
607 return write_string_file(fs, c);
610 int cg_set_group_access(
611 const char *controller,
617 _cleanup_free_ char *fs = NULL;
622 if (mode != (mode_t) -1)
625 r = cg_get_path(controller, path, NULL, &fs);
629 return chmod_and_chown(fs, mode, uid, gid);
632 int cg_set_task_access(
633 const char *controller,
639 _cleanup_free_ char *fs = NULL, *procs = NULL;
644 if (mode == (mode_t) -1 && uid == (uid_t) -1 && gid == (gid_t) -1)
647 if (mode != (mode_t) -1)
650 r = cg_get_path(controller, path, "cgroup.procs", &fs);
654 r = chmod_and_chown(fs, mode, uid, gid);
658 /* Compatibility, Always keep values for "tasks" in sync with
660 r = cg_get_path(controller, path, "tasks", &procs);
664 return chmod_and_chown(procs, mode, uid, gid);
667 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
668 _cleanup_fclose_ FILE *f = NULL;
677 if (!cg_controller_is_valid(controller, true))
680 controller = normalize_controller(controller);
682 controller = SYSTEMD_CGROUP_CONTROLLER;
685 fs = "/proc/self/cgroup";
687 fs = procfs_file_alloca(pid, "cgroup");
691 return errno == ENOENT ? -ESRCH : -errno;
693 cs = strlen(controller);
695 FOREACH_LINE(line, f, return -errno) {
703 l = strchr(line, ':');
714 FOREACH_WORD_SEPARATOR(w, k, l, ",", state) {
716 if (k == cs && memcmp(w, controller, cs) == 0) {
722 memcmp(w, "name=", 5) == 0 &&
723 memcmp(w+5, controller, cs) == 0) {
743 int cg_install_release_agent(const char *controller, const char *agent) {
744 _cleanup_free_ char *fs = NULL, *contents = NULL;
750 r = cg_get_path(controller, NULL, "release_agent", &fs);
754 r = read_one_line_file(fs, &contents);
758 sc = strstrip(contents);
760 r = write_string_file(fs, agent);
763 } else if (!streq(sc, agent))
768 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
774 r = read_one_line_file(fs, &contents);
778 sc = strstrip(contents);
779 if (streq(sc, "0")) {
780 r = write_string_file(fs, "1");
793 int cg_uninstall_release_agent(const char *controller) {
794 _cleanup_free_ char *fs = NULL;
797 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
801 r = write_string_file(fs, "0");
808 r = cg_get_path(controller, NULL, "release_agent", &fs);
812 r = write_string_file(fs, "");
819 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
820 _cleanup_fclose_ FILE *f = NULL;
821 pid_t pid = 0, self_pid;
827 r = cg_enumerate_processes(controller, path, &f);
829 return r == -ENOENT ? 1 : r;
833 while ((r = cg_read_pid(f, &pid)) > 0) {
835 if (ignore_self && pid == self_pid)
848 int cg_is_empty_by_spec(const char *spec, bool ignore_self) {
849 _cleanup_free_ char *controller = NULL, *path = NULL;
854 r = cg_split_spec(spec, &controller, &path);
858 return cg_is_empty(controller, path, ignore_self);
861 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
862 _cleanup_closedir_ DIR *d = NULL;
868 r = cg_is_empty(controller, path, ignore_self);
872 r = cg_enumerate_subgroups(controller, path, &d);
874 return r == -ENOENT ? 1 : r;
876 while ((r = cg_read_subgroup(d, &fn)) > 0) {
877 _cleanup_free_ char *p = NULL;
879 p = strjoin(path, "/", fn, NULL);
884 r = cg_is_empty_recursive(controller, p, ignore_self);
895 int cg_split_spec(const char *spec, char **controller, char **path) {
897 char *t = NULL, *u = NULL;
898 _cleanup_free_ char *v = NULL;
903 if (!path_is_safe(spec))
911 path_kill_slashes(t);
921 e = strchr(spec, ':');
923 if (!cg_controller_is_valid(spec, true))
927 t = strdup(normalize_controller(spec));
940 v = strndup(spec, e-spec);
943 t = strdup(normalize_controller(v));
946 if (!cg_controller_is_valid(t, true)) {
956 if (!path_is_safe(u) ||
957 !path_is_absolute(u)) {
963 path_kill_slashes(u);
978 int cg_join_spec(const char *controller, const char *path, char **spec) {
984 controller = "systemd";
986 if (!cg_controller_is_valid(controller, true))
989 controller = normalize_controller(controller);
992 if (!path_is_absolute(path))
995 s = strjoin(controller, ":", path, NULL);
999 path_kill_slashes(s + strlen(controller) + 1);
1005 int cg_mangle_path(const char *path, char **result) {
1006 _cleanup_free_ char *c = NULL, *p = NULL;
1013 /* First check if it already is a filesystem path */
1014 if (path_startswith(path, "/sys/fs/cgroup")) {
1020 path_kill_slashes(t);
1025 /* Otherwise treat it as cg spec */
1026 r = cg_split_spec(path, &c, &p);
1030 return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1033 int cg_get_root_path(char **path) {
1039 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1043 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1051 char **cg_shorten_controllers(char **controllers) {
1057 for (f = controllers, t = controllers; *f; f++) {
1061 p = normalize_controller(*f);
1063 if (streq(p, "systemd")) {
1068 if (!cg_controller_is_valid(p, true)) {
1069 log_warning("Controller %s is not valid, removing from controllers list.", p);
1074 r = check_hierarchy(p);
1076 log_debug("Controller %s is not available, removing from controllers list.", p);
1085 return strv_uniq(controllers);
1088 int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
1089 _cleanup_free_ char *cg_root = NULL;
1090 char *cg_process, *p;
1093 r = cg_get_root_path(&cg_root);
1097 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
1101 p = path_startswith(cg_process, cg_root);
1120 cg_process[p-cg_process] = 0;
1128 int cg_path_decode_unit(const char *cgroup, char **unit){
1129 char *p, *e, *c, *s, *k;
1134 e = strchrnul(cgroup, '/');
1135 c = strndupa(cgroup, e - cgroup);
1138 /* Could this be a valid unit name? */
1139 if (!unit_name_is_valid(c, true))
1142 if (!unit_name_is_template(c))
1148 e += strspn(e, "/");
1150 p = strchrnul(e, '/');
1151 k = strndupa(e, p - e);
1154 if (!unit_name_is_valid(k, false))
1167 static const char *skip_slices(const char *p) {
1168 /* Skips over all slice assignments */
1173 p += strspn(p, "/");
1175 n = strcspn(p, "/");
1176 if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0)
1183 int cg_path_get_unit(const char *path, char **unit) {
1189 e = skip_slices(path);
1191 return cg_path_decode_unit(e, unit);
1194 int cg_pid_get_unit(pid_t pid, char **unit) {
1195 _cleanup_free_ char *cgroup = NULL;
1200 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1204 return cg_path_get_unit(cgroup, unit);
1207 static const char *skip_session(const char *p) {
1212 p += strspn(p, "/");
1214 n = strcspn(p, "/");
1215 if (n <= 12 || memcmp(p, "session-", 8) != 0 || memcmp(p + n - 6, ".scope", 6) != 0)
1219 p += strspn(p, "/");
1224 int cg_path_get_user_unit(const char *path, char **unit) {
1230 /* We always have to parse the path from the beginning as unit
1231 * cgroups might have arbitrary child cgroups and we shouldn't get
1232 * confused by those */
1234 /* Skip slices, if there are any */
1235 e = skip_slices(path);
1237 /* Skip the session scope, require that there is one */
1238 e = skip_session(e);
1242 /* And skip more slices */
1245 return cg_path_decode_unit(e, unit);
1248 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1249 _cleanup_free_ char *cgroup = NULL;
1254 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1258 return cg_path_get_user_unit(cgroup, unit);
1261 int cg_path_get_machine_name(const char *path, char **machine) {
1262 const char *e, *n, *x;
1269 /* Skip slices, if there are any */
1270 e = skip_slices(path);
1272 n = strchrnul(e, '/');
1276 s = strndupa(e, n - e);
1279 x = startswith(s, "machine-");
1282 if (!endswith(x, ".scope"))
1289 r = strndup(x, l - 6);
1297 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1298 _cleanup_free_ char *cgroup = NULL;
1303 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1307 return cg_path_get_machine_name(cgroup, machine);
1310 int cg_path_get_session(const char *path, char **session) {
1311 const char *e, *n, *x;
1318 /* Skip slices, if there are any */
1319 e = skip_slices(path);
1321 n = strchrnul(e, '/');
1325 s = strndupa(e, n - e);
1328 x = startswith(s, "session-");
1331 if (!endswith(x, ".scope"))
1338 r = strndup(x, l - 6);
1346 int cg_pid_get_session(pid_t pid, char **session) {
1347 _cleanup_free_ char *cgroup = NULL;
1352 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1356 return cg_path_get_session(cgroup, session);
1359 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1360 _cleanup_free_ char *slice = NULL;
1368 r = cg_path_get_slice(path, &slice);
1372 e = startswith(slice, "user-");
1375 if (!endswith(slice, ".slice"))
1378 s = strndupa(e, strlen(e) - 6);
1382 return parse_uid(s, uid);
1385 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1386 _cleanup_free_ char *cgroup = NULL;
1391 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1395 return cg_path_get_owner_uid(cgroup, uid);
1398 int cg_path_get_slice(const char *p, char **slice) {
1399 const char *e = NULL;
1408 p += strspn(p, "/");
1410 n = strcspn(p, "/");
1411 if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) {
1432 int cg_pid_get_slice(pid_t pid, char **slice) {
1433 _cleanup_free_ char *cgroup = NULL;
1438 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1442 return cg_path_get_slice(cgroup, slice);
1445 int cg_controller_from_attr(const char *attr, char **controller) {
1452 if (!filename_is_safe(attr))
1455 dot = strchr(attr, '.');
1461 c = strndup(attr, dot - attr);
1465 if (!cg_controller_is_valid(c, false)) {
1474 char *cg_escape(const char *p) {
1475 bool need_prefix = false;
1477 /* This implements very minimal escaping for names to be used
1478 * as file names in the cgroup tree: any name which might
1479 * conflict with a kernel name or is prefixed with '_' is
1480 * prefixed with a '_'. That way, when reading cgroup names it
1481 * is sufficient to remove a single prefixing underscore if
1484 /* The return value of this function (unlike cg_unescape())
1490 streq(p, "notify_on_release") ||
1491 streq(p, "release_agent") ||
1497 dot = strrchr(p, '.');
1500 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1505 n = strndupa(p, dot - p);
1507 if (check_hierarchy(n) >= 0)
1514 return strappend("_", p);
1519 char *cg_unescape(const char *p) {
1522 /* The return value of this function (unlike cg_escape())
1523 * doesn't need free()! */
1531 #define CONTROLLER_VALID \
1533 "abcdefghijklmnopqrstuvwxyz" \
1534 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
1537 bool cg_controller_is_valid(const char *p, bool allow_named) {
1544 s = startswith(p, "name=");
1549 if (*p == 0 || *p == '_')
1552 for (t = p; *t; t++)
1553 if (!strchr(CONTROLLER_VALID, *t))
1556 if (t - p > FILENAME_MAX)
1562 int cg_slice_to_path(const char *unit, char **ret) {
1563 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1569 if (!unit_name_is_valid(unit, false))
1572 if (!endswith(unit, ".slice"))
1575 p = unit_name_to_prefix(unit);
1579 dash = strchr(p, '-');
1581 _cleanup_free_ char *escaped = NULL;
1582 char n[dash - p + sizeof(".slice")];
1584 strcpy(stpncpy(n, p, dash - p), ".slice");
1586 if (!unit_name_is_valid(n, false))
1589 escaped = cg_escape(n);
1593 if (!strextend(&s, escaped, "/", NULL))
1596 dash = strchr(dash+1, '-');
1599 e = cg_escape(unit);
1603 if (!strextend(&s, e, NULL))
1612 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1613 _cleanup_free_ char *p = NULL;
1616 r = cg_get_path(controller, path, attribute, &p);
1620 return write_string_file(p, value);
1623 static const char mask_names[] =
1630 int cg_create_with_mask(CGroupControllerMask mask, const char *path) {
1631 CGroupControllerMask bit = 1;
1635 /* This one will create a cgroup in our private tree, but also
1636 * duplicate it in the trees specified in mask, and remove it
1639 /* First create the cgroup in our own hierarchy. */
1640 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1644 /* Then, do the same in the other hierarchies */
1645 NULSTR_FOREACH(n, mask_names) {
1649 cg_trim(n, path, true);
1657 int cg_attach_with_mask(CGroupControllerMask mask, const char *path, pid_t pid) {
1658 CGroupControllerMask bit = 1;
1662 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1664 NULSTR_FOREACH(n, mask_names) {
1666 cg_attach(n, path, pid);
1668 char prefix[strlen(path) + 1], *slash;
1670 /* OK, this one is a bit harder... Now we need
1671 * to add to the closest parent cgroup we
1673 strcpy(prefix, path);
1674 while ((slash = strrchr(prefix, '/'))) {
1678 q = cg_attach(n, prefix, pid);
1690 int cg_attach_many_with_mask(CGroupControllerMask mask, const char *path, Set* pids) {
1695 SET_FOREACH(pidp, pids, i) {
1696 pid_t pid = PTR_TO_LONG(pidp);
1699 k = cg_attach_with_mask(mask, path, pid);
1707 int cg_migrate_with_mask(CGroupControllerMask mask, const char *from, const char *to) {
1708 CGroupControllerMask bit = 1;
1712 if (path_equal(from, to))
1715 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1717 NULSTR_FOREACH(n, mask_names) {
1719 cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, to, n, to, false, false);
1721 char prefix[strlen(to) + 1], *slash;
1724 while ((slash = strrchr(prefix, '/'))) {
1729 q = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, to, n, prefix, false, false);
1741 int cg_trim_with_mask(CGroupControllerMask mask, const char *path, bool delete_root) {
1742 CGroupControllerMask bit = 1;
1746 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
1750 NULSTR_FOREACH(n, mask_names) {
1752 cg_trim(n, path, delete_root);
1760 CGroupControllerMask cg_mask_supported(void) {
1761 CGroupControllerMask bit = 1, mask = 0;
1764 NULSTR_FOREACH(n, mask_names) {
1765 if (check_hierarchy(n) >= 0)