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 (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
445 else if (startswith(controller, "name="))
446 return controller + 5;
451 static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
454 if (!isempty(controller)) {
455 if (!isempty(path) && !isempty(suffix))
456 t = strjoin("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
457 else if (!isempty(path))
458 t = strjoin("/sys/fs/cgroup/", controller, "/", path, NULL);
459 else if (!isempty(suffix))
460 t = strjoin("/sys/fs/cgroup/", controller, "/", suffix, NULL);
462 t = strappend("/sys/fs/cgroup/", controller);
464 if (!isempty(path) && !isempty(suffix))
465 t = strjoin(path, "/", suffix, NULL);
466 else if (!isempty(path))
475 *fs = path_kill_slashes(t);
479 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
481 static thread_local bool good = false;
485 if (controller && !cg_controller_is_valid(controller, true))
488 if (_unlikely_(!good)) {
491 r = path_is_mount_point("/sys/fs/cgroup", false);
497 /* Cache this to save a few stat()s */
501 p = controller ? normalize_controller(controller) : NULL;
503 return join_path(p, path, suffix, fs);
506 static int check_hierarchy(const char *p) {
511 if (!filename_is_valid(p))
514 /* Check if this controller actually really exists */
515 cc = strjoina("/sys/fs/cgroup/", p);
516 if (laccess(cc, F_OK) < 0)
522 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
528 if (!cg_controller_is_valid(controller, true))
531 /* Normalize the controller syntax */
532 p = normalize_controller(controller);
534 /* Check if this controller actually really exists */
535 r = check_hierarchy(p);
539 return join_path(p, path, suffix, fs);
542 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
547 if (typeflag != FTW_DP)
550 if (ftwbuf->level < 1)
557 int cg_trim(const char *controller, const char *path, bool delete_root) {
558 _cleanup_free_ char *fs = NULL;
563 r = cg_get_path(controller, path, NULL, &fs);
568 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
569 r = errno ? -errno : -EIO;
572 if (rmdir(fs) < 0 && errno != ENOENT)
579 int cg_delete(const char *controller, const char *path) {
580 _cleanup_free_ char *parent = NULL;
585 r = path_get_parent(path, &parent);
589 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
590 return r == -ENOENT ? 0 : r;
593 int cg_create(const char *controller, const char *path) {
594 _cleanup_free_ char *fs = NULL;
597 r = cg_get_path_and_check(controller, path, NULL, &fs);
601 r = mkdir_parents(fs, 0755);
605 if (mkdir(fs, 0755) < 0) {
616 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
621 r = cg_create(controller, path);
625 q = cg_attach(controller, path, pid);
629 /* This does not remove the cgroup on failure */
633 int cg_attach(const char *controller, const char *path, pid_t pid) {
634 _cleanup_free_ char *fs = NULL;
635 char c[DECIMAL_STR_MAX(pid_t) + 2];
641 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
648 snprintf(c, sizeof(c), PID_FMT"\n", pid);
650 return write_string_file_no_create(fs, c);
653 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
660 r = cg_attach(controller, path, pid);
662 char prefix[strlen(path) + 1];
664 /* This didn't work? Then let's try all prefixes of
667 PATH_FOREACH_PREFIX(prefix, path) {
668 r = cg_attach(controller, prefix, pid);
677 int cg_set_group_access(
678 const char *controller,
684 _cleanup_free_ char *fs = NULL;
689 if (mode != MODE_INVALID)
692 r = cg_get_path(controller, path, NULL, &fs);
696 return chmod_and_chown(fs, mode, uid, gid);
699 int cg_set_task_access(
700 const char *controller,
706 _cleanup_free_ char *fs = NULL, *procs = NULL;
711 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
714 if (mode != MODE_INVALID)
717 r = cg_get_path(controller, path, "cgroup.procs", &fs);
721 r = chmod_and_chown(fs, mode, uid, gid);
725 /* Compatibility, Always keep values for "tasks" in sync with
727 r = cg_get_path(controller, path, "tasks", &procs);
731 return chmod_and_chown(procs, mode, uid, gid);
734 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
735 _cleanup_fclose_ FILE *f = NULL;
744 if (!cg_controller_is_valid(controller, true))
747 controller = normalize_controller(controller);
749 controller = SYSTEMD_CGROUP_CONTROLLER;
751 fs = procfs_file_alloca(pid, "cgroup");
755 return errno == ENOENT ? -ESRCH : -errno;
757 cs = strlen(controller);
759 FOREACH_LINE(line, f, return -errno) {
762 const char *word, *state;
767 l = strchr(line, ':');
778 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
780 if (k == cs && memcmp(word, controller, cs) == 0) {
786 memcmp(word, "name=", 5) == 0 &&
787 memcmp(word+5, controller, cs) == 0) {
807 int cg_install_release_agent(const char *controller, const char *agent) {
808 _cleanup_free_ char *fs = NULL, *contents = NULL;
814 r = cg_get_path(controller, NULL, "release_agent", &fs);
818 r = read_one_line_file(fs, &contents);
822 sc = strstrip(contents);
824 r = write_string_file_no_create(fs, agent);
827 } else if (!streq(sc, agent))
832 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
838 r = read_one_line_file(fs, &contents);
842 sc = strstrip(contents);
843 if (streq(sc, "0")) {
844 r = write_string_file_no_create(fs, "1");
857 int cg_uninstall_release_agent(const char *controller) {
858 _cleanup_free_ char *fs = NULL;
861 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
865 r = write_string_file_no_create(fs, "0");
872 r = cg_get_path(controller, NULL, "release_agent", &fs);
876 r = write_string_file_no_create(fs, "");
883 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
884 _cleanup_fclose_ FILE *f = NULL;
885 pid_t pid = 0, self_pid;
891 r = cg_enumerate_processes(controller, path, &f);
893 return r == -ENOENT ? 1 : r;
897 while ((r = cg_read_pid(f, &pid)) > 0) {
899 if (ignore_self && pid == self_pid)
912 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
913 _cleanup_closedir_ DIR *d = NULL;
919 r = cg_is_empty(controller, path, ignore_self);
923 r = cg_enumerate_subgroups(controller, path, &d);
925 return r == -ENOENT ? 1 : r;
927 while ((r = cg_read_subgroup(d, &fn)) > 0) {
928 _cleanup_free_ char *p = NULL;
930 p = strjoin(path, "/", fn, NULL);
935 r = cg_is_empty_recursive(controller, p, ignore_self);
946 int cg_split_spec(const char *spec, char **controller, char **path) {
948 char *t = NULL, *u = NULL;
949 _cleanup_free_ char *v = NULL;
954 if (!path_is_safe(spec))
962 *path = path_kill_slashes(t);
971 e = strchr(spec, ':');
973 if (!cg_controller_is_valid(spec, true))
977 t = strdup(normalize_controller(spec));
990 v = strndup(spec, e-spec);
993 t = strdup(normalize_controller(v));
996 if (!cg_controller_is_valid(t, true)) {
1001 if (streq(e+1, "")) {
1014 if (!path_is_safe(u) ||
1015 !path_is_absolute(u)) {
1021 path_kill_slashes(u);
1037 int cg_mangle_path(const char *path, char **result) {
1038 _cleanup_free_ char *c = NULL, *p = NULL;
1045 /* First, check if it already is a filesystem path */
1046 if (path_startswith(path, "/sys/fs/cgroup")) {
1052 *result = path_kill_slashes(t);
1056 /* Otherwise, treat it as cg spec */
1057 r = cg_split_spec(path, &c, &p);
1061 return cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1064 int cg_get_root_path(char **path) {
1070 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
1074 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1082 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1083 _cleanup_free_ char *rt = NULL;
1091 /* If the root was specified let's use that, otherwise
1092 * let's determine it from PID 1 */
1094 r = cg_get_root_path(&rt);
1101 p = path_startswith(cgroup, root);
1110 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1111 _cleanup_free_ char *raw = NULL;
1118 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &raw);
1122 r = cg_shift_path(raw, root, &c);
1142 int cg_path_decode_unit(const char *cgroup, char **unit){
1149 n = strcspn(cgroup, "/");
1153 c = strndupa(cgroup, n);
1156 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1167 static bool valid_slice_name(const char *p, size_t n) {
1172 if (n < strlen("x.slice"))
1175 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1181 c = cg_unescape(buf);
1183 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1189 static const char *skip_slices(const char *p) {
1192 /* Skips over all slice assignments */
1197 p += strspn(p, "/");
1199 n = strcspn(p, "/");
1200 if (!valid_slice_name(p, n))
1207 int cg_path_get_unit(const char *path, char **ret) {
1215 e = skip_slices(path);
1217 r = cg_path_decode_unit(e, &unit);
1221 /* We skipped over the slices, don't accept any now */
1222 if (endswith(unit, ".slice")) {
1231 int cg_pid_get_unit(pid_t pid, char **unit) {
1232 _cleanup_free_ char *cgroup = NULL;
1237 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1241 return cg_path_get_unit(cgroup, unit);
1245 * Skip session-*.scope, but require it to be there.
1247 static const char *skip_session(const char *p) {
1253 p += strspn(p, "/");
1255 n = strcspn(p, "/");
1256 if (n < strlen("session-x.scope"))
1259 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1260 char buf[n - 8 - 6 + 1];
1262 memcpy(buf, p + 8, n - 8 - 6);
1265 /* Note that session scopes never need unescaping,
1266 * since they cannot conflict with the kernel's own
1267 * names, hence we don't need to call cg_unescape()
1270 if (!session_id_valid(buf))
1274 p += strspn(p, "/");
1282 * Skip user@*.service, but require it to be there.
1284 static const char *skip_user_manager(const char *p) {
1290 p += strspn(p, "/");
1292 n = strcspn(p, "/");
1293 if (n < strlen("user@x.service"))
1296 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1297 char buf[n - 5 - 8 + 1];
1299 memcpy(buf, p + 5, n - 5 - 8);
1302 /* Note that user manager services never need unescaping,
1303 * since they cannot conflict with the kernel's own
1304 * names, hence we don't need to call cg_unescape()
1307 if (parse_uid(buf, NULL) < 0)
1311 p += strspn(p, "/");
1319 static const char *skip_user_prefix(const char *path) {
1324 /* Skip slices, if there are any */
1325 e = skip_slices(path);
1327 /* Skip the user manager, if it's in the path now... */
1328 t = skip_user_manager(e);
1332 /* Alternatively skip the user session if it is in the path... */
1333 return skip_session(e);
1336 int cg_path_get_user_unit(const char *path, char **ret) {
1342 t = skip_user_prefix(path);
1346 /* And from here on it looks pretty much the same as for a
1347 * system unit, hence let's use the same parser from here
1349 return cg_path_get_unit(t, ret);
1352 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1353 _cleanup_free_ char *cgroup = NULL;
1358 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1362 return cg_path_get_user_unit(cgroup, unit);
1365 int cg_path_get_machine_name(const char *path, char **machine) {
1366 _cleanup_free_ char *u = NULL, *sl = NULL;
1369 r = cg_path_get_unit(path, &u);
1373 sl = strjoin("/run/systemd/machines/unit:", u, NULL);
1377 return readlink_malloc(sl, machine);
1380 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1381 _cleanup_free_ char *cgroup = NULL;
1386 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1390 return cg_path_get_machine_name(cgroup, machine);
1393 int cg_path_get_session(const char *path, char **session) {
1394 _cleanup_free_ char *unit = NULL;
1400 r = cg_path_get_unit(path, &unit);
1404 start = startswith(unit, "session-");
1407 end = endswith(start, ".scope");
1412 if (!session_id_valid(start))
1428 int cg_pid_get_session(pid_t pid, char **session) {
1429 _cleanup_free_ char *cgroup = NULL;
1432 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1436 return cg_path_get_session(cgroup, session);
1439 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1440 _cleanup_free_ char *slice = NULL;
1446 r = cg_path_get_slice(path, &slice);
1450 start = startswith(slice, "user-");
1453 end = endswith(start, ".slice");
1458 if (parse_uid(start, uid) < 0)
1464 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1465 _cleanup_free_ char *cgroup = NULL;
1468 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1472 return cg_path_get_owner_uid(cgroup, uid);
1475 int cg_path_get_slice(const char *p, char **slice) {
1476 const char *e = NULL;
1481 /* Finds the right-most slice unit from the beginning, but
1482 * stops before we come to the first non-slice unit. */
1487 p += strspn(p, "/");
1489 n = strcspn(p, "/");
1490 if (!valid_slice_name(p, n)) {
1495 s = strdup("-.slice");
1503 return cg_path_decode_unit(e, slice);
1511 int cg_pid_get_slice(pid_t pid, char **slice) {
1512 _cleanup_free_ char *cgroup = NULL;
1517 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1521 return cg_path_get_slice(cgroup, slice);
1524 int cg_path_get_user_slice(const char *p, char **slice) {
1529 t = skip_user_prefix(p);
1533 /* And now it looks pretty much the same as for a system
1534 * slice, so let's just use the same parser from here on. */
1535 return cg_path_get_slice(t, slice);
1538 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1539 _cleanup_free_ char *cgroup = NULL;
1544 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1548 return cg_path_get_user_slice(cgroup, slice);
1551 char *cg_escape(const char *p) {
1552 bool need_prefix = false;
1554 /* This implements very minimal escaping for names to be used
1555 * as file names in the cgroup tree: any name which might
1556 * conflict with a kernel name or is prefixed with '_' is
1557 * prefixed with a '_'. That way, when reading cgroup names it
1558 * is sufficient to remove a single prefixing underscore if
1561 /* The return value of this function (unlike cg_unescape())
1567 streq(p, "notify_on_release") ||
1568 streq(p, "release_agent") ||
1574 dot = strrchr(p, '.');
1577 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1582 n = strndupa(p, dot - p);
1584 if (check_hierarchy(n) >= 0)
1591 return strappend("_", p);
1596 char *cg_unescape(const char *p) {
1599 /* The return value of this function (unlike cg_escape())
1600 * doesn't need free()! */
1608 #define CONTROLLER_VALID \
1612 bool cg_controller_is_valid(const char *p, bool allow_named) {
1619 s = startswith(p, "name=");
1624 if (*p == 0 || *p == '_')
1627 for (t = p; *t; t++)
1628 if (!strchr(CONTROLLER_VALID, *t))
1631 if (t - p > FILENAME_MAX)
1637 int cg_slice_to_path(const char *unit, char **ret) {
1638 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1645 if (streq(unit, "-.slice")) {
1655 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1658 if (!endswith(unit, ".slice"))
1661 r = unit_name_to_prefix(unit, &p);
1665 dash = strchr(p, '-');
1667 _cleanup_free_ char *escaped = NULL;
1668 char n[dash - p + sizeof(".slice")];
1670 if (isempty(dash + 1))
1673 strcpy(stpncpy(n, p, dash - p), ".slice");
1674 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1677 escaped = cg_escape(n);
1681 if (!strextend(&s, escaped, "/", NULL))
1684 dash = strchr(dash+1, '-');
1687 e = cg_escape(unit);
1691 if (!strextend(&s, e, NULL))
1700 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1701 _cleanup_free_ char *p = NULL;
1704 r = cg_get_path(controller, path, attribute, &p);
1708 return write_string_file_no_create(p, value);
1711 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1712 _cleanup_free_ char *p = NULL;
1715 r = cg_get_path(controller, path, attribute, &p);
1719 return read_one_line_file(p, ret);
1722 static const char mask_names[] =
1729 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
1730 CGroupControllerMask bit = 1;
1734 /* This one will create a cgroup in our private tree, but also
1735 * duplicate it in the trees specified in mask, and remove it
1738 /* First create the cgroup in our own hierarchy. */
1739 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1743 /* Then, do the same in the other hierarchies */
1744 NULSTR_FOREACH(n, mask_names) {
1747 else if (supported & bit)
1748 cg_trim(n, path, true);
1756 int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1757 CGroupControllerMask bit = 1;
1761 r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, path, pid);
1765 NULSTR_FOREACH(n, mask_names) {
1767 if (supported & bit) {
1768 const char *p = NULL;
1771 p = path_callback(bit, userdata);
1776 cg_attach_fallback(n, path, pid);
1785 int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1790 SET_FOREACH(pidp, pids, i) {
1791 pid_t pid = PTR_TO_LONG(pidp);
1794 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1802 int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1803 CGroupControllerMask bit = 1;
1807 if (!path_equal(from, to)) {
1808 r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
1813 NULSTR_FOREACH(n, mask_names) {
1814 if (supported & bit) {
1815 const char *p = NULL;
1818 p = to_callback(bit, userdata);
1823 cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false);
1832 int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
1833 CGroupControllerMask bit = 1;
1837 r = cg_trim(SYSTEMD_CGROUP_CONTROLLER, path, delete_root);
1841 NULSTR_FOREACH(n, mask_names) {
1842 if (supported & bit)
1843 cg_trim(n, path, delete_root);
1851 CGroupControllerMask cg_mask_supported(void) {
1852 CGroupControllerMask bit = 1, mask = 0;
1855 NULSTR_FOREACH(n, mask_names) {
1856 if (check_hierarchy(n) >= 0)
1865 int cg_kernel_controllers(Set *controllers) {
1866 _cleanup_fclose_ FILE *f = NULL;
1870 assert(controllers);
1872 f = fopen("/proc/cgroups", "re");
1874 if (errno == ENOENT)
1879 /* Ignore the header line */
1880 (void) fgets(buf, sizeof(buf), f);
1887 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
1892 if (ferror(f) && errno)
1903 if (!filename_is_valid(controller)) {
1908 r = set_consume(controllers, controller);