]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
d46de8a1 LP |
22 | #include <errno.h> |
23 | ||
87f0e418 | 24 | #include "unit.h" |
871d7de4 | 25 | #include "unit-name.h" |
5cb5a6ff | 26 | #include "timer.h" |
871d7de4 | 27 | #include "dbus-timer.h" |
a40eb732 | 28 | #include "special.h" |
398ef8ba | 29 | #include "bus-errors.h" |
871d7de4 LP |
30 | |
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, | |
fdf20a31 | 36 | [TIMER_FAILED] = UNIT_FAILED |
871d7de4 LP |
37 | }; |
38 | ||
39 | static void timer_init(Unit *u) { | |
40 | Timer *t = TIMER(u); | |
41 | ||
42 | assert(u); | |
43 | assert(u->meta.load_state == UNIT_STUB); | |
44 | ||
45 | t->next_elapse = (usec_t) -1; | |
871d7de4 | 46 | } |
5cb5a6ff | 47 | |
87f0e418 LP |
48 | static void timer_done(Unit *u) { |
49 | Timer *t = TIMER(u); | |
871d7de4 LP |
50 | TimerValue *v; |
51 | ||
52 | assert(t); | |
53 | ||
54 | while ((v = t->values)) { | |
55 | LIST_REMOVE(TimerValue, value, t->values, v); | |
56 | free(v); | |
57 | } | |
58 | ||
59 | unit_unwatch_timer(u, &t->timer_watch); | |
60 | } | |
61 | ||
62 | static int timer_verify(Timer *t) { | |
63 | assert(t); | |
64 | ||
4cd1fbcc | 65 | if (t->meta.load_state != UNIT_LOADED) |
871d7de4 LP |
66 | return 0; |
67 | ||
68 | if (!t->values) { | |
69 | log_error("%s lacks value setting. Refusing.", t->meta.id); | |
70 | return -EINVAL; | |
71 | } | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
6c155fe3 LP |
76 | static int timer_add_default_dependencies(Timer *t) { |
77 | int r; | |
78 | ||
79 | assert(t); | |
80 | ||
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) | |
83 | return r; | |
84 | ||
69dd2852 | 85 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
6c155fe3 LP |
86 | } |
87 | ||
871d7de4 LP |
88 | static int timer_load(Unit *u) { |
89 | Timer *t = TIMER(u); | |
90 | int r; | |
91 | ||
92 | assert(u); | |
93 | assert(u->meta.load_state == UNIT_STUB); | |
94 | ||
95 | if ((r = unit_load_fragment_and_dropin(u)) < 0) | |
96 | return r; | |
97 | ||
98 | if (u->meta.load_state == UNIT_LOADED) { | |
99 | ||
100 | if (!t->unit) | |
101 | if ((r = unit_load_related_unit(u, ".service", &t->unit))) | |
102 | return r; | |
103 | ||
104 | if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0) | |
105 | return r; | |
a40eb732 | 106 | |
a40eb732 | 107 | if (t->meta.default_dependencies) |
6c155fe3 | 108 | if ((r = timer_add_default_dependencies(t)) < 0) |
a40eb732 | 109 | return r; |
871d7de4 LP |
110 | } |
111 | ||
112 | return timer_verify(t); | |
113 | } | |
114 | ||
115 | static void timer_dump(Unit *u, FILE *f, const char *prefix) { | |
116 | Timer *t = TIMER(u); | |
871d7de4 LP |
117 | TimerValue *v; |
118 | char | |
119 | timespan1[FORMAT_TIMESPAN_MAX]; | |
034c6ed7 | 120 | |
871d7de4 LP |
121 | fprintf(f, |
122 | "%sTimer State: %s\n" | |
123 | "%sUnit: %s\n", | |
124 | prefix, timer_state_to_string(t->state), | |
125 | prefix, t->unit->meta.id); | |
126 | ||
127 | LIST_FOREACH(value, v, t->values) | |
128 | fprintf(f, | |
129 | "%s%s: %s\n", | |
130 | prefix, | |
131 | timer_base_to_string(v->base), | |
132 | strna(format_timespan(timespan1, sizeof(timespan1), v->value))); | |
871d7de4 LP |
133 | } |
134 | ||
135 | static void timer_set_state(Timer *t, TimerState state) { | |
136 | TimerState old_state; | |
034c6ed7 | 137 | assert(t); |
871d7de4 LP |
138 | |
139 | old_state = t->state; | |
140 | t->state = state; | |
141 | ||
142 | if (state != TIMER_WAITING) | |
143 | unit_unwatch_timer(UNIT(t), &t->timer_watch); | |
144 | ||
145 | if (state != old_state) | |
146 | log_debug("%s changed %s -> %s", | |
147 | t->meta.id, | |
148 | timer_state_to_string(old_state), | |
149 | timer_state_to_string(state)); | |
150 | ||
151 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); | |
152 | } | |
153 | ||
154 | static void timer_enter_waiting(Timer *t, bool initial); | |
155 | ||
156 | static int timer_coldplug(Unit *u) { | |
157 | Timer *t = TIMER(u); | |
158 | ||
159 | assert(t); | |
160 | assert(t->state == TIMER_DEAD); | |
161 | ||
162 | if (t->deserialized_state != t->state) { | |
163 | ||
b363ca6f | 164 | if (t->deserialized_state == TIMER_WAITING) |
871d7de4 LP |
165 | timer_enter_waiting(t, false); |
166 | else | |
167 | timer_set_state(t, t->deserialized_state); | |
168 | } | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | static void timer_enter_dead(Timer *t, bool success) { | |
174 | assert(t); | |
175 | ||
176 | if (!success) | |
177 | t->failure = true; | |
178 | ||
fdf20a31 | 179 | timer_set_state(t, t->failure ? TIMER_FAILED : TIMER_DEAD); |
871d7de4 LP |
180 | } |
181 | ||
182 | static void timer_enter_waiting(Timer *t, bool initial) { | |
183 | TimerValue *v; | |
184 | usec_t base = 0, delay, n; | |
185 | bool found = false; | |
186 | int r; | |
187 | ||
188 | n = now(CLOCK_MONOTONIC); | |
189 | ||
190 | LIST_FOREACH(value, v, t->values) { | |
191 | ||
192 | if (v->disabled) | |
193 | continue; | |
194 | ||
195 | switch (v->base) { | |
196 | ||
197 | case TIMER_ACTIVE: | |
10717a1a | 198 | if (state_translation_table[t->state] == UNIT_ACTIVE) |
871d7de4 | 199 | base = t->meta.inactive_exit_timestamp.monotonic; |
10717a1a | 200 | else |
871d7de4 LP |
201 | base = n; |
202 | break; | |
203 | ||
204 | case TIMER_BOOT: | |
205 | /* CLOCK_MONOTONIC equals the uptime on Linux */ | |
206 | base = 0; | |
207 | break; | |
208 | ||
209 | case TIMER_STARTUP: | |
210 | base = t->meta.manager->startup_timestamp.monotonic; | |
211 | break; | |
212 | ||
213 | case TIMER_UNIT_ACTIVE: | |
214 | ||
215 | if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0) | |
216 | continue; | |
217 | ||
218 | base = t->unit->meta.inactive_exit_timestamp.monotonic; | |
219 | break; | |
220 | ||
221 | case TIMER_UNIT_INACTIVE: | |
222 | ||
223 | if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0) | |
224 | continue; | |
225 | ||
226 | base = t->unit->meta.inactive_enter_timestamp.monotonic; | |
227 | break; | |
228 | ||
229 | default: | |
230 | assert_not_reached("Unknown timer base"); | |
231 | } | |
232 | ||
233 | v->next_elapse = base + v->value; | |
234 | ||
235 | if (!initial && v->next_elapse < n) { | |
236 | v->disabled = true; | |
237 | continue; | |
238 | } | |
239 | ||
240 | if (!found) | |
241 | t->next_elapse = v->next_elapse; | |
242 | else | |
243 | t->next_elapse = MIN(t->next_elapse, v->next_elapse); | |
244 | ||
245 | found = true; | |
246 | } | |
247 | ||
248 | if (!found) { | |
249 | timer_set_state(t, TIMER_ELAPSED); | |
250 | return; | |
251 | } | |
252 | ||
253 | delay = n < t->next_elapse ? t->next_elapse - n : 0; | |
254 | ||
255 | if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0) | |
256 | goto fail; | |
257 | ||
258 | timer_set_state(t, TIMER_WAITING); | |
259 | return; | |
260 | ||
261 | fail: | |
262 | log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r)); | |
263 | timer_enter_dead(t, false); | |
264 | } | |
265 | ||
266 | static void timer_enter_running(Timer *t) { | |
398ef8ba | 267 | DBusError error; |
871d7de4 | 268 | int r; |
398ef8ba | 269 | |
871d7de4 | 270 | assert(t); |
398ef8ba | 271 | dbus_error_init(&error); |
871d7de4 | 272 | |
ba3e67a7 LP |
273 | /* Don't start job if we are supposed to go down */ |
274 | if (t->meta.job && t->meta.job->type == JOB_STOP) | |
275 | return; | |
276 | ||
398ef8ba | 277 | if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0) |
871d7de4 LP |
278 | goto fail; |
279 | ||
280 | timer_set_state(t, TIMER_RUNNING); | |
281 | return; | |
282 | ||
283 | fail: | |
398ef8ba | 284 | log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r)); |
871d7de4 | 285 | timer_enter_dead(t, false); |
398ef8ba LP |
286 | |
287 | dbus_error_free(&error); | |
871d7de4 LP |
288 | } |
289 | ||
290 | static int timer_start(Unit *u) { | |
291 | Timer *t = TIMER(u); | |
292 | ||
293 | assert(t); | |
fdf20a31 | 294 | assert(t->state == TIMER_DEAD || t->state == TIMER_FAILED); |
01f78473 LP |
295 | |
296 | if (t->unit->meta.load_state != UNIT_LOADED) | |
297 | return -ENOENT; | |
871d7de4 | 298 | |
01f78473 | 299 | t->failure = false; |
871d7de4 LP |
300 | timer_enter_waiting(t, true); |
301 | return 0; | |
302 | } | |
303 | ||
304 | static int timer_stop(Unit *u) { | |
305 | Timer *t = TIMER(u); | |
306 | ||
307 | assert(t); | |
308 | assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED); | |
309 | ||
310 | timer_enter_dead(t, true); | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int timer_serialize(Unit *u, FILE *f, FDSet *fds) { | |
315 | Timer *t = TIMER(u); | |
316 | ||
317 | assert(u); | |
318 | assert(f); | |
319 | assert(fds); | |
320 | ||
321 | unit_serialize_item(u, f, "state", timer_state_to_string(t->state)); | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
327 | Timer *t = TIMER(u); | |
328 | ||
329 | assert(u); | |
330 | assert(key); | |
331 | assert(value); | |
332 | assert(fds); | |
333 | ||
334 | if (streq(key, "state")) { | |
335 | TimerState state; | |
336 | ||
337 | if ((state = timer_state_from_string(value)) < 0) | |
338 | log_debug("Failed to parse state value %s", value); | |
339 | else | |
340 | t->deserialized_state = state; | |
341 | } else | |
342 | log_debug("Unknown serialization key '%s'", key); | |
343 | ||
344 | return 0; | |
034c6ed7 LP |
345 | } |
346 | ||
87f0e418 | 347 | static UnitActiveState timer_active_state(Unit *u) { |
871d7de4 LP |
348 | assert(u); |
349 | ||
350 | return state_translation_table[TIMER(u)->state]; | |
351 | } | |
352 | ||
353 | static const char *timer_sub_state_to_string(Unit *u) { | |
354 | assert(u); | |
355 | ||
356 | return timer_state_to_string(TIMER(u)->state); | |
357 | } | |
358 | ||
359 | static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) { | |
360 | Timer *t = TIMER(u); | |
361 | ||
362 | assert(t); | |
363 | assert(elapsed == 1); | |
5cb5a6ff | 364 | |
871d7de4 LP |
365 | if (t->state != TIMER_WAITING) |
366 | return; | |
5cb5a6ff | 367 | |
871d7de4 LP |
368 | log_debug("Timer elapsed on %s", u->meta.id); |
369 | timer_enter_running(t); | |
5cb5a6ff LP |
370 | } |
371 | ||
871d7de4 LP |
372 | void timer_unit_notify(Unit *u, UnitActiveState new_state) { |
373 | char *n; | |
374 | int r; | |
375 | Iterator i; | |
376 | ||
377 | if (u->meta.type == UNIT_TIMER) | |
378 | return; | |
379 | ||
380 | SET_FOREACH(n, u->meta.names, i) { | |
381 | char *k; | |
382 | Unit *p; | |
383 | Timer *t; | |
384 | TimerValue *v; | |
385 | ||
386 | if (!(k = unit_name_change_suffix(n, ".timer"))) { | |
387 | r = -ENOMEM; | |
388 | goto fail; | |
389 | } | |
390 | ||
391 | p = manager_get_unit(u->meta.manager, k); | |
392 | free(k); | |
393 | ||
394 | if (!p) | |
395 | continue; | |
396 | ||
01f78473 LP |
397 | if (p->meta.load_state != UNIT_LOADED) |
398 | continue; | |
399 | ||
871d7de4 LP |
400 | t = TIMER(p); |
401 | ||
01f78473 | 402 | if (t->unit != u) |
871d7de4 LP |
403 | continue; |
404 | ||
405 | /* Reenable all timers that depend on unit state */ | |
406 | LIST_FOREACH(value, v, t->values) | |
407 | if (v->base == TIMER_UNIT_ACTIVE || | |
408 | v->base == TIMER_UNIT_INACTIVE) | |
409 | v->disabled = false; | |
410 | ||
411 | switch (t->state) { | |
412 | ||
413 | case TIMER_WAITING: | |
414 | case TIMER_ELAPSED: | |
415 | ||
416 | /* Recalculate sleep time */ | |
417 | timer_enter_waiting(t, false); | |
418 | break; | |
419 | ||
420 | case TIMER_RUNNING: | |
421 | ||
fdf20a31 | 422 | if (UNIT_IS_INACTIVE_OR_FAILED(new_state)) { |
871d7de4 LP |
423 | log_debug("%s got notified about unit deactivation.", t->meta.id); |
424 | timer_enter_waiting(t, false); | |
425 | } | |
426 | ||
427 | break; | |
428 | ||
429 | case TIMER_DEAD: | |
fdf20a31 | 430 | case TIMER_FAILED: |
0b021426 | 431 | break; |
871d7de4 LP |
432 | |
433 | default: | |
434 | assert_not_reached("Unknown timer state"); | |
435 | } | |
436 | } | |
437 | ||
438 | return; | |
439 | ||
440 | fail: | |
441 | log_error("Failed find timer unit: %s", strerror(-r)); | |
442 | } | |
443 | ||
fdf20a31 | 444 | static void timer_reset_failed(Unit *u) { |
5632e374 LP |
445 | Timer *t = TIMER(u); |
446 | ||
447 | assert(t); | |
448 | ||
fdf20a31 | 449 | if (t->state == TIMER_FAILED) |
5632e374 LP |
450 | timer_set_state(t, TIMER_DEAD); |
451 | ||
452 | t->failure = false; | |
453 | } | |
454 | ||
871d7de4 LP |
455 | static const char* const timer_state_table[_TIMER_STATE_MAX] = { |
456 | [TIMER_DEAD] = "dead", | |
457 | [TIMER_WAITING] = "waiting", | |
458 | [TIMER_RUNNING] = "running", | |
459 | [TIMER_ELAPSED] = "elapsed", | |
fdf20a31 | 460 | [TIMER_FAILED] = "failed" |
871d7de4 LP |
461 | }; |
462 | ||
463 | DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState); | |
464 | ||
465 | static const char* const timer_base_table[_TIMER_BASE_MAX] = { | |
03fae018 LP |
466 | [TIMER_ACTIVE] = "OnActiveSec", |
467 | [TIMER_BOOT] = "OnBootSec", | |
468 | [TIMER_STARTUP] = "OnStartupSec", | |
469 | [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec", | |
470 | [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec" | |
871d7de4 LP |
471 | }; |
472 | ||
473 | DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase); | |
474 | ||
87f0e418 | 475 | const UnitVTable timer_vtable = { |
5cb5a6ff LP |
476 | .suffix = ".timer", |
477 | ||
871d7de4 | 478 | .init = timer_init, |
034c6ed7 | 479 | .done = timer_done, |
871d7de4 LP |
480 | .load = timer_load, |
481 | ||
482 | .coldplug = timer_coldplug, | |
483 | ||
484 | .dump = timer_dump, | |
485 | ||
486 | .start = timer_start, | |
487 | .stop = timer_stop, | |
488 | ||
489 | .serialize = timer_serialize, | |
490 | .deserialize_item = timer_deserialize_item, | |
491 | ||
492 | .active_state = timer_active_state, | |
493 | .sub_state_to_string = timer_sub_state_to_string, | |
494 | ||
495 | .timer_event = timer_timer_event, | |
5cb5a6ff | 496 | |
fdf20a31 | 497 | .reset_failed = timer_reset_failed, |
5632e374 | 498 | |
c4e2ceae LP |
499 | .bus_interface = "org.freedesktop.systemd1.Timer", |
500 | .bus_message_handler = bus_timer_message_handler, | |
501 | .bus_invalidating_properties = bus_timer_invalidating_properties | |
5cb5a6ff | 502 | }; |