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