1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 "cgroup-util.h"
35 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
44 if ((r = cg_get_path(controller, path, "cgroup.procs", &fs)) < 0)
57 int cg_enumerate_tasks(const char *controller, const char *path, FILE **_f) {
66 if ((r = cg_get_path(controller, path, "tasks", &fs)) < 0)
79 int cg_read_pid(FILE *f, pid_t *_pid) {
82 /* Note that the cgroup.procs might contain duplicates! See
83 * cgroups.txt for details. */
86 if (fscanf(f, "%lu", &ul) != 1) {
91 return errno ? -errno : -EIO;
101 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
110 /* This is not recursive! */
112 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
125 int cg_read_subgroup(DIR *d, char **fn) {
131 while ((de = readdir(d))) {
134 if (de->d_type != DT_DIR)
137 if (streq(de->d_name, ".") ||
138 streq(de->d_name, ".."))
141 if (!(b = strdup(de->d_name)))
154 int cg_rmdir(const char *controller, const char *path) {
158 if ((r = cg_get_path(controller, path, NULL, &p)) < 0)
164 return r < 0 ? -errno : 0;
167 int cg_kill(const char *controller, const char *path, int sig, bool ignore_self) {
178 /* This goes through the tasks list and kills them all. This
179 * is repeated until no further processes are added to the
180 * tasks list, to properly handle forking processes */
182 if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
191 if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
192 if (ret >= 0 && r != -ENOENT)
198 while ((r = cg_read_pid(f, &pid)) > 0) {
200 if (pid == my_pid && ignore_self)
203 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
206 /* If we haven't killed this process yet, kill
208 if (kill(pid, sig) < 0) {
209 if (ret >= 0 && errno != ESRCH)
216 if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
234 /* To avoid racing against processes which fork
235 * quicker than we can kill them we repeat this until
236 * no new pids need to be killed. */
249 int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self, bool rem) {
258 ret = cg_kill(controller, path, sig, ignore_self);
260 if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
261 if (ret >= 0 && r != -ENOENT)
267 while ((r = cg_read_subgroup(d, &fn)) > 0) {
270 r = asprintf(&p, "%s/%s", path, fn);
280 r = cg_kill_recursive(controller, p, sig, ignore_self, rem);
283 if (r != 0 && ret >= 0)
287 if (r < 0 && ret >= 0)
291 if ((r = cg_rmdir(controller, path)) < 0) {
292 if (ret >= 0 && r != -ENOENT)
303 int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) {
309 /* This safely kills all processes; first it sends a SIGTERM,
310 * then checks 8 times after 50ms whether the group is
311 * now empty, and finally kills everything that is left with
314 for (i = 0; i < 10; i++) {
324 if ((r = cg_kill_recursive(controller, path, sig, true, rem)) <= 0)
327 usleep(50 * USEC_PER_MSEC);
333 int cg_migrate(const char *controller, const char *from, const char *to, bool ignore_self) {
344 if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
353 if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
354 if (ret >= 0 && r != -ENOENT)
360 while ((r = cg_read_pid(f, &pid)) > 0) {
362 /* This might do weird stuff if we aren't a
363 * single-threaded program. However, we
364 * luckily know we are not */
365 if (pid == my_pid && ignore_self)
368 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
371 if ((r = cg_attach(controller, to, pid)) < 0) {
372 if (ret >= 0 && r != -ESRCH)
379 if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
408 int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self, bool rem) {
417 ret = cg_migrate(controller, from, to, ignore_self);
419 if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) {
420 if (ret >= 0 && r != -ENOENT)
425 while ((r = cg_read_subgroup(d, &fn)) > 0) {
428 r = asprintf(&p, "%s/%s", from, fn);
438 r = cg_migrate_recursive(controller, p, to, ignore_self, rem);
441 if (r != 0 && ret >= 0)
445 if (r < 0 && ret >= 0)
449 if ((r = cg_rmdir(controller, from)) < 0) {
450 if (ret >= 0 && r != -ENOENT)
461 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
469 /* This is a very minimal lookup from controller names to
470 * paths. Since we have mounted most hierarchies ourselves
471 * should be kinda safe, but eventually we might want to
472 * extend this to have a fallback to actually check
473 * /proc/mounts. Might need caching then. */
475 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
477 else if (startswith(controller, "name="))
482 if (asprintf(&mp, "/cgroup/%s", p) < 0)
485 if ((r = path_is_mount_point(mp)) <= 0) {
487 return r < 0 ? r : -ENOENT;
491 r = asprintf(fs, "%s/%s/%s", mp, path, suffix);
493 r = asprintf(fs, "%s/%s", mp, path);
495 r = asprintf(fs, "%s/%s", mp, suffix);
497 path_kill_slashes(mp);
503 path_kill_slashes(*fs);
504 return r < 0 ? -ENOMEM : 0;
507 int cg_trim(const char *controller, const char *path, bool delete_root) {
514 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
517 r = rm_rf(fs, true, delete_root);
520 return r == -ENOENT ? 0 : r;
523 int cg_delete(const char *controller, const char *path) {
530 if ((r = parent_of_path(path, &parent)) < 0)
533 r = cg_migrate_recursive(controller, path, parent, false, true);
536 return r == -ENOENT ? 0 : r;
539 int cg_create(const char *controller, const char *path) {
546 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
549 r = mkdir_p(fs, 0755);
555 int cg_attach(const char *controller, const char *path, pid_t pid) {
564 if ((r = cg_get_path(controller, path, "tasks", &fs)) < 0)
570 snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
573 r = write_one_line_file(fs, c);
579 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
586 if ((r = cg_create(controller, path)) < 0)
589 if ((r = cg_attach(controller, path, pid)) < 0)
592 /* This does not remove the cgroup on failure */
597 int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
604 if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
607 r = chmod_and_chown(fs, mode, uid, gid);
613 int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
620 if ((r = cg_get_path(controller, path, "tasks", &fs)) < 0)
623 r = chmod_and_chown(fs, mode, uid, gid);
629 int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
643 if (asprintf(&fs, "/proc/%lu/cgroup", (unsigned long) pid) < 0)
650 return errno == ENOENT ? -ESRCH : -errno;
652 cs = strlen(controller);
659 if (!(fgets(line, sizeof(line), f))) {
663 r = errno ? -errno : -EIO;
669 if (!(l = strchr(line, ':')))
673 if (strncmp(l, controller, cs) != 0)
679 if (!(p = strdup(l + cs + 1))) {
697 int cg_install_release_agent(const char *controller, const char *agent) {
698 char *fs = NULL, *contents = NULL, *line = NULL, *sc;
704 if ((r = cg_get_path(controller, NULL, "release_agent", &fs)) < 0)
707 if ((r = read_one_line_file(fs, &contents)) < 0)
710 sc = strstrip(contents);
713 if (asprintf(&line, "%s\n", agent) < 0) {
718 if ((r = write_one_line_file(fs, line)) < 0)
721 } else if (!streq(sc, agent)) {
728 if ((r = cg_get_path(controller, NULL, "notify_on_release", &fs)) < 0) {
735 if ((r = read_one_line_file(fs, &contents)) < 0)
738 sc = strstrip(contents);
740 if (streq(sc, "0")) {
741 if ((r = write_one_line_file(fs, "1\n")) < 0)
745 } else if (!streq(sc, "1")) {
759 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
768 if ((r = cg_enumerate_tasks(controller, path, &f)) < 0)
769 return r == -ENOENT ? 1 : r;
771 while ((r = cg_read_pid(f, &pid)) > 0) {
773 if (ignore_self && pid == getpid())
788 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
796 if ((r = cg_is_empty(controller, path, ignore_self)) <= 0)
799 if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0)
800 return r == -ENOENT ? 1 : r;
802 while ((r = cg_read_subgroup(d, &fn)) > 0) {
805 r = asprintf(&p, "%s/%s", path, fn);
813 r = cg_is_empty_recursive(controller, p, ignore_self);
831 int cg_split_spec(const char *spec, char **controller, char **path) {
833 char *t = NULL, *u = NULL;
836 assert(controller || path);
841 if (!(t = strdup(spec)))
853 if (!(e = strchr(spec, ':'))) {
855 if (strchr(spec, '/') || spec[0] == 0)
859 if (!(t = strdup(spec)))
873 memchr(spec, '/', e-spec))
877 if (!(t = strndup(spec, e-spec)))
881 if (!(u = strdup(e+1))) {
895 int cg_join_spec(const char *controller, const char *path, char **spec) {
899 if (!path_is_absolute(path) ||
900 controller[0] == 0 ||
901 strchr(controller, ':') ||
902 strchr(controller, '/'))
905 if (asprintf(spec, "%s:%s", controller, path) < 0)
911 int cg_fix_path(const char *path, char **result) {
918 /* First check if it already is a filesystem path */
919 if (path_is_absolute(path) &&
920 path_startswith(path, "/cgroup") &&
921 access(path, F_OK) >= 0) {
923 if (!(t = strdup(path)))
930 /* Otherwise treat it as cg spec */
931 if ((r = cg_split_spec(path, &c, &p)) < 0)
934 r = cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);