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