1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "dbus-scope.h"
9 #include "load-dropin.h"
12 #include "serialize.h"
14 #include "string-table.h"
15 #include "string-util.h"
17 #include "unit-name.h"
20 static const UnitActiveState state_translation_table
[_SCOPE_STATE_MAX
] = {
21 [SCOPE_DEAD
] = UNIT_INACTIVE
,
22 [SCOPE_RUNNING
] = UNIT_ACTIVE
,
23 [SCOPE_ABANDONED
] = UNIT_ACTIVE
,
24 [SCOPE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
25 [SCOPE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
26 [SCOPE_FAILED
] = UNIT_FAILED
29 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
31 static void scope_init(Unit
*u
) {
35 assert(u
->load_state
== UNIT_STUB
);
37 s
->runtime_max_usec
= USEC_INFINITY
;
38 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
39 u
->ignore_on_isolate
= true;
42 static void scope_done(Unit
*u
) {
47 s
->controller
= mfree(s
->controller
);
48 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
50 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
53 static int scope_arm_timer(Scope
*s
, usec_t usec
) {
58 if (s
->timer_event_source
) {
59 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
63 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
66 if (usec
== USEC_INFINITY
)
69 r
= sd_event_add_time(
70 UNIT(s
)->manager
->event
,
71 &s
->timer_event_source
,
74 scope_dispatch_timer
, s
);
78 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
83 static void scope_set_state(Scope
*s
, ScopeState state
) {
87 if (s
->state
!= state
)
88 bus_unit_send_pending_change_signal(UNIT(s
), false);
93 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
94 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
96 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
97 unit_unwatch_all_pids(UNIT(s
));
98 unit_dequeue_rewatch_pids(UNIT(s
));
101 if (state
!= old_state
)
102 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
104 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
107 static int scope_add_default_dependencies(Scope
*s
) {
112 if (!UNIT(s
)->default_dependencies
)
115 /* Make sure scopes are unloaded on shutdown */
116 r
= unit_add_two_dependencies_by_name(
118 UNIT_BEFORE
, UNIT_CONFLICTS
,
119 SPECIAL_SHUTDOWN_TARGET
, true,
120 UNIT_DEPENDENCY_DEFAULT
);
127 static int scope_verify(Scope
*s
) {
129 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
131 if (set_isempty(UNIT(s
)->pids
) &&
132 !MANAGER_IS_RELOADING(UNIT(s
)->manager
) &&
133 !unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
)) {
134 log_unit_error(UNIT(s
), "Scope has no PIDs. Refusing.");
141 static int scope_load_init_scope(Unit
*u
) {
144 if (!unit_has_name(u
, SPECIAL_INIT_SCOPE
))
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. */
153 u
->default_dependencies
= false;
155 /* Prettify things, if we can. */
157 u
->description
= strdup("System and Service Manager");
158 if (!u
->documentation
)
159 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
164 static int scope_add_extras(Scope
*s
) {
167 r
= unit_patch_contexts(UNIT(s
));
171 r
= unit_set_default_slice(UNIT(s
));
175 return scope_add_default_dependencies(s
);
178 static int scope_load(Unit
*u
) {
183 assert(u
->load_state
== UNIT_STUB
);
185 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
186 /* Refuse to load non-transient scope units, but allow them while reloading. */
189 r
= scope_load_init_scope(u
);
193 r
= unit_load_fragment_and_dropin(u
, false);
197 if (u
->load_state
!= UNIT_LOADED
)
200 r
= scope_add_extras(s
);
204 return scope_verify(s
);
207 static usec_t
scope_coldplug_timeout(Scope
*s
) {
210 switch (s
->deserialized_state
) {
213 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
215 case SCOPE_STOP_SIGKILL
:
216 case SCOPE_STOP_SIGTERM
:
217 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
220 return USEC_INFINITY
;
224 static int scope_coldplug(Unit
*u
) {
229 assert(s
->state
== SCOPE_DEAD
);
231 if (s
->deserialized_state
== s
->state
)
234 r
= scope_arm_timer(s
, scope_coldplug_timeout(s
));
238 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
))
239 (void) unit_enqueue_rewatch_pids(u
);
241 bus_scope_track_controller(s
);
243 scope_set_state(s
, s
->deserialized_state
);
247 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
249 char buf_runtime
[FORMAT_TIMESPAN_MAX
];
255 "%sScope State: %s\n"
257 "%sRuntimeMaxSec: %s\n",
258 prefix
, scope_state_to_string(s
->state
),
259 prefix
, scope_result_to_string(s
->result
),
260 prefix
, format_timespan(buf_runtime
, sizeof(buf_runtime
), s
->runtime_max_usec
, USEC_PER_SEC
));
262 cgroup_context_dump(UNIT(s
), f
, prefix
);
263 kill_context_dump(&s
->kill_context
, f
, prefix
);
266 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
269 if (s
->result
== SCOPE_SUCCESS
)
272 unit_log_result(UNIT(s
), s
->result
== SCOPE_SUCCESS
, scope_result_to_string(s
->result
));
273 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
276 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
277 bool skip_signal
= false;
282 if (s
->result
== SCOPE_SUCCESS
)
285 /* Before sending any signal, make sure we track all members of this cgroup */
286 (void) unit_watch_all_pids(UNIT(s
));
288 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
290 (void) unit_enqueue_rewatch_pids(UNIT(s
));
292 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
293 * directly into SIGTERM berserk mode */
294 if (state
== SCOPE_STOP_SIGTERM
)
295 skip_signal
= bus_scope_send_request_stop(s
) > 0;
300 r
= unit_kill_context(
303 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
:
304 s
->was_abandoned
? KILL_TERMINATE_AND_LOG
:
312 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
316 scope_set_state(s
, state
);
317 } else if (state
== SCOPE_STOP_SIGTERM
)
318 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
320 scope_enter_dead(s
, SCOPE_SUCCESS
);
325 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
327 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
330 static int scope_start(Unit
*u
) {
336 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
339 if (s
->state
== SCOPE_FAILED
)
342 /* We can't fulfill this right now, please try again later */
343 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
346 assert(s
->state
== SCOPE_DEAD
);
348 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
351 (void) bus_scope_track_controller(s
);
353 r
= unit_acquire_invocation_id(u
);
357 (void) unit_realize_cgroup(u
);
358 (void) unit_reset_accounting(u
);
360 unit_export_state_files(u
);
362 r
= unit_attach_pids_to_cgroup(u
, u
->pids
, NULL
);
364 log_unit_warning_errno(u
, r
, "Failed to add PIDs to scope's control group: %m");
365 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
369 s
->result
= SCOPE_SUCCESS
;
371 scope_set_state(s
, SCOPE_RUNNING
);
373 /* Set the maximum runtime timeout. */
374 scope_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
376 /* Start watching the PIDs currently in the scope */
377 (void) unit_enqueue_rewatch_pids(u
);
381 static int scope_stop(Unit
*u
) {
386 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
389 assert(IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
));
391 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
395 static void scope_reset_failed(Unit
*u
) {
400 if (s
->state
== SCOPE_FAILED
)
401 scope_set_state(s
, SCOPE_DEAD
);
403 s
->result
= SCOPE_SUCCESS
;
406 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
407 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
410 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
415 if (!s
->timer_event_source
)
418 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
421 if (t
== USEC_INFINITY
)
428 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
435 (void) serialize_item(f
, "state", scope_state_to_string(s
->state
));
436 (void) serialize_bool(f
, "was-abandoned", s
->was_abandoned
);
439 (void) serialize_item(f
, "controller", s
->controller
);
444 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
453 if (streq(key
, "state")) {
456 state
= scope_state_from_string(value
);
458 log_unit_debug(u
, "Failed to parse state value: %s", value
);
460 s
->deserialized_state
= state
;
462 } else if (streq(key
, "was-abandoned")) {
465 k
= parse_boolean(value
);
467 log_unit_debug(u
, "Failed to parse boolean value: %s", value
);
469 s
->was_abandoned
= k
;
470 } else if (streq(key
, "controller")) {
472 r
= free_and_strdup(&s
->controller
, value
);
477 log_unit_debug(u
, "Unknown serialization key: %s", key
);
482 static void scope_notify_cgroup_empty_event(Unit
*u
) {
486 log_unit_debug(u
, "cgroup is empty");
488 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
489 scope_enter_dead(s
, SCOPE_SUCCESS
);
491 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
492 * up the cgroup earlier and should do it now. */
493 if (IN_SET(s
->state
, SCOPE_DEAD
, SCOPE_FAILED
))
494 unit_prune_cgroup(u
);
497 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
500 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
501 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
502 * process we watched was probably the parent of them, and they are hence now our children. */
504 (void) unit_enqueue_rewatch_pids(u
);
507 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
508 Scope
*s
= SCOPE(userdata
);
511 assert(s
->timer_event_source
== source
);
516 log_unit_warning(UNIT(s
), "Scope reached runtime time limit. Stopping.");
517 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_FAILURE_TIMEOUT
);
520 case SCOPE_STOP_SIGTERM
:
521 if (s
->kill_context
.send_sigkill
) {
522 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
523 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
525 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
526 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
531 case SCOPE_STOP_SIGKILL
:
532 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
533 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
537 assert_not_reached("Timeout at wrong time.");
543 int scope_abandon(Scope
*s
) {
546 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
549 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
552 s
->was_abandoned
= true;
554 s
->controller
= mfree(s
->controller
);
555 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
557 scope_set_state(s
, SCOPE_ABANDONED
);
559 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
560 * the remaining processes will be sooner or later reassigned to us as parent. */
561 (void) unit_enqueue_rewatch_pids(UNIT(s
));
566 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
569 return state_translation_table
[SCOPE(u
)->state
];
572 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
575 return scope_state_to_string(SCOPE(u
)->state
);
578 static void scope_enumerate_perpetual(Manager
*m
) {
584 /* Let's unconditionally add the "init.scope" special unit
585 * that encapsulates PID 1. Note that PID 1 already is in the
586 * cgroup for this, we hence just need to allocate the object
587 * for it and that's it. */
589 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
591 r
= unit_new_for_name(m
, sizeof(Scope
), SPECIAL_INIT_SCOPE
, &u
);
593 log_error_errno(r
, "Failed to allocate the special " SPECIAL_INIT_SCOPE
" unit: %m");
600 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
602 unit_add_to_load_queue(u
);
603 unit_add_to_dbus_queue(u
);
606 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
607 [SCOPE_SUCCESS
] = "success",
608 [SCOPE_FAILURE_RESOURCES
] = "resources",
609 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
612 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
614 const UnitVTable scope_vtable
= {
615 .object_size
= sizeof(Scope
),
616 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
617 .kill_context_offset
= offsetof(Scope
, kill_context
),
623 .private_section
= "Scope",
625 .can_transient
= true,
626 .can_delegate
= true,
629 .can_set_managed_oom
= true,
635 .coldplug
= scope_coldplug
,
639 .start
= scope_start
,
644 .freeze
= unit_freeze_vtable_common
,
645 .thaw
= unit_thaw_vtable_common
,
647 .get_timeout
= scope_get_timeout
,
649 .serialize
= scope_serialize
,
650 .deserialize_item
= scope_deserialize_item
,
652 .active_state
= scope_active_state
,
653 .sub_state_to_string
= scope_sub_state_to_string
,
655 .sigchld_event
= scope_sigchld_event
,
657 .reset_failed
= scope_reset_failed
,
659 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
661 .bus_set_property
= bus_scope_set_property
,
662 .bus_commit_properties
= bus_scope_commit_properties
,
664 .enumerate_perpetual
= scope_enumerate_perpetual
,