]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/timer.c
core: Detect initial timer state from serialized data
[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 "fs-util.h"
10 #include "parse-util.h"
11 #include "random-util.h"
12 #include "serialize.h"
13 #include "special.h"
14 #include "string-table.h"
15 #include "string-util.h"
16 #include "timer.h"
17 #include "unit-name.h"
18 #include "unit.h"
19 #include "user-util.h"
20 #include "virt.h"
21
22 static 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,
27 [TIMER_FAILED] = UNIT_FAILED
28 };
29
30 static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata);
31
32 static void timer_init(Unit *u) {
33 Timer *t = TIMER(u);
34
35 assert(u);
36 assert(u->load_state == UNIT_STUB);
37
38 t->next_elapse_monotonic_or_boottime = USEC_INFINITY;
39 t->next_elapse_realtime = USEC_INFINITY;
40 t->accuracy_usec = u->manager->default_timer_accuracy_usec;
41 t->remain_after_elapse = true;
42 }
43
44 void timer_free_values(Timer *t) {
45 TimerValue *v;
46
47 assert(t);
48
49 while ((v = t->values)) {
50 LIST_REMOVE(value, t->values, v);
51 calendar_spec_free(v->calendar_spec);
52 free(v);
53 }
54 }
55
56 static void timer_done(Unit *u) {
57 Timer *t = TIMER(u);
58
59 assert(t);
60
61 timer_free_values(t);
62
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);
65
66 free(t->stamp_path);
67 }
68
69 static int timer_verify(Timer *t) {
70 assert(t);
71
72 if (UNIT(t)->load_state != UNIT_LOADED)
73 return 0;
74
75 if (!t->values) {
76 log_unit_error(UNIT(t), "Timer unit lacks value setting. Refusing.");
77 return -ENOEXEC;
78 }
79
80 return 0;
81 }
82
83 static int timer_add_default_dependencies(Timer *t) {
84 int r;
85 TimerValue *v;
86
87 assert(t);
88
89 if (!UNIT(t)->default_dependencies)
90 return 0;
91
92 r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_TIMERS_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
93 if (r < 0)
94 return r;
95
96 if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) {
97 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
98 if (r < 0)
99 return r;
100
101 LIST_FOREACH(value, v, t->values) {
102 if (v->base == TIMER_CALENDAR) {
103 r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, SPECIAL_TIME_SYNC_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
104 if (r < 0)
105 return r;
106 break;
107 }
108 }
109 }
110
111 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
112 }
113
114 static 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);
128 }
129
130 static int timer_setup_persistent(Timer *t) {
131 int r;
132
133 assert(t);
134
135 if (!t->persistent)
136 return 0;
137
138 if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) {
139
140 r = unit_require_mounts_for(UNIT(t), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE);
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)
150 t->stamp_path = strjoin(e, "/systemd/timers/stamp-", UNIT(t)->id);
151 else {
152
153 _cleanup_free_ char *h = NULL;
154
155 r = get_home_dir(&h);
156 if (r < 0)
157 return log_unit_error_errno(UNIT(t), r, "Failed to determine home directory: %m");
158
159 t->stamp_path = strjoin(h, "/.local/share/systemd/timers/stamp-", UNIT(t)->id);
160 }
161 }
162
163 if (!t->stamp_path)
164 return log_oom();
165
166 return 0;
167 }
168
169 static int timer_load(Unit *u) {
170 Timer *t = TIMER(u);
171 int r;
172
173 assert(u);
174 assert(u->load_state == UNIT_STUB);
175
176 r = unit_load_fragment_and_dropin(u);
177 if (r < 0)
178 return r;
179
180 if (u->load_state == UNIT_LOADED) {
181
182 r = timer_add_trigger_dependencies(t);
183 if (r < 0)
184 return r;
185
186 r = timer_setup_persistent(t);
187 if (r < 0)
188 return r;
189
190 r = timer_add_default_dependencies(t);
191 if (r < 0)
192 return r;
193 }
194
195 return timer_verify(t);
196 }
197
198 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
199 char buf[FORMAT_TIMESPAN_MAX];
200 Timer *t = TIMER(u);
201 Unit *trigger;
202 TimerValue *v;
203
204 trigger = UNIT_TRIGGER(u);
205
206 fprintf(f,
207 "%sTimer State: %s\n"
208 "%sResult: %s\n"
209 "%sUnit: %s\n"
210 "%sPersistent: %s\n"
211 "%sWakeSystem: %s\n"
212 "%sAccuracy: %s\n"
213 "%sRemainAfterElapse: %s\n",
214 prefix, timer_state_to_string(t->state),
215 prefix, timer_result_to_string(t->result),
216 prefix, trigger ? trigger->id : "n/a",
217 prefix, yes_no(t->persistent),
218 prefix, yes_no(t->wake_system),
219 prefix, format_timespan(buf, sizeof(buf), t->accuracy_usec, 1),
220 prefix, yes_no(t->remain_after_elapse));
221
222 LIST_FOREACH(value, v, t->values) {
223
224 if (v->base == TIMER_CALENDAR) {
225 _cleanup_free_ char *p = NULL;
226
227 (void) calendar_spec_to_string(v->calendar_spec, &p);
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),
241 format_timespan(timespan1, sizeof(timespan1), v->value, 0));
242 }
243 }
244 }
245
246 static void timer_set_state(Timer *t, TimerState state) {
247 TimerState old_state;
248 assert(t);
249
250 old_state = t->state;
251 t->state = state;
252
253 if (state != TIMER_WAITING) {
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);
256 t->next_elapse_monotonic_or_boottime = USEC_INFINITY;
257 t->next_elapse_realtime = USEC_INFINITY;
258 }
259
260 if (state != old_state)
261 log_unit_debug(UNIT(t), "Changed %s -> %s", timer_state_to_string(old_state), timer_state_to_string(state));
262
263 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
264 }
265
266 static void timer_enter_waiting(Timer *t, bool time_change);
267
268 static int timer_coldplug(Unit *u) {
269 Timer *t = TIMER(u);
270
271 assert(t);
272 assert(t->state == TIMER_DEAD);
273
274 if (t->deserialized_state == t->state)
275 return 0;
276
277 if (t->deserialized_state == TIMER_WAITING)
278 timer_enter_waiting(t, false);
279 else
280 timer_set_state(t, t->deserialized_state);
281
282 return 0;
283 }
284
285 static void timer_enter_dead(Timer *t, TimerResult f) {
286 assert(t);
287
288 if (t->result == TIMER_SUCCESS)
289 t->result = f;
290
291 unit_log_result(UNIT(t), t->result == TIMER_SUCCESS, timer_result_to_string(t->result));
292 timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD);
293 }
294
295 static 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
312 static void add_random(Timer *t, usec_t *v) {
313 char s[FORMAT_TIMESPAN_MAX];
314 usec_t add;
315
316 assert(t);
317 assert(v);
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
331 log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
332 }
333
334 static void timer_enter_waiting(Timer *t, bool time_change) {
335 bool found_monotonic = false, found_realtime = false;
336 bool leave_around = false;
337 triple_timestamp ts;
338 TimerValue *v;
339 Unit *trigger;
340 int r;
341
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
351 triple_timestamp_get(&ts);
352 t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0;
353
354 LIST_FOREACH(value, v, t->values) {
355 if (v->disabled)
356 continue;
357
358 if (v->base == TIMER_CALENDAR) {
359 usec_t b;
360
361 /* If we know the last time this was
362 * triggered, schedule the job based relative
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 }
374
375 r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse);
376 if (r < 0)
377 continue;
378
379 if (!found_realtime)
380 t->next_elapse_realtime = v->next_elapse;
381 else
382 t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
383
384 found_realtime = true;
385
386 } else {
387 usec_t base;
388
389 switch (v->base) {
390
391 case TIMER_ACTIVE:
392 if (state_translation_table[t->state] == UNIT_ACTIVE)
393 base = UNIT(t)->inactive_exit_timestamp.monotonic;
394 else
395 base = ts.monotonic;
396 break;
397
398 case TIMER_BOOT:
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
406 * our own startup. */
407 _fallthrough_;
408 case TIMER_STARTUP:
409 base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
410 break;
411
412 case TIMER_UNIT_ACTIVE:
413 leave_around = true;
414 base = trigger->inactive_exit_timestamp.monotonic;
415
416 if (base <= 0)
417 base = t->last_trigger.monotonic;
418
419 if (base <= 0)
420 continue;
421 base = MAX(base, t->last_trigger.monotonic);
422
423 break;
424
425 case TIMER_UNIT_INACTIVE:
426 leave_around = true;
427 base = trigger->inactive_enter_timestamp.monotonic;
428
429 if (base <= 0)
430 base = t->last_trigger.monotonic;
431
432 if (base <= 0)
433 continue;
434 base = MAX(base, t->last_trigger.monotonic);
435
436 break;
437
438 default:
439 assert_not_reached("Unknown timer base");
440 }
441
442 v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
443
444 if (dual_timestamp_is_set(&t->last_trigger) &&
445 !time_change &&
446 v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
447 IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
448 /* This is a one time trigger, disable it now */
449 v->disabled = true;
450 continue;
451 }
452
453 if (!found_monotonic)
454 t->next_elapse_monotonic_or_boottime = v->next_elapse;
455 else
456 t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse);
457
458 found_monotonic = true;
459 }
460 }
461
462 if (!found_monotonic && !found_realtime) {
463 log_unit_debug(UNIT(t), "Timer is elapsed.");
464 timer_enter_elapsed(t, leave_around);
465 return;
466 }
467
468 if (found_monotonic) {
469 char buf[FORMAT_TIMESPAN_MAX];
470 usec_t left;
471
472 add_random(t, &t->next_elapse_monotonic_or_boottime);
473
474 left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)));
475 log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", format_timespan(buf, sizeof(buf), left, 0));
476
477 if (t->monotonic_event_source) {
478 r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime);
479 if (r < 0)
480 goto fail;
481
482 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT);
483 if (r < 0)
484 goto fail;
485 } else {
486
487 r = sd_event_add_time(
488 UNIT(t)->manager->event,
489 &t->monotonic_event_source,
490 t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC,
491 t->next_elapse_monotonic_or_boottime, t->accuracy_usec,
492 timer_dispatch, t);
493 if (r < 0)
494 goto fail;
495
496 (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic");
497 }
498
499 } else if (t->monotonic_event_source) {
500
501 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF);
502 if (r < 0)
503 goto fail;
504 }
505
506 if (found_realtime) {
507 char buf[FORMAT_TIMESTAMP_MAX];
508
509 add_random(t, &t->next_elapse_realtime);
510
511 log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
512
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);
519 if (r < 0)
520 goto fail;
521 } else {
522 r = sd_event_add_time(
523 UNIT(t)->manager->event,
524 &t->realtime_event_source,
525 t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME,
526 t->next_elapse_realtime, t->accuracy_usec,
527 timer_dispatch, t);
528 if (r < 0)
529 goto fail;
530
531 (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime");
532 }
533
534 } else if (t->realtime_event_source) {
535
536 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF);
537 if (r < 0)
538 goto fail;
539 }
540
541 timer_set_state(t, TIMER_WAITING);
542 return;
543
544 fail:
545 log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m");
546 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
547 }
548
549 static void timer_enter_running(Timer *t) {
550 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
551 Unit *trigger;
552 int r;
553
554 assert(t);
555
556 /* Don't start job if we are supposed to go down */
557 if (unit_stop_pending(UNIT(t)))
558 return;
559
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);
568 if (r < 0)
569 goto fail;
570
571 dual_timestamp_get(&t->last_trigger);
572
573 if (t->stamp_path)
574 touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID);
575
576 timer_set_state(t, TIMER_RUNNING);
577 return;
578
579 fail:
580 log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r));
581 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
582 }
583
584 static int timer_start(Unit *u) {
585 Timer *t = TIMER(u);
586 TimerValue *v;
587 Unit *trigger;
588 int r;
589
590 assert(t);
591 assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
592
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.");
596 return -ENOENT;
597 }
598
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
605 r = unit_acquire_invocation_id(u);
606 if (r < 0)
607 return r;
608
609 t->last_trigger = DUAL_TIMESTAMP_NULL;
610
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
616 if (t->stamp_path) {
617 struct stat st;
618
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)
636 /* The timer has never run before,
637 * make sure a stamp file exists.
638 */
639 (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
640 }
641
642 t->result = TIMER_SUCCESS;
643 timer_enter_waiting(t, false);
644 return 1;
645 }
646
647 static int timer_stop(Unit *u) {
648 Timer *t = TIMER(u);
649
650 assert(t);
651 assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED));
652
653 timer_enter_dead(t, TIMER_SUCCESS);
654 return 1;
655 }
656
657 static 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 (void) serialize_item(f, "state", timer_state_to_string(t->state));
665 (void) serialize_item(f, "result", timer_result_to_string(t->result));
666
667 if (t->last_trigger.realtime > 0)
668 (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime);
669
670 if (t->last_trigger.monotonic > 0)
671 (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic);
672
673 return 0;
674 }
675
676 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
677 Timer *t = TIMER(u);
678
679 assert(u);
680 assert(key);
681 assert(value);
682 assert(fds);
683
684 if (streq(key, "state")) {
685 TimerState state;
686
687 state = timer_state_from_string(value);
688 if (state < 0)
689 log_unit_debug(u, "Failed to parse state value: %s", value);
690 else
691 t->deserialized_state = state;
692
693 } else if (streq(key, "result")) {
694 TimerResult f;
695
696 f = timer_result_from_string(value);
697 if (f < 0)
698 log_unit_debug(u, "Failed to parse result value: %s", value);
699 else if (f != TIMER_SUCCESS)
700 t->result = f;
701
702 } else if (streq(key, "last-trigger-realtime"))
703 (void) deserialize_usec(value, &t->last_trigger.realtime);
704 else if (streq(key, "last-trigger-monotonic"))
705 (void) deserialize_usec(value, &t->last_trigger.monotonic);
706 else
707 log_unit_debug(u, "Unknown serialization key: %s", key);
708
709 return 0;
710 }
711
712 _pure_ static UnitActiveState timer_active_state(Unit *u) {
713 assert(u);
714
715 return state_translation_table[TIMER(u)->state];
716 }
717
718 _pure_ static const char *timer_sub_state_to_string(Unit *u) {
719 assert(u);
720
721 return timer_state_to_string(TIMER(u)->state);
722 }
723
724 static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
725 Timer *t = TIMER(userdata);
726
727 assert(t);
728
729 if (t->state != TIMER_WAITING)
730 return 0;
731
732 log_unit_debug(UNIT(t), "Timer elapsed.");
733 timer_enter_running(t);
734 return 0;
735 }
736
737 static void timer_trigger_notify(Unit *u, Unit *other) {
738 Timer *t = TIMER(u);
739 TimerValue *v;
740
741 assert(u);
742 assert(other);
743
744 if (other->load_state != UNIT_LOADED)
745 return;
746
747 /* Reenable all timers that depend on unit state */
748 LIST_FOREACH(value, v, t->values)
749 if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE))
750 v->disabled = false;
751
752 switch (t->state) {
753
754 case TIMER_WAITING:
755 case TIMER_ELAPSED:
756
757 /* Recalculate sleep time */
758 timer_enter_waiting(t, false);
759 break;
760
761 case TIMER_RUNNING:
762
763 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
764 log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
765 timer_enter_waiting(t, false);
766 }
767 break;
768
769 case TIMER_DEAD:
770 case TIMER_FAILED:
771 break;
772
773 default:
774 assert_not_reached("Unknown timer state");
775 }
776 }
777
778 static void timer_reset_failed(Unit *u) {
779 Timer *t = TIMER(u);
780
781 assert(t);
782
783 if (t->state == TIMER_FAILED)
784 timer_set_state(t, TIMER_DEAD);
785
786 t->result = TIMER_SUCCESS;
787 }
788
789 static void timer_time_change(Unit *u) {
790 Timer *t = TIMER(u);
791 usec_t ts;
792
793 assert(u);
794
795 if (t->state != TIMER_WAITING)
796 return;
797
798 /* If we appear to have triggered in the future, the system clock must
799 * have been set backwards. So let's rewind our own clock and allow
800 * the future trigger(s) to happen again :). Exactly the same as when
801 * you start a timer unit with Persistent=yes. */
802 ts = now(CLOCK_REALTIME);
803 if (t->last_trigger.realtime > ts)
804 t->last_trigger.realtime = ts;
805
806 log_unit_debug(u, "Time change, recalculating next elapse.");
807 timer_enter_waiting(t, true);
808 }
809
810 static void timer_timezone_change(Unit *u) {
811 Timer *t = TIMER(u);
812
813 assert(u);
814
815 if (t->state != TIMER_WAITING)
816 return;
817
818 log_unit_debug(u, "Timezone change, recalculating next elapse.");
819 timer_enter_waiting(t, false);
820 }
821
822 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
823 [TIMER_ACTIVE] = "OnActiveSec",
824 [TIMER_BOOT] = "OnBootSec",
825 [TIMER_STARTUP] = "OnStartupSec",
826 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
827 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
828 [TIMER_CALENDAR] = "OnCalendar"
829 };
830
831 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
832
833 static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
834 [TIMER_SUCCESS] = "success",
835 [TIMER_FAILURE_RESOURCES] = "resources",
836 [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
837 };
838
839 DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
840
841 const UnitVTable timer_vtable = {
842 .object_size = sizeof(Timer),
843
844 .sections =
845 "Unit\0"
846 "Timer\0"
847 "Install\0",
848 .private_section = "Timer",
849
850 .init = timer_init,
851 .done = timer_done,
852 .load = timer_load,
853
854 .coldplug = timer_coldplug,
855
856 .dump = timer_dump,
857
858 .start = timer_start,
859 .stop = timer_stop,
860
861 .serialize = timer_serialize,
862 .deserialize_item = timer_deserialize_item,
863
864 .active_state = timer_active_state,
865 .sub_state_to_string = timer_sub_state_to_string,
866
867 .trigger_notify = timer_trigger_notify,
868
869 .reset_failed = timer_reset_failed,
870 .time_change = timer_time_change,
871 .timezone_change = timer_timezone_change,
872
873 .bus_vtable = bus_timer_vtable,
874 .bus_set_property = bus_timer_set_property,
875
876 .can_transient = true,
877 };