]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/scope.c
test: skip the relevant test case if systemd-measure is not present
[thirdparty/systemd.git] / src / core / scope.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
6c12b52e
LP
2
3#include <errno.h>
6c12b52e
LP
4#include <unistd.h>
5
b5efdb8a 6#include "alloc-util.h"
07630cea 7#include "dbus-scope.h"
6fcbec6f 8#include "dbus-unit.h"
07630cea 9#include "load-dropin.h"
6c12b52e 10#include "log.h"
428a9f6f 11#include "process-util.h"
5918a933 12#include "random-util.h"
b5efdb8a 13#include "scope.h"
d68c645b 14#include "serialize.h"
6c12b52e 15#include "special.h"
8b43440b 16#include "string-table.h"
07630cea
LP
17#include "string-util.h"
18#include "strv.h"
6c12b52e 19#include "unit-name.h"
efdb0237 20#include "unit.h"
6c12b52e
LP
21
22static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
23 [SCOPE_DEAD] = UNIT_INACTIVE,
24 [SCOPE_RUNNING] = UNIT_ACTIVE,
a911bb9a 25 [SCOPE_ABANDONED] = UNIT_ACTIVE,
6c12b52e
LP
26 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
27 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
28 [SCOPE_FAILED] = UNIT_FAILED
29};
30
718db961
LP
31static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
32
6c12b52e
LP
33static void scope_init(Unit *u) {
34 Scope *s = SCOPE(u);
35
36 assert(u);
37 assert(u->load_state == UNIT_STUB);
38
9ed7de60 39 s->runtime_max_usec = USEC_INFINITY;
1f19a534 40 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
1b4cd0cf 41 u->ignore_on_isolate = true;
6c12b52e
LP
42}
43
44static void scope_done(Unit *u) {
45 Scope *s = SCOPE(u);
46
47 assert(u);
48
371c0b79
LP
49 s->controller = mfree(s->controller);
50 s->controller_track = sd_bus_track_unref(s->controller_track);
2d4a39e7 51
5dcadb4c 52 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
718db961
LP
53}
54
ecea250d 55static usec_t scope_running_timeout(Scope *s) {
5918a933
AB
56 usec_t delta = 0;
57
58 assert(s);
59
60 if (s->runtime_rand_extra_usec != 0) {
61 delta = random_u64_range(s->runtime_rand_extra_usec);
62 log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC));
63 }
64
65 return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic,
66 s->runtime_max_usec),
67 delta);
68}
69
36c16a7c 70static int scope_arm_timer(Scope *s, usec_t usec) {
718db961
LP
71 int r;
72
73 assert(s);
74
718db961 75 if (s->timer_event_source) {
36c16a7c 76 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
77 if (r < 0)
78 return r;
79
80 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
81 }
82
36c16a7c
LP
83 if (usec == USEC_INFINITY)
84 return 0;
85
cbf60d0a 86 r = sd_event_add_time(
6a0f1f6d
LP
87 UNIT(s)->manager->event,
88 &s->timer_event_source,
89 CLOCK_MONOTONIC,
36c16a7c 90 usec, 0,
6a0f1f6d 91 scope_dispatch_timer, s);
7dfbe2e3
TG
92 if (r < 0)
93 return r;
94
95 (void) sd_event_source_set_description(s->timer_event_source, "scope-timer");
96
97 return 0;
6c12b52e
LP
98}
99
100static void scope_set_state(Scope *s, ScopeState state) {
101 ScopeState old_state;
102 assert(s);
103
6fcbec6f
LP
104 if (s->state != state)
105 bus_unit_send_pending_change_signal(UNIT(s), false);
106
6c12b52e
LP
107 old_state = s->state;
108 s->state = state;
109
a911bb9a 110 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
5dcadb4c 111 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
6c12b52e 112
50be4f4a 113 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) {
a911bb9a 114 unit_unwatch_all_pids(UNIT(s));
50be4f4a
LP
115 unit_dequeue_rewatch_pids(UNIT(s));
116 }
a911bb9a 117
6c12b52e 118 if (state != old_state)
a911bb9a 119 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
6c12b52e 120
2ad2e41a 121 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
6c12b52e
LP
122}
123
124static int scope_add_default_dependencies(Scope *s) {
125 int r;
126
127 assert(s);
128
4c9ea260
LP
129 if (!UNIT(s)->default_dependencies)
130 return 0;
131
6c12b52e
LP
132 /* Make sure scopes are unloaded on shutdown */
133 r = unit_add_two_dependencies_by_name(
134 UNIT(s),
135 UNIT_BEFORE, UNIT_CONFLICTS,
5a724170 136 SPECIAL_SHUTDOWN_TARGET, true,
eef85c4a 137 UNIT_DEPENDENCY_DEFAULT);
6c12b52e
LP
138 if (r < 0)
139 return r;
140
141 return 0;
142}
143
144static int scope_verify(Scope *s) {
145 assert(s);
75193d41 146 assert(UNIT(s)->load_state == UNIT_LOADED);
6c12b52e 147
efdb0237 148 if (set_isempty(UNIT(s)->pids) &&
2c289ea8 149 !MANAGER_IS_RELOADING(UNIT(s)->manager) &&
d85ff944
YW
150 !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
151 return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOENT), "Scope has no PIDs. Refusing.");
6c12b52e
LP
152
153 return 0;
154}
155
8e4e851f
LP
156static int scope_load_init_scope(Unit *u) {
157 assert(u);
158
159 if (!unit_has_name(u, SPECIAL_INIT_SCOPE))
160 return 0;
161
162 u->transient = true;
f5869324 163 u->perpetual = true;
8e4e851f
LP
164
165 /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we
166 * synthesize it here, instead of relying on the unit file on disk. */
167
168 u->default_dependencies = false;
8e4e851f
LP
169
170 /* Prettify things, if we can. */
171 if (!u->description)
172 u->description = strdup("System and Service Manager");
173 if (!u->documentation)
174 (void) strv_extend(&u->documentation, "man:systemd(1)");
175
176 return 1;
177}
178
75193d41
ZJS
179static int scope_add_extras(Scope *s) {
180 int r;
181
182 r = unit_patch_contexts(UNIT(s));
183 if (r < 0)
184 return r;
185
186 r = unit_set_default_slice(UNIT(s));
187 if (r < 0)
188 return r;
189
190 return scope_add_default_dependencies(s);
191}
192
6c12b52e
LP
193static int scope_load(Unit *u) {
194 Scope *s = SCOPE(u);
195 int r;
196
197 assert(s);
198 assert(u->load_state == UNIT_STUB);
199
2c289ea8 200 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
4f4afc88 201 /* Refuse to load non-transient scope units, but allow them while reloading. */
6c12b52e
LP
202 return -ENOENT;
203
8e4e851f
LP
204 r = scope_load_init_scope(u);
205 if (r < 0)
206 return r;
c3620770
ZJS
207
208 r = unit_load_fragment_and_dropin(u, false);
6c12b52e
LP
209 if (r < 0)
210 return r;
211
75193d41
ZJS
212 if (u->load_state != UNIT_LOADED)
213 return 0;
6c12b52e 214
75193d41
ZJS
215 r = scope_add_extras(s);
216 if (r < 0)
217 return r;
6c12b52e
LP
218
219 return scope_verify(s);
220}
221
7508f7f2
PW
222static usec_t scope_coldplug_timeout(Scope *s) {
223 assert(s);
224
225 switch (s->deserialized_state) {
226
9ed7de60 227 case SCOPE_RUNNING:
5918a933 228 return scope_running_timeout(s);
9ed7de60 229
7508f7f2
PW
230 case SCOPE_STOP_SIGKILL:
231 case SCOPE_STOP_SIGTERM:
232 return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec);
233
234 default:
235 return USEC_INFINITY;
236 }
237}
238
be847e82 239static int scope_coldplug(Unit *u) {
6c12b52e
LP
240 Scope *s = SCOPE(u);
241 int r;
242
243 assert(s);
244 assert(s->state == SCOPE_DEAD);
245
36c16a7c
LP
246 if (s->deserialized_state == s->state)
247 return 0;
a911bb9a 248
7508f7f2
PW
249 r = scope_arm_timer(s, scope_coldplug_timeout(s));
250 if (r < 0)
251 return r;
6c12b52e 252
428a9f6f
FB
253 if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) {
254 if (u->pids) {
255 void *pidp;
256
257 SET_FOREACH(pidp, u->pids) {
258 r = unit_watch_pid(u, PTR_TO_PID(pidp), false);
259 if (r < 0 && r != -EEXIST)
260 return r;
261 }
262 } else
263 (void) unit_enqueue_rewatch_pids(u);
264 }
36c16a7c 265
371c0b79
LP
266 bus_scope_track_controller(s);
267
36c16a7c 268 scope_set_state(s, s->deserialized_state);
6c12b52e
LP
269 return 0;
270}
271
272static void scope_dump(Unit *u, FILE *f, const char *prefix) {
273 Scope *s = SCOPE(u);
274
275 assert(s);
276 assert(f);
277
278 fprintf(f,
279 "%sScope State: %s\n"
9ed7de60 280 "%sResult: %s\n"
5918a933
AB
281 "%sRuntimeMaxSec: %s\n"
282 "%sRuntimeRandomizedExtraSec: %s\n",
6c12b52e 283 prefix, scope_state_to_string(s->state),
9ed7de60 284 prefix, scope_result_to_string(s->result),
5918a933
AB
285 prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC),
286 prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC));
6c12b52e 287
bc0623df 288 cgroup_context_dump(UNIT(s), f, prefix);
6c12b52e
LP
289 kill_context_dump(&s->kill_context, f, prefix);
290}
291
292static void scope_enter_dead(Scope *s, ScopeResult f) {
293 assert(s);
294
a0fef983 295 if (s->result == SCOPE_SUCCESS)
6c12b52e
LP
296 s->result = f;
297
aac99f30 298 unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result));
6c12b52e
LP
299 scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
300}
301
302static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
2d4a39e7 303 bool skip_signal = false;
6c12b52e
LP
304 int r;
305
306 assert(s);
307
a0fef983 308 if (s->result == SCOPE_SUCCESS)
6c12b52e
LP
309 s->result = f;
310
50be4f4a
LP
311 /* Before sending any signal, make sure we track all members of this cgroup */
312 (void) unit_watch_all_pids(UNIT(s));
313
314 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
315 * died now */
316 (void) unit_enqueue_rewatch_pids(UNIT(s));
a911bb9a 317
371c0b79
LP
318 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
319 * directly into SIGTERM berserk mode */
2d4a39e7
LP
320 if (state == SCOPE_STOP_SIGTERM)
321 skip_signal = bus_scope_send_request_stop(s) > 0;
322
59ec09a8
ZJS
323 if (skip_signal)
324 r = 1; /* wait */
325 else {
2d4a39e7
LP
326 r = unit_kill_context(
327 UNIT(s),
328 &s->kill_context,
3862e809
LP
329 state != SCOPE_STOP_SIGTERM ? KILL_KILL :
330 s->was_abandoned ? KILL_TERMINATE_AND_LOG :
331 KILL_TERMINATE,
2d4a39e7
LP
332 -1, -1, false);
333 if (r < 0)
334 goto fail;
59ec09a8 335 }
6c12b52e
LP
336
337 if (r > 0) {
36c16a7c 338 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
718db961
LP
339 if (r < 0)
340 goto fail;
6c12b52e
LP
341
342 scope_set_state(s, state);
ac84d1fb
LP
343 } else if (state == SCOPE_STOP_SIGTERM)
344 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
345 else
6c12b52e
LP
346 scope_enter_dead(s, SCOPE_SUCCESS);
347
348 return;
349
350fail:
f2341e0a 351 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
6c12b52e
LP
352
353 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
354}
355
356static int scope_start(Unit *u) {
357 Scope *s = SCOPE(u);
358 int r;
359
360 assert(s);
361
efdb0237
LP
362 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
363 return -EPERM;
364
7b617155
LP
365 if (s->state == SCOPE_FAILED)
366 return -EPERM;
367
dd305ec9 368 /* We can't fulfill this right now, please try again later */
3742095b 369 if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
6c12b52e
LP
370 return -EAGAIN;
371
372 assert(s->state == SCOPE_DEAD);
373
2c289ea8 374 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
6c12b52e
LP
375 return -ENOENT;
376
371c0b79
LP
377 (void) bus_scope_track_controller(s);
378
4b58153d
LP
379 r = unit_acquire_invocation_id(u);
380 if (r < 0)
381 return r;
382
5ad096b3 383 (void) unit_realize_cgroup(u);
9b2559a1 384 (void) unit_reset_accounting(u);
5ad096b3 385
01542056 386 unit_export_state_files(u);
d3070fbd 387
01542056 388 r = unit_attach_pids_to_cgroup(u, u->pids, NULL);
dd305ec9 389 if (r < 0) {
01542056 390 log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m");
68a01fb6 391 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
6c12b52e 392 return r;
dd305ec9 393 }
8d3e4ac7 394 if (r == 0) {
e99b9285 395 log_unit_warning(u, "No PIDs left to attach to the scope's control group, refusing.");
8d3e4ac7
LP
396 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
397 return -ECHILD;
398 }
399 log_unit_debug(u, "%i %s added to scope's control group.", r, r == 1 ? "process" : "processes");
6c12b52e 400
6c12b52e
LP
401 s->result = SCOPE_SUCCESS;
402
403 scope_set_state(s, SCOPE_RUNNING);
b91ada2a 404
9ed7de60 405 /* Set the maximum runtime timeout. */
5918a933 406 scope_arm_timer(s, scope_running_timeout(s));
9ed7de60 407
e9eec8b5
FB
408 /* On unified we use proper notifications hence we can unwatch the PIDs
409 * we just attached to the scope. This can also be done on legacy as
410 * we're going to update the list of the processes we watch with the
411 * PIDs currently in the scope anyway. */
412 unit_unwatch_all_pids(u);
413
414 /* Start watching the PIDs currently in the scope (legacy hierarchy only) */
01542056 415 (void) unit_enqueue_rewatch_pids(u);
82a2b6bb 416 return 1;
6c12b52e
LP
417}
418
419static int scope_stop(Unit *u) {
420 Scope *s = SCOPE(u);
421
422 assert(s);
6c12b52e 423
3742095b 424 if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
6c12b52e
LP
425 return 0;
426
3742095b 427 assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED));
6c12b52e
LP
428
429 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
82a2b6bb 430 return 1;
6c12b52e
LP
431}
432
8bcca7e2
LP
433static void scope_reset_failed(Unit *u) {
434 Scope *s = SCOPE(u);
435
436 assert(s);
437
438 if (s->state == SCOPE_FAILED)
439 scope_set_state(s, SCOPE_DEAD);
440
441 s->result = SCOPE_SUCCESS;
442}
443
718db961 444static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
6c12b52e
LP
445 return unit_kill_common(u, who, signo, -1, -1, error);
446}
447
7a7821c8 448static int scope_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 449 Scope *s = SCOPE(u);
7a7821c8 450 usec_t t;
68db7a3b
ZJS
451 int r;
452
453 if (!s->timer_event_source)
454 return 0;
455
7a7821c8 456 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
457 if (r < 0)
458 return r;
7a7821c8
LP
459 if (t == USEC_INFINITY)
460 return 0;
68db7a3b 461
7a7821c8 462 *timeout = t;
68db7a3b
ZJS
463 return 1;
464}
465
6c12b52e
LP
466static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
467 Scope *s = SCOPE(u);
428a9f6f 468 void *pidp;
6c12b52e
LP
469
470 assert(s);
471 assert(f);
472 assert(fds);
473
d68c645b
LP
474 (void) serialize_item(f, "state", scope_state_to_string(s->state));
475 (void) serialize_bool(f, "was-abandoned", s->was_abandoned);
33fe0afe
LP
476
477 if (s->controller)
d68c645b 478 (void) serialize_item(f, "controller", s->controller);
33fe0afe 479
428a9f6f
FB
480 SET_FOREACH(pidp, u->pids)
481 serialize_item_format(f, "pids", PID_FMT, PTR_TO_PID(pidp));
482
6c12b52e
LP
483 return 0;
484}
485
486static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
487 Scope *s = SCOPE(u);
33fe0afe 488 int r;
6c12b52e
LP
489
490 assert(u);
491 assert(key);
492 assert(value);
493 assert(fds);
494
495 if (streq(key, "state")) {
496 ScopeState state;
497
498 state = scope_state_from_string(value);
499 if (state < 0)
f2341e0a 500 log_unit_debug(u, "Failed to parse state value: %s", value);
6c12b52e
LP
501 else
502 s->deserialized_state = state;
503
3862e809
LP
504 } else if (streq(key, "was-abandoned")) {
505 int k;
506
507 k = parse_boolean(value);
508 if (k < 0)
509 log_unit_debug(u, "Failed to parse boolean value: %s", value);
510 else
511 s->was_abandoned = k;
33fe0afe
LP
512 } else if (streq(key, "controller")) {
513
514 r = free_and_strdup(&s->controller, value);
515 if (r < 0)
d68c645b 516 return log_oom();
33fe0afe 517
428a9f6f
FB
518 } else if (streq(key, "pids")) {
519 pid_t pid;
520
521 if (parse_pid(value, &pid) < 0)
522 log_unit_debug(u, "Failed to parse pids value: %s", value);
523 else {
614f57ed 524 r = set_ensure_put(&u->pids, NULL, PID_TO_PTR(pid));
428a9f6f
FB
525 if (r < 0)
526 return r;
527 }
6c12b52e 528 } else
f2341e0a 529 log_unit_debug(u, "Unknown serialization key: %s", key);
6c12b52e
LP
530
531 return 0;
532}
533
a911bb9a
LP
534static void scope_notify_cgroup_empty_event(Unit *u) {
535 Scope *s = SCOPE(u);
536 assert(u);
537
f2341e0a 538 log_unit_debug(u, "cgroup is empty");
a911bb9a
LP
539
540 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
541 scope_enter_dead(s, SCOPE_SUCCESS);
e08dabfe
AZ
542
543 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
544 * up the cgroup earlier and should do it now. */
545 if (IN_SET(s->state, SCOPE_DEAD, SCOPE_FAILED))
546 unit_prune_cgroup(u);
a911bb9a
LP
547}
548
549static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
11aef522 550 assert(u);
a911bb9a 551
11aef522
LP
552 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
553 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
554 * process we watched was probably the parent of them, and they are hence now our children. */
a911bb9a 555
50be4f4a 556 (void) unit_enqueue_rewatch_pids(u);
a911bb9a
LP
557}
558
718db961
LP
559static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
560 Scope *s = SCOPE(userdata);
6c12b52e
LP
561
562 assert(s);
718db961 563 assert(s->timer_event_source == source);
6c12b52e
LP
564
565 switch (s->state) {
566
9ed7de60
PW
567 case SCOPE_RUNNING:
568 log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping.");
569 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT);
570 break;
571
6c12b52e
LP
572 case SCOPE_STOP_SIGTERM:
573 if (s->kill_context.send_sigkill) {
f2341e0a 574 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
6c12b52e
LP
575 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
576 } else {
f2341e0a 577 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
6c12b52e
LP
578 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
579 }
580
581 break;
582
583 case SCOPE_STOP_SIGKILL:
f2341e0a 584 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
6c12b52e
LP
585 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
586 break;
587
588 default:
04499a70 589 assert_not_reached();
6c12b52e 590 }
718db961
LP
591
592 return 0;
6c12b52e
LP
593}
594
a911bb9a
LP
595int scope_abandon(Scope *s) {
596 assert(s);
6c12b52e 597
efdb0237
LP
598 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
599 return -EPERM;
600
a911bb9a
LP
601 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
602 return -ESTALE;
6c12b52e 603
3862e809 604 s->was_abandoned = true;
371c0b79 605
a1e58e8e 606 s->controller = mfree(s->controller);
371c0b79
LP
607 s->controller_track = sd_bus_track_unref(s->controller_track);
608
8cb83266 609 scope_set_state(s, SCOPE_ABANDONED);
6c12b52e 610
50be4f4a
LP
611 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
612 * the remaining processes will be sooner or later reassigned to us as parent. */
613 (void) unit_enqueue_rewatch_pids(UNIT(s));
6c12b52e 614
a911bb9a 615 return 0;
6c12b52e
LP
616}
617
618_pure_ static UnitActiveState scope_active_state(Unit *u) {
619 assert(u);
620
621 return state_translation_table[SCOPE(u)->state];
622}
623
624_pure_ static const char *scope_sub_state_to_string(Unit *u) {
625 assert(u);
626
627 return scope_state_to_string(SCOPE(u)->state);
628}
629
04eb582a 630static void scope_enumerate_perpetual(Manager *m) {
efdb0237
LP
631 Unit *u;
632 int r;
633
634 assert(m);
635
636 /* Let's unconditionally add the "init.scope" special unit
637 * that encapsulates PID 1. Note that PID 1 already is in the
638 * cgroup for this, we hence just need to allocate the object
639 * for it and that's it. */
640
641 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
642 if (!u) {
a581e45a 643 r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
efdb0237 644 if (r < 0) {
a581e45a 645 log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
ba64af90 646 return;
efdb0237
LP
647 }
648 }
649
650 u->transient = true;
f5869324 651 u->perpetual = true;
efdb0237 652 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
efdb0237
LP
653
654 unit_add_to_load_queue(u);
655 unit_add_to_dbus_queue(u);
efdb0237
LP
656}
657
6c12b52e 658static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
48d83e33 659 [SCOPE_SUCCESS] = "success",
6c12b52e 660 [SCOPE_FAILURE_RESOURCES] = "resources",
48d83e33 661 [SCOPE_FAILURE_TIMEOUT] = "timeout",
6c12b52e
LP
662};
663
664DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
665
666const UnitVTable scope_vtable = {
667 .object_size = sizeof(Scope),
718db961
LP
668 .cgroup_context_offset = offsetof(Scope, cgroup_context),
669 .kill_context_offset = offsetof(Scope, kill_context),
670
6c12b52e
LP
671 .sections =
672 "Unit\0"
673 "Scope\0"
674 "Install\0",
6c12b52e 675 .private_section = "Scope",
6c12b52e 676
700e2d63 677 .can_transient = true,
1d9cc876 678 .can_delegate = true,
c80a9a33 679 .can_fail = true,
d4fd1cf2 680 .once_only = true,
4d824a4e 681 .can_set_managed_oom = true,
6c12b52e
LP
682
683 .init = scope_init,
684 .load = scope_load,
685 .done = scope_done,
686
687 .coldplug = scope_coldplug,
688
689 .dump = scope_dump,
690
691 .start = scope_start,
692 .stop = scope_stop,
693
694 .kill = scope_kill,
695
d9e45bc3
MS
696 .freeze = unit_freeze_vtable_common,
697 .thaw = unit_thaw_vtable_common,
698
68db7a3b
ZJS
699 .get_timeout = scope_get_timeout,
700
6c12b52e
LP
701 .serialize = scope_serialize,
702 .deserialize_item = scope_deserialize_item,
703
704 .active_state = scope_active_state,
705 .sub_state_to_string = scope_sub_state_to_string,
706
a911bb9a
LP
707 .sigchld_event = scope_sigchld_event,
708
8bcca7e2
LP
709 .reset_failed = scope_reset_failed,
710
6c12b52e
LP
711 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
712
6c12b52e
LP
713 .bus_set_property = bus_scope_set_property,
714 .bus_commit_properties = bus_scope_commit_properties,
715
04eb582a 716 .enumerate_perpetual = scope_enumerate_perpetual,
6c12b52e 717};