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/>.
25 #include <sys/types.h>
27 #include <sys/mount.h>
32 static int translate_error(int error, int _errno) {
36 case ECGROUPNOTCOMPILED:
37 case ECGROUPNOTMOUNTED:
39 case ECGROUPNOTCREATED:
45 case ECGROUPNOTALLOWED:
55 int cgroup_bonding_realize(CGroupBonding *b) {
60 assert(b->controller);
65 if (!(b->cgroup = cgroup_new_cgroup(b->path)))
68 if (!cgroup_add_controller(b->cgroup, b->controller)) {
74 r = cgroup_create_cgroup_from_parent(b->cgroup, true);
76 r = cgroup_create_cgroup(b->cgroup, true);
79 r = translate_error(r, errno);
86 cgroup_free(&b->cgroup);
91 int cgroup_bonding_realize_list(CGroupBonding *first) {
94 LIST_FOREACH(by_unit, b, first) {
97 if ((r = cgroup_bonding_realize(b)) < 0)
104 void cgroup_bonding_free(CGroupBonding *b) {
110 LIST_REMOVE(CGroupBonding, by_unit, b->unit->meta.cgroup_bondings, b);
112 assert_se(f = hashmap_get(b->unit->meta.manager->cgroup_bondings, b->path));
113 LIST_REMOVE(CGroupBonding, by_path, f, b);
116 hashmap_replace(b->unit->meta.manager->cgroup_bondings, b->path, f);
118 hashmap_remove(b->unit->meta.manager->cgroup_bondings, b->path);
126 if (b->only_us && b->clean_up)
127 cgroup_delete_cgroup(b->cgroup, true);
129 cgroup_free(&b->cgroup);
135 void cgroup_bonding_free_list(CGroupBonding *first) {
136 CGroupBonding *b, *n;
138 LIST_FOREACH_SAFE(by_unit, b, n, first)
139 cgroup_bonding_free(b);
142 int cgroup_bonding_install(CGroupBonding *b, pid_t pid) {
154 if ((r = cgroup_attach_task_pid(b->cgroup, pid)))
155 return translate_error(r, errno);
160 int cgroup_bonding_install_list(CGroupBonding *first, pid_t pid) {
163 LIST_FOREACH(by_unit, b, first) {
166 if ((r = cgroup_bonding_install(b, pid)) < 0)
173 int cgroup_bonding_kill(CGroupBonding *b, int sig) {
181 if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
190 if ((r = cgroup_get_task_begin(b->path, b->controller, &iterator, &pid)) != 0) {
195 r = translate_error(r, errno);
201 if (set_get(s, INT_TO_PTR(pid)) != INT_TO_PTR(pid)) {
203 /* If we haven't killed this process
206 if (kill(pid, sig) < 0 && errno != ESRCH) {
213 if ((r = set_put(s, INT_TO_PTR(pid))) < 0)
217 if ((r = cgroup_get_task_next(&iterator, &pid)) != 0) {
222 r = translate_error(r, errno);
229 assert_se(cgroup_get_task_end(&iterator) == 0);
231 /* To avoid racing against processes which fork
232 * quicker than we can kill them we repeat this until
233 * no new pids need to be killed. */
235 } while (!done && r >= 0);
241 int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
244 LIST_FOREACH(by_unit, b, first) {
247 if ((r = cgroup_bonding_kill(b, sig)) < 0)
254 /* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we
256 int cgroup_bonding_is_empty(CGroupBonding *b) {
263 r = cgroup_get_task_begin(b->path, b->controller, &iterator, &pid);
265 if (r == 0 || r == ECGEOF)
266 cgroup_get_task_end(&iterator);
268 /* Hmm, no PID in this group? Then it is definitely empty */
272 /* Some error? Let's return it */
274 return translate_error(r, errno);
276 /* It's not empty, and we are the only user, then it is
277 * definitely not empty */
281 /* There are PIDs in the group but we aren't the only users,
282 * hence we cannot say */
286 int cgroup_bonding_is_empty_list(CGroupBonding *first) {
289 LIST_FOREACH(by_unit, b, first) {
292 if ((r = cgroup_bonding_is_empty(b)) < 0) {
293 /* If this returned -EAGAIN, then we don't know if the
294 * group is empty, so let's see if another group can
306 static int install_release_agent(Manager *m, const char *mount_point) {
313 if (asprintf(&p, "%s/release_agent", mount_point) < 0)
316 if ((r = read_one_line_file(p, &c)) < 0) {
324 if ((r = write_one_line_file(p, CGROUP_AGENT_PATH "\n" )) < 0) {
329 } else if (!streq(sc, CGROUP_AGENT_PATH)) {
338 if (asprintf(&p, "%s/notify_on_release", mount_point) < 0)
341 if ((r = read_one_line_file(p, &c)) < 0) {
348 if (streq(sc, "0")) {
349 if ((r = write_one_line_file(p, "1\n")) < 0) {
354 } else if (!streq(sc, "1")) {
366 static int create_hierarchy_cgroup(Manager *m) {
372 if (!(cg = cgroup_new_cgroup(m->cgroup_hierarchy)))
375 if (!(cgroup_add_controller(cg, m->cgroup_controller))) {
380 if ((r = cgroup_create_cgroup(cg, true)) != 0) {
381 log_error("Failed to create cgroup hierarchy group: %s", cgroup_strerror(r));
382 r = translate_error(r, errno);
386 if ((r = cgroup_attach_task(cg)) != 0) {
387 log_error("Failed to add ourselves to hierarchy group: %s", cgroup_strerror(r));
388 r = translate_error(r, errno);
399 int manager_setup_cgroup(Manager *m) {
406 if ((r = cgroup_init()) != 0) {
407 log_error("Failed to initialize libcg: %s", cgroup_strerror(r));
408 return translate_error(r, errno);
411 free(m->cgroup_controller);
412 if (!(m->cgroup_controller = strdup("debug")))
415 if ((r = cgroup_get_subsys_mount_point(m->cgroup_controller, &mp)))
416 return translate_error(r, errno);
420 if ((r = cgroup_get_current_controller_path(pid, m->cgroup_controller, &cp))) {
422 return translate_error(r, errno);
425 free(m->cgroup_hierarchy);
426 m->cgroup_hierarchy = NULL;
427 if (asprintf(&m->cgroup_hierarchy, "%s/systemd-%llu", strcmp(cp, "/") == 0 ? "" : cp, (unsigned long long) pid) < 0) {
433 log_info("Using cgroup controller <%s>, hierarchy mounted at <%s>, using root group <%s>.",
434 m->cgroup_controller,
436 m->cgroup_hierarchy);
438 if ((r = install_release_agent(m, mp)) < 0)
439 log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
441 log_info("Installed release agent, or already installed.");
446 if ((r = create_hierarchy_cgroup(m)) < 0)
447 log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
449 log_info("Created root group.");
454 int manager_shutdown_cgroup(Manager *m) {
460 if (!m->cgroup_hierarchy)
463 if (!(cg = cgroup_new_cgroup(m->cgroup_hierarchy)))
466 if (!(cgroup_add_controller(cg, m->cgroup_controller))) {
471 if ((r = cgroup_delete_cgroup_ext(cg, CGFLAG_DELETE_IGNORE_MIGRATION|CGFLAG_DELETE_RECURSIVE)) != 0) {
472 log_error("Failed to delete cgroup hierarchy group: %s", cgroup_strerror(r));
473 r = translate_error(r, errno);
484 int cgroup_notify_empty(Manager *m, const char *group) {
485 CGroupBonding *l, *b;
490 if (!(l = hashmap_get(m->cgroup_bondings, group)))
493 LIST_FOREACH(by_path, b, l) {
499 if ((t = cgroup_bonding_is_empty_list(b)) < 0) {
501 /* If we don't know, we don't know */
503 log_warning("Failed to check whether cgroup is empty: %s", strerror(errno));
509 if (UNIT_VTABLE(b->unit)->cgroup_notify_empty)
510 UNIT_VTABLE(b->unit)->cgroup_notify_empty(b->unit);
516 CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) {
521 LIST_FOREACH(by_unit, b, first)
522 if (streq(b->controller, controller))