X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Ftimer.c;h=b01944ccdbd34ae81322c74c66977280f91fb00a;hp=b4521e6efc9b5acd57648e2912dfdbeffcd40732;hb=10717a1a8d48e50463abf2f6b47e2618eaa529e7;hpb=03fae01822b5275a2940458f65644796283a8a23 diff --git a/src/timer.c b/src/timer.c index b4521e6ef..b01944ccd 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -26,13 +26,14 @@ #include "timer.h" #include "dbus-timer.h" #include "special.h" +#include "bus-errors.h" static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = { [TIMER_DEAD] = UNIT_INACTIVE, [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) { @@ -72,6 +73,18 @@ static int timer_verify(Timer *t) { return 0; } +static int timer_add_default_dependencies(Timer *t) { + int r; + + assert(t); + + if (t->meta.manager->running_as == MANAGER_SYSTEM) + 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); +} + static int timer_load(Unit *u) { Timer *t = TIMER(u); int r; @@ -91,9 +104,8 @@ static int timer_load(Unit *u) { if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0) return r; - /* Timers shouldn't stay around on shutdown */ if (t->meta.default_dependencies) - if ((r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0) + if ((r = timer_add_default_dependencies(t)) < 0) return r; } @@ -102,15 +114,10 @@ static int timer_load(Unit *u) { static void timer_dump(Unit *u, FILE *f, const char *prefix) { Timer *t = TIMER(u); - const char *prefix2; - char *p2; TimerValue *v; char timespan1[FORMAT_TIMESPAN_MAX]; - p2 = strappend(prefix, "\t"); - prefix2 = p2 ? p2 : prefix; - fprintf(f, "%sTimer State: %s\n" "%sUnit: %s\n", @@ -123,8 +130,6 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) { prefix, timer_base_to_string(v->base), strna(format_timespan(timespan1, sizeof(timespan1), v->value))); - - free(p2); } static void timer_set_state(Timer *t, TimerState state) { @@ -173,7 +178,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) { @@ -192,9 +197,9 @@ static void timer_enter_waiting(Timer *t, bool initial) { switch (v->base) { case TIMER_ACTIVE: - if (state_translation_table[t->state] == UNIT_ACTIVE) { + if (state_translation_table[t->state] == UNIT_ACTIVE) base = t->meta.inactive_exit_timestamp.monotonic; - } else + else base = n; break; @@ -261,25 +266,34 @@ fail: } static void timer_enter_running(Timer *t) { + DBusError error; int r; + assert(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) + return; - if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0) + if ((r = manager_add_job(t->meta.manager, JOB_START, 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, strerror(-r)); + log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r)); timer_enter_dead(t, false); + + dbus_error_free(&error); } 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) return -ENOENT; @@ -407,7 +421,7 @@ void timer_unit_notify(Unit *u, UnitActiveState new_state) { case TIMER_RUNNING: - if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) { + if (UNIT_IS_INACTIVE_OR_FAILED(new_state)) { log_debug("%s got notified about unit deactivation.", t->meta.id); timer_enter_waiting(t, false); } @@ -415,8 +429,8 @@ void timer_unit_notify(Unit *u, UnitActiveState new_state) { break; case TIMER_DEAD: - case TIMER_MAINTENANCE: - ; + case TIMER_FAILED: + break; default: assert_not_reached("Unknown timer state"); @@ -429,12 +443,23 @@ fail: log_error("Failed find timer unit: %s", strerror(-r)); } +static void timer_reset_failed(Unit *u) { + Timer *t = TIMER(u); + + assert(t); + + if (t->state == TIMER_FAILED) + timer_set_state(t, TIMER_DEAD); + + t->failure = false; +} + static const char* const timer_state_table[_TIMER_STATE_MAX] = { [TIMER_DEAD] = "dead", [TIMER_WAITING] = "waiting", [TIMER_RUNNING] = "running", [TIMER_ELAPSED] = "elapsed", - [TIMER_MAINTENANCE] = "maintenance" + [TIMER_FAILED] = "failed" }; DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState); @@ -471,5 +496,9 @@ const UnitVTable timer_vtable = { .timer_event = timer_timer_event, - .bus_message_handler = bus_timer_message_handler + .reset_failed = timer_reset_failed, + + .bus_interface = "org.freedesktop.systemd1.Timer", + .bus_message_handler = bus_timer_message_handler, + .bus_invalidating_properties = bus_timer_invalidating_properties };