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"
13 #include "serialize.h"
15 #include "string-table.h"
16 #include "string-util.h"
18 #include "unit-name.h"
21 static const UnitActiveState state_translation_table
[_SCOPE_STATE_MAX
] = {
22 [SCOPE_DEAD
] = UNIT_INACTIVE
,
23 [SCOPE_RUNNING
] = UNIT_ACTIVE
,
24 [SCOPE_ABANDONED
] = UNIT_ACTIVE
,
25 [SCOPE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
26 [SCOPE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
27 [SCOPE_FAILED
] = UNIT_FAILED
30 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
32 static void scope_init(Unit
*u
) {
36 assert(u
->load_state
== UNIT_STUB
);
38 s
->runtime_max_usec
= USEC_INFINITY
;
39 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
40 u
->ignore_on_isolate
= true;
43 static void scope_done(Unit
*u
) {
48 s
->controller
= mfree(s
->controller
);
49 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
51 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
54 static int scope_arm_timer(Scope
*s
, usec_t usec
) {
59 if (s
->timer_event_source
) {
60 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
64 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
67 if (usec
== USEC_INFINITY
)
70 r
= sd_event_add_time(
71 UNIT(s
)->manager
->event
,
72 &s
->timer_event_source
,
75 scope_dispatch_timer
, s
);
79 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
84 static void scope_set_state(Scope
*s
, ScopeState state
) {
88 if (s
->state
!= state
)
89 bus_unit_send_pending_change_signal(UNIT(s
), false);
94 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
95 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
97 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
98 unit_unwatch_all_pids(UNIT(s
));
99 unit_dequeue_rewatch_pids(UNIT(s
));
102 if (state
!= old_state
)
103 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
105 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
108 static int scope_add_default_dependencies(Scope
*s
) {
113 if (!UNIT(s
)->default_dependencies
)
116 /* Make sure scopes are unloaded on shutdown */
117 r
= unit_add_two_dependencies_by_name(
119 UNIT_BEFORE
, UNIT_CONFLICTS
,
120 SPECIAL_SHUTDOWN_TARGET
, true,
121 UNIT_DEPENDENCY_DEFAULT
);
128 static int scope_verify(Scope
*s
) {
130 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
132 if (set_isempty(UNIT(s
)->pids
) &&
133 !MANAGER_IS_RELOADING(UNIT(s
)->manager
) &&
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.");
140 static int scope_load_init_scope(Unit
*u
) {
143 if (!unit_has_name(u
, SPECIAL_INIT_SCOPE
))
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. */
152 u
->default_dependencies
= false;
154 /* Prettify things, if we can. */
156 u
->description
= strdup("System and Service Manager");
157 if (!u
->documentation
)
158 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
163 static int scope_add_extras(Scope
*s
) {
166 r
= unit_patch_contexts(UNIT(s
));
170 r
= unit_set_default_slice(UNIT(s
));
174 return scope_add_default_dependencies(s
);
177 static int scope_load(Unit
*u
) {
182 assert(u
->load_state
== UNIT_STUB
);
184 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
185 /* Refuse to load non-transient scope units, but allow them while reloading. */
188 r
= scope_load_init_scope(u
);
192 r
= unit_load_fragment_and_dropin(u
, false);
196 if (u
->load_state
!= UNIT_LOADED
)
199 r
= scope_add_extras(s
);
203 return scope_verify(s
);
206 static usec_t
scope_coldplug_timeout(Scope
*s
) {
209 switch (s
->deserialized_state
) {
212 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
214 case SCOPE_STOP_SIGKILL
:
215 case SCOPE_STOP_SIGTERM
:
216 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
219 return USEC_INFINITY
;
223 static int scope_coldplug(Unit
*u
) {
228 assert(s
->state
== SCOPE_DEAD
);
230 if (s
->deserialized_state
== s
->state
)
233 r
= scope_arm_timer(s
, scope_coldplug_timeout(s
));
237 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
241 SET_FOREACH(pidp
, u
->pids
) {
242 r
= unit_watch_pid(u
, PTR_TO_PID(pidp
), false);
243 if (r
< 0 && r
!= -EEXIST
)
247 (void) unit_enqueue_rewatch_pids(u
);
250 bus_scope_track_controller(s
);
252 scope_set_state(s
, s
->deserialized_state
);
256 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
258 char buf_runtime
[FORMAT_TIMESPAN_MAX
];
264 "%sScope State: %s\n"
266 "%sRuntimeMaxSec: %s\n",
267 prefix
, scope_state_to_string(s
->state
),
268 prefix
, scope_result_to_string(s
->result
),
269 prefix
, format_timespan(buf_runtime
, sizeof(buf_runtime
), s
->runtime_max_usec
, USEC_PER_SEC
));
271 cgroup_context_dump(UNIT(s
), f
, prefix
);
272 kill_context_dump(&s
->kill_context
, f
, prefix
);
275 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
278 if (s
->result
== SCOPE_SUCCESS
)
281 unit_log_result(UNIT(s
), s
->result
== SCOPE_SUCCESS
, scope_result_to_string(s
->result
));
282 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
285 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
286 bool skip_signal
= false;
291 if (s
->result
== SCOPE_SUCCESS
)
294 /* Before sending any signal, make sure we track all members of this cgroup */
295 (void) unit_watch_all_pids(UNIT(s
));
297 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
299 (void) unit_enqueue_rewatch_pids(UNIT(s
));
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 */
303 if (state
== SCOPE_STOP_SIGTERM
)
304 skip_signal
= bus_scope_send_request_stop(s
) > 0;
309 r
= unit_kill_context(
312 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
:
313 s
->was_abandoned
? KILL_TERMINATE_AND_LOG
:
321 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
325 scope_set_state(s
, state
);
326 } else if (state
== SCOPE_STOP_SIGTERM
)
327 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
329 scope_enter_dead(s
, SCOPE_SUCCESS
);
334 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
336 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
339 static int scope_start(Unit
*u
) {
345 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
348 if (s
->state
== SCOPE_FAILED
)
351 /* We can't fulfill this right now, please try again later */
352 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
355 assert(s
->state
== SCOPE_DEAD
);
357 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
360 (void) bus_scope_track_controller(s
);
362 r
= unit_acquire_invocation_id(u
);
366 (void) unit_realize_cgroup(u
);
367 (void) unit_reset_accounting(u
);
369 unit_export_state_files(u
);
371 r
= unit_attach_pids_to_cgroup(u
, u
->pids
, NULL
);
373 log_unit_warning_errno(u
, r
, "Failed to add PIDs to scope's control group: %m");
374 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
378 /* Now u->pids have been moved into the scope cgroup, it's not needed
380 u
->pids
= set_free(u
->pids
);
382 s
->result
= SCOPE_SUCCESS
;
384 scope_set_state(s
, SCOPE_RUNNING
);
386 /* Set the maximum runtime timeout. */
387 scope_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
389 /* Start watching the PIDs currently in the scope */
390 (void) unit_enqueue_rewatch_pids(u
);
394 static int scope_stop(Unit
*u
) {
399 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
402 assert(IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
));
404 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
408 static void scope_reset_failed(Unit
*u
) {
413 if (s
->state
== SCOPE_FAILED
)
414 scope_set_state(s
, SCOPE_DEAD
);
416 s
->result
= SCOPE_SUCCESS
;
419 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
420 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
423 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
428 if (!s
->timer_event_source
)
431 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
434 if (t
== USEC_INFINITY
)
441 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
449 (void) serialize_item(f
, "state", scope_state_to_string(s
->state
));
450 (void) serialize_bool(f
, "was-abandoned", s
->was_abandoned
);
453 (void) serialize_item(f
, "controller", s
->controller
);
455 SET_FOREACH(pidp
, u
->pids
)
456 serialize_item_format(f
, "pids", PID_FMT
, PTR_TO_PID(pidp
));
461 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
470 if (streq(key
, "state")) {
473 state
= scope_state_from_string(value
);
475 log_unit_debug(u
, "Failed to parse state value: %s", value
);
477 s
->deserialized_state
= state
;
479 } else if (streq(key
, "was-abandoned")) {
482 k
= parse_boolean(value
);
484 log_unit_debug(u
, "Failed to parse boolean value: %s", value
);
486 s
->was_abandoned
= k
;
487 } else if (streq(key
, "controller")) {
489 r
= free_and_strdup(&s
->controller
, value
);
493 } else if (streq(key
, "pids")) {
496 if (parse_pid(value
, &pid
) < 0)
497 log_unit_debug(u
, "Failed to parse pids value: %s", value
);
499 r
= set_ensure_allocated(&u
->pids
, NULL
);
503 r
= set_put(u
->pids
, PID_TO_PTR(pid
));
508 log_unit_debug(u
, "Unknown serialization key: %s", key
);
513 static void scope_notify_cgroup_empty_event(Unit
*u
) {
517 log_unit_debug(u
, "cgroup is empty");
519 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
520 scope_enter_dead(s
, SCOPE_SUCCESS
);
522 /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean
523 * up the cgroup earlier and should do it now. */
524 if (IN_SET(s
->state
, SCOPE_DEAD
, SCOPE_FAILED
))
525 unit_prune_cgroup(u
);
528 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
531 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
532 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
533 * process we watched was probably the parent of them, and they are hence now our children. */
535 (void) unit_enqueue_rewatch_pids(u
);
538 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
539 Scope
*s
= SCOPE(userdata
);
542 assert(s
->timer_event_source
== source
);
547 log_unit_warning(UNIT(s
), "Scope reached runtime time limit. Stopping.");
548 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_FAILURE_TIMEOUT
);
551 case SCOPE_STOP_SIGTERM
:
552 if (s
->kill_context
.send_sigkill
) {
553 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
554 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
556 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
557 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
562 case SCOPE_STOP_SIGKILL
:
563 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
564 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
568 assert_not_reached("Timeout at wrong time.");
574 int scope_abandon(Scope
*s
) {
577 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
580 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
583 s
->was_abandoned
= true;
585 s
->controller
= mfree(s
->controller
);
586 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
588 scope_set_state(s
, SCOPE_ABANDONED
);
590 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
591 * the remaining processes will be sooner or later reassigned to us as parent. */
592 (void) unit_enqueue_rewatch_pids(UNIT(s
));
597 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
600 return state_translation_table
[SCOPE(u
)->state
];
603 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
606 return scope_state_to_string(SCOPE(u
)->state
);
609 static void scope_enumerate_perpetual(Manager
*m
) {
615 /* Let's unconditionally add the "init.scope" special unit
616 * that encapsulates PID 1. Note that PID 1 already is in the
617 * cgroup for this, we hence just need to allocate the object
618 * for it and that's it. */
620 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
622 r
= unit_new_for_name(m
, sizeof(Scope
), SPECIAL_INIT_SCOPE
, &u
);
624 log_error_errno(r
, "Failed to allocate the special " SPECIAL_INIT_SCOPE
" unit: %m");
631 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
633 unit_add_to_load_queue(u
);
634 unit_add_to_dbus_queue(u
);
637 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
638 [SCOPE_SUCCESS
] = "success",
639 [SCOPE_FAILURE_RESOURCES
] = "resources",
640 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
643 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
645 const UnitVTable scope_vtable
= {
646 .object_size
= sizeof(Scope
),
647 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
648 .kill_context_offset
= offsetof(Scope
, kill_context
),
654 .private_section
= "Scope",
656 .can_transient
= true,
657 .can_delegate
= true,
660 .can_set_managed_oom
= true,
666 .coldplug
= scope_coldplug
,
670 .start
= scope_start
,
675 .freeze
= unit_freeze_vtable_common
,
676 .thaw
= unit_thaw_vtable_common
,
678 .get_timeout
= scope_get_timeout
,
680 .serialize
= scope_serialize
,
681 .deserialize_item
= scope_deserialize_item
,
683 .active_state
= scope_active_state
,
684 .sub_state_to_string
= scope_sub_state_to_string
,
686 .sigchld_event
= scope_sigchld_event
,
688 .reset_failed
= scope_reset_failed
,
690 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
692 .bus_set_property
= bus_scope_set_property
,
693 .bus_commit_properties
= bus_scope_commit_properties
,
695 .enumerate_perpetual
= scope_enumerate_perpetual
,