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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
31 #include "cgroup-util.h"
37 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
46 if ((r = cg_get_path(controller, path, "cgroup.procs", &fs)) < 0)
59 int cg_enumerate_tasks(const char *controller, const char *path, FILE **_f) {
68 if ((r = cg_get_path(controller, path, "tasks", &fs)) < 0)
81 int cg_read_pid(FILE *f, pid_t *_pid) {
84 /* Note that the cgroup.procs might contain duplicates! See
85 * cgroups.txt for details. */
88 if (fscanf(f, "%lu", &ul) != 1) {
93 return errno ? -errno : -EIO;
103 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
112 /* This is not recursive! */
114 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
127 int cg_read_subgroup(DIR *d, char **fn) {
133 while ((de = readdir(d))) {
136 if (de->d_type != DT_DIR)
139 if (streq(de->d_name, ".") ||
140 streq(de->d_name, ".."))
143 if (!(b = strdup(de->d_name)))
156 int cg_rmdir(const char *controller, const char *path) {
160 if ((r = cg_get_path(controller, path, NULL, &p)) < 0)
166 return r < 0 ? -errno : 0;
169 int cg_kill(const char *controller, const char *path, int sig, bool ignore_self, Set *s) {
174 Set *allocated_set = NULL;
180 /* This goes through the tasks list and kills them all. This
181 * is repeated until no further processes are added to the
182 * tasks list, to properly handle forking processes */
185 if (!(s = allocated_set = set_new(trivial_hash_func, trivial_compare_func)))
194 if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
195 if (ret >= 0 && r != -ENOENT)
201 while ((r = cg_read_pid(f, &pid)) > 0) {
203 if (pid == my_pid && ignore_self)
206 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
209 /* If we haven't killed this process yet, kill
211 if (kill(pid, sig) < 0) {
212 if (ret >= 0 && errno != ESRCH)
219 if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
237 /* To avoid racing against processes which fork
238 * quicker than we can kill them we repeat this until
239 * no new pids need to be killed. */
245 set_free(allocated_set);
253 int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self, bool rem, Set *s) {
257 Set *allocated_set = NULL;
264 if (!(s = allocated_set = set_new(trivial_hash_func, trivial_compare_func)))
267 ret = cg_kill(controller, path, sig, ignore_self, s);
269 if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
270 if (ret >= 0 && r != -ENOENT)
276 while ((r = cg_read_subgroup(d, &fn)) > 0) {
279 r = asprintf(&p, "%s/%s", path, fn);
289 r = cg_kill_recursive(controller, p, sig, ignore_self, rem, s);
292 if (r != 0 && ret >= 0)
296 if (r < 0 && ret >= 0)
300 if ((r = cg_rmdir(controller, path)) < 0) {
301 if (ret >= 0 && r != -ENOENT)
310 set_free(allocated_set);
315 int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) {
321 /* This safely kills all processes; first it sends a SIGTERM,
322 * then checks 8 times after 50ms whether the group is
323 * now empty, and finally kills everything that is left with
326 for (i = 0; i < 10; i++) {
336 if ((r = cg_kill_recursive(controller, path, sig, true, rem, NULL)) <= 0)
339 usleep(50 * USEC_PER_MSEC);
345 int cg_migrate(const char *controller, const char *from, const char *to, bool ignore_self) {
356 if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
365 if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
366 if (ret >= 0 && r != -ENOENT)
372 while ((r = cg_read_pid(f, &pid)) > 0) {
374 /* This might do weird stuff if we aren't a
375 * single-threaded program. However, we
376 * luckily know we are not */
377 if (pid == my_pid && ignore_self)
380 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
383 if ((r = cg_attach(controller, to, pid)) < 0) {
384 if (ret >= 0 && r != -ESRCH)
391 if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
420 int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self, bool rem) {
429 ret = cg_migrate(controller, from, to, ignore_self);
431 if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) {
432 if (ret >= 0 && r != -ENOENT)
437 while ((r = cg_read_subgroup(d, &fn)) > 0) {
440 r = asprintf(&p, "%s/%s", from, fn);
450 r = cg_migrate_recursive(controller, p, to, ignore_self, rem);
453 if (r != 0 && ret >= 0)
457 if (r < 0 && ret >= 0)
461 if ((r = cg_rmdir(controller, from)) < 0) {
462 if (ret >= 0 && r != -ENOENT)
473 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
481 /* This is a very minimal lookup from controller names to
482 * paths. Since we have mounted most hierarchies ourselves
483 * should be kinda safe, but eventually we might want to
484 * extend this to have a fallback to actually check
485 * /proc/mounts. Might need caching then. */
487 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
489 else if (startswith(controller, "name="))
494 if (asprintf(&mp, "/sys/fs/cgroup/%s", p) < 0)
497 if ((r = path_is_mount_point(mp)) <= 0) {
499 return r < 0 ? r : -ENOENT;
503 r = asprintf(fs, "%s/%s/%s", mp, path, suffix);
505 r = asprintf(fs, "%s/%s", mp, path);
507 r = asprintf(fs, "%s/%s", mp, suffix);
509 path_kill_slashes(mp);
515 path_kill_slashes(*fs);
516 return r < 0 ? -ENOMEM : 0;
519 int cg_trim(const char *controller, const char *path, bool delete_root) {
526 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
529 r = rm_rf(fs, true, delete_root);
532 return r == -ENOENT ? 0 : r;
535 int cg_delete(const char *controller, const char *path) {
542 if ((r = parent_of_path(path, &parent)) < 0)
545 r = cg_migrate_recursive(controller, path, parent, false, true);
548 return r == -ENOENT ? 0 : r;
551 int cg_create(const char *controller, const char *path) {
558 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
561 r = mkdir_parents(fs, 0755);
564 if (mkdir(fs, 0755) >= 0)
566 else if (errno == EEXIST)
577 int cg_attach(const char *controller, const char *path, pid_t pid) {
586 if ((r = cg_get_path(controller, path, "tasks", &fs)) < 0)
592 snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
595 r = write_one_line_file(fs, c);
601 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
608 if ((r = cg_create(controller, path)) < 0)
611 if ((q = cg_attach(controller, path, pid)) < 0)
614 /* This does not remove the cgroup on failure */
619 int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
626 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
629 r = chmod_and_chown(fs, mode, uid, gid);
635 int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
642 if ((r = cg_get_path(controller, path, "tasks", &fs)) < 0)
645 r = chmod_and_chown(fs, mode, uid, gid);
651 int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
665 if (asprintf(&fs, "/proc/%lu/cgroup", (unsigned long) pid) < 0)
672 return errno == ENOENT ? -ESRCH : -errno;
674 cs = strlen(controller);
681 if (!(fgets(line, sizeof(line), f))) {
685 r = errno ? -errno : -EIO;
691 if (!(l = strchr(line, ':')))
695 if (strncmp(l, controller, cs) != 0)
701 if (!(p = strdup(l + cs + 1))) {
719 int cg_install_release_agent(const char *controller, const char *agent) {
720 char *fs = NULL, *contents = NULL, *line = NULL, *sc;
726 if ((r = cg_get_path(controller, NULL, "release_agent", &fs)) < 0)
729 if ((r = read_one_line_file(fs, &contents)) < 0)
732 sc = strstrip(contents);
735 if (asprintf(&line, "%s\n", agent) < 0) {
740 if ((r = write_one_line_file(fs, line)) < 0)
743 } else if (!streq(sc, agent)) {
750 if ((r = cg_get_path(controller, NULL, "notify_on_release", &fs)) < 0)
755 if ((r = read_one_line_file(fs, &contents)) < 0)
758 sc = strstrip(contents);
760 if (streq(sc, "0")) {
761 if ((r = write_one_line_file(fs, "1\n")) < 0)
765 } else if (!streq(sc, "1")) {
779 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
788 if ((r = cg_enumerate_tasks(controller, path, &f)) < 0)
789 return r == -ENOENT ? 1 : r;
791 while ((r = cg_read_pid(f, &pid)) > 0) {
793 if (ignore_self && pid == getpid())
808 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
816 if ((r = cg_is_empty(controller, path, ignore_self)) <= 0)
819 if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0)
820 return r == -ENOENT ? 1 : r;
822 while ((r = cg_read_subgroup(d, &fn)) > 0) {
825 r = asprintf(&p, "%s/%s", path, fn);
833 r = cg_is_empty_recursive(controller, p, ignore_self);
851 int cg_split_spec(const char *spec, char **controller, char **path) {
853 char *t = NULL, *u = NULL;
856 assert(controller || path);
861 if (!(t = strdup(spec)))
873 if (!(e = strchr(spec, ':'))) {
875 if (strchr(spec, '/') || spec[0] == 0)
879 if (!(t = strdup(spec)))
893 memchr(spec, '/', e-spec))
897 if (!(t = strndup(spec, e-spec)))
901 if (!(u = strdup(e+1))) {
915 int cg_join_spec(const char *controller, const char *path, char **spec) {
919 if (!path_is_absolute(path) ||
920 controller[0] == 0 ||
921 strchr(controller, ':') ||
922 strchr(controller, '/'))
925 if (asprintf(spec, "%s:%s", controller, path) < 0)
931 int cg_fix_path(const char *path, char **result) {
938 /* First check if it already is a filesystem path */
939 if (path_is_absolute(path) &&
940 path_startswith(path, "/sys/fs/cgroup") &&
941 access(path, F_OK) >= 0) {
943 if (!(t = strdup(path)))
950 /* Otherwise treat it as cg spec */
951 if ((r = cg_split_spec(path, &c, &p)) < 0)
954 r = cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);