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_MAINTENANCE] = UNIT_INACTIVE
37 static void timer_init(Unit *u) {
41 assert(u->meta.load_state == UNIT_STUB);
43 t->next_elapse = (usec_t) -1;
46 static void timer_done(Unit *u) {
52 while ((v = t->values)) {
53 LIST_REMOVE(TimerValue, value, t->values, v);
57 unit_unwatch_timer(u, &t->timer_watch);
60 static int timer_verify(Timer *t) {
63 if (t->meta.load_state != UNIT_LOADED)
67 log_error("%s lacks value setting. Refusing.", t->meta.id);
74 static int timer_load(Unit *u) {
79 assert(u->meta.load_state == UNIT_STUB);
81 if ((r = unit_load_fragment_and_dropin(u)) < 0)
84 if (u->meta.load_state == UNIT_LOADED) {
87 if ((r = unit_load_related_unit(u, ".service", &t->unit)))
90 if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
94 return timer_verify(t);
97 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
103 timespan1[FORMAT_TIMESPAN_MAX];
105 p2 = strappend(prefix, "\t");
106 prefix2 = p2 ? p2 : prefix;
109 "%sTimer State: %s\n"
111 prefix, timer_state_to_string(t->state),
112 prefix, t->unit->meta.id);
114 LIST_FOREACH(value, v, t->values)
118 timer_base_to_string(v->base),
119 strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
124 static void timer_set_state(Timer *t, TimerState state) {
125 TimerState old_state;
128 old_state = t->state;
131 if (state != TIMER_WAITING)
132 unit_unwatch_timer(UNIT(t), &t->timer_watch);
134 if (state != old_state)
135 log_debug("%s changed %s -> %s",
137 timer_state_to_string(old_state),
138 timer_state_to_string(state));
140 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
143 static void timer_enter_waiting(Timer *t, bool initial);
145 static int timer_coldplug(Unit *u) {
149 assert(t->state == TIMER_DEAD);
151 if (t->deserialized_state != t->state) {
153 if (t->deserialized_state == TIMER_WAITING ||
154 t->deserialized_state == TIMER_RUNNING ||
155 t->deserialized_state == TIMER_ELAPSED)
156 timer_enter_waiting(t, false);
158 timer_set_state(t, t->deserialized_state);
164 static void timer_enter_dead(Timer *t, bool success) {
170 timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD);
173 static void timer_enter_waiting(Timer *t, bool initial) {
175 usec_t base = 0, delay, n;
179 n = now(CLOCK_MONOTONIC);
181 LIST_FOREACH(value, v, t->values) {
189 if (state_translation_table[t->state] == UNIT_ACTIVE) {
190 base = t->meta.inactive_exit_timestamp.monotonic;
196 /* CLOCK_MONOTONIC equals the uptime on Linux */
201 base = t->meta.manager->startup_timestamp.monotonic;
204 case TIMER_UNIT_ACTIVE:
206 if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
209 base = t->unit->meta.inactive_exit_timestamp.monotonic;
212 case TIMER_UNIT_INACTIVE:
214 if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
217 base = t->unit->meta.inactive_enter_timestamp.monotonic;
221 assert_not_reached("Unknown timer base");
224 v->next_elapse = base + v->value;
226 if (!initial && v->next_elapse < n) {
232 t->next_elapse = v->next_elapse;
234 t->next_elapse = MIN(t->next_elapse, v->next_elapse);
240 timer_set_state(t, TIMER_ELAPSED);
244 delay = n < t->next_elapse ? t->next_elapse - n : 0;
246 if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
249 timer_set_state(t, TIMER_WAITING);
253 log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
254 timer_enter_dead(t, false);
257 static void timer_enter_running(Timer *t) {
261 if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0)
264 timer_set_state(t, TIMER_RUNNING);
268 log_warning("%s failed to queue unit startup job: %s", t->meta.id, strerror(-r));
269 timer_enter_dead(t, false);
272 static int timer_start(Unit *u) {
276 assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE);
278 if (t->unit->meta.load_state != UNIT_LOADED)
282 timer_enter_waiting(t, true);
286 static int timer_stop(Unit *u) {
290 assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
292 timer_enter_dead(t, true);
296 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
303 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
308 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
316 if (streq(key, "state")) {
319 if ((state = timer_state_from_string(value)) < 0)
320 log_debug("Failed to parse state value %s", value);
322 t->deserialized_state = state;
324 log_debug("Unknown serialization key '%s'", key);
329 static UnitActiveState timer_active_state(Unit *u) {
332 return state_translation_table[TIMER(u)->state];
335 static const char *timer_sub_state_to_string(Unit *u) {
338 return timer_state_to_string(TIMER(u)->state);
341 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
345 assert(elapsed == 1);
347 if (t->state != TIMER_WAITING)
350 log_debug("Timer elapsed on %s", u->meta.id);
351 timer_enter_running(t);
354 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
359 if (u->meta.type == UNIT_TIMER)
362 SET_FOREACH(n, u->meta.names, i) {
368 if (!(k = unit_name_change_suffix(n, ".timer"))) {
373 p = manager_get_unit(u->meta.manager, k);
379 if (p->meta.load_state != UNIT_LOADED)
387 /* Reenable all timers that depend on unit state */
388 LIST_FOREACH(value, v, t->values)
389 if (v->base == TIMER_UNIT_ACTIVE ||
390 v->base == TIMER_UNIT_INACTIVE)
398 /* Recalculate sleep time */
399 timer_enter_waiting(t, false);
404 if (new_state == UNIT_INACTIVE) {
405 log_debug("%s got notified about unit deactivation.", t->meta.id);
406 timer_enter_waiting(t, false);
412 case TIMER_MAINTENANCE:
416 assert_not_reached("Unknown timer state");
423 log_error("Failed find timer unit: %s", strerror(-r));
426 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
427 [TIMER_DEAD] = "dead",
428 [TIMER_WAITING] = "waiting",
429 [TIMER_RUNNING] = "running",
430 [TIMER_ELAPSED] = "elapsed",
431 [TIMER_MAINTENANCE] = "maintenance"
434 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
436 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
437 [TIMER_ACTIVE] = "OnActive",
438 [TIMER_BOOT] = "OnBoot",
439 [TIMER_STARTUP] = "OnStartup",
440 [TIMER_UNIT_ACTIVE] = "OnUnitActive",
441 [TIMER_UNIT_INACTIVE] = "OnUnitInactive"
444 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
446 const UnitVTable timer_vtable = {
453 .coldplug = timer_coldplug,
457 .start = timer_start,
460 .serialize = timer_serialize,
461 .deserialize_item = timer_deserialize_item,
463 .active_state = timer_active_state,
464 .sub_state_to_string = timer_sub_state_to_string,
466 .timer_event = timer_timer_event,
468 .bus_message_handler = bus_timer_message_handler