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 "dbus-scope.h"
30 #include "unit-name.h"
31 #include "load-dropin.h"
33 static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
34 [SCOPE_DEAD] = UNIT_INACTIVE,
35 [SCOPE_RUNNING] = UNIT_ACTIVE,
36 [SCOPE_ABANDONED] = UNIT_ACTIVE,
37 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
38 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
39 [SCOPE_FAILED] = UNIT_FAILED
42 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
44 static void scope_init(Unit *u) {
48 assert(u->load_state == UNIT_STUB);
50 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
52 UNIT(s)->ignore_on_isolate = true;
53 UNIT(s)->ignore_on_snapshot = true;
56 static void scope_done(Unit *u) {
63 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
66 static int scope_arm_timer(Scope *s) {
71 if (s->timeout_stop_usec <= 0) {
72 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
76 if (s->timer_event_source) {
77 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_stop_usec);
81 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
84 return sd_event_add_time(
85 UNIT(s)->manager->event,
86 &s->timer_event_source,
88 now(CLOCK_MONOTONIC) + s->timeout_stop_usec, 0,
89 scope_dispatch_timer, s);
92 static void scope_set_state(Scope *s, ScopeState state) {
99 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
100 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
102 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED))
103 unit_unwatch_all_pids(UNIT(s));
105 if (state != old_state)
106 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
108 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
111 static int scope_add_default_dependencies(Scope *s) {
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, NULL, true);
127 static int scope_verify(Scope *s) {
130 if (UNIT(s)->load_state != UNIT_LOADED)
133 if (set_isempty(UNIT(s)->pids) && UNIT(s)->manager->n_reloading <= 0) {
134 log_unit_error(UNIT(s)->id, "Scope %s has no PIDs. Refusing.", UNIT(s)->id);
141 static int scope_load(Unit *u) {
146 assert(u->load_state == UNIT_STUB);
148 if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
151 u->load_state = UNIT_LOADED;
153 r = unit_load_dropin(u);
157 r = unit_patch_contexts(u);
161 r = unit_add_default_slice(u, &s->cgroup_context);
165 if (u->default_dependencies) {
166 r = scope_add_default_dependencies(s);
171 return scope_verify(s);
174 static int scope_coldplug(Unit *u) {
179 assert(s->state == SCOPE_DEAD);
181 if (s->deserialized_state != s->state) {
183 if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) {
184 r = scope_arm_timer(s);
189 if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED))
190 unit_watch_all_pids(UNIT(s));
192 scope_set_state(s, s->deserialized_state);
198 static void scope_dump(Unit *u, FILE *f, const char *prefix) {
205 "%sScope State: %s\n"
207 prefix, scope_state_to_string(s->state),
208 prefix, scope_result_to_string(s->result));
210 cgroup_context_dump(&s->cgroup_context, f, prefix);
211 kill_context_dump(&s->kill_context, f, prefix);
214 static void scope_enter_dead(Scope *s, ScopeResult f) {
217 if (f != SCOPE_SUCCESS)
220 scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
223 static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
224 bool skip_signal = false;
229 if (f != SCOPE_SUCCESS)
232 unit_watch_all_pids(UNIT(s));
234 /* If we have a controller set let's ask the controller nicely
235 * to terminate the scope, instead of us going directly into
236 * SIGTERM beserk mode */
237 if (state == SCOPE_STOP_SIGTERM)
238 skip_signal = bus_scope_send_request_stop(s) > 0;
241 r = unit_kill_context(
244 state != SCOPE_STOP_SIGTERM ? KILL_KILL : KILL_TERMINATE,
252 r = scope_arm_timer(s);
256 scope_set_state(s, state);
257 } else if (state == SCOPE_STOP_SIGTERM)
258 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
260 scope_enter_dead(s, SCOPE_SUCCESS);
265 log_unit_warning(UNIT(s)->id,
266 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
268 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
271 static int scope_start(Unit *u) {
277 if (s->state == SCOPE_FAILED)
280 if (s->state == SCOPE_STOP_SIGTERM ||
281 s->state == SCOPE_STOP_SIGKILL)
284 assert(s->state == SCOPE_DEAD);
286 if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
289 r = unit_attach_pids_to_cgroup(u);
293 s->result = SCOPE_SUCCESS;
295 scope_set_state(s, SCOPE_RUNNING);
299 static int scope_stop(Unit *u) {
304 if (s->state == SCOPE_STOP_SIGTERM ||
305 s->state == SCOPE_STOP_SIGKILL)
308 assert(s->state == SCOPE_RUNNING ||
309 s->state == SCOPE_ABANDONED);
311 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
315 static void scope_reset_failed(Unit *u) {
320 if (s->state == SCOPE_FAILED)
321 scope_set_state(s, SCOPE_DEAD);
323 s->result = SCOPE_SUCCESS;
326 static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
327 return unit_kill_common(u, who, signo, -1, -1, error);
330 static int scope_get_timeout(Unit *u, uint64_t *timeout) {
334 if (!s->timer_event_source)
337 r = sd_event_source_get_time(s->timer_event_source, timeout);
344 static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
351 unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
355 static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
363 if (streq(key, "state")) {
366 state = scope_state_from_string(value);
368 log_debug("Failed to parse state value %s", value);
370 s->deserialized_state = state;
373 log_debug("Unknown serialization key '%s'", key);
378 static bool scope_check_gc(Unit *u) {
381 /* Never clean up scopes that still have a process around,
382 * even if the scope is formally dead. */
384 if (u->cgroup_path) {
387 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
395 static void scope_notify_cgroup_empty_event(Unit *u) {
399 log_unit_debug(u->id, "%s: cgroup is empty", u->id);
401 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
402 scope_enter_dead(s, SCOPE_SUCCESS);
405 static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
407 /* If we get a SIGCHLD event for one of the processes we were
408 interested in, then we look for others to watch, under the
409 assumption that we'll sooner or later get a SIGCHLD for
410 them, as the original process we watched was probably the
411 parent of them, and they are hence now our children. */
413 unit_tidy_watch_pids(u, 0, 0);
414 unit_watch_all_pids(u);
416 /* If the PID set is empty now, then let's finish this off */
417 if (set_isempty(u->pids))
418 scope_notify_cgroup_empty_event(u);
421 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
422 Scope *s = SCOPE(userdata);
425 assert(s->timer_event_source == source);
429 case SCOPE_STOP_SIGTERM:
430 if (s->kill_context.send_sigkill) {
431 log_unit_warning(UNIT(s)->id, "%s stopping timed out. Killing.", UNIT(s)->id);
432 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
434 log_unit_warning(UNIT(s)->id, "%s stopping timed out. Skipping SIGKILL.", UNIT(s)->id);
435 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
440 case SCOPE_STOP_SIGKILL:
441 log_unit_warning(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
442 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
446 assert_not_reached("Timeout at wrong time.");
452 int scope_abandon(Scope *s) {
455 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
459 s->controller = NULL;
461 /* The client is no longer watching the remaining processes,
462 * so let's step in here, under the assumption that the
463 * remaining processes will be sooner or later reassigned to
466 unit_tidy_watch_pids(UNIT(s), 0, 0);
467 unit_watch_all_pids(UNIT(s));
469 /* If the PID set is empty now, then let's finish this off */
470 if (set_isempty(UNIT(s)->pids))
471 scope_notify_cgroup_empty_event(UNIT(s));
473 scope_set_state(s, SCOPE_ABANDONED);
478 _pure_ static UnitActiveState scope_active_state(Unit *u) {
481 return state_translation_table[SCOPE(u)->state];
484 _pure_ static const char *scope_sub_state_to_string(Unit *u) {
487 return scope_state_to_string(SCOPE(u)->state);
490 static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
491 [SCOPE_DEAD] = "dead",
492 [SCOPE_RUNNING] = "running",
493 [SCOPE_ABANDONED] = "abandoned",
494 [SCOPE_STOP_SIGTERM] = "stop-sigterm",
495 [SCOPE_STOP_SIGKILL] = "stop-sigkill",
496 [SCOPE_FAILED] = "failed",
499 DEFINE_STRING_TABLE_LOOKUP(scope_state, ScopeState);
501 static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
502 [SCOPE_SUCCESS] = "success",
503 [SCOPE_FAILURE_RESOURCES] = "resources",
504 [SCOPE_FAILURE_TIMEOUT] = "timeout",
507 DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
509 const UnitVTable scope_vtable = {
510 .object_size = sizeof(Scope),
511 .cgroup_context_offset = offsetof(Scope, cgroup_context),
512 .kill_context_offset = offsetof(Scope, kill_context),
518 .private_section = "Scope",
521 .no_instances = true,
527 .coldplug = scope_coldplug,
531 .start = scope_start,
536 .get_timeout = scope_get_timeout,
538 .serialize = scope_serialize,
539 .deserialize_item = scope_deserialize_item,
541 .active_state = scope_active_state,
542 .sub_state_to_string = scope_sub_state_to_string,
544 .check_gc = scope_check_gc,
546 .sigchld_event = scope_sigchld_event,
548 .reset_failed = scope_reset_failed,
550 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
552 .bus_interface = "org.freedesktop.systemd1.Scope",
553 .bus_vtable = bus_scope_vtable,
554 .bus_set_property = bus_scope_set_property,
555 .bus_commit_properties = bus_scope_commit_properties,
557 .can_transient = true