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-shared.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 (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
446 else if (startswith(controller
, "name="))
447 return controller
+ 5;
452 static int join_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
455 if (!isempty(controller
)) {
456 if (!isempty(path
) && !isempty(suffix
))
457 t
= strjoin("/sys/fs/cgroup/", controller
, "/", path
, "/", suffix
, NULL
);
458 else if (!isempty(path
))
459 t
= strjoin("/sys/fs/cgroup/", controller
, "/", path
, NULL
);
460 else if (!isempty(suffix
))
461 t
= strjoin("/sys/fs/cgroup/", controller
, "/", suffix
, NULL
);
463 t
= strappend("/sys/fs/cgroup/", controller
);
465 if (!isempty(path
) && !isempty(suffix
))
466 t
= strjoin(path
, "/", suffix
, NULL
);
467 else if (!isempty(path
))
476 *fs
= path_kill_slashes(t
);
480 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
482 static thread_local
bool good
= false;
486 if (controller
&& !cg_controller_is_valid(controller
, true))
489 if (_unlikely_(!good
)) {
492 r
= path_is_mount_point("/sys/fs/cgroup", false);
498 /* Cache this to save a few stat()s */
502 p
= controller
? normalize_controller(controller
) : NULL
;
504 return join_path(p
, path
, suffix
, fs
);
507 static int check_hierarchy(const char *p
) {
512 if (!filename_is_valid(p
))
515 /* Check if this controller actually really exists */
516 cc
= strjoina("/sys/fs/cgroup/", p
);
517 if (laccess(cc
, F_OK
) < 0)
523 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
529 if (!cg_controller_is_valid(controller
, true))
532 /* Normalize the controller syntax */
533 p
= normalize_controller(controller
);
535 /* Check if this controller actually really exists */
536 r
= check_hierarchy(p
);
540 return join_path(p
, path
, suffix
, fs
);
543 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
548 if (typeflag
!= FTW_DP
)
551 if (ftwbuf
->level
< 1)
558 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
559 _cleanup_free_
char *fs
= NULL
;
564 r
= cg_get_path(controller
, path
, NULL
, &fs
);
569 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0)
570 r
= errno
? -errno
: -EIO
;
573 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
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
;
594 int cg_create(const char *controller
, const char *path
) {
595 _cleanup_free_
char *fs
= NULL
;
598 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
602 r
= mkdir_parents(fs
, 0755);
606 if (mkdir(fs
, 0755) < 0) {
617 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
622 r
= cg_create(controller
, path
);
626 q
= cg_attach(controller
, path
, pid
);
630 /* This does not remove the cgroup on failure */
634 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
635 _cleanup_free_
char *fs
= NULL
;
636 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
642 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
649 snprintf(c
, sizeof(c
), PID_FMT
"\n", pid
);
651 return write_string_file_no_create(fs
, c
);
654 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
661 r
= cg_attach(controller
, path
, pid
);
663 char prefix
[strlen(path
) + 1];
665 /* This didn't work? Then let's try all prefixes of
668 PATH_FOREACH_PREFIX(prefix
, path
) {
669 r
= cg_attach(controller
, prefix
, pid
);
678 int cg_set_group_access(
679 const char *controller
,
685 _cleanup_free_
char *fs
= NULL
;
690 if (mode
!= MODE_INVALID
)
693 r
= cg_get_path(controller
, path
, NULL
, &fs
);
697 return chmod_and_chown(fs
, mode
, uid
, gid
);
700 int cg_set_task_access(
701 const char *controller
,
707 _cleanup_free_
char *fs
= NULL
, *procs
= NULL
;
712 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
715 if (mode
!= MODE_INVALID
)
718 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
722 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
726 /* Compatibility, Always keep values for "tasks" in sync with
728 r
= cg_get_path(controller
, path
, "tasks", &procs
);
732 return chmod_and_chown(procs
, mode
, uid
, gid
);
735 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
736 _cleanup_fclose_
FILE *f
= NULL
;
745 if (!cg_controller_is_valid(controller
, true))
748 controller
= normalize_controller(controller
);
750 controller
= SYSTEMD_CGROUP_CONTROLLER
;
752 fs
= procfs_file_alloca(pid
, "cgroup");
756 return errno
== ENOENT
? -ESRCH
: -errno
;
758 cs
= strlen(controller
);
760 FOREACH_LINE(line
, f
, return -errno
) {
763 const char *word
, *state
;
768 l
= strchr(line
, ':');
779 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
) {
781 if (k
== cs
&& memcmp(word
, controller
, cs
) == 0) {
787 memcmp(word
, "name=", 5) == 0 &&
788 memcmp(word
+5, controller
, cs
) == 0) {
808 int cg_install_release_agent(const char *controller
, const char *agent
) {
809 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
815 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
819 r
= read_one_line_file(fs
, &contents
);
823 sc
= strstrip(contents
);
825 r
= write_string_file_no_create(fs
, agent
);
828 } else if (!streq(sc
, agent
))
833 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
839 r
= read_one_line_file(fs
, &contents
);
843 sc
= strstrip(contents
);
844 if (streq(sc
, "0")) {
845 r
= write_string_file_no_create(fs
, "1");
858 int cg_uninstall_release_agent(const char *controller
) {
859 _cleanup_free_
char *fs
= NULL
;
862 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
866 r
= write_string_file_no_create(fs
, "0");
873 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
877 r
= write_string_file_no_create(fs
, "");
884 int cg_is_empty(const char *controller
, const char *path
, bool ignore_self
) {
885 _cleanup_fclose_
FILE *f
= NULL
;
886 pid_t pid
= 0, self_pid
;
892 r
= cg_enumerate_processes(controller
, path
, &f
);
894 return r
== -ENOENT
? 1 : r
;
898 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
900 if (ignore_self
&& pid
== self_pid
)
913 int cg_is_empty_recursive(const char *controller
, const char *path
, bool ignore_self
) {
914 _cleanup_closedir_
DIR *d
= NULL
;
920 r
= cg_is_empty(controller
, path
, ignore_self
);
924 r
= cg_enumerate_subgroups(controller
, path
, &d
);
926 return r
== -ENOENT
? 1 : r
;
928 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
929 _cleanup_free_
char *p
= NULL
;
931 p
= strjoin(path
, "/", fn
, NULL
);
936 r
= cg_is_empty_recursive(controller
, p
, ignore_self
);
947 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
949 char *t
= NULL
, *u
= NULL
;
950 _cleanup_free_
char *v
= NULL
;
955 if (!path_is_safe(spec
))
963 *path
= path_kill_slashes(t
);
972 e
= strchr(spec
, ':');
974 if (!cg_controller_is_valid(spec
, true))
978 t
= strdup(normalize_controller(spec
));
991 v
= strndup(spec
, e
-spec
);
994 t
= strdup(normalize_controller(v
));
997 if (!cg_controller_is_valid(t
, true)) {
1002 if (streq(e
+1, "")) {
1015 if (!path_is_safe(u
) ||
1016 !path_is_absolute(u
)) {
1022 path_kill_slashes(u
);
1038 int cg_mangle_path(const char *path
, char **result
) {
1039 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1046 /* First, check if it already is a filesystem path */
1047 if (path_startswith(path
, "/sys/fs/cgroup")) {
1053 *result
= path_kill_slashes(t
);
1057 /* Otherwise, treat it as cg spec */
1058 r
= cg_split_spec(path
, &c
, &p
);
1062 return cg_get_path(c
? c
: SYSTEMD_CGROUP_CONTROLLER
, p
? p
: "/", NULL
, result
);
1065 int cg_get_root_path(char **path
) {
1071 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1075 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
);
1083 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1084 _cleanup_free_
char *rt
= NULL
;
1092 /* If the root was specified let's use that, otherwise
1093 * let's determine it from PID 1 */
1095 r
= cg_get_root_path(&rt
);
1102 p
= path_startswith(cgroup
, root
);
1111 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1112 _cleanup_free_
char *raw
= NULL
;
1119 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1123 r
= cg_shift_path(raw
, root
, &c
);
1143 int cg_path_decode_unit(const char *cgroup
, char **unit
){
1150 n
= strcspn(cgroup
, "/");
1154 c
= strndupa(cgroup
, n
);
1157 if (!unit_name_is_valid(c
, TEMPLATE_INVALID
))
1168 static bool valid_slice_name(const char *p
, size_t n
) {
1173 if (n
< strlen("x.slice"))
1176 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1182 c
= cg_unescape(buf
);
1184 return unit_name_is_valid(c
, TEMPLATE_INVALID
);
1190 static const char *skip_slices(const char *p
) {
1193 /* Skips over all slice assignments */
1198 p
+= strspn(p
, "/");
1200 n
= strcspn(p
, "/");
1201 if (!valid_slice_name(p
, n
))
1208 int cg_path_get_unit(const char *path
, char **ret
) {
1216 e
= skip_slices(path
);
1218 r
= cg_path_decode_unit(e
, &unit
);
1222 /* We skipped over the slices, don't accept any now */
1223 if (endswith(unit
, ".slice")) {
1232 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1233 _cleanup_free_
char *cgroup
= NULL
;
1238 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1242 return cg_path_get_unit(cgroup
, unit
);
1246 * Skip session-*.scope, but require it to be there.
1248 static const char *skip_session(const char *p
) {
1254 p
+= strspn(p
, "/");
1256 n
= strcspn(p
, "/");
1257 if (n
< strlen("session-x.scope"))
1260 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1261 char buf
[n
- 8 - 6 + 1];
1263 memcpy(buf
, p
+ 8, n
- 8 - 6);
1266 /* Note that session scopes never need unescaping,
1267 * since they cannot conflict with the kernel's own
1268 * names, hence we don't need to call cg_unescape()
1271 if (!session_id_valid(buf
))
1275 p
+= strspn(p
, "/");
1283 * Skip user@*.service, but require it to be there.
1285 static const char *skip_user_manager(const char *p
) {
1291 p
+= strspn(p
, "/");
1293 n
= strcspn(p
, "/");
1294 if (n
< strlen("user@x.service"))
1297 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1298 char buf
[n
- 5 - 8 + 1];
1300 memcpy(buf
, p
+ 5, n
- 5 - 8);
1303 /* Note that user manager services never need unescaping,
1304 * since they cannot conflict with the kernel's own
1305 * names, hence we don't need to call cg_unescape()
1308 if (parse_uid(buf
, NULL
) < 0)
1312 p
+= strspn(p
, "/");
1320 static const char *skip_user_prefix(const char *path
) {
1325 /* Skip slices, if there are any */
1326 e
= skip_slices(path
);
1328 /* Skip the user manager, if it's in the path now... */
1329 t
= skip_user_manager(e
);
1333 /* Alternatively skip the user session if it is in the path... */
1334 return skip_session(e
);
1337 int cg_path_get_user_unit(const char *path
, char **ret
) {
1343 t
= skip_user_prefix(path
);
1347 /* And from here on it looks pretty much the same as for a
1348 * system unit, hence let's use the same parser from here
1350 return cg_path_get_unit(t
, ret
);
1353 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1354 _cleanup_free_
char *cgroup
= NULL
;
1359 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1363 return cg_path_get_user_unit(cgroup
, unit
);
1366 int cg_path_get_machine_name(const char *path
, char **machine
) {
1367 _cleanup_free_
char *u
= NULL
, *sl
= NULL
;
1370 r
= cg_path_get_unit(path
, &u
);
1374 sl
= strjoin("/run/systemd/machines/unit:", u
, NULL
);
1378 return readlink_malloc(sl
, machine
);
1381 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1382 _cleanup_free_
char *cgroup
= NULL
;
1387 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1391 return cg_path_get_machine_name(cgroup
, machine
);
1394 int cg_path_get_session(const char *path
, char **session
) {
1395 _cleanup_free_
char *unit
= NULL
;
1401 r
= cg_path_get_unit(path
, &unit
);
1405 start
= startswith(unit
, "session-");
1408 end
= endswith(start
, ".scope");
1413 if (!session_id_valid(start
))
1429 int cg_pid_get_session(pid_t pid
, char **session
) {
1430 _cleanup_free_
char *cgroup
= NULL
;
1433 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1437 return cg_path_get_session(cgroup
, session
);
1440 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1441 _cleanup_free_
char *slice
= NULL
;
1447 r
= cg_path_get_slice(path
, &slice
);
1451 start
= startswith(slice
, "user-");
1454 end
= endswith(start
, ".slice");
1459 if (parse_uid(start
, uid
) < 0)
1465 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1466 _cleanup_free_
char *cgroup
= NULL
;
1469 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1473 return cg_path_get_owner_uid(cgroup
, uid
);
1476 int cg_path_get_slice(const char *p
, char **slice
) {
1477 const char *e
= NULL
;
1482 /* Finds the right-most slice unit from the beginning, but
1483 * stops before we come to the first non-slice unit. */
1488 p
+= strspn(p
, "/");
1490 n
= strcspn(p
, "/");
1491 if (!valid_slice_name(p
, n
)) {
1496 s
= strdup("-.slice");
1504 return cg_path_decode_unit(e
, slice
);
1512 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1513 _cleanup_free_
char *cgroup
= NULL
;
1518 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1522 return cg_path_get_slice(cgroup
, slice
);
1525 int cg_path_get_user_slice(const char *p
, char **slice
) {
1530 t
= skip_user_prefix(p
);
1534 /* And now it looks pretty much the same as for a system
1535 * slice, so let's just use the same parser from here on. */
1536 return cg_path_get_slice(t
, slice
);
1539 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1540 _cleanup_free_
char *cgroup
= NULL
;
1545 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1549 return cg_path_get_user_slice(cgroup
, slice
);
1552 char *cg_escape(const char *p
) {
1553 bool need_prefix
= false;
1555 /* This implements very minimal escaping for names to be used
1556 * as file names in the cgroup tree: any name which might
1557 * conflict with a kernel name or is prefixed with '_' is
1558 * prefixed with a '_'. That way, when reading cgroup names it
1559 * is sufficient to remove a single prefixing underscore if
1562 /* The return value of this function (unlike cg_unescape())
1568 streq(p
, "notify_on_release") ||
1569 streq(p
, "release_agent") ||
1575 dot
= strrchr(p
, '.');
1578 if (dot
- p
== 6 && memcmp(p
, "cgroup", 6) == 0)
1583 n
= strndupa(p
, dot
- p
);
1585 if (check_hierarchy(n
) >= 0)
1592 return strappend("_", p
);
1597 char *cg_unescape(const char *p
) {
1600 /* The return value of this function (unlike cg_escape())
1601 * doesn't need free()! */
1609 #define CONTROLLER_VALID \
1613 bool cg_controller_is_valid(const char *p
, bool allow_named
) {
1620 s
= startswith(p
, "name=");
1625 if (*p
== 0 || *p
== '_')
1628 for (t
= p
; *t
; t
++)
1629 if (!strchr(CONTROLLER_VALID
, *t
))
1632 if (t
- p
> FILENAME_MAX
)
1638 int cg_slice_to_path(const char *unit
, char **ret
) {
1639 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1645 if (streq(unit
, "-.slice")) {
1655 if (!unit_name_is_valid(unit
, TEMPLATE_INVALID
))
1658 if (!endswith(unit
, ".slice"))
1661 p
= unit_name_to_prefix(unit
);
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
, TEMPLATE_INVALID
))
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
);