]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/scope.c
78aa638ed2c3c205e58ca82ad71be2cd098cfb49
[thirdparty/systemd.git] / src / core / scope.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "cgroup-setup.h"
8 #include "dbus-scope.h"
9 #include "dbus-unit.h"
10 #include "exit-status.h"
11 #include "load-dropin.h"
12 #include "log.h"
13 #include "process-util.h"
14 #include "random-util.h"
15 #include "scope.h"
16 #include "serialize.h"
17 #include "special.h"
18 #include "string-table.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "unit-name.h"
22 #include "unit.h"
23 #include "user-util.h"
24
25 static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
26 [SCOPE_DEAD] = UNIT_INACTIVE,
27 [SCOPE_START_CHOWN] = UNIT_ACTIVATING,
28 [SCOPE_RUNNING] = UNIT_ACTIVE,
29 [SCOPE_ABANDONED] = UNIT_ACTIVE,
30 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
31 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
32 [SCOPE_FAILED] = UNIT_FAILED,
33 };
34
35 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
36
37 static void scope_init(Unit *u) {
38 Scope *s = SCOPE(u);
39
40 assert(u);
41 assert(u->load_state == UNIT_STUB);
42
43 s->runtime_max_usec = USEC_INFINITY;
44 s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec;
45 u->ignore_on_isolate = true;
46 s->user = s->group = NULL;
47 s->oom_policy = _OOM_POLICY_INVALID;
48 }
49
50 static void scope_done(Unit *u) {
51 Scope *s = SCOPE(u);
52
53 assert(u);
54
55 s->controller = mfree(s->controller);
56 s->controller_track = sd_bus_track_unref(s->controller_track);
57
58 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
59
60 s->user = mfree(s->user);
61 s->group = mfree(s->group);
62 }
63
64 static usec_t scope_running_timeout(Scope *s) {
65 usec_t delta = 0;
66
67 assert(s);
68
69 if (s->runtime_rand_extra_usec != 0) {
70 delta = random_u64_range(s->runtime_rand_extra_usec);
71 log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC));
72 }
73
74 return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic,
75 s->runtime_max_usec),
76 delta);
77 }
78
79 static int scope_arm_timer(Scope *s, usec_t usec) {
80 int r;
81
82 assert(s);
83
84 if (s->timer_event_source) {
85 r = sd_event_source_set_time(s->timer_event_source, usec);
86 if (r < 0)
87 return r;
88
89 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
90 }
91
92 if (usec == USEC_INFINITY)
93 return 0;
94
95 r = sd_event_add_time(
96 UNIT(s)->manager->event,
97 &s->timer_event_source,
98 CLOCK_MONOTONIC,
99 usec, 0,
100 scope_dispatch_timer, s);
101 if (r < 0)
102 return r;
103
104 (void) sd_event_source_set_description(s->timer_event_source, "scope-timer");
105
106 return 0;
107 }
108
109 static void scope_set_state(Scope *s, ScopeState state) {
110 ScopeState old_state;
111 assert(s);
112
113 if (s->state != state)
114 bus_unit_send_pending_change_signal(UNIT(s), false);
115
116 old_state = s->state;
117 s->state = state;
118
119 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN, SCOPE_RUNNING))
120 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
121
122 if (!IN_SET(old_state, SCOPE_DEAD, SCOPE_FAILED) && IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) {
123 unit_unwatch_all_pids(UNIT(s));
124 unit_dequeue_rewatch_pids(UNIT(s));
125 }
126
127 if (state != old_state)
128 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
129
130 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
131 }
132
133 static int scope_add_default_dependencies(Scope *s) {
134 int r;
135
136 assert(s);
137
138 if (!UNIT(s)->default_dependencies)
139 return 0;
140
141 /* Make sure scopes are unloaded on shutdown */
142 r = unit_add_two_dependencies_by_name(
143 UNIT(s),
144 UNIT_BEFORE, UNIT_CONFLICTS,
145 SPECIAL_SHUTDOWN_TARGET, true,
146 UNIT_DEPENDENCY_DEFAULT);
147 if (r < 0)
148 return r;
149
150 return 0;
151 }
152
153 static int scope_verify(Scope *s) {
154 assert(s);
155 assert(UNIT(s)->load_state == UNIT_LOADED);
156
157 if (set_isempty(UNIT(s)->pids) &&
158 !MANAGER_IS_RELOADING(UNIT(s)->manager) &&
159 !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
160 return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOENT), "Scope has no PIDs. Refusing.");
161
162 return 0;
163 }
164
165 static int scope_load_init_scope(Unit *u) {
166 assert(u);
167
168 if (!unit_has_name(u, SPECIAL_INIT_SCOPE))
169 return 0;
170
171 u->transient = true;
172 u->perpetual = true;
173
174 /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we
175 * synthesize it here, instead of relying on the unit file on disk. */
176
177 u->default_dependencies = false;
178
179 /* Prettify things, if we can. */
180 if (!u->description)
181 u->description = strdup("System and Service Manager");
182 if (!u->documentation)
183 (void) strv_extend(&u->documentation, "man:systemd(1)");
184
185 return 1;
186 }
187
188 static int scope_add_extras(Scope *s) {
189 int r;
190
191 r = unit_patch_contexts(UNIT(s));
192 if (r < 0)
193 return r;
194
195 r = unit_set_default_slice(UNIT(s));
196 if (r < 0)
197 return r;
198
199 if (s->oom_policy < 0)
200 s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->defaults.oom_policy;
201
202 s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL;
203
204 return scope_add_default_dependencies(s);
205 }
206
207 static int scope_load(Unit *u) {
208 Scope *s = SCOPE(u);
209 int r;
210
211 assert(s);
212 assert(u->load_state == UNIT_STUB);
213
214 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
215 /* Refuse to load non-transient scope units, but allow them while reloading. */
216 return -ENOENT;
217
218 r = scope_load_init_scope(u);
219 if (r < 0)
220 return r;
221
222 r = unit_load_fragment_and_dropin(u, false);
223 if (r < 0)
224 return r;
225
226 if (u->load_state != UNIT_LOADED)
227 return 0;
228
229 r = scope_add_extras(s);
230 if (r < 0)
231 return r;
232
233 return scope_verify(s);
234 }
235
236 static usec_t scope_coldplug_timeout(Scope *s) {
237 assert(s);
238
239 switch (s->deserialized_state) {
240
241 case SCOPE_RUNNING:
242 return scope_running_timeout(s);
243
244 case SCOPE_STOP_SIGKILL:
245 case SCOPE_STOP_SIGTERM:
246 return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec);
247
248 default:
249 return USEC_INFINITY;
250 }
251 }
252
253 static int scope_coldplug(Unit *u) {
254 Scope *s = SCOPE(u);
255 int r;
256
257 assert(s);
258 assert(s->state == SCOPE_DEAD);
259
260 if (s->deserialized_state == s->state)
261 return 0;
262
263 r = scope_arm_timer(s, scope_coldplug_timeout(s));
264 if (r < 0)
265 return r;
266
267 if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) {
268 if (u->pids) {
269 void *pidp;
270
271 SET_FOREACH(pidp, u->pids) {
272 r = unit_watch_pid(u, PTR_TO_PID(pidp), false);
273 if (r < 0 && r != -EEXIST)
274 return r;
275 }
276 } else
277 (void) unit_enqueue_rewatch_pids(u);
278 }
279
280 bus_scope_track_controller(s);
281
282 scope_set_state(s, s->deserialized_state);
283 return 0;
284 }
285
286 static void scope_dump(Unit *u, FILE *f, const char *prefix) {
287 Scope *s = SCOPE(u);
288
289 assert(s);
290 assert(f);
291
292 fprintf(f,
293 "%sScope State: %s\n"
294 "%sResult: %s\n"
295 "%sRuntimeMaxSec: %s\n"
296 "%sRuntimeRandomizedExtraSec: %s\n"
297 "%sOOMPolicy: %s\n",
298 prefix, scope_state_to_string(s->state),
299 prefix, scope_result_to_string(s->result),
300 prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC),
301 prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC),
302 prefix, oom_policy_to_string(s->oom_policy));
303
304 cgroup_context_dump(UNIT(s), f, prefix);
305 kill_context_dump(&s->kill_context, f, prefix);
306 }
307
308 static void scope_enter_dead(Scope *s, ScopeResult f) {
309 assert(s);
310
311 if (s->result == SCOPE_SUCCESS)
312 s->result = f;
313
314 unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result));
315 scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
316 }
317
318 static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
319 bool skip_signal = false;
320 int r;
321
322 assert(s);
323
324 if (s->result == SCOPE_SUCCESS)
325 s->result = f;
326
327 /* Before sending any signal, make sure we track all members of this cgroup */
328 (void) unit_watch_all_pids(UNIT(s));
329
330 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
331 * died now */
332 (void) unit_enqueue_rewatch_pids(UNIT(s));
333
334 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
335 * directly into SIGTERM berserk mode */
336 if (state == SCOPE_STOP_SIGTERM)
337 skip_signal = bus_scope_send_request_stop(s) > 0;
338
339 if (skip_signal)
340 r = 1; /* wait */
341 else {
342 r = unit_kill_context(
343 UNIT(s),
344 &s->kill_context,
345 state != SCOPE_STOP_SIGTERM ? KILL_KILL :
346 s->was_abandoned ? KILL_TERMINATE_AND_LOG :
347 KILL_TERMINATE,
348 /* main_pid= */ NULL,
349 /* control_pid= */ NULL,
350 /* main_pid_alien= */ false);
351 if (r < 0) {
352 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
353 goto fail;
354 }
355 }
356
357 if (r > 0) {
358 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
359 if (r < 0) {
360 log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m");
361 goto fail;
362 }
363
364 scope_set_state(s, state);
365 } else if (state == SCOPE_STOP_SIGTERM)
366 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
367 else
368 scope_enter_dead(s, SCOPE_SUCCESS);
369
370 return;
371
372 fail:
373 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
374 }
375
376 static int scope_enter_start_chown(Scope *s) {
377 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
378 Unit *u = UNIT(s);
379 int r;
380
381 assert(s);
382 assert(s->user);
383
384 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), u->manager->defaults.timeout_start_usec));
385 if (r < 0)
386 return r;
387
388 r = unit_fork_helper_process(u, "(sd-chown-cgroup)", &pidref);
389 if (r < 0)
390 goto fail;
391
392 if (r == 0) {
393 uid_t uid = UID_INVALID;
394 gid_t gid = GID_INVALID;
395
396 if (!isempty(s->user)) {
397 const char *user = s->user;
398
399 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
400 if (r < 0) {
401 log_unit_error_errno(UNIT(s), r, "Failed to resolve user \"%s\": %m", user);
402 _exit(EXIT_USER);
403 }
404 }
405
406 if (!isempty(s->group)) {
407 const char *group = s->group;
408
409 r = get_group_creds(&group, &gid, 0);
410 if (r < 0) {
411 log_unit_error_errno(UNIT(s), r, "Failed to resolve group \"%s\": %m", group);
412 _exit(EXIT_GROUP);
413 }
414 }
415
416 r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, uid, gid);
417 if (r < 0) {
418 log_unit_error_errno(UNIT(s), r, "Failed to adjust control group access: %m");
419 _exit(EXIT_CGROUP);
420 }
421
422 _exit(EXIT_SUCCESS);
423 }
424
425 r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true);
426 if (r < 0)
427 goto fail;
428
429 scope_set_state(s, SCOPE_START_CHOWN);
430
431 return 1;
432 fail:
433 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
434 return r;
435 }
436
437 static int scope_enter_running(Scope *s) {
438 Unit *u = UNIT(s);
439 int r;
440
441 assert(s);
442
443 (void) bus_scope_track_controller(s);
444
445 r = unit_acquire_invocation_id(u);
446 if (r < 0)
447 return r;
448
449 unit_export_state_files(u);
450
451 r = unit_attach_pids_to_cgroup(u, u->pids, NULL);
452 if (r < 0) {
453 log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m");
454 goto fail;
455 }
456 if (r == 0) {
457 r = log_unit_warning_errno(u, SYNTHETIC_ERRNO(ECHILD), "No PIDs left to attach to the scope's control group, refusing.");
458 goto fail;
459 }
460 log_unit_debug(u, "%i %s added to scope's control group.", r, r == 1 ? "process" : "processes");
461
462 s->result = SCOPE_SUCCESS;
463
464 scope_set_state(s, SCOPE_RUNNING);
465
466 /* Set the maximum runtime timeout. */
467 scope_arm_timer(s, scope_running_timeout(s));
468
469 /* On unified we use proper notifications hence we can unwatch the PIDs
470 * we just attached to the scope. This can also be done on legacy as
471 * we're going to update the list of the processes we watch with the
472 * PIDs currently in the scope anyway. */
473 unit_unwatch_all_pids(u);
474
475 /* Start watching the PIDs currently in the scope (legacy hierarchy only) */
476 (void) unit_enqueue_rewatch_pids(u);
477 return 1;
478
479 fail:
480 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
481 return r;
482 }
483
484 static int scope_start(Unit *u) {
485 Scope *s = SCOPE(u);
486
487 assert(s);
488
489 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
490 return -EPERM;
491
492 if (s->state == SCOPE_FAILED)
493 return -EPERM;
494
495 /* We can't fulfill this right now, please try again later */
496 if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
497 return -EAGAIN;
498
499 assert(s->state == SCOPE_DEAD);
500
501 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
502 return -ENOENT;
503
504 (void) unit_realize_cgroup(u);
505 (void) unit_reset_accounting(u);
506
507 /* We check only for User= option to keep behavior consistent with logic for service units,
508 * i.e. having 'Delegate=true Group=foo' w/o specifying User= has no effect. */
509 if (s->user && unit_cgroup_delegate(u))
510 return scope_enter_start_chown(s);
511
512 return scope_enter_running(s);
513 }
514
515 static int scope_stop(Unit *u) {
516 Scope *s = SCOPE(u);
517
518 assert(s);
519
520 if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
521 return 0;
522
523 assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED));
524
525 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
526 return 1;
527 }
528
529 static void scope_reset_failed(Unit *u) {
530 Scope *s = SCOPE(u);
531
532 assert(s);
533
534 if (s->state == SCOPE_FAILED)
535 scope_set_state(s, SCOPE_DEAD);
536
537 s->result = SCOPE_SUCCESS;
538 }
539
540 static int scope_get_timeout(Unit *u, usec_t *timeout) {
541 Scope *s = SCOPE(u);
542 usec_t t;
543 int r;
544
545 if (!s->timer_event_source)
546 return 0;
547
548 r = sd_event_source_get_time(s->timer_event_source, &t);
549 if (r < 0)
550 return r;
551 if (t == USEC_INFINITY)
552 return 0;
553
554 *timeout = t;
555 return 1;
556 }
557
558 static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
559 Scope *s = SCOPE(u);
560 void *pidp;
561
562 assert(s);
563 assert(f);
564 assert(fds);
565
566 (void) serialize_item(f, "state", scope_state_to_string(s->state));
567 (void) serialize_bool(f, "was-abandoned", s->was_abandoned);
568
569 if (s->controller)
570 (void) serialize_item(f, "controller", s->controller);
571
572 SET_FOREACH(pidp, u->pids)
573 serialize_item_format(f, "pids", PID_FMT, PTR_TO_PID(pidp));
574
575 return 0;
576 }
577
578 static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
579 Scope *s = SCOPE(u);
580 int r;
581
582 assert(u);
583 assert(key);
584 assert(value);
585 assert(fds);
586
587 if (streq(key, "state")) {
588 ScopeState state;
589
590 state = scope_state_from_string(value);
591 if (state < 0)
592 log_unit_debug(u, "Failed to parse state value: %s", value);
593 else
594 s->deserialized_state = state;
595
596 } else if (streq(key, "was-abandoned")) {
597 int k;
598
599 k = parse_boolean(value);
600 if (k < 0)
601 log_unit_debug(u, "Failed to parse boolean value: %s", value);
602 else
603 s->was_abandoned = k;
604 } else if (streq(key, "controller")) {
605
606 r = free_and_strdup(&s->controller, value);
607 if (r < 0)
608 return log_oom();
609
610 } else if (streq(key, "pids")) {
611 pid_t pid;
612
613 if (parse_pid(value, &pid) < 0)
614 log_unit_debug(u, "Failed to parse pids value: %s", value);
615 else {
616 r = set_ensure_put(&u->pids, NULL, PID_TO_PTR(pid));
617 if (r < 0)
618 return r;
619 }
620 } else
621 log_unit_debug(u, "Unknown serialization key: %s", key);
622
623 return 0;
624 }
625
626 static void scope_notify_cgroup_empty_event(Unit *u) {
627 Scope *s = SCOPE(u);
628 assert(u);
629
630 log_unit_debug(u, "cgroup is empty");
631
632 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
633 scope_enter_dead(s, SCOPE_SUCCESS);
634 }
635
636 static void scope_notify_cgroup_oom_event(Unit *u, bool managed_oom) {
637 Scope *s = SCOPE(u);
638
639 if (managed_oom)
640 log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd.");
641 else
642 log_unit_debug(u, "Process of control group was killed by the OOM killer.");
643
644 if (s->oom_policy == OOM_CONTINUE)
645 return;
646
647 switch (s->state) {
648
649 case SCOPE_START_CHOWN:
650 case SCOPE_RUNNING:
651 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_OOM_KILL);
652 break;
653
654 case SCOPE_STOP_SIGTERM:
655 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_OOM_KILL);
656 break;
657
658 case SCOPE_STOP_SIGKILL:
659 if (s->result == SCOPE_SUCCESS)
660 s->result = SCOPE_FAILURE_OOM_KILL;
661 break;
662 /* SCOPE_DEAD, SCOPE_ABANDONED, and SCOPE_FAILED end up in default */
663 default:
664 ;
665 }
666 }
667
668 static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
669 Scope *s = SCOPE(u);
670
671 assert(s);
672
673 if (s->state == SCOPE_START_CHOWN) {
674 if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
675 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
676 else
677 scope_enter_running(s);
678 return;
679 }
680
681 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
682 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
683 * process we watched was probably the parent of them, and they are hence now our children. */
684
685 (void) unit_enqueue_rewatch_pids(u);
686 }
687
688 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
689 Scope *s = SCOPE(userdata);
690
691 assert(s);
692 assert(s->timer_event_source == source);
693
694 switch (s->state) {
695
696 case SCOPE_RUNNING:
697 log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping.");
698 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT);
699 break;
700
701 case SCOPE_STOP_SIGTERM:
702 if (s->kill_context.send_sigkill) {
703 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
704 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
705 } else {
706 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
707 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
708 }
709
710 break;
711
712 case SCOPE_STOP_SIGKILL:
713 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
714 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
715 break;
716
717 case SCOPE_START_CHOWN:
718 log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state.");
719 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
720 break;
721
722 default:
723 assert_not_reached();
724 }
725
726 return 0;
727 }
728
729 int scope_abandon(Scope *s) {
730 assert(s);
731
732 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
733 return -EPERM;
734
735 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
736 return -ESTALE;
737
738 s->was_abandoned = true;
739
740 s->controller = mfree(s->controller);
741 s->controller_track = sd_bus_track_unref(s->controller_track);
742
743 scope_set_state(s, SCOPE_ABANDONED);
744
745 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
746 * the remaining processes will be sooner or later reassigned to us as parent. */
747 (void) unit_enqueue_rewatch_pids(UNIT(s));
748
749 return 0;
750 }
751
752 static UnitActiveState scope_active_state(Unit *u) {
753 assert(u);
754
755 return state_translation_table[SCOPE(u)->state];
756 }
757
758 static const char *scope_sub_state_to_string(Unit *u) {
759 assert(u);
760
761 return scope_state_to_string(SCOPE(u)->state);
762 }
763
764 static void scope_enumerate_perpetual(Manager *m) {
765 Unit *u;
766 int r;
767
768 assert(m);
769
770 /* Let's unconditionally add the "init.scope" special unit
771 * that encapsulates PID 1. Note that PID 1 already is in the
772 * cgroup for this, we hence just need to allocate the object
773 * for it and that's it. */
774
775 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
776 if (!u) {
777 r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
778 if (r < 0) {
779 log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
780 return;
781 }
782 }
783
784 u->transient = true;
785 u->perpetual = true;
786 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
787
788 unit_add_to_load_queue(u);
789 unit_add_to_dbus_queue(u);
790 /* Enqueue an explicit cgroup realization here. Unlike other cgroups this one already exists and is
791 * populated (by us, after all!) already, even when we are not in a reload cycle. Hence we cannot
792 * apply the settings at creation time anymore, but let's at least apply them asynchronously. */
793 unit_add_to_cgroup_realize_queue(u);
794 }
795
796 static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
797 [SCOPE_SUCCESS] = "success",
798 [SCOPE_FAILURE_RESOURCES] = "resources",
799 [SCOPE_FAILURE_TIMEOUT] = "timeout",
800 [SCOPE_FAILURE_OOM_KILL] = "oom-kill",
801 };
802
803 DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
804
805 const UnitVTable scope_vtable = {
806 .object_size = sizeof(Scope),
807 .cgroup_context_offset = offsetof(Scope, cgroup_context),
808 .kill_context_offset = offsetof(Scope, kill_context),
809
810 .sections =
811 "Unit\0"
812 "Scope\0"
813 "Install\0",
814 .private_section = "Scope",
815
816 .can_transient = true,
817 .can_delegate = true,
818 .can_fail = true,
819 .once_only = true,
820 .can_set_managed_oom = true,
821
822 .init = scope_init,
823 .load = scope_load,
824 .done = scope_done,
825
826 .coldplug = scope_coldplug,
827
828 .dump = scope_dump,
829
830 .start = scope_start,
831 .stop = scope_stop,
832
833 .freeze = unit_freeze_vtable_common,
834 .thaw = unit_thaw_vtable_common,
835
836 .get_timeout = scope_get_timeout,
837
838 .serialize = scope_serialize,
839 .deserialize_item = scope_deserialize_item,
840
841 .active_state = scope_active_state,
842 .sub_state_to_string = scope_sub_state_to_string,
843
844 .sigchld_event = scope_sigchld_event,
845
846 .reset_failed = scope_reset_failed,
847
848 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
849 .notify_cgroup_oom = scope_notify_cgroup_oom_event,
850
851 .bus_set_property = bus_scope_set_property,
852 .bus_commit_properties = bus_scope_commit_properties,
853
854 .enumerate_perpetual = scope_enumerate_perpetual,
855 };