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