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