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"
30 static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
31 [TIMER_DEAD] = UNIT_INACTIVE,
32 [TIMER_WAITING] = UNIT_ACTIVE,
33 [TIMER_RUNNING] = UNIT_ACTIVE,
34 [TIMER_ELAPSED] = UNIT_ACTIVE,
35 [TIMER_MAINTENANCE] = UNIT_MAINTENANCE
38 static void timer_init(Unit *u) {
42 assert(u->meta.load_state == UNIT_STUB);
44 t->next_elapse = (usec_t) -1;
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 (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)
94 /* Timers shouldn't stay around on shutdown */
95 if (t->meta.default_dependencies)
96 if ((r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
100 return timer_verify(t);
103 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
109 timespan1[FORMAT_TIMESPAN_MAX];
111 p2 = strappend(prefix, "\t");
112 prefix2 = p2 ? p2 : prefix;
115 "%sTimer State: %s\n"
117 prefix, timer_state_to_string(t->state),
118 prefix, t->unit->meta.id);
120 LIST_FOREACH(value, v, t->values)
124 timer_base_to_string(v->base),
125 strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
130 static void timer_set_state(Timer *t, TimerState state) {
131 TimerState old_state;
134 old_state = t->state;
137 if (state != TIMER_WAITING)
138 unit_unwatch_timer(UNIT(t), &t->timer_watch);
140 if (state != old_state)
141 log_debug("%s changed %s -> %s",
143 timer_state_to_string(old_state),
144 timer_state_to_string(state));
146 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
149 static void timer_enter_waiting(Timer *t, bool initial);
151 static int timer_coldplug(Unit *u) {
155 assert(t->state == TIMER_DEAD);
157 if (t->deserialized_state != t->state) {
159 if (t->deserialized_state == TIMER_WAITING ||
160 t->deserialized_state == TIMER_RUNNING ||
161 t->deserialized_state == TIMER_ELAPSED)
162 timer_enter_waiting(t, false);
164 timer_set_state(t, t->deserialized_state);
170 static void timer_enter_dead(Timer *t, bool success) {
176 timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD);
179 static void timer_enter_waiting(Timer *t, bool initial) {
181 usec_t base = 0, delay, n;
185 n = now(CLOCK_MONOTONIC);
187 LIST_FOREACH(value, v, t->values) {
195 if (state_translation_table[t->state] == UNIT_ACTIVE) {
196 base = t->meta.inactive_exit_timestamp.monotonic;
202 /* CLOCK_MONOTONIC equals the uptime on Linux */
207 base = t->meta.manager->startup_timestamp.monotonic;
210 case TIMER_UNIT_ACTIVE:
212 if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
215 base = t->unit->meta.inactive_exit_timestamp.monotonic;
218 case TIMER_UNIT_INACTIVE:
220 if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
223 base = t->unit->meta.inactive_enter_timestamp.monotonic;
227 assert_not_reached("Unknown timer base");
230 v->next_elapse = base + v->value;
232 if (!initial && v->next_elapse < n) {
238 t->next_elapse = v->next_elapse;
240 t->next_elapse = MIN(t->next_elapse, v->next_elapse);
246 timer_set_state(t, TIMER_ELAPSED);
250 delay = n < t->next_elapse ? t->next_elapse - n : 0;
252 if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
255 timer_set_state(t, TIMER_WAITING);
259 log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
260 timer_enter_dead(t, false);
263 static void timer_enter_running(Timer *t) {
267 if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0)
270 timer_set_state(t, TIMER_RUNNING);
274 log_warning("%s failed to queue unit startup job: %s", t->meta.id, strerror(-r));
275 timer_enter_dead(t, false);
278 static int timer_start(Unit *u) {
282 assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE);
284 if (t->unit->meta.load_state != UNIT_LOADED)
288 timer_enter_waiting(t, true);
292 static int timer_stop(Unit *u) {
296 assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
298 timer_enter_dead(t, true);
302 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
309 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
314 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
322 if (streq(key, "state")) {
325 if ((state = timer_state_from_string(value)) < 0)
326 log_debug("Failed to parse state value %s", value);
328 t->deserialized_state = state;
330 log_debug("Unknown serialization key '%s'", key);
335 static UnitActiveState timer_active_state(Unit *u) {
338 return state_translation_table[TIMER(u)->state];
341 static const char *timer_sub_state_to_string(Unit *u) {
344 return timer_state_to_string(TIMER(u)->state);
347 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
351 assert(elapsed == 1);
353 if (t->state != TIMER_WAITING)
356 log_debug("Timer elapsed on %s", u->meta.id);
357 timer_enter_running(t);
360 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
365 if (u->meta.type == UNIT_TIMER)
368 SET_FOREACH(n, u->meta.names, i) {
374 if (!(k = unit_name_change_suffix(n, ".timer"))) {
379 p = manager_get_unit(u->meta.manager, k);
385 if (p->meta.load_state != UNIT_LOADED)
393 /* Reenable all timers that depend on unit state */
394 LIST_FOREACH(value, v, t->values)
395 if (v->base == TIMER_UNIT_ACTIVE ||
396 v->base == TIMER_UNIT_INACTIVE)
404 /* Recalculate sleep time */
405 timer_enter_waiting(t, false);
410 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) {
411 log_debug("%s got notified about unit deactivation.", t->meta.id);
412 timer_enter_waiting(t, false);
418 case TIMER_MAINTENANCE:
422 assert_not_reached("Unknown timer state");
429 log_error("Failed find timer unit: %s", strerror(-r));
432 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
433 [TIMER_DEAD] = "dead",
434 [TIMER_WAITING] = "waiting",
435 [TIMER_RUNNING] = "running",
436 [TIMER_ELAPSED] = "elapsed",
437 [TIMER_MAINTENANCE] = "maintenance"
440 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
442 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
443 [TIMER_ACTIVE] = "OnActiveSec",
444 [TIMER_BOOT] = "OnBootSec",
445 [TIMER_STARTUP] = "OnStartupSec",
446 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
447 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec"
450 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
452 const UnitVTable timer_vtable = {
459 .coldplug = timer_coldplug,
463 .start = timer_start,
466 .serialize = timer_serialize,
467 .deserialize_item = timer_deserialize_item,
469 .active_state = timer_active_state,
470 .sub_state_to_string = timer_sub_state_to_string,
472 .timer_event = timer_timer_event,
474 .bus_message_handler = bus_timer_message_handler