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