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