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