chiark / gitweb /
service: interpret Debian-style X-Interactive LSB header field
[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 "cgroup.h"
30 #include "log.h"
31
32 static int translate_error(int error, int _errno) {
33
34         switch (error) {
35
36         case ECGROUPNOTCOMPILED:
37         case ECGROUPNOTMOUNTED:
38         case ECGROUPNOTEXIST:
39         case ECGROUPNOTCREATED:
40                 return -ENOENT;
41
42         case ECGINVAL:
43                 return -EINVAL;
44
45         case ECGROUPNOTALLOWED:
46                 return -EPERM;
47
48         case ECGOTHER:
49                 return -_errno;
50         }
51
52         return -EIO;
53 }
54
55 int cgroup_bonding_realize(CGroupBonding *b) {
56         int r;
57
58         assert(b);
59         assert(b->path);
60         assert(b->controller);
61
62         if (b->cgroup)
63                 return 0;
64
65         if (!(b->cgroup = cgroup_new_cgroup(b->path)))
66                 return -ENOMEM;
67
68         if (!cgroup_add_controller(b->cgroup, b->controller)) {
69                 r = -ENOMEM;
70                 goto fail;
71         }
72
73         if (b->inherit)
74                 r = cgroup_create_cgroup_from_parent(b->cgroup, true);
75         else
76                 r = cgroup_create_cgroup(b->cgroup, true);
77
78         if (r != 0) {
79                 r = translate_error(r, errno);
80                 goto fail;
81         }
82
83         return 0;
84
85 fail:
86         cgroup_free(&b->cgroup);
87         b->cgroup = NULL;
88         return r;
89 }
90
91 int cgroup_bonding_realize_list(CGroupBonding *first) {
92         CGroupBonding *b;
93
94         LIST_FOREACH(by_unit, b, first) {
95                 int r;
96
97                 if ((r = cgroup_bonding_realize(b)) < 0)
98                         return r;
99         }
100
101         return 0;
102 }
103
104 void cgroup_bonding_free(CGroupBonding *b) {
105         assert(b);
106
107         if (b->unit) {
108                 CGroupBonding *f;
109
110                 LIST_REMOVE(CGroupBonding, by_unit, b->unit->meta.cgroup_bondings, b);
111
112                 assert_se(f = hashmap_get(b->unit->meta.manager->cgroup_bondings, b->path));
113                 LIST_REMOVE(CGroupBonding, by_path, f, b);
114
115                 if (f)
116                         hashmap_replace(b->unit->meta.manager->cgroup_bondings, b->path, f);
117                 else
118                         hashmap_remove(b->unit->meta.manager->cgroup_bondings, b->path);
119         }
120
121         if (b->cgroup) {
122                 if (b->only_us && b->clean_up && cgroup_bonding_is_empty(b) > 0)
123                         cgroup_delete_cgroup_ext(b->cgroup, true);
124
125                 cgroup_free(&b->cgroup);
126         }
127
128         free(b->controller);
129         free(b->path);
130         free(b);
131 }
132
133 void cgroup_bonding_free_list(CGroupBonding *first) {
134         CGroupBonding *b, *n;
135
136         LIST_FOREACH_SAFE(by_unit, b, n, first)
137                 cgroup_bonding_free(b);
138 }
139
140 int cgroup_bonding_install(CGroupBonding *b, pid_t pid) {
141         int r;
142
143         assert(b);
144         assert(pid >= 0);
145
146         if (pid == 0)
147                 pid = getpid();
148
149         if (!b->cgroup)
150                 return -ENOENT;
151
152         if ((r = cgroup_attach_task_pid(b->cgroup, pid)))
153                 return translate_error(r, errno);
154
155         return 0;
156 }
157
158 int cgroup_bonding_install_list(CGroupBonding *first, pid_t pid) {
159         CGroupBonding *b;
160
161         LIST_FOREACH(by_unit, b, first) {
162                 int r;
163
164                 if ((r = cgroup_bonding_install(b, pid)) < 0)
165                         return r;
166         }
167
168         return 0;
169 }
170
171 int cgroup_bonding_kill(CGroupBonding *b, int sig) {
172         int r;
173         Set *s;
174         bool done;
175         bool killed = false;
176
177         assert(b);
178         assert(sig > 0);
179
180         if (!b->only_us)
181                 return -EAGAIN;
182
183         if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
184                 return -ENOMEM;
185
186         do {
187                 void *iterator;
188                 pid_t pid;
189
190                 done = true;
191
192                 if ((r = cgroup_get_task_begin(b->path, b->controller, &iterator, &pid)) != 0) {
193                         if (r == ECGEOF) {
194                                 r = 0;
195                                 goto kill_done;
196                         } else {
197                                 if (r == ECGOTHER && errno == ENOENT)
198                                         r = ESRCH;
199                                 else
200                                         r = translate_error(r, errno);
201                                 break;
202                         }
203                 }
204
205                 for (;;) {
206                         if (set_get(s, INT_TO_PTR(pid)) != INT_TO_PTR(pid)) {
207
208                                 /* If we haven't killed this process
209                                  * yet, kill it */
210
211                                 if (kill(pid, sig) < 0 && errno != ESRCH) {
212                                         r = -errno;
213                                         break;
214                                 }
215
216                                 killed = true;
217                                 done = false;
218
219                                 if ((r = set_put(s, INT_TO_PTR(pid))) < 0)
220                                     break;
221                         }
222
223                         if ((r = cgroup_get_task_next(&iterator, &pid)) != 0) {
224
225                                 if (r == ECGEOF)
226                                         r = 0;
227                                 else
228                                         r = translate_error(r, errno);
229
230                                 break;
231                         }
232                 }
233
234         kill_done:
235                 assert_se(cgroup_get_task_end(&iterator) == 0);
236
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. */
240
241         } while (!done && r >= 0);
242
243         set_free(s);
244
245         if (r < 0)
246                 return r;
247
248         return killed ? 0 : -ESRCH;
249 }
250
251 int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
252         CGroupBonding *b;
253         int r = -EAGAIN;
254
255         LIST_FOREACH(by_unit, b, first) {
256                 if ((r = cgroup_bonding_kill(b, sig)) < 0) {
257                         if (r == -EAGAIN || -ESRCH)
258                                 continue;
259
260                         return r;
261                 }
262
263                 return 0;
264         }
265
266         return r;
267 }
268
269 /* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we
270  * cannot know */
271 int cgroup_bonding_is_empty(CGroupBonding *b) {
272         void *iterator;
273         pid_t pid;
274         int r;
275
276         assert(b);
277
278         r = cgroup_get_task_begin(b->path, b->controller, &iterator, &pid);
279
280         if (r == 0 || r == ECGEOF)
281                 cgroup_get_task_end(&iterator);
282
283         /* Hmm, no PID in this group? Then it is definitely empty */
284         if (r == ECGEOF)
285                 return 1;
286
287         /* Some error? Let's return it */
288         if (r != 0)
289                 return translate_error(r, errno);
290
291         /* It's not empty, and we are the only user, then it is
292          * definitely not empty */
293         if (b->only_us)
294                 return 0;
295
296         /* There are PIDs in the group but we aren't the only users,
297          * hence we cannot say */
298         return -EAGAIN;
299 }
300
301 int cgroup_bonding_is_empty_list(CGroupBonding *first) {
302         CGroupBonding *b;
303
304         LIST_FOREACH(by_unit, b, first) {
305                 int r;
306
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
310                          * tell us */
311
312                         if (r != -EAGAIN)
313                                 return r;
314                 } else
315                         return r;
316         }
317
318         return -EAGAIN;
319 }
320
321 static int install_release_agent(Manager *m, const char *mount_point) {
322         char *p, *c, *sc;
323         int r;
324
325         assert(m);
326         assert(mount_point);
327
328         if (asprintf(&p, "%s/release_agent", mount_point) < 0)
329                 return -ENOMEM;
330
331         if ((r = read_one_line_file(p, &c)) < 0) {
332                 free(p);
333                 return r;
334         }
335
336         sc = strstrip(c);
337
338         if (sc[0] == 0) {
339                 if ((r = write_one_line_file(p, CGROUP_AGENT_PATH "\n" )) < 0) {
340                         free(p);
341                         free(c);
342                         return r;
343                 }
344         } else if (!streq(sc, CGROUP_AGENT_PATH)) {
345                 free(p);
346                 free(c);
347                 return -EEXIST;
348         }
349
350         free(c);
351         free(p);
352
353         if (asprintf(&p, "%s/notify_on_release", mount_point) < 0)
354                 return -ENOMEM;
355
356         if ((r = read_one_line_file(p, &c)) < 0) {
357                 free(p);
358                 return r;
359         }
360
361         sc = strstrip(c);
362
363         if (streq(sc, "0")) {
364                 if ((r = write_one_line_file(p, "1\n")) < 0) {
365                         free(p);
366                         free(c);
367                         return r;
368                 }
369         } else if (!streq(sc, "1")) {
370                 free(p);
371                 free(c);
372                 return -EIO;
373         }
374
375         free(p);
376         free(c);
377
378         return 0;
379 }
380
381 static int create_hierarchy_cgroup(Manager *m) {
382         struct cgroup *cg;
383         int r;
384
385         assert(m);
386
387         if (!(cg = cgroup_new_cgroup(m->cgroup_hierarchy)))
388                 return -ENOMEM;
389
390         if (!(cgroup_add_controller(cg, m->cgroup_controller))) {
391                 r = -ENOMEM;
392                 goto finish;
393         }
394
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);
398                 goto finish;
399         }
400
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);
404                 goto finish;
405         }
406
407         r = 0;
408
409 finish:
410         cgroup_free(&cg);
411         return r;
412 }
413
414 int manager_setup_cgroup(Manager *m) {
415         char *mp, *cp;
416         int r;
417         pid_t pid;
418         char suffix[32];
419
420         assert(m);
421
422         if ((r = cgroup_init()) != 0) {
423                 log_error("Failed to initialize libcg: %s", cgroup_strerror(r));
424                 return translate_error(r, errno);
425         }
426
427         free(m->cgroup_controller);
428         if (!(m->cgroup_controller = strdup("name=systemd")))
429                 return -ENOMEM;
430
431         if ((r = cgroup_get_subsys_mount_point(m->cgroup_controller, &mp)))
432                 return translate_error(r, errno);
433
434         pid = getpid();
435
436         if ((r = cgroup_get_current_controller_path(pid, m->cgroup_controller, &cp))) {
437                 free(mp);
438                 return translate_error(r, errno);
439         }
440
441         snprintf(suffix, sizeof(suffix), "/systemd-%u", (unsigned) pid);
442         char_array_0(suffix);
443
444         free(m->cgroup_hierarchy);
445
446         if (endswith(cp, suffix))
447                 /* We probably got reexecuted and can continue to use our root cgroup */
448                 m->cgroup_hierarchy = cp;
449         else {
450                 /* We need a new root cgroup */
451
452                 m->cgroup_hierarchy = NULL;
453                 r = asprintf(&m->cgroup_hierarchy, "%s%s", streq(cp, "/") ? "" : cp, suffix);
454                 free(cp);
455
456                 if (r < 0) {
457                         free(mp);
458                         return -ENOMEM;
459                 }
460         }
461
462         log_debug("Using cgroup controller <%s>, hierarchy mounted at <%s>, using root group <%s>.",
463                   m->cgroup_controller,
464                   mp,
465                   m->cgroup_hierarchy);
466
467         if ((r = install_release_agent(m, mp)) < 0)
468                 log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
469         else
470                 log_debug("Installed release agent, or already installed.");
471
472         free(mp);
473
474         if ((r = create_hierarchy_cgroup(m)) < 0)
475                 log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
476         else
477                 log_debug("Created root group.");
478
479         return r;
480 }
481
482 int manager_shutdown_cgroup(Manager *m, bool delete) {
483         struct cgroup *cg;
484         int r;
485
486         assert(m);
487
488         if (!m->cgroup_hierarchy)
489                 return 0;
490
491         if (!(cg = cgroup_new_cgroup(m->cgroup_hierarchy)))
492                 return -ENOMEM;
493
494         if (!(cgroup_add_controller(cg, m->cgroup_controller))) {
495                 r = -ENOMEM;
496                 goto finish;
497         }
498
499         /* Often enough we won't be able to delete the cgroup we
500          * ourselves are in, hence ignore all errors here */
501         if (delete)
502                 cgroup_delete_cgroup_ext(cg, CGFLAG_DELETE_IGNORE_MIGRATION|CGFLAG_DELETE_RECURSIVE);
503         r = 0;
504
505 finish:
506         cgroup_free(&cg);
507         return r;
508
509 }
510
511 int cgroup_notify_empty(Manager *m, const char *group) {
512         CGroupBonding *l, *b;
513
514         assert(m);
515         assert(group);
516
517         if (!(l = hashmap_get(m->cgroup_bondings, group)))
518                 return 0;
519
520         LIST_FOREACH(by_path, b, l) {
521                 int t;
522
523                 if (!b->unit)
524                         continue;
525
526                 if ((t = cgroup_bonding_is_empty_list(b)) < 0) {
527
528                         /* If we don't know, we don't know */
529                         if (t != -EAGAIN)
530                                 log_warning("Failed to check whether cgroup is empty: %s", strerror(errno));
531
532                         continue;
533                 }
534
535                 if (t > 0)
536                         if (UNIT_VTABLE(b->unit)->cgroup_notify_empty)
537                                 UNIT_VTABLE(b->unit)->cgroup_notify_empty(b->unit);
538         }
539
540         return 0;
541 }
542
543 CGroupBonding *cgroup_bonding_find_list(CGroupBonding *first, const char *controller) {
544         CGroupBonding *b;
545
546         assert(controller);
547
548         LIST_FOREACH(by_unit, b, first)
549                 if (streq(b->controller, controller))
550                         return b;
551
552         return NULL;
553 }
554
555 char *cgroup_bonding_to_string(CGroupBonding *b) {
556         char *r;
557
558         assert(b);
559
560         if (asprintf(&r, "%s:%s", b->controller, b->path) < 0)
561                 return NULL;
562
563         return r;
564 }