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