1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "unit-name.h"
31 #include "dbus-scope.h"
32 #include "load-dropin.h"
34 static const UnitActiveState state_translation_table
[_SCOPE_STATE_MAX
] = {
35 [SCOPE_DEAD
] = UNIT_INACTIVE
,
36 [SCOPE_RUNNING
] = UNIT_ACTIVE
,
37 [SCOPE_ABANDONED
] = UNIT_ACTIVE
,
38 [SCOPE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
39 [SCOPE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
40 [SCOPE_FAILED
] = UNIT_FAILED
43 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
45 static void scope_init(Unit
*u
) {
49 assert(u
->load_state
== UNIT_STUB
);
51 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
53 UNIT(s
)->ignore_on_isolate
= true;
54 UNIT(s
)->ignore_on_snapshot
= true;
57 static void scope_done(Unit
*u
) {
64 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
67 static int scope_arm_timer(Scope
*s
) {
72 if (s
->timeout_stop_usec
<= 0) {
73 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
77 if (s
->timer_event_source
) {
78 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + s
->timeout_stop_usec
);
82 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
85 r
= sd_event_add_time(
86 UNIT(s
)->manager
->event
,
87 &s
->timer_event_source
,
89 now(CLOCK_MONOTONIC
) + s
->timeout_stop_usec
, 0,
90 scope_dispatch_timer
, s
);
94 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
99 static void scope_set_state(Scope
*s
, ScopeState state
) {
100 ScopeState old_state
;
103 old_state
= s
->state
;
106 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
107 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
109 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
))
110 unit_unwatch_all_pids(UNIT(s
));
112 if (state
!= old_state
)
113 log_debug("%s changed %s -> %s", UNIT(s
)->id
, scope_state_to_string(old_state
), scope_state_to_string(state
));
115 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
118 static int scope_add_default_dependencies(Scope
*s
) {
123 /* Make sure scopes are unloaded on shutdown */
124 r
= unit_add_two_dependencies_by_name(
126 UNIT_BEFORE
, UNIT_CONFLICTS
,
127 SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
134 static int scope_verify(Scope
*s
) {
137 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
140 if (set_isempty(UNIT(s
)->pids
) &&
141 !manager_is_reloading_or_reexecuting(UNIT(s
)->manager
) &&
142 !unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
)) {
143 log_unit_error(UNIT(s
), "Scope has no PIDs. Refusing.");
150 static int scope_load(Unit
*u
) {
155 assert(u
->load_state
== UNIT_STUB
);
157 if (!u
->transient
&& !manager_is_reloading_or_reexecuting(u
->manager
))
160 u
->load_state
= UNIT_LOADED
;
162 r
= unit_load_dropin(u
);
166 r
= unit_patch_contexts(u
);
170 r
= unit_set_default_slice(u
);
174 if (u
->default_dependencies
) {
175 r
= scope_add_default_dependencies(s
);
180 return scope_verify(s
);
183 static int scope_coldplug(Unit
*u
) {
188 assert(s
->state
== SCOPE_DEAD
);
190 if (s
->deserialized_state
!= s
->state
) {
192 if (IN_SET(s
->deserialized_state
, SCOPE_STOP_SIGKILL
, SCOPE_STOP_SIGTERM
)) {
193 r
= scope_arm_timer(s
);
198 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
))
199 unit_watch_all_pids(UNIT(s
));
201 scope_set_state(s
, s
->deserialized_state
);
207 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
214 "%sScope State: %s\n"
216 prefix
, scope_state_to_string(s
->state
),
217 prefix
, scope_result_to_string(s
->result
));
219 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
220 kill_context_dump(&s
->kill_context
, f
, prefix
);
223 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
226 if (f
!= SCOPE_SUCCESS
)
229 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
232 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
233 bool skip_signal
= false;
238 if (f
!= SCOPE_SUCCESS
)
241 unit_watch_all_pids(UNIT(s
));
243 /* If we have a controller set let's ask the controller nicely
244 * to terminate the scope, instead of us going directly into
245 * SIGTERM beserk mode */
246 if (state
== SCOPE_STOP_SIGTERM
)
247 skip_signal
= bus_scope_send_request_stop(s
) > 0;
250 r
= unit_kill_context(
253 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
: KILL_TERMINATE
,
261 r
= scope_arm_timer(s
);
265 scope_set_state(s
, state
);
266 } else if (state
== SCOPE_STOP_SIGTERM
)
267 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
269 scope_enter_dead(s
, SCOPE_SUCCESS
);
274 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
276 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
279 static int scope_start(Unit
*u
) {
285 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
288 if (s
->state
== SCOPE_FAILED
)
291 /* We can't fulfill this right now, please try again later */
292 if (s
->state
== SCOPE_STOP_SIGTERM
||
293 s
->state
== SCOPE_STOP_SIGKILL
)
296 assert(s
->state
== SCOPE_DEAD
);
298 if (!u
->transient
&& !manager_is_reloading_or_reexecuting(u
->manager
))
301 (void) unit_realize_cgroup(u
);
302 (void) unit_reset_cpu_usage(u
);
304 r
= unit_attach_pids_to_cgroup(u
);
306 log_unit_warning_errno(UNIT(s
), r
, "Failed to add PIDs to scope's control group: %m");
307 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
311 s
->result
= SCOPE_SUCCESS
;
313 scope_set_state(s
, SCOPE_RUNNING
);
317 static int scope_stop(Unit
*u
) {
322 if (s
->state
== SCOPE_STOP_SIGTERM
||
323 s
->state
== SCOPE_STOP_SIGKILL
)
326 assert(s
->state
== SCOPE_RUNNING
||
327 s
->state
== SCOPE_ABANDONED
);
329 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
333 static void scope_reset_failed(Unit
*u
) {
338 if (s
->state
== SCOPE_FAILED
)
339 scope_set_state(s
, SCOPE_DEAD
);
341 s
->result
= SCOPE_SUCCESS
;
344 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
345 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
348 static int scope_get_timeout(Unit
*u
, uint64_t *timeout
) {
352 if (!s
->timer_event_source
)
355 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
362 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
369 unit_serialize_item(u
, f
, "state", scope_state_to_string(s
->state
));
373 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
381 if (streq(key
, "state")) {
384 state
= scope_state_from_string(value
);
386 log_unit_debug(u
, "Failed to parse state value: %s", value
);
388 s
->deserialized_state
= state
;
391 log_unit_debug(u
, "Unknown serialization key: %s", key
);
396 static bool scope_check_gc(Unit
*u
) {
399 /* Never clean up scopes that still have a process around,
400 * even if the scope is formally dead. */
402 if (u
->cgroup_path
) {
405 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
);
413 static void scope_notify_cgroup_empty_event(Unit
*u
) {
417 log_unit_debug(u
, "cgroup is empty");
419 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
420 scope_enter_dead(s
, SCOPE_SUCCESS
);
423 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
425 /* If we get a SIGCHLD event for one of the processes we were
426 interested in, then we look for others to watch, under the
427 assumption that we'll sooner or later get a SIGCHLD for
428 them, as the original process we watched was probably the
429 parent of them, and they are hence now our children. */
431 unit_tidy_watch_pids(u
, 0, 0);
432 unit_watch_all_pids(u
);
434 /* If the PID set is empty now, then let's finish this off */
435 if (set_isempty(u
->pids
))
436 scope_notify_cgroup_empty_event(u
);
439 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
440 Scope
*s
= SCOPE(userdata
);
443 assert(s
->timer_event_source
== source
);
447 case SCOPE_STOP_SIGTERM
:
448 if (s
->kill_context
.send_sigkill
) {
449 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
450 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
452 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
453 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
458 case SCOPE_STOP_SIGKILL
:
459 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
460 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
464 assert_not_reached("Timeout at wrong time.");
470 int scope_abandon(Scope
*s
) {
473 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
476 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
479 s
->controller
= mfree(s
->controller
);
481 /* The client is no longer watching the remaining processes,
482 * so let's step in here, under the assumption that the
483 * remaining processes will be sooner or later reassigned to
486 unit_tidy_watch_pids(UNIT(s
), 0, 0);
487 unit_watch_all_pids(UNIT(s
));
489 /* If the PID set is empty now, then let's finish this off */
490 if (set_isempty(UNIT(s
)->pids
))
491 scope_notify_cgroup_empty_event(UNIT(s
));
493 scope_set_state(s
, SCOPE_ABANDONED
);
498 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
501 return state_translation_table
[SCOPE(u
)->state
];
504 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
507 return scope_state_to_string(SCOPE(u
)->state
);
510 static int scope_enumerate(Manager
*m
) {
516 /* Let's unconditionally add the "init.scope" special unit
517 * that encapsulates PID 1. Note that PID 1 already is in the
518 * cgroup for this, we hence just need to allocate the object
519 * for it and that's it. */
521 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
523 u
= unit_new(m
, sizeof(Scope
));
527 r
= unit_add_name(u
, SPECIAL_INIT_SCOPE
);
530 return log_error_errno(r
, "Failed to add init.scope name");
535 u
->default_dependencies
= false;
537 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
538 SCOPE(u
)->kill_context
.kill_signal
= SIGRTMIN
+14;
540 /* Prettify things, if we can. */
542 u
->description
= strdup("System and Service Manager");
543 if (!u
->documentation
)
544 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
546 unit_add_to_load_queue(u
);
547 unit_add_to_dbus_queue(u
);
552 static const char* const scope_state_table
[_SCOPE_STATE_MAX
] = {
553 [SCOPE_DEAD
] = "dead",
554 [SCOPE_RUNNING
] = "running",
555 [SCOPE_ABANDONED
] = "abandoned",
556 [SCOPE_STOP_SIGTERM
] = "stop-sigterm",
557 [SCOPE_STOP_SIGKILL
] = "stop-sigkill",
558 [SCOPE_FAILED
] = "failed",
561 DEFINE_STRING_TABLE_LOOKUP(scope_state
, ScopeState
);
563 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
564 [SCOPE_SUCCESS
] = "success",
565 [SCOPE_FAILURE_RESOURCES
] = "resources",
566 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
569 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
571 const UnitVTable scope_vtable
= {
572 .object_size
= sizeof(Scope
),
573 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
574 .kill_context_offset
= offsetof(Scope
, kill_context
),
580 .private_section
= "Scope",
583 .no_instances
= true,
589 .coldplug
= scope_coldplug
,
593 .start
= scope_start
,
598 .get_timeout
= scope_get_timeout
,
600 .serialize
= scope_serialize
,
601 .deserialize_item
= scope_deserialize_item
,
603 .active_state
= scope_active_state
,
604 .sub_state_to_string
= scope_sub_state_to_string
,
606 .check_gc
= scope_check_gc
,
608 .sigchld_event
= scope_sigchld_event
,
610 .reset_failed
= scope_reset_failed
,
612 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
614 .bus_vtable
= bus_scope_vtable
,
615 .bus_set_property
= bus_scope_set_property
,
616 .bus_commit_properties
= bus_scope_commit_properties
,
618 .can_transient
= true,
620 .enumerate
= scope_enumerate
,