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 "process-util.h"
38 #include "path-util.h"
39 #include "unit-name.h"
43 #include "login-util.h"
45 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
46 _cleanup_free_ char *fs = NULL;
52 r = cg_get_path(controller, path, "cgroup.procs", &fs);
64 int cg_read_pid(FILE *f, pid_t *_pid) {
67 /* Note that the cgroup.procs might contain duplicates! See
68 * cgroups.txt for details. */
74 if (fscanf(f, "%lu", &ul) != 1) {
79 return errno ? -errno : -EIO;
89 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
90 _cleanup_free_ char *fs = NULL;
96 /* This is not recursive! */
98 r = cg_get_path(controller, path, NULL, &fs);
110 int cg_read_subgroup(DIR *d, char **fn) {
116 FOREACH_DIRENT(de, d, return -errno) {
119 if (de->d_type != DT_DIR)
122 if (streq(de->d_name, ".") ||
123 streq(de->d_name, ".."))
126 b = strdup(de->d_name);
137 int cg_rmdir(const char *controller, const char *path) {
138 _cleanup_free_ char *p = NULL;
141 r = cg_get_path(controller, path, NULL, &p);
146 if (r < 0 && errno != ENOENT)
152 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
153 _cleanup_set_free_ Set *allocated_set = NULL;
160 /* This goes through the tasks list and kills them all. This
161 * is repeated until no further processes are added to the
162 * tasks list, to properly handle forking processes */
165 s = allocated_set = set_new(NULL);
173 _cleanup_fclose_ FILE *f = NULL;
177 r = cg_enumerate_processes(controller, path, &f);
179 if (ret >= 0 && r != -ENOENT)
185 while ((r = cg_read_pid(f, &pid)) > 0) {
187 if (ignore_self && pid == my_pid)
190 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
193 /* If we haven't killed this process yet, kill
195 if (kill(pid, sig) < 0) {
196 if (ret >= 0 && errno != ESRCH)
199 if (sigcont && sig != SIGKILL)
208 r = set_put(s, LONG_TO_PTR(pid));
224 /* To avoid racing against processes which fork
225 * quicker than we can kill them we repeat this until
226 * no new pids need to be killed. */
233 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
234 _cleanup_set_free_ Set *allocated_set = NULL;
235 _cleanup_closedir_ DIR *d = NULL;
243 s = allocated_set = set_new(NULL);
248 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
250 r = cg_enumerate_subgroups(controller, path, &d);
252 if (ret >= 0 && r != -ENOENT)
258 while ((r = cg_read_subgroup(d, &fn)) > 0) {
259 _cleanup_free_ char *p = NULL;
261 p = strjoin(path, "/", fn, NULL);
266 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
267 if (ret >= 0 && r != 0)
271 if (ret >= 0 && r < 0)
275 r = cg_rmdir(controller, path);
276 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
283 int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
285 _cleanup_set_free_ Set *s = NULL;
301 _cleanup_fclose_ FILE *f = NULL;
305 r = cg_enumerate_processes(cfrom, pfrom, &f);
307 if (ret >= 0 && r != -ENOENT)
313 while ((r = cg_read_pid(f, &pid)) > 0) {
315 /* This might do weird stuff if we aren't a
316 * single-threaded program. However, we
317 * luckily know we are not */
318 if (ignore_self && pid == my_pid)
321 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
324 r = cg_attach(cto, pto, pid);
326 if (ret >= 0 && r != -ESRCH)
333 r = set_put(s, LONG_TO_PTR(pid));
353 int cg_migrate_recursive(
361 _cleanup_closedir_ DIR *d = NULL;
370 ret = cg_migrate(cfrom, pfrom, cto, pto, ignore_self);
372 r = cg_enumerate_subgroups(cfrom, pfrom, &d);
374 if (ret >= 0 && r != -ENOENT)
380 while ((r = cg_read_subgroup(d, &fn)) > 0) {
381 _cleanup_free_ char *p = NULL;
383 p = strjoin(pfrom, "/", fn, NULL);
392 r = cg_migrate_recursive(cfrom, p, cto, pto, ignore_self, rem);
393 if (r != 0 && ret >= 0)
397 if (r < 0 && ret >= 0)
401 r = cg_rmdir(cfrom, pfrom);
402 if (r < 0 && ret >= 0 && r != -ENOENT && r != -EBUSY)
409 int cg_migrate_recursive_fallback(
424 r = cg_migrate_recursive(cfrom, pfrom, cto, pto, ignore_self, rem);
426 char prefix[strlen(pto) + 1];
428 /* This didn't work? Then let's try all prefixes of the destination */
430 PATH_FOREACH_PREFIX(prefix, pto) {
431 r = cg_migrate_recursive(cfrom, pfrom, cto, prefix, ignore_self, rem);
440 static const char *normalize_controller(const char *controller) {
444 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 *fs = path_kill_slashes(t);
478 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
480 static thread_local bool good = false;
484 if (controller && !cg_controller_is_valid(controller))
487 if (_unlikely_(!good)) {
490 r = path_is_mount_point("/sys/fs/cgroup", 0);
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 if (!filename_is_valid(p))
513 /* Check if this controller actually really exists */
514 cc = strjoina("/sys/fs/cgroup/", p);
515 if (laccess(cc, F_OK) < 0)
521 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
527 if (!cg_controller_is_valid(controller))
530 /* Normalize the controller syntax */
531 p = normalize_controller(controller);
533 /* Check if this controller actually really exists */
534 r = check_hierarchy(p);
538 return join_path(p, path, suffix, fs);
541 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
546 if (typeflag != FTW_DP)
549 if (ftwbuf->level < 1)
556 int cg_trim(const char *controller, const char *path, bool delete_root) {
557 _cleanup_free_ char *fs = NULL;
562 r = cg_get_path(controller, path, NULL, &fs);
567 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) != 0)
568 r = errno ? -errno : -EIO;
571 if (rmdir(fs) < 0 && errno != ENOENT)
578 /// UNNEDED by elogind
580 int cg_delete(const char *controller, const char *path) {
581 _cleanup_free_ char *parent = NULL;
586 r = path_get_parent(path, &parent);
590 r = cg_migrate_recursive(controller, path, controller, parent, false, true);
591 return r == -ENOENT ? 0 : r;
595 int cg_create(const char *controller, const char *path) {
596 _cleanup_free_ char *fs = NULL;
599 r = cg_get_path_and_check(controller, path, NULL, &fs);
603 r = mkdir_parents(fs, 0755);
607 if (mkdir(fs, 0755) < 0) {
618 /// UNNEEDED by elogind
620 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
625 r = cg_create(controller, path);
629 q = cg_attach(controller, path, pid);
633 /* This does not remove the cgroup on failure */
638 int cg_attach(const char *controller, const char *path, pid_t pid) {
639 _cleanup_free_ char *fs = NULL;
640 char c[DECIMAL_STR_MAX(pid_t) + 2];
646 r = cg_get_path_and_check(controller, path, "cgroup.procs", &fs);
653 snprintf(c, sizeof(c), PID_FMT"\n", pid);
655 return write_string_file_no_create(fs, c);
658 int cg_attach_fallback(const char *controller, const char *path, pid_t pid) {
665 r = cg_attach(controller, path, pid);
667 char prefix[strlen(path) + 1];
669 /* This didn't work? Then let's try all prefixes of
672 PATH_FOREACH_PREFIX(prefix, path) {
673 r = cg_attach(controller, prefix, pid);
682 /// UNNEEDED by elogind
684 int cg_set_group_access(
685 const char *controller,
691 _cleanup_free_ char *fs = NULL;
696 if (mode != MODE_INVALID)
699 r = cg_get_path(controller, path, NULL, &fs);
703 return chmod_and_chown(fs, mode, uid, gid);
706 int cg_set_task_access(
707 const char *controller,
713 _cleanup_free_ char *fs = NULL, *procs = NULL;
718 if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID)
721 if (mode != MODE_INVALID)
724 r = cg_get_path(controller, path, "cgroup.procs", &fs);
728 r = chmod_and_chown(fs, mode, uid, gid);
732 /* Compatibility, Always keep values for "tasks" in sync with
734 r = cg_get_path(controller, path, "tasks", &procs);
738 return chmod_and_chown(procs, mode, uid, gid);
742 int cg_pid_get_path(const char *controller, pid_t pid, char **path) {
743 _cleanup_fclose_ FILE *f = NULL;
752 if (!cg_controller_is_valid(controller))
755 controller = normalize_controller(controller);
757 controller = ELOGIND_CGROUP_CONTROLLER;
759 fs = procfs_file_alloca(pid, "cgroup");
763 return errno == ENOENT ? -ESRCH : -errno;
765 cs = strlen(controller);
767 FOREACH_LINE(line, f, return -errno) {
770 const char *word, *state;
775 l = strchr(line, ':');
786 FOREACH_WORD_SEPARATOR(word, k, l, ",", state) {
788 if (k == cs && memcmp(word, controller, cs) == 0) {
794 memcmp(word, "name=", 5) == 0 &&
795 memcmp(word+5, controller, cs) == 0) {
815 /// UNNEEDED by elogind
817 int cg_install_release_agent(const char *controller, const char *agent) {
818 _cleanup_free_ char *fs = NULL, *contents = NULL;
824 r = cg_get_path(controller, NULL, "release_agent", &fs);
828 r = read_one_line_file(fs, &contents);
832 sc = strstrip(contents);
834 r = write_string_file_no_create(fs, agent);
837 } else if (!streq(sc, agent))
841 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
845 contents = mfree(contents);
846 r = read_one_line_file(fs, &contents);
850 sc = strstrip(contents);
851 if (streq(sc, "0")) {
852 r = write_string_file_no_create(fs, "1");
865 int cg_uninstall_release_agent(const char *controller) {
866 _cleanup_free_ char *fs = NULL;
869 r = cg_get_path(controller, NULL, "notify_on_release", &fs);
873 r = write_string_file_no_create(fs, "0");
879 r = cg_get_path(controller, NULL, "release_agent", &fs);
883 r = write_string_file_no_create(fs, "");
891 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
892 _cleanup_fclose_ FILE *f = NULL;
893 pid_t pid = 0, self_pid;
899 r = cg_enumerate_processes(controller, path, &f);
901 return r == -ENOENT ? 1 : r;
905 while ((r = cg_read_pid(f, &pid)) > 0) {
907 if (ignore_self && pid == self_pid)
920 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
921 _cleanup_closedir_ DIR *d = NULL;
927 r = cg_is_empty(controller, path, ignore_self);
931 r = cg_enumerate_subgroups(controller, path, &d);
933 return r == -ENOENT ? 1 : r;
935 while ((r = cg_read_subgroup(d, &fn)) > 0) {
936 _cleanup_free_ char *p = NULL;
938 p = strjoin(path, "/", fn, NULL);
943 r = cg_is_empty_recursive(controller, p, ignore_self);
954 int cg_split_spec(const char *spec, char **controller, char **path) {
956 char *t = NULL, *u = NULL;
957 _cleanup_free_ char *v = NULL;
962 if (!path_is_safe(spec))
970 *path = path_kill_slashes(t);
979 e = strchr(spec, ':');
981 if (!cg_controller_is_valid(spec))
985 t = strdup(normalize_controller(spec));
998 v = strndup(spec, e-spec);
1001 t = strdup(normalize_controller(v));
1004 if (!cg_controller_is_valid(t)) {
1009 if (streq(e+1, "")) {
1022 if (!path_is_safe(u) ||
1023 !path_is_absolute(u)) {
1029 path_kill_slashes(u);
1045 int cg_mangle_path(const char *path, char **result) {
1046 _cleanup_free_ char *c = NULL, *p = NULL;
1053 /* First, check if it already is a filesystem path */
1054 if (path_startswith(path, "/sys/fs/cgroup")) {
1060 *result = path_kill_slashes(t);
1064 /* Otherwise, treat it as cg spec */
1065 r = cg_split_spec(path, &c, &p);
1069 return cg_get_path(c ? c : ELOGIND_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1072 int cg_get_root_path(char **path) {
1078 r = cg_pid_get_path(ELOGIND_CGROUP_CONTROLLER, 1, &p);
1082 e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
1090 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
1091 _cleanup_free_ char *rt = NULL;
1099 /* If the root was specified let's use that, otherwise
1100 * let's determine it from PID 1 */
1102 r = cg_get_root_path(&rt);
1109 p = path_startswith(cgroup, root);
1118 int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
1119 _cleanup_free_ char *raw = NULL;
1126 r = cg_pid_get_path(ELOGIND_CGROUP_CONTROLLER, pid, &raw);
1130 r = cg_shift_path(raw, root, &c);
1150 int cg_path_decode_unit(const char *cgroup, char **unit){
1157 n = strcspn(cgroup, "/");
1161 c = strndupa(cgroup, n);
1164 if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1175 static bool valid_slice_name(const char *p, size_t n) {
1180 if (n < strlen("x.slice"))
1183 if (memcmp(p + n - 6, ".slice", 6) == 0) {
1189 c = cg_unescape(buf);
1191 return unit_name_is_valid(c, UNIT_NAME_PLAIN);
1197 static const char *skip_slices(const char *p) {
1200 /* Skips over all slice assignments */
1205 p += strspn(p, "/");
1207 n = strcspn(p, "/");
1208 if (!valid_slice_name(p, n))
1215 int cg_path_get_unit(const char *path, char **ret) {
1223 e = skip_slices(path);
1225 r = cg_path_decode_unit(e, &unit);
1229 /* We skipped over the slices, don't accept any now */
1230 if (endswith(unit, ".slice")) {
1239 int cg_pid_get_unit(pid_t pid, char **unit) {
1240 _cleanup_free_ char *cgroup = NULL;
1245 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1249 return cg_path_get_unit(cgroup, unit);
1253 * Skip session-*.scope, but require it to be there.
1255 static const char *skip_session(const char *p) {
1261 p += strspn(p, "/");
1263 n = strcspn(p, "/");
1264 if (n < strlen("session-x.scope"))
1267 if (memcmp(p, "session-", 8) == 0 && memcmp(p + n - 6, ".scope", 6) == 0) {
1268 char buf[n - 8 - 6 + 1];
1270 memcpy(buf, p + 8, n - 8 - 6);
1273 /* Note that session scopes never need unescaping,
1274 * since they cannot conflict with the kernel's own
1275 * names, hence we don't need to call cg_unescape()
1278 if (!session_id_valid(buf))
1282 p += strspn(p, "/");
1290 * Skip user@*.service, but require it to be there.
1292 static const char *skip_user_manager(const char *p) {
1298 p += strspn(p, "/");
1300 n = strcspn(p, "/");
1301 if (n < strlen("user@x.service"))
1304 if (memcmp(p, "user@", 5) == 0 && memcmp(p + n - 8, ".service", 8) == 0) {
1305 char buf[n - 5 - 8 + 1];
1307 memcpy(buf, p + 5, n - 5 - 8);
1310 /* Note that user manager services never need unescaping,
1311 * since they cannot conflict with the kernel's own
1312 * names, hence we don't need to call cg_unescape()
1315 if (parse_uid(buf, NULL) < 0)
1319 p += strspn(p, "/");
1327 static const char *skip_user_prefix(const char *path) {
1332 /* Skip slices, if there are any */
1333 e = skip_slices(path);
1335 /* Skip the user manager, if it's in the path now... */
1336 t = skip_user_manager(e);
1340 /* Alternatively skip the user session if it is in the path... */
1341 return skip_session(e);
1344 int cg_path_get_user_unit(const char *path, char **ret) {
1350 t = skip_user_prefix(path);
1354 /* And from here on it looks pretty much the same as for a
1355 * system unit, hence let's use the same parser from here
1357 return cg_path_get_unit(t, ret);
1360 /// UNNEDED by elogind
1362 int cg_pid_get_user_unit(pid_t pid, char **unit) {
1363 _cleanup_free_ char *cgroup = NULL;
1368 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1372 return cg_path_get_user_unit(cgroup, unit);
1376 int cg_path_get_machine_name(const char *path, char **machine) {
1377 _cleanup_free_ char *u = NULL, *sl = NULL;
1380 r = cg_path_get_unit(path, &u);
1384 sl = strjoin("/run/systemd/machines/unit:", u, NULL);
1388 return readlink_malloc(sl, machine);
1391 /// UNNEDED by elogind
1393 int cg_pid_get_machine_name(pid_t pid, char **machine) {
1394 _cleanup_free_ char *cgroup = NULL;
1399 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1403 return cg_path_get_machine_name(cgroup, machine);
1407 int cg_path_get_session(const char *path, char **session) {
1408 _cleanup_free_ char *unit = NULL;
1414 r = cg_path_get_unit(path, &unit);
1418 start = startswith(unit, "session-");
1421 end = endswith(start, ".scope");
1426 if (!session_id_valid(start))
1442 int cg_pid_get_session(pid_t pid, char **session) {
1443 _cleanup_free_ char *cgroup = NULL;
1446 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1450 return cg_path_get_session(cgroup, session);
1453 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
1454 _cleanup_free_ char *slice = NULL;
1460 r = cg_path_get_slice(path, &slice);
1464 start = startswith(slice, "user-");
1467 end = endswith(start, ".slice");
1472 if (parse_uid(start, uid) < 0)
1478 /// UNNEDED by elogind
1480 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
1481 _cleanup_free_ char *cgroup = NULL;
1484 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1488 return cg_path_get_owner_uid(cgroup, uid);
1492 int cg_path_get_slice(const char *p, char **slice) {
1493 const char *e = NULL;
1498 /* Finds the right-most slice unit from the beginning, but
1499 * stops before we come to the first non-slice unit. */
1504 p += strspn(p, "/");
1506 n = strcspn(p, "/");
1507 if (!valid_slice_name(p, n)) {
1512 s = strdup("-.slice");
1520 return cg_path_decode_unit(e, slice);
1528 int cg_pid_get_slice(pid_t pid, char **slice) {
1529 _cleanup_free_ char *cgroup = NULL;
1534 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1538 return cg_path_get_slice(cgroup, slice);
1541 int cg_path_get_user_slice(const char *p, char **slice) {
1546 t = skip_user_prefix(p);
1550 /* And now it looks pretty much the same as for a system
1551 * slice, so let's just use the same parser from here on. */
1552 return cg_path_get_slice(t, slice);
1555 int cg_pid_get_user_slice(pid_t pid, char **slice) {
1556 _cleanup_free_ char *cgroup = NULL;
1561 r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
1565 return cg_path_get_user_slice(cgroup, slice);
1568 char *cg_escape(const char *p) {
1569 bool need_prefix = false;
1571 /* This implements very minimal escaping for names to be used
1572 * as file names in the cgroup tree: any name which might
1573 * conflict with a kernel name or is prefixed with '_' is
1574 * prefixed with a '_'. That way, when reading cgroup names it
1575 * is sufficient to remove a single prefixing underscore if
1578 /* The return value of this function (unlike cg_unescape())
1584 streq(p, "notify_on_release") ||
1585 streq(p, "release_agent") ||
1591 dot = strrchr(p, '.');
1594 if (dot - p == 6 && memcmp(p, "cgroup", 6) == 0)
1599 n = strndupa(p, dot - p);
1601 if (check_hierarchy(n) >= 0)
1608 return strappend("_", p);
1613 char *cg_unescape(const char *p) {
1616 /* The return value of this function (unlike cg_escape())
1617 * doesn't need free()! */
1625 #define CONTROLLER_VALID \
1629 bool cg_controller_is_valid(const char *p) {
1635 s = startswith(p, "name=");
1639 if (*p == 0 || *p == '_')
1642 for (t = p; *t; t++)
1643 if (!strchr(CONTROLLER_VALID, *t))
1646 if (t - p > FILENAME_MAX)
1652 /// UNNEEDED by elogind
1654 int cg_slice_to_path(const char *unit, char **ret) {
1655 _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
1662 if (streq(unit, "-.slice")) {
1672 if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN))
1675 if (!endswith(unit, ".slice"))
1678 r = unit_name_to_prefix(unit, &p);
1682 dash = strchr(p, '-');
1684 /* Don't allow initial dashes */
1689 _cleanup_free_ char *escaped = NULL;
1690 char n[dash - p + sizeof(".slice")];
1692 /* Don't allow trailing or double dashes */
1693 if (dash[1] == 0 || dash[1] == '-')
1696 strcpy(stpncpy(n, p, dash - p), ".slice");
1697 if (!unit_name_is_valid(n, UNIT_NAME_PLAIN))
1700 escaped = cg_escape(n);
1704 if (!strextend(&s, escaped, "/", NULL))
1707 dash = strchr(dash+1, '-');
1710 e = cg_escape(unit);
1714 if (!strextend(&s, e, NULL))
1723 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
1724 _cleanup_free_ char *p = NULL;
1727 r = cg_get_path(controller, path, attribute, &p);
1731 return write_string_file_no_create(p, value);
1734 int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) {
1735 _cleanup_free_ char *p = NULL;
1738 r = cg_get_path(controller, path, attribute, &p);
1742 return read_one_line_file(p, ret);
1746 static const char mask_names[] =
1753 /// UNNEEDED by elogind
1755 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
1756 CGroupControllerMask bit = 1;
1760 /* This one will create a cgroup in our private tree, but also
1761 * duplicate it in the trees specified in mask, and remove it
1764 /* First create the cgroup in our own hierarchy. */
1765 r = cg_create(ELOGIND_CGROUP_CONTROLLER, path);
1769 /* Then, do the same in the other hierarchies */
1770 NULSTR_FOREACH(n, mask_names) {
1773 else if (supported & bit)
1774 cg_trim(n, path, true);
1783 int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
1784 CGroupControllerMask bit = 1;
1788 r = cg_attach(ELOGIND_CGROUP_CONTROLLER, path, pid);
1792 NULSTR_FOREACH(n, mask_names) {
1794 if (supported & bit) {
1795 const char *p = NULL;
1798 p = path_callback(bit, userdata);
1803 cg_attach_fallback(n, p, pid);
1812 /// UNNEEDED by elogind
1814 int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) {
1819 SET_FOREACH(pidp, pids, i) {
1820 pid_t pid = PTR_TO_LONG(pidp);
1823 q = cg_attach_everywhere(supported, path, pid, path_callback, userdata);
1831 int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
1832 CGroupControllerMask bit = 1;
1836 if (!path_equal(from, to)) {
1837 r = cg_migrate_recursive(ELOGIND_CGROUP_CONTROLLER, from, ELOGIND_CGROUP_CONTROLLER, to, false, true);
1842 NULSTR_FOREACH(n, mask_names) {
1843 if (supported & bit) {
1844 const char *p = NULL;
1847 p = to_callback(bit, userdata);
1852 cg_migrate_recursive_fallback(ELOGIND_CGROUP_CONTROLLER, to, n, p, false, false);
1861 int cg_trim_everywhere(CGroupControllerMask supported, const char *path, bool delete_root) {
1862 CGroupControllerMask bit = 1;
1866 r = cg_trim(ELOGIND_CGROUP_CONTROLLER, path, delete_root);
1870 NULSTR_FOREACH(n, mask_names) {
1871 if (supported & bit)
1872 cg_trim(n, path, delete_root);
1880 CGroupControllerMask cg_mask_supported(void) {
1881 CGroupControllerMask bit = 1, mask = 0;
1884 NULSTR_FOREACH(n, mask_names) {
1885 if (check_hierarchy(n) >= 0)
1894 int cg_kernel_controllers(Set *controllers) {
1895 _cleanup_fclose_ FILE *f = NULL;
1899 assert(controllers);
1901 f = fopen("/proc/cgroups", "re");
1903 if (errno == ENOENT)
1908 /* Ignore the header line */
1909 (void) fgets(buf, sizeof(buf), f);
1916 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
1921 if (ferror(f) && errno)
1932 if (!filename_is_valid(controller)) {
1937 r = set_consume(controllers, controller);