timer.c revision 82a2b6bb5e4e5d294f09af778c48974a7857afb6
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen This file is part of systemd.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Copyright 2010 Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is free software; you can redistribute it and/or modify it
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen under the terms of the GNU Lesser General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen the Free Software Foundation; either version 2.1 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is distributed in the hope that it will be useful, but
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Lesser General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen You should have received a copy of the GNU Lesser General Public License
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_DEAD] = UNIT_INACTIVE,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_WAITING] = UNIT_ACTIVE,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_RUNNING] = UNIT_ACTIVE,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_ELAPSED] = UNIT_ACTIVE,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_init(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(u->load_state == UNIT_STUB);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_monotonic_or_boottime = USEC_INFINITY;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_realtime = USEC_INFINITY;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->accuracy_usec = u->manager->default_timer_accuracy_usec;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while ((v = t->values)) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_REMOVE(value, t->values, v);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen calendar_spec_free(v->calendar_spec);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_done(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->monotonic_event_source = sd_event_source_unref(t->monotonic_event_source);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->realtime_event_source = sd_event_source_unref(t->realtime_event_source);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_verify(Timer *t) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (UNIT(t)->load_state != UNIT_LOADED)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_error(UNIT(t)->id, "%s lacks value setting. Refusing.", UNIT(t)->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_add_default_dependencies(Timer *t) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_TIMERS_TARGET, NULL, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (UNIT(t)->manager->running_as == SYSTEMD_SYSTEM) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(value, v, t->values) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (v->base == TIMER_CALENDAR) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, SPECIAL_TIME_SYNC_TARGET, NULL, true);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl if (UNIT(t)->manager->running_as == SYSTEMD_SYSTEM) {
b7e7184634d573fb73143210962acce205f37f61Michael Biebl r = unit_require_mounts_for(UNIT(t), "/var/lib/systemd/timers");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->stamp_path = strappend("/var/lib/systemd/timers/stamp-", UNIT(t)->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->stamp_path = strjoin(e, "/systemd/timers/stamp-", UNIT(t)->id, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *h = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return log_error_errno(r, "Failed to determine home directory: %m");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->stamp_path = strjoin(h, "/.local/share/systemd/timers/stamp-", UNIT(t)->id, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_load(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (u->load_state == UNIT_LOADED) {
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt if (set_isempty(u->dependencies[UNIT_TRIGGERS])) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = unit_load_related_unit(u, ".service", &x);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, x, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = timer_setup_persistent(t);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (u->default_dependencies) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = timer_add_default_dependencies(t);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_dump(Unit *u, FILE *f, const char *prefix) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char buf[FORMAT_TIMESPAN_MAX];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "%sTimer State: %s\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "%sPersistent: %s\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "%sWakeSystem: %s\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "%sAccuracy: %s\n",
260ad50f5b4a9795032e3119c64f838a2d03370dThomas Hindoe Paaboel Andersen prefix, timer_state_to_string(t->state),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen prefix, timer_result_to_string(t->result),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen prefix, trigger ? trigger->id : "n/a",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen prefix, yes_no(t->wake_system),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen prefix, format_timespan(buf, sizeof(buf), t->accuracy_usec, 1));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(value, v, t->values) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (v->base == TIMER_CALENDAR) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *p = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen calendar_spec_to_string(v->calendar_spec, &p);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_base_to_string(v->base),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char timespan1[FORMAT_TIMESPAN_MAX];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_base_to_string(v->base),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen format_timespan(timespan1, sizeof(timespan1), v->value, 0));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_set_state(Timer *t, TimerState state) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (state != TIMER_WAITING) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->monotonic_event_source = sd_event_source_unref(t->monotonic_event_source);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->realtime_event_source = sd_event_source_unref(t->realtime_event_source);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "%s changed %s -> %s", UNIT(t)->id,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_state_to_string(old_state),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_state_to_string(state));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_enter_waiting(Timer *t, bool initial);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(t->state == TIMER_DEAD);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->deserialized_state != t->state) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->deserialized_state == TIMER_WAITING)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_waiting(t, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_set_state(t, t->deserialized_state);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_enter_dead(Timer *t, TimerResult f) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return t + a - b;
3315f085178f46155fda345d9526c09083b45946Lennart Poetteringstatic void timer_enter_waiting(Timer *t, bool initial) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen bool found_monotonic = false, found_realtime = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen usec_t ts_realtime, ts_monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* If we shall wake the system we use the boottime clock
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * rather than the monotonic clock. */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ts_realtime = now(CLOCK_REALTIME);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen ts_monotonic = now(t->wake_system ? CLOCK_BOOTTIME : CLOCK_MONOTONIC);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(value, v, t->values) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (v->base == TIMER_CALENDAR) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* If we know the last time this was
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * triggered, schedule the job based relative
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * to that. If we don't just start from
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen b = t->last_trigger.realtime > 0 ? t->last_trigger.realtime : ts_realtime;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_realtime = v->next_elapse;
77354c7e6f096a447245a8781c1eaa4acbe67089Martin Pitt t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (state_translation_table[t->state] == UNIT_ACTIVE)
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering base = UNIT(t)->inactive_exit_timestamp.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* CLOCK_MONOTONIC equals the uptime on Linux */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen base = UNIT(t)->manager->userspace_timestamp.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen base = UNIT_TRIGGER(UNIT(t))->inactive_exit_timestamp.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen base = t->last_trigger.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen base = UNIT_TRIGGER(UNIT(t))->inactive_enter_timestamp.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen base = t->last_trigger.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert_not_reached("Unknown timer base");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen base = monotonic_to_boottime(base);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen v->next_elapse = base + v->value;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!initial && v->next_elapse < ts_monotonic && IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* This is a one time trigger, disable it now */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_monotonic_or_boottime = v->next_elapse;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!found_monotonic && !found_realtime) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(UNIT(t)->id, "%s: Timer is elapsed.", UNIT(t)->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_set_state(t, TIMER_ELAPSED);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char buf[FORMAT_TIMESPAN_MAX];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(UNIT(t)->id, "%s: Monotonic timer elapses in %s.",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen format_timespan(buf, sizeof(buf), t->next_elapse_monotonic_or_boottime > ts_monotonic ? t->next_elapse_monotonic_or_boottime - ts_monotonic : 0, 0));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_monotonic_or_boottime, t->accuracy_usec,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (t->monotonic_event_source) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen char buf[FORMAT_TIMESTAMP_MAX];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(UNIT(t)->id, "%s: Realtime timer elapses at %s.", UNIT(t)->id, format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->realtime_event_source) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->next_elapse_realtime, t->accuracy_usec,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (t->realtime_event_source) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_set_state(t, TIMER_WAITING);
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek log_unit_warning_errno(UNIT(t)->id, r, "%s failed to enter waiting state: %m", UNIT(t)->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_enter_running(Timer *t) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
b7e7184634d573fb73143210962acce205f37f61Michael Biebl /* Don't start job if we are supposed to go down */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = manager_add_job(UNIT(t)->manager, JOB_START, UNIT_TRIGGER(UNIT(t)),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen JOB_REPLACE, true, &error, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen dual_timestamp_get(&t->last_trigger);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, 0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_set_state(t, TIMER_RUNNING);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_warning(UNIT(t)->id,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "%s failed to queue unit startup job: %s",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen UNIT(t)->id, bus_error_message(&error, r));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(t->state == TIMER_DEAD || t->state == TIMER_FAILED);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->last_trigger = DUAL_TIMESTAMP_NULL;
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering /* Reenable all timers that depend on unit activation time */
b2fadec6048adb3596f2633cb7fe7a49f5937a18Zbigniew Jędrzejewski-Szmek LIST_FOREACH(value, v, t->values)
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek if (stat(t->stamp_path, &st) >= 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->last_trigger.realtime = timespec_load(&st.st_atim);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* The timer has never run before,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen * make sure a stamp file exists.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_waiting(t, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_stop(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_dead(t, TIMER_SUCCESS);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen unit_serialize_item(u, f, "result", timer_result_to_string(t->result));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->last_trigger.realtime > 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen unit_serialize_item_format(u, f, "last-trigger-realtime", "%" PRIu64, t->last_trigger.realtime);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->last_trigger.monotonic > 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen unit_serialize_item_format(u, f, "last-trigger-monotonic", "%" PRIu64, t->last_trigger.monotonic);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
b2fadec6048adb3596f2633cb7fe7a49f5937a18Zbigniew Jędrzejewski-Szmek state = timer_state_from_string(value);
b2fadec6048adb3596f2633cb7fe7a49f5937a18Zbigniew Jędrzejewski-Szmek log_unit_debug(u->id, "Failed to parse state value %s", value);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen t->deserialized_state = state;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (streq(key, "result")) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen f = timer_result_from_string(value);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(u->id, "Failed to parse result value %s", value);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (streq(key, "last-trigger-realtime")) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = safe_atou64(value, &t->last_trigger.realtime);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(u->id, "Failed to parse last-trigger-realtime value %s", value);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (streq(key, "last-trigger-monotonic")) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = safe_atou64(value, &t->last_trigger.monotonic);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(u->id, "Failed to parse last-trigger-monotonic value %s", value);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(u->id, "Unknown serialization key '%s'", key);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen_pure_ static UnitActiveState timer_active_state(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return state_translation_table[TIMER(u)->state];
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen_pure_ static const char *timer_sub_state_to_string(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return timer_state_to_string(TIMER(u)->state);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->state != TIMER_WAITING)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(UNIT(t)->id, "Timer elapsed on %s", UNIT(t)->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_trigger_notify(Unit *u, Unit *other) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (other->load_state != UNIT_LOADED)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* Reenable all timers that depend on unit state */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(value, v, t->values)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (v->base == TIMER_UNIT_ACTIVE ||
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen v->base == TIMER_UNIT_INACTIVE)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen /* Recalculate sleep time */
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_waiting(t, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(UNIT(t)->id, "%s got notified about unit deactivation.", UNIT(t)->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_waiting(t, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert_not_reached("Unknown timer state");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_reset_failed(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->state == TIMER_FAILED)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_set_state(t, TIMER_DEAD);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void timer_time_change(Unit *u) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (t->state != TIMER_WAITING)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_unit_debug(u->id, "%s: time change, recalculating next elapse.", u->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen timer_enter_waiting(t, false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char* const timer_state_table[_TIMER_STATE_MAX] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel AndersenDEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char* const timer_base_table[_TIMER_BASE_MAX] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_ACTIVE] = "OnActiveSec",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_STARTUP] = "OnStartupSec",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_CALENDAR] = "OnCalendar"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel AndersenDEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char* const timer_result_table[_TIMER_RESULT_MAX] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen [TIMER_FAILURE_RESOURCES] = "resources"
805e5dda0a01c99d231824e1a9c4a208418bf342Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenconst UnitVTable timer_vtable = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .object_size = sizeof(Timer),
a986501b9059b72e8deced262554fbdd1ab9da17Lennart Poettering .deserialize_item = timer_deserialize_item,
805e5dda0a01c99d231824e1a9c4a208418bf342Lennart Poettering .sub_state_to_string = timer_sub_state_to_string,
f4f01ec146d91cb6943828851d98eee6a1ad4dd9Martin Pitt .bus_interface = "org.freedesktop.systemd1.Timer",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen .bus_vtable = bus_timer_vtable,