X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Ftimer.c;h=ce7fd6bb3a5dd5745d5589c0960feeca8473e29e;hp=61bd85c7ef2b5324188c3c3478a6ec5562b13a9d;hb=1124fe6f01b1d59d016c238026f20380f38d98dc;hpb=d6c9574fb558d9e304699b1cc7522c3b133adfc9 diff --git a/src/timer.c b/src/timer.c index 61bd85c7e..ce7fd6bb3 100644 --- a/src/timer.c +++ b/src/timer.c @@ -33,14 +33,14 @@ static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = { [TIMER_WAITING] = UNIT_ACTIVE, [TIMER_RUNNING] = UNIT_ACTIVE, [TIMER_ELAPSED] = UNIT_ACTIVE, - [TIMER_MAINTENANCE] = UNIT_MAINTENANCE + [TIMER_FAILED] = UNIT_FAILED }; static void timer_init(Unit *u) { Timer *t = TIMER(u); assert(u); - assert(u->meta.load_state == UNIT_STUB); + assert(u->load_state == UNIT_STUB); t->next_elapse = (usec_t) -1; } @@ -57,16 +57,18 @@ static void timer_done(Unit *u) { } unit_unwatch_timer(u, &t->timer_watch); + + unit_ref_unset(&t->unit); } static int timer_verify(Timer *t) { assert(t); - if (t->meta.load_state != UNIT_LOADED) + if (UNIT(t)->load_state != UNIT_LOADED) return 0; if (!t->values) { - log_error("%s lacks value setting. Refusing.", t->meta.id); + log_error("%s lacks value setting. Refusing.", UNIT(t)->id); return -EINVAL; } @@ -78,11 +80,15 @@ static int timer_add_default_dependencies(Timer *t) { assert(t); - if (t->meta.manager->running_as == MANAGER_SYSTEM) + if (UNIT(t)->manager->running_as == MANAGER_SYSTEM) { + if ((r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0) + return r; + if ((r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0) return r; + } - return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); + return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); } static int timer_load(Unit *u) { @@ -90,21 +96,28 @@ static int timer_load(Unit *u) { int r; assert(u); - assert(u->meta.load_state == UNIT_STUB); + assert(u->load_state == UNIT_STUB); if ((r = unit_load_fragment_and_dropin(u)) < 0) return r; - if (u->meta.load_state == UNIT_LOADED) { + if (u->load_state == UNIT_LOADED) { + + if (!UNIT_DEREF(t->unit)) { + Unit *x; - if (!t->unit) - if ((r = unit_load_related_unit(u, ".service", &t->unit))) + r = unit_load_related_unit(u, ".service", &x); + if (r < 0) return r; - if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0) + unit_ref_set(&t->unit, x); + } + + r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(t->unit), true); + if (r < 0) return r; - if (t->meta.default_dependencies) + if (UNIT(t)->default_dependencies) if ((r = timer_add_default_dependencies(t)) < 0) return r; } @@ -122,7 +135,7 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) { "%sTimer State: %s\n" "%sUnit: %s\n", prefix, timer_state_to_string(t->state), - prefix, t->unit->meta.id); + prefix, UNIT_DEREF(t->unit)->id); LIST_FOREACH(value, v, t->values) fprintf(f, @@ -144,11 +157,11 @@ static void timer_set_state(Timer *t, TimerState state) { if (state != old_state) log_debug("%s changed %s -> %s", - t->meta.id, + UNIT(t)->id, timer_state_to_string(old_state), timer_state_to_string(state)); - unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); + unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true); } static void timer_enter_waiting(Timer *t, bool initial); @@ -161,9 +174,7 @@ static int timer_coldplug(Unit *u) { if (t->deserialized_state != t->state) { - if (t->deserialized_state == TIMER_WAITING || - t->deserialized_state == TIMER_RUNNING || - t->deserialized_state == TIMER_ELAPSED) + if (t->deserialized_state == TIMER_WAITING) timer_enter_waiting(t, false); else timer_set_state(t, t->deserialized_state); @@ -178,7 +189,7 @@ static void timer_enter_dead(Timer *t, bool success) { if (!success) t->failure = true; - timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD); + timer_set_state(t, t->failure ? TIMER_FAILED : TIMER_DEAD); } static void timer_enter_waiting(Timer *t, bool initial) { @@ -197,9 +208,9 @@ static void timer_enter_waiting(Timer *t, bool initial) { switch (v->base) { case TIMER_ACTIVE: - if (state_translation_table[t->state] == UNIT_ACTIVE) { - base = t->meta.inactive_exit_timestamp.monotonic; - } else + if (state_translation_table[t->state] == UNIT_ACTIVE) + base = UNIT(t)->inactive_exit_timestamp.monotonic; + else base = n; break; @@ -209,23 +220,23 @@ static void timer_enter_waiting(Timer *t, bool initial) { break; case TIMER_STARTUP: - base = t->meta.manager->startup_timestamp.monotonic; + base = UNIT(t)->manager->startup_timestamp.monotonic; break; case TIMER_UNIT_ACTIVE: - if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0) + if (UNIT_DEREF(t->unit)->inactive_exit_timestamp.monotonic <= 0) continue; - base = t->unit->meta.inactive_exit_timestamp.monotonic; + base = UNIT_DEREF(t->unit)->inactive_exit_timestamp.monotonic; break; case TIMER_UNIT_INACTIVE: - if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0) + if (UNIT_DEREF(t->unit)->inactive_enter_timestamp.monotonic <= 0) continue; - base = t->unit->meta.inactive_enter_timestamp.monotonic; + base = UNIT_DEREF(t->unit)->inactive_enter_timestamp.monotonic; break; default: @@ -261,7 +272,7 @@ static void timer_enter_waiting(Timer *t, bool initial) { return; fail: - log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r)); + log_warning("%s failed to enter waiting state: %s", UNIT(t)->id, strerror(-r)); timer_enter_dead(t, false); } @@ -273,17 +284,17 @@ static void timer_enter_running(Timer *t) { dbus_error_init(&error); /* Don't start job if we are supposed to go down */ - if (t->meta.job && t->meta.job->type == JOB_STOP) + if (UNIT(t)->job && UNIT(t)->job->type == JOB_STOP) return; - if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0) + if ((r = manager_add_job(UNIT(t)->manager, JOB_START, UNIT_DEREF(t->unit), JOB_REPLACE, true, &error, NULL)) < 0) goto fail; timer_set_state(t, TIMER_RUNNING); return; fail: - log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r)); + log_warning("%s failed to queue unit startup job: %s", UNIT(t)->id, bus_error(&error, r)); timer_enter_dead(t, false); dbus_error_free(&error); @@ -293,9 +304,9 @@ static int timer_start(Unit *u) { Timer *t = TIMER(u); assert(t); - assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE); + assert(t->state == TIMER_DEAD || t->state == TIMER_FAILED); - if (t->unit->meta.load_state != UNIT_LOADED) + if (UNIT_DEREF(t->unit)->load_state != UNIT_LOADED) return -ENOENT; t->failure = false; @@ -367,42 +378,28 @@ static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) { if (t->state != TIMER_WAITING) return; - log_debug("Timer elapsed on %s", u->meta.id); + log_debug("Timer elapsed on %s", u->id); timer_enter_running(t); } void timer_unit_notify(Unit *u, UnitActiveState new_state) { - char *n; - int r; Iterator i; + Unit *k; - if (u->meta.type == UNIT_TIMER) + if (u->type == UNIT_TIMER) return; - SET_FOREACH(n, u->meta.names, i) { - char *k; - Unit *p; + SET_FOREACH(k, u->dependencies[UNIT_TRIGGERED_BY], i) { Timer *t; TimerValue *v; - if (!(k = unit_name_change_suffix(n, ".timer"))) { - r = -ENOMEM; - goto fail; - } - - p = manager_get_unit(u->meta.manager, k); - free(k); - - if (!p) + if (k->type != UNIT_TIMER) continue; - if (p->meta.load_state != UNIT_LOADED) + if (k->load_state != UNIT_LOADED) continue; - t = TIMER(p); - - if (t->unit != u) - continue; + t = TIMER(k); /* Reenable all timers that depend on unit state */ LIST_FOREACH(value, v, t->values) @@ -421,34 +418,29 @@ void timer_unit_notify(Unit *u, UnitActiveState new_state) { case TIMER_RUNNING: - if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) { - log_debug("%s got notified about unit deactivation.", t->meta.id); + if (UNIT_IS_INACTIVE_OR_FAILED(new_state)) { + log_debug("%s got notified about unit deactivation.", UNIT(t)->id); timer_enter_waiting(t, false); } break; case TIMER_DEAD: - case TIMER_MAINTENANCE: - ; + case TIMER_FAILED: + break; default: assert_not_reached("Unknown timer state"); } } - - return; - -fail: - log_error("Failed find timer unit: %s", strerror(-r)); } -static void timer_reset_maintenance(Unit *u) { +static void timer_reset_failed(Unit *u) { Timer *t = TIMER(u); assert(t); - if (t->state == TIMER_MAINTENANCE) + if (t->state == TIMER_FAILED) timer_set_state(t, TIMER_DEAD); t->failure = false; @@ -459,7 +451,7 @@ static const char* const timer_state_table[_TIMER_STATE_MAX] = { [TIMER_WAITING] = "waiting", [TIMER_RUNNING] = "running", [TIMER_ELAPSED] = "elapsed", - [TIMER_MAINTENANCE] = "maintenance" + [TIMER_FAILED] = "failed" }; DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState); @@ -476,6 +468,11 @@ DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase); const UnitVTable timer_vtable = { .suffix = ".timer", + .object_size = sizeof(Timer), + .sections = + "Unit\0" + "Timer\0" + "Install\0", .init = timer_init, .done = timer_done, @@ -496,7 +493,9 @@ const UnitVTable timer_vtable = { .timer_event = timer_timer_event, - .reset_maintenance = timer_reset_maintenance, + .reset_failed = timer_reset_failed, - .bus_message_handler = bus_timer_message_handler + .bus_interface = "org.freedesktop.systemd1.Timer", + .bus_message_handler = bus_timer_message_handler, + .bus_invalidating_properties = bus_timer_invalidating_properties };