chiark / gitweb /
systemctl: show exec status of all exited programs
[elogind.git] / src / cgroup.c
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
29 #include <libcgroup.h>
30
31 #include "cgroup.h"
32 #include "cgroup-util.h"
33 #include "log.h"
34
35 int cgroup_bonding_realize(CGroupBonding *b) {
36         int r;
37
38         assert(b);
39         assert(b->path);
40         assert(b->controller);
41
42         if (b->realized)
43                 return 0;
44
45         if ((r = cg_create(b->controller, b->path)) < 0)
46                 return r;
47
48         b->realized = true;
49
50         if (b->only_us && b->clean_up)
51                 cg_trim(b->controller, b->path, false);
52
53         return 0;
54 }
55
56 int cgroup_bonding_realize_list(CGroupBonding *first) {
57         CGroupBonding *b;
58         int r;
59
60         LIST_FOREACH(by_unit, b, first)
61                 if ((r = cgroup_bonding_realize(b)) < 0)
62                         return r;
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
84         if (b->realized && b->only_us && b->clean_up) {
85
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);
90         }
91
92         free(b->controller);
93         free(b->path);
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
104 int cgroup_bonding_install(CGroupBonding *b, pid_t pid) {
105         int r;
106
107         assert(b);
108         assert(pid >= 0);
109
110         if ((r = cg_create_and_attach(b->controller, b->path, pid)) < 0)
111                 return r;
112
113         b->realized = true;
114         return 0;
115 }
116
117 int cgroup_bonding_install_list(CGroupBonding *first, pid_t pid) {
118         CGroupBonding *b;
119         int r;
120
121         LIST_FOREACH(by_unit, b, first)
122                 if ((r = cgroup_bonding_install(b, pid)) < 0)
123                         return r;
124
125         return 0;
126 }
127
128 int cgroup_bonding_kill(CGroupBonding *b, int sig) {
129         int r;
130
131         assert(b);
132         assert(sig >= 0);
133
134         if ((r = cgroup_bonding_realize(b)) < 0)
135                 return r;
136
137         assert(b->realized);
138
139         return cg_kill_recursive(b->controller, b->path, sig, true);
140 }
141
142 int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
143         CGroupBonding *b;
144         int r = -EAGAIN;
145
146         LIST_FOREACH(by_unit, b, first) {
147                 if ((r = cgroup_bonding_kill(b, sig)) < 0) {
148                         if (r == -EAGAIN || r == -ESRCH)
149                                 continue;
150
151                         return r;
152                 }
153
154                 return 0;
155         }
156
157         return r;
158 }
159
160 /* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we
161  * cannot know */
162 int cgroup_bonding_is_empty(CGroupBonding *b) {
163         int r;
164
165         assert(b);
166
167         if ((r = cg_is_empty_recursive(b->controller, b->path, true)) < 0)
168                 return r;
169
170         /* If it is empty it is empty */
171         if (r > 0)
172                 return 1;
173
174         /* It's not only us using this cgroup, so we just don't know */
175         return b->only_us ? 0 : -EAGAIN;
176 }
177
178 int cgroup_bonding_is_empty_list(CGroupBonding *first) {
179         CGroupBonding *b;
180
181         LIST_FOREACH(by_unit, b, first) {
182                 int r;
183
184                 if ((r = cgroup_bonding_is_empty(b)) < 0) {
185                         /* If this returned -EAGAIN, then we don't know if the
186                          * group is empty, so let's see if another group can
187                          * tell us */
188
189                         if (r != -EAGAIN)
190                                 return r;
191                 } else
192                         return r;
193         }
194
195         return -EAGAIN;
196 }
197
198 int manager_setup_cgroup(Manager *m) {
199         char *cp;
200         int r;
201         pid_t pid;
202         char suffix[32];
203
204         assert(m);
205
206         if ((r = cgroup_init()) != 0) {
207                 log_error("Failed to initialize libcg: %s", cgroup_strerror(r));
208                 return cg_translate_error(r, errno);
209         }
210
211         free(m->cgroup_controller);
212         if (!(m->cgroup_controller = strdup("name=systemd")))
213                 return -ENOMEM;
214
215         free(m->cgroup_mount_point);
216         m->cgroup_mount_point = NULL;
217         if ((r = cgroup_get_subsys_mount_point(m->cgroup_controller, &m->cgroup_mount_point)))
218                 return cg_translate_error(r, errno);
219
220         pid = getpid();
221
222         if ((r = cgroup_get_current_controller_path(pid, m->cgroup_controller, &cp)))
223                 return cg_translate_error(r, errno);
224
225         snprintf(suffix, sizeof(suffix), "/systemd-%u", (unsigned) pid);
226         char_array_0(suffix);
227
228         free(m->cgroup_hierarchy);
229
230         if (endswith(cp, suffix))
231                 /* We probably got reexecuted and can continue to use our root cgroup */
232                 m->cgroup_hierarchy = cp;
233         else {
234                 /* We need a new root cgroup */
235
236                 m->cgroup_hierarchy = NULL;
237                 r = asprintf(&m->cgroup_hierarchy, "%s%s", streq(cp, "/") ? "" : cp, suffix);
238                 free(cp);
239
240                 if (r < 0)
241                         return -ENOMEM;
242         }
243
244         log_debug("Using cgroup controller <%s>, hierarchy mounted at <%s>, using root group <%s>.",
245                   m->cgroup_controller,
246                   m->cgroup_mount_point,
247                   m->cgroup_hierarchy);
248
249         if ((r = cg_install_release_agent(m->cgroup_controller, CGROUP_AGENT_PATH)) < 0)
250                 log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
251         else
252                 log_debug("Installed release agent, or already installed.");
253
254         if ((r = cg_create_and_attach(m->cgroup_controller, m->cgroup_hierarchy, 0)) < 0)
255                 log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
256         else
257                 log_debug("Created root group.");
258
259         return r;
260 }
261
262 int manager_shutdown_cgroup(Manager *m) {
263         assert(m);
264
265         if (!m->cgroup_controller || !m->cgroup_hierarchy)
266                 return 0;
267
268         return cg_delete(m->cgroup_controller, m->cgroup_hierarchy);
269 }
270
271 int cgroup_notify_empty(Manager *m, const char *group) {
272         CGroupBonding *l, *b;
273
274         assert(m);
275         assert(group);
276
277         if (!(l = hashmap_get(m->cgroup_bondings, group)))
278                 return 0;
279
280         LIST_FOREACH(by_path, b, l) {
281                 int t;
282
283                 if (!b->unit)
284                         continue;
285
286                 if ((t = cgroup_bonding_is_empty_list(b)) < 0) {
287
288                         /* If we don't know, we don't know */
289                         if (t != -EAGAIN)
290                                 log_warning("Failed to check whether cgroup is empty: %s", strerror(errno));
291
292                         continue;
293                 }
294
295                 if (t > 0)
296                         if (UNIT_VTABLE(b->unit)->cgroup_notify_empty)
297                                 UNIT_VTABLE(b->unit)->cgroup_notify_empty(b->unit);
298         }
299
300         return 0;
301 }
302
303 Unit* cgroup_unit_by_pid(Manager *m, pid_t pid) {
304         CGroupBonding *l, *b;
305         char *group = NULL;
306         int r;
307
308         assert(m);
309
310         if (pid <= 1)
311                 return NULL;
312
313         if ((r = cg_get_by_pid(m->cgroup_controller, pid, &group)))
314                 return NULL;
315
316         l = hashmap_get(m->cgroup_bondings, group);
317         free(group);
318
319         LIST_FOREACH(by_path, b, l) {
320
321                 if (!b->unit)
322                         continue;
323
324                 if (b->only_us)
325                         return b->unit;
326         }
327
328         return NULL;
329 }
330
331 CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) {
332         CGroupBonding *b;
333
334         assert(controller);
335
336         LIST_FOREACH(by_unit, b, first)
337                 if (streq(b->controller, controller))
338                         return b;
339
340         return NULL;
341 }
342
343 char *cgroup_bonding_to_string(CGroupBonding *b) {
344         char *r;
345
346         assert(b);
347
348         if (asprintf(&r, "%s:%s", b->controller, b->path) < 0)
349                 return NULL;
350
351         return r;
352 }