]> git.proxmox.com Git - systemd.git/blame - src/core/scope.c
Imported Upstream version 229
[systemd.git] / src / core / scope.c
CommitLineData
14228c0d
MB
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
14228c0d
MB
21#include <unistd.h>
22
db2df898
MP
23#include "alloc-util.h"
24#include "dbus-scope.h"
25#include "load-dropin.h"
14228c0d 26#include "log.h"
db2df898 27#include "scope.h"
14228c0d 28#include "special.h"
db2df898
MP
29#include "string-table.h"
30#include "string-util.h"
31#include "strv.h"
14228c0d 32#include "unit-name.h"
d9dfd233 33#include "unit.h"
14228c0d
MB
34
35static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
36 [SCOPE_DEAD] = UNIT_INACTIVE,
37 [SCOPE_RUNNING] = UNIT_ACTIVE,
60f067b4 38 [SCOPE_ABANDONED] = UNIT_ACTIVE,
14228c0d
MB
39 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
40 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
41 [SCOPE_FAILED] = UNIT_FAILED
42};
43
60f067b4
JS
44static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
45
14228c0d
MB
46static void scope_init(Unit *u) {
47 Scope *s = SCOPE(u);
48
49 assert(u);
50 assert(u->load_state == UNIT_STUB);
51
60f067b4 52 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
14228c0d
MB
53
54 UNIT(s)->ignore_on_isolate = true;
14228c0d
MB
55}
56
57static void scope_done(Unit *u) {
58 Scope *s = SCOPE(u);
59
60 assert(u);
61
60f067b4
JS
62 free(s->controller);
63
64 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
65}
66
4c89c718 67static int scope_arm_timer(Scope *s, usec_t usec) {
60f067b4
JS
68 int r;
69
70 assert(s);
71
60f067b4 72 if (s->timer_event_source) {
4c89c718 73 r = sd_event_source_set_time(s->timer_event_source, usec);
60f067b4
JS
74 if (r < 0)
75 return r;
14228c0d 76
60f067b4
JS
77 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
78 }
14228c0d 79
4c89c718
MP
80 if (usec == USEC_INFINITY)
81 return 0;
82
e3bff60a 83 r = sd_event_add_time(
60f067b4
JS
84 UNIT(s)->manager->event,
85 &s->timer_event_source,
86 CLOCK_MONOTONIC,
4c89c718 87 usec, 0,
60f067b4 88 scope_dispatch_timer, s);
e3bff60a
MP
89 if (r < 0)
90 return r;
91
92 (void) sd_event_source_set_description(s->timer_event_source, "scope-timer");
93
94 return 0;
14228c0d
MB
95}
96
97static void scope_set_state(Scope *s, ScopeState state) {
98 ScopeState old_state;
99 assert(s);
100
101 old_state = s->state;
102 s->state = state;
103
60f067b4
JS
104 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
105 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
106
107 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED))
108 unit_unwatch_all_pids(UNIT(s));
14228c0d
MB
109
110 if (state != old_state)
60f067b4 111 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
14228c0d
MB
112
113 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
114}
115
116static int scope_add_default_dependencies(Scope *s) {
117 int r;
118
119 assert(s);
120
db2df898
MP
121 if (!UNIT(s)->default_dependencies)
122 return 0;
123
14228c0d
MB
124 /* Make sure scopes are unloaded on shutdown */
125 r = unit_add_two_dependencies_by_name(
126 UNIT(s),
127 UNIT_BEFORE, UNIT_CONFLICTS,
128 SPECIAL_SHUTDOWN_TARGET, NULL, true);
129 if (r < 0)
130 return r;
131
132 return 0;
133}
134
135static int scope_verify(Scope *s) {
136 assert(s);
137
138 if (UNIT(s)->load_state != UNIT_LOADED)
139 return 0;
140
d9dfd233
MP
141 if (set_isempty(UNIT(s)->pids) &&
142 !manager_is_reloading_or_reexecuting(UNIT(s)->manager) &&
143 !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) {
e3bff60a 144 log_unit_error(UNIT(s), "Scope has no PIDs. Refusing.");
14228c0d
MB
145 return -EINVAL;
146 }
147
148 return 0;
149}
150
151static int scope_load(Unit *u) {
152 Scope *s = SCOPE(u);
153 int r;
154
155 assert(s);
156 assert(u->load_state == UNIT_STUB);
157
d9dfd233 158 if (!u->transient && !manager_is_reloading_or_reexecuting(u->manager))
14228c0d
MB
159 return -ENOENT;
160
161 u->load_state = UNIT_LOADED;
162
163 r = unit_load_dropin(u);
164 if (r < 0)
165 return r;
166
60f067b4
JS
167 r = unit_patch_contexts(u);
168 if (r < 0)
169 return r;
170
d9dfd233 171 r = unit_set_default_slice(u);
14228c0d
MB
172 if (r < 0)
173 return r;
174
db2df898
MP
175 r = scope_add_default_dependencies(s);
176 if (r < 0)
177 return r;
14228c0d
MB
178
179 return scope_verify(s);
180}
181
182static int scope_coldplug(Unit *u) {
183 Scope *s = SCOPE(u);
184 int r;
185
186 assert(s);
187 assert(s->state == SCOPE_DEAD);
188
4c89c718
MP
189 if (s->deserialized_state == s->state)
190 return 0;
60f067b4 191
4c89c718
MP
192 if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) {
193 r = scope_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_stop_usec));
194 if (r < 0)
195 return r;
14228c0d
MB
196 }
197
4c89c718
MP
198 if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED))
199 unit_watch_all_pids(UNIT(s));
200
201 scope_set_state(s, s->deserialized_state);
14228c0d
MB
202 return 0;
203}
204
205static void scope_dump(Unit *u, FILE *f, const char *prefix) {
206 Scope *s = SCOPE(u);
207
208 assert(s);
209 assert(f);
210
211 fprintf(f,
212 "%sScope State: %s\n"
213 "%sResult: %s\n",
214 prefix, scope_state_to_string(s->state),
215 prefix, scope_result_to_string(s->result));
216
217 cgroup_context_dump(&s->cgroup_context, f, prefix);
218 kill_context_dump(&s->kill_context, f, prefix);
219}
220
221static void scope_enter_dead(Scope *s, ScopeResult f) {
222 assert(s);
223
224 if (f != SCOPE_SUCCESS)
225 s->result = f;
226
227 scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
228}
229
230static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
60f067b4 231 bool skip_signal = false;
14228c0d
MB
232 int r;
233
234 assert(s);
235
236 if (f != SCOPE_SUCCESS)
237 s->result = f;
238
60f067b4
JS
239 unit_watch_all_pids(UNIT(s));
240
241 /* If we have a controller set let's ask the controller nicely
242 * to terminate the scope, instead of us going directly into
243 * SIGTERM beserk mode */
244 if (state == SCOPE_STOP_SIGTERM)
245 skip_signal = bus_scope_send_request_stop(s) > 0;
246
247 if (!skip_signal) {
248 r = unit_kill_context(
249 UNIT(s),
250 &s->kill_context,
5eef597e 251 state != SCOPE_STOP_SIGTERM ? KILL_KILL : KILL_TERMINATE,
60f067b4
JS
252 -1, -1, false);
253 if (r < 0)
254 goto fail;
255 } else
256 r = 1;
14228c0d
MB
257
258 if (r > 0) {
4c89c718 259 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
60f067b4
JS
260 if (r < 0)
261 goto fail;
14228c0d
MB
262
263 scope_set_state(s, state);
60f067b4
JS
264 } else if (state == SCOPE_STOP_SIGTERM)
265 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
266 else
14228c0d
MB
267 scope_enter_dead(s, SCOPE_SUCCESS);
268
269 return;
270
271fail:
e3bff60a 272 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
14228c0d
MB
273
274 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
275}
276
277static int scope_start(Unit *u) {
278 Scope *s = SCOPE(u);
279 int r;
280
281 assert(s);
282
d9dfd233
MP
283 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
284 return -EPERM;
285
14228c0d
MB
286 if (s->state == SCOPE_FAILED)
287 return -EPERM;
288
e3bff60a 289 /* We can't fulfill this right now, please try again later */
14228c0d
MB
290 if (s->state == SCOPE_STOP_SIGTERM ||
291 s->state == SCOPE_STOP_SIGKILL)
292 return -EAGAIN;
293
294 assert(s->state == SCOPE_DEAD);
295
d9dfd233 296 if (!u->transient && !manager_is_reloading_or_reexecuting(u->manager))
14228c0d
MB
297 return -ENOENT;
298
e3bff60a
MP
299 (void) unit_realize_cgroup(u);
300 (void) unit_reset_cpu_usage(u);
301
f47781d8 302 r = unit_attach_pids_to_cgroup(u);
e3bff60a
MP
303 if (r < 0) {
304 log_unit_warning_errno(UNIT(s), r, "Failed to add PIDs to scope's control group: %m");
305 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
14228c0d 306 return r;
e3bff60a 307 }
14228c0d 308
14228c0d
MB
309 s->result = SCOPE_SUCCESS;
310
311 scope_set_state(s, SCOPE_RUNNING);
e735f4d4 312 return 1;
14228c0d
MB
313}
314
315static int scope_stop(Unit *u) {
316 Scope *s = SCOPE(u);
317
318 assert(s);
14228c0d
MB
319
320 if (s->state == SCOPE_STOP_SIGTERM ||
321 s->state == SCOPE_STOP_SIGKILL)
322 return 0;
323
60f067b4
JS
324 assert(s->state == SCOPE_RUNNING ||
325 s->state == SCOPE_ABANDONED);
14228c0d
MB
326
327 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
e735f4d4 328 return 1;
14228c0d
MB
329}
330
331static void scope_reset_failed(Unit *u) {
332 Scope *s = SCOPE(u);
333
334 assert(s);
335
336 if (s->state == SCOPE_FAILED)
337 scope_set_state(s, SCOPE_DEAD);
338
339 s->result = SCOPE_SUCCESS;
340}
341
60f067b4 342static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
14228c0d
MB
343 return unit_kill_common(u, who, signo, -1, -1, error);
344}
345
4c89c718 346static int scope_get_timeout(Unit *u, usec_t *timeout) {
60f067b4 347 Scope *s = SCOPE(u);
4c89c718 348 usec_t t;
60f067b4
JS
349 int r;
350
351 if (!s->timer_event_source)
352 return 0;
353
4c89c718 354 r = sd_event_source_get_time(s->timer_event_source, &t);
60f067b4
JS
355 if (r < 0)
356 return r;
4c89c718
MP
357 if (t == USEC_INFINITY)
358 return 0;
60f067b4 359
4c89c718 360 *timeout = t;
60f067b4
JS
361 return 1;
362}
363
14228c0d
MB
364static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
365 Scope *s = SCOPE(u);
366
367 assert(s);
368 assert(f);
369 assert(fds);
370
371 unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
372 return 0;
373}
374
375static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
376 Scope *s = SCOPE(u);
377
378 assert(u);
379 assert(key);
380 assert(value);
381 assert(fds);
382
383 if (streq(key, "state")) {
384 ScopeState state;
385
386 state = scope_state_from_string(value);
387 if (state < 0)
e3bff60a 388 log_unit_debug(u, "Failed to parse state value: %s", value);
14228c0d
MB
389 else
390 s->deserialized_state = state;
391
392 } else
e3bff60a 393 log_unit_debug(u, "Unknown serialization key: %s", key);
14228c0d
MB
394
395 return 0;
396}
397
398static bool scope_check_gc(Unit *u) {
f47781d8 399 assert(u);
14228c0d
MB
400
401 /* Never clean up scopes that still have a process around,
402 * even if the scope is formally dead. */
403
db2df898
MP
404 if (!u->cgroup_path)
405 return false;
f47781d8 406
db2df898 407 return cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path) <= 0;
14228c0d
MB
408}
409
60f067b4 410static void scope_notify_cgroup_empty_event(Unit *u) {
14228c0d 411 Scope *s = SCOPE(u);
60f067b4
JS
412 assert(u);
413
e3bff60a 414 log_unit_debug(u, "cgroup is empty");
60f067b4
JS
415
416 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
417 scope_enter_dead(s, SCOPE_SUCCESS);
418}
419
420static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
421
422 /* If we get a SIGCHLD event for one of the processes we were
423 interested in, then we look for others to watch, under the
424 assumption that we'll sooner or later get a SIGCHLD for
425 them, as the original process we watched was probably the
426 parent of them, and they are hence now our children. */
427
428 unit_tidy_watch_pids(u, 0, 0);
429 unit_watch_all_pids(u);
430
431 /* If the PID set is empty now, then let's finish this off */
432 if (set_isempty(u->pids))
433 scope_notify_cgroup_empty_event(u);
434}
435
436static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
437 Scope *s = SCOPE(userdata);
14228c0d
MB
438
439 assert(s);
60f067b4 440 assert(s->timer_event_source == source);
14228c0d
MB
441
442 switch (s->state) {
443
444 case SCOPE_STOP_SIGTERM:
445 if (s->kill_context.send_sigkill) {
e3bff60a 446 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
14228c0d
MB
447 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
448 } else {
e3bff60a 449 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
14228c0d
MB
450 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
451 }
452
453 break;
454
455 case SCOPE_STOP_SIGKILL:
e3bff60a 456 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
14228c0d
MB
457 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
458 break;
459
460 default:
461 assert_not_reached("Timeout at wrong time.");
462 }
60f067b4
JS
463
464 return 0;
14228c0d
MB
465}
466
60f067b4
JS
467int scope_abandon(Scope *s) {
468 assert(s);
14228c0d 469
d9dfd233
MP
470 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
471 return -EPERM;
472
60f067b4
JS
473 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
474 return -ESTALE;
14228c0d 475
6300502b 476 s->controller = mfree(s->controller);
14228c0d 477
60f067b4
JS
478 /* The client is no longer watching the remaining processes,
479 * so let's step in here, under the assumption that the
480 * remaining processes will be sooner or later reassigned to
481 * us as parent. */
14228c0d 482
60f067b4
JS
483 unit_tidy_watch_pids(UNIT(s), 0, 0);
484 unit_watch_all_pids(UNIT(s));
14228c0d 485
60f067b4
JS
486 /* If the PID set is empty now, then let's finish this off */
487 if (set_isempty(UNIT(s)->pids))
488 scope_notify_cgroup_empty_event(UNIT(s));
489 else
490 scope_set_state(s, SCOPE_ABANDONED);
491
492 return 0;
14228c0d
MB
493}
494
495_pure_ static UnitActiveState scope_active_state(Unit *u) {
496 assert(u);
497
498 return state_translation_table[SCOPE(u)->state];
499}
500
501_pure_ static const char *scope_sub_state_to_string(Unit *u) {
502 assert(u);
503
504 return scope_state_to_string(SCOPE(u)->state);
505}
506
db2df898 507static void scope_enumerate(Manager *m) {
d9dfd233
MP
508 Unit *u;
509 int r;
510
511 assert(m);
512
513 /* Let's unconditionally add the "init.scope" special unit
514 * that encapsulates PID 1. Note that PID 1 already is in the
515 * cgroup for this, we hence just need to allocate the object
516 * for it and that's it. */
517
518 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
519 if (!u) {
520 u = unit_new(m, sizeof(Scope));
db2df898
MP
521 if (!u) {
522 log_oom();
523 return;
524 }
d9dfd233
MP
525
526 r = unit_add_name(u, SPECIAL_INIT_SCOPE);
527 if (r < 0) {
528 unit_free(u);
db2df898
MP
529 log_error_errno(r, "Failed to add init.scope name");
530 return;
d9dfd233
MP
531 }
532 }
533
534 u->transient = true;
535 u->default_dependencies = false;
536 u->no_gc = true;
db2df898
MP
537 u->ignore_on_isolate = true;
538 u->refuse_manual_start = true;
539 u->refuse_manual_stop = true;
d9dfd233
MP
540 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
541 SCOPE(u)->kill_context.kill_signal = SIGRTMIN+14;
542
543 /* Prettify things, if we can. */
544 if (!u->description)
545 u->description = strdup("System and Service Manager");
546 if (!u->documentation)
547 (void) strv_extend(&u->documentation, "man:systemd(1)");
548
549 unit_add_to_load_queue(u);
550 unit_add_to_dbus_queue(u);
d9dfd233
MP
551}
552
14228c0d
MB
553static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
554 [SCOPE_SUCCESS] = "success",
555 [SCOPE_FAILURE_RESOURCES] = "resources",
556 [SCOPE_FAILURE_TIMEOUT] = "timeout",
557};
558
559DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
560
561const UnitVTable scope_vtable = {
562 .object_size = sizeof(Scope),
60f067b4
JS
563 .cgroup_context_offset = offsetof(Scope, cgroup_context),
564 .kill_context_offset = offsetof(Scope, kill_context),
565
14228c0d
MB
566 .sections =
567 "Unit\0"
568 "Scope\0"
569 "Install\0",
14228c0d 570 .private_section = "Scope",
14228c0d
MB
571
572 .no_alias = true,
573 .no_instances = true,
db2df898 574 .can_transient = true,
14228c0d
MB
575
576 .init = scope_init,
577 .load = scope_load,
578 .done = scope_done,
579
580 .coldplug = scope_coldplug,
581
582 .dump = scope_dump,
583
584 .start = scope_start,
585 .stop = scope_stop,
586
587 .kill = scope_kill,
588
60f067b4
JS
589 .get_timeout = scope_get_timeout,
590
14228c0d
MB
591 .serialize = scope_serialize,
592 .deserialize_item = scope_deserialize_item,
593
594 .active_state = scope_active_state,
595 .sub_state_to_string = scope_sub_state_to_string,
596
597 .check_gc = scope_check_gc,
598
60f067b4 599 .sigchld_event = scope_sigchld_event,
14228c0d
MB
600
601 .reset_failed = scope_reset_failed,
602
603 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
604
60f067b4 605 .bus_vtable = bus_scope_vtable,
14228c0d
MB
606 .bus_set_property = bus_scope_set_property,
607 .bus_commit_properties = bus_scope_commit_properties,
608
d9dfd233 609 .enumerate = scope_enumerate,
14228c0d 610};