]>
Commit | Line | Data |
---|---|---|
8e274523 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <assert.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/types.h> | |
26 | #include <signal.h> | |
27 | #include <sys/mount.h> | |
28 | ||
8c6db833 LP |
29 | #include <libcgroup.h> |
30 | ||
8e274523 | 31 | #include "cgroup.h" |
8c6db833 | 32 | #include "cgroup-util.h" |
8e274523 LP |
33 | #include "log.h" |
34 | ||
8e274523 LP |
35 | int cgroup_bonding_realize(CGroupBonding *b) { |
36 | int r; | |
37 | ||
38 | assert(b); | |
39 | assert(b->path); | |
40 | assert(b->controller); | |
41 | ||
8c6db833 | 42 | if (b->realized) |
8e274523 LP |
43 | return 0; |
44 | ||
8c6db833 LP |
45 | if ((r = cg_create(b->controller, b->path)) < 0) |
46 | return r; | |
8e274523 | 47 | |
8c6db833 | 48 | b->realized = true; |
8e274523 | 49 | |
8c6db833 LP |
50 | if (b->only_us && b->clean_up) |
51 | cg_trim(b->controller, b->path, false); | |
8e274523 LP |
52 | |
53 | return 0; | |
8e274523 LP |
54 | } |
55 | ||
56 | int cgroup_bonding_realize_list(CGroupBonding *first) { | |
57 | CGroupBonding *b; | |
8c6db833 | 58 | int r; |
8e274523 | 59 | |
8c6db833 | 60 | LIST_FOREACH(by_unit, b, first) |
8e274523 LP |
61 | if ((r = cgroup_bonding_realize(b)) < 0) |
62 | return r; | |
8e274523 LP |
63 | |
64 | return 0; | |
65 | } | |
66 | ||
67 | void cgroup_bonding_free(CGroupBonding *b) { | |
68 | assert(b); | |
69 | ||
70 | if (b->unit) { | |
71 | CGroupBonding *f; | |
72 | ||
73 | LIST_REMOVE(CGroupBonding, by_unit, b->unit->meta.cgroup_bondings, b); | |
74 | ||
75 | assert_se(f = hashmap_get(b->unit->meta.manager->cgroup_bondings, b->path)); | |
76 | LIST_REMOVE(CGroupBonding, by_path, f, b); | |
77 | ||
78 | if (f) | |
79 | hashmap_replace(b->unit->meta.manager->cgroup_bondings, b->path, f); | |
80 | else | |
81 | hashmap_remove(b->unit->meta.manager->cgroup_bondings, b->path); | |
82 | } | |
83 | ||
8c6db833 | 84 | if (b->realized && b->only_us && b->clean_up) { |
8e274523 | 85 | |
8c6db833 LP |
86 | if (cgroup_bonding_is_empty(b) > 0) |
87 | cg_delete(b->controller, b->path); | |
88 | else | |
89 | cg_trim(b->controller, b->path, false); | |
8e274523 LP |
90 | } |
91 | ||
c9106f61 LP |
92 | free(b->controller); |
93 | free(b->path); | |
8e274523 LP |
94 | free(b); |
95 | } | |
96 | ||
97 | void cgroup_bonding_free_list(CGroupBonding *first) { | |
98 | CGroupBonding *b, *n; | |
99 | ||
100 | LIST_FOREACH_SAFE(by_unit, b, n, first) | |
101 | cgroup_bonding_free(b); | |
102 | } | |
103 | ||
fb385181 LP |
104 | void cgroup_bonding_trim(CGroupBonding *b, bool delete_root) { |
105 | assert(b); | |
106 | ||
107 | if (b->realized && b->only_us && b->clean_up) | |
108 | cg_trim(b->controller, b->path, delete_root); | |
109 | } | |
110 | ||
111 | void cgroup_bonding_trim_list(CGroupBonding *first, bool delete_root) { | |
112 | CGroupBonding *b; | |
113 | ||
114 | LIST_FOREACH(by_unit, b, first) | |
115 | cgroup_bonding_trim(b, delete_root); | |
116 | } | |
117 | ||
8e274523 LP |
118 | int cgroup_bonding_install(CGroupBonding *b, pid_t pid) { |
119 | int r; | |
120 | ||
121 | assert(b); | |
122 | assert(pid >= 0); | |
123 | ||
8c6db833 LP |
124 | if ((r = cg_create_and_attach(b->controller, b->path, pid)) < 0) |
125 | return r; | |
8e274523 | 126 | |
8c6db833 | 127 | b->realized = true; |
8e274523 LP |
128 | return 0; |
129 | } | |
130 | ||
131 | int cgroup_bonding_install_list(CGroupBonding *first, pid_t pid) { | |
132 | CGroupBonding *b; | |
8c6db833 | 133 | int r; |
8e274523 | 134 | |
8c6db833 | 135 | LIST_FOREACH(by_unit, b, first) |
8e274523 LP |
136 | if ((r = cgroup_bonding_install(b, pid)) < 0) |
137 | return r; | |
8e274523 LP |
138 | |
139 | return 0; | |
140 | } | |
141 | ||
142 | int cgroup_bonding_kill(CGroupBonding *b, int sig) { | |
143 | int r; | |
8e274523 LP |
144 | |
145 | assert(b); | |
8c6db833 | 146 | assert(sig >= 0); |
8e274523 | 147 | |
8c6db833 | 148 | if ((r = cgroup_bonding_realize(b)) < 0) |
50159e6a LP |
149 | return r; |
150 | ||
8c6db833 LP |
151 | assert(b->realized); |
152 | ||
153 | return cg_kill_recursive(b->controller, b->path, sig, true); | |
8e274523 LP |
154 | } |
155 | ||
156 | int cgroup_bonding_kill_list(CGroupBonding *first, int sig) { | |
157 | CGroupBonding *b; | |
50159e6a | 158 | int r = -EAGAIN; |
8e274523 LP |
159 | |
160 | LIST_FOREACH(by_unit, b, first) { | |
50159e6a | 161 | if ((r = cgroup_bonding_kill(b, sig)) < 0) { |
8c6db833 | 162 | if (r == -EAGAIN || r == -ESRCH) |
50159e6a | 163 | continue; |
8e274523 | 164 | |
8e274523 | 165 | return r; |
50159e6a LP |
166 | } |
167 | ||
168 | return 0; | |
8e274523 LP |
169 | } |
170 | ||
50159e6a | 171 | return r; |
8e274523 LP |
172 | } |
173 | ||
174 | /* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we | |
175 | * cannot know */ | |
176 | int cgroup_bonding_is_empty(CGroupBonding *b) { | |
8e274523 LP |
177 | int r; |
178 | ||
179 | assert(b); | |
180 | ||
8c6db833 LP |
181 | if ((r = cg_is_empty_recursive(b->controller, b->path, true)) < 0) |
182 | return r; | |
8e274523 | 183 | |
8c6db833 LP |
184 | /* If it is empty it is empty */ |
185 | if (r > 0) | |
8e274523 LP |
186 | return 1; |
187 | ||
8c6db833 LP |
188 | /* It's not only us using this cgroup, so we just don't know */ |
189 | return b->only_us ? 0 : -EAGAIN; | |
8e274523 LP |
190 | } |
191 | ||
192 | int cgroup_bonding_is_empty_list(CGroupBonding *first) { | |
193 | CGroupBonding *b; | |
194 | ||
195 | LIST_FOREACH(by_unit, b, first) { | |
196 | int r; | |
197 | ||
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 | |
201 | * tell us */ | |
202 | ||
203 | if (r != -EAGAIN) | |
204 | return r; | |
205 | } else | |
206 | return r; | |
207 | } | |
208 | ||
209 | return -EAGAIN; | |
210 | } | |
211 | ||
8e274523 | 212 | int manager_setup_cgroup(Manager *m) { |
33be102a | 213 | char *cp; |
8e274523 LP |
214 | int r; |
215 | pid_t pid; | |
7ccfb64a | 216 | char suffix[32]; |
8e274523 LP |
217 | |
218 | assert(m); | |
219 | ||
220 | if ((r = cgroup_init()) != 0) { | |
221 | log_error("Failed to initialize libcg: %s", cgroup_strerror(r)); | |
8c6db833 | 222 | return cg_translate_error(r, errno); |
8e274523 LP |
223 | } |
224 | ||
33be102a LP |
225 | free(m->cgroup_mount_point); |
226 | m->cgroup_mount_point = NULL; | |
55096547 | 227 | if ((r = cgroup_get_subsys_mount_point(SYSTEMD_CGROUP_CONTROLLER, &m->cgroup_mount_point))) |
8c6db833 | 228 | return cg_translate_error(r, errno); |
8e274523 LP |
229 | |
230 | pid = getpid(); | |
231 | ||
55096547 | 232 | if ((r = cgroup_get_current_controller_path(pid, SYSTEMD_CGROUP_CONTROLLER, &cp))) |
8c6db833 | 233 | return cg_translate_error(r, errno); |
8e274523 | 234 | |
7ccfb64a LP |
235 | snprintf(suffix, sizeof(suffix), "/systemd-%u", (unsigned) pid); |
236 | char_array_0(suffix); | |
237 | ||
8e274523 | 238 | free(m->cgroup_hierarchy); |
7ccfb64a LP |
239 | |
240 | if (endswith(cp, suffix)) | |
241 | /* We probably got reexecuted and can continue to use our root cgroup */ | |
242 | m->cgroup_hierarchy = cp; | |
243 | else { | |
244 | /* We need a new root cgroup */ | |
245 | ||
246 | m->cgroup_hierarchy = NULL; | |
247 | r = asprintf(&m->cgroup_hierarchy, "%s%s", streq(cp, "/") ? "" : cp, suffix); | |
8e274523 | 248 | free(cp); |
7ccfb64a | 249 | |
33be102a | 250 | if (r < 0) |
7ccfb64a | 251 | return -ENOMEM; |
8e274523 LP |
252 | } |
253 | ||
55096547 | 254 | log_debug("Using cgroup controller <" SYSTEMD_CGROUP_CONTROLLER ">, hierarchy mounted at <%s>, using root group <%s>.", |
33be102a | 255 | m->cgroup_mount_point, |
30de7d85 | 256 | m->cgroup_hierarchy); |
8e274523 | 257 | |
55096547 | 258 | if ((r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, CGROUP_AGENT_PATH)) < 0) |
8e274523 LP |
259 | log_warning("Failed to install release agent, ignoring: %s", strerror(-r)); |
260 | else | |
30de7d85 | 261 | log_debug("Installed release agent, or already installed."); |
8e274523 | 262 | |
55096547 | 263 | if ((r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_hierarchy, 0)) < 0) |
8e274523 LP |
264 | log_error("Failed to create root cgroup hierarchy: %s", strerror(-r)); |
265 | else | |
30de7d85 | 266 | log_debug("Created root group."); |
8e274523 LP |
267 | |
268 | return r; | |
269 | } | |
270 | ||
8c6db833 | 271 | int manager_shutdown_cgroup(Manager *m) { |
8e274523 LP |
272 | assert(m); |
273 | ||
55096547 | 274 | if (!m->cgroup_hierarchy) |
8e274523 LP |
275 | return 0; |
276 | ||
55096547 | 277 | return cg_delete(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_hierarchy); |
8e274523 LP |
278 | } |
279 | ||
280 | int cgroup_notify_empty(Manager *m, const char *group) { | |
281 | CGroupBonding *l, *b; | |
282 | ||
283 | assert(m); | |
284 | assert(group); | |
285 | ||
286 | if (!(l = hashmap_get(m->cgroup_bondings, group))) | |
287 | return 0; | |
288 | ||
289 | LIST_FOREACH(by_path, b, l) { | |
290 | int t; | |
291 | ||
292 | if (!b->unit) | |
293 | continue; | |
294 | ||
295 | if ((t = cgroup_bonding_is_empty_list(b)) < 0) { | |
296 | ||
297 | /* If we don't know, we don't know */ | |
298 | if (t != -EAGAIN) | |
299 | log_warning("Failed to check whether cgroup is empty: %s", strerror(errno)); | |
300 | ||
301 | continue; | |
302 | } | |
303 | ||
304 | if (t > 0) | |
305 | if (UNIT_VTABLE(b->unit)->cgroup_notify_empty) | |
306 | UNIT_VTABLE(b->unit)->cgroup_notify_empty(b->unit); | |
307 | } | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
8c47c732 LP |
312 | Unit* cgroup_unit_by_pid(Manager *m, pid_t pid) { |
313 | CGroupBonding *l, *b; | |
314 | char *group = NULL; | |
315 | int r; | |
316 | ||
317 | assert(m); | |
318 | ||
319 | if (pid <= 1) | |
320 | return NULL; | |
321 | ||
55096547 | 322 | if ((r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &group))) |
8c47c732 LP |
323 | return NULL; |
324 | ||
325 | l = hashmap_get(m->cgroup_bondings, group); | |
326 | free(group); | |
327 | ||
8c47c732 LP |
328 | LIST_FOREACH(by_path, b, l) { |
329 | ||
330 | if (!b->unit) | |
331 | continue; | |
332 | ||
333 | if (b->only_us) | |
334 | return b->unit; | |
335 | } | |
336 | ||
337 | return NULL; | |
338 | } | |
339 | ||
8e274523 LP |
340 | CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) { |
341 | CGroupBonding *b; | |
342 | ||
343 | assert(controller); | |
344 | ||
345 | LIST_FOREACH(by_unit, b, first) | |
346 | if (streq(b->controller, controller)) | |
347 | return b; | |
348 | ||
349 | return NULL; | |
350 | } | |
6dde1f33 LP |
351 | |
352 | char *cgroup_bonding_to_string(CGroupBonding *b) { | |
353 | char *r; | |
354 | ||
355 | assert(b); | |
356 | ||
357 | if (asprintf(&r, "%s:%s", b->controller, b->path) < 0) | |
358 | return NULL; | |
359 | ||
360 | return r; | |
361 | } |