]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/timer.c
Merge pull request #10866 from yuwata/libudev-util-cleanups
[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
fee04d7f 266static void timer_enter_waiting(Timer *t, bool initial, bool time_change);
871d7de4 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 277 if (t->deserialized_state == TIMER_WAITING)
fee04d7f 278 timer_enter_waiting(t, false, false);
3e0c30ac
LP
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
aac99f30 291 unit_log_result(UNIT(t), t->result == TIMER_SUCCESS, timer_result_to_string(t->result));
067d72c9 292 timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD);
871d7de4
LP
293}
294
3e0c30ac
LP
295static void timer_enter_elapsed(Timer *t, bool leave_around) {
296 assert(t);
297
298 /* If a unit is marked with RemainAfterElapse=yes we leave it
299 * around even after it elapsed once, so that starting it
300 * later again does not necessarily mean immediate
301 * retriggering. We unconditionally leave units with
302 * TIMER_UNIT_ACTIVE or TIMER_UNIT_INACTIVE triggers around,
303 * since they might be restarted automatically at any time
304 * later on. */
305
306 if (t->remain_after_elapse || leave_around)
307 timer_set_state(t, TIMER_ELAPSED);
308 else
309 timer_enter_dead(t, TIMER_SUCCESS);
310}
311
744c7693
LP
312static void add_random(Timer *t, usec_t *v) {
313 char s[FORMAT_TIMESPAN_MAX];
314 usec_t add;
315
316 assert(t);
3f51aec8 317 assert(v);
744c7693
LP
318
319 if (t->random_usec == 0)
320 return;
321 if (*v == USEC_INFINITY)
322 return;
323
324 add = random_u64() % t->random_usec;
325
326 if (*v + add < *v) /* overflow */
327 *v = (usec_t) -2; /* Highest possible value, that is not USEC_INFINITY */
328 else
329 *v += add;
330
382852fd 331 log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
744c7693
LP
332}
333
fee04d7f 334static void timer_enter_waiting(Timer *t, bool initial, bool time_change) {
36697dc0 335 bool found_monotonic = false, found_realtime = false;
3e0c30ac 336 bool leave_around = false;
c54be90b 337 triple_timestamp ts;
dedabea4 338 TimerValue *v;
e903182e 339 Unit *trigger;
871d7de4
LP
340 int r;
341
e903182e
LP
342 assert(t);
343
344 trigger = UNIT_TRIGGER(UNIT(t));
345 if (!trigger) {
346 log_unit_error(UNIT(t), "Unit to trigger vanished.");
347 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
348 return;
349 }
350
c54be90b 351 triple_timestamp_get(&ts);
dedabea4 352 t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0;
871d7de4
LP
353
354 LIST_FOREACH(value, v, t->values) {
871d7de4
LP
355 if (v->disabled)
356 continue;
357
36697dc0 358 if (v->base == TIMER_CALENDAR) {
06642d17
LP
359 usec_t b;
360
361 /* If we know the last time this was
362 * triggered, schedule the job based relative
9ea9faff
AJ
363 * to that. If we don't, just start from
364 * the activation time. */
365
366 if (t->last_trigger.realtime > 0)
367 b = t->last_trigger.realtime;
368 else {
369 if (state_translation_table[t->state] == UNIT_ACTIVE)
370 b = UNIT(t)->inactive_exit_timestamp.realtime;
371 else
372 b = ts.realtime;
373 }
06642d17
LP
374
375 r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse);
36697dc0
LP
376 if (r < 0)
377 continue;
378
36697dc0
LP
379 if (!found_realtime)
380 t->next_elapse_realtime = v->next_elapse;
10717a1a 381 else
36697dc0 382 t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
871d7de4 383
36697dc0 384 found_realtime = true;
871d7de4 385
60933bb8
AJ
386 } else {
387 usec_t base;
c54be90b 388
36697dc0 389 switch (v->base) {
871d7de4 390
36697dc0
LP
391 case TIMER_ACTIVE:
392 if (state_translation_table[t->state] == UNIT_ACTIVE)
393 base = UNIT(t)->inactive_exit_timestamp.monotonic;
394 else
c54be90b 395 base = ts.monotonic;
36697dc0 396 break;
871d7de4 397
36697dc0 398 case TIMER_BOOT:
c1d9ba99
MS
399 if (detect_container() <= 0) {
400 /* CLOCK_MONOTONIC equals the uptime on Linux */
401 base = 0;
402 break;
403 }
404 /* In a container we don't want to include the time the host
405 * was already up when the container started, so count from
ec251fe7 406 * our own startup. */
4831981d 407 _fallthrough_;
36697dc0 408 case TIMER_STARTUP:
9f9f0342 409 base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
36697dc0 410 break;
871d7de4 411
36697dc0 412 case TIMER_UNIT_ACTIVE:
3e0c30ac 413 leave_around = true;
e903182e 414 base = trigger->inactive_exit_timestamp.monotonic;
e41e1943
LP
415
416 if (base <= 0)
06642d17 417 base = t->last_trigger.monotonic;
e41e1943
LP
418
419 if (base <= 0)
36697dc0 420 continue;
204d140c 421 base = MAX(base, t->last_trigger.monotonic);
871d7de4 422
36697dc0 423 break;
871d7de4 424
36697dc0 425 case TIMER_UNIT_INACTIVE:
3e0c30ac 426 leave_around = true;
e903182e 427 base = trigger->inactive_enter_timestamp.monotonic;
e41e1943
LP
428
429 if (base <= 0)
06642d17 430 base = t->last_trigger.monotonic;
e41e1943
LP
431
432 if (base <= 0)
36697dc0 433 continue;
204d140c 434 base = MAX(base, t->last_trigger.monotonic);
871d7de4 435
36697dc0 436 break;
871d7de4 437
36697dc0
LP
438 default:
439 assert_not_reached("Unknown timer base");
440 }
871d7de4 441
c54be90b 442 v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
36697dc0 443
fee04d7f 444 if (!initial && !time_change &&
c54be90b
LP
445 v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
446 IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
e41e1943 447 /* This is a one time trigger, disable it now */
36697dc0
LP
448 v->disabled = true;
449 continue;
450 }
451
452 if (!found_monotonic)
dedabea4 453 t->next_elapse_monotonic_or_boottime = v->next_elapse;
36697dc0 454 else
dedabea4 455 t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse);
36697dc0
LP
456
457 found_monotonic = true;
458 }
871d7de4
LP
459 }
460
36697dc0 461 if (!found_monotonic && !found_realtime) {
f2341e0a 462 log_unit_debug(UNIT(t), "Timer is elapsed.");
3e0c30ac 463 timer_enter_elapsed(t, leave_around);
871d7de4
LP
464 return;
465 }
466
36697dc0
LP
467 if (found_monotonic) {
468 char buf[FORMAT_TIMESPAN_MAX];
3e0c30ac 469 usec_t left;
dedabea4 470
744c7693
LP
471 add_random(t, &t->next_elapse_monotonic_or_boottime);
472
c54be90b 473 left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)));
3e0c30ac 474 log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", format_timespan(buf, sizeof(buf), left, 0));
871d7de4 475
718db961 476 if (t->monotonic_event_source) {
dedabea4 477 r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime);
718db961
LP
478 if (r < 0)
479 goto fail;
480
481 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT);
cbf60d0a
LP
482 if (r < 0)
483 goto fail;
484 } else {
485
6a0f1f6d
LP
486 r = sd_event_add_time(
487 UNIT(t)->manager->event,
488 &t->monotonic_event_source,
dedabea4
LP
489 t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC,
490 t->next_elapse_monotonic_or_boottime, t->accuracy_usec,
6a0f1f6d 491 timer_dispatch, t);
cbf60d0a
LP
492 if (r < 0)
493 goto fail;
718db961 494
cbf60d0a
LP
495 (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic");
496 }
7dfbe2e3 497
718db961 498 } else if (t->monotonic_event_source) {
718db961 499
06642d17 500 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF);
718db961
LP
501 if (r < 0)
502 goto fail;
503 }
36697dc0
LP
504
505 if (found_realtime) {
506 char buf[FORMAT_TIMESTAMP_MAX];
744c7693
LP
507
508 add_random(t, &t->next_elapse_realtime);
509
f2341e0a 510 log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
36697dc0 511
718db961
LP
512 if (t->realtime_event_source) {
513 r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime);
514 if (r < 0)
515 goto fail;
516
517 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT);
cbf60d0a
LP
518 if (r < 0)
519 goto fail;
520 } else {
6a0f1f6d
LP
521 r = sd_event_add_time(
522 UNIT(t)->manager->event,
523 &t->realtime_event_source,
dedabea4 524 t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME,
6a0f1f6d
LP
525 t->next_elapse_realtime, t->accuracy_usec,
526 timer_dispatch, t);
cbf60d0a
LP
527 if (r < 0)
528 goto fail;
718db961 529
cbf60d0a
LP
530 (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime");
531 }
7dfbe2e3 532
718db961 533 } else if (t->realtime_event_source) {
718db961 534
06642d17 535 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF);
718db961
LP
536 if (r < 0)
537 goto fail;
538 }
871d7de4
LP
539
540 timer_set_state(t, TIMER_WAITING);
541 return;
542
543fail:
f2341e0a 544 log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m");
067d72c9 545 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
871d7de4
LP
546}
547
548static void timer_enter_running(Timer *t) {
4afd3348 549 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
e903182e 550 Unit *trigger;
871d7de4 551 int r;
398ef8ba 552
871d7de4
LP
553 assert(t);
554
ba3e67a7 555 /* Don't start job if we are supposed to go down */
31afa0a4 556 if (unit_stop_pending(UNIT(t)))
ba3e67a7
LP
557 return;
558
e903182e
LP
559 trigger = UNIT_TRIGGER(UNIT(t));
560 if (!trigger) {
561 log_unit_error(UNIT(t), "Unit to trigger vanished.");
562 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
563 return;
564 }
565
566 r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, &error, NULL);
36697dc0 567 if (r < 0)
871d7de4
LP
568 goto fail;
569
06642d17
LP
570 dual_timestamp_get(&t->last_trigger);
571
472fc28f 572 if (t->stamp_path)
ee735086 573 touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID);
e41e1943 574
871d7de4
LP
575 timer_set_state(t, TIMER_RUNNING);
576 return;
577
578fail:
f2341e0a 579 log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r));
067d72c9 580 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
871d7de4
LP
581}
582
583static int timer_start(Unit *u) {
584 Timer *t = TIMER(u);
779042e7 585 TimerValue *v;
e903182e 586 Unit *trigger;
07299350 587 int r;
871d7de4
LP
588
589 assert(t);
3742095b 590 assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
01f78473 591
e903182e
LP
592 trigger = UNIT_TRIGGER(u);
593 if (!trigger || trigger->load_state != UNIT_LOADED) {
594 log_unit_error(u, "Refusing to start, unit to trigger not loaded.");
01f78473 595 return -ENOENT;
e903182e 596 }
871d7de4 597
07299350
LP
598 r = unit_start_limit_test(u);
599 if (r < 0) {
600 timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
601 return r;
602 }
603
4b58153d
LP
604 r = unit_acquire_invocation_id(u);
605 if (r < 0)
606 return r;
607
06642d17
LP
608 t->last_trigger = DUAL_TIMESTAMP_NULL;
609
779042e7
MC
610 /* Reenable all timers that depend on unit activation time */
611 LIST_FOREACH(value, v, t->values)
612 if (v->base == TIMER_ACTIVE)
613 v->disabled = false;
614
06642d17
LP
615 if (t->stamp_path) {
616 struct stat st;
617
77542a79
LP
618 if (stat(t->stamp_path, &st) >= 0) {
619 usec_t ft;
620
621 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
622 * something is wrong with the system clock. */
623
624 ft = timespec_load(&st.st_mtim);
625 if (ft < now(CLOCK_REALTIME))
626 t->last_trigger.realtime = ft;
627 else {
628 char z[FORMAT_TIMESTAMP_MAX];
629
630 log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.",
631 format_timestamp(z, sizeof(z), ft));
632 }
633
634 } else if (errno == ENOENT)
472fc28f
TB
635 /* The timer has never run before,
636 * make sure a stamp file exists.
637 */
4b58153d 638 (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
06642d17
LP
639 }
640
067d72c9 641 t->result = TIMER_SUCCESS;
fee04d7f 642 timer_enter_waiting(t, true, false);
82a2b6bb 643 return 1;
871d7de4
LP
644}
645
646static int timer_stop(Unit *u) {
647 Timer *t = TIMER(u);
648
649 assert(t);
3742095b 650 assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED));
871d7de4 651
067d72c9 652 timer_enter_dead(t, TIMER_SUCCESS);
82a2b6bb 653 return 1;
871d7de4
LP
654}
655
656static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
657 Timer *t = TIMER(u);
658
659 assert(u);
660 assert(f);
661 assert(fds);
662
d68c645b
LP
663 (void) serialize_item(f, "state", timer_state_to_string(t->state));
664 (void) serialize_item(f, "result", timer_result_to_string(t->result));
871d7de4 665
06642d17 666 if (t->last_trigger.realtime > 0)
d68c645b 667 (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime);
06642d17
LP
668
669 if (t->last_trigger.monotonic > 0)
d68c645b 670 (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic);
06642d17 671
871d7de4
LP
672 return 0;
673}
674
675static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
676 Timer *t = TIMER(u);
677
678 assert(u);
679 assert(key);
680 assert(value);
681 assert(fds);
682
683 if (streq(key, "state")) {
684 TimerState state;
685
36697dc0
LP
686 state = timer_state_from_string(value);
687 if (state < 0)
f2341e0a 688 log_unit_debug(u, "Failed to parse state value: %s", value);
871d7de4
LP
689 else
690 t->deserialized_state = state;
d68c645b 691
067d72c9
LP
692 } else if (streq(key, "result")) {
693 TimerResult f;
694
695 f = timer_result_from_string(value);
696 if (f < 0)
f2341e0a 697 log_unit_debug(u, "Failed to parse result value: %s", value);
067d72c9
LP
698 else if (f != TIMER_SUCCESS)
699 t->result = f;
06642d17 700
d68c645b
LP
701 } else if (streq(key, "last-trigger-realtime"))
702 (void) deserialize_usec(value, &t->last_trigger.realtime);
703 else if (streq(key, "last-trigger-monotonic"))
704 (void) deserialize_usec(value, &t->last_trigger.monotonic);
705 else
f2341e0a 706 log_unit_debug(u, "Unknown serialization key: %s", key);
871d7de4
LP
707
708 return 0;
034c6ed7
LP
709}
710
44a6b1b6 711_pure_ static UnitActiveState timer_active_state(Unit *u) {
871d7de4
LP
712 assert(u);
713
714 return state_translation_table[TIMER(u)->state];
715}
716
44a6b1b6 717_pure_ static const char *timer_sub_state_to_string(Unit *u) {
871d7de4
LP
718 assert(u);
719
720 return timer_state_to_string(TIMER(u)->state);
721}
722
718db961
LP
723static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
724 Timer *t = TIMER(userdata);
871d7de4
LP
725
726 assert(t);
5cb5a6ff 727
871d7de4 728 if (t->state != TIMER_WAITING)
718db961 729 return 0;
5cb5a6ff 730
f2341e0a 731 log_unit_debug(UNIT(t), "Timer elapsed.");
871d7de4 732 timer_enter_running(t);
718db961 733 return 0;
5cb5a6ff
LP
734}
735
3ecaa09b
LP
736static void timer_trigger_notify(Unit *u, Unit *other) {
737 Timer *t = TIMER(u);
738 TimerValue *v;
871d7de4 739
3ecaa09b
LP
740 assert(u);
741 assert(other);
871d7de4 742
3ecaa09b
LP
743 if (other->load_state != UNIT_LOADED)
744 return;
871d7de4 745
3ecaa09b
LP
746 /* Reenable all timers that depend on unit state */
747 LIST_FOREACH(value, v, t->values)
3742095b 748 if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE))
3ecaa09b 749 v->disabled = false;
01f78473 750
3ecaa09b 751 switch (t->state) {
871d7de4 752
3ecaa09b
LP
753 case TIMER_WAITING:
754 case TIMER_ELAPSED:
871d7de4 755
3ecaa09b 756 /* Recalculate sleep time */
fee04d7f 757 timer_enter_waiting(t, false, false);
3ecaa09b 758 break;
871d7de4 759
3ecaa09b 760 case TIMER_RUNNING:
871d7de4 761
3ecaa09b 762 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
f2341e0a 763 log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
fee04d7f 764 timer_enter_waiting(t, false, false);
3ecaa09b
LP
765 }
766 break;
871d7de4 767
3ecaa09b
LP
768 case TIMER_DEAD:
769 case TIMER_FAILED:
770 break;
871d7de4 771
3ecaa09b
LP
772 default:
773 assert_not_reached("Unknown timer state");
871d7de4 774 }
871d7de4
LP
775}
776
fdf20a31 777static void timer_reset_failed(Unit *u) {
5632e374
LP
778 Timer *t = TIMER(u);
779
780 assert(t);
781
fdf20a31 782 if (t->state == TIMER_FAILED)
5632e374
LP
783 timer_set_state(t, TIMER_DEAD);
784
067d72c9 785 t->result = TIMER_SUCCESS;
5632e374
LP
786}
787
8742514c
LP
788static void timer_time_change(Unit *u) {
789 Timer *t = TIMER(u);
13f512d3 790 usec_t ts;
8742514c
LP
791
792 assert(u);
793
794 if (t->state != TIMER_WAITING)
795 return;
796
13f512d3
AJ
797 /* If we appear to have triggered in the future, the system clock must
798 * have been set backwards. So let's rewind our own clock and allow
799 * the future trigger(s) to happen again :). Exactly the same as when
800 * you start a timer unit with Persistent=yes. */
801 ts = now(CLOCK_REALTIME);
802 if (t->last_trigger.realtime > ts)
803 t->last_trigger.realtime = ts;
804
f2341e0a 805 log_unit_debug(u, "Time change, recalculating next elapse.");
fee04d7f 806 timer_enter_waiting(t, false, true);
8742514c
LP
807}
808
bbf5fd8e
LP
809static void timer_timezone_change(Unit *u) {
810 Timer *t = TIMER(u);
811
812 assert(u);
813
814 if (t->state != TIMER_WAITING)
815 return;
816
817 log_unit_debug(u, "Timezone change, recalculating next elapse.");
fee04d7f 818 timer_enter_waiting(t, false, false);
bbf5fd8e
LP
819}
820
871d7de4 821static const char* const timer_base_table[_TIMER_BASE_MAX] = {
03fae018
LP
822 [TIMER_ACTIVE] = "OnActiveSec",
823 [TIMER_BOOT] = "OnBootSec",
824 [TIMER_STARTUP] = "OnStartupSec",
825 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
36697dc0
LP
826 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
827 [TIMER_CALENDAR] = "OnCalendar"
871d7de4
LP
828};
829
830DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
831
067d72c9
LP
832static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
833 [TIMER_SUCCESS] = "success",
07299350
LP
834 [TIMER_FAILURE_RESOURCES] = "resources",
835 [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
067d72c9
LP
836};
837
838DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
839
87f0e418 840const UnitVTable timer_vtable = {
7d17cfbc 841 .object_size = sizeof(Timer),
718db961 842
f975e971
LP
843 .sections =
844 "Unit\0"
845 "Timer\0"
846 "Install\0",
d8a812d1 847 .private_section = "Timer",
5cb5a6ff 848
871d7de4 849 .init = timer_init,
034c6ed7 850 .done = timer_done,
871d7de4
LP
851 .load = timer_load,
852
853 .coldplug = timer_coldplug,
854
855 .dump = timer_dump,
856
857 .start = timer_start,
858 .stop = timer_stop,
859
860 .serialize = timer_serialize,
861 .deserialize_item = timer_deserialize_item,
862
863 .active_state = timer_active_state,
864 .sub_state_to_string = timer_sub_state_to_string,
865
3ecaa09b
LP
866 .trigger_notify = timer_trigger_notify,
867
fdf20a31 868 .reset_failed = timer_reset_failed,
8742514c 869 .time_change = timer_time_change,
bbf5fd8e 870 .timezone_change = timer_timezone_change,
5632e374 871
718db961 872 .bus_vtable = bus_timer_vtable,
d8a812d1
WC
873 .bus_set_property = bus_timer_set_property,
874
875 .can_transient = true,
5cb5a6ff 876};