1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
11 #include "alloc-util.h"
12 #include "dbus-scope.h"
13 #include "load-dropin.h"
17 #include "string-table.h"
18 #include "string-util.h"
20 #include "unit-name.h"
23 static const UnitActiveState state_translation_table
[_SCOPE_STATE_MAX
] = {
24 [SCOPE_DEAD
] = UNIT_INACTIVE
,
25 [SCOPE_RUNNING
] = UNIT_ACTIVE
,
26 [SCOPE_ABANDONED
] = UNIT_ACTIVE
,
27 [SCOPE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
28 [SCOPE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
29 [SCOPE_FAILED
] = UNIT_FAILED
32 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
34 static void scope_init(Unit
*u
) {
38 assert(u
->load_state
== UNIT_STUB
);
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_unref(s
->timer_event_source
);
55 static int scope_arm_timer(Scope
*s
, usec_t usec
) {
60 if (s
->timer_event_source
) {
61 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
65 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
68 if (usec
== USEC_INFINITY
)
71 r
= sd_event_add_time(
72 UNIT(s
)->manager
->event
,
73 &s
->timer_event_source
,
76 scope_dispatch_timer
, s
);
80 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
85 static void scope_set_state(Scope
*s
, ScopeState state
) {
92 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
93 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
95 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
)) {
96 unit_unwatch_all_pids(UNIT(s
));
97 unit_dequeue_rewatch_pids(UNIT(s
));
100 if (state
!= old_state
)
101 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
103 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
106 static int scope_add_default_dependencies(Scope
*s
) {
111 if (!UNIT(s
)->default_dependencies
)
114 /* Make sure scopes are unloaded on shutdown */
115 r
= unit_add_two_dependencies_by_name(
117 UNIT_BEFORE
, UNIT_CONFLICTS
,
118 SPECIAL_SHUTDOWN_TARGET
, NULL
, true,
119 UNIT_DEPENDENCY_DEFAULT
);
126 static int scope_verify(Scope
*s
) {
129 if (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 log_unit_error(UNIT(s
), "Scope has no PIDs. Refusing.");
142 static int scope_load_init_scope(Unit
*u
) {
145 if (!unit_has_name(u
, SPECIAL_INIT_SCOPE
))
151 /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we
152 * synthesize it here, instead of relying on the unit file on disk. */
154 u
->default_dependencies
= false;
156 /* Prettify things, if we can. */
158 u
->description
= strdup("System and Service Manager");
159 if (!u
->documentation
)
160 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
165 static int scope_load(Unit
*u
) {
170 assert(u
->load_state
== UNIT_STUB
);
172 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
173 /* Refuse to load non-transient scope units, but allow them while reloading. */
176 r
= scope_load_init_scope(u
);
179 r
= unit_load_fragment_and_dropin_optional(u
);
183 if (u
->load_state
== UNIT_LOADED
) {
184 r
= unit_patch_contexts(u
);
188 r
= unit_set_default_slice(u
);
192 r
= scope_add_default_dependencies(s
);
197 return scope_verify(s
);
200 static int scope_coldplug(Unit
*u
) {
205 assert(s
->state
== SCOPE_DEAD
);
207 if (s
->deserialized_state
== s
->state
)
210 if (IN_SET(s
->deserialized_state
, SCOPE_STOP_SIGKILL
, SCOPE_STOP_SIGTERM
)) {
211 r
= scope_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
));
216 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
))
217 (void) unit_enqueue_rewatch_pids(u
);
219 bus_scope_track_controller(s
);
221 scope_set_state(s
, s
->deserialized_state
);
225 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
232 "%sScope State: %s\n"
234 prefix
, scope_state_to_string(s
->state
),
235 prefix
, scope_result_to_string(s
->result
));
237 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
238 kill_context_dump(&s
->kill_context
, f
, prefix
);
241 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
244 if (s
->result
== SCOPE_SUCCESS
)
247 if (s
->result
!= SCOPE_SUCCESS
)
248 log_unit_warning(UNIT(s
), "Failed with result '%s'.", scope_result_to_string(s
->result
));
250 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
253 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
254 bool skip_signal
= false;
259 if (s
->result
== SCOPE_SUCCESS
)
262 /* Before sending any signal, make sure we track all members of this cgroup */
263 (void) unit_watch_all_pids(UNIT(s
));
265 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
267 (void) unit_enqueue_rewatch_pids(UNIT(s
));
269 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
270 * directly into SIGTERM berserk mode */
271 if (state
== SCOPE_STOP_SIGTERM
)
272 skip_signal
= bus_scope_send_request_stop(s
) > 0;
277 r
= unit_kill_context(
280 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
:
281 s
->was_abandoned
? KILL_TERMINATE_AND_LOG
:
289 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
293 scope_set_state(s
, state
);
294 } else if (state
== SCOPE_STOP_SIGTERM
)
295 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
297 scope_enter_dead(s
, SCOPE_SUCCESS
);
302 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
304 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
307 static int scope_start(Unit
*u
) {
313 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
316 if (s
->state
== SCOPE_FAILED
)
319 /* We can't fulfill this right now, please try again later */
320 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
323 assert(s
->state
== SCOPE_DEAD
);
325 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
328 (void) bus_scope_track_controller(s
);
330 r
= unit_acquire_invocation_id(u
);
334 (void) unit_realize_cgroup(u
);
335 (void) unit_reset_cpu_accounting(u
);
336 (void) unit_reset_ip_accounting(u
);
338 unit_export_state_files(UNIT(s
));
340 r
= unit_attach_pids_to_cgroup(u
, UNIT(s
)->pids
, NULL
);
342 log_unit_warning_errno(UNIT(s
), r
, "Failed to add PIDs to scope's control group: %m");
343 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
347 s
->result
= SCOPE_SUCCESS
;
349 scope_set_state(s
, SCOPE_RUNNING
);
351 /* Start watching the PIDs currently in the scope */
352 (void) unit_enqueue_rewatch_pids(UNIT(s
));
356 static int scope_stop(Unit
*u
) {
361 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
364 assert(IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
));
366 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
370 static void scope_reset_failed(Unit
*u
) {
375 if (s
->state
== SCOPE_FAILED
)
376 scope_set_state(s
, SCOPE_DEAD
);
378 s
->result
= SCOPE_SUCCESS
;
381 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
382 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
385 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
390 if (!s
->timer_event_source
)
393 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
396 if (t
== USEC_INFINITY
)
403 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
410 unit_serialize_item(u
, f
, "state", scope_state_to_string(s
->state
));
411 unit_serialize_item(u
, f
, "was-abandoned", yes_no(s
->was_abandoned
));
414 unit_serialize_item(u
, f
, "controller", s
->controller
);
419 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
428 if (streq(key
, "state")) {
431 state
= scope_state_from_string(value
);
433 log_unit_debug(u
, "Failed to parse state value: %s", value
);
435 s
->deserialized_state
= state
;
437 } else if (streq(key
, "was-abandoned")) {
440 k
= parse_boolean(value
);
442 log_unit_debug(u
, "Failed to parse boolean value: %s", value
);
444 s
->was_abandoned
= k
;
445 } else if (streq(key
, "controller")) {
447 r
= free_and_strdup(&s
->controller
, value
);
452 log_unit_debug(u
, "Unknown serialization key: %s", key
);
457 static void scope_notify_cgroup_empty_event(Unit
*u
) {
461 log_unit_debug(u
, "cgroup is empty");
463 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
464 scope_enter_dead(s
, SCOPE_SUCCESS
);
467 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
470 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
471 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
472 * process we watched was probably the parent of them, and they are hence now our children. */
474 (void) unit_enqueue_rewatch_pids(u
);
477 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
478 Scope
*s
= SCOPE(userdata
);
481 assert(s
->timer_event_source
== source
);
485 case SCOPE_STOP_SIGTERM
:
486 if (s
->kill_context
.send_sigkill
) {
487 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
488 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
490 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
491 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
496 case SCOPE_STOP_SIGKILL
:
497 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
498 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
502 assert_not_reached("Timeout at wrong time.");
508 int scope_abandon(Scope
*s
) {
511 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
514 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
517 s
->was_abandoned
= true;
519 s
->controller
= mfree(s
->controller
);
520 s
->controller_track
= sd_bus_track_unref(s
->controller_track
);
522 scope_set_state(s
, SCOPE_ABANDONED
);
524 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
525 * the remaining processes will be sooner or later reassigned to us as parent. */
526 (void) unit_enqueue_rewatch_pids(UNIT(s
));
531 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
534 return state_translation_table
[SCOPE(u
)->state
];
537 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
540 return scope_state_to_string(SCOPE(u
)->state
);
543 static void scope_enumerate_perpetual(Manager
*m
) {
549 /* Let's unconditionally add the "init.scope" special unit
550 * that encapsulates PID 1. Note that PID 1 already is in the
551 * cgroup for this, we hence just need to allocate the object
552 * for it and that's it. */
554 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
556 r
= unit_new_for_name(m
, sizeof(Scope
), SPECIAL_INIT_SCOPE
, &u
);
558 log_error_errno(r
, "Failed to allocate the special " SPECIAL_INIT_SCOPE
" unit: %m");
565 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
567 unit_add_to_load_queue(u
);
568 unit_add_to_dbus_queue(u
);
571 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
572 [SCOPE_SUCCESS
] = "success",
573 [SCOPE_FAILURE_RESOURCES
] = "resources",
574 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
577 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
579 const UnitVTable scope_vtable
= {
580 .object_size
= sizeof(Scope
),
581 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
582 .kill_context_offset
= offsetof(Scope
, kill_context
),
588 .private_section
= "Scope",
590 .can_transient
= true,
591 .can_delegate
= true,
598 .coldplug
= scope_coldplug
,
602 .start
= scope_start
,
607 .get_timeout
= scope_get_timeout
,
609 .serialize
= scope_serialize
,
610 .deserialize_item
= scope_deserialize_item
,
612 .active_state
= scope_active_state
,
613 .sub_state_to_string
= scope_sub_state_to_string
,
615 .sigchld_event
= scope_sigchld_event
,
617 .reset_failed
= scope_reset_failed
,
619 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
621 .bus_vtable
= bus_scope_vtable
,
622 .bus_set_property
= bus_scope_set_property
,
623 .bus_commit_properties
= bus_scope_commit_properties
,
625 .enumerate_perpetual
= scope_enumerate_perpetual
,