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