]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/timer.c
core: unify code for checking whether unit to trigger is loaded
[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 int r;
599
600 assert(t);
601 assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
602
603 r = unit_test_trigger_loaded(u);
604 if (r < 0)
605 return r;
606
607 r = unit_test_start_limit(u);
608 if (r < 0) {
609 timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
610 return r;
611 }
612
613 r = unit_acquire_invocation_id(u);
614 if (r < 0)
615 return r;
616
617 t->last_trigger = DUAL_TIMESTAMP_NULL;
618
619 /* Reenable all timers that depend on unit activation time */
620 LIST_FOREACH(value, v, t->values)
621 if (v->base == TIMER_ACTIVE)
622 v->disabled = false;
623
624 if (t->stamp_path) {
625 struct stat st;
626
627 if (stat(t->stamp_path, &st) >= 0) {
628 usec_t ft;
629
630 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
631 * something is wrong with the system clock. */
632
633 ft = timespec_load(&st.st_mtim);
634 if (ft < now(CLOCK_REALTIME))
635 t->last_trigger.realtime = ft;
636 else {
637 char z[FORMAT_TIMESTAMP_MAX];
638
639 log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.",
640 format_timestamp(z, sizeof(z), ft));
641 }
642
643 } else if (errno == ENOENT)
644 /* The timer has never run before,
645 * make sure a stamp file exists.
646 */
647 (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
648 }
649
650 t->result = TIMER_SUCCESS;
651 timer_enter_waiting(t, false);
652 return 1;
653 }
654
655 static int timer_stop(Unit *u) {
656 Timer *t = TIMER(u);
657
658 assert(t);
659 assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED));
660
661 timer_enter_dead(t, TIMER_SUCCESS);
662 return 1;
663 }
664
665 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
666 Timer *t = TIMER(u);
667
668 assert(u);
669 assert(f);
670 assert(fds);
671
672 (void) serialize_item(f, "state", timer_state_to_string(t->state));
673 (void) serialize_item(f, "result", timer_result_to_string(t->result));
674
675 if (t->last_trigger.realtime > 0)
676 (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime);
677
678 if (t->last_trigger.monotonic > 0)
679 (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic);
680
681 return 0;
682 }
683
684 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
685 Timer *t = TIMER(u);
686
687 assert(u);
688 assert(key);
689 assert(value);
690 assert(fds);
691
692 if (streq(key, "state")) {
693 TimerState state;
694
695 state = timer_state_from_string(value);
696 if (state < 0)
697 log_unit_debug(u, "Failed to parse state value: %s", value);
698 else
699 t->deserialized_state = state;
700
701 } else if (streq(key, "result")) {
702 TimerResult f;
703
704 f = timer_result_from_string(value);
705 if (f < 0)
706 log_unit_debug(u, "Failed to parse result value: %s", value);
707 else if (f != TIMER_SUCCESS)
708 t->result = f;
709
710 } else if (streq(key, "last-trigger-realtime"))
711 (void) deserialize_usec(value, &t->last_trigger.realtime);
712 else if (streq(key, "last-trigger-monotonic"))
713 (void) deserialize_usec(value, &t->last_trigger.monotonic);
714 else
715 log_unit_debug(u, "Unknown serialization key: %s", key);
716
717 return 0;
718 }
719
720 _pure_ static UnitActiveState timer_active_state(Unit *u) {
721 assert(u);
722
723 return state_translation_table[TIMER(u)->state];
724 }
725
726 _pure_ static const char *timer_sub_state_to_string(Unit *u) {
727 assert(u);
728
729 return timer_state_to_string(TIMER(u)->state);
730 }
731
732 static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
733 Timer *t = TIMER(userdata);
734
735 assert(t);
736
737 if (t->state != TIMER_WAITING)
738 return 0;
739
740 log_unit_debug(UNIT(t), "Timer elapsed.");
741 timer_enter_running(t);
742 return 0;
743 }
744
745 static void timer_trigger_notify(Unit *u, Unit *other) {
746 Timer *t = TIMER(u);
747 TimerValue *v;
748
749 assert(u);
750 assert(other);
751
752 if (other->load_state != UNIT_LOADED)
753 return;
754
755 /* Reenable all timers that depend on unit state */
756 LIST_FOREACH(value, v, t->values)
757 if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE))
758 v->disabled = false;
759
760 switch (t->state) {
761
762 case TIMER_WAITING:
763 case TIMER_ELAPSED:
764
765 /* Recalculate sleep time */
766 timer_enter_waiting(t, false);
767 break;
768
769 case TIMER_RUNNING:
770
771 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
772 log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
773 timer_enter_waiting(t, false);
774 }
775 break;
776
777 case TIMER_DEAD:
778 case TIMER_FAILED:
779 break;
780
781 default:
782 assert_not_reached("Unknown timer state");
783 }
784 }
785
786 static void timer_reset_failed(Unit *u) {
787 Timer *t = TIMER(u);
788
789 assert(t);
790
791 if (t->state == TIMER_FAILED)
792 timer_set_state(t, TIMER_DEAD);
793
794 t->result = TIMER_SUCCESS;
795 }
796
797 static void timer_time_change(Unit *u) {
798 Timer *t = TIMER(u);
799 usec_t ts;
800
801 assert(u);
802
803 if (t->state != TIMER_WAITING)
804 return;
805
806 /* If we appear to have triggered in the future, the system clock must
807 * have been set backwards. So let's rewind our own clock and allow
808 * the future trigger(s) to happen again :). Exactly the same as when
809 * you start a timer unit with Persistent=yes. */
810 ts = now(CLOCK_REALTIME);
811 if (t->last_trigger.realtime > ts)
812 t->last_trigger.realtime = ts;
813
814 log_unit_debug(u, "Time change, recalculating next elapse.");
815 timer_enter_waiting(t, true);
816 }
817
818 static void timer_timezone_change(Unit *u) {
819 Timer *t = TIMER(u);
820
821 assert(u);
822
823 if (t->state != TIMER_WAITING)
824 return;
825
826 log_unit_debug(u, "Timezone change, recalculating next elapse.");
827 timer_enter_waiting(t, false);
828 }
829
830 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
831 [TIMER_ACTIVE] = "OnActiveSec",
832 [TIMER_BOOT] = "OnBootSec",
833 [TIMER_STARTUP] = "OnStartupSec",
834 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
835 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
836 [TIMER_CALENDAR] = "OnCalendar"
837 };
838
839 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
840
841 static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
842 [TIMER_SUCCESS] = "success",
843 [TIMER_FAILURE_RESOURCES] = "resources",
844 [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
845 };
846
847 DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
848
849 const UnitVTable timer_vtable = {
850 .object_size = sizeof(Timer),
851
852 .sections =
853 "Unit\0"
854 "Timer\0"
855 "Install\0",
856 .private_section = "Timer",
857
858 .init = timer_init,
859 .done = timer_done,
860 .load = timer_load,
861
862 .coldplug = timer_coldplug,
863
864 .dump = timer_dump,
865
866 .start = timer_start,
867 .stop = timer_stop,
868
869 .serialize = timer_serialize,
870 .deserialize_item = timer_deserialize_item,
871
872 .active_state = timer_active_state,
873 .sub_state_to_string = timer_sub_state_to_string,
874
875 .trigger_notify = timer_trigger_notify,
876
877 .reset_failed = timer_reset_failed,
878 .time_change = timer_time_change,
879 .timezone_change = timer_timezone_change,
880
881 .bus_vtable = bus_timer_vtable,
882 .bus_set_property = bus_timer_set_property,
883
884 .can_transient = true,
885 };