1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "unit-name.h"
27 #include "dbus-timer.h"
29 #include "bus-errors.h"
31 static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
32 [TIMER_DEAD] = UNIT_INACTIVE,
33 [TIMER_WAITING] = UNIT_ACTIVE,
34 [TIMER_RUNNING] = UNIT_ACTIVE,
35 [TIMER_ELAPSED] = UNIT_ACTIVE,
36 [TIMER_FAILED] = UNIT_FAILED
39 static void timer_init(Unit *u) {
43 assert(u->meta.load_state == UNIT_STUB);
45 t->next_elapse = (usec_t) -1;
48 static void timer_done(Unit *u) {
54 while ((v = t->values)) {
55 LIST_REMOVE(TimerValue, value, t->values, v);
59 unit_unwatch_timer(u, &t->timer_watch);
62 static int timer_verify(Timer *t) {
65 if (t->meta.load_state != UNIT_LOADED)
69 log_error("%s lacks value setting. Refusing.", t->meta.id);
76 static int timer_add_default_dependencies(Timer *t) {
81 if (t->meta.manager->running_as == MANAGER_SYSTEM) {
82 if ((r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
85 if ((r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
89 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
92 static int timer_load(Unit *u) {
97 assert(u->meta.load_state == UNIT_STUB);
99 if ((r = unit_load_fragment_and_dropin(u)) < 0)
102 if (u->meta.load_state == UNIT_LOADED) {
105 if ((r = unit_load_related_unit(u, ".service", &t->unit)))
108 if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
111 if (t->meta.default_dependencies)
112 if ((r = timer_add_default_dependencies(t)) < 0)
116 return timer_verify(t);
119 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
123 timespan1[FORMAT_TIMESPAN_MAX];
126 "%sTimer State: %s\n"
128 prefix, timer_state_to_string(t->state),
129 prefix, t->unit->meta.id);
131 LIST_FOREACH(value, v, t->values)
135 timer_base_to_string(v->base),
136 strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
139 static void timer_set_state(Timer *t, TimerState state) {
140 TimerState old_state;
143 old_state = t->state;
146 if (state != TIMER_WAITING)
147 unit_unwatch_timer(UNIT(t), &t->timer_watch);
149 if (state != old_state)
150 log_debug("%s changed %s -> %s",
152 timer_state_to_string(old_state),
153 timer_state_to_string(state));
155 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
158 static void timer_enter_waiting(Timer *t, bool initial);
160 static int timer_coldplug(Unit *u) {
164 assert(t->state == TIMER_DEAD);
166 if (t->deserialized_state != t->state) {
168 if (t->deserialized_state == TIMER_WAITING)
169 timer_enter_waiting(t, false);
171 timer_set_state(t, t->deserialized_state);
177 static void timer_enter_dead(Timer *t, bool success) {
183 timer_set_state(t, t->failure ? TIMER_FAILED : TIMER_DEAD);
186 static void timer_enter_waiting(Timer *t, bool initial) {
188 usec_t base = 0, delay, n;
192 n = now(CLOCK_MONOTONIC);
194 LIST_FOREACH(value, v, t->values) {
202 if (state_translation_table[t->state] == UNIT_ACTIVE)
203 base = t->meta.inactive_exit_timestamp.monotonic;
209 /* CLOCK_MONOTONIC equals the uptime on Linux */
214 base = t->meta.manager->startup_timestamp.monotonic;
217 case TIMER_UNIT_ACTIVE:
219 if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
222 base = t->unit->meta.inactive_exit_timestamp.monotonic;
225 case TIMER_UNIT_INACTIVE:
227 if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
230 base = t->unit->meta.inactive_enter_timestamp.monotonic;
234 assert_not_reached("Unknown timer base");
237 v->next_elapse = base + v->value;
239 if (!initial && v->next_elapse < n) {
245 t->next_elapse = v->next_elapse;
247 t->next_elapse = MIN(t->next_elapse, v->next_elapse);
253 timer_set_state(t, TIMER_ELAPSED);
257 delay = n < t->next_elapse ? t->next_elapse - n : 0;
259 if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
262 timer_set_state(t, TIMER_WAITING);
266 log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
267 timer_enter_dead(t, false);
270 static void timer_enter_running(Timer *t) {
275 dbus_error_init(&error);
277 /* Don't start job if we are supposed to go down */
278 if (t->meta.job && t->meta.job->type == JOB_STOP)
281 if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0)
284 timer_set_state(t, TIMER_RUNNING);
288 log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r));
289 timer_enter_dead(t, false);
291 dbus_error_free(&error);
294 static int timer_start(Unit *u) {
298 assert(t->state == TIMER_DEAD || t->state == TIMER_FAILED);
300 if (t->unit->meta.load_state != UNIT_LOADED)
304 timer_enter_waiting(t, true);
308 static int timer_stop(Unit *u) {
312 assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
314 timer_enter_dead(t, true);
318 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
325 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
330 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
338 if (streq(key, "state")) {
341 if ((state = timer_state_from_string(value)) < 0)
342 log_debug("Failed to parse state value %s", value);
344 t->deserialized_state = state;
346 log_debug("Unknown serialization key '%s'", key);
351 static UnitActiveState timer_active_state(Unit *u) {
354 return state_translation_table[TIMER(u)->state];
357 static const char *timer_sub_state_to_string(Unit *u) {
360 return timer_state_to_string(TIMER(u)->state);
363 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
367 assert(elapsed == 1);
369 if (t->state != TIMER_WAITING)
372 log_debug("Timer elapsed on %s", u->meta.id);
373 timer_enter_running(t);
376 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
381 if (u->meta.type == UNIT_TIMER)
384 SET_FOREACH(n, u->meta.names, i) {
390 if (!(k = unit_name_change_suffix(n, ".timer"))) {
395 p = manager_get_unit(u->meta.manager, k);
401 if (p->meta.load_state != UNIT_LOADED)
409 /* Reenable all timers that depend on unit state */
410 LIST_FOREACH(value, v, t->values)
411 if (v->base == TIMER_UNIT_ACTIVE ||
412 v->base == TIMER_UNIT_INACTIVE)
420 /* Recalculate sleep time */
421 timer_enter_waiting(t, false);
426 if (UNIT_IS_INACTIVE_OR_FAILED(new_state)) {
427 log_debug("%s got notified about unit deactivation.", t->meta.id);
428 timer_enter_waiting(t, false);
438 assert_not_reached("Unknown timer state");
445 log_error("Failed find timer unit: %s", strerror(-r));
448 static void timer_reset_failed(Unit *u) {
453 if (t->state == TIMER_FAILED)
454 timer_set_state(t, TIMER_DEAD);
459 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
460 [TIMER_DEAD] = "dead",
461 [TIMER_WAITING] = "waiting",
462 [TIMER_RUNNING] = "running",
463 [TIMER_ELAPSED] = "elapsed",
464 [TIMER_FAILED] = "failed"
467 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
469 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
470 [TIMER_ACTIVE] = "OnActiveSec",
471 [TIMER_BOOT] = "OnBootSec",
472 [TIMER_STARTUP] = "OnStartupSec",
473 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
474 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec"
477 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
479 const UnitVTable timer_vtable = {
490 .coldplug = timer_coldplug,
494 .start = timer_start,
497 .serialize = timer_serialize,
498 .deserialize_item = timer_deserialize_item,
500 .active_state = timer_active_state,
501 .sub_state_to_string = timer_sub_state_to_string,
503 .timer_event = timer_timer_event,
505 .reset_failed = timer_reset_failed,
507 .bus_interface = "org.freedesktop.systemd1.Timer",
508 .bus_message_handler = bus_timer_message_handler,
509 .bus_invalidating_properties = bus_timer_invalidating_properties