1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "dbus-scope.h"
8 #include "load-dropin.h"
11 #include "serialize.h"
13 #include "string-table.h"
14 #include "string-util.h"
16 #include "unit-name.h"
19 static const UnitActiveState state_translation_table
[_SCOPE_STATE_MAX
] = {
20 [SCOPE_DEAD
] = UNIT_INACTIVE
,
21 [SCOPE_RUNNING
] = UNIT_ACTIVE
,
22 [SCOPE_ABANDONED
] = UNIT_ACTIVE
,
23 [SCOPE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
24 [SCOPE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
25 [SCOPE_FAILED
] = UNIT_FAILED
28 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
30 static void scope_init(Unit
*u
) {
34 assert(u
->load_state
== UNIT_STUB
);
36 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
37 u
->ignore_on_isolate
= true;
40 static void scope_done(Unit
*u
) {
45 s
->controller
= mfree(s
->controller
);
46 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
48 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
51 static int scope_arm_timer(Scope
*s
, usec_t usec
) {
56 if (s
->timer_event_source
) {
57 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
61 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
64 if (usec
== USEC_INFINITY
)
67 r
= sd_event_add_time(
68 UNIT(s
)->manager
->event
,
69 &s
->timer_event_source
,
72 scope_dispatch_timer
, s
);
76 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
81 static void scope_set_state(Scope
*s
, ScopeState state
) {
88 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
89 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
91 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
92 unit_unwatch_all_pids(UNIT(s
));
93 unit_dequeue_rewatch_pids(UNIT(s
));
96 if (state
!= old_state
)
97 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
99 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
102 static int scope_add_default_dependencies(Scope
*s
) {
107 if (!UNIT(s
)->default_dependencies
)
110 /* Make sure scopes are unloaded on shutdown */
111 r
= unit_add_two_dependencies_by_name(
113 UNIT_BEFORE
, UNIT_CONFLICTS
,
114 SPECIAL_SHUTDOWN_TARGET
, true,
115 UNIT_DEPENDENCY_DEFAULT
);
122 static int scope_verify(Scope
*s
) {
125 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
128 if (set_isempty(UNIT(s
)->pids
) &&
129 !MANAGER_IS_RELOADING(UNIT(s
)->manager
) &&
130 !unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
)) {
131 log_unit_error(UNIT(s
), "Scope has no PIDs. Refusing.");
138 static int scope_load_init_scope(Unit
*u
) {
141 if (!unit_has_name(u
, SPECIAL_INIT_SCOPE
))
147 /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we
148 * synthesize it here, instead of relying on the unit file on disk. */
150 u
->default_dependencies
= false;
152 /* Prettify things, if we can. */
154 u
->description
= strdup("System and Service Manager");
155 if (!u
->documentation
)
156 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
161 static int scope_load(Unit
*u
) {
166 assert(u
->load_state
== UNIT_STUB
);
168 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
169 /* Refuse to load non-transient scope units, but allow them while reloading. */
172 r
= scope_load_init_scope(u
);
175 r
= unit_load_fragment_and_dropin_optional(u
);
179 if (u
->load_state
== UNIT_LOADED
) {
180 r
= unit_patch_contexts(u
);
184 r
= unit_set_default_slice(u
);
188 r
= scope_add_default_dependencies(s
);
193 return scope_verify(s
);
196 static int scope_coldplug(Unit
*u
) {
201 assert(s
->state
== SCOPE_DEAD
);
203 if (s
->deserialized_state
== s
->state
)
206 if (IN_SET(s
->deserialized_state
, SCOPE_STOP_SIGKILL
, SCOPE_STOP_SIGTERM
)) {
207 r
= scope_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
));
212 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
))
213 (void) unit_enqueue_rewatch_pids(u
);
215 bus_scope_track_controller(s
);
217 scope_set_state(s
, s
->deserialized_state
);
221 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
228 "%sScope State: %s\n"
230 prefix
, scope_state_to_string(s
->state
),
231 prefix
, scope_result_to_string(s
->result
));
233 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
234 kill_context_dump(&s
->kill_context
, f
, prefix
);
237 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
240 if (s
->result
== SCOPE_SUCCESS
)
243 unit_log_result(UNIT(s
), s
->result
== SCOPE_SUCCESS
, scope_result_to_string(s
->result
));
244 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
247 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
248 bool skip_signal
= false;
253 if (s
->result
== SCOPE_SUCCESS
)
256 /* Before sending any signal, make sure we track all members of this cgroup */
257 (void) unit_watch_all_pids(UNIT(s
));
259 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
261 (void) unit_enqueue_rewatch_pids(UNIT(s
));
263 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
264 * directly into SIGTERM berserk mode */
265 if (state
== SCOPE_STOP_SIGTERM
)
266 skip_signal
= bus_scope_send_request_stop(s
) > 0;
271 r
= unit_kill_context(
274 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
:
275 s
->was_abandoned
? KILL_TERMINATE_AND_LOG
:
283 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
287 scope_set_state(s
, state
);
288 } else if (state
== SCOPE_STOP_SIGTERM
)
289 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
291 scope_enter_dead(s
, SCOPE_SUCCESS
);
296 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
298 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
301 static int scope_start(Unit
*u
) {
307 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
310 if (s
->state
== SCOPE_FAILED
)
313 /* We can't fulfill this right now, please try again later */
314 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
317 assert(s
->state
== SCOPE_DEAD
);
319 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
322 (void) bus_scope_track_controller(s
);
324 r
= unit_acquire_invocation_id(u
);
328 (void) unit_realize_cgroup(u
);
329 (void) unit_reset_cpu_accounting(u
);
330 (void) unit_reset_ip_accounting(u
);
332 unit_export_state_files(UNIT(s
));
334 r
= unit_attach_pids_to_cgroup(u
, UNIT(s
)->pids
, NULL
);
336 log_unit_warning_errno(UNIT(s
), r
, "Failed to add PIDs to scope's control group: %m");
337 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
341 s
->result
= SCOPE_SUCCESS
;
343 scope_set_state(s
, SCOPE_RUNNING
);
345 /* Start watching the PIDs currently in the scope */
346 (void) unit_enqueue_rewatch_pids(UNIT(s
));
350 static int scope_stop(Unit
*u
) {
355 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
358 assert(IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
));
360 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
364 static void scope_reset_failed(Unit
*u
) {
369 if (s
->state
== SCOPE_FAILED
)
370 scope_set_state(s
, SCOPE_DEAD
);
372 s
->result
= SCOPE_SUCCESS
;
375 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
376 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
379 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
384 if (!s
->timer_event_source
)
387 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
390 if (t
== USEC_INFINITY
)
397 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
404 (void) serialize_item(f
, "state", scope_state_to_string(s
->state
));
405 (void) serialize_bool(f
, "was-abandoned", s
->was_abandoned
);
408 (void) serialize_item(f
, "controller", s
->controller
);
413 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
422 if (streq(key
, "state")) {
425 state
= scope_state_from_string(value
);
427 log_unit_debug(u
, "Failed to parse state value: %s", value
);
429 s
->deserialized_state
= state
;
431 } else if (streq(key
, "was-abandoned")) {
434 k
= parse_boolean(value
);
436 log_unit_debug(u
, "Failed to parse boolean value: %s", value
);
438 s
->was_abandoned
= k
;
439 } else if (streq(key
, "controller")) {
441 r
= free_and_strdup(&s
->controller
, value
);
446 log_unit_debug(u
, "Unknown serialization key: %s", key
);
451 static void scope_notify_cgroup_empty_event(Unit
*u
) {
455 log_unit_debug(u
, "cgroup is empty");
457 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
458 scope_enter_dead(s
, SCOPE_SUCCESS
);
461 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
464 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
465 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
466 * process we watched was probably the parent of them, and they are hence now our children. */
468 (void) unit_enqueue_rewatch_pids(u
);
471 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
472 Scope
*s
= SCOPE(userdata
);
475 assert(s
->timer_event_source
== source
);
479 case SCOPE_STOP_SIGTERM
:
480 if (s
->kill_context
.send_sigkill
) {
481 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
482 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
484 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
485 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
490 case SCOPE_STOP_SIGKILL
:
491 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
492 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
496 assert_not_reached("Timeout at wrong time.");
502 int scope_abandon(Scope
*s
) {
505 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
508 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
511 s
->was_abandoned
= true;
513 s
->controller
= mfree(s
->controller
);
514 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
516 scope_set_state(s
, SCOPE_ABANDONED
);
518 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
519 * the remaining processes will be sooner or later reassigned to us as parent. */
520 (void) unit_enqueue_rewatch_pids(UNIT(s
));
525 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
528 return state_translation_table
[SCOPE(u
)->state
];
531 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
534 return scope_state_to_string(SCOPE(u
)->state
);
537 static void scope_enumerate_perpetual(Manager
*m
) {
543 /* Let's unconditionally add the "init.scope" special unit
544 * that encapsulates PID 1. Note that PID 1 already is in the
545 * cgroup for this, we hence just need to allocate the object
546 * for it and that's it. */
548 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
550 r
= unit_new_for_name(m
, sizeof(Scope
), SPECIAL_INIT_SCOPE
, &u
);
552 log_error_errno(r
, "Failed to allocate the special " SPECIAL_INIT_SCOPE
" unit: %m");
559 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
561 unit_add_to_load_queue(u
);
562 unit_add_to_dbus_queue(u
);
565 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
566 [SCOPE_SUCCESS
] = "success",
567 [SCOPE_FAILURE_RESOURCES
] = "resources",
568 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
571 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
573 const UnitVTable scope_vtable
= {
574 .object_size
= sizeof(Scope
),
575 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
576 .kill_context_offset
= offsetof(Scope
, kill_context
),
582 .private_section
= "Scope",
584 .can_transient
= true,
585 .can_delegate
= true,
592 .coldplug
= scope_coldplug
,
596 .start
= scope_start
,
601 .get_timeout
= scope_get_timeout
,
603 .serialize
= scope_serialize
,
604 .deserialize_item
= scope_deserialize_item
,
606 .active_state
= scope_active_state
,
607 .sub_state_to_string
= scope_sub_state_to_string
,
609 .sigchld_event
= scope_sigchld_event
,
611 .reset_failed
= scope_reset_failed
,
613 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
615 .bus_vtable
= bus_scope_vtable
,
616 .bus_set_property
= bus_scope_set_property
,
617 .bus_commit_properties
= bus_scope_commit_properties
,
619 .enumerate_perpetual
= scope_enumerate_perpetual
,