]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/timer.c
core: make manager_serialize() a bit easier to read by adding predicate function
[thirdparty/systemd.git] / src / core / timer.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09 2
d46de8a1
LP
3#include <errno.h>
4
b5efdb8a 5#include "alloc-util.h"
07630cea
LP
6#include "bus-error.h"
7#include "bus-util.h"
871d7de4 8#include "dbus-timer.h"
f4f15635 9#include "fs-util.h"
6bedfcbb 10#include "parse-util.h"
744c7693 11#include "random-util.h"
a40eb732 12#include "special.h"
8b43440b 13#include "string-table.h"
07630cea 14#include "string-util.h"
b1d4f8e1 15#include "timer.h"
07630cea
LP
16#include "unit-name.h"
17#include "unit.h"
b1d4f8e1 18#include "user-util.h"
c1d9ba99 19#include "virt.h"
871d7de4
LP
20
21static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
22 [TIMER_DEAD] = UNIT_INACTIVE,
23 [TIMER_WAITING] = UNIT_ACTIVE,
24 [TIMER_RUNNING] = UNIT_ACTIVE,
25 [TIMER_ELAPSED] = UNIT_ACTIVE,
fdf20a31 26 [TIMER_FAILED] = UNIT_FAILED
871d7de4
LP
27};
28
718db961
LP
29static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata);
30
871d7de4
LP
31static void timer_init(Unit *u) {
32 Timer *t = TIMER(u);
33
34 assert(u);
ac155bb8 35 assert(u->load_state == UNIT_STUB);
871d7de4 36
3a43da28
KS
37 t->next_elapse_monotonic_or_boottime = USEC_INFINITY;
38 t->next_elapse_realtime = USEC_INFINITY;
bd8f585b 39 t->accuracy_usec = u->manager->default_timer_accuracy_usec;
3e0c30ac 40 t->remain_after_elapse = true;
871d7de4 41}
5cb5a6ff 42
74051b9b 43void timer_free_values(Timer *t) {
871d7de4
LP
44 TimerValue *v;
45
46 assert(t);
47
48 while ((v = t->values)) {
71fda00f 49 LIST_REMOVE(value, t->values, v);
3e044c49 50 calendar_spec_free(v->calendar_spec);
871d7de4
LP
51 free(v);
52 }
74051b9b
LP
53}
54
55static void timer_done(Unit *u) {
56 Timer *t = TIMER(u);
57
58 assert(t);
59
60 timer_free_values(t);
871d7de4 61
718db961
LP
62 t->monotonic_event_source = sd_event_source_unref(t->monotonic_event_source);
63 t->realtime_event_source = sd_event_source_unref(t->realtime_event_source);
06642d17
LP
64
65 free(t->stamp_path);
871d7de4
LP
66}
67
68static int timer_verify(Timer *t) {
69 assert(t);
70
1124fe6f 71 if (UNIT(t)->load_state != UNIT_LOADED)
871d7de4
LP
72 return 0;
73
74 if (!t->values) {
f2341e0a 75 log_unit_error(UNIT(t), "Timer unit lacks value setting. Refusing.");
6f40aa45 76 return -ENOEXEC;
871d7de4
LP
77 }
78
79 return 0;
80}
81
6c155fe3
LP
82static int timer_add_default_dependencies(Timer *t) {
83 int r;
19f8d037 84 TimerValue *v;
6c155fe3
LP
85
86 assert(t);
87
4c9ea260
LP
88 if (!UNIT(t)->default_dependencies)
89 return 0;
90
35d8c19a 91 r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_TIMERS_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
e3d84721
LP
92 if (r < 0)
93 return r;
2a77d31d 94
463d0d15 95 if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) {
5a724170 96 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
36697dc0 97 if (r < 0)
6c155fe3 98 return r;
19f8d037
TGR
99
100 LIST_FOREACH(value, v, t->values) {
101 if (v->base == TIMER_CALENDAR) {
35d8c19a 102 r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, SPECIAL_TIME_SYNC_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
19f8d037
TGR
103 if (r < 0)
104 return r;
105 break;
106 }
107 }
2a77d31d 108 }
6c155fe3 109
5a724170 110 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
eef85c4a
LP
111}
112
113static int timer_add_trigger_dependencies(Timer *t) {
114 Unit *x;
115 int r;
116
117 assert(t);
118
119 if (!hashmap_isempty(UNIT(t)->dependencies[UNIT_TRIGGERS]))
120 return 0;
121
122 r = unit_load_related_unit(UNIT(t), ".service", &x);
123 if (r < 0)
124 return r;
125
126 return unit_add_two_dependencies(UNIT(t), UNIT_BEFORE, UNIT_TRIGGERS, x, true, UNIT_DEPENDENCY_IMPLICIT);
6c155fe3
LP
127}
128
06642d17
LP
129static int timer_setup_persistent(Timer *t) {
130 int r;
131
132 assert(t);
133
134 if (!t->persistent)
135 return 0;
136
463d0d15 137 if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) {
06642d17 138
eef85c4a 139 r = unit_require_mounts_for(UNIT(t), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE);
06642d17
LP
140 if (r < 0)
141 return r;
142
143 t->stamp_path = strappend("/var/lib/systemd/timers/stamp-", UNIT(t)->id);
144 } else {
145 const char *e;
146
147 e = getenv("XDG_DATA_HOME");
148 if (e)
605405c6 149 t->stamp_path = strjoin(e, "/systemd/timers/stamp-", UNIT(t)->id);
06642d17
LP
150 else {
151
152 _cleanup_free_ char *h = NULL;
153
154 r = get_home_dir(&h);
23bbb0de 155 if (r < 0)
f2341e0a 156 return log_unit_error_errno(UNIT(t), r, "Failed to determine home directory: %m");
06642d17 157
605405c6 158 t->stamp_path = strjoin(h, "/.local/share/systemd/timers/stamp-", UNIT(t)->id);
06642d17
LP
159 }
160 }
161
162 if (!t->stamp_path)
163 return log_oom();
164
165 return 0;
166}
167
871d7de4
LP
168static int timer_load(Unit *u) {
169 Timer *t = TIMER(u);
170 int r;
171
172 assert(u);
ac155bb8 173 assert(u->load_state == UNIT_STUB);
871d7de4 174
36697dc0
LP
175 r = unit_load_fragment_and_dropin(u);
176 if (r < 0)
871d7de4
LP
177 return r;
178
ac155bb8 179 if (u->load_state == UNIT_LOADED) {
871d7de4 180
eef85c4a
LP
181 r = timer_add_trigger_dependencies(t);
182 if (r < 0)
183 return r;
57020a3a 184
06642d17
LP
185 r = timer_setup_persistent(t);
186 if (r < 0)
187 return r;
188
4c9ea260
LP
189 r = timer_add_default_dependencies(t);
190 if (r < 0)
191 return r;
871d7de4
LP
192 }
193
194 return timer_verify(t);
195}
196
197static void timer_dump(Unit *u, FILE *f, const char *prefix) {
9f5eb56a 198 char buf[FORMAT_TIMESPAN_MAX];
871d7de4 199 Timer *t = TIMER(u);
3ecaa09b 200 Unit *trigger;
871d7de4 201 TimerValue *v;
034c6ed7 202
3ecaa09b
LP
203 trigger = UNIT_TRIGGER(u);
204
871d7de4
LP
205 fprintf(f,
206 "%sTimer State: %s\n"
067d72c9 207 "%sResult: %s\n"
9f5eb56a 208 "%sUnit: %s\n"
dedabea4
LP
209 "%sPersistent: %s\n"
210 "%sWakeSystem: %s\n"
3e0c30ac
LP
211 "%sAccuracy: %s\n"
212 "%sRemainAfterElapse: %s\n",
871d7de4 213 prefix, timer_state_to_string(t->state),
067d72c9 214 prefix, timer_result_to_string(t->result),
9f5eb56a 215 prefix, trigger ? trigger->id : "n/a",
dedabea4
LP
216 prefix, yes_no(t->persistent),
217 prefix, yes_no(t->wake_system),
3e0c30ac
LP
218 prefix, format_timespan(buf, sizeof(buf), t->accuracy_usec, 1),
219 prefix, yes_no(t->remain_after_elapse));
871d7de4 220
36697dc0
LP
221 LIST_FOREACH(value, v, t->values) {
222
223 if (v->base == TIMER_CALENDAR) {
224 _cleanup_free_ char *p = NULL;
225
25cb94d4 226 (void) calendar_spec_to_string(v->calendar_spec, &p);
36697dc0
LP
227
228 fprintf(f,
229 "%s%s: %s\n",
230 prefix,
231 timer_base_to_string(v->base),
232 strna(p));
233 } else {
234 char timespan1[FORMAT_TIMESPAN_MAX];
235
236 fprintf(f,
237 "%s%s: %s\n",
238 prefix,
239 timer_base_to_string(v->base),
b1d6dcf5 240 format_timespan(timespan1, sizeof(timespan1), v->value, 0));
36697dc0
LP
241 }
242 }
871d7de4
LP
243}
244
245static void timer_set_state(Timer *t, TimerState state) {
246 TimerState old_state;
034c6ed7 247 assert(t);
871d7de4
LP
248
249 old_state = t->state;
250 t->state = state;
251
36697dc0 252 if (state != TIMER_WAITING) {
718db961
LP
253 t->monotonic_event_source = sd_event_source_unref(t->monotonic_event_source);
254 t->realtime_event_source = sd_event_source_unref(t->realtime_event_source);
6e2c9ce1
ZJS
255 t->next_elapse_monotonic_or_boottime = USEC_INFINITY;
256 t->next_elapse_realtime = USEC_INFINITY;
36697dc0 257 }
871d7de4
LP
258
259 if (state != old_state)
f2341e0a 260 log_unit_debug(UNIT(t), "Changed %s -> %s", timer_state_to_string(old_state), timer_state_to_string(state));
871d7de4 261
2ad2e41a 262 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
871d7de4
LP
263}
264
265static void timer_enter_waiting(Timer *t, bool initial);
266
be847e82 267static int timer_coldplug(Unit *u) {
871d7de4
LP
268 Timer *t = TIMER(u);
269
270 assert(t);
271 assert(t->state == TIMER_DEAD);
272
3e0c30ac
LP
273 if (t->deserialized_state == t->state)
274 return 0;
871d7de4 275
3e0c30ac
LP
276 if (t->deserialized_state == TIMER_WAITING)
277 timer_enter_waiting(t, false);
278 else
279 timer_set_state(t, t->deserialized_state);
871d7de4
LP
280
281 return 0;
282}
283
067d72c9 284static void timer_enter_dead(Timer *t, TimerResult f) {
871d7de4
LP
285 assert(t);
286
a0fef983 287 if (t->result == TIMER_SUCCESS)
067d72c9 288 t->result = f;
871d7de4 289
ed77d407
LP
290 if (t->result != TIMER_SUCCESS)
291 log_unit_warning(UNIT(t), "Failed with result '%s'.", timer_result_to_string(t->result));
292
067d72c9 293 timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD);
871d7de4
LP
294}
295
3e0c30ac
LP
296static void timer_enter_elapsed(Timer *t, bool leave_around) {
297 assert(t);
298
299 /* If a unit is marked with RemainAfterElapse=yes we leave it
300 * around even after it elapsed once, so that starting it
301 * later again does not necessarily mean immediate
302 * retriggering. We unconditionally leave units with
303 * TIMER_UNIT_ACTIVE or TIMER_UNIT_INACTIVE triggers around,
304 * since they might be restarted automatically at any time
305 * later on. */
306
307 if (t->remain_after_elapse || leave_around)
308 timer_set_state(t, TIMER_ELAPSED);
309 else
310 timer_enter_dead(t, TIMER_SUCCESS);
311}
312
744c7693
LP
313static void add_random(Timer *t, usec_t *v) {
314 char s[FORMAT_TIMESPAN_MAX];
315 usec_t add;
316
317 assert(t);
3f51aec8 318 assert(v);
744c7693
LP
319
320 if (t->random_usec == 0)
321 return;
322 if (*v == USEC_INFINITY)
323 return;
324
325 add = random_u64() % t->random_usec;
326
327 if (*v + add < *v) /* overflow */
328 *v = (usec_t) -2; /* Highest possible value, that is not USEC_INFINITY */
329 else
330 *v += add;
331
382852fd 332 log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
744c7693
LP
333}
334
871d7de4 335static void timer_enter_waiting(Timer *t, bool initial) {
36697dc0 336 bool found_monotonic = false, found_realtime = false;
3e0c30ac 337 bool leave_around = false;
c54be90b 338 triple_timestamp ts;
dedabea4 339 TimerValue *v;
e903182e 340 Unit *trigger;
871d7de4
LP
341 int r;
342
e903182e
LP
343 assert(t);
344
345 trigger = UNIT_TRIGGER(UNIT(t));
346 if (!trigger) {
347 log_unit_error(UNIT(t), "Unit to trigger vanished.");
348 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
349 return;
350 }
351
c54be90b 352 triple_timestamp_get(&ts);
dedabea4 353 t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0;
871d7de4
LP
354
355 LIST_FOREACH(value, v, t->values) {
871d7de4
LP
356 if (v->disabled)
357 continue;
358
36697dc0 359 if (v->base == TIMER_CALENDAR) {
06642d17
LP
360 usec_t b;
361
362 /* If we know the last time this was
363 * triggered, schedule the job based relative
9ea9faff
AJ
364 * to that. If we don't, just start from
365 * the activation time. */
366
367 if (t->last_trigger.realtime > 0)
368 b = t->last_trigger.realtime;
369 else {
370 if (state_translation_table[t->state] == UNIT_ACTIVE)
371 b = UNIT(t)->inactive_exit_timestamp.realtime;
372 else
373 b = ts.realtime;
374 }
06642d17
LP
375
376 r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse);
36697dc0
LP
377 if (r < 0)
378 continue;
379
36697dc0
LP
380 if (!found_realtime)
381 t->next_elapse_realtime = v->next_elapse;
10717a1a 382 else
36697dc0 383 t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
871d7de4 384
36697dc0 385 found_realtime = true;
871d7de4 386
60933bb8
AJ
387 } else {
388 usec_t base;
c54be90b 389
36697dc0 390 switch (v->base) {
871d7de4 391
36697dc0
LP
392 case TIMER_ACTIVE:
393 if (state_translation_table[t->state] == UNIT_ACTIVE)
394 base = UNIT(t)->inactive_exit_timestamp.monotonic;
395 else
c54be90b 396 base = ts.monotonic;
36697dc0 397 break;
871d7de4 398
36697dc0 399 case TIMER_BOOT:
c1d9ba99
MS
400 if (detect_container() <= 0) {
401 /* CLOCK_MONOTONIC equals the uptime on Linux */
402 base = 0;
403 break;
404 }
405 /* In a container we don't want to include the time the host
406 * was already up when the container started, so count from
ec251fe7 407 * our own startup. */
4831981d 408 _fallthrough_;
36697dc0 409 case TIMER_STARTUP:
9f9f0342 410 base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
36697dc0 411 break;
871d7de4 412
36697dc0 413 case TIMER_UNIT_ACTIVE:
3e0c30ac 414 leave_around = true;
e903182e 415 base = trigger->inactive_exit_timestamp.monotonic;
e41e1943
LP
416
417 if (base <= 0)
06642d17 418 base = t->last_trigger.monotonic;
e41e1943
LP
419
420 if (base <= 0)
36697dc0 421 continue;
204d140c 422 base = MAX(base, t->last_trigger.monotonic);
871d7de4 423
36697dc0 424 break;
871d7de4 425
36697dc0 426 case TIMER_UNIT_INACTIVE:
3e0c30ac 427 leave_around = true;
e903182e 428 base = trigger->inactive_enter_timestamp.monotonic;
e41e1943
LP
429
430 if (base <= 0)
06642d17 431 base = t->last_trigger.monotonic;
e41e1943
LP
432
433 if (base <= 0)
36697dc0 434 continue;
204d140c 435 base = MAX(base, t->last_trigger.monotonic);
871d7de4 436
36697dc0 437 break;
871d7de4 438
36697dc0
LP
439 default:
440 assert_not_reached("Unknown timer base");
441 }
871d7de4 442
c54be90b 443 v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
36697dc0 444
c54be90b
LP
445 if (!initial &&
446 v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
447 IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
e41e1943 448 /* This is a one time trigger, disable it now */
36697dc0
LP
449 v->disabled = true;
450 continue;
451 }
452
453 if (!found_monotonic)
dedabea4 454 t->next_elapse_monotonic_or_boottime = v->next_elapse;
36697dc0 455 else
dedabea4 456 t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse);
36697dc0
LP
457
458 found_monotonic = true;
459 }
871d7de4
LP
460 }
461
36697dc0 462 if (!found_monotonic && !found_realtime) {
f2341e0a 463 log_unit_debug(UNIT(t), "Timer is elapsed.");
3e0c30ac 464 timer_enter_elapsed(t, leave_around);
871d7de4
LP
465 return;
466 }
467
36697dc0
LP
468 if (found_monotonic) {
469 char buf[FORMAT_TIMESPAN_MAX];
3e0c30ac 470 usec_t left;
dedabea4 471
744c7693
LP
472 add_random(t, &t->next_elapse_monotonic_or_boottime);
473
c54be90b 474 left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)));
3e0c30ac 475 log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", format_timespan(buf, sizeof(buf), left, 0));
871d7de4 476
718db961 477 if (t->monotonic_event_source) {
dedabea4 478 r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime);
718db961
LP
479 if (r < 0)
480 goto fail;
481
482 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT);
cbf60d0a
LP
483 if (r < 0)
484 goto fail;
485 } else {
486
6a0f1f6d
LP
487 r = sd_event_add_time(
488 UNIT(t)->manager->event,
489 &t->monotonic_event_source,
dedabea4
LP
490 t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC,
491 t->next_elapse_monotonic_or_boottime, t->accuracy_usec,
6a0f1f6d 492 timer_dispatch, t);
cbf60d0a
LP
493 if (r < 0)
494 goto fail;
718db961 495
cbf60d0a
LP
496 (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic");
497 }
7dfbe2e3 498
718db961 499 } else if (t->monotonic_event_source) {
718db961 500
06642d17 501 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF);
718db961
LP
502 if (r < 0)
503 goto fail;
504 }
36697dc0
LP
505
506 if (found_realtime) {
507 char buf[FORMAT_TIMESTAMP_MAX];
744c7693
LP
508
509 add_random(t, &t->next_elapse_realtime);
510
f2341e0a 511 log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
36697dc0 512
718db961
LP
513 if (t->realtime_event_source) {
514 r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime);
515 if (r < 0)
516 goto fail;
517
518 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT);
cbf60d0a
LP
519 if (r < 0)
520 goto fail;
521 } else {
6a0f1f6d
LP
522 r = sd_event_add_time(
523 UNIT(t)->manager->event,
524 &t->realtime_event_source,
dedabea4 525 t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME,
6a0f1f6d
LP
526 t->next_elapse_realtime, t->accuracy_usec,
527 timer_dispatch, t);
cbf60d0a
LP
528 if (r < 0)
529 goto fail;
718db961 530
cbf60d0a
LP
531 (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime");
532 }
7dfbe2e3 533
718db961 534 } else if (t->realtime_event_source) {
718db961 535
06642d17 536 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF);
718db961
LP
537 if (r < 0)
538 goto fail;
539 }
871d7de4
LP
540
541 timer_set_state(t, TIMER_WAITING);
542 return;
543
544fail:
f2341e0a 545 log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m");
067d72c9 546 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
871d7de4
LP
547}
548
549static void timer_enter_running(Timer *t) {
4afd3348 550 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
e903182e 551 Unit *trigger;
871d7de4 552 int r;
398ef8ba 553
871d7de4
LP
554 assert(t);
555
ba3e67a7 556 /* Don't start job if we are supposed to go down */
31afa0a4 557 if (unit_stop_pending(UNIT(t)))
ba3e67a7
LP
558 return;
559
e903182e
LP
560 trigger = UNIT_TRIGGER(UNIT(t));
561 if (!trigger) {
562 log_unit_error(UNIT(t), "Unit to trigger vanished.");
563 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
564 return;
565 }
566
567 r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, &error, NULL);
36697dc0 568 if (r < 0)
871d7de4
LP
569 goto fail;
570
06642d17
LP
571 dual_timestamp_get(&t->last_trigger);
572
472fc28f 573 if (t->stamp_path)
ee735086 574 touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID);
e41e1943 575
871d7de4
LP
576 timer_set_state(t, TIMER_RUNNING);
577 return;
578
579fail:
f2341e0a 580 log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r));
067d72c9 581 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
871d7de4
LP
582}
583
584static int timer_start(Unit *u) {
585 Timer *t = TIMER(u);
779042e7 586 TimerValue *v;
e903182e 587 Unit *trigger;
07299350 588 int r;
871d7de4
LP
589
590 assert(t);
3742095b 591 assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
01f78473 592
e903182e
LP
593 trigger = UNIT_TRIGGER(u);
594 if (!trigger || trigger->load_state != UNIT_LOADED) {
595 log_unit_error(u, "Refusing to start, unit to trigger not loaded.");
01f78473 596 return -ENOENT;
e903182e 597 }
871d7de4 598
07299350
LP
599 r = unit_start_limit_test(u);
600 if (r < 0) {
601 timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
602 return r;
603 }
604
4b58153d
LP
605 r = unit_acquire_invocation_id(u);
606 if (r < 0)
607 return r;
608
06642d17
LP
609 t->last_trigger = DUAL_TIMESTAMP_NULL;
610
779042e7
MC
611 /* Reenable all timers that depend on unit activation time */
612 LIST_FOREACH(value, v, t->values)
613 if (v->base == TIMER_ACTIVE)
614 v->disabled = false;
615
06642d17
LP
616 if (t->stamp_path) {
617 struct stat st;
618
77542a79
LP
619 if (stat(t->stamp_path, &st) >= 0) {
620 usec_t ft;
621
622 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
623 * something is wrong with the system clock. */
624
625 ft = timespec_load(&st.st_mtim);
626 if (ft < now(CLOCK_REALTIME))
627 t->last_trigger.realtime = ft;
628 else {
629 char z[FORMAT_TIMESTAMP_MAX];
630
631 log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.",
632 format_timestamp(z, sizeof(z), ft));
633 }
634
635 } else if (errno == ENOENT)
472fc28f
TB
636 /* The timer has never run before,
637 * make sure a stamp file exists.
638 */
4b58153d 639 (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
06642d17
LP
640 }
641
067d72c9 642 t->result = TIMER_SUCCESS;
871d7de4 643 timer_enter_waiting(t, true);
82a2b6bb 644 return 1;
871d7de4
LP
645}
646
647static int timer_stop(Unit *u) {
648 Timer *t = TIMER(u);
649
650 assert(t);
3742095b 651 assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED));
871d7de4 652
067d72c9 653 timer_enter_dead(t, TIMER_SUCCESS);
82a2b6bb 654 return 1;
871d7de4
LP
655}
656
657static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
658 Timer *t = TIMER(u);
659
660 assert(u);
661 assert(f);
662 assert(fds);
663
664 unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
067d72c9 665 unit_serialize_item(u, f, "result", timer_result_to_string(t->result));
871d7de4 666
06642d17
LP
667 if (t->last_trigger.realtime > 0)
668 unit_serialize_item_format(u, f, "last-trigger-realtime", "%" PRIu64, t->last_trigger.realtime);
669
670 if (t->last_trigger.monotonic > 0)
671 unit_serialize_item_format(u, f, "last-trigger-monotonic", "%" PRIu64, t->last_trigger.monotonic);
672
871d7de4
LP
673 return 0;
674}
675
676static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
677 Timer *t = TIMER(u);
06642d17 678 int r;
871d7de4
LP
679
680 assert(u);
681 assert(key);
682 assert(value);
683 assert(fds);
684
685 if (streq(key, "state")) {
686 TimerState state;
687
36697dc0
LP
688 state = timer_state_from_string(value);
689 if (state < 0)
f2341e0a 690 log_unit_debug(u, "Failed to parse state value: %s", value);
871d7de4
LP
691 else
692 t->deserialized_state = state;
067d72c9
LP
693 } else if (streq(key, "result")) {
694 TimerResult f;
695
696 f = timer_result_from_string(value);
697 if (f < 0)
f2341e0a 698 log_unit_debug(u, "Failed to parse result value: %s", value);
067d72c9
LP
699 else if (f != TIMER_SUCCESS)
700 t->result = f;
06642d17
LP
701 } else if (streq(key, "last-trigger-realtime")) {
702
703 r = safe_atou64(value, &t->last_trigger.realtime);
704 if (r < 0)
f2341e0a 705 log_unit_debug(u, "Failed to parse last-trigger-realtime value: %s", value);
06642d17
LP
706
707 } else if (streq(key, "last-trigger-monotonic")) {
708
709 r = safe_atou64(value, &t->last_trigger.monotonic);
710 if (r < 0)
f2341e0a 711 log_unit_debug(u, "Failed to parse last-trigger-monotonic value: %s", value);
067d72c9 712
871d7de4 713 } else
f2341e0a 714 log_unit_debug(u, "Unknown serialization key: %s", key);
871d7de4
LP
715
716 return 0;
034c6ed7
LP
717}
718
44a6b1b6 719_pure_ static UnitActiveState timer_active_state(Unit *u) {
871d7de4
LP
720 assert(u);
721
722 return state_translation_table[TIMER(u)->state];
723}
724
44a6b1b6 725_pure_ static const char *timer_sub_state_to_string(Unit *u) {
871d7de4
LP
726 assert(u);
727
728 return timer_state_to_string(TIMER(u)->state);
729}
730
718db961
LP
731static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
732 Timer *t = TIMER(userdata);
871d7de4
LP
733
734 assert(t);
5cb5a6ff 735
871d7de4 736 if (t->state != TIMER_WAITING)
718db961 737 return 0;
5cb5a6ff 738
f2341e0a 739 log_unit_debug(UNIT(t), "Timer elapsed.");
871d7de4 740 timer_enter_running(t);
718db961 741 return 0;
5cb5a6ff
LP
742}
743
3ecaa09b
LP
744static void timer_trigger_notify(Unit *u, Unit *other) {
745 Timer *t = TIMER(u);
746 TimerValue *v;
871d7de4 747
3ecaa09b
LP
748 assert(u);
749 assert(other);
871d7de4 750
3ecaa09b
LP
751 if (other->load_state != UNIT_LOADED)
752 return;
871d7de4 753
3ecaa09b
LP
754 /* Reenable all timers that depend on unit state */
755 LIST_FOREACH(value, v, t->values)
3742095b 756 if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE))
3ecaa09b 757 v->disabled = false;
01f78473 758
3ecaa09b 759 switch (t->state) {
871d7de4 760
3ecaa09b
LP
761 case TIMER_WAITING:
762 case TIMER_ELAPSED:
871d7de4 763
3ecaa09b
LP
764 /* Recalculate sleep time */
765 timer_enter_waiting(t, false);
766 break;
871d7de4 767
3ecaa09b 768 case TIMER_RUNNING:
871d7de4 769
3ecaa09b 770 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
f2341e0a 771 log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
871d7de4 772 timer_enter_waiting(t, false);
3ecaa09b
LP
773 }
774 break;
871d7de4 775
3ecaa09b
LP
776 case TIMER_DEAD:
777 case TIMER_FAILED:
778 break;
871d7de4 779
3ecaa09b
LP
780 default:
781 assert_not_reached("Unknown timer state");
871d7de4 782 }
871d7de4
LP
783}
784
fdf20a31 785static void timer_reset_failed(Unit *u) {
5632e374
LP
786 Timer *t = TIMER(u);
787
788 assert(t);
789
fdf20a31 790 if (t->state == TIMER_FAILED)
5632e374
LP
791 timer_set_state(t, TIMER_DEAD);
792
067d72c9 793 t->result = TIMER_SUCCESS;
5632e374
LP
794}
795
8742514c
LP
796static void timer_time_change(Unit *u) {
797 Timer *t = TIMER(u);
13f512d3 798 usec_t ts;
8742514c
LP
799
800 assert(u);
801
802 if (t->state != TIMER_WAITING)
803 return;
804
13f512d3
AJ
805 /* If we appear to have triggered in the future, the system clock must
806 * have been set backwards. So let's rewind our own clock and allow
807 * the future trigger(s) to happen again :). Exactly the same as when
808 * you start a timer unit with Persistent=yes. */
809 ts = now(CLOCK_REALTIME);
810 if (t->last_trigger.realtime > ts)
811 t->last_trigger.realtime = ts;
812
f2341e0a 813 log_unit_debug(u, "Time change, recalculating next elapse.");
8742514c
LP
814 timer_enter_waiting(t, false);
815}
816
bbf5fd8e
LP
817static void timer_timezone_change(Unit *u) {
818 Timer *t = TIMER(u);
819
820 assert(u);
821
822 if (t->state != TIMER_WAITING)
823 return;
824
825 log_unit_debug(u, "Timezone change, recalculating next elapse.");
826 timer_enter_waiting(t, false);
827}
828
871d7de4 829static const char* const timer_base_table[_TIMER_BASE_MAX] = {
03fae018
LP
830 [TIMER_ACTIVE] = "OnActiveSec",
831 [TIMER_BOOT] = "OnBootSec",
832 [TIMER_STARTUP] = "OnStartupSec",
833 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
36697dc0
LP
834 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
835 [TIMER_CALENDAR] = "OnCalendar"
871d7de4
LP
836};
837
838DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
839
067d72c9
LP
840static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
841 [TIMER_SUCCESS] = "success",
07299350
LP
842 [TIMER_FAILURE_RESOURCES] = "resources",
843 [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
067d72c9
LP
844};
845
846DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
847
87f0e418 848const UnitVTable timer_vtable = {
7d17cfbc 849 .object_size = sizeof(Timer),
718db961 850
f975e971
LP
851 .sections =
852 "Unit\0"
853 "Timer\0"
854 "Install\0",
d8a812d1 855 .private_section = "Timer",
5cb5a6ff 856
871d7de4 857 .init = timer_init,
034c6ed7 858 .done = timer_done,
871d7de4
LP
859 .load = timer_load,
860
861 .coldplug = timer_coldplug,
862
863 .dump = timer_dump,
864
865 .start = timer_start,
866 .stop = timer_stop,
867
868 .serialize = timer_serialize,
869 .deserialize_item = timer_deserialize_item,
870
871 .active_state = timer_active_state,
872 .sub_state_to_string = timer_sub_state_to_string,
873
3ecaa09b
LP
874 .trigger_notify = timer_trigger_notify,
875
fdf20a31 876 .reset_failed = timer_reset_failed,
8742514c 877 .time_change = timer_time_change,
bbf5fd8e 878 .timezone_change = timer_timezone_change,
5632e374 879
718db961 880 .bus_vtable = bus_timer_vtable,
d8a812d1
WC
881 .bus_set_property = bus_timer_set_property,
882
883 .can_transient = true,
5cb5a6ff 884};