1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
30 [TIMER_DEAD] = UNIT_INACTIVE,
31 [TIMER_WAITING] = UNIT_ACTIVE,
32 [TIMER_RUNNING] = UNIT_ACTIVE,
33 [TIMER_ELAPSED] = UNIT_ACTIVE,
34 [TIMER_MAINTAINANCE] = UNIT_INACTIVE
37 static void timer_init(Unit *u) {
41 assert(u->meta.load_state == UNIT_STUB);
43 t->next_elapse = (usec_t) -1;
44 t->timer_watch.type = WATCH_INVALID;
47 static void timer_done(Unit *u) {
53 while ((v = t->values)) {
54 LIST_REMOVE(TimerValue, value, t->values, v);
58 unit_unwatch_timer(u, &t->timer_watch);
61 static int timer_verify(Timer *t) {
64 if (UNIT(t)->meta.load_state != UNIT_LOADED)
68 log_error("%s lacks value setting. Refusing.", t->meta.id);
75 static int timer_load(Unit *u) {
80 assert(u->meta.load_state == UNIT_STUB);
82 if ((r = unit_load_fragment_and_dropin(u)) < 0)
85 if (u->meta.load_state == UNIT_LOADED) {
88 if ((r = unit_load_related_unit(u, ".service", &t->unit)))
91 if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
95 return timer_verify(t);
98 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
104 timespan1[FORMAT_TIMESPAN_MAX];
106 p2 = strappend(prefix, "\t");
107 prefix2 = p2 ? p2 : prefix;
110 "%sTimer State: %s\n"
112 prefix, timer_state_to_string(t->state),
113 prefix, t->unit->meta.id);
115 LIST_FOREACH(value, v, t->values)
119 timer_base_to_string(v->base),
120 strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
125 static void timer_set_state(Timer *t, TimerState state) {
126 TimerState old_state;
129 old_state = t->state;
132 if (state != TIMER_WAITING)
133 unit_unwatch_timer(UNIT(t), &t->timer_watch);
135 if (state != old_state)
136 log_debug("%s changed %s -> %s",
138 timer_state_to_string(old_state),
139 timer_state_to_string(state));
141 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
144 static void timer_enter_waiting(Timer *t, bool initial);
146 static int timer_coldplug(Unit *u) {
150 assert(t->state == TIMER_DEAD);
152 if (t->deserialized_state != t->state) {
154 if (t->deserialized_state == TIMER_WAITING ||
155 t->deserialized_state == TIMER_RUNNING ||
156 t->deserialized_state == TIMER_ELAPSED)
157 timer_enter_waiting(t, false);
159 timer_set_state(t, t->deserialized_state);
165 static void timer_enter_dead(Timer *t, bool success) {
171 timer_set_state(t, t->failure ? TIMER_MAINTAINANCE : TIMER_DEAD);
174 static void timer_enter_waiting(Timer *t, bool initial) {
176 usec_t base = 0, delay, n;
180 n = now(CLOCK_MONOTONIC);
182 LIST_FOREACH(value, v, t->values) {
190 if (state_translation_table[t->state] == UNIT_ACTIVE) {
191 base = t->meta.inactive_exit_timestamp.monotonic;
197 /* CLOCK_MONOTONIC equals the uptime on Linux */
202 base = t->meta.manager->startup_timestamp.monotonic;
205 case TIMER_UNIT_ACTIVE:
207 if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
210 base = t->unit->meta.inactive_exit_timestamp.monotonic;
213 case TIMER_UNIT_INACTIVE:
215 if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
218 base = t->unit->meta.inactive_enter_timestamp.monotonic;
222 assert_not_reached("Unknown timer base");
225 v->next_elapse = base + v->value;
227 if (!initial && v->next_elapse < n) {
233 t->next_elapse = v->next_elapse;
235 t->next_elapse = MIN(t->next_elapse, v->next_elapse);
241 timer_set_state(t, TIMER_ELAPSED);
245 delay = n < t->next_elapse ? t->next_elapse - n : 0;
247 if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
250 timer_set_state(t, TIMER_WAITING);
254 log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
255 timer_enter_dead(t, false);
258 static void timer_enter_running(Timer *t) {
262 if ((r = manager_add_job(UNIT(t)->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0)
265 timer_set_state(t, TIMER_RUNNING);
269 log_warning("%s failed to queue unit startup job: %s", t->meta.id, strerror(-r));
270 timer_enter_dead(t, false);
273 static int timer_start(Unit *u) {
277 assert(t->state == TIMER_DEAD);
279 timer_enter_waiting(t, true);
283 static int timer_stop(Unit *u) {
287 assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
289 timer_enter_dead(t, true);
293 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
300 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
305 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
313 if (streq(key, "state")) {
316 if ((state = timer_state_from_string(value)) < 0)
317 log_debug("Failed to parse state value %s", value);
319 t->deserialized_state = state;
321 log_debug("Unknown serialization key '%s'", key);
326 static UnitActiveState timer_active_state(Unit *u) {
329 return state_translation_table[TIMER(u)->state];
332 static const char *timer_sub_state_to_string(Unit *u) {
335 return timer_state_to_string(TIMER(u)->state);
338 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
342 assert(elapsed == 1);
344 if (t->state != TIMER_WAITING)
347 log_debug("Timer elapsed on %s", u->meta.id);
348 timer_enter_running(t);
351 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
356 if (u->meta.type == UNIT_TIMER)
359 SET_FOREACH(n, u->meta.names, i) {
365 if (!(k = unit_name_change_suffix(n, ".timer"))) {
370 p = manager_get_unit(u->meta.manager, k);
378 if (t->meta.load_state != UNIT_LOADED)
381 /* Reenable all timers that depend on unit state */
382 LIST_FOREACH(value, v, t->values)
383 if (v->base == TIMER_UNIT_ACTIVE ||
384 v->base == TIMER_UNIT_INACTIVE)
392 /* Recalculate sleep time */
393 timer_enter_waiting(t, false);
398 if (new_state == UNIT_INACTIVE) {
399 log_debug("%s got notified about unit deactivation.", t->meta.id);
400 timer_enter_waiting(t, false);
406 case TIMER_MAINTAINANCE:
410 assert_not_reached("Unknown timer state");
417 log_error("Failed find timer unit: %s", strerror(-r));
420 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
421 [TIMER_DEAD] = "dead",
422 [TIMER_WAITING] = "waiting",
423 [TIMER_RUNNING] = "running",
424 [TIMER_ELAPSED] = "elapsed",
425 [TIMER_MAINTAINANCE] = "maintainance"
428 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
430 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
431 [TIMER_ACTIVE] = "OnActive",
432 [TIMER_BOOT] = "OnBoot",
433 [TIMER_STARTUP] = "OnStartup",
434 [TIMER_UNIT_ACTIVE] = "OnUnitActive",
435 [TIMER_UNIT_INACTIVE] = "OnUnitInactive"
438 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
440 const UnitVTable timer_vtable = {
447 .coldplug = timer_coldplug,
451 .start = timer_start,
454 .serialize = timer_serialize,
455 .deserialize_item = timer_deserialize_item,
457 .active_state = timer_active_state,
458 .sub_state_to_string = timer_sub_state_to_string,
460 .timer_event = timer_timer_event,
462 .bus_message_handler = bus_timer_message_handler