X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcore%2Fscope.c;h=be2a6370239f0762c327dcb6606100602c019d05;hp=50e5dbacb4d7fa3936b27d7732cb5341b3b42948;hb=26abdc73a212b90f7c4b71808a1028d2e87ab09f;hpb=13b84ec7df103ce388910a2b868fe1668c1e27ef diff --git a/src/core/scope.c b/src/core/scope.c index 50e5dbacb..be2a63702 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -35,24 +35,27 @@ static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = { [SCOPE_DEAD] = UNIT_INACTIVE, [SCOPE_RUNNING] = UNIT_ACTIVE, + [SCOPE_ABANDONED] = UNIT_ACTIVE, [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING, [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING, [SCOPE_FAILED] = UNIT_FAILED }; +static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); + static void scope_init(Unit *u) { Scope *s = SCOPE(u); assert(u); assert(u->load_state == UNIT_STUB); - s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC; - - watch_init(&s->timer_watch); + s->timeout_stop_usec = u->manager->default_timeout_stop_usec; cgroup_context_init(&s->cgroup_context); kill_context_init(&s->kill_context); + unit_cgroup_context_init_defaults(u, &s->cgroup_context); + UNIT(s)->ignore_on_isolate = true; UNIT(s)->ignore_on_snapshot = true; } @@ -64,10 +67,30 @@ static void scope_done(Unit *u) { cgroup_context_done(&s->cgroup_context); - set_free(s->pids); - s->pids = NULL; + free(s->controller); - unit_unwatch_timer(u, &s->timer_watch); + s->timer_event_source = sd_event_source_unref(s->timer_event_source); +} + +static int scope_arm_timer(Scope *s) { + int r; + + assert(s); + + if (s->timeout_stop_usec <= 0) { + s->timer_event_source = sd_event_source_unref(s->timer_event_source); + return 0; + } + + if (s->timer_event_source) { + r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_stop_usec); + if (r < 0) + return r; + + return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); + } + + return sd_event_add_monotonic(UNIT(s)->manager->event, &s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_stop_usec, 0, scope_dispatch_timer, s); } static void scope_set_state(Scope *s, ScopeState state) { @@ -77,15 +100,14 @@ static void scope_set_state(Scope *s, ScopeState state) { old_state = s->state; s->state = state; - if (state != SCOPE_STOP_SIGTERM && - state != SCOPE_STOP_SIGKILL) - unit_unwatch_timer(UNIT(s), &s->timer_watch); + if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) + s->timer_event_source = sd_event_source_unref(s->timer_event_source); + + if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) + unit_unwatch_all_pids(UNIT(s)); if (state != old_state) - log_debug("%s changed %s -> %s", - UNIT(s)->id, - scope_state_to_string(old_state), - scope_state_to_string(state)); + log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state)); unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true); } @@ -112,7 +134,7 @@ static int scope_verify(Scope *s) { if (UNIT(s)->load_state != UNIT_LOADED) return 0; - if (set_size(s->pids) <= 0 && UNIT(s)->manager->n_reloading <= 0) { + if (set_isempty(UNIT(s)->pids) && UNIT(s)->manager->n_reloading <= 0) { log_error_unit(UNIT(s)->id, "Scope %s has no PIDs. Refusing.", UNIT(s)->id); return -EINVAL; } @@ -158,14 +180,15 @@ static int scope_coldplug(Unit *u) { if (s->deserialized_state != s->state) { - if ((s->deserialized_state == SCOPE_STOP_SIGKILL || s->deserialized_state == SCOPE_STOP_SIGTERM) - && s->timeout_stop_usec > 0) { - r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch); + if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) { + r = scope_arm_timer(s); if (r < 0) - return r; } + if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) + unit_watch_all_pids(UNIT(s)); + scope_set_state(s, s->deserialized_state); } @@ -198,6 +221,7 @@ static void scope_enter_dead(Scope *s, ScopeResult f) { } static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { + bool skip_signal = false; int r; assert(s); @@ -205,23 +229,34 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { if (f != SCOPE_SUCCESS) s->result = f; - r = unit_kill_context( - UNIT(s), - &s->kill_context, - state != SCOPE_STOP_SIGTERM, - -1, -1, false); - if (r < 0) - goto fail; + unit_watch_all_pids(UNIT(s)); + + /* If we have a controller set let's ask the controller nicely + * to terminate the scope, instead of us going directly into + * SIGTERM beserk mode */ + if (state == SCOPE_STOP_SIGTERM) + skip_signal = bus_scope_send_request_stop(s) > 0; + + if (!skip_signal) { + r = unit_kill_context( + UNIT(s), + &s->kill_context, + state != SCOPE_STOP_SIGTERM, + -1, -1, false); + if (r < 0) + goto fail; + } else + r = 1; if (r > 0) { - if (s->timeout_stop_usec > 0) { - r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch); - if (r < 0) - goto fail; - } + r = scope_arm_timer(s); + if (r < 0) + goto fail; scope_set_state(s, state); - } else + } else if (state == SCOPE_STOP_SIGTERM) + scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); + else scope_enter_dead(s, SCOPE_SUCCESS); return; @@ -257,13 +292,10 @@ static int scope_start(Unit *u) { return r; } - r = cg_attach_many_everywhere(u->manager->cgroup_supported, u->cgroup_path, s->pids); + r = cg_attach_many_everywhere(u->manager->cgroup_supported, u->cgroup_path, UNIT(s)->pids); if (r < 0) return r; - set_free(s->pids); - s->pids = NULL; - s->result = SCOPE_SUCCESS; scope_set_state(s, SCOPE_RUNNING); @@ -274,13 +306,13 @@ static int scope_stop(Unit *u) { Scope *s = SCOPE(u); assert(s); - assert(s->state == SCOPE_RUNNING); if (s->state == SCOPE_STOP_SIGTERM || s->state == SCOPE_STOP_SIGKILL) return 0; - assert(s->state == SCOPE_RUNNING); + assert(s->state == SCOPE_RUNNING || + s->state == SCOPE_ABANDONED); scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS); return 0; @@ -297,10 +329,24 @@ static void scope_reset_failed(Unit *u) { s->result = SCOPE_SUCCESS; } -static int scope_kill(Unit *u, KillWho who, int signo, DBusError *error) { +static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { return unit_kill_common(u, who, signo, -1, -1, error); } +static int scope_get_timeout(Unit *u, uint64_t *timeout) { + Scope *s = SCOPE(u); + int r; + + if (!s->timer_event_source) + return 0; + + r = sd_event_source_get_time(s->timer_event_source, timeout); + if (r < 0) + return r; + + return 1; +} + static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { Scope *s = SCOPE(u); @@ -344,8 +390,8 @@ static bool scope_check_gc(Unit *u) { /* Never clean up scopes that still have a process around, * even if the scope is formally dead. */ - if (UNIT(s)->cgroup_path) { - r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true); + if (u->cgroup_path) { + r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); if (r <= 0) return true; } @@ -353,54 +399,87 @@ static bool scope_check_gc(Unit *u) { return false; } -static void scope_timer_event(Unit *u, uint64_t elapsed, Watch*w) { +static void scope_notify_cgroup_empty_event(Unit *u) { Scope *s = SCOPE(u); + assert(u); + + log_debug_unit(u->id, "%s: cgroup is empty", u->id); + + if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) + scope_enter_dead(s, SCOPE_SUCCESS); +} + +static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) { + + /* If we get a SIGCHLD event for one of the processes we were + interested in, then we look for others to watch, under the + assumption that we'll sooner or later get a SIGCHLD for + them, as the original process we watched was probably the + parent of them, and they are hence now our children. */ + + unit_tidy_watch_pids(u, 0, 0); + unit_watch_all_pids(u); + + /* If the PID set is empty now, then let's finish this off */ + if (set_isempty(u->pids)) + scope_notify_cgroup_empty_event(u); +} + +static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { + Scope *s = SCOPE(userdata); assert(s); - assert(elapsed == 1); - assert(w == &s->timer_watch); + assert(s->timer_event_source == source); switch (s->state) { case SCOPE_STOP_SIGTERM: if (s->kill_context.send_sigkill) { - log_warning_unit(u->id, "%s stopping timed out. Killing.", u->id); + log_warning_unit(UNIT(s)->id, "%s stopping timed out. Killing.", UNIT(s)->id); scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); } else { - log_warning_unit(u->id, "%s stopping timed out. Skipping SIGKILL.", u->id); + log_warning_unit(UNIT(s)->id, "%s stopping timed out. Skipping SIGKILL.", UNIT(s)->id); scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); } break; case SCOPE_STOP_SIGKILL: - log_warning_unit(u->id, "%s still around after SIGKILL. Ignoring.", u->id); + log_warning_unit(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id); scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); break; default: assert_not_reached("Timeout at wrong time."); } + + return 0; } -static void scope_notify_cgroup_empty_event(Unit *u) { - Scope *s = SCOPE(u); - assert(u); +int scope_abandon(Scope *s) { + assert(s); - log_debug_unit(u->id, "%s: cgroup is empty", u->id); + if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) + return -ESTALE; - switch (s->state) { + free(s->controller); + s->controller = NULL; - case SCOPE_RUNNING: - case SCOPE_STOP_SIGTERM: - case SCOPE_STOP_SIGKILL: - scope_enter_dead(s, SCOPE_SUCCESS); + /* The client is no longer watching the remaining processes, + * so let's step in here, under the assumption that the + * remaining processes will be sooner or later reassigned to + * us as parent. */ - break; + unit_tidy_watch_pids(UNIT(s), 0, 0); + unit_watch_all_pids(UNIT(s)); - default: - ; - } + /* If the PID set is empty now, then let's finish this off */ + if (set_isempty(UNIT(s)->pids)) + scope_notify_cgroup_empty_event(UNIT(s)); + else + scope_set_state(s, SCOPE_ABANDONED); + + return 0; } _pure_ static UnitActiveState scope_active_state(Unit *u) { @@ -418,6 +497,7 @@ _pure_ static const char *scope_sub_state_to_string(Unit *u) { static const char* const scope_state_table[_SCOPE_STATE_MAX] = { [SCOPE_DEAD] = "dead", [SCOPE_RUNNING] = "running", + [SCOPE_ABANDONED] = "abandoned", [SCOPE_STOP_SIGTERM] = "stop-sigterm", [SCOPE_STOP_SIGKILL] = "stop-sigkill", [SCOPE_FAILED] = "failed", @@ -435,13 +515,14 @@ DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); const UnitVTable scope_vtable = { .object_size = sizeof(Scope), + .cgroup_context_offset = offsetof(Scope, cgroup_context), + .kill_context_offset = offsetof(Scope, kill_context), + .sections = "Unit\0" "Scope\0" "Install\0", - .private_section = "Scope", - .cgroup_context_offset = offsetof(Scope, cgroup_context), .no_alias = true, .no_instances = true, @@ -459,6 +540,8 @@ const UnitVTable scope_vtable = { .kill = scope_kill, + .get_timeout = scope_get_timeout, + .serialize = scope_serialize, .deserialize_item = scope_deserialize_item, @@ -467,14 +550,14 @@ const UnitVTable scope_vtable = { .check_gc = scope_check_gc, - .timer_event = scope_timer_event, + .sigchld_event = scope_sigchld_event, .reset_failed = scope_reset_failed, .notify_cgroup_empty = scope_notify_cgroup_empty_event, .bus_interface = "org.freedesktop.systemd1.Scope", - .bus_message_handler = bus_scope_message_handler, + .bus_vtable = bus_scope_vtable, .bus_set_property = bus_scope_set_property, .bus_commit_properties = bus_scope_commit_properties,