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 #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_MAINTENANCE] = UNIT_MAINTENANCE
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_load(Unit *u) {
81 assert(u->meta.load_state == UNIT_STUB);
83 if ((r = unit_load_fragment_and_dropin(u)) < 0)
86 if (u->meta.load_state == UNIT_LOADED) {
89 if ((r = unit_load_related_unit(u, ".service", &t->unit)))
92 if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
95 /* Timers shouldn't stay around on shutdown */
96 if (t->meta.default_dependencies)
97 if ((r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
101 return timer_verify(t);
104 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
110 timespan1[FORMAT_TIMESPAN_MAX];
112 p2 = strappend(prefix, "\t");
113 prefix2 = p2 ? p2 : prefix;
116 "%sTimer State: %s\n"
118 prefix, timer_state_to_string(t->state),
119 prefix, t->unit->meta.id);
121 LIST_FOREACH(value, v, t->values)
125 timer_base_to_string(v->base),
126 strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
131 static void timer_set_state(Timer *t, TimerState state) {
132 TimerState old_state;
135 old_state = t->state;
138 if (state != TIMER_WAITING)
139 unit_unwatch_timer(UNIT(t), &t->timer_watch);
141 if (state != old_state)
142 log_debug("%s changed %s -> %s",
144 timer_state_to_string(old_state),
145 timer_state_to_string(state));
147 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
150 static void timer_enter_waiting(Timer *t, bool initial);
152 static int timer_coldplug(Unit *u) {
156 assert(t->state == TIMER_DEAD);
158 if (t->deserialized_state != t->state) {
160 if (t->deserialized_state == TIMER_WAITING ||
161 t->deserialized_state == TIMER_RUNNING ||
162 t->deserialized_state == TIMER_ELAPSED)
163 timer_enter_waiting(t, false);
165 timer_set_state(t, t->deserialized_state);
171 static void timer_enter_dead(Timer *t, bool success) {
177 timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD);
180 static void timer_enter_waiting(Timer *t, bool initial) {
182 usec_t base = 0, delay, n;
186 n = now(CLOCK_MONOTONIC);
188 LIST_FOREACH(value, v, t->values) {
196 if (state_translation_table[t->state] == UNIT_ACTIVE) {
197 base = t->meta.inactive_exit_timestamp.monotonic;
203 /* CLOCK_MONOTONIC equals the uptime on Linux */
208 base = t->meta.manager->startup_timestamp.monotonic;
211 case TIMER_UNIT_ACTIVE:
213 if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
216 base = t->unit->meta.inactive_exit_timestamp.monotonic;
219 case TIMER_UNIT_INACTIVE:
221 if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
224 base = t->unit->meta.inactive_enter_timestamp.monotonic;
228 assert_not_reached("Unknown timer base");
231 v->next_elapse = base + v->value;
233 if (!initial && v->next_elapse < n) {
239 t->next_elapse = v->next_elapse;
241 t->next_elapse = MIN(t->next_elapse, v->next_elapse);
247 timer_set_state(t, TIMER_ELAPSED);
251 delay = n < t->next_elapse ? t->next_elapse - n : 0;
253 if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
256 timer_set_state(t, TIMER_WAITING);
260 log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
261 timer_enter_dead(t, false);
264 static void timer_enter_running(Timer *t) {
269 dbus_error_init(&error);
271 /* Don't start job if we are supposed to go down */
272 if (t->meta.job && t->meta.job->type == JOB_STOP)
275 if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0)
278 timer_set_state(t, TIMER_RUNNING);
282 log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r));
283 timer_enter_dead(t, false);
285 dbus_error_free(&error);
288 static int timer_start(Unit *u) {
292 assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE);
294 if (t->unit->meta.load_state != UNIT_LOADED)
298 timer_enter_waiting(t, true);
302 static int timer_stop(Unit *u) {
306 assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
308 timer_enter_dead(t, true);
312 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
319 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
324 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
332 if (streq(key, "state")) {
335 if ((state = timer_state_from_string(value)) < 0)
336 log_debug("Failed to parse state value %s", value);
338 t->deserialized_state = state;
340 log_debug("Unknown serialization key '%s'", key);
345 static UnitActiveState timer_active_state(Unit *u) {
348 return state_translation_table[TIMER(u)->state];
351 static const char *timer_sub_state_to_string(Unit *u) {
354 return timer_state_to_string(TIMER(u)->state);
357 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
361 assert(elapsed == 1);
363 if (t->state != TIMER_WAITING)
366 log_debug("Timer elapsed on %s", u->meta.id);
367 timer_enter_running(t);
370 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
375 if (u->meta.type == UNIT_TIMER)
378 SET_FOREACH(n, u->meta.names, i) {
384 if (!(k = unit_name_change_suffix(n, ".timer"))) {
389 p = manager_get_unit(u->meta.manager, k);
395 if (p->meta.load_state != UNIT_LOADED)
403 /* Reenable all timers that depend on unit state */
404 LIST_FOREACH(value, v, t->values)
405 if (v->base == TIMER_UNIT_ACTIVE ||
406 v->base == TIMER_UNIT_INACTIVE)
414 /* Recalculate sleep time */
415 timer_enter_waiting(t, false);
420 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) {
421 log_debug("%s got notified about unit deactivation.", t->meta.id);
422 timer_enter_waiting(t, false);
428 case TIMER_MAINTENANCE:
432 assert_not_reached("Unknown timer state");
439 log_error("Failed find timer unit: %s", strerror(-r));
442 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
443 [TIMER_DEAD] = "dead",
444 [TIMER_WAITING] = "waiting",
445 [TIMER_RUNNING] = "running",
446 [TIMER_ELAPSED] = "elapsed",
447 [TIMER_MAINTENANCE] = "maintenance"
450 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
452 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
453 [TIMER_ACTIVE] = "OnActiveSec",
454 [TIMER_BOOT] = "OnBootSec",
455 [TIMER_STARTUP] = "OnStartupSec",
456 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
457 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec"
460 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
462 const UnitVTable timer_vtable = {
469 .coldplug = timer_coldplug,
473 .start = timer_start,
476 .serialize = timer_serialize,
477 .deserialize_item = timer_deserialize_item,
479 .active_state = timer_active_state,
480 .sub_state_to_string = timer_sub_state_to_string,
482 .timer_event = timer_timer_event,
484 .bus_message_handler = bus_timer_message_handler