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"
37 #include "path-util.h"
40 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
48 r = cg_get_path(controller, path, "cgroup.procs", &fs);
62 int cg_enumerate_tasks(const char *controller, const char *path, FILE **_f) {
70 r = cg_get_path(controller, path, "tasks", &fs);
84 int cg_read_pid(FILE *f, pid_t *_pid) {
87 /* Note that the cgroup.procs might contain duplicates! See
88 * cgroups.txt for details. */
91 if (fscanf(f, "%lu", &ul) != 1) {
96 return errno ? -errno : -EIO;
106 int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
114 /* This is not recursive! */
116 r = cg_get_path(controller, path, NULL, &fs);
130 int cg_read_subgroup(DIR *d, char **fn) {
136 while ((de = readdir(d))) {
139 if (de->d_type != DT_DIR)
142 if (streq(de->d_name, ".") ||
143 streq(de->d_name, ".."))
146 if (!(b = strdup(de->d_name)))
159 int cg_rmdir(const char *controller, const char *path, bool honour_sticky) {
163 r = cg_get_path(controller, path, NULL, &p);
170 /* If the sticky bit is set don't remove the directory */
172 tasks = strappend(p, "/tasks");
178 r = file_is_priv_sticky(tasks);
190 return (r < 0 && errno != ENOENT) ? -errno : 0;
193 int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
198 Set *allocated_set = NULL;
204 /* This goes through the tasks list and kills them all. This
205 * is repeated until no further processes are added to the
206 * tasks list, to properly handle forking processes */
209 if (!(s = allocated_set = set_new(trivial_hash_func, trivial_compare_func)))
218 if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
219 if (ret >= 0 && r != -ENOENT)
225 while ((r = cg_read_pid(f, &pid)) > 0) {
227 if (pid == my_pid && ignore_self)
230 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
233 /* If we haven't killed this process yet, kill
235 if (kill(pid, sig) < 0) {
236 if (ret >= 0 && errno != ESRCH)
238 } else if (ret == 0) {
248 if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
266 /* To avoid racing against processes which fork
267 * quicker than we can kill them we repeat this until
268 * no new pids need to be killed. */
274 set_free(allocated_set);
282 int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
286 Set *allocated_set = NULL;
293 if (!(s = allocated_set = set_new(trivial_hash_func, trivial_compare_func)))
296 ret = cg_kill(controller, path, sig, sigcont, ignore_self, s);
298 if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
299 if (ret >= 0 && r != -ENOENT)
305 while ((r = cg_read_subgroup(d, &fn)) > 0) {
308 r = asprintf(&p, "%s/%s", path, fn);
318 r = cg_kill_recursive(controller, p, sig, sigcont, ignore_self, rem, s);
321 if (r != 0 && ret >= 0)
325 if (r < 0 && ret >= 0)
329 if ((r = cg_rmdir(controller, path, true)) < 0) {
341 set_free(allocated_set);
346 int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) {
352 /* This safely kills all processes; first it sends a SIGTERM,
353 * then checks 8 times after 200ms whether the group is now
354 * empty, then kills everything that is left with SIGKILL and
355 * finally checks 5 times after 200ms each whether the group
356 * is finally empty. */
358 for (i = 0; i < 15; i++) {
368 if ((r = cg_kill_recursive(controller, path, sig, true, true, rem, NULL)) <= 0)
371 usleep(200 * USEC_PER_MSEC);
377 int cg_migrate(const char *controller, const char *from, const char *to, bool ignore_self) {
388 if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
397 if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
398 if (ret >= 0 && r != -ENOENT)
404 while ((r = cg_read_pid(f, &pid)) > 0) {
406 /* This might do weird stuff if we aren't a
407 * single-threaded program. However, we
408 * luckily know we are not */
409 if (pid == my_pid && ignore_self)
412 if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
415 if ((r = cg_attach(controller, to, pid)) < 0) {
416 if (ret >= 0 && r != -ESRCH)
423 if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
452 int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self, bool rem) {
461 ret = cg_migrate(controller, from, to, ignore_self);
463 if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) {
464 if (ret >= 0 && r != -ENOENT)
469 while ((r = cg_read_subgroup(d, &fn)) > 0) {
472 r = asprintf(&p, "%s/%s", from, fn);
482 r = cg_migrate_recursive(controller, p, to, ignore_self, rem);
485 if (r != 0 && ret >= 0)
489 if (r < 0 && ret >= 0)
493 if ((r = cg_rmdir(controller, from, true)) < 0) {
507 static const char *normalize_controller(const char *controller) {
509 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
511 else if (startswith(controller, "name="))
512 return controller + 5;
517 static int join_path(const char *controller, const char *path, const char *suffix, char **fs) {
520 if (!(controller || path))
525 t = join("/sys/fs/cgroup/", controller, "/", path, "/", suffix, NULL);
527 t = join("/sys/fs/cgroup/", controller, "/", path, NULL);
529 t = join("/sys/fs/cgroup/", controller, "/", suffix, NULL);
531 t = join("/sys/fs/cgroup/", controller, NULL);
534 t = join(path, "/", suffix, NULL);
542 path_kill_slashes(t);
548 int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) {
550 static __thread bool good = false;
554 if (_unlikely_(!good)) {
557 r = path_is_mount_point("/sys/fs/cgroup", false);
559 return r < 0 ? r : -ENOENT;
561 /* Cache this to save a few stat()s */
565 p = controller ? normalize_controller(controller) : NULL;
566 return join_path(p, path, suffix, fs);
569 static int check(const char *p) {
574 /* Check if this controller actually really exists */
575 cc = alloca(sizeof("/sys/fs/cgroup/") + strlen(p));
576 strcpy(stpcpy(cc, "/sys/fs/cgroup/"), p);
577 if (access(cc, F_OK) < 0)
583 int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
590 if (isempty(controller))
593 /* Normalize the controller syntax */
594 p = normalize_controller(controller);
596 /* Check if this controller actually really exists */
601 return join_path(p, path, suffix, fs);
604 static int trim_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
608 if (typeflag != FTW_DP)
611 if (ftwbuf->level < 1)
614 p = strappend(path, "/tasks");
620 is_sticky = file_is_priv_sticky(p) > 0;
630 int cg_trim(const char *controller, const char *path, bool delete_root) {
637 r = cg_get_path(controller, path, NULL, &fs);
642 if (nftw(fs, trim_cb, 64, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) < 0)
643 r = errno ? -errno : -EIO;
649 p = strappend(fs, "/tasks");
655 is_sticky = file_is_priv_sticky(p) > 0;
659 if (rmdir(fs) < 0 && errno != ENOENT) {
670 int cg_delete(const char *controller, const char *path) {
677 if ((r = path_get_parent(path, &parent)) < 0)
680 r = cg_migrate_recursive(controller, path, parent, false, true);
683 return r == -ENOENT ? 0 : r;
686 int cg_attach(const char *controller, const char *path, pid_t pid) {
695 r = cg_get_path_and_check(controller, path, "tasks", &fs);
702 snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
705 r = write_one_line_file(fs, c);
711 int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
718 if (mode != (mode_t) -1)
721 r = cg_get_path(controller, path, NULL, &fs);
725 r = chmod_and_chown(fs, mode, uid, gid);
731 int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid, int sticky) {
738 if (mode == (mode_t) -1 && uid == (uid_t) -1 && gid == (gid_t) -1 && sticky < 0)
741 if (mode != (mode_t) -1)
744 r = cg_get_path(controller, path, "tasks", &fs);
748 if (sticky >= 0 && mode != (mode_t) -1)
749 /* Both mode and sticky param are passed */
750 mode |= (sticky ? S_ISVTX : 0);
751 else if ((sticky >= 0 && mode == (mode_t) -1) ||
752 (mode != (mode_t) -1 && sticky < 0)) {
755 /* Only one param is passed, hence read the current
756 * mode from the file itself */
764 if (mode == (mode_t) -1)
765 /* No mode set, we just shall set the sticky bit */
766 mode = (st.st_mode & ~S_ISVTX) | (sticky ? S_ISVTX : 0);
768 /* Only mode set, leave sticky bit untouched */
769 mode = (st.st_mode & ~0777) | mode;
772 r = chmod_and_chown(fs, mode, uid, gid);
778 int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
792 if (asprintf(&fs, "/proc/%lu/cgroup", (unsigned long) pid) < 0)
799 return errno == ENOENT ? -ESRCH : -errno;
801 cs = strlen(controller);
808 if (!(fgets(line, sizeof(line), f))) {
812 r = errno ? -errno : -EIO;
818 if (!(l = strchr(line, ':')))
822 if (strncmp(l, controller, cs) != 0)
828 if (!(p = strdup(l + cs + 1))) {
846 int cg_install_release_agent(const char *controller, const char *agent) {
847 char *fs = NULL, *contents = NULL, *line = NULL, *sc;
853 if ((r = cg_get_path(controller, NULL, "release_agent", &fs)) < 0)
856 if ((r = read_one_line_file(fs, &contents)) < 0)
859 sc = strstrip(contents);
862 if (asprintf(&line, "%s\n", agent) < 0) {
867 if ((r = write_one_line_file(fs, line)) < 0)
870 } else if (!streq(sc, agent)) {
877 if ((r = cg_get_path(controller, NULL, "notify_on_release", &fs)) < 0)
882 if ((r = read_one_line_file(fs, &contents)) < 0)
885 sc = strstrip(contents);
887 if (streq(sc, "0")) {
888 if ((r = write_one_line_file(fs, "1\n")) < 0)
892 } else if (!streq(sc, "1")) {
906 int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
907 pid_t pid = 0, self_pid;
914 r = cg_enumerate_tasks(controller, path, &f);
916 return r == -ENOENT ? 1 : r;
920 while ((r = cg_read_pid(f, &pid)) > 0) {
922 if (ignore_self && pid == self_pid)
937 int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
944 r = cg_is_empty(controller, path, ignore_self);
948 r = cg_enumerate_subgroups(controller, path, &d);
950 return r == -ENOENT ? 1 : r;
952 while ((r = cg_read_subgroup(d, &fn)) > 0) {
955 r = asprintf(&p, "%s/%s", path, fn);
963 r = cg_is_empty_recursive(controller, p, ignore_self);
981 int cg_split_spec(const char *spec, char **controller, char **path) {
983 char *t = NULL, *u = NULL;
986 assert(controller || path);
991 if (!(t = strdup(spec)))
1003 if (!(e = strchr(spec, ':'))) {
1005 if (strchr(spec, '/') || spec[0] == 0)
1009 if (!(t = strdup(spec)))
1023 memchr(spec, '/', e-spec))
1027 if (!(t = strndup(spec, e-spec)))
1031 if (!(u = strdup(e+1))) {
1045 int cg_join_spec(const char *controller, const char *path, char **spec) {
1049 if (!path_is_absolute(path) ||
1050 controller[0] == 0 ||
1051 strchr(controller, ':') ||
1052 strchr(controller, '/'))
1055 if (asprintf(spec, "%s:%s", controller, path) < 0)
1061 int cg_fix_path(const char *path, char **result) {
1068 /* First check if it already is a filesystem path */
1069 if (path_startswith(path, "/sys/fs/cgroup") &&
1070 access(path, F_OK) >= 0) {
1080 /* Otherwise treat it as cg spec */
1081 r = cg_split_spec(path, &c, &p);
1085 r = cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
1092 int cg_get_user_path(char **path) {
1097 /* Figure out the place to put user cgroups below. We use the
1098 * same as PID 1 has but with the "/system" suffix replaced by
1101 if (cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root) < 0)
1102 p = strdup("/user");
1104 if (endswith(root, "/system"))
1105 root[strlen(root) - 7] = 0;
1106 else if (streq(root, "/"))
1109 p = strappend(root, "/user");
1120 char **cg_shorten_controllers(char **controllers) {
1123 controllers = strv_uniq(controllers);
1128 for (f = controllers, t = controllers; *f; f++) {
1132 if (streq(*f, "systemd") || streq(*f, SYSTEMD_CGROUP_CONTROLLER)) {
1137 p = normalize_controller(*f);
1141 log_debug("Controller %s is not available, removing from controllers list.", *f);