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