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"
44 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
45 _cleanup_free_ char *fs = NULL;
51 r = cg_get_path(controller, path, "cgroup.procs", &fs);
63 int cg_read_pid(FILE *f, pid_t *_pid) {
66 /* Note that the cgroup.procs might contain duplicates! See
67 * cgroups.txt for details. */
73 if (fscanf(f, "%lu", &ul) != 1) {
78 return errno ? -errno : -EIO;
88 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
89 _cleanup_free_ char *fs = NULL;
95 /* This is not recursive! */
97 r = cg_get_path(controller, path, NULL, &fs);
109 int cg_read_subgroup(DIR *d, char **fn) {
115 FOREACH_DIRENT(de, d, return -errno) {
118 if (de->d_type != DT_DIR)
121 if (streq(de->d_name, ".") ||
122 streq(de->d_name, ".."))
125 b = strdup(de->d_name);
136 int cg_rmdir(const char *controller, const char *path) {
137 _cleanup_free_ char *p = NULL;
140 r = cg_get_path(controller, path, NULL, &p);
145 if (r < 0 && errno != ENOENT)
151 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
152 _cleanup_set_free_ Set *allocated_set = NULL;
159 /* This goes through the tasks list and kills them all. This
160 * is repeated until no further processes are added to the
161 * tasks list, to properly handle forking processes */
164 s = allocated_set = set_new(trivial_hash_func, trivial_compare_func);
172 _cleanup_fclose_ FILE *f = NULL;
176 r = cg_enumerate_processes(controller, path, &f);
178 if (ret >= 0 && r != -ENOENT)
184 while ((r = cg_read_pid(f, &pid)) > 0) {
186 if (ignore_self && pid == my_pid)
189 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
192 /* If we haven't killed this process yet, kill
194 if (kill(pid, sig) < 0) {
195 if (ret >= 0 && errno != ESRCH)
197 } else if (ret == 0) {
207 r = set_put(s, LONG_TO_PTR(pid));
223 /* To avoid racing against processes which fork
224 * quicker than we can kill them we repeat this until
225 * no new pids need to be killed. */
232 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
233 _cleanup_set_free_ Set *allocated_set = NULL;
234 _cleanup_closedir_ DIR *d = NULL;
242 s = allocated_set = set_new(trivial_hash_func, trivial_compare_func);
247 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
249 r = cg_enumerate_subgroups(controller, path, &d);
251 if (ret >= 0 && r != -ENOENT)
257 while ((r = cg_read_subgroup(d, &fn)) > 0) {
258 _cleanup_free_ char *p = NULL;
260 p = strjoin(path, "/", fn, NULL);
265 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
266 if (ret >= 0 && r != 0)
270 if (ret >= 0 && r < 0)
274 r = cg_rmdir(controller, path);
275 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
282 int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) {
287 /* This safely kills all processes; first it sends a SIGTERM,
288 * then checks 8 times after 200ms whether the group is now
289 * empty, then kills everything that is left with SIGKILL and
290 * finally checks 5 times after 200ms each whether the group
291 * is finally empty. */
293 for (i = 0; i < 15; i++) {
303 r = cg_kill_recursive(controller, path, sig, true, true, rem, NULL);
307 usleep(200 * USEC_PER_MSEC);
313 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
315 _cleanup_set_free_ Set *s = NULL;
324 s = set_new(trivial_hash_func, trivial_compare_func);
331 _cleanup_fclose_ FILE *f = NULL;
335 r = cg_enumerate_processes(cfrom, pfrom, &f);
337 if (ret >= 0 && r != -ENOENT)
343 while ((r = cg_read_pid(f, &pid)) > 0) {
345 /* This might do weird stuff if we aren't a
346 * single-threaded program. However, we
347 * luckily know we are not */
348 if (ignore_self && pid == my_pid)
351 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
354 r = cg_attach(cto, pto, pid);
356 if (ret >= 0 && r != -ESRCH)
363 r = set_put(s, LONG_TO_PTR(pid));
383 int cg_migrate_recursive(
391 _cleanup_closedir_ DIR *d = NULL;
400 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
402 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
404 if (ret >= 0 && r != -ENOENT)
410 while ((r = cg_read_subgroup(d, &fn)) > 0) {
411 _cleanup_free_ char *p = NULL;
413 p = strjoin(pfrom, "/", fn, NULL);
422 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
423 if (r != 0 && ret >= 0)
427 if (r < 0 && ret >= 0)
431 r = cg_rmdir(cfrom, pfrom);
432 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
439 int cg_migrate_recursive_fallback(
454 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
456 char prefix[strlen(pto) + 1];
458 /* This didn't work? Then let's try all prefixes of the destination */
460 PATH_FOREACH_PREFIX(prefix, pto) {
461 r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
470 static const char *normalize_controller(const char *controller) {
474 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
476 else if (startswith(controller, "name="))
477 return controller + 5;
482 static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
485 if (!isempty(controller)) {
486 if (!isempty(path) && !isempty(suffix))
487 t = strjoin("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
488 else if (!isempty(path))
489 t = strjoin("/sys/fs/cgroup/", controller, "/", path, NULL);
490 else if (!isempty(suffix))
491 t = strjoin("/sys/fs/cgroup/", controller, "/", suffix, NULL);
493 t = strappend("/sys/fs/cgroup/", controller);
495 if (!isempty(path) && !isempty(suffix))
496 t = strjoin(path, "/", suffix, NULL);
497 else if (!isempty(path))
506 path_kill_slashes(t);
512 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
514 static __thread bool good = false;
518 if (controller && !cg_controller_is_valid(controller, true))
521 if (_unlikely_(!good)) {
524 r = path_is_mount_point("/sys/fs/cgroup", false);
526 return r < 0 ? r : -ENOENT;
528 /* Cache this to save a few stat()s */
532 p = controller ? normalize_controller(controller) : NULL;
534 return join_path(p, path, suffix, fs);
537 static int check_hierarchy(const char *p) {
542 /* Check if this controller actually really exists */
543 cc = alloca(sizeof("/sys/fs/cgroup/") + strlen(p));
544 strcpy(stpcpy(cc, "/sys/fs/cgroup/"), p);
545 if (access(cc, F_OK) < 0)
551 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
557 if (!cg_controller_is_valid(controller, true))
560 /* Normalize the controller syntax */
561 p = normalize_controller(controller);
563 /* Check if this controller actually really exists */
564 r = check_hierarchy(p);
568 return join_path(p, path, suffix, fs);
571 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
576 if (typeflag != FTW_DP)
579 if (ftwbuf->level < 1)
586 int cg_trim(const char *controller, const char *path, bool delete_root) {
587 _cleanup_free_ char *fs = NULL;
592 r = cg_get_path(controller, path, NULL, &fs);
597 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
598 r = errno ? -errno : -EIO;
601 if (rmdir(fs) < 0 && errno != ENOENT)
608 int cg_delete(const char *controller, const char *path) {
609 _cleanup_free_ char *parent = NULL;
614 r = path_get_parent(path, &parent);
618 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
619 return r == -ENOENT ? 0 : r;
622 int cg_create(const char *controller, const char *path) {
623 _cleanup_free_ char *fs = NULL;
626 r = cg_get_path_and_check(controller, path, NULL, &fs);
630 r = mkdir_parents(fs, 0755);
634 if (mkdir(fs, 0755) < 0) {
645 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
650 r = cg_create(controller, path);
654 q = cg_attach(controller, path, pid);
658 /* This does not remove the cgroup on failure */
662 int cg_attach(const char *controller, const char *path, pid_t pid) {
663 _cleanup_free_ char *fs = NULL;
664 char c[DECIMAL_STR_MAX(pid_t) + 2];
670 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
677 snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
679 return write_string_file(fs, c);
682 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
689 r = cg_attach(controller, path, pid);
691 char prefix[strlen(path) + 1];
693 /* This didn't work? Then let's try all prefixes of
696 PATH_FOREACH_PREFIX(prefix, path) {
697 r = cg_attach(controller, prefix, pid);
706 int cg_set_group_access(
707 const char *controller,
713 _cleanup_free_ char *fs = NULL;
718 if (mode != (mode_t) -1)
721 r = cg_get_path(controller, path, NULL, &fs);
725 return chmod_and_chown(fs, mode, uid, gid);
728 int cg_set_task_access(
729 const char *controller,
735 _cleanup_free_ char *fs = NULL, *procs = NULL;
740 if (mode == (mode_t) -1 && uid == (uid_t) -1 && gid == (gid_t) -1)
743 if (mode != (mode_t) -1)
746 r = cg_get_path(controller, path, "cgroup.procs", &fs);
750 r = chmod_and_chown(fs, mode, uid, gid);
754 /* Compatibility, Always keep values for "tasks" in sync with
756 r = cg_get_path(controller, path, "tasks", &procs);
760 return chmod_and_chown(procs, mode, uid, gid);
763 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
764 _cleanup_fclose_ FILE *f = NULL;
773 if (!cg_controller_is_valid(controller, true))
776 controller = normalize_controller(controller);
778 controller = SYSTEMD_CGROUP_CONTROLLER;
781 fs = "/proc/self/cgroup";
783 fs = procfs_file_alloca(pid, "cgroup");
787 return errno == ENOENT ? -ESRCH : -errno;
789 cs = strlen(controller);
791 FOREACH_LINE(line, f, return -errno) {
799 l = strchr(line, ':');
810 FOREACH_WORD_SEPARATOR(w, k, l, ",", state) {
812 if (k == cs && memcmp(w, controller, cs) == 0) {
818 memcmp(w, "name=", 5) == 0 &&
819 memcmp(w+5, controller, cs) == 0) {
839 int cg_install_release_agent(const char *controller, const char *agent) {
840 _cleanup_free_ char *fs = NULL, *contents = NULL;
846 r = cg_get_path(controller, NULL, "release_agent", &fs);
850 r = read_one_line_file(fs, &contents);
854 sc = strstrip(contents);
856 r = write_string_file(fs, agent);
859 } else if (!streq(sc, agent))
864 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
870 r = read_one_line_file(fs, &contents);
874 sc = strstrip(contents);
875 if (streq(sc, "0")) {
876 r = write_string_file(fs, "1");
889 int cg_uninstall_release_agent(const char *controller) {
890 _cleanup_free_ char *fs = NULL;
893 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
897 r = write_string_file(fs, "0");
904 r = cg_get_path(controller, NULL, "release_agent", &fs);
908 r = write_string_file(fs, "");
915 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
916 _cleanup_fclose_ FILE *f = NULL;
917 pid_t pid = 0, self_pid;
923 r = cg_enumerate_processes(controller, path, &f);
925 return r == -ENOENT ? 1 : r;
929 while ((r = cg_read_pid(f, &pid)) > 0) {
931 if (ignore_self && pid == self_pid)
944 int cg_is_empty_by_spec(const char *spec, bool ignore_self) {
945 _cleanup_free_ char *controller = NULL, *path = NULL;
950 r = cg_split_spec(spec, &controller, &path);
954 return cg_is_empty(controller, path, ignore_self);
957 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
958 _cleanup_closedir_ DIR *d = NULL;
964 r = cg_is_empty(controller, path, ignore_self);
968 r = cg_enumerate_subgroups(controller, path, &d);
970 return r == -ENOENT ? 1 : r;
972 while ((r = cg_read_subgroup(d, &fn)) > 0) {
973 _cleanup_free_ char *p = NULL;
975 p = strjoin(path, "/", fn, NULL);
980 r = cg_is_empty_recursive(controller, p, ignore_self);
991 int cg_split_spec(const char *spec, char **controller, char **path) {
993 char *t = NULL, *u = NULL;
994 _cleanup_free_ char *v = NULL;
999 if (!path_is_safe(spec))
1007 path_kill_slashes(t);
1017 e = strchr(spec, ':');
1019 if (!cg_controller_is_valid(spec, true))
1023 t = strdup(normalize_controller(spec));
1036 v = strndup(spec, e-spec);
1039 t = strdup(normalize_controller(v));
1042 if (!cg_controller_is_valid(t, true)) {
1047 if (streq(e+1, "")) {
1060 if (!path_is_safe(u) ||
1061 !path_is_absolute(u)) {
1067 path_kill_slashes(u);
1083 int cg_join_spec(const char *controller, const char *path, char **spec) {
1089 controller = "systemd";
1091 if (!cg_controller_is_valid(controller, true))
1094 controller = normalize_controller(controller);
1097 if (!path_is_absolute(path))
1100 s = strjoin(controller, ":", path, NULL);
1104 path_kill_slashes(s + strlen(controller) + 1);
1110 int cg_mangle_path(const char *path, char **result) {
1111 _cleanup_free_ char *c = NULL, *p = NULL;
1118 /* First check if it already is a filesystem path */
1119 if (path_startswith(path, "/sys/fs/cgroup")) {
1125 path_kill_slashes(t);
1130 /* Otherwise treat it as cg spec */
1131 r = cg_split_spec(path, &c, &p);
1135 return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1138 int cg_get_root_path(char **path) {
1144 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1148 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1156 char **cg_shorten_controllers(char **controllers) {
1162 for (f = controllers, t = controllers; *f; f++) {
1166 p = normalize_controller(*f);
1168 if (streq(p, "systemd")) {
1173 if (!cg_controller_is_valid(p, true)) {
1174 log_warning("Controller %s is not valid, removing from controllers list.", p);
1179 r = check_hierarchy(p);
1181 log_debug("Controller %s is not available, removing from controllers list.", p);
1190 return strv_uniq(controllers);
1193 int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
1194 _cleanup_free_ char *cg_root = NULL;
1195 char *cg_process, *p;
1198 r = cg_get_root_path(&cg_root);
1202 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
1206 p = path_startswith(cg_process, cg_root);
1225 cg_process[p-cg_process] = 0;
1233 int cg_path_decode_unit(const char *cgroup, char **unit){
1239 e = strchrnul(cgroup, '/');
1240 c = strndupa(cgroup, e - cgroup);
1243 if (!unit_name_is_valid(c, false))
1254 static const char *skip_slices(const char *p) {
1255 /* Skips over all slice assignments */
1260 p += strspn(p, "/");
1262 n = strcspn(p, "/");
1263 if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0)
1270 int cg_path_get_unit(const char *path, char **unit) {
1276 e = skip_slices(path);
1278 return cg_path_decode_unit(e, unit);
1281 int cg_pid_get_unit(pid_t pid, char **unit) {
1282 _cleanup_free_ char *cgroup = NULL;
1287 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1291 return cg_path_get_unit(cgroup, unit);
1294 static const char *skip_session(const char *p) {
1299 p += strspn(p, "/");
1301 n = strcspn(p, "/");
1302 if (n <= 12 || memcmp(p, "session-", 8) != 0 || memcmp(p + n - 6, ".scope", 6) != 0)
1306 p += strspn(p, "/");
1311 int cg_path_get_user_unit(const char *path, char **unit) {
1317 /* We always have to parse the path from the beginning as unit
1318 * cgroups might have arbitrary child cgroups and we shouldn't get
1319 * confused by those */
1321 /* Skip slices, if there are any */
1322 e = skip_slices(path);
1324 /* Skip the session scope, require that there is one */
1325 e = skip_session(e);
1329 /* And skip more slices */
1332 return cg_path_decode_unit(e, unit);
1335 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1336 _cleanup_free_ char *cgroup = NULL;
1341 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1345 return cg_path_get_user_unit(cgroup, unit);
1348 int cg_path_get_machine_name(const char *path, char **machine) {
1349 const char *e, *n, *x;
1356 /* Skip slices, if there are any */
1357 e = skip_slices(path);
1359 n = strchrnul(e, '/');
1363 s = strndupa(e, n - e);
1366 x = startswith(s, "machine-");
1369 if (!endswith(x, ".scope"))
1376 r = strndup(x, l - 6);
1384 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1385 _cleanup_free_ char *cgroup = NULL;
1390 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1394 return cg_path_get_machine_name(cgroup, machine);
1397 int cg_path_get_session(const char *path, char **session) {
1398 const char *e, *n, *x;
1405 /* Skip slices, if there are any */
1406 e = skip_slices(path);
1408 n = strchrnul(e, '/');
1412 s = strndupa(e, n - e);
1415 x = startswith(s, "session-");
1418 if (!endswith(x, ".scope"))
1425 r = strndup(x, l - 6);
1433 int cg_pid_get_session(pid_t pid, char **session) {
1434 _cleanup_free_ char *cgroup = NULL;
1439 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1443 return cg_path_get_session(cgroup, session);
1446 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1447 _cleanup_free_ char *slice = NULL;
1455 r = cg_path_get_slice(path, &slice);
1459 e = startswith(slice, "user-");
1462 if (!endswith(slice, ".slice"))
1465 s = strndupa(e, strlen(e) - 6);
1469 return parse_uid(s, uid);
1472 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1473 _cleanup_free_ char *cgroup = NULL;
1478 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1482 return cg_path_get_owner_uid(cgroup, uid);
1485 int cg_path_get_slice(const char *p, char **slice) {
1486 const char *e = NULL;
1495 p += strspn(p, "/");
1497 n = strcspn(p, "/");
1498 if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) {
1519 int cg_pid_get_slice(pid_t pid, char **slice) {
1520 _cleanup_free_ char *cgroup = NULL;
1525 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1529 return cg_path_get_slice(cgroup, slice);
1532 int cg_controller_from_attr(const char *attr, char **controller) {
1539 if (!filename_is_safe(attr))
1542 dot = strchr(attr, '.');
1548 c = strndup(attr, dot - attr);
1552 if (!cg_controller_is_valid(c, false)) {
1561 char *cg_escape(const char *p) {
1562 bool need_prefix = false;
1564 /* This implements very minimal escaping for names to be used
1565 * as file names in the cgroup tree: any name which might
1566 * conflict with a kernel name or is prefixed with '_' is
1567 * prefixed with a '_'. That way, when reading cgroup names it
1568 * is sufficient to remove a single prefixing underscore if
1571 /* The return value of this function (unlike cg_unescape())
1577 streq(p, "notify_on_release") ||
1578 streq(p, "release_agent") ||
1584 dot = strrchr(p, '.');
1587 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1592 n = strndupa(p, dot - p);
1594 if (check_hierarchy(n) >= 0)
1601 return strappend("_", p);
1606 char *cg_unescape(const char *p) {
1609 /* The return value of this function (unlike cg_escape())
1610 * doesn't need free()! */
1618 #define CONTROLLER_VALID \
1622 bool cg_controller_is_valid(const char *p, bool allow_named) {
1629 s = startswith(p, "name=");
1634 if (*p == 0 || *p == '_')
1637 for (t = p; *t; t++)
1638 if (!strchr(CONTROLLER_VALID, *t))
1641 if (t - p > FILENAME_MAX)
1647 int cg_slice_to_path(const char *unit, char **ret) {
1648 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1654 if (!unit_name_is_valid(unit, false))
1657 if (!endswith(unit, ".slice"))
1660 p = unit_name_to_prefix(unit);
1664 dash = strchr(p, '-');
1666 _cleanup_free_ char *escaped = NULL;
1667 char n[dash - p + sizeof(".slice")];
1669 strcpy(stpncpy(n, p, dash - p), ".slice");
1671 if (!unit_name_is_valid(n, false))
1674 escaped = cg_escape(n);
1678 if (!strextend(&s, escaped, "/", NULL))
1681 dash = strchr(dash+1, '-');
1684 e = cg_escape(unit);
1688 if (!strextend(&s, e, NULL))
1697 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1698 _cleanup_free_ char *p = NULL;
1701 r = cg_get_path(controller, path, attribute, &p);
1705 return write_string_file(p, value);
1708 static const char mask_names[] =
1715 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
1716 CGroupControllerMask bit = 1;
1720 /* This one will create a cgroup in our private tree, but also
1721 * duplicate it in the trees specified in mask, and remove it
1724 /* First create the cgroup in our own hierarchy. */
1725 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1729 /* Then, do the same in the other hierarchies */
1730 NULSTR_FOREACH(n, mask_names) {
1733 else if (supported & bit)
1734 cg_trim(n, path, true);
1742 int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid) {
1743 CGroupControllerMask bit = 1;
1747 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1751 NULSTR_FOREACH(n, mask_names) {
1752 if (supported & bit)
1753 cg_attach_fallback(n, path, pid);
1761 int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids) {
1766 SET_FOREACH(pidp, pids, i) {
1767 pid_t pid = PTR_TO_LONG(pidp);
1770 q = cg_attach_everywhere(supported, path, pid);
1778 int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to) {
1779 CGroupControllerMask bit = 1;
1783 if (!path_equal(from, to)) {
1784 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1789 NULSTR_FOREACH(n, mask_names) {
1790 if (supported & bit)
1791 cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, to, false, false);
1799 int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
1800 CGroupControllerMask bit = 1;
1804 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
1808 NULSTR_FOREACH(n, mask_names) {
1809 if (supported & bit)
1810 cg_trim(n, path, delete_root);
1818 CGroupControllerMask cg_mask_supported(void) {
1819 CGroupControllerMask bit = 1, mask = 0;
1822 NULSTR_FOREACH(n, mask_names) {
1823 if (check_hierarchy(n) >= 0)