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