]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/timer.c
Merge pull request #10935 from poettering/rlimit-nofile-safe
[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 if (!found_realtime)
384 t->next_elapse_realtime = v->next_elapse;
385 else
386 t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
387
388 found_realtime = true;
389
390 } else {
391 usec_t base;
392
393 switch (v->base) {
394
395 case TIMER_ACTIVE:
396 if (state_translation_table[t->state] == UNIT_ACTIVE)
397 base = UNIT(t)->inactive_exit_timestamp.monotonic;
398 else
399 base = ts.monotonic;
400 break;
401
402 case TIMER_BOOT:
403 if (detect_container() <= 0) {
404 /* CLOCK_MONOTONIC equals the uptime on Linux */
405 base = 0;
406 break;
407 }
408 /* In a container we don't want to include the time the host
409 * was already up when the container started, so count from
410 * our own startup. */
411 _fallthrough_;
412 case TIMER_STARTUP:
413 base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
414 break;
415
416 case TIMER_UNIT_ACTIVE:
417 leave_around = true;
418 base = trigger->inactive_exit_timestamp.monotonic;
419
420 if (base <= 0)
421 base = t->last_trigger.monotonic;
422
423 if (base <= 0)
424 continue;
425 base = MAX(base, t->last_trigger.monotonic);
426
427 break;
428
429 case TIMER_UNIT_INACTIVE:
430 leave_around = true;
431 base = trigger->inactive_enter_timestamp.monotonic;
432
433 if (base <= 0)
434 base = t->last_trigger.monotonic;
435
436 if (base <= 0)
437 continue;
438 base = MAX(base, t->last_trigger.monotonic);
439
440 break;
441
442 default:
443 assert_not_reached("Unknown timer base");
444 }
445
446 v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
447
448 if (dual_timestamp_is_set(&t->last_trigger) &&
449 !time_change &&
450 v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
451 IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
452 /* This is a one time trigger, disable it now */
453 v->disabled = true;
454 continue;
455 }
456
457 if (!found_monotonic)
458 t->next_elapse_monotonic_or_boottime = v->next_elapse;
459 else
460 t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse);
461
462 found_monotonic = true;
463 }
464 }
465
466 if (!found_monotonic && !found_realtime) {
467 log_unit_debug(UNIT(t), "Timer is elapsed.");
468 timer_enter_elapsed(t, leave_around);
469 return;
470 }
471
472 if (found_monotonic) {
473 char buf[FORMAT_TIMESPAN_MAX];
474 usec_t left;
475
476 add_random(t, &t->next_elapse_monotonic_or_boottime);
477
478 left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)));
479 log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", format_timespan(buf, sizeof(buf), left, 0));
480
481 if (t->monotonic_event_source) {
482 r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime);
483 if (r < 0)
484 goto fail;
485
486 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT);
487 if (r < 0)
488 goto fail;
489 } else {
490
491 r = sd_event_add_time(
492 UNIT(t)->manager->event,
493 &t->monotonic_event_source,
494 t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC,
495 t->next_elapse_monotonic_or_boottime, t->accuracy_usec,
496 timer_dispatch, t);
497 if (r < 0)
498 goto fail;
499
500 (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic");
501 }
502
503 } else if (t->monotonic_event_source) {
504
505 r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF);
506 if (r < 0)
507 goto fail;
508 }
509
510 if (found_realtime) {
511 char buf[FORMAT_TIMESTAMP_MAX];
512
513 add_random(t, &t->next_elapse_realtime);
514
515 log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
516
517 if (t->realtime_event_source) {
518 r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime);
519 if (r < 0)
520 goto fail;
521
522 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT);
523 if (r < 0)
524 goto fail;
525 } else {
526 r = sd_event_add_time(
527 UNIT(t)->manager->event,
528 &t->realtime_event_source,
529 t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME,
530 t->next_elapse_realtime, t->accuracy_usec,
531 timer_dispatch, t);
532 if (r < 0)
533 goto fail;
534
535 (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime");
536 }
537
538 } else if (t->realtime_event_source) {
539
540 r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF);
541 if (r < 0)
542 goto fail;
543 }
544
545 timer_set_state(t, TIMER_WAITING);
546 return;
547
548 fail:
549 log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m");
550 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
551 }
552
553 static void timer_enter_running(Timer *t) {
554 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
555 Unit *trigger;
556 int r;
557
558 assert(t);
559
560 /* Don't start job if we are supposed to go down */
561 if (unit_stop_pending(UNIT(t)))
562 return;
563
564 trigger = UNIT_TRIGGER(UNIT(t));
565 if (!trigger) {
566 log_unit_error(UNIT(t), "Unit to trigger vanished.");
567 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
568 return;
569 }
570
571 r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, &error, NULL);
572 if (r < 0)
573 goto fail;
574
575 dual_timestamp_get(&t->last_trigger);
576
577 if (t->stamp_path)
578 touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID);
579
580 timer_set_state(t, TIMER_RUNNING);
581 return;
582
583 fail:
584 log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r));
585 timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
586 }
587
588 static int timer_start(Unit *u) {
589 Timer *t = TIMER(u);
590 TimerValue *v;
591 Unit *trigger;
592 int r;
593
594 assert(t);
595 assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
596
597 trigger = UNIT_TRIGGER(u);
598 if (!trigger || trigger->load_state != UNIT_LOADED) {
599 log_unit_error(u, "Refusing to start, unit to trigger not loaded.");
600 return -ENOENT;
601 }
602
603 r = unit_start_limit_test(u);
604 if (r < 0) {
605 timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
606 return r;
607 }
608
609 r = unit_acquire_invocation_id(u);
610 if (r < 0)
611 return r;
612
613 t->last_trigger = DUAL_TIMESTAMP_NULL;
614
615 /* Reenable all timers that depend on unit activation time */
616 LIST_FOREACH(value, v, t->values)
617 if (v->base == TIMER_ACTIVE)
618 v->disabled = false;
619
620 if (t->stamp_path) {
621 struct stat st;
622
623 if (stat(t->stamp_path, &st) >= 0) {
624 usec_t ft;
625
626 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
627 * something is wrong with the system clock. */
628
629 ft = timespec_load(&st.st_mtim);
630 if (ft < now(CLOCK_REALTIME))
631 t->last_trigger.realtime = ft;
632 else {
633 char z[FORMAT_TIMESTAMP_MAX];
634
635 log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.",
636 format_timestamp(z, sizeof(z), ft));
637 }
638
639 } else if (errno == ENOENT)
640 /* The timer has never run before,
641 * make sure a stamp file exists.
642 */
643 (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
644 }
645
646 t->result = TIMER_SUCCESS;
647 timer_enter_waiting(t, false);
648 return 1;
649 }
650
651 static int timer_stop(Unit *u) {
652 Timer *t = TIMER(u);
653
654 assert(t);
655 assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED));
656
657 timer_enter_dead(t, TIMER_SUCCESS);
658 return 1;
659 }
660
661 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
662 Timer *t = TIMER(u);
663
664 assert(u);
665 assert(f);
666 assert(fds);
667
668 (void) serialize_item(f, "state", timer_state_to_string(t->state));
669 (void) serialize_item(f, "result", timer_result_to_string(t->result));
670
671 if (t->last_trigger.realtime > 0)
672 (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime);
673
674 if (t->last_trigger.monotonic > 0)
675 (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic);
676
677 return 0;
678 }
679
680 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
681 Timer *t = TIMER(u);
682
683 assert(u);
684 assert(key);
685 assert(value);
686 assert(fds);
687
688 if (streq(key, "state")) {
689 TimerState state;
690
691 state = timer_state_from_string(value);
692 if (state < 0)
693 log_unit_debug(u, "Failed to parse state value: %s", value);
694 else
695 t->deserialized_state = state;
696
697 } else if (streq(key, "result")) {
698 TimerResult f;
699
700 f = timer_result_from_string(value);
701 if (f < 0)
702 log_unit_debug(u, "Failed to parse result value: %s", value);
703 else if (f != TIMER_SUCCESS)
704 t->result = f;
705
706 } else if (streq(key, "last-trigger-realtime"))
707 (void) deserialize_usec(value, &t->last_trigger.realtime);
708 else if (streq(key, "last-trigger-monotonic"))
709 (void) deserialize_usec(value, &t->last_trigger.monotonic);
710 else
711 log_unit_debug(u, "Unknown serialization key: %s", key);
712
713 return 0;
714 }
715
716 _pure_ static UnitActiveState timer_active_state(Unit *u) {
717 assert(u);
718
719 return state_translation_table[TIMER(u)->state];
720 }
721
722 _pure_ static const char *timer_sub_state_to_string(Unit *u) {
723 assert(u);
724
725 return timer_state_to_string(TIMER(u)->state);
726 }
727
728 static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) {
729 Timer *t = TIMER(userdata);
730
731 assert(t);
732
733 if (t->state != TIMER_WAITING)
734 return 0;
735
736 log_unit_debug(UNIT(t), "Timer elapsed.");
737 timer_enter_running(t);
738 return 0;
739 }
740
741 static void timer_trigger_notify(Unit *u, Unit *other) {
742 Timer *t = TIMER(u);
743 TimerValue *v;
744
745 assert(u);
746 assert(other);
747
748 if (other->load_state != UNIT_LOADED)
749 return;
750
751 /* Reenable all timers that depend on unit state */
752 LIST_FOREACH(value, v, t->values)
753 if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE))
754 v->disabled = false;
755
756 switch (t->state) {
757
758 case TIMER_WAITING:
759 case TIMER_ELAPSED:
760
761 /* Recalculate sleep time */
762 timer_enter_waiting(t, false);
763 break;
764
765 case TIMER_RUNNING:
766
767 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
768 log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
769 timer_enter_waiting(t, false);
770 }
771 break;
772
773 case TIMER_DEAD:
774 case TIMER_FAILED:
775 break;
776
777 default:
778 assert_not_reached("Unknown timer state");
779 }
780 }
781
782 static void timer_reset_failed(Unit *u) {
783 Timer *t = TIMER(u);
784
785 assert(t);
786
787 if (t->state == TIMER_FAILED)
788 timer_set_state(t, TIMER_DEAD);
789
790 t->result = TIMER_SUCCESS;
791 }
792
793 static void timer_time_change(Unit *u) {
794 Timer *t = TIMER(u);
795 usec_t ts;
796
797 assert(u);
798
799 if (t->state != TIMER_WAITING)
800 return;
801
802 /* If we appear to have triggered in the future, the system clock must
803 * have been set backwards. So let's rewind our own clock and allow
804 * the future trigger(s) to happen again :). Exactly the same as when
805 * you start a timer unit with Persistent=yes. */
806 ts = now(CLOCK_REALTIME);
807 if (t->last_trigger.realtime > ts)
808 t->last_trigger.realtime = ts;
809
810 log_unit_debug(u, "Time change, recalculating next elapse.");
811 timer_enter_waiting(t, true);
812 }
813
814 static void timer_timezone_change(Unit *u) {
815 Timer *t = TIMER(u);
816
817 assert(u);
818
819 if (t->state != TIMER_WAITING)
820 return;
821
822 log_unit_debug(u, "Timezone change, recalculating next elapse.");
823 timer_enter_waiting(t, false);
824 }
825
826 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
827 [TIMER_ACTIVE] = "OnActiveSec",
828 [TIMER_BOOT] = "OnBootSec",
829 [TIMER_STARTUP] = "OnStartupSec",
830 [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
831 [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
832 [TIMER_CALENDAR] = "OnCalendar"
833 };
834
835 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
836
837 static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
838 [TIMER_SUCCESS] = "success",
839 [TIMER_FAILURE_RESOURCES] = "resources",
840 [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
841 };
842
843 DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult);
844
845 const UnitVTable timer_vtable = {
846 .object_size = sizeof(Timer),
847
848 .sections =
849 "Unit\0"
850 "Timer\0"
851 "Install\0",
852 .private_section = "Timer",
853
854 .init = timer_init,
855 .done = timer_done,
856 .load = timer_load,
857
858 .coldplug = timer_coldplug,
859
860 .dump = timer_dump,
861
862 .start = timer_start,
863 .stop = timer_stop,
864
865 .serialize = timer_serialize,
866 .deserialize_item = timer_deserialize_item,
867
868 .active_state = timer_active_state,
869 .sub_state_to_string = timer_sub_state_to_string,
870
871 .trigger_notify = timer_trigger_notify,
872
873 .reset_failed = timer_reset_failed,
874 .time_change = timer_time_change,
875 .timezone_change = timer_timezone_change,
876
877 .bus_vtable = bus_timer_vtable,
878 .bus_set_property = bus_timer_set_property,
879
880 .can_transient = true,
881 };