]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/timer.c
Merge pull request #10573 from faheel/master
[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 initial, 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, 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 if (t->result != TIMER_SUCCESS)
292 log_unit_warning(UNIT(t), "Failed with result '%s'.", timer_result_to_string(t->result));
293
294 timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD);
295 }
296
297 static 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
314 static void add_random(Timer *t, usec_t *v) {
315 char s[FORMAT_TIMESPAN_MAX];
316 usec_t add;
317
318 assert(t);
319 assert(v);
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
333 log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
334 }
335
336 static void timer_enter_waiting(Timer *t, bool initial, bool time_change) {
337 bool found_monotonic = false, found_realtime = false;
338 bool leave_around = false;
339 triple_timestamp ts;
340 TimerValue *v;
341 Unit *trigger;
342 int r;
343
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
353 triple_timestamp_get(&ts);
354 t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0;
355
356 LIST_FOREACH(value, v, t->values) {
357 if (v->disabled)
358 continue;
359
360 if (v->base == TIMER_CALENDAR) {
361 usec_t b;
362
363 /* If we know the last time this was
364 * triggered, schedule the job based relative
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 }
376
377 r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse);
378 if (r < 0)
379 continue;
380
381 if (!found_realtime)
382 t->next_elapse_realtime = v->next_elapse;
383 else
384 t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
385
386 found_realtime = true;
387
388 } else {
389 usec_t base;
390
391 switch (v->base) {
392
393 case TIMER_ACTIVE:
394 if (state_translation_table[t->state] == UNIT_ACTIVE)
395 base = UNIT(t)->inactive_exit_timestamp.monotonic;
396 else
397 base = ts.monotonic;
398 break;
399
400 case TIMER_BOOT:
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
408 * our own startup. */
409 _fallthrough_;
410 case TIMER_STARTUP:
411 base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
412 break;
413
414 case TIMER_UNIT_ACTIVE:
415 leave_around = true;
416 base = trigger->inactive_exit_timestamp.monotonic;
417
418 if (base <= 0)
419 base = t->last_trigger.monotonic;
420
421 if (base <= 0)
422 continue;
423 base = MAX(base, t->last_trigger.monotonic);
424
425 break;
426
427 case TIMER_UNIT_INACTIVE:
428 leave_around = true;
429 base = trigger->inactive_enter_timestamp.monotonic;
430
431 if (base <= 0)
432 base = t->last_trigger.monotonic;
433
434 if (base <= 0)
435 continue;
436 base = MAX(base, t->last_trigger.monotonic);
437
438 break;
439
440 default:
441 assert_not_reached("Unknown timer base");
442 }
443
444 v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
445
446 if (!initial && !time_change &&
447 v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
448 IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
449 /* This is a one time trigger, disable it now */
450 v->disabled = true;
451 continue;
452 }
453
454 if (!found_monotonic)
455 t->next_elapse_monotonic_or_boottime = v->next_elapse;
456 else
457 t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse);
458
459 found_monotonic = true;
460 }
461 }
462
463 if (!found_monotonic && !found_realtime) {
464 log_unit_debug(UNIT(t), "Timer is elapsed.");
465 timer_enter_elapsed(t, leave_around);
466 return;
467 }
468
469 if (found_monotonic) {
470 char buf[FORMAT_TIMESPAN_MAX];
471 usec_t left;
472
473 add_random(t, &t->next_elapse_monotonic_or_boottime);
474
475 left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)));
476 log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", format_timespan(buf, sizeof(buf), left, 0));
477
478 if (t->monotonic_event_source) {
479 r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime);
480 if (r < 0)
481 goto fail;
482
483 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT);
484 if (r < 0)
485 goto fail;
486 } else {
487
488 r = sd_event_add_time(
489 UNIT(t)->manager->event,
490 &t->monotonic_event_source,
491 t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC,
492 t->next_elapse_monotonic_or_boottime, t->accuracy_usec,
493 timer_dispatch, t);
494 if (r < 0)
495 goto fail;
496
497 (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic");
498 }
499
500 } else if (t->monotonic_event_source) {
501
502 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF);
503 if (r < 0)
504 goto fail;
505 }
506
507 if (found_realtime) {
508 char buf[FORMAT_TIMESTAMP_MAX];
509
510 add_random(t, &t->next_elapse_realtime);
511
512 log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
513
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);
520 if (r < 0)
521 goto fail;
522 } else {
523 r = sd_event_add_time(
524 UNIT(t)->manager->event,
525 &t->realtime_event_source,
526 t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME,
527 t->next_elapse_realtime, t->accuracy_usec,
528 timer_dispatch, t);
529 if (r < 0)
530 goto fail;
531
532 (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime");
533 }
534
535 } else if (t->realtime_event_source) {
536
537 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF);
538 if (r < 0)
539 goto fail;
540 }
541
542 timer_set_state(t, TIMER_WAITING);
543 return;
544
545 fail:
546 log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m");
547 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
548 }
549
550 static void timer_enter_running(Timer *t) {
551 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
552 Unit *trigger;
553 int r;
554
555 assert(t);
556
557 /* Don't start job if we are supposed to go down */
558 if (unit_stop_pending(UNIT(t)))
559 return;
560
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);
569 if (r < 0)
570 goto fail;
571
572 dual_timestamp_get(&t->last_trigger);
573
574 if (t->stamp_path)
575 touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID);
576
577 timer_set_state(t, TIMER_RUNNING);
578 return;
579
580 fail:
581 log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r));
582 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
583 }
584
585 static int timer_start(Unit *u) {
586 Timer *t = TIMER(u);
587 TimerValue *v;
588 Unit *trigger;
589 int r;
590
591 assert(t);
592 assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
593
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.");
597 return -ENOENT;
598 }
599
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
606 r = unit_acquire_invocation_id(u);
607 if (r < 0)
608 return r;
609
610 t->last_trigger = DUAL_TIMESTAMP_NULL;
611
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
617 if (t->stamp_path) {
618 struct stat st;
619
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)
637 /* The timer has never run before,
638 * make sure a stamp file exists.
639 */
640 (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
641 }
642
643 t->result = TIMER_SUCCESS;
644 timer_enter_waiting(t, true, false);
645 return 1;
646 }
647
648 static int timer_stop(Unit *u) {
649 Timer *t = TIMER(u);
650
651 assert(t);
652 assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED));
653
654 timer_enter_dead(t, TIMER_SUCCESS);
655 return 1;
656 }
657
658 static 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
665 (void) serialize_item(f, "state", timer_state_to_string(t->state));
666 (void) serialize_item(f, "result", timer_result_to_string(t->result));
667
668 if (t->last_trigger.realtime > 0)
669 (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime);
670
671 if (t->last_trigger.monotonic > 0)
672 (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic);
673
674 return 0;
675 }
676
677 static 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
688 state = timer_state_from_string(value);
689 if (state < 0)
690 log_unit_debug(u, "Failed to parse state value: %s", value);
691 else
692 t->deserialized_state = state;
693
694 } else if (streq(key, "result")) {
695 TimerResult f;
696
697 f = timer_result_from_string(value);
698 if (f < 0)
699 log_unit_debug(u, "Failed to parse result value: %s", value);
700 else if (f != TIMER_SUCCESS)
701 t->result = f;
702
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
708 log_unit_debug(u, "Unknown serialization key: %s", key);
709
710 return 0;
711 }
712
713 _pure_ static UnitActiveState timer_active_state(Unit *u) {
714 assert(u);
715
716 return state_translation_table[TIMER(u)->state];
717 }
718
719 _pure_ static const char *timer_sub_state_to_string(Unit *u) {
720 assert(u);
721
722 return timer_state_to_string(TIMER(u)->state);
723 }
724
725 static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
726 Timer *t = TIMER(userdata);
727
728 assert(t);
729
730 if (t->state != TIMER_WAITING)
731 return 0;
732
733 log_unit_debug(UNIT(t), "Timer elapsed.");
734 timer_enter_running(t);
735 return 0;
736 }
737
738 static void timer_trigger_notify(Unit *u, Unit *other) {
739 Timer *t = TIMER(u);
740 TimerValue *v;
741
742 assert(u);
743 assert(other);
744
745 if (other->load_state != UNIT_LOADED)
746 return;
747
748 /* Reenable all timers that depend on unit state */
749 LIST_FOREACH(value, v, t->values)
750 if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE))
751 v->disabled = false;
752
753 switch (t->state) {
754
755 case TIMER_WAITING:
756 case TIMER_ELAPSED:
757
758 /* Recalculate sleep time */
759 timer_enter_waiting(t, false, false);
760 break;
761
762 case TIMER_RUNNING:
763
764 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
765 log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
766 timer_enter_waiting(t, false, false);
767 }
768 break;
769
770 case TIMER_DEAD:
771 case TIMER_FAILED:
772 break;
773
774 default:
775 assert_not_reached("Unknown timer state");
776 }
777 }
778
779 static void timer_reset_failed(Unit *u) {
780 Timer *t = TIMER(u);
781
782 assert(t);
783
784 if (t->state == TIMER_FAILED)
785 timer_set_state(t, TIMER_DEAD);
786
787 t->result = TIMER_SUCCESS;
788 }
789
790 static void timer_time_change(Unit *u) {
791 Timer *t = TIMER(u);
792 usec_t ts;
793
794 assert(u);
795
796 if (t->state != TIMER_WAITING)
797 return;
798
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
807 log_unit_debug(u, "Time change, recalculating next elapse.");
808 timer_enter_waiting(t, false, true);
809 }
810
811 static 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, false);
821 }
822
823 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
824 [TIMER_ACTIVE] = "OnActiveSec",
825 [TIMER_BOOT] = "OnBootSec",
826 [TIMER_STARTUP] = "OnStartupSec",
827 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
828 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
829 [TIMER_CALENDAR] = "OnCalendar"
830 };
831
832 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
833
834 static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
835 [TIMER_SUCCESS] = "success",
836 [TIMER_FAILURE_RESOURCES] = "resources",
837 [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
838 };
839
840 DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
841
842 const UnitVTable timer_vtable = {
843 .object_size = sizeof(Timer),
844
845 .sections =
846 "Unit\0"
847 "Timer\0"
848 "Install\0",
849 .private_section = "Timer",
850
851 .init = timer_init,
852 .done = timer_done,
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
868 .trigger_notify = timer_trigger_notify,
869
870 .reset_failed = timer_reset_failed,
871 .time_change = timer_time_change,
872 .timezone_change = timer_timezone_change,
873
874 .bus_vtable = bus_timer_vtable,
875 .bus_set_property = bus_timer_set_property,
876
877 .can_transient = true,
878 };