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>
35 #include "formats-util.h"
36 #include "process-util.h"
37 #include "path-util.h"
38 #include "unit-name.h"
42 #include "login-util.h"
43 #include "cgroup-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_ALL(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
)
200 (void) kill(pid
, SIGCONT
);
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 (r
!= 0 && ret
>= 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 /* Ignore kernel threads. Since they can only
325 * exist in the root cgroup, we only check for
328 (isempty(pfrom
) || path_equal(pfrom
, "/")) &&
329 is_kernel_thread(pid
) > 0)
332 r
= cg_attach(cto
, pto
, pid
);
334 if (ret
>= 0 && r
!= -ESRCH
)
341 r
= set_put(s
, LONG_TO_PTR(pid
));
361 int cg_migrate_recursive(
369 _cleanup_closedir_
DIR *d
= NULL
;
378 ret
= cg_migrate(cfrom
, pfrom
, cto
, pto
, ignore_self
);
380 r
= cg_enumerate_subgroups(cfrom
, pfrom
, &d
);
382 if (ret
>= 0 && r
!= -ENOENT
)
388 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
389 _cleanup_free_
char *p
= NULL
;
391 p
= strjoin(pfrom
, "/", fn
, NULL
);
396 r
= cg_migrate_recursive(cfrom
, p
, cto
, pto
, ignore_self
, rem
);
397 if (r
!= 0 && ret
>= 0)
401 if (r
< 0 && ret
>= 0)
405 r
= cg_rmdir(cfrom
, pfrom
);
406 if (r
< 0 && ret
>= 0 && r
!= -ENOENT
&& r
!= -EBUSY
)
413 int cg_migrate_recursive_fallback(
428 r
= cg_migrate_recursive(cfrom
, pfrom
, cto
, pto
, ignore_self
, rem
);
430 char prefix
[strlen(pto
) + 1];
432 /* This didn't work? Then let's try all prefixes of the destination */
434 PATH_FOREACH_PREFIX(prefix
, pto
) {
437 q
= cg_migrate_recursive(cfrom
, pfrom
, cto
, prefix
, ignore_self
, rem
);
446 static const char *normalize_controller(const char *controller
) {
450 if (startswith(controller
, "name="))
451 return controller
+ 5;
456 static int join_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
459 if (!isempty(controller
)) {
460 if (!isempty(path
) && !isempty(suffix
))
461 t
= strjoin("/sys/fs/cgroup/", controller
, "/", path
, "/", suffix
, NULL
);
462 else if (!isempty(path
))
463 t
= strjoin("/sys/fs/cgroup/", controller
, "/", path
, NULL
);
464 else if (!isempty(suffix
))
465 t
= strjoin("/sys/fs/cgroup/", controller
, "/", suffix
, NULL
);
467 t
= strappend("/sys/fs/cgroup/", controller
);
469 if (!isempty(path
) && !isempty(suffix
))
470 t
= strjoin(path
, "/", suffix
, NULL
);
471 else if (!isempty(path
))
480 *fs
= path_kill_slashes(t
);
484 int cg_get_path(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
486 static thread_local
bool good
= false;
490 if (controller
&& !cg_controller_is_valid(controller
))
493 if (_unlikely_(!good
)) {
496 r
= path_is_mount_point("/sys/fs/cgroup", 0);
502 /* Cache this to save a few stat()s */
506 p
= controller
? normalize_controller(controller
) : NULL
;
508 return join_path(p
, path
, suffix
, fs
);
511 static int check_hierarchy(const char *p
) {
516 if (!filename_is_valid(p
))
519 /* Check if this controller actually really exists */
520 cc
= strjoina("/sys/fs/cgroup/", p
);
521 if (laccess(cc
, F_OK
) < 0)
527 int cg_get_path_and_check(const char *controller
, const char *path
, const char *suffix
, char **fs
) {
533 if (!cg_controller_is_valid(controller
))
536 /* Normalize the controller syntax */
537 p
= normalize_controller(controller
);
539 /* Check if this controller actually really exists */
540 r
= check_hierarchy(p
);
544 return join_path(p
, path
, suffix
, fs
);
547 static int trim_cb(const char *path
, const struct stat
*sb
, int typeflag
, struct FTW
*ftwbuf
) {
552 if (typeflag
!= FTW_DP
)
555 if (ftwbuf
->level
< 1)
562 int cg_trim(const char *controller
, const char *path
, bool delete_root
) {
563 _cleanup_free_
char *fs
= NULL
;
568 r
= cg_get_path(controller
, path
, NULL
, &fs
);
573 if (nftw(fs
, trim_cb
, 64, FTW_DEPTH
|FTW_MOUNT
|FTW_PHYS
) != 0) {
583 if (rmdir(fs
) < 0 && errno
!= ENOENT
)
590 int cg_delete(const char *controller
, const char *path
) {
591 _cleanup_free_
char *parent
= NULL
;
596 r
= path_get_parent(path
, &parent
);
600 r
= cg_migrate_recursive(controller
, path
, controller
, parent
, false, true);
601 return r
== -ENOENT
? 0 : r
;
604 int cg_create(const char *controller
, const char *path
) {
605 _cleanup_free_
char *fs
= NULL
;
608 r
= cg_get_path_and_check(controller
, path
, NULL
, &fs
);
612 r
= mkdir_parents(fs
, 0755);
616 if (mkdir(fs
, 0755) < 0) {
627 int cg_create_and_attach(const char *controller
, const char *path
, pid_t pid
) {
632 r
= cg_create(controller
, path
);
636 q
= cg_attach(controller
, path
, pid
);
640 /* This does not remove the cgroup on failure */
644 int cg_attach(const char *controller
, const char *path
, pid_t pid
) {
645 _cleanup_free_
char *fs
= NULL
;
646 char c
[DECIMAL_STR_MAX(pid_t
) + 2];
652 r
= cg_get_path_and_check(controller
, path
, "cgroup.procs", &fs
);
659 snprintf(c
, sizeof(c
), PID_FMT
"\n", pid
);
661 return write_string_file(fs
, c
, 0);
664 int cg_attach_fallback(const char *controller
, const char *path
, pid_t pid
) {
671 r
= cg_attach(controller
, path
, pid
);
673 char prefix
[strlen(path
) + 1];
675 /* This didn't work? Then let's try all prefixes of
678 PATH_FOREACH_PREFIX(prefix
, path
) {
681 q
= cg_attach(controller
, prefix
, pid
);
690 int cg_set_group_access(
691 const char *controller
,
697 _cleanup_free_
char *fs
= NULL
;
700 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
703 if (mode
!= MODE_INVALID
)
706 r
= cg_get_path(controller
, path
, NULL
, &fs
);
710 return chmod_and_chown(fs
, mode
, uid
, gid
);
713 int cg_set_task_access(
714 const char *controller
,
720 _cleanup_free_
char *fs
= NULL
, *procs
= NULL
;
725 if (mode
== MODE_INVALID
&& uid
== UID_INVALID
&& gid
== GID_INVALID
)
728 if (mode
!= MODE_INVALID
)
731 r
= cg_get_path(controller
, path
, "cgroup.procs", &fs
);
735 r
= chmod_and_chown(fs
, mode
, uid
, gid
);
739 /* Compatibility, Always keep values for "tasks" in sync with
741 r
= cg_get_path(controller
, path
, "tasks", &procs
);
745 return chmod_and_chown(procs
, mode
, uid
, gid
);
748 int cg_pid_get_path(const char *controller
, pid_t pid
, char **path
) {
749 _cleanup_fclose_
FILE *f
= NULL
;
758 if (!cg_controller_is_valid(controller
))
761 controller
= normalize_controller(controller
);
763 controller
= SYSTEMD_CGROUP_CONTROLLER
;
765 fs
= procfs_file_alloca(pid
, "cgroup");
769 return errno
== ENOENT
? -ESRCH
: -errno
;
771 cs
= strlen(controller
);
773 FOREACH_LINE(line
, f
, return -errno
) {
776 const char *word
, *state
;
781 l
= strchr(line
, ':');
792 FOREACH_WORD_SEPARATOR(word
, k
, l
, ",", state
) {
794 if (k
== cs
&& memcmp(word
, controller
, cs
) == 0) {
800 memcmp(word
, "name=", 5) == 0 &&
801 memcmp(word
+5, controller
, cs
) == 0) {
821 int cg_install_release_agent(const char *controller
, const char *agent
) {
822 _cleanup_free_
char *fs
= NULL
, *contents
= NULL
;
828 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
832 r
= read_one_line_file(fs
, &contents
);
836 sc
= strstrip(contents
);
838 r
= write_string_file(fs
, agent
, 0);
841 } else if (!streq(sc
, agent
))
845 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
849 contents
= mfree(contents
);
850 r
= read_one_line_file(fs
, &contents
);
854 sc
= strstrip(contents
);
855 if (streq(sc
, "0")) {
856 r
= write_string_file(fs
, "1", 0);
869 int cg_uninstall_release_agent(const char *controller
) {
870 _cleanup_free_
char *fs
= NULL
;
873 r
= cg_get_path(controller
, NULL
, "notify_on_release", &fs
);
877 r
= write_string_file(fs
, "0", 0);
883 r
= cg_get_path(controller
, NULL
, "release_agent", &fs
);
887 r
= write_string_file(fs
, "", 0);
894 int cg_is_empty(const char *controller
, const char *path
, bool ignore_self
) {
895 _cleanup_fclose_
FILE *f
= NULL
;
896 pid_t pid
= 0, self_pid
;
902 r
= cg_enumerate_processes(controller
, path
, &f
);
904 return r
== -ENOENT
? 1 : r
;
908 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
910 if (ignore_self
&& pid
== self_pid
)
923 int cg_is_empty_recursive(const char *controller
, const char *path
, bool ignore_self
) {
924 _cleanup_closedir_
DIR *d
= NULL
;
930 r
= cg_is_empty(controller
, path
, ignore_self
);
934 r
= cg_enumerate_subgroups(controller
, path
, &d
);
936 return r
== -ENOENT
? 1 : r
;
938 while ((r
= cg_read_subgroup(d
, &fn
)) > 0) {
939 _cleanup_free_
char *p
= NULL
;
941 p
= strjoin(path
, "/", fn
, NULL
);
946 r
= cg_is_empty_recursive(controller
, p
, ignore_self
);
957 int cg_split_spec(const char *spec
, char **controller
, char **path
) {
959 char *t
= NULL
, *u
= NULL
;
960 _cleanup_free_
char *v
= NULL
;
965 if (!path_is_safe(spec
))
973 *path
= path_kill_slashes(t
);
982 e
= strchr(spec
, ':');
984 if (!cg_controller_is_valid(spec
))
988 t
= strdup(normalize_controller(spec
));
1001 v
= strndup(spec
, e
-spec
);
1004 t
= strdup(normalize_controller(v
));
1007 if (!cg_controller_is_valid(t
)) {
1012 if (streq(e
+1, "")) {
1025 if (!path_is_safe(u
) ||
1026 !path_is_absolute(u
)) {
1032 path_kill_slashes(u
);
1048 int cg_mangle_path(const char *path
, char **result
) {
1049 _cleanup_free_
char *c
= NULL
, *p
= NULL
;
1056 /* First, check if it already is a filesystem path */
1057 if (path_startswith(path
, "/sys/fs/cgroup")) {
1063 *result
= path_kill_slashes(t
);
1067 /* Otherwise, treat it as cg spec */
1068 r
= cg_split_spec(path
, &c
, &p
);
1072 return cg_get_path(c
? c
: SYSTEMD_CGROUP_CONTROLLER
, p
? p
: "/", NULL
, result
);
1075 int cg_get_root_path(char **path
) {
1081 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 1, &p
);
1085 e
= endswith(p
, "/" SPECIAL_SYSTEM_SLICE
);
1093 int cg_shift_path(const char *cgroup
, const char *root
, const char **shifted
) {
1094 _cleanup_free_
char *rt
= NULL
;
1102 /* If the root was specified let's use that, otherwise
1103 * let's determine it from PID 1 */
1105 r
= cg_get_root_path(&rt
);
1112 p
= path_startswith(cgroup
, root
);
1121 int cg_pid_get_path_shifted(pid_t pid
, const char *root
, char **cgroup
) {
1122 _cleanup_free_
char *raw
= NULL
;
1129 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &raw
);
1133 r
= cg_shift_path(raw
, root
, &c
);
1153 int cg_path_decode_unit(const char *cgroup
, char **unit
){
1160 n
= strcspn(cgroup
, "/");
1164 c
= strndupa(cgroup
, n
);
1167 if (!unit_name_is_valid(c
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
1178 static bool valid_slice_name(const char *p
, size_t n
) {
1183 if (n
< strlen("x.slice"))
1186 if (memcmp(p
+ n
- 6, ".slice", 6) == 0) {
1192 c
= cg_unescape(buf
);
1194 return unit_name_is_valid(c
, UNIT_NAME_PLAIN
);
1200 static const char *skip_slices(const char *p
) {
1203 /* Skips over all slice assignments */
1208 p
+= strspn(p
, "/");
1210 n
= strcspn(p
, "/");
1211 if (!valid_slice_name(p
, n
))
1218 int cg_path_get_unit(const char *path
, char **ret
) {
1226 e
= skip_slices(path
);
1228 r
= cg_path_decode_unit(e
, &unit
);
1232 /* We skipped over the slices, don't accept any now */
1233 if (endswith(unit
, ".slice")) {
1242 int cg_pid_get_unit(pid_t pid
, char **unit
) {
1243 _cleanup_free_
char *cgroup
= NULL
;
1248 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1252 return cg_path_get_unit(cgroup
, unit
);
1256 * Skip session-*.scope, but require it to be there.
1258 static const char *skip_session(const char *p
) {
1264 p
+= strspn(p
, "/");
1266 n
= strcspn(p
, "/");
1267 if (n
< strlen("session-x.scope"))
1270 if (memcmp(p
, "session-", 8) == 0 && memcmp(p
+ n
- 6, ".scope", 6) == 0) {
1271 char buf
[n
- 8 - 6 + 1];
1273 memcpy(buf
, p
+ 8, n
- 8 - 6);
1276 /* Note that session scopes never need unescaping,
1277 * since they cannot conflict with the kernel's own
1278 * names, hence we don't need to call cg_unescape()
1281 if (!session_id_valid(buf
))
1285 p
+= strspn(p
, "/");
1293 * Skip user@*.service, but require it to be there.
1295 static const char *skip_user_manager(const char *p
) {
1301 p
+= strspn(p
, "/");
1303 n
= strcspn(p
, "/");
1304 if (n
< strlen("user@x.service"))
1307 if (memcmp(p
, "user@", 5) == 0 && memcmp(p
+ n
- 8, ".service", 8) == 0) {
1308 char buf
[n
- 5 - 8 + 1];
1310 memcpy(buf
, p
+ 5, n
- 5 - 8);
1313 /* Note that user manager services never need unescaping,
1314 * since they cannot conflict with the kernel's own
1315 * names, hence we don't need to call cg_unescape()
1318 if (parse_uid(buf
, NULL
) < 0)
1322 p
+= strspn(p
, "/");
1330 static const char *skip_user_prefix(const char *path
) {
1335 /* Skip slices, if there are any */
1336 e
= skip_slices(path
);
1338 /* Skip the user manager, if it's in the path now... */
1339 t
= skip_user_manager(e
);
1343 /* Alternatively skip the user session if it is in the path... */
1344 return skip_session(e
);
1347 int cg_path_get_user_unit(const char *path
, char **ret
) {
1353 t
= skip_user_prefix(path
);
1357 /* And from here on it looks pretty much the same as for a
1358 * system unit, hence let's use the same parser from here
1360 return cg_path_get_unit(t
, ret
);
1363 int cg_pid_get_user_unit(pid_t pid
, char **unit
) {
1364 _cleanup_free_
char *cgroup
= NULL
;
1369 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1373 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 int cg_pid_get_machine_name(pid_t pid
, char **machine
) {
1392 _cleanup_free_
char *cgroup
= NULL
;
1397 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1401 return cg_path_get_machine_name(cgroup
, machine
);
1404 int cg_path_get_session(const char *path
, char **session
) {
1405 _cleanup_free_
char *unit
= NULL
;
1411 r
= cg_path_get_unit(path
, &unit
);
1415 start
= startswith(unit
, "session-");
1418 end
= endswith(start
, ".scope");
1423 if (!session_id_valid(start
))
1439 int cg_pid_get_session(pid_t pid
, char **session
) {
1440 _cleanup_free_
char *cgroup
= NULL
;
1443 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1447 return cg_path_get_session(cgroup
, session
);
1450 int cg_path_get_owner_uid(const char *path
, uid_t
*uid
) {
1451 _cleanup_free_
char *slice
= NULL
;
1457 r
= cg_path_get_slice(path
, &slice
);
1461 start
= startswith(slice
, "user-");
1464 end
= endswith(start
, ".slice");
1469 if (parse_uid(start
, uid
) < 0)
1475 int cg_pid_get_owner_uid(pid_t pid
, uid_t
*uid
) {
1476 _cleanup_free_
char *cgroup
= NULL
;
1479 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1483 return cg_path_get_owner_uid(cgroup
, uid
);
1486 int cg_path_get_slice(const char *p
, char **slice
) {
1487 const char *e
= NULL
;
1492 /* Finds the right-most slice unit from the beginning, but
1493 * stops before we come to the first non-slice unit. */
1498 p
+= strspn(p
, "/");
1500 n
= strcspn(p
, "/");
1501 if (!valid_slice_name(p
, n
)) {
1506 s
= strdup("-.slice");
1514 return cg_path_decode_unit(e
, slice
);
1522 int cg_pid_get_slice(pid_t pid
, char **slice
) {
1523 _cleanup_free_
char *cgroup
= NULL
;
1528 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1532 return cg_path_get_slice(cgroup
, slice
);
1535 int cg_path_get_user_slice(const char *p
, char **slice
) {
1540 t
= skip_user_prefix(p
);
1544 /* And now it looks pretty much the same as for a system
1545 * slice, so let's just use the same parser from here on. */
1546 return cg_path_get_slice(t
, slice
);
1549 int cg_pid_get_user_slice(pid_t pid
, char **slice
) {
1550 _cleanup_free_
char *cgroup
= NULL
;
1555 r
= cg_pid_get_path_shifted(pid
, NULL
, &cgroup
);
1559 return cg_path_get_user_slice(cgroup
, slice
);
1562 char *cg_escape(const char *p
) {
1563 bool need_prefix
= false;
1565 /* This implements very minimal escaping for names to be used
1566 * as file names in the cgroup tree: any name which might
1567 * conflict with a kernel name or is prefixed with '_' is
1568 * prefixed with a '_'. That way, when reading cgroup names it
1569 * is sufficient to remove a single prefixing underscore if
1572 /* The return value of this function (unlike cg_unescape())
1578 streq(p
, "notify_on_release") ||
1579 streq(p
, "release_agent") ||
1585 dot
= strrchr(p
, '.');
1588 if (dot
- p
== 6 && memcmp(p
, "cgroup", 6) == 0)
1593 n
= strndupa(p
, dot
- p
);
1595 if (check_hierarchy(n
) >= 0)
1602 return strappend("_", p
);
1607 char *cg_unescape(const char *p
) {
1610 /* The return value of this function (unlike cg_escape())
1611 * doesn't need free()! */
1619 #define CONTROLLER_VALID \
1623 bool cg_controller_is_valid(const char *p
) {
1629 s
= startswith(p
, "name=");
1633 if (*p
== 0 || *p
== '_')
1636 for (t
= p
; *t
; t
++)
1637 if (!strchr(CONTROLLER_VALID
, *t
))
1640 if (t
- p
> FILENAME_MAX
)
1646 int cg_slice_to_path(const char *unit
, char **ret
) {
1647 _cleanup_free_
char *p
= NULL
, *s
= NULL
, *e
= NULL
;
1654 if (streq(unit
, "-.slice")) {
1664 if (!unit_name_is_valid(unit
, UNIT_NAME_PLAIN
))
1667 if (!endswith(unit
, ".slice"))
1670 r
= unit_name_to_prefix(unit
, &p
);
1674 dash
= strchr(p
, '-');
1676 /* Don't allow initial dashes */
1681 _cleanup_free_
char *escaped
= NULL
;
1682 char n
[dash
- p
+ sizeof(".slice")];
1684 /* Don't allow trailing or double dashes */
1685 if (dash
[1] == 0 || dash
[1] == '-')
1688 strcpy(stpncpy(n
, p
, dash
- p
), ".slice");
1689 if (!unit_name_is_valid(n
, UNIT_NAME_PLAIN
))
1692 escaped
= cg_escape(n
);
1696 if (!strextend(&s
, escaped
, "/", NULL
))
1699 dash
= strchr(dash
+1, '-');
1702 e
= cg_escape(unit
);
1706 if (!strextend(&s
, e
, NULL
))
1715 int cg_set_attribute(const char *controller
, const char *path
, const char *attribute
, const char *value
) {
1716 _cleanup_free_
char *p
= NULL
;
1719 r
= cg_get_path(controller
, path
, attribute
, &p
);
1723 return write_string_file(p
, value
, 0);
1726 int cg_get_attribute(const char *controller
, const char *path
, const char *attribute
, char **ret
) {
1727 _cleanup_free_
char *p
= NULL
;
1730 r
= cg_get_path(controller
, path
, attribute
, &p
);
1734 return read_one_line_file(p
, ret
);
1737 static const char mask_names
[] =
1744 int cg_create_everywhere(CGroupControllerMask supported
, CGroupControllerMask mask
, const char *path
) {
1745 CGroupControllerMask bit
= 1;
1749 /* This one will create a cgroup in our private tree, but also
1750 * duplicate it in the trees specified in mask, and remove it
1753 /* First create the cgroup in our own hierarchy. */
1754 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1758 /* Then, do the same in the other hierarchies */
1759 NULSTR_FOREACH(n
, mask_names
) {
1762 else if (supported
& bit
)
1763 cg_trim(n
, path
, true);
1771 int cg_attach_everywhere(CGroupControllerMask supported
, const char *path
, pid_t pid
, cg_migrate_callback_t path_callback
, void *userdata
) {
1772 CGroupControllerMask bit
= 1;
1776 r
= cg_attach(SYSTEMD_CGROUP_CONTROLLER
, path
, pid
);
1780 NULSTR_FOREACH(n
, mask_names
) {
1782 if (supported
& bit
) {
1783 const char *p
= NULL
;
1786 p
= path_callback(bit
, userdata
);
1791 cg_attach_fallback(n
, p
, pid
);
1800 int cg_attach_many_everywhere(CGroupControllerMask supported
, const char *path
, Set
* pids
, cg_migrate_callback_t path_callback
, void *userdata
) {
1805 SET_FOREACH(pidp
, pids
, i
) {
1806 pid_t pid
= PTR_TO_LONG(pidp
);
1809 q
= cg_attach_everywhere(supported
, path
, pid
, path_callback
, userdata
);
1817 int cg_migrate_everywhere(CGroupControllerMask supported
, const char *from
, const char *to
, cg_migrate_callback_t to_callback
, void *userdata
) {
1818 CGroupControllerMask bit
= 1;
1822 if (!path_equal(from
, to
)) {
1823 r
= cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER
, from
, SYSTEMD_CGROUP_CONTROLLER
, to
, false, true);
1828 NULSTR_FOREACH(n
, mask_names
) {
1829 if (supported
& bit
) {
1830 const char *p
= NULL
;
1833 p
= to_callback(bit
, userdata
);
1838 cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER
, to
, n
, p
, false, false);
1847 int cg_trim_everywhere(CGroupControllerMask supported
, const char *path
, bool delete_root
) {
1848 CGroupControllerMask bit
= 1;
1852 r
= cg_trim(SYSTEMD_CGROUP_CONTROLLER
, path
, delete_root
);
1856 NULSTR_FOREACH(n
, mask_names
) {
1857 if (supported
& bit
)
1858 cg_trim(n
, path
, delete_root
);
1866 CGroupControllerMask
cg_mask_supported(void) {
1867 CGroupControllerMask bit
= 1, mask
= 0;
1870 NULSTR_FOREACH(n
, mask_names
) {
1871 if (check_hierarchy(n
) >= 0)
1880 int cg_kernel_controllers(Set
*controllers
) {
1881 _cleanup_fclose_
FILE *f
= NULL
;
1885 assert(controllers
);
1887 /* Determines the full list of kernel-known controllers. Might
1888 * include controllers we don't actually support, arbitrary
1889 * named hierarchies and controllers that aren't currently
1890 * accessible (because not mounted). */
1892 f
= fopen("/proc/cgroups", "re");
1894 if (errno
== ENOENT
)
1899 /* Ignore the header line */
1900 (void) fgets(buf
, sizeof(buf
), f
);
1907 if (fscanf(f
, "%ms %*i %*i %i", &controller
, &enabled
) != 2) {
1912 if (ferror(f
) && errno
!= 0)
1923 if (!filename_is_valid(controller
)) {
1928 r
= set_consume(controllers
, controller
);