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);
122 if (b->only_us && b->clean_up && cgroup_bonding_is_empty(b) > 0)
123 cgroup_delete_cgroup_ext(b->cgroup, true);
125 cgroup_free(&b->cgroup);
133 void cgroup_bonding_free_list(CGroupBonding *first) {
134 CGroupBonding *b, *n;
136 LIST_FOREACH_SAFE(by_unit, b, n, first)
137 cgroup_bonding_free(b);
140 int cgroup_bonding_install(CGroupBonding *b, pid_t pid) {
152 if ((r = cgroup_attach_task_pid(b->cgroup, pid)))
153 return translate_error(r, errno);
158 int cgroup_bonding_install_list(CGroupBonding *first, pid_t pid) {
161 LIST_FOREACH(by_unit, b, first) {
164 if ((r = cgroup_bonding_install(b, pid)) < 0)
171 int cgroup_bonding_kill(CGroupBonding *b, int sig) {
183 if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
192 if ((r = cgroup_get_task_begin(b->path, b->controller, &iterator, &pid)) != 0) {
197 if (r == ECGOTHER && errno == ENOENT)
200 r = translate_error(r, errno);
206 if (set_get(s, INT_TO_PTR(pid)) != INT_TO_PTR(pid)) {
208 /* If we haven't killed this process
211 if (kill(pid, sig) < 0 && errno != ESRCH) {
219 if ((r = set_put(s, INT_TO_PTR(pid))) < 0)
223 if ((r = cgroup_get_task_next(&iterator, &pid)) != 0) {
228 r = translate_error(r, errno);
235 assert_se(cgroup_get_task_end(&iterator) == 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. */
241 } while (!done && r >= 0);
248 return killed ? 0 : -ESRCH;
251 int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
255 LIST_FOREACH(by_unit, b, first) {
256 if ((r = cgroup_bonding_kill(b, sig)) < 0) {
257 if (r == -EAGAIN || -ESRCH)
269 /* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we
271 int cgroup_bonding_is_empty(CGroupBonding *b) {
278 r = cgroup_get_task_begin(b->path, b->controller, &iterator, &pid);
280 if (r == 0 || r == ECGEOF)
281 cgroup_get_task_end(&iterator);
283 /* Hmm, no PID in this group? Then it is definitely empty */
287 /* Some error? Let's return it */
289 return translate_error(r, errno);
291 /* It's not empty, and we are the only user, then it is
292 * definitely not empty */
296 /* There are PIDs in the group but we aren't the only users,
297 * hence we cannot say */
301 int cgroup_bonding_is_empty_list(CGroupBonding *first) {
304 LIST_FOREACH(by_unit, b, first) {
307 if ((r = cgroup_bonding_is_empty(b)) < 0) {
308 /* If this returned -EAGAIN, then we don't know if the
309 * group is empty, so let's see if another group can
321 static int install_release_agent(Manager *m, const char *mount_point) {
328 if (asprintf(&p, "%s/release_agent", mount_point) < 0)
331 if ((r = read_one_line_file(p, &c)) < 0) {
339 if ((r = write_one_line_file(p, CGROUP_AGENT_PATH "\n" )) < 0) {
344 } else if (!streq(sc, CGROUP_AGENT_PATH)) {
353 if (asprintf(&p, "%s/notify_on_release", mount_point) < 0)
356 if ((r = read_one_line_file(p, &c)) < 0) {
363 if (streq(sc, "0")) {
364 if ((r = write_one_line_file(p, "1\n")) < 0) {
369 } else if (!streq(sc, "1")) {
381 static int create_hierarchy_cgroup(Manager *m) {
387 if (!(cg = cgroup_new_cgroup(m->cgroup_hierarchy)))
390 if (!(cgroup_add_controller(cg, m->cgroup_controller))) {
395 if ((r = cgroup_create_cgroup(cg, true)) != 0) {
396 log_error("Failed to create cgroup hierarchy group: %s", cgroup_strerror(r));
397 r = translate_error(r, errno);
401 if ((r = cgroup_attach_task(cg)) != 0) {
402 log_error("Failed to add ourselves to hierarchy group: %s", cgroup_strerror(r));
403 r = translate_error(r, errno);
414 int manager_setup_cgroup(Manager *m) {
422 if ((r = cgroup_init()) != 0) {
423 log_error("Failed to initialize libcg: %s", cgroup_strerror(r));
424 return translate_error(r, errno);
427 free(m->cgroup_controller);
428 if (!(m->cgroup_controller = strdup("name=systemd")))
431 if ((r = cgroup_get_subsys_mount_point(m->cgroup_controller, &mp)))
432 return translate_error(r, errno);
436 if ((r = cgroup_get_current_controller_path(pid, m->cgroup_controller, &cp))) {
438 return translate_error(r, errno);
441 snprintf(suffix, sizeof(suffix), "/systemd-%u", (unsigned) pid);
442 char_array_0(suffix);
444 free(m->cgroup_hierarchy);
446 if (endswith(cp, suffix))
447 /* We probably got reexecuted and can continue to use our root cgroup */
448 m->cgroup_hierarchy = cp;
450 /* We need a new root cgroup */
452 m->cgroup_hierarchy = NULL;
453 r = asprintf(&m->cgroup_hierarchy, "%s%s", streq(cp, "/") ? "" : cp, suffix);
462 log_debug("Using cgroup controller <%s>, hierarchy mounted at <%s>, using root group <%s>.",
463 m->cgroup_controller,
465 m->cgroup_hierarchy);
467 if ((r = install_release_agent(m, mp)) < 0)
468 log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
470 log_debug("Installed release agent, or already installed.");
474 if ((r = create_hierarchy_cgroup(m)) < 0)
475 log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
477 log_debug("Created root group.");
482 int manager_shutdown_cgroup(Manager *m, bool delete) {
488 if (!m->cgroup_hierarchy)
491 if (!(cg = cgroup_new_cgroup(m->cgroup_hierarchy)))
494 if (!(cgroup_add_controller(cg, m->cgroup_controller))) {
499 /* Often enough we won't be able to delete the cgroup we
500 * ourselves are in, hence ignore all errors here */
502 cgroup_delete_cgroup_ext(cg, CGFLAG_DELETE_IGNORE_MIGRATION|CGFLAG_DELETE_RECURSIVE);
511 int cgroup_notify_empty(Manager *m, const char *group) {
512 CGroupBonding *l, *b;
517 if (!(l = hashmap_get(m->cgroup_bondings, group)))
520 LIST_FOREACH(by_path, b, l) {
526 if ((t = cgroup_bonding_is_empty_list(b)) < 0) {
528 /* If we don't know, we don't know */
530 log_warning("Failed to check whether cgroup is empty: %s", strerror(errno));
536 if (UNIT_VTABLE(b->unit)->cgroup_notify_empty)
537 UNIT_VTABLE(b->unit)->cgroup_notify_empty(b->unit);
543 CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) {
548 LIST_FOREACH(by_unit, b, first)
549 if (streq(b->controller, controller))
555 char *cgroup_bonding_to_string(CGroupBonding *b) {
560 if (asprintf(&r, "%s:%s", b->controller, b->path) < 0)