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