]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/scope.c
Merge pull request #26438 from poettering/event-source-shorten
[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 "dbus-scope.h"
8 #include "dbus-unit.h"
9 #include "exit-status.h"
10 #include "load-dropin.h"
11 #include "log.h"
12 #include "process-util.h"
13 #include "random-util.h"
14 #include "scope.h"
15 #include "serialize.h"
16 #include "special.h"
17 #include "string-table.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "unit-name.h"
21 #include "unit.h"
22 #include "user-util.h"
23
24 static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
25 [SCOPE_DEAD] = UNIT_INACTIVE,
26 [SCOPE_START_CHOWN] = UNIT_ACTIVATING,
27 [SCOPE_RUNNING] = UNIT_ACTIVE,
28 [SCOPE_ABANDONED] = UNIT_ACTIVE,
29 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
30 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
31 [SCOPE_FAILED] = UNIT_FAILED
32 };
33
34 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
35
36 static void scope_init(Unit *u) {
37 Scope *s = SCOPE(u);
38
39 assert(u);
40 assert(u->load_state == UNIT_STUB);
41
42 s->runtime_max_usec = USEC_INFINITY;
43 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
44 u->ignore_on_isolate = true;
45 s->user = s->group = NULL;
46 s->oom_policy = _OOM_POLICY_INVALID;
47 }
48
49 static void scope_done(Unit *u) {
50 Scope *s = SCOPE(u);
51
52 assert(u);
53
54 s->controller = mfree(s->controller);
55 s->controller_track = sd_bus_track_unref(s->controller_track);
56
57 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
58
59 s->user = mfree(s->user);
60 s->group = mfree(s->group);
61 }
62
63 static usec_t scope_running_timeout(Scope *s) {
64 usec_t delta = 0;
65
66 assert(s);
67
68 if (s->runtime_rand_extra_usec != 0) {
69 delta = random_u64_range(s->runtime_rand_extra_usec);
70 log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC));
71 }
72
73 return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic,
74 s->runtime_max_usec),
75 delta);
76 }
77
78 static int scope_arm_timer(Scope *s, usec_t usec) {
79 int r;
80
81 assert(s);
82
83 if (s->timer_event_source) {
84 r = sd_event_source_set_time(s->timer_event_source, usec);
85 if (r < 0)
86 return r;
87
88 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
89 }
90
91 if (usec == USEC_INFINITY)
92 return 0;
93
94 r = sd_event_add_time(
95 UNIT(s)->manager->event,
96 &s->timer_event_source,
97 CLOCK_MONOTONIC,
98 usec, 0,
99 scope_dispatch_timer, s);
100 if (r < 0)
101 return r;
102
103 (void) sd_event_source_set_description(s->timer_event_source, "scope-timer");
104
105 return 0;
106 }
107
108 static void scope_set_state(Scope *s, ScopeState state) {
109 ScopeState old_state;
110 assert(s);
111
112 if (s->state != state)
113 bus_unit_send_pending_change_signal(UNIT(s), false);
114
115 old_state = s->state;
116 s->state = state;
117
118 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN))
119 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
120
121 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) {
122 unit_unwatch_all_pids(UNIT(s));
123 unit_dequeue_rewatch_pids(UNIT(s));
124 }
125
126 if (state != old_state)
127 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
128
129 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
130 }
131
132 static int scope_add_default_dependencies(Scope *s) {
133 int r;
134
135 assert(s);
136
137 if (!UNIT(s)->default_dependencies)
138 return 0;
139
140 /* Make sure scopes are unloaded on shutdown */
141 r = unit_add_two_dependencies_by_name(
142 UNIT(s),
143 UNIT_BEFORE, UNIT_CONFLICTS,
144 SPECIAL_SHUTDOWN_TARGET, true,
145 UNIT_DEPENDENCY_DEFAULT);
146 if (r < 0)
147 return r;
148
149 return 0;
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 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
633 * up the cgroup earlier and should do it now. */
634 if (IN_SET(s->state, SCOPE_DEAD, SCOPE_FAILED))
635 unit_prune_cgroup(u);
636 }
637
638 static void scope_notify_cgroup_oom_event(Unit *u, bool managed_oom) {
639 Scope *s = SCOPE(u);
640
641 if (managed_oom)
642 log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd.");
643 else
644 log_unit_debug(u, "Process of control group was killed by the OOM killer.");
645
646 if (s->oom_policy == OOM_CONTINUE)
647 return;
648
649 switch (s->state) {
650
651 case SCOPE_START_CHOWN:
652 case SCOPE_RUNNING:
653 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_OOM_KILL);
654 break;
655
656 case SCOPE_STOP_SIGTERM:
657 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_OOM_KILL);
658 break;
659
660 case SCOPE_STOP_SIGKILL:
661 if (s->result == SCOPE_SUCCESS)
662 s->result = SCOPE_FAILURE_OOM_KILL;
663 break;
664 /* SCOPE_DEAD, SCOPE_ABANDONED, and SCOPE_FAILED end up in default */
665 default:
666 ;
667 }
668 }
669
670 static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
671 Scope *s = SCOPE(u);
672
673 assert(s);
674
675 if (s->state == SCOPE_START_CHOWN) {
676 if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
677 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
678 else
679 scope_enter_running(s);
680 return;
681 }
682
683 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
684 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
685 * process we watched was probably the parent of them, and they are hence now our children. */
686
687 (void) unit_enqueue_rewatch_pids(u);
688 }
689
690 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
691 Scope *s = SCOPE(userdata);
692
693 assert(s);
694 assert(s->timer_event_source == source);
695
696 switch (s->state) {
697
698 case SCOPE_RUNNING:
699 log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping.");
700 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT);
701 break;
702
703 case SCOPE_STOP_SIGTERM:
704 if (s->kill_context.send_sigkill) {
705 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
706 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
707 } else {
708 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
709 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
710 }
711
712 break;
713
714 case SCOPE_STOP_SIGKILL:
715 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
716 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
717 break;
718
719 case SCOPE_START_CHOWN:
720 log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state.");
721 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
722 break;
723
724 default:
725 assert_not_reached();
726 }
727
728 return 0;
729 }
730
731 int scope_abandon(Scope *s) {
732 assert(s);
733
734 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
735 return -EPERM;
736
737 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
738 return -ESTALE;
739
740 s->was_abandoned = true;
741
742 s->controller = mfree(s->controller);
743 s->controller_track = sd_bus_track_unref(s->controller_track);
744
745 scope_set_state(s, SCOPE_ABANDONED);
746
747 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
748 * the remaining processes will be sooner or later reassigned to us as parent. */
749 (void) unit_enqueue_rewatch_pids(UNIT(s));
750
751 return 0;
752 }
753
754 _pure_ static UnitActiveState scope_active_state(Unit *u) {
755 assert(u);
756
757 return state_translation_table[SCOPE(u)->state];
758 }
759
760 _pure_ static const char *scope_sub_state_to_string(Unit *u) {
761 assert(u);
762
763 return scope_state_to_string(SCOPE(u)->state);
764 }
765
766 static void scope_enumerate_perpetual(Manager *m) {
767 Unit *u;
768 int r;
769
770 assert(m);
771
772 /* Let's unconditionally add the "init.scope" special unit
773 * that encapsulates PID 1. Note that PID 1 already is in the
774 * cgroup for this, we hence just need to allocate the object
775 * for it and that's it. */
776
777 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
778 if (!u) {
779 r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
780 if (r < 0) {
781 log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
782 return;
783 }
784 }
785
786 u->transient = true;
787 u->perpetual = true;
788 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
789
790 unit_add_to_load_queue(u);
791 unit_add_to_dbus_queue(u);
792 /* Enqueue an explicit cgroup realization here. Unlike other cgroups this one already exists and is
793 * populated (by us, after all!) already, even when we are not in a reload cycle. Hence we cannot
794 * apply the settings at creation time anymore, but let's at least apply them asynchronously. */
795 unit_add_to_cgroup_realize_queue(u);
796 }
797
798 static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
799 [SCOPE_SUCCESS] = "success",
800 [SCOPE_FAILURE_RESOURCES] = "resources",
801 [SCOPE_FAILURE_TIMEOUT] = "timeout",
802 [SCOPE_FAILURE_OOM_KILL] = "oom-kill",
803 };
804
805 DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
806
807 const UnitVTable scope_vtable = {
808 .object_size = sizeof(Scope),
809 .cgroup_context_offset = offsetof(Scope, cgroup_context),
810 .kill_context_offset = offsetof(Scope, kill_context),
811
812 .sections =
813 "Unit\0"
814 "Scope\0"
815 "Install\0",
816 .private_section = "Scope",
817
818 .can_transient = true,
819 .can_delegate = true,
820 .can_fail = true,
821 .once_only = true,
822 .can_set_managed_oom = true,
823
824 .init = scope_init,
825 .load = scope_load,
826 .done = scope_done,
827
828 .coldplug = scope_coldplug,
829
830 .dump = scope_dump,
831
832 .start = scope_start,
833 .stop = scope_stop,
834
835 .kill = scope_kill,
836
837 .freeze = unit_freeze_vtable_common,
838 .thaw = unit_thaw_vtable_common,
839
840 .get_timeout = scope_get_timeout,
841
842 .serialize = scope_serialize,
843 .deserialize_item = scope_deserialize_item,
844
845 .active_state = scope_active_state,
846 .sub_state_to_string = scope_sub_state_to_string,
847
848 .sigchld_event = scope_sigchld_event,
849
850 .reset_failed = scope_reset_failed,
851
852 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
853 .notify_cgroup_oom = scope_notify_cgroup_oom_event,
854
855 .bus_set_property = bus_scope_set_property,
856 .bus_commit_properties = bus_scope_commit_properties,
857
858 .enumerate_perpetual = scope_enumerate_perpetual,
859 };