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