1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
10 #include "bus-error.h"
12 #include "dbus-timer.h"
13 #include "dbus-unit.h"
15 #include "parse-util.h"
16 #include "random-util.h"
17 #include "serialize.h"
19 #include "string-table.h"
20 #include "string-util.h"
22 #include "unit-name.h"
24 #include "user-util.h"
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
35 static int timer_dispatch(sd_event_source
*s
, uint64_t usec
, void *userdata
);
37 static void timer_init(Unit
*u
) {
41 assert(u
->load_state
== UNIT_STUB
);
43 t
->next_elapse_monotonic_or_boottime
= USEC_INFINITY
;
44 t
->next_elapse_realtime
= USEC_INFINITY
;
45 t
->accuracy_usec
= u
->manager
->defaults
.timer_accuracy_usec
;
46 t
->remain_after_elapse
= true;
49 void timer_free_values(Timer
*t
) {
54 while ((v
= LIST_POP(value
, t
->values
))) {
55 calendar_spec_free(v
->calendar_spec
);
60 static void timer_done(Unit
*u
) {
67 t
->monotonic_event_source
= sd_event_source_disable_unref(t
->monotonic_event_source
);
68 t
->realtime_event_source
= sd_event_source_disable_unref(t
->realtime_event_source
);
70 t
->stamp_path
= mfree(t
->stamp_path
);
73 static int timer_verify(Timer
*t
) {
75 assert(UNIT(t
)->load_state
== UNIT_LOADED
);
77 if (!t
->values
&& !t
->on_clock_change
&& !t
->on_timezone_change
)
78 return log_unit_error_errno(UNIT(t
), SYNTHETIC_ERRNO(ENOEXEC
), "Timer unit lacks value setting. Refusing.");
83 static int timer_add_default_dependencies(Timer
*t
) {
88 if (!UNIT(t
)->default_dependencies
)
91 r
= unit_add_dependency_by_name(UNIT(t
), UNIT_BEFORE
, SPECIAL_TIMERS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
95 if (MANAGER_IS_SYSTEM(UNIT(t
)->manager
)) {
96 r
= unit_add_two_dependencies_by_name(UNIT(t
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
100 LIST_FOREACH(value
, v
, t
->values
) {
101 if (v
->base
!= TIMER_CALENDAR
)
104 FOREACH_STRING(target
, SPECIAL_TIME_SYNC_TARGET
, SPECIAL_TIME_SET_TARGET
) {
105 r
= unit_add_dependency_by_name(UNIT(t
), UNIT_AFTER
, target
, true, UNIT_DEPENDENCY_DEFAULT
);
114 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
117 static int timer_add_trigger_dependencies(Timer
*t
) {
123 if (UNIT_TRIGGER(UNIT(t
)))
126 r
= unit_load_related_unit(UNIT(t
), ".service", &x
);
130 return unit_add_two_dependencies(UNIT(t
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
133 static int timer_setup_persistent(Timer
*t
) {
134 _cleanup_free_
char *stamp_path
= NULL
;
142 if (MANAGER_IS_SYSTEM(UNIT(t
)->manager
)) {
144 r
= unit_add_mounts_for(UNIT(t
), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE
, UNIT_MOUNT_REQUIRES
);
148 stamp_path
= strjoin("/var/lib/systemd/timers/stamp-", UNIT(t
)->id
);
152 e
= getenv("XDG_DATA_HOME");
154 stamp_path
= strjoin(e
, "/systemd/timers/stamp-", UNIT(t
)->id
);
157 _cleanup_free_
char *h
= NULL
;
159 r
= get_home_dir(&h
);
161 return log_unit_error_errno(UNIT(t
), r
, "Failed to determine home directory: %m");
163 stamp_path
= strjoin(h
, "/.local/share/systemd/timers/stamp-", UNIT(t
)->id
);
170 return free_and_replace(t
->stamp_path
, stamp_path
);
173 static uint64_t timer_get_fixed_delay_hash(Timer
*t
) {
174 static const uint8_t hash_key
[] = {
175 0x51, 0x0a, 0xdb, 0x76, 0x29, 0x51, 0x42, 0xc2,
176 0x80, 0x35, 0xea, 0xe6, 0x8e, 0x3a, 0x37, 0xbd
179 struct siphash state
;
180 sd_id128_t machine_id
;
187 r
= sd_id128_get_machine(&machine_id
);
189 log_unit_debug_errno(UNIT(t
), r
,
190 "Failed to get machine ID for the fixed delay calculation, proceeding with 0: %m");
191 machine_id
= SD_ID128_NULL
;
194 siphash24_init(&state
, hash_key
);
195 siphash24_compress(&machine_id
, sizeof(sd_id128_t
), &state
);
196 siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t
)->manager
), &state
);
197 siphash24_compress(&uid
, sizeof(uid_t
), &state
);
198 siphash24_compress_string(UNIT(t
)->id
, &state
);
200 return siphash24_finalize(&state
);
203 static int timer_load(Unit
*u
) {
208 assert(u
->load_state
== UNIT_STUB
);
210 r
= unit_load_fragment_and_dropin(u
, true);
214 if (u
->load_state
!= UNIT_LOADED
)
217 /* This is a new unit? Then let's add in some extras */
218 r
= timer_add_trigger_dependencies(t
);
222 r
= timer_setup_persistent(t
);
226 r
= timer_add_default_dependencies(t
);
230 return timer_verify(t
);
233 static void timer_dump(Unit
*u
, FILE *f
, const char *prefix
) {
237 trigger
= UNIT_TRIGGER(u
);
240 "%sTimer State: %s\n"
246 "%sRemainAfterElapse: %s\n"
247 "%sFixedRandomDelay: %s\n"
248 "%sOnClockChange: %s\n"
249 "%sOnTimeZoneChange: %s\n",
250 prefix
, timer_state_to_string(t
->state
),
251 prefix
, timer_result_to_string(t
->result
),
252 prefix
, trigger
? trigger
->id
: "n/a",
253 prefix
, yes_no(t
->persistent
),
254 prefix
, yes_no(t
->wake_system
),
255 prefix
, FORMAT_TIMESPAN(t
->accuracy_usec
, 1),
256 prefix
, yes_no(t
->remain_after_elapse
),
257 prefix
, yes_no(t
->fixed_random_delay
),
258 prefix
, yes_no(t
->on_clock_change
),
259 prefix
, yes_no(t
->on_timezone_change
));
261 LIST_FOREACH(value
, v
, t
->values
)
262 if (v
->base
== TIMER_CALENDAR
) {
263 _cleanup_free_
char *p
= NULL
;
265 (void) calendar_spec_to_string(v
->calendar_spec
, &p
);
270 timer_base_to_string(v
->base
),
276 timer_base_to_string(v
->base
),
277 FORMAT_TIMESPAN(v
->value
, 0));
280 static void timer_set_state(Timer
*t
, TimerState state
) {
281 TimerState old_state
;
284 if (t
->state
!= state
)
285 bus_unit_send_pending_change_signal(UNIT(t
), false);
287 old_state
= t
->state
;
290 if (state
!= TIMER_WAITING
) {
291 t
->monotonic_event_source
= sd_event_source_disable_unref(t
->monotonic_event_source
);
292 t
->realtime_event_source
= sd_event_source_disable_unref(t
->realtime_event_source
);
293 t
->next_elapse_monotonic_or_boottime
= USEC_INFINITY
;
294 t
->next_elapse_realtime
= USEC_INFINITY
;
297 if (state
!= old_state
)
298 log_unit_debug(UNIT(t
), "Changed %s -> %s", timer_state_to_string(old_state
), timer_state_to_string(state
));
300 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
303 static void timer_enter_waiting(Timer
*t
, bool time_change
);
305 static int timer_coldplug(Unit
*u
) {
309 assert(t
->state
== TIMER_DEAD
);
311 if (t
->deserialized_state
== t
->state
)
314 if (t
->deserialized_state
== TIMER_WAITING
)
315 timer_enter_waiting(t
, false);
317 timer_set_state(t
, t
->deserialized_state
);
322 static void timer_enter_dead(Timer
*t
, TimerResult f
) {
325 if (t
->result
== TIMER_SUCCESS
)
328 unit_log_result(UNIT(t
), t
->result
== TIMER_SUCCESS
, timer_result_to_string(t
->result
));
329 timer_set_state(t
, t
->result
!= TIMER_SUCCESS
? TIMER_FAILED
: TIMER_DEAD
);
332 static void timer_enter_elapsed(Timer
*t
, bool leave_around
) {
335 /* If a unit is marked with RemainAfterElapse=yes we leave it
336 * around even after it elapsed once, so that starting it
337 * later again does not necessarily mean immediate
338 * retriggering. We unconditionally leave units with
339 * TIMER_UNIT_ACTIVE or TIMER_UNIT_INACTIVE triggers around,
340 * since they might be restarted automatically at any time
343 if (t
->remain_after_elapse
|| leave_around
)
344 timer_set_state(t
, TIMER_ELAPSED
);
346 timer_enter_dead(t
, TIMER_SUCCESS
);
349 static void add_random(Timer
*t
, usec_t
*v
) {
355 if (t
->random_usec
== 0)
357 if (*v
== USEC_INFINITY
)
360 add
= (t
->fixed_random_delay
? timer_get_fixed_delay_hash(t
) : random_u64()) % t
->random_usec
;
362 if (*v
+ add
< *v
) /* overflow */
363 *v
= (usec_t
) -2; /* Highest possible value, that is not USEC_INFINITY */
367 log_unit_debug(UNIT(t
), "Adding %s random time.", FORMAT_TIMESPAN(add
, 0));
370 static void timer_enter_waiting(Timer
*t
, bool time_change
) {
371 bool found_monotonic
= false, found_realtime
= false;
372 bool leave_around
= false;
379 trigger
= UNIT_TRIGGER(UNIT(t
));
381 log_unit_error(UNIT(t
), "Unit to trigger vanished.");
385 triple_timestamp_now(&ts
);
386 t
->next_elapse_monotonic_or_boottime
= t
->next_elapse_realtime
= 0;
388 LIST_FOREACH(value
, v
, t
->values
) {
392 if (v
->base
== TIMER_CALENDAR
) {
395 /* If we know the last time this was
396 * triggered, schedule the job based relative
397 * to that. If we don't, just start from
398 * the activation time. */
400 if (dual_timestamp_is_set(&t
->last_trigger
))
401 b
= t
->last_trigger
.realtime
;
402 else if (dual_timestamp_is_set(&UNIT(t
)->inactive_exit_timestamp
))
403 b
= UNIT(t
)->inactive_exit_timestamp
.realtime
;
407 r
= calendar_spec_next_usec(v
->calendar_spec
, b
, &v
->next_elapse
);
411 /* To make the delay due to RandomizedDelaySec= work even at boot, if the scheduled
412 * time has already passed, set the time when systemd first started as the scheduled
413 * time. Note that we base this on the monotonic timestamp of the boot, not the
414 * realtime one, since the wallclock might have been off during boot. */
415 rebased
= map_clock_usec(UNIT(t
)->manager
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
].monotonic
,
416 CLOCK_MONOTONIC
, CLOCK_REALTIME
);
417 if (v
->next_elapse
< rebased
)
418 v
->next_elapse
= rebased
;
421 t
->next_elapse_realtime
= v
->next_elapse
;
423 t
->next_elapse_realtime
= MIN(t
->next_elapse_realtime
, v
->next_elapse
);
425 found_realtime
= true;
433 if (state_translation_table
[t
->state
] == UNIT_ACTIVE
)
434 base
= UNIT(t
)->inactive_exit_timestamp
.monotonic
;
440 if (detect_container() <= 0) {
441 /* CLOCK_MONOTONIC equals the uptime on Linux */
445 /* In a container we don't want to include the time the host
446 * was already up when the container started, so count from
447 * our own startup. */
450 base
= UNIT(t
)->manager
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
].monotonic
;
453 case TIMER_UNIT_ACTIVE
:
455 base
= MAX(trigger
->inactive_exit_timestamp
.monotonic
, t
->last_trigger
.monotonic
);
460 case TIMER_UNIT_INACTIVE
:
462 base
= MAX(trigger
->inactive_enter_timestamp
.monotonic
, t
->last_trigger
.monotonic
);
468 assert_not_reached();
471 v
->next_elapse
= usec_add(usec_shift_clock(base
, CLOCK_MONOTONIC
, TIMER_MONOTONIC_CLOCK(t
)), v
->value
);
473 if (dual_timestamp_is_set(&t
->last_trigger
) &&
475 v
->next_elapse
< triple_timestamp_by_clock(&ts
, TIMER_MONOTONIC_CLOCK(t
)) &&
476 IN_SET(v
->base
, TIMER_ACTIVE
, TIMER_BOOT
, TIMER_STARTUP
)) {
477 /* This is a one time trigger, disable it now */
482 if (!found_monotonic
)
483 t
->next_elapse_monotonic_or_boottime
= v
->next_elapse
;
485 t
->next_elapse_monotonic_or_boottime
= MIN(t
->next_elapse_monotonic_or_boottime
, v
->next_elapse
);
487 found_monotonic
= true;
491 if (!found_monotonic
&& !found_realtime
&& !t
->on_timezone_change
&& !t
->on_clock_change
) {
492 log_unit_debug(UNIT(t
), "Timer is elapsed.");
493 timer_enter_elapsed(t
, leave_around
);
497 if (found_monotonic
) {
500 add_random(t
, &t
->next_elapse_monotonic_or_boottime
);
502 left
= usec_sub_unsigned(t
->next_elapse_monotonic_or_boottime
, triple_timestamp_by_clock(&ts
, TIMER_MONOTONIC_CLOCK(t
)));
503 log_unit_debug(UNIT(t
), "Monotonic timer elapses in %s.", FORMAT_TIMESPAN(left
, 0));
505 if (t
->monotonic_event_source
) {
506 r
= sd_event_source_set_time(t
->monotonic_event_source
, t
->next_elapse_monotonic_or_boottime
);
508 log_unit_warning_errno(UNIT(t
), r
, "Failed to reschedule monotonic event source: %m");
512 r
= sd_event_source_set_enabled(t
->monotonic_event_source
, SD_EVENT_ONESHOT
);
514 log_unit_warning_errno(UNIT(t
), r
, "Failed to enable monotonic event source: %m");
519 r
= sd_event_add_time(
520 UNIT(t
)->manager
->event
,
521 &t
->monotonic_event_source
,
522 t
->wake_system
? CLOCK_BOOTTIME_ALARM
: CLOCK_MONOTONIC
,
523 t
->next_elapse_monotonic_or_boottime
, t
->accuracy_usec
,
526 log_unit_warning_errno(UNIT(t
), r
, "Failed to add monotonic event source: %m");
530 (void) sd_event_source_set_description(t
->monotonic_event_source
, "timer-monotonic");
533 } else if (t
->monotonic_event_source
) {
535 r
= sd_event_source_set_enabled(t
->monotonic_event_source
, SD_EVENT_OFF
);
537 log_unit_warning_errno(UNIT(t
), r
, "Failed to disable monotonic event source: %m");
542 if (found_realtime
) {
543 add_random(t
, &t
->next_elapse_realtime
);
545 log_unit_debug(UNIT(t
), "Realtime timer elapses at %s.", FORMAT_TIMESTAMP(t
->next_elapse_realtime
));
547 if (t
->realtime_event_source
) {
548 r
= sd_event_source_set_time(t
->realtime_event_source
, t
->next_elapse_realtime
);
550 log_unit_warning_errno(UNIT(t
), r
, "Failed to reschedule realtime event source: %m");
554 r
= sd_event_source_set_enabled(t
->realtime_event_source
, SD_EVENT_ONESHOT
);
556 log_unit_warning_errno(UNIT(t
), r
, "Failed to enable realtime event source: %m");
560 r
= sd_event_add_time(
561 UNIT(t
)->manager
->event
,
562 &t
->realtime_event_source
,
563 t
->wake_system
? CLOCK_REALTIME_ALARM
: CLOCK_REALTIME
,
564 t
->next_elapse_realtime
, t
->accuracy_usec
,
567 log_unit_warning_errno(UNIT(t
), r
, "Failed to add realtime event source: %m");
571 (void) sd_event_source_set_description(t
->realtime_event_source
, "timer-realtime");
574 } else if (t
->realtime_event_source
) {
576 r
= sd_event_source_set_enabled(t
->realtime_event_source
, SD_EVENT_OFF
);
578 log_unit_warning_errno(UNIT(t
), r
, "Failed to disable realtime event source: %m");
583 timer_set_state(t
, TIMER_WAITING
);
587 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
590 static void timer_enter_running(Timer
*t
) {
591 _cleanup_(activation_details_unrefp
) ActivationDetails
*details
= NULL
;
592 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
599 /* Don't start job if we are supposed to go down */
600 if (unit_stop_pending(UNIT(t
)))
603 trigger
= UNIT_TRIGGER(UNIT(t
));
605 log_unit_error(UNIT(t
), "Unit to trigger vanished.");
609 details
= activation_details_new(UNIT(t
));
615 r
= manager_add_job(UNIT(t
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, NULL
, &error
, &job
);
617 log_unit_warning(UNIT(t
), "Failed to queue unit startup job: %s", bus_error_message(&error
, r
));
621 dual_timestamp_now(&t
->last_trigger
);
622 ACTIVATION_DETAILS_TIMER(details
)->last_trigger
= t
->last_trigger
;
624 job_set_activation_details(job
, details
);
627 touch_file(t
->stamp_path
, true, t
->last_trigger
.realtime
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
629 timer_set_state(t
, TIMER_RUNNING
);
633 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
636 static int timer_start(Unit
*u
) {
641 assert(IN_SET(t
->state
, TIMER_DEAD
, TIMER_FAILED
));
643 r
= unit_test_trigger_loaded(u
);
647 r
= unit_acquire_invocation_id(u
);
651 t
->last_trigger
= DUAL_TIMESTAMP_NULL
;
653 /* Reenable all timers that depend on unit activation time */
654 LIST_FOREACH(value
, v
, t
->values
)
655 if (v
->base
== TIMER_ACTIVE
)
661 if (stat(t
->stamp_path
, &st
) >= 0) {
664 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
665 * something is wrong with the system clock. */
667 ft
= timespec_load(&st
.st_mtim
);
668 if (ft
< now(CLOCK_REALTIME
))
669 t
->last_trigger
.realtime
= ft
;
671 log_unit_warning(u
, "Not using persistent file timestamp %s as it is in the future.",
672 FORMAT_TIMESTAMP(ft
));
674 } else if (errno
== ENOENT
)
675 /* The timer has never run before, make sure a stamp file exists. */
676 (void) touch_file(t
->stamp_path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
679 t
->result
= TIMER_SUCCESS
;
680 timer_enter_waiting(t
, false);
684 static int timer_stop(Unit
*u
) {
688 assert(IN_SET(t
->state
, TIMER_WAITING
, TIMER_RUNNING
, TIMER_ELAPSED
));
690 timer_enter_dead(t
, TIMER_SUCCESS
);
694 static int timer_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
701 (void) serialize_item(f
, "state", timer_state_to_string(t
->state
));
702 (void) serialize_item(f
, "result", timer_result_to_string(t
->result
));
704 if (dual_timestamp_is_set(&t
->last_trigger
))
705 (void) serialize_usec(f
, "last-trigger-realtime", t
->last_trigger
.realtime
);
707 if (t
->last_trigger
.monotonic
> 0)
708 (void) serialize_usec(f
, "last-trigger-monotonic", t
->last_trigger
.monotonic
);
713 static int timer_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
721 if (streq(key
, "state")) {
724 state
= timer_state_from_string(value
);
726 log_unit_debug(u
, "Failed to parse state value: %s", value
);
728 t
->deserialized_state
= state
;
730 } else if (streq(key
, "result")) {
733 f
= timer_result_from_string(value
);
735 log_unit_debug(u
, "Failed to parse result value: %s", value
);
736 else if (f
!= TIMER_SUCCESS
)
739 } else if (streq(key
, "last-trigger-realtime"))
740 (void) deserialize_usec(value
, &t
->last_trigger
.realtime
);
741 else if (streq(key
, "last-trigger-monotonic"))
742 (void) deserialize_usec(value
, &t
->last_trigger
.monotonic
);
744 log_unit_debug(u
, "Unknown serialization key: %s", key
);
749 static UnitActiveState
timer_active_state(Unit
*u
) {
752 return state_translation_table
[TIMER(u
)->state
];
755 static const char *timer_sub_state_to_string(Unit
*u
) {
758 return timer_state_to_string(TIMER(u
)->state
);
761 static int timer_dispatch(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
762 Timer
*t
= TIMER(userdata
);
766 if (t
->state
!= TIMER_WAITING
)
769 log_unit_debug(UNIT(t
), "Timer elapsed.");
770 timer_enter_running(t
);
774 static void timer_trigger_notify(Unit
*u
, Unit
*other
) {
780 /* Filter out invocations with bogus state */
781 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
783 /* Reenable all timers that depend on unit state */
784 LIST_FOREACH(value
, v
, t
->values
)
785 if (IN_SET(v
->base
, TIMER_UNIT_ACTIVE
, TIMER_UNIT_INACTIVE
))
793 /* Recalculate sleep time */
794 timer_enter_waiting(t
, false);
799 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other
))) {
800 log_unit_debug(UNIT(t
), "Got notified about unit deactivation.");
801 timer_enter_waiting(t
, false);
810 assert_not_reached();
814 static void timer_reset_failed(Unit
*u
) {
819 if (t
->state
== TIMER_FAILED
)
820 timer_set_state(t
, TIMER_DEAD
);
822 t
->result
= TIMER_SUCCESS
;
825 static void timer_time_change(Unit
*u
) {
831 if (t
->state
!= TIMER_WAITING
)
834 /* If we appear to have triggered in the future, the system clock must
835 * have been set backwards. So let's rewind our own clock and allow
836 * the future triggers to happen again :). Exactly the same as when
837 * you start a timer unit with Persistent=yes. */
838 ts
= now(CLOCK_REALTIME
);
839 if (t
->last_trigger
.realtime
> ts
)
840 t
->last_trigger
.realtime
= ts
;
842 if (t
->on_clock_change
) {
843 log_unit_debug(u
, "Time change, triggering activation.");
844 timer_enter_running(t
);
846 log_unit_debug(u
, "Time change, recalculating next elapse.");
847 timer_enter_waiting(t
, true);
851 static void timer_timezone_change(Unit
*u
) {
856 if (t
->state
!= TIMER_WAITING
)
859 if (t
->on_timezone_change
) {
860 log_unit_debug(u
, "Timezone change, triggering activation.");
861 timer_enter_running(t
);
863 log_unit_debug(u
, "Timezone change, recalculating next elapse.");
864 timer_enter_waiting(t
, false);
868 static int timer_clean(Unit
*u
, ExecCleanMask mask
) {
875 if (t
->state
!= TIMER_DEAD
)
878 if (mask
!= EXEC_CLEAN_STATE
)
881 r
= timer_setup_persistent(t
);
888 if (unlink(t
->stamp_path
) && errno
!= ENOENT
)
889 return log_unit_error_errno(u
, errno
, "Failed to clean stamp file of timer: %m");
894 static int timer_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
900 *ret
= t
->persistent
? EXEC_CLEAN_STATE
: 0;
904 static int timer_can_start(Unit
*u
) {
910 r
= unit_test_start_limit(u
);
912 timer_enter_dead(t
, TIMER_FAILURE_START_LIMIT_HIT
);
919 static void activation_details_timer_serialize(ActivationDetails
*details
, FILE *f
) {
920 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(details
);
926 (void) serialize_dual_timestamp(f
, "activation-details-timer-last-trigger", &t
->last_trigger
);
929 static int activation_details_timer_deserialize(const char *key
, const char *value
, ActivationDetails
**details
) {
935 if (!details
|| !*details
)
938 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(*details
);
942 if (!streq(key
, "activation-details-timer-last-trigger"))
945 r
= deserialize_dual_timestamp(value
, &t
->last_trigger
);
952 static int activation_details_timer_append_env(ActivationDetails
*details
, char ***strv
) {
953 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(details
);
960 if (!dual_timestamp_is_set(&t
->last_trigger
))
963 r
= strv_extendf(strv
, "TRIGGER_TIMER_REALTIME_USEC=" USEC_FMT
, t
->last_trigger
.realtime
);
967 r
= strv_extendf(strv
, "TRIGGER_TIMER_MONOTONIC_USEC=" USEC_FMT
, t
->last_trigger
.monotonic
);
971 return 2; /* Return the number of variables added to the env block */
974 static int activation_details_timer_append_pair(ActivationDetails
*details
, char ***strv
) {
975 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(details
);
982 if (!dual_timestamp_is_set(&t
->last_trigger
))
985 r
= strv_extend(strv
, "trigger_timer_realtime_usec");
989 r
= strv_extendf(strv
, USEC_FMT
, t
->last_trigger
.realtime
);
993 r
= strv_extend(strv
, "trigger_timer_monotonic_usec");
997 r
= strv_extendf(strv
, USEC_FMT
, t
->last_trigger
.monotonic
);
1001 return 2; /* Return the number of pairs added to the env block */
1004 uint64_t timer_next_elapse_monotonic(const Timer
*t
) {
1007 return (uint64_t) usec_shift_clock(t
->next_elapse_monotonic_or_boottime
,
1008 TIMER_MONOTONIC_CLOCK(t
), CLOCK_MONOTONIC
);
1011 static const char* const timer_base_table
[_TIMER_BASE_MAX
] = {
1012 [TIMER_ACTIVE
] = "OnActiveSec",
1013 [TIMER_BOOT
] = "OnBootSec",
1014 [TIMER_STARTUP
] = "OnStartupSec",
1015 [TIMER_UNIT_ACTIVE
] = "OnUnitActiveSec",
1016 [TIMER_UNIT_INACTIVE
] = "OnUnitInactiveSec",
1017 [TIMER_CALENDAR
] = "OnCalendar"
1020 DEFINE_STRING_TABLE_LOOKUP(timer_base
, TimerBase
);
1022 char* timer_base_to_usec_string(TimerBase i
) {
1023 _cleanup_free_
char *buf
= NULL
;
1027 s
= timer_base_to_string(i
);
1029 if (endswith(s
, "Sec")) {
1032 buf
= new(char, l
+2);
1036 memcpy(buf
, s
, l
-3);
1037 memcpy(buf
+l
-3, "USec", 5);
1044 return TAKE_PTR(buf
);
1047 static const char* const timer_result_table
[_TIMER_RESULT_MAX
] = {
1048 [TIMER_SUCCESS
] = "success",
1049 [TIMER_FAILURE_RESOURCES
] = "resources",
1050 [TIMER_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1053 DEFINE_STRING_TABLE_LOOKUP(timer_result
, TimerResult
);
1055 const UnitVTable timer_vtable
= {
1056 .object_size
= sizeof(Timer
),
1062 .private_section
= "Timer",
1064 .can_transient
= true,
1066 .can_trigger
= true,
1072 .coldplug
= timer_coldplug
,
1076 .start
= timer_start
,
1079 .clean
= timer_clean
,
1080 .can_clean
= timer_can_clean
,
1082 .serialize
= timer_serialize
,
1083 .deserialize_item
= timer_deserialize_item
,
1085 .active_state
= timer_active_state
,
1086 .sub_state_to_string
= timer_sub_state_to_string
,
1088 .trigger_notify
= timer_trigger_notify
,
1090 .reset_failed
= timer_reset_failed
,
1091 .time_change
= timer_time_change
,
1092 .timezone_change
= timer_timezone_change
,
1094 .bus_set_property
= bus_timer_set_property
,
1096 .can_start
= timer_can_start
,
1099 const ActivationDetailsVTable activation_details_timer_vtable
= {
1100 .object_size
= sizeof(ActivationDetailsTimer
),
1102 .serialize
= activation_details_timer_serialize
,
1103 .deserialize
= activation_details_timer_deserialize
,
1104 .append_env
= activation_details_timer_append_env
,
1105 .append_pair
= activation_details_timer_append_pair
,