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
->default_timer_accuracy_usec
;
46 t
->remain_after_elapse
= true;
49 void timer_free_values(Timer
*t
) {
54 while ((v
= t
->values
)) {
55 LIST_REMOVE(value
, t
->values
, v
);
56 calendar_spec_free(v
->calendar_spec
);
61 static void timer_done(Unit
*u
) {
68 t
->monotonic_event_source
= sd_event_source_disable_unref(t
->monotonic_event_source
);
69 t
->realtime_event_source
= sd_event_source_disable_unref(t
->realtime_event_source
);
71 t
->stamp_path
= mfree(t
->stamp_path
);
74 static int timer_verify(Timer
*t
) {
76 assert(UNIT(t
)->load_state
== UNIT_LOADED
);
78 if (!t
->values
&& !t
->on_clock_change
&& !t
->on_timezone_change
)
79 return log_unit_error_errno(UNIT(t
), SYNTHETIC_ERRNO(ENOEXEC
), "Timer unit lacks value setting. Refusing.");
84 static int timer_add_default_dependencies(Timer
*t
) {
89 if (!UNIT(t
)->default_dependencies
)
92 r
= unit_add_dependency_by_name(UNIT(t
), UNIT_BEFORE
, SPECIAL_TIMERS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
96 if (MANAGER_IS_SYSTEM(UNIT(t
)->manager
)) {
97 r
= unit_add_two_dependencies_by_name(UNIT(t
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
101 LIST_FOREACH(value
, v
, t
->values
) {
102 if (v
->base
!= TIMER_CALENDAR
)
105 FOREACH_STRING(target
, SPECIAL_TIME_SYNC_TARGET
, SPECIAL_TIME_SET_TARGET
) {
106 r
= unit_add_dependency_by_name(UNIT(t
), UNIT_AFTER
, target
, true, UNIT_DEPENDENCY_DEFAULT
);
115 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
118 static int timer_add_trigger_dependencies(Timer
*t
) {
124 if (UNIT_TRIGGER(UNIT(t
)))
127 r
= unit_load_related_unit(UNIT(t
), ".service", &x
);
131 return unit_add_two_dependencies(UNIT(t
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
134 static int timer_setup_persistent(Timer
*t
) {
135 _cleanup_free_
char *stamp_path
= NULL
;
143 if (MANAGER_IS_SYSTEM(UNIT(t
)->manager
)) {
145 r
= unit_require_mounts_for(UNIT(t
), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE
);
149 stamp_path
= strjoin("/var/lib/systemd/timers/stamp-", UNIT(t
)->id
);
153 e
= getenv("XDG_DATA_HOME");
155 stamp_path
= strjoin(e
, "/systemd/timers/stamp-", UNIT(t
)->id
);
158 _cleanup_free_
char *h
= NULL
;
160 r
= get_home_dir(&h
);
162 return log_unit_error_errno(UNIT(t
), r
, "Failed to determine home directory: %m");
164 stamp_path
= strjoin(h
, "/.local/share/systemd/timers/stamp-", UNIT(t
)->id
);
171 return free_and_replace(t
->stamp_path
, stamp_path
);
174 static uint64_t timer_get_fixed_delay_hash(Timer
*t
) {
175 static const uint8_t hash_key
[] = {
176 0x51, 0x0a, 0xdb, 0x76, 0x29, 0x51, 0x42, 0xc2,
177 0x80, 0x35, 0xea, 0xe6, 0x8e, 0x3a, 0x37, 0xbd
180 struct siphash state
;
181 sd_id128_t machine_id
;
188 r
= sd_id128_get_machine(&machine_id
);
190 log_unit_debug_errno(UNIT(t
), r
,
191 "Failed to get machine ID for the fixed delay calculation, proceeding with 0: %m");
192 machine_id
= SD_ID128_NULL
;
195 siphash24_init(&state
, hash_key
);
196 siphash24_compress(&machine_id
, sizeof(sd_id128_t
), &state
);
197 siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t
)->manager
), &state
);
198 siphash24_compress(&uid
, sizeof(uid_t
), &state
);
199 siphash24_compress_string(UNIT(t
)->id
, &state
);
201 return siphash24_finalize(&state
);
204 static int timer_load(Unit
*u
) {
209 assert(u
->load_state
== UNIT_STUB
);
211 r
= unit_load_fragment_and_dropin(u
, true);
215 if (u
->load_state
!= UNIT_LOADED
)
218 /* This is a new unit? Then let's add in some extras */
219 r
= timer_add_trigger_dependencies(t
);
223 r
= timer_setup_persistent(t
);
227 r
= timer_add_default_dependencies(t
);
231 return timer_verify(t
);
234 static void timer_dump(Unit
*u
, FILE *f
, const char *prefix
) {
238 trigger
= UNIT_TRIGGER(u
);
241 "%sTimer State: %s\n"
247 "%sRemainAfterElapse: %s\n"
248 "%sFixedRandomDelay: %s\n"
249 "%sOnClockChange: %s\n"
250 "%sOnTimeZoneChange: %s\n",
251 prefix
, timer_state_to_string(t
->state
),
252 prefix
, timer_result_to_string(t
->result
),
253 prefix
, trigger
? trigger
->id
: "n/a",
254 prefix
, yes_no(t
->persistent
),
255 prefix
, yes_no(t
->wake_system
),
256 prefix
, FORMAT_TIMESPAN(t
->accuracy_usec
, 1),
257 prefix
, yes_no(t
->remain_after_elapse
),
258 prefix
, yes_no(t
->fixed_random_delay
),
259 prefix
, yes_no(t
->on_clock_change
),
260 prefix
, yes_no(t
->on_timezone_change
));
262 LIST_FOREACH(value
, v
, t
->values
)
263 if (v
->base
== TIMER_CALENDAR
) {
264 _cleanup_free_
char *p
= NULL
;
266 (void) calendar_spec_to_string(v
->calendar_spec
, &p
);
271 timer_base_to_string(v
->base
),
277 timer_base_to_string(v
->base
),
278 FORMAT_TIMESPAN(v
->value
, 0));
281 static void timer_set_state(Timer
*t
, TimerState state
) {
282 TimerState old_state
;
285 if (t
->state
!= state
)
286 bus_unit_send_pending_change_signal(UNIT(t
), false);
288 old_state
= t
->state
;
291 if (state
!= TIMER_WAITING
) {
292 t
->monotonic_event_source
= sd_event_source_disable_unref(t
->monotonic_event_source
);
293 t
->realtime_event_source
= sd_event_source_disable_unref(t
->realtime_event_source
);
294 t
->next_elapse_monotonic_or_boottime
= USEC_INFINITY
;
295 t
->next_elapse_realtime
= USEC_INFINITY
;
298 if (state
!= old_state
)
299 log_unit_debug(UNIT(t
), "Changed %s -> %s", timer_state_to_string(old_state
), timer_state_to_string(state
));
301 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
304 static void timer_enter_waiting(Timer
*t
, bool time_change
);
306 static int timer_coldplug(Unit
*u
) {
310 assert(t
->state
== TIMER_DEAD
);
312 if (t
->deserialized_state
== t
->state
)
315 if (t
->deserialized_state
== TIMER_WAITING
)
316 timer_enter_waiting(t
, false);
318 timer_set_state(t
, t
->deserialized_state
);
323 static void timer_enter_dead(Timer
*t
, TimerResult f
) {
326 if (t
->result
== TIMER_SUCCESS
)
329 unit_log_result(UNIT(t
), t
->result
== TIMER_SUCCESS
, timer_result_to_string(t
->result
));
330 timer_set_state(t
, t
->result
!= TIMER_SUCCESS
? TIMER_FAILED
: TIMER_DEAD
);
333 static void timer_enter_elapsed(Timer
*t
, bool leave_around
) {
336 /* If a unit is marked with RemainAfterElapse=yes we leave it
337 * around even after it elapsed once, so that starting it
338 * later again does not necessarily mean immediate
339 * retriggering. We unconditionally leave units with
340 * TIMER_UNIT_ACTIVE or TIMER_UNIT_INACTIVE triggers around,
341 * since they might be restarted automatically at any time
344 if (t
->remain_after_elapse
|| leave_around
)
345 timer_set_state(t
, TIMER_ELAPSED
);
347 timer_enter_dead(t
, TIMER_SUCCESS
);
350 static void add_random(Timer
*t
, usec_t
*v
) {
356 if (t
->random_usec
== 0)
358 if (*v
== USEC_INFINITY
)
361 add
= (t
->fixed_random_delay
? timer_get_fixed_delay_hash(t
) : random_u64()) % t
->random_usec
;
363 if (*v
+ add
< *v
) /* overflow */
364 *v
= (usec_t
) -2; /* Highest possible value, that is not USEC_INFINITY */
368 log_unit_debug(UNIT(t
), "Adding %s random time.", FORMAT_TIMESPAN(add
, 0));
371 static void timer_enter_waiting(Timer
*t
, bool time_change
) {
372 bool found_monotonic
= false, found_realtime
= false;
373 bool leave_around
= false;
380 trigger
= UNIT_TRIGGER(UNIT(t
));
382 log_unit_error(UNIT(t
), "Unit to trigger vanished.");
383 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
387 triple_timestamp_get(&ts
);
388 t
->next_elapse_monotonic_or_boottime
= t
->next_elapse_realtime
= 0;
390 LIST_FOREACH(value
, v
, t
->values
) {
394 if (v
->base
== TIMER_CALENDAR
) {
397 /* If we know the last time this was
398 * triggered, schedule the job based relative
399 * to that. If we don't, just start from
400 * the activation time. */
402 if (dual_timestamp_is_set(&t
->last_trigger
))
403 b
= t
->last_trigger
.realtime
;
404 else if (dual_timestamp_is_set(&UNIT(t
)->inactive_exit_timestamp
))
405 b
= UNIT(t
)->inactive_exit_timestamp
.realtime
;
409 r
= calendar_spec_next_usec(v
->calendar_spec
, b
, &v
->next_elapse
);
413 /* To make the delay due to RandomizedDelaySec= work even at boot, if the scheduled
414 * time has already passed, set the time when systemd first started as the scheduled
415 * time. Note that we base this on the monotonic timestamp of the boot, not the
416 * realtime one, since the wallclock might have been off during boot. */
417 rebased
= map_clock_usec(UNIT(t
)->manager
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
].monotonic
,
418 CLOCK_MONOTONIC
, CLOCK_REALTIME
);
419 if (v
->next_elapse
< rebased
)
420 v
->next_elapse
= rebased
;
423 t
->next_elapse_realtime
= v
->next_elapse
;
425 t
->next_elapse_realtime
= MIN(t
->next_elapse_realtime
, v
->next_elapse
);
427 found_realtime
= true;
435 if (state_translation_table
[t
->state
] == UNIT_ACTIVE
)
436 base
= UNIT(t
)->inactive_exit_timestamp
.monotonic
;
442 if (detect_container() <= 0) {
443 /* CLOCK_MONOTONIC equals the uptime on Linux */
447 /* In a container we don't want to include the time the host
448 * was already up when the container started, so count from
449 * our own startup. */
452 base
= UNIT(t
)->manager
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
].monotonic
;
455 case TIMER_UNIT_ACTIVE
:
457 base
= MAX(trigger
->inactive_exit_timestamp
.monotonic
, t
->last_trigger
.monotonic
);
462 case TIMER_UNIT_INACTIVE
:
464 base
= MAX(trigger
->inactive_enter_timestamp
.monotonic
, t
->last_trigger
.monotonic
);
470 assert_not_reached();
473 v
->next_elapse
= usec_add(usec_shift_clock(base
, CLOCK_MONOTONIC
, TIMER_MONOTONIC_CLOCK(t
)), v
->value
);
475 if (dual_timestamp_is_set(&t
->last_trigger
) &&
477 v
->next_elapse
< triple_timestamp_by_clock(&ts
, TIMER_MONOTONIC_CLOCK(t
)) &&
478 IN_SET(v
->base
, TIMER_ACTIVE
, TIMER_BOOT
, TIMER_STARTUP
)) {
479 /* This is a one time trigger, disable it now */
484 if (!found_monotonic
)
485 t
->next_elapse_monotonic_or_boottime
= v
->next_elapse
;
487 t
->next_elapse_monotonic_or_boottime
= MIN(t
->next_elapse_monotonic_or_boottime
, v
->next_elapse
);
489 found_monotonic
= true;
493 if (!found_monotonic
&& !found_realtime
&& !t
->on_timezone_change
&& !t
->on_clock_change
) {
494 log_unit_debug(UNIT(t
), "Timer is elapsed.");
495 timer_enter_elapsed(t
, leave_around
);
499 if (found_monotonic
) {
502 add_random(t
, &t
->next_elapse_monotonic_or_boottime
);
504 left
= usec_sub_unsigned(t
->next_elapse_monotonic_or_boottime
, triple_timestamp_by_clock(&ts
, TIMER_MONOTONIC_CLOCK(t
)));
505 log_unit_debug(UNIT(t
), "Monotonic timer elapses in %s.", FORMAT_TIMESPAN(left
, 0));
507 if (t
->monotonic_event_source
) {
508 r
= sd_event_source_set_time(t
->monotonic_event_source
, t
->next_elapse_monotonic_or_boottime
);
512 r
= sd_event_source_set_enabled(t
->monotonic_event_source
, SD_EVENT_ONESHOT
);
517 r
= sd_event_add_time(
518 UNIT(t
)->manager
->event
,
519 &t
->monotonic_event_source
,
520 t
->wake_system
? CLOCK_BOOTTIME_ALARM
: CLOCK_MONOTONIC
,
521 t
->next_elapse_monotonic_or_boottime
, t
->accuracy_usec
,
526 (void) sd_event_source_set_description(t
->monotonic_event_source
, "timer-monotonic");
529 } else if (t
->monotonic_event_source
) {
531 r
= sd_event_source_set_enabled(t
->monotonic_event_source
, SD_EVENT_OFF
);
536 if (found_realtime
) {
537 add_random(t
, &t
->next_elapse_realtime
);
539 log_unit_debug(UNIT(t
), "Realtime timer elapses at %s.", FORMAT_TIMESTAMP(t
->next_elapse_realtime
));
541 if (t
->realtime_event_source
) {
542 r
= sd_event_source_set_time(t
->realtime_event_source
, t
->next_elapse_realtime
);
546 r
= sd_event_source_set_enabled(t
->realtime_event_source
, SD_EVENT_ONESHOT
);
550 r
= sd_event_add_time(
551 UNIT(t
)->manager
->event
,
552 &t
->realtime_event_source
,
553 t
->wake_system
? CLOCK_REALTIME_ALARM
: CLOCK_REALTIME
,
554 t
->next_elapse_realtime
, t
->accuracy_usec
,
559 (void) sd_event_source_set_description(t
->realtime_event_source
, "timer-realtime");
562 } else if (t
->realtime_event_source
) {
564 r
= sd_event_source_set_enabled(t
->realtime_event_source
, SD_EVENT_OFF
);
569 timer_set_state(t
, TIMER_WAITING
);
573 log_unit_warning_errno(UNIT(t
), r
, "Failed to enter waiting state: %m");
574 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
577 static void timer_enter_running(Timer
*t
) {
578 _cleanup_(activation_details_unrefp
) ActivationDetails
*details
= NULL
;
579 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
586 /* Don't start job if we are supposed to go down */
587 if (unit_stop_pending(UNIT(t
)))
590 trigger
= UNIT_TRIGGER(UNIT(t
));
592 log_unit_error(UNIT(t
), "Unit to trigger vanished.");
593 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
597 details
= activation_details_new(UNIT(t
));
603 r
= manager_add_job(UNIT(t
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, NULL
, &error
, &job
);
607 dual_timestamp_get(&t
->last_trigger
);
608 ACTIVATION_DETAILS_TIMER(details
)->last_trigger
= t
->last_trigger
;
610 job_set_activation_details(job
, details
);
613 touch_file(t
->stamp_path
, true, t
->last_trigger
.realtime
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
615 timer_set_state(t
, TIMER_RUNNING
);
619 log_unit_warning(UNIT(t
), "Failed to queue unit startup job: %s", bus_error_message(&error
, r
));
620 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
623 static int timer_start(Unit
*u
) {
628 assert(IN_SET(t
->state
, TIMER_DEAD
, TIMER_FAILED
));
630 r
= unit_test_trigger_loaded(u
);
634 r
= unit_acquire_invocation_id(u
);
638 t
->last_trigger
= DUAL_TIMESTAMP_NULL
;
640 /* Reenable all timers that depend on unit activation time */
641 LIST_FOREACH(value
, v
, t
->values
)
642 if (v
->base
== TIMER_ACTIVE
)
648 if (stat(t
->stamp_path
, &st
) >= 0) {
651 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
652 * something is wrong with the system clock. */
654 ft
= timespec_load(&st
.st_mtim
);
655 if (ft
< now(CLOCK_REALTIME
))
656 t
->last_trigger
.realtime
= ft
;
658 log_unit_warning(u
, "Not using persistent file timestamp %s as it is in the future.",
659 FORMAT_TIMESTAMP(ft
));
661 } else if (errno
== ENOENT
)
662 /* The timer has never run before, make sure a stamp file exists. */
663 (void) touch_file(t
->stamp_path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
666 t
->result
= TIMER_SUCCESS
;
667 timer_enter_waiting(t
, false);
671 static int timer_stop(Unit
*u
) {
675 assert(IN_SET(t
->state
, TIMER_WAITING
, TIMER_RUNNING
, TIMER_ELAPSED
));
677 timer_enter_dead(t
, TIMER_SUCCESS
);
681 static int timer_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
688 (void) serialize_item(f
, "state", timer_state_to_string(t
->state
));
689 (void) serialize_item(f
, "result", timer_result_to_string(t
->result
));
691 if (dual_timestamp_is_set(&t
->last_trigger
))
692 (void) serialize_usec(f
, "last-trigger-realtime", t
->last_trigger
.realtime
);
694 if (t
->last_trigger
.monotonic
> 0)
695 (void) serialize_usec(f
, "last-trigger-monotonic", t
->last_trigger
.monotonic
);
700 static int timer_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
708 if (streq(key
, "state")) {
711 state
= timer_state_from_string(value
);
713 log_unit_debug(u
, "Failed to parse state value: %s", value
);
715 t
->deserialized_state
= state
;
717 } else if (streq(key
, "result")) {
720 f
= timer_result_from_string(value
);
722 log_unit_debug(u
, "Failed to parse result value: %s", value
);
723 else if (f
!= TIMER_SUCCESS
)
726 } else if (streq(key
, "last-trigger-realtime"))
727 (void) deserialize_usec(value
, &t
->last_trigger
.realtime
);
728 else if (streq(key
, "last-trigger-monotonic"))
729 (void) deserialize_usec(value
, &t
->last_trigger
.monotonic
);
731 log_unit_debug(u
, "Unknown serialization key: %s", key
);
736 static UnitActiveState
timer_active_state(Unit
*u
) {
739 return state_translation_table
[TIMER(u
)->state
];
742 static const char *timer_sub_state_to_string(Unit
*u
) {
745 return timer_state_to_string(TIMER(u
)->state
);
748 static int timer_dispatch(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
749 Timer
*t
= TIMER(userdata
);
753 if (t
->state
!= TIMER_WAITING
)
756 log_unit_debug(UNIT(t
), "Timer elapsed.");
757 timer_enter_running(t
);
761 static void timer_trigger_notify(Unit
*u
, Unit
*other
) {
767 /* Filter out invocations with bogus state */
768 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
770 /* Reenable all timers that depend on unit state */
771 LIST_FOREACH(value
, v
, t
->values
)
772 if (IN_SET(v
->base
, TIMER_UNIT_ACTIVE
, TIMER_UNIT_INACTIVE
))
780 /* Recalculate sleep time */
781 timer_enter_waiting(t
, false);
786 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other
))) {
787 log_unit_debug(UNIT(t
), "Got notified about unit deactivation.");
788 timer_enter_waiting(t
, false);
797 assert_not_reached();
801 static void timer_reset_failed(Unit
*u
) {
806 if (t
->state
== TIMER_FAILED
)
807 timer_set_state(t
, TIMER_DEAD
);
809 t
->result
= TIMER_SUCCESS
;
812 static void timer_time_change(Unit
*u
) {
818 if (t
->state
!= TIMER_WAITING
)
821 /* If we appear to have triggered in the future, the system clock must
822 * have been set backwards. So let's rewind our own clock and allow
823 * the future triggers to happen again :). Exactly the same as when
824 * you start a timer unit with Persistent=yes. */
825 ts
= now(CLOCK_REALTIME
);
826 if (t
->last_trigger
.realtime
> ts
)
827 t
->last_trigger
.realtime
= ts
;
829 if (t
->on_clock_change
) {
830 log_unit_debug(u
, "Time change, triggering activation.");
831 timer_enter_running(t
);
833 log_unit_debug(u
, "Time change, recalculating next elapse.");
834 timer_enter_waiting(t
, true);
838 static void timer_timezone_change(Unit
*u
) {
843 if (t
->state
!= TIMER_WAITING
)
846 if (t
->on_timezone_change
) {
847 log_unit_debug(u
, "Timezone change, triggering activation.");
848 timer_enter_running(t
);
850 log_unit_debug(u
, "Timezone change, recalculating next elapse.");
851 timer_enter_waiting(t
, false);
855 static int timer_clean(Unit
*u
, ExecCleanMask mask
) {
862 if (t
->state
!= TIMER_DEAD
)
865 if (mask
!= EXEC_CLEAN_STATE
)
868 r
= timer_setup_persistent(t
);
875 if (unlink(t
->stamp_path
) && errno
!= ENOENT
)
876 return log_unit_error_errno(u
, errno
, "Failed to clean stamp file of timer: %m");
881 static int timer_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
887 *ret
= t
->persistent
? EXEC_CLEAN_STATE
: 0;
891 static int timer_can_start(Unit
*u
) {
897 r
= unit_test_start_limit(u
);
899 timer_enter_dead(t
, TIMER_FAILURE_START_LIMIT_HIT
);
906 static void activation_details_timer_serialize(ActivationDetails
*details
, FILE *f
) {
907 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(details
);
913 (void) serialize_dual_timestamp(f
, "activation-details-timer-last-trigger", &t
->last_trigger
);
916 static int activation_details_timer_deserialize(const char *key
, const char *value
, ActivationDetails
**details
) {
922 if (!details
|| !*details
)
925 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(*details
);
929 if (!streq(key
, "activation-details-timer-last-trigger"))
932 r
= deserialize_dual_timestamp(value
, &t
->last_trigger
);
939 static int activation_details_timer_append_env(ActivationDetails
*details
, char ***strv
) {
940 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(details
);
947 if (!dual_timestamp_is_set(&t
->last_trigger
))
950 r
= strv_extendf(strv
, "TRIGGER_TIMER_REALTIME_USEC=" USEC_FMT
, t
->last_trigger
.realtime
);
954 r
= strv_extendf(strv
, "TRIGGER_TIMER_MONOTONIC_USEC=" USEC_FMT
, t
->last_trigger
.monotonic
);
958 return 2; /* Return the number of variables added to the env block */
961 static int activation_details_timer_append_pair(ActivationDetails
*details
, char ***strv
) {
962 ActivationDetailsTimer
*t
= ACTIVATION_DETAILS_TIMER(details
);
969 if (!dual_timestamp_is_set(&t
->last_trigger
))
972 r
= strv_extend(strv
, "trigger_timer_realtime_usec");
976 r
= strv_extendf(strv
, USEC_FMT
, t
->last_trigger
.realtime
);
980 r
= strv_extend(strv
, "trigger_timer_monotonic_usec");
984 r
= strv_extendf(strv
, USEC_FMT
, t
->last_trigger
.monotonic
);
988 return 2; /* Return the number of pairs added to the env block */
991 static const char* const timer_base_table
[_TIMER_BASE_MAX
] = {
992 [TIMER_ACTIVE
] = "OnActiveSec",
993 [TIMER_BOOT
] = "OnBootSec",
994 [TIMER_STARTUP
] = "OnStartupSec",
995 [TIMER_UNIT_ACTIVE
] = "OnUnitActiveSec",
996 [TIMER_UNIT_INACTIVE
] = "OnUnitInactiveSec",
997 [TIMER_CALENDAR
] = "OnCalendar"
1000 DEFINE_STRING_TABLE_LOOKUP(timer_base
, TimerBase
);
1002 static const char* const timer_result_table
[_TIMER_RESULT_MAX
] = {
1003 [TIMER_SUCCESS
] = "success",
1004 [TIMER_FAILURE_RESOURCES
] = "resources",
1005 [TIMER_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1008 DEFINE_STRING_TABLE_LOOKUP(timer_result
, TimerResult
);
1010 const UnitVTable timer_vtable
= {
1011 .object_size
= sizeof(Timer
),
1017 .private_section
= "Timer",
1019 .can_transient
= true,
1021 .can_trigger
= true,
1027 .coldplug
= timer_coldplug
,
1031 .start
= timer_start
,
1034 .clean
= timer_clean
,
1035 .can_clean
= timer_can_clean
,
1037 .serialize
= timer_serialize
,
1038 .deserialize_item
= timer_deserialize_item
,
1040 .active_state
= timer_active_state
,
1041 .sub_state_to_string
= timer_sub_state_to_string
,
1043 .trigger_notify
= timer_trigger_notify
,
1045 .reset_failed
= timer_reset_failed
,
1046 .time_change
= timer_time_change
,
1047 .timezone_change
= timer_timezone_change
,
1049 .bus_set_property
= bus_timer_set_property
,
1051 .can_start
= timer_can_start
,
1054 const ActivationDetailsVTable activation_details_timer_vtable
= {
1055 .object_size
= sizeof(ActivationDetailsTimer
),
1057 .serialize
= activation_details_timer_serialize
,
1058 .deserialize
= activation_details_timer_deserialize
,
1059 .append_env
= activation_details_timer_append_env
,
1060 .append_pair
= activation_details_timer_append_pair
,