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_add_default_dependencies(Timer *t) {
81 if (t->meta.manager->running_as == MANAGER_SYSTEM)
82 if ((r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
85 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
88 static int timer_load(Unit *u) {
93 assert(u->meta.load_state == UNIT_STUB);
95 if ((r = unit_load_fragment_and_dropin(u)) < 0)
98 if (u->meta.load_state == UNIT_LOADED) {
101 if ((r = unit_load_related_unit(u, ".service", &t->unit)))
104 if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
107 if (t->meta.default_dependencies)
108 if ((r = timer_add_default_dependencies(t)) < 0)
112 return timer_verify(t);
115 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
121 timespan1[FORMAT_TIMESPAN_MAX];
123 p2 = strappend(prefix, "\t");
124 prefix2 = p2 ? p2 : prefix;
127 "%sTimer State: %s\n"
129 prefix, timer_state_to_string(t->state),
130 prefix, t->unit->meta.id);
132 LIST_FOREACH(value, v, t->values)
136 timer_base_to_string(v->base),
137 strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
142 static void timer_set_state(Timer *t, TimerState state) {
143 TimerState old_state;
146 old_state = t->state;
149 if (state != TIMER_WAITING)
150 unit_unwatch_timer(UNIT(t), &t->timer_watch);
152 if (state != old_state)
153 log_debug("%s changed %s -> %s",
155 timer_state_to_string(old_state),
156 timer_state_to_string(state));
158 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
161 static void timer_enter_waiting(Timer *t, bool initial);
163 static int timer_coldplug(Unit *u) {
167 assert(t->state == TIMER_DEAD);
169 if (t->deserialized_state != t->state) {
171 if (t->deserialized_state == TIMER_WAITING ||
172 t->deserialized_state == TIMER_RUNNING ||
173 t->deserialized_state == TIMER_ELAPSED)
174 timer_enter_waiting(t, false);
176 timer_set_state(t, t->deserialized_state);
182 static void timer_enter_dead(Timer *t, bool success) {
188 timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD);
191 static void timer_enter_waiting(Timer *t, bool initial) {
193 usec_t base = 0, delay, n;
197 n = now(CLOCK_MONOTONIC);
199 LIST_FOREACH(value, v, t->values) {
207 if (state_translation_table[t->state] == UNIT_ACTIVE) {
208 base = t->meta.inactive_exit_timestamp.monotonic;
214 /* CLOCK_MONOTONIC equals the uptime on Linux */
219 base = t->meta.manager->startup_timestamp.monotonic;
222 case TIMER_UNIT_ACTIVE:
224 if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
227 base = t->unit->meta.inactive_exit_timestamp.monotonic;
230 case TIMER_UNIT_INACTIVE:
232 if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
235 base = t->unit->meta.inactive_enter_timestamp.monotonic;
239 assert_not_reached("Unknown timer base");
242 v->next_elapse = base + v->value;
244 if (!initial && v->next_elapse < n) {
250 t->next_elapse = v->next_elapse;
252 t->next_elapse = MIN(t->next_elapse, v->next_elapse);
258 timer_set_state(t, TIMER_ELAPSED);
262 delay = n < t->next_elapse ? t->next_elapse - n : 0;
264 if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
267 timer_set_state(t, TIMER_WAITING);
271 log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
272 timer_enter_dead(t, false);
275 static void timer_enter_running(Timer *t) {
280 dbus_error_init(&error);
282 /* Don't start job if we are supposed to go down */
283 if (t->meta.job && t->meta.job->type == JOB_STOP)
286 if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0)
289 timer_set_state(t, TIMER_RUNNING);
293 log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r));
294 timer_enter_dead(t, false);
296 dbus_error_free(&error);
299 static int timer_start(Unit *u) {
303 assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE);
305 if (t->unit->meta.load_state != UNIT_LOADED)
309 timer_enter_waiting(t, true);
313 static int timer_stop(Unit *u) {
317 assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
319 timer_enter_dead(t, true);
323 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
330 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
335 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
343 if (streq(key, "state")) {
346 if ((state = timer_state_from_string(value)) < 0)
347 log_debug("Failed to parse state value %s", value);
349 t->deserialized_state = state;
351 log_debug("Unknown serialization key '%s'", key);
356 static UnitActiveState timer_active_state(Unit *u) {
359 return state_translation_table[TIMER(u)->state];
362 static const char *timer_sub_state_to_string(Unit *u) {
365 return timer_state_to_string(TIMER(u)->state);
368 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
372 assert(elapsed == 1);
374 if (t->state != TIMER_WAITING)
377 log_debug("Timer elapsed on %s", u->meta.id);
378 timer_enter_running(t);
381 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
386 if (u->meta.type == UNIT_TIMER)
389 SET_FOREACH(n, u->meta.names, i) {
395 if (!(k = unit_name_change_suffix(n, ".timer"))) {
400 p = manager_get_unit(u->meta.manager, k);
406 if (p->meta.load_state != UNIT_LOADED)
414 /* Reenable all timers that depend on unit state */
415 LIST_FOREACH(value, v, t->values)
416 if (v->base == TIMER_UNIT_ACTIVE ||
417 v->base == TIMER_UNIT_INACTIVE)
425 /* Recalculate sleep time */
426 timer_enter_waiting(t, false);
431 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) {
432 log_debug("%s got notified about unit deactivation.", t->meta.id);
433 timer_enter_waiting(t, false);
439 case TIMER_MAINTENANCE:
443 assert_not_reached("Unknown timer state");
450 log_error("Failed find timer unit: %s", strerror(-r));
453 static void timer_reset_maintenance(Unit *u) {
458 if (t->state == TIMER_MAINTENANCE)
459 timer_set_state(t, TIMER_DEAD);
464 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
465 [TIMER_DEAD] = "dead",
466 [TIMER_WAITING] = "waiting",
467 [TIMER_RUNNING] = "running",
468 [TIMER_ELAPSED] = "elapsed",
469 [TIMER_MAINTENANCE] = "maintenance"
472 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
474 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
475 [TIMER_ACTIVE] = "OnActiveSec",
476 [TIMER_BOOT] = "OnBootSec",
477 [TIMER_STARTUP] = "OnStartupSec",
478 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
479 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec"
482 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
484 const UnitVTable timer_vtable = {
491 .coldplug = timer_coldplug,
495 .start = timer_start,
498 .serialize = timer_serialize,
499 .deserialize_item = timer_deserialize_item,
501 .active_state = timer_active_state,
502 .sub_state_to_string = timer_sub_state_to_string,
504 .timer_event = timer_timer_event,
506 .reset_maintenance = timer_reset_maintenance,
508 .bus_message_handler = bus_timer_message_handler