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 "formats-util.h"
37 #include "path-util.h"
38 #include "unit-name.h"
42 #include "login-shared.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(NULL);
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)
198 if (sigcont && sig != SIGKILL)
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(NULL);
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_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
284 _cleanup_set_free_ Set *s = NULL;
300 _cleanup_fclose_ FILE *f = NULL;
304 r = cg_enumerate_processes(cfrom, pfrom, &f);
306 if (ret >= 0 && r != -ENOENT)
312 while ((r = cg_read_pid(f, &pid)) > 0) {
314 /* This might do weird stuff if we aren't a
315 * single-threaded program. However, we
316 * luckily know we are not */
317 if (ignore_self && pid == my_pid)
320 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
323 r = cg_attach(cto, pto, pid);
325 if (ret >= 0 && r != -ESRCH)
332 r = set_put(s, LONG_TO_PTR(pid));
352 int cg_migrate_recursive(
360 _cleanup_closedir_ DIR *d = NULL;
369 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
371 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
373 if (ret >= 0 && r != -ENOENT)
379 while ((r = cg_read_subgroup(d, &fn)) > 0) {
380 _cleanup_free_ char *p = NULL;
382 p = strjoin(pfrom, "/", fn, NULL);
391 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
392 if (r != 0 && ret >= 0)
396 if (r < 0 && ret >= 0)
400 r = cg_rmdir(cfrom, pfrom);
401 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
408 int cg_migrate_recursive_fallback(
423 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
425 char prefix[strlen(pto) + 1];
427 /* This didn't work? Then let's try all prefixes of the destination */
429 PATH_FOREACH_PREFIX(prefix, pto) {
430 r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
439 static const char *normalize_controller(const char *controller) {
443 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))
486 if (_unlikely_(!good)) {
489 r = path_is_mount_point("/sys/fs/cgroup", 0);
495 /* Cache this to save a few stat()s */
499 p = controller ? normalize_controller(controller) : NULL;
501 return join_path(p, path, suffix, fs);
504 static int check_hierarchy(const char *p) {
509 if (!filename_is_valid(p))
512 /* Check if this controller actually really exists */
513 cc = strjoina("/sys/fs/cgroup/", p);
514 if (laccess(cc, F_OK) < 0)
520 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
526 if (!cg_controller_is_valid(controller))
529 /* Normalize the controller syntax */
530 p = normalize_controller(controller);
532 /* Check if this controller actually really exists */
533 r = check_hierarchy(p);
537 return join_path(p, path, suffix, fs);
540 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
545 if (typeflag != FTW_DP)
548 if (ftwbuf->level < 1)
555 int cg_trim(const char *controller, const char *path, bool delete_root) {
556 _cleanup_free_ char *fs = NULL;
561 r = cg_get_path(controller, path, NULL, &fs);
566 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
567 r = errno ? -errno : -EIO;
570 if (rmdir(fs) < 0 && errno != ENOENT)
577 int cg_delete(const char *controller, const char *path) {
578 _cleanup_free_ char *parent = NULL;
583 r = path_get_parent(path, &parent);
587 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
588 return r == -ENOENT ? 0 : r;
591 int cg_create(const char *controller, const char *path) {
592 _cleanup_free_ char *fs = NULL;
595 r = cg_get_path_and_check(controller, path, NULL, &fs);
599 r = mkdir_parents(fs, 0755);
603 if (mkdir(fs, 0755) < 0) {
614 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
619 r = cg_create(controller, path);
623 q = cg_attach(controller, path, pid);
627 /* This does not remove the cgroup on failure */
631 int cg_attach(const char *controller, const char *path, pid_t pid) {
632 _cleanup_free_ char *fs = NULL;
633 char c[DECIMAL_STR_MAX(pid_t) + 2];
639 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
646 snprintf(c, sizeof(c), PID_FMT"\n", pid);
648 return write_string_file(fs, c, 0);
651 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
658 r = cg_attach(controller, path, pid);
660 char prefix[strlen(path) + 1];
662 /* This didn't work? Then let's try all prefixes of
665 PATH_FOREACH_PREFIX(prefix, path) {
666 r = cg_attach(controller, prefix, pid);
675 int cg_set_group_access(
676 const char *controller,
682 _cleanup_free_ char *fs = NULL;
687 if (mode != MODE_INVALID)
690 r = cg_get_path(controller, path, NULL, &fs);
694 return chmod_and_chown(fs, mode, uid, gid);
697 int cg_set_task_access(
698 const char *controller,
704 _cleanup_free_ char *fs = NULL, *procs = NULL;
709 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
712 if (mode != MODE_INVALID)
715 r = cg_get_path(controller, path, "cgroup.procs", &fs);
719 r = chmod_and_chown(fs, mode, uid, gid);
723 /* Compatibility, Always keep values for "tasks" in sync with
725 r = cg_get_path(controller, path, "tasks", &procs);
729 return chmod_and_chown(procs, mode, uid, gid);
732 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
733 _cleanup_fclose_ FILE *f = NULL;
742 if (!cg_controller_is_valid(controller))
745 controller = normalize_controller(controller);
747 controller = SYSTEMD_CGROUP_CONTROLLER;
749 fs = procfs_file_alloca(pid, "cgroup");
753 return errno == ENOENT ? -ESRCH : -errno;
755 cs = strlen(controller);
757 FOREACH_LINE(line, f, return -errno) {
760 const char *word, *state;
765 l = strchr(line, ':');
776 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
778 if (k == cs && memcmp(word, controller, cs) == 0) {
784 memcmp(word, "name=", 5) == 0 &&
785 memcmp(word+5, controller, cs) == 0) {
805 int cg_install_release_agent(const char *controller, const char *agent) {
806 _cleanup_free_ char *fs = NULL, *contents = NULL;
812 r = cg_get_path(controller, NULL, "release_agent", &fs);
816 r = read_one_line_file(fs, &contents);
820 sc = strstrip(contents);
822 r = write_string_file(fs, agent, 0);
825 } else if (!streq(sc, agent))
830 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
836 r = read_one_line_file(fs, &contents);
840 sc = strstrip(contents);
841 if (streq(sc, "0")) {
842 r = write_string_file(fs, "1", 0);
855 int cg_uninstall_release_agent(const char *controller) {
856 _cleanup_free_ char *fs = NULL;
859 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
863 r = write_string_file(fs, "0", 0);
870 r = cg_get_path(controller, NULL, "release_agent", &fs);
874 r = write_string_file(fs, "", 0);
881 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
882 _cleanup_fclose_ FILE *f = NULL;
883 pid_t pid = 0, self_pid;
889 r = cg_enumerate_processes(controller, path, &f);
891 return r == -ENOENT ? 1 : r;
895 while ((r = cg_read_pid(f, &pid)) > 0) {
897 if (ignore_self && pid == self_pid)
910 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
911 _cleanup_closedir_ DIR *d = NULL;
917 r = cg_is_empty(controller, path, ignore_self);
921 r = cg_enumerate_subgroups(controller, path, &d);
923 return r == -ENOENT ? 1 : r;
925 while ((r = cg_read_subgroup(d, &fn)) > 0) {
926 _cleanup_free_ char *p = NULL;
928 p = strjoin(path, "/", fn, NULL);
933 r = cg_is_empty_recursive(controller, p, ignore_self);
944 int cg_split_spec(const char *spec, char **controller, char **path) {
946 char *t = NULL, *u = NULL;
947 _cleanup_free_ char *v = NULL;
952 if (!path_is_safe(spec))
960 *path = path_kill_slashes(t);
969 e = strchr(spec, ':');
971 if (!cg_controller_is_valid(spec))
975 t = strdup(normalize_controller(spec));
988 v = strndup(spec, e-spec);
991 t = strdup(normalize_controller(v));
994 if (!cg_controller_is_valid(t)) {
999 if (streq(e+1, "")) {
1012 if (!path_is_safe(u) ||
1013 !path_is_absolute(u)) {
1019 path_kill_slashes(u);
1035 int cg_mangle_path(const char *path, char **result) {
1036 _cleanup_free_ char *c = NULL, *p = NULL;
1043 /* First, check if it already is a filesystem path */
1044 if (path_startswith(path, "/sys/fs/cgroup")) {
1050 *result = path_kill_slashes(t);
1054 /* Otherwise, treat it as cg spec */
1055 r = cg_split_spec(path, &c, &p);
1059 return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1062 int cg_get_root_path(char **path) {
1068 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1072 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1080 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1081 _cleanup_free_ char *rt = NULL;
1089 /* If the root was specified let's use that, otherwise
1090 * let's determine it from PID 1 */
1092 r = cg_get_root_path(&rt);
1099 p = path_startswith(cgroup, root);
1108 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1109 _cleanup_free_ char *raw = NULL;
1116 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1120 r = cg_shift_path(raw, root, &c);
1140 int cg_path_decode_unit(const char *cgroup, char **unit){
1147 n = strcspn(cgroup, "/");
1151 c = strndupa(cgroup, n);
1154 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1165 static bool valid_slice_name(const char *p, size_t n) {
1170 if (n < strlen("x.slice"))
1173 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1179 c = cg_unescape(buf);
1181 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1187 static const char *skip_slices(const char *p) {
1190 /* Skips over all slice assignments */
1195 p += strspn(p, "/");
1197 n = strcspn(p, "/");
1198 if (!valid_slice_name(p, n))
1205 int cg_path_get_unit(const char *path, char **ret) {
1213 e = skip_slices(path);
1215 r = cg_path_decode_unit(e, &unit);
1219 /* We skipped over the slices, don't accept any now */
1220 if (endswith(unit, ".slice")) {
1229 int cg_pid_get_unit(pid_t pid, char **unit) {
1230 _cleanup_free_ char *cgroup = NULL;
1235 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1239 return cg_path_get_unit(cgroup, unit);
1243 * Skip session-*.scope, but require it to be there.
1245 static const char *skip_session(const char *p) {
1251 p += strspn(p, "/");
1253 n = strcspn(p, "/");
1254 if (n < strlen("session-x.scope"))
1257 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1258 char buf[n - 8 - 6 + 1];
1260 memcpy(buf, p + 8, n - 8 - 6);
1263 /* Note that session scopes never need unescaping,
1264 * since they cannot conflict with the kernel's own
1265 * names, hence we don't need to call cg_unescape()
1268 if (!session_id_valid(buf))
1272 p += strspn(p, "/");
1280 * Skip user@*.service, but require it to be there.
1282 static const char *skip_user_manager(const char *p) {
1288 p += strspn(p, "/");
1290 n = strcspn(p, "/");
1291 if (n < strlen("user@x.service"))
1294 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1295 char buf[n - 5 - 8 + 1];
1297 memcpy(buf, p + 5, n - 5 - 8);
1300 /* Note that user manager services never need unescaping,
1301 * since they cannot conflict with the kernel's own
1302 * names, hence we don't need to call cg_unescape()
1305 if (parse_uid(buf, NULL) < 0)
1309 p += strspn(p, "/");
1317 static const char *skip_user_prefix(const char *path) {
1322 /* Skip slices, if there are any */
1323 e = skip_slices(path);
1325 /* Skip the user manager, if it's in the path now... */
1326 t = skip_user_manager(e);
1330 /* Alternatively skip the user session if it is in the path... */
1331 return skip_session(e);
1334 int cg_path_get_user_unit(const char *path, char **ret) {
1340 t = skip_user_prefix(path);
1344 /* And from here on it looks pretty much the same as for a
1345 * system unit, hence let's use the same parser from here
1347 return cg_path_get_unit(t, ret);
1350 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1351 _cleanup_free_ char *cgroup = NULL;
1356 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1360 return cg_path_get_user_unit(cgroup, unit);
1363 int cg_path_get_machine_name(const char *path, char **machine) {
1364 _cleanup_free_ char *u = NULL, *sl = NULL;
1367 r = cg_path_get_unit(path, &u);
1371 sl = strjoin("/run/systemd/machines/unit:", u, NULL);
1375 return readlink_malloc(sl, machine);
1378 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1379 _cleanup_free_ char *cgroup = NULL;
1384 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1388 return cg_path_get_machine_name(cgroup, machine);
1391 int cg_path_get_session(const char *path, char **session) {
1392 _cleanup_free_ char *unit = NULL;
1398 r = cg_path_get_unit(path, &unit);
1402 start = startswith(unit, "session-");
1405 end = endswith(start, ".scope");
1410 if (!session_id_valid(start))
1426 int cg_pid_get_session(pid_t pid, char **session) {
1427 _cleanup_free_ char *cgroup = NULL;
1430 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1434 return cg_path_get_session(cgroup, session);
1437 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1438 _cleanup_free_ char *slice = NULL;
1444 r = cg_path_get_slice(path, &slice);
1448 start = startswith(slice, "user-");
1451 end = endswith(start, ".slice");
1456 if (parse_uid(start, uid) < 0)
1462 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1463 _cleanup_free_ char *cgroup = NULL;
1466 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1470 return cg_path_get_owner_uid(cgroup, uid);
1473 int cg_path_get_slice(const char *p, char **slice) {
1474 const char *e = NULL;
1479 /* Finds the right-most slice unit from the beginning, but
1480 * stops before we come to the first non-slice unit. */
1485 p += strspn(p, "/");
1487 n = strcspn(p, "/");
1488 if (!valid_slice_name(p, n)) {
1493 s = strdup("-.slice");
1501 return cg_path_decode_unit(e, slice);
1509 int cg_pid_get_slice(pid_t pid, char **slice) {
1510 _cleanup_free_ char *cgroup = NULL;
1515 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1519 return cg_path_get_slice(cgroup, slice);
1522 int cg_path_get_user_slice(const char *p, char **slice) {
1527 t = skip_user_prefix(p);
1531 /* And now it looks pretty much the same as for a system
1532 * slice, so let's just use the same parser from here on. */
1533 return cg_path_get_slice(t, slice);
1536 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1537 _cleanup_free_ char *cgroup = NULL;
1542 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1546 return cg_path_get_user_slice(cgroup, slice);
1549 char *cg_escape(const char *p) {
1550 bool need_prefix = false;
1552 /* This implements very minimal escaping for names to be used
1553 * as file names in the cgroup tree: any name which might
1554 * conflict with a kernel name or is prefixed with '_' is
1555 * prefixed with a '_'. That way, when reading cgroup names it
1556 * is sufficient to remove a single prefixing underscore if
1559 /* The return value of this function (unlike cg_unescape())
1565 streq(p, "notify_on_release") ||
1566 streq(p, "release_agent") ||
1572 dot = strrchr(p, '.');
1575 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1580 n = strndupa(p, dot - p);
1582 if (check_hierarchy(n) >= 0)
1589 return strappend("_", p);
1594 char *cg_unescape(const char *p) {
1597 /* The return value of this function (unlike cg_escape())
1598 * doesn't need free()! */
1606 #define CONTROLLER_VALID \
1610 bool cg_controller_is_valid(const char *p) {
1616 s = startswith(p, "name=");
1620 if (*p == 0 || *p == '_')
1623 for (t = p; *t; t++)
1624 if (!strchr(CONTROLLER_VALID, *t))
1627 if (t - p > FILENAME_MAX)
1633 int cg_slice_to_path(const char *unit, char **ret) {
1634 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1641 if (streq(unit, "-.slice")) {
1651 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1654 if (!endswith(unit, ".slice"))
1657 r = unit_name_to_prefix(unit, &p);
1661 dash = strchr(p, '-');
1663 _cleanup_free_ char *escaped = NULL;
1664 char n[dash - p + sizeof(".slice")];
1666 if (isempty(dash + 1))
1669 strcpy(stpncpy(n, p, dash - p), ".slice");
1670 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1673 escaped = cg_escape(n);
1677 if (!strextend(&s, escaped, "/", NULL))
1680 dash = strchr(dash+1, '-');
1683 e = cg_escape(unit);
1687 if (!strextend(&s, e, NULL))
1696 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1697 _cleanup_free_ char *p = NULL;
1700 r = cg_get_path(controller, path, attribute, &p);
1704 return write_string_file(p, value, 0);
1707 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1708 _cleanup_free_ char *p = NULL;
1711 r = cg_get_path(controller, path, attribute, &p);
1715 return read_one_line_file(p, ret);
1718 static const char mask_names[] =
1725 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
1726 CGroupControllerMask bit = 1;
1730 /* This one will create a cgroup in our private tree, but also
1731 * duplicate it in the trees specified in mask, and remove it
1734 /* First create the cgroup in our own hierarchy. */
1735 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1739 /* Then, do the same in the other hierarchies */
1740 NULSTR_FOREACH(n, mask_names) {
1743 else if (supported & bit)
1744 cg_trim(n, path, true);
1752 int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1753 CGroupControllerMask bit = 1;
1757 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1761 NULSTR_FOREACH(n, mask_names) {
1763 if (supported & bit) {
1764 const char *p = NULL;
1767 p = path_callback(bit, userdata);
1772 cg_attach_fallback(n, path, pid);
1781 int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1786 SET_FOREACH(pidp, pids, i) {
1787 pid_t pid = PTR_TO_LONG(pidp);
1790 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1798 int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1799 CGroupControllerMask bit = 1;
1803 if (!path_equal(from, to)) {
1804 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1809 NULSTR_FOREACH(n, mask_names) {
1810 if (supported & bit) {
1811 const char *p = NULL;
1814 p = to_callback(bit, userdata);
1819 cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false);
1828 int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
1829 CGroupControllerMask bit = 1;
1833 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
1837 NULSTR_FOREACH(n, mask_names) {
1838 if (supported & bit)
1839 cg_trim(n, path, delete_root);
1847 CGroupControllerMask cg_mask_supported(void) {
1848 CGroupControllerMask bit = 1, mask = 0;
1851 NULSTR_FOREACH(n, mask_names) {
1852 if (check_hierarchy(n) >= 0)
1861 int cg_kernel_controllers(Set *controllers) {
1862 _cleanup_fclose_ FILE *f = NULL;
1866 assert(controllers);
1868 f = fopen("/proc/cgroups", "re");
1870 if (errno == ENOENT)
1875 /* Ignore the header line */
1876 (void) fgets(buf, sizeof(buf), f);
1883 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
1888 if (ferror(f) && errno)
1899 if (!filename_is_valid(controller)) {
1904 r = set_consume(controllers, controller);