1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "dbus-scope.h"
9 #include "load-dropin.h"
11 #include "process-util.h"
12 #include "random-util.h"
14 #include "serialize.h"
16 #include "string-table.h"
17 #include "string-util.h"
19 #include "unit-name.h"
22 static const UnitActiveState state_translation_table
[_SCOPE_STATE_MAX
] = {
23 [SCOPE_DEAD
] = UNIT_INACTIVE
,
24 [SCOPE_RUNNING
] = UNIT_ACTIVE
,
25 [SCOPE_ABANDONED
] = UNIT_ACTIVE
,
26 [SCOPE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
27 [SCOPE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
28 [SCOPE_FAILED
] = UNIT_FAILED
31 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
33 static void scope_init(Unit
*u
) {
37 assert(u
->load_state
== UNIT_STUB
);
39 s
->runtime_max_usec
= USEC_INFINITY
;
40 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
41 u
->ignore_on_isolate
= true;
44 static void scope_done(Unit
*u
) {
49 s
->controller
= mfree(s
->controller
);
50 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
52 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
55 static usec_t
scope_running_timeout(Scope
*s
) {
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
));
65 return usec_add(usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
,
70 static int scope_arm_timer(Scope
*s
, usec_t usec
) {
75 if (s
->timer_event_source
) {
76 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
80 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
83 if (usec
== USEC_INFINITY
)
86 r
= sd_event_add_time(
87 UNIT(s
)->manager
->event
,
88 &s
->timer_event_source
,
91 scope_dispatch_timer
, s
);
95 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
100 static void scope_set_state(Scope
*s
, ScopeState state
) {
101 ScopeState old_state
;
104 if (s
->state
!= state
)
105 bus_unit_send_pending_change_signal(UNIT(s
), false);
107 old_state
= s
->state
;
110 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
111 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
113 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
114 unit_unwatch_all_pids(UNIT(s
));
115 unit_dequeue_rewatch_pids(UNIT(s
));
118 if (state
!= old_state
)
119 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
121 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
124 static int scope_add_default_dependencies(Scope
*s
) {
129 if (!UNIT(s
)->default_dependencies
)
132 /* Make sure scopes are unloaded on shutdown */
133 r
= unit_add_two_dependencies_by_name(
135 UNIT_BEFORE
, UNIT_CONFLICTS
,
136 SPECIAL_SHUTDOWN_TARGET
, true,
137 UNIT_DEPENDENCY_DEFAULT
);
144 static int scope_verify(Scope
*s
) {
146 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
148 if (set_isempty(UNIT(s
)->pids
) &&
149 !MANAGER_IS_RELOADING(UNIT(s
)->manager
) &&
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.");
156 static int scope_load_init_scope(Unit
*u
) {
159 if (!unit_has_name(u
, SPECIAL_INIT_SCOPE
))
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. */
168 u
->default_dependencies
= false;
170 /* Prettify things, if we can. */
172 u
->description
= strdup("System and Service Manager");
173 if (!u
->documentation
)
174 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
179 static int scope_add_extras(Scope
*s
) {
182 r
= unit_patch_contexts(UNIT(s
));
186 r
= unit_set_default_slice(UNIT(s
));
190 return scope_add_default_dependencies(s
);
193 static int scope_load(Unit
*u
) {
198 assert(u
->load_state
== UNIT_STUB
);
200 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
201 /* Refuse to load non-transient scope units, but allow them while reloading. */
204 r
= scope_load_init_scope(u
);
208 r
= unit_load_fragment_and_dropin(u
, false);
212 if (u
->load_state
!= UNIT_LOADED
)
215 r
= scope_add_extras(s
);
219 return scope_verify(s
);
222 static usec_t
scope_coldplug_timeout(Scope
*s
) {
225 switch (s
->deserialized_state
) {
228 return scope_running_timeout(s
);
230 case SCOPE_STOP_SIGKILL
:
231 case SCOPE_STOP_SIGTERM
:
232 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
235 return USEC_INFINITY
;
239 static int scope_coldplug(Unit
*u
) {
244 assert(s
->state
== SCOPE_DEAD
);
246 if (s
->deserialized_state
== s
->state
)
249 r
= scope_arm_timer(s
, scope_coldplug_timeout(s
));
253 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
257 SET_FOREACH(pidp
, u
->pids
) {
258 r
= unit_watch_pid(u
, PTR_TO_PID(pidp
), false);
259 if (r
< 0 && r
!= -EEXIST
)
263 (void) unit_enqueue_rewatch_pids(u
);
266 bus_scope_track_controller(s
);
268 scope_set_state(s
, s
->deserialized_state
);
272 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
279 "%sScope State: %s\n"
281 "%sRuntimeMaxSec: %s\n"
282 "%sRuntimeRandomizedExtraSec: %s\n",
283 prefix
, scope_state_to_string(s
->state
),
284 prefix
, scope_result_to_string(s
->result
),
285 prefix
, FORMAT_TIMESPAN(s
->runtime_max_usec
, USEC_PER_SEC
),
286 prefix
, FORMAT_TIMESPAN(s
->runtime_rand_extra_usec
, USEC_PER_SEC
));
288 cgroup_context_dump(UNIT(s
), f
, prefix
);
289 kill_context_dump(&s
->kill_context
, f
, prefix
);
292 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
295 if (s
->result
== SCOPE_SUCCESS
)
298 unit_log_result(UNIT(s
), s
->result
== SCOPE_SUCCESS
, scope_result_to_string(s
->result
));
299 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
302 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
303 bool skip_signal
= false;
308 if (s
->result
== SCOPE_SUCCESS
)
311 /* Before sending any signal, make sure we track all members of this cgroup */
312 (void) unit_watch_all_pids(UNIT(s
));
314 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
316 (void) unit_enqueue_rewatch_pids(UNIT(s
));
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 */
320 if (state
== SCOPE_STOP_SIGTERM
)
321 skip_signal
= bus_scope_send_request_stop(s
) > 0;
326 r
= unit_kill_context(
329 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
:
330 s
->was_abandoned
? KILL_TERMINATE_AND_LOG
:
338 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
342 scope_set_state(s
, state
);
343 } else if (state
== SCOPE_STOP_SIGTERM
)
344 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
346 scope_enter_dead(s
, SCOPE_SUCCESS
);
351 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
353 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
356 static int scope_start(Unit
*u
) {
362 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
365 if (s
->state
== SCOPE_FAILED
)
368 /* We can't fulfill this right now, please try again later */
369 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
372 assert(s
->state
== SCOPE_DEAD
);
374 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
377 (void) bus_scope_track_controller(s
);
379 r
= unit_acquire_invocation_id(u
);
383 (void) unit_realize_cgroup(u
);
384 (void) unit_reset_accounting(u
);
386 unit_export_state_files(u
);
388 r
= unit_attach_pids_to_cgroup(u
, u
->pids
, NULL
);
390 log_unit_warning_errno(u
, r
, "Failed to add PIDs to scope's control group: %m");
391 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
395 s
->result
= SCOPE_SUCCESS
;
397 scope_set_state(s
, SCOPE_RUNNING
);
399 /* Set the maximum runtime timeout. */
400 scope_arm_timer(s
, scope_running_timeout(s
));
402 /* On unified we use proper notifications hence we can unwatch the PIDs
403 * we just attached to the scope. This can also be done on legacy as
404 * we're going to update the list of the processes we watch with the
405 * PIDs currently in the scope anyway. */
406 unit_unwatch_all_pids(u
);
408 /* Start watching the PIDs currently in the scope (legacy hierarchy only) */
409 (void) unit_enqueue_rewatch_pids(u
);
413 static int scope_stop(Unit
*u
) {
418 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
421 assert(IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
));
423 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
427 static void scope_reset_failed(Unit
*u
) {
432 if (s
->state
== SCOPE_FAILED
)
433 scope_set_state(s
, SCOPE_DEAD
);
435 s
->result
= SCOPE_SUCCESS
;
438 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
439 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
442 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
447 if (!s
->timer_event_source
)
450 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
453 if (t
== USEC_INFINITY
)
460 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
468 (void) serialize_item(f
, "state", scope_state_to_string(s
->state
));
469 (void) serialize_bool(f
, "was-abandoned", s
->was_abandoned
);
472 (void) serialize_item(f
, "controller", s
->controller
);
474 SET_FOREACH(pidp
, u
->pids
)
475 serialize_item_format(f
, "pids", PID_FMT
, PTR_TO_PID(pidp
));
480 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
489 if (streq(key
, "state")) {
492 state
= scope_state_from_string(value
);
494 log_unit_debug(u
, "Failed to parse state value: %s", value
);
496 s
->deserialized_state
= state
;
498 } else if (streq(key
, "was-abandoned")) {
501 k
= parse_boolean(value
);
503 log_unit_debug(u
, "Failed to parse boolean value: %s", value
);
505 s
->was_abandoned
= k
;
506 } else if (streq(key
, "controller")) {
508 r
= free_and_strdup(&s
->controller
, value
);
512 } else if (streq(key
, "pids")) {
515 if (parse_pid(value
, &pid
) < 0)
516 log_unit_debug(u
, "Failed to parse pids value: %s", value
);
518 r
= set_ensure_put(&u
->pids
, NULL
, PID_TO_PTR(pid
));
523 log_unit_debug(u
, "Unknown serialization key: %s", key
);
528 static void scope_notify_cgroup_empty_event(Unit
*u
) {
532 log_unit_debug(u
, "cgroup is empty");
534 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
535 scope_enter_dead(s
, SCOPE_SUCCESS
);
537 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
538 * up the cgroup earlier and should do it now. */
539 if (IN_SET(s
->state
, SCOPE_DEAD
, SCOPE_FAILED
))
540 unit_prune_cgroup(u
);
543 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
546 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
547 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
548 * process we watched was probably the parent of them, and they are hence now our children. */
550 (void) unit_enqueue_rewatch_pids(u
);
553 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
554 Scope
*s
= SCOPE(userdata
);
557 assert(s
->timer_event_source
== source
);
562 log_unit_warning(UNIT(s
), "Scope reached runtime time limit. Stopping.");
563 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_FAILURE_TIMEOUT
);
566 case SCOPE_STOP_SIGTERM
:
567 if (s
->kill_context
.send_sigkill
) {
568 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
569 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
571 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
572 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
577 case SCOPE_STOP_SIGKILL
:
578 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
579 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
583 assert_not_reached();
589 int scope_abandon(Scope
*s
) {
592 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
595 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
598 s
->was_abandoned
= true;
600 s
->controller
= mfree(s
->controller
);
601 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
603 scope_set_state(s
, SCOPE_ABANDONED
);
605 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
606 * the remaining processes will be sooner or later reassigned to us as parent. */
607 (void) unit_enqueue_rewatch_pids(UNIT(s
));
612 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
615 return state_translation_table
[SCOPE(u
)->state
];
618 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
621 return scope_state_to_string(SCOPE(u
)->state
);
624 static void scope_enumerate_perpetual(Manager
*m
) {
630 /* Let's unconditionally add the "init.scope" special unit
631 * that encapsulates PID 1. Note that PID 1 already is in the
632 * cgroup for this, we hence just need to allocate the object
633 * for it and that's it. */
635 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
637 r
= unit_new_for_name(m
, sizeof(Scope
), SPECIAL_INIT_SCOPE
, &u
);
639 log_error_errno(r
, "Failed to allocate the special " SPECIAL_INIT_SCOPE
" unit: %m");
646 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
648 unit_add_to_load_queue(u
);
649 unit_add_to_dbus_queue(u
);
652 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
653 [SCOPE_SUCCESS
] = "success",
654 [SCOPE_FAILURE_RESOURCES
] = "resources",
655 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
658 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
660 const UnitVTable scope_vtable
= {
661 .object_size
= sizeof(Scope
),
662 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
663 .kill_context_offset
= offsetof(Scope
, kill_context
),
669 .private_section
= "Scope",
671 .can_transient
= true,
672 .can_delegate
= true,
675 .can_set_managed_oom
= true,
681 .coldplug
= scope_coldplug
,
685 .start
= scope_start
,
690 .freeze
= unit_freeze_vtable_common
,
691 .thaw
= unit_thaw_vtable_common
,
693 .get_timeout
= scope_get_timeout
,
695 .serialize
= scope_serialize
,
696 .deserialize_item
= scope_deserialize_item
,
698 .active_state
= scope_active_state
,
699 .sub_state_to_string
= scope_sub_state_to_string
,
701 .sigchld_event
= scope_sigchld_event
,
703 .reset_failed
= scope_reset_failed
,
705 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
707 .bus_set_property
= bus_scope_set_property
,
708 .bus_commit_properties
= bus_scope_commit_properties
,
710 .enumerate_perpetual
= scope_enumerate_perpetual
,