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
));
98 if (state
!= old_state
)
99 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
101 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
104 static int scope_add_default_dependencies(Scope
*s
) {
109 if (!UNIT(s
)->default_dependencies
)
112 /* Make sure scopes are unloaded on shutdown */
113 r
= unit_add_two_dependencies_by_name(
115 UNIT_BEFORE
, UNIT_CONFLICTS
,
116 SPECIAL_SHUTDOWN_TARGET
, NULL
, true,
117 UNIT_DEPENDENCY_DEFAULT
);
124 static int scope_verify(Scope
*s
) {
127 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
130 if (set_isempty(UNIT(s
)->pids
) &&
131 !MANAGER_IS_RELOADING(UNIT(s
)->manager
) &&
132 !unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
)) {
133 log_unit_error(UNIT(s
), "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_load(Unit
*u
) {
168 assert(u
->load_state
== UNIT_STUB
);
170 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
171 /* Refuse to load non-transient scope units, but allow them while reloading. */
174 r
= scope_load_init_scope(u
);
177 r
= unit_load_fragment_and_dropin_optional(u
);
181 if (u
->load_state
== UNIT_LOADED
) {
182 r
= unit_patch_contexts(u
);
186 r
= unit_set_default_slice(u
);
190 r
= scope_add_default_dependencies(s
);
195 return scope_verify(s
);
198 static int scope_coldplug(Unit
*u
) {
203 assert(s
->state
== SCOPE_DEAD
);
205 if (s
->deserialized_state
== s
->state
)
208 if (IN_SET(s
->deserialized_state
, SCOPE_STOP_SIGKILL
, SCOPE_STOP_SIGTERM
)) {
209 r
= scope_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
));
214 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
))
215 unit_watch_all_pids(UNIT(s
));
217 bus_scope_track_controller(s
);
219 scope_set_state(s
, s
->deserialized_state
);
223 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
230 "%sScope State: %s\n"
232 prefix
, scope_state_to_string(s
->state
),
233 prefix
, scope_result_to_string(s
->result
));
235 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
236 kill_context_dump(&s
->kill_context
, f
, prefix
);
239 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
242 if (s
->result
== SCOPE_SUCCESS
)
245 if (s
->result
!= SCOPE_SUCCESS
)
246 log_unit_warning(UNIT(s
), "Failed with result '%s'.", scope_result_to_string(s
->result
));
248 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
251 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
252 bool skip_signal
= false;
257 if (s
->result
== SCOPE_SUCCESS
)
260 unit_watch_all_pids(UNIT(s
));
262 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
263 * directly into SIGTERM berserk mode */
264 if (state
== SCOPE_STOP_SIGTERM
)
265 skip_signal
= bus_scope_send_request_stop(s
) > 0;
270 r
= unit_kill_context(
273 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
:
274 s
->was_abandoned
? KILL_TERMINATE_AND_LOG
:
282 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
286 scope_set_state(s
, state
);
287 } else if (state
== SCOPE_STOP_SIGTERM
)
288 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
290 scope_enter_dead(s
, SCOPE_SUCCESS
);
295 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
297 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
300 static int scope_start(Unit
*u
) {
306 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
309 if (s
->state
== SCOPE_FAILED
)
312 /* We can't fulfill this right now, please try again later */
313 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
316 assert(s
->state
== SCOPE_DEAD
);
318 if (!u
->transient
&& !MANAGER_IS_RELOADING(u
->manager
))
321 (void) bus_scope_track_controller(s
);
323 r
= unit_acquire_invocation_id(u
);
327 (void) unit_realize_cgroup(u
);
328 (void) unit_reset_cpu_accounting(u
);
329 (void) unit_reset_ip_accounting(u
);
331 unit_export_state_files(UNIT(s
));
333 r
= unit_attach_pids_to_cgroup(u
, UNIT(s
)->pids
, NULL
);
335 log_unit_warning_errno(UNIT(s
), r
, "Failed to add PIDs to scope's control group: %m");
336 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
340 s
->result
= SCOPE_SUCCESS
;
342 scope_set_state(s
, SCOPE_RUNNING
);
346 static int scope_stop(Unit
*u
) {
351 if (IN_SET(s
->state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
354 assert(IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
));
356 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
360 static void scope_reset_failed(Unit
*u
) {
365 if (s
->state
== SCOPE_FAILED
)
366 scope_set_state(s
, SCOPE_DEAD
);
368 s
->result
= SCOPE_SUCCESS
;
371 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
372 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
375 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
380 if (!s
->timer_event_source
)
383 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
386 if (t
== USEC_INFINITY
)
393 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
400 unit_serialize_item(u
, f
, "state", scope_state_to_string(s
->state
));
401 unit_serialize_item(u
, f
, "was-abandoned", yes_no(s
->was_abandoned
));
404 unit_serialize_item(u
, f
, "controller", s
->controller
);
409 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
418 if (streq(key
, "state")) {
421 state
= scope_state_from_string(value
);
423 log_unit_debug(u
, "Failed to parse state value: %s", value
);
425 s
->deserialized_state
= state
;
427 } else if (streq(key
, "was-abandoned")) {
430 k
= parse_boolean(value
);
432 log_unit_debug(u
, "Failed to parse boolean value: %s", value
);
434 s
->was_abandoned
= k
;
435 } else if (streq(key
, "controller")) {
437 r
= free_and_strdup(&s
->controller
, value
);
442 log_unit_debug(u
, "Unknown serialization key: %s", key
);
447 static void scope_notify_cgroup_empty_event(Unit
*u
) {
451 log_unit_debug(u
, "cgroup is empty");
453 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
454 scope_enter_dead(s
, SCOPE_SUCCESS
);
457 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
461 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
462 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
463 * process we watched was probably the parent of them, and they are hence now our children. */
464 unit_tidy_watch_pids(u
, 0, 0);
465 unit_watch_all_pids(u
);
467 /* If the PID set is empty now, then let's finish this off. */
468 unit_synthesize_cgroup_empty_event(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,
519 * so let's step in here, under the assumption that the
520 * remaining processes will be sooner or later reassigned to
523 unit_tidy_watch_pids(UNIT(s
), 0, 0);
524 unit_watch_all_pids(UNIT(s
));
529 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
532 return state_translation_table
[SCOPE(u
)->state
];
535 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
538 return scope_state_to_string(SCOPE(u
)->state
);
541 static void scope_enumerate(Manager
*m
) {
547 /* Let's unconditionally add the "init.scope" special unit
548 * that encapsulates PID 1. Note that PID 1 already is in the
549 * cgroup for this, we hence just need to allocate the object
550 * for it and that's it. */
552 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
554 r
= unit_new_for_name(m
, sizeof(Scope
), SPECIAL_INIT_SCOPE
, &u
);
556 log_error_errno(r
, "Failed to allocate the special " SPECIAL_INIT_SCOPE
" unit: %m");
563 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
565 unit_add_to_load_queue(u
);
566 unit_add_to_dbus_queue(u
);
569 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
570 [SCOPE_SUCCESS
] = "success",
571 [SCOPE_FAILURE_RESOURCES
] = "resources",
572 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
575 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
577 const UnitVTable scope_vtable
= {
578 .object_size
= sizeof(Scope
),
579 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
580 .kill_context_offset
= offsetof(Scope
, kill_context
),
586 .private_section
= "Scope",
588 .can_transient
= true,
589 .can_delegate
= true,
595 .coldplug
= scope_coldplug
,
599 .start
= scope_start
,
604 .get_timeout
= scope_get_timeout
,
606 .serialize
= scope_serialize
,
607 .deserialize_item
= scope_deserialize_item
,
609 .active_state
= scope_active_state
,
610 .sub_state_to_string
= scope_sub_state_to_string
,
612 .sigchld_event
= scope_sigchld_event
,
614 .reset_failed
= scope_reset_failed
,
616 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
618 .bus_vtable
= bus_scope_vtable
,
619 .bus_set_property
= bus_scope_set_property
,
620 .bus_commit_properties
= bus_scope_commit_properties
,
622 .enumerate
= scope_enumerate
,