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