X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fscope.c;h=0c1d17e68c569a2fd5bcfaa1261db29a756875c3;hb=c7040b5d1c2c148f12b6a5eef3dfce1661805131;hp=f88addadf34b8fdd39842dff79bfba1193b7f7f1;hpb=6c12b52e19640747e96f89d85422941a23dc6b29;p=elogind.git diff --git a/src/core/scope.c b/src/core/scope.c index f88addadf..0c1d17e68 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -40,15 +40,15 @@ static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = { [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); @@ -64,10 +64,33 @@ static void scope_done(Unit *u) { cgroup_context_done(&s->cgroup_context); + free(s->controller); + set_free(s->pids); s->pids = NULL; - 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, now(CLOCK_MONOTONIC) + s->timeout_stop_usec, 0, scope_dispatch_timer, s, &s->timer_event_source); } static void scope_set_state(Scope *s, ScopeState state) { @@ -79,7 +102,7 @@ static void scope_set_state(Scope *s, ScopeState state) { if (state != SCOPE_STOP_SIGTERM && state != SCOPE_STOP_SIGKILL) - unit_unwatch_timer(UNIT(s), &s->timer_watch); + s->timer_event_source = sd_event_source_unref(s->timer_event_source); if (state != old_state) log_debug("%s changed %s -> %s", @@ -112,7 +135,7 @@ static int scope_verify(Scope *s) { if (UNIT(s)->load_state != UNIT_LOADED) return 0; - if (set_size(s->pids) <= 0) { + if (set_size(s->pids) <= 0 && UNIT(s)->manager->n_reloading <= 0) { log_error_unit(UNIT(s)->id, "Scope %s has no PIDs. Refusing.", UNIT(s)->id); return -EINVAL; } @@ -158,11 +181,9 @@ 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 (s->deserialized_state == SCOPE_STOP_SIGKILL || s->deserialized_state == SCOPE_STOP_SIGTERM) { + r = scope_arm_timer(s); if (r < 0) - return r; } @@ -198,6 +219,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 +227,32 @@ 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; + /* 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; @@ -239,6 +270,9 @@ static int scope_start(Unit *u) { assert(s); + if (s->state == SCOPE_FAILED) + return -EPERM; + if (s->state == SCOPE_STOP_SIGTERM || s->state == SCOPE_STOP_SIGKILL) return -EAGAIN; @@ -254,7 +288,7 @@ static int scope_start(Unit *u) { return r; } - r = cg_attach_many_with_mask(u->cgroup_mask, u->cgroup_path, s->pids); + r = cg_attach_many_everywhere(u->manager->cgroup_supported, u->cgroup_path, s->pids); if (r < 0) return r; @@ -283,10 +317,35 @@ static int scope_stop(Unit *u) { return 0; } -static int scope_kill(Unit *u, KillWho who, int signo, DBusError *error) { +static void scope_reset_failed(Unit *u) { + Scope *s = SCOPE(u); + + assert(s); + + if (s->state == SCOPE_FAILED) + scope_set_state(s, SCOPE_DEAD); + + s->result = SCOPE_SUCCESS; +} + +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); @@ -339,34 +398,35 @@ static bool scope_check_gc(Unit *u) { return false; } -static void scope_timer_event(Unit *u, uint64_t elapsed, Watch*w) { - Scope *s = SCOPE(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) { @@ -403,7 +463,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] = "active", + [SCOPE_RUNNING] = "running", [SCOPE_STOP_SIGTERM] = "stop-sigterm", [SCOPE_STOP_SIGKILL] = "stop-sigkill", [SCOPE_FAILED] = "failed", @@ -421,13 +481,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, @@ -445,6 +506,8 @@ const UnitVTable scope_vtable = { .kill = scope_kill, + .get_timeout = scope_get_timeout, + .serialize = scope_serialize, .deserialize_item = scope_deserialize_item, @@ -453,12 +516,12 @@ const UnitVTable scope_vtable = { .check_gc = scope_check_gc, - .timer_event = scope_timer_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,