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>
29 #include <libcgroup.h>
32 #include "cgroup-util.h"
35 int cgroup_bonding_realize(CGroupBonding *b) {
40 assert(b->controller);
45 if ((r = cg_create(b->controller, b->path)) < 0)
50 if (b->only_us && b->clean_up)
51 cg_trim(b->controller, b->path, false);
56 int cgroup_bonding_realize_list(CGroupBonding *first) {
60 LIST_FOREACH(by_unit, b, first)
61 if ((r = cgroup_bonding_realize(b)) < 0)
67 void cgroup_bonding_free(CGroupBonding *b) {
73 LIST_REMOVE(CGroupBonding, by_unit, b->unit->meta.cgroup_bondings, b);
75 assert_se(f = hashmap_get(b->unit->meta.manager->cgroup_bondings, b->path));
76 LIST_REMOVE(CGroupBonding, by_path, f, b);
79 hashmap_replace(b->unit->meta.manager->cgroup_bondings, b->path, f);
81 hashmap_remove(b->unit->meta.manager->cgroup_bondings, b->path);
84 if (b->realized && b->only_us && b->clean_up) {
86 if (cgroup_bonding_is_empty(b) > 0)
87 cg_delete(b->controller, b->path);
89 cg_trim(b->controller, b->path, false);
97 void cgroup_bonding_free_list(CGroupBonding *first) {
100 LIST_FOREACH_SAFE(by_unit, b, n, first)
101 cgroup_bonding_free(b);
104 void cgroup_bonding_trim(CGroupBonding *b, bool delete_root) {
107 if (b->realized && b->only_us && b->clean_up)
108 cg_trim(b->controller, b->path, delete_root);
111 void cgroup_bonding_trim_list(CGroupBonding *first, bool delete_root) {
114 LIST_FOREACH(by_unit, b, first)
115 cgroup_bonding_trim(b, delete_root);
118 int cgroup_bonding_install(CGroupBonding *b, pid_t pid) {
124 if ((r = cg_create_and_attach(b->controller, b->path, pid)) < 0)
131 int cgroup_bonding_install_list(CGroupBonding *first, pid_t pid) {
135 LIST_FOREACH(by_unit, b, first)
136 if ((r = cgroup_bonding_install(b, pid)) < 0)
142 int cgroup_bonding_kill(CGroupBonding *b, int sig) {
148 if ((r = cgroup_bonding_realize(b)) < 0)
153 return cg_kill_recursive(b->controller, b->path, sig, true);
156 int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
160 LIST_FOREACH(by_unit, b, first) {
161 if ((r = cgroup_bonding_kill(b, sig)) < 0) {
162 if (r == -EAGAIN || r == -ESRCH)
174 /* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we
176 int cgroup_bonding_is_empty(CGroupBonding *b) {
181 if ((r = cg_is_empty_recursive(b->controller, b->path, true)) < 0)
184 /* If it is empty it is empty */
188 /* It's not only us using this cgroup, so we just don't know */
189 return b->only_us ? 0 : -EAGAIN;
192 int cgroup_bonding_is_empty_list(CGroupBonding *first) {
195 LIST_FOREACH(by_unit, b, first) {
198 if ((r = cgroup_bonding_is_empty(b)) < 0) {
199 /* If this returned -EAGAIN, then we don't know if the
200 * group is empty, so let's see if another group can
212 int manager_setup_cgroup(Manager *m) {
220 if ((r = cgroup_init()) != 0) {
221 log_error("Failed to initialize libcg: %s", cgroup_strerror(r));
222 return cg_translate_error(r, errno);
225 free(m->cgroup_controller);
226 if (!(m->cgroup_controller = strdup("name=systemd")))
229 free(m->cgroup_mount_point);
230 m->cgroup_mount_point = NULL;
231 if ((r = cgroup_get_subsys_mount_point(m->cgroup_controller, &m->cgroup_mount_point)))
232 return cg_translate_error(r, errno);
236 if ((r = cgroup_get_current_controller_path(pid, m->cgroup_controller, &cp)))
237 return cg_translate_error(r, errno);
239 snprintf(suffix, sizeof(suffix), "/systemd-%u", (unsigned) pid);
240 char_array_0(suffix);
242 free(m->cgroup_hierarchy);
244 if (endswith(cp, suffix))
245 /* We probably got reexecuted and can continue to use our root cgroup */
246 m->cgroup_hierarchy = cp;
248 /* We need a new root cgroup */
250 m->cgroup_hierarchy = NULL;
251 r = asprintf(&m->cgroup_hierarchy, "%s%s", streq(cp, "/") ? "" : cp, suffix);
258 log_debug("Using cgroup controller <%s>, hierarchy mounted at <%s>, using root group <%s>.",
259 m->cgroup_controller,
260 m->cgroup_mount_point,
261 m->cgroup_hierarchy);
263 if ((r = cg_install_release_agent(m->cgroup_controller, CGROUP_AGENT_PATH)) < 0)
264 log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
266 log_debug("Installed release agent, or already installed.");
268 if ((r = cg_create_and_attach(m->cgroup_controller, m->cgroup_hierarchy, 0)) < 0)
269 log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
271 log_debug("Created root group.");
276 int manager_shutdown_cgroup(Manager *m) {
279 if (!m->cgroup_controller || !m->cgroup_hierarchy)
282 return cg_delete(m->cgroup_controller, m->cgroup_hierarchy);
285 int cgroup_notify_empty(Manager *m, const char *group) {
286 CGroupBonding *l, *b;
291 if (!(l = hashmap_get(m->cgroup_bondings, group)))
294 LIST_FOREACH(by_path, b, l) {
300 if ((t = cgroup_bonding_is_empty_list(b)) < 0) {
302 /* If we don't know, we don't know */
304 log_warning("Failed to check whether cgroup is empty: %s", strerror(errno));
310 if (UNIT_VTABLE(b->unit)->cgroup_notify_empty)
311 UNIT_VTABLE(b->unit)->cgroup_notify_empty(b->unit);
317 Unit* cgroup_unit_by_pid(Manager *m, pid_t pid) {
318 CGroupBonding *l, *b;
327 if ((r = cg_get_by_pid(m->cgroup_controller, pid, &group)))
330 l = hashmap_get(m->cgroup_bondings, group);
333 LIST_FOREACH(by_path, b, l) {
345 CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) {
350 LIST_FOREACH(by_unit, b, first)
351 if (streq(b->controller, controller))
357 char *cgroup_bonding_to_string(CGroupBonding *b) {
362 if (asprintf(&r, "%s:%s", b->controller, b->path) < 0)