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_unref(t
->monotonic_event_source
);
69 t
->realtime_event_source
= sd_event_source_unref(t
->realtime_event_source
);
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
) {
90 if (!UNIT(t
)->default_dependencies
)
93 r
= unit_add_dependency_by_name(UNIT(t
), UNIT_BEFORE
, SPECIAL_TIMERS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
97 if (MANAGER_IS_SYSTEM(UNIT(t
)->manager
)) {
98 r
= unit_add_two_dependencies_by_name(UNIT(t
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
102 LIST_FOREACH(value
, v
, t
->values
)
103 if (v
->base
== TIMER_CALENDAR
) {
104 r
= unit_add_dependency_by_name(UNIT(t
), UNIT_AFTER
, SPECIAL_TIME_SYNC_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
111 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
114 static int timer_add_trigger_dependencies(Timer
*t
) {
120 if (!hashmap_isempty(UNIT(t
)->dependencies
[UNIT_TRIGGERS
]))
123 r
= unit_load_related_unit(UNIT(t
), ".service", &x
);
127 return unit_add_two_dependencies(UNIT(t
), UNIT_BEFORE
, UNIT_TRIGGERS
, x
, true, UNIT_DEPENDENCY_IMPLICIT
);
130 static int timer_setup_persistent(Timer
*t
) {
138 if (MANAGER_IS_SYSTEM(UNIT(t
)->manager
)) {
140 r
= unit_require_mounts_for(UNIT(t
), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE
);
144 t
->stamp_path
= strjoin("/var/lib/systemd/timers/stamp-", UNIT(t
)->id
);
148 e
= getenv("XDG_DATA_HOME");
150 t
->stamp_path
= strjoin(e
, "/systemd/timers/stamp-", UNIT(t
)->id
);
153 _cleanup_free_
char *h
= NULL
;
155 r
= get_home_dir(&h
);
157 return log_unit_error_errno(UNIT(t
), r
, "Failed to determine home directory: %m");
159 t
->stamp_path
= strjoin(h
, "/.local/share/systemd/timers/stamp-", UNIT(t
)->id
);
169 static uint64_t timer_get_fixed_delay_hash(Timer
*t
) {
170 static const uint8_t hash_key
[] = {
171 0x51, 0x0a, 0xdb, 0x76, 0x29, 0x51, 0x42, 0xc2,
172 0x80, 0x35, 0xea, 0xe6, 0x8e, 0x3a, 0x37, 0xbd
175 struct siphash state
;
176 sd_id128_t machine_id
;
183 r
= sd_id128_get_machine(&machine_id
);
185 log_unit_debug_errno(UNIT(t
), r
,
186 "Failed to get machine ID for the fixed delay calculation, proceeding with 0: %m");
187 machine_id
= SD_ID128_NULL
;
190 siphash24_init(&state
, hash_key
);
191 siphash24_compress(&machine_id
, sizeof(sd_id128_t
), &state
);
192 siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t
)->manager
), &state
);
193 siphash24_compress(&uid
, sizeof(uid_t
), &state
);
194 siphash24_compress_string(UNIT(t
)->id
, &state
);
196 return siphash24_finalize(&state
);
199 static int timer_load(Unit
*u
) {
204 assert(u
->load_state
== UNIT_STUB
);
206 r
= unit_load_fragment_and_dropin(u
, true);
210 if (u
->load_state
!= UNIT_LOADED
)
213 /* This is a new unit? Then let's add in some extras */
214 r
= timer_add_trigger_dependencies(t
);
218 r
= timer_setup_persistent(t
);
222 r
= timer_add_default_dependencies(t
);
226 return timer_verify(t
);
229 static void timer_dump(Unit
*u
, FILE *f
, const char *prefix
) {
230 char buf
[FORMAT_TIMESPAN_MAX
];
235 trigger
= UNIT_TRIGGER(u
);
238 "%sTimer State: %s\n"
244 "%sRemainAfterElapse: %s\n"
245 "%sFixedRandomDelay: %s\n"
246 "%sOnClockChange: %s\n"
247 "%sOnTimeZoneChange: %s\n",
248 prefix
, timer_state_to_string(t
->state
),
249 prefix
, timer_result_to_string(t
->result
),
250 prefix
, trigger
? trigger
->id
: "n/a",
251 prefix
, yes_no(t
->persistent
),
252 prefix
, yes_no(t
->wake_system
),
253 prefix
, format_timespan(buf
, sizeof(buf
), t
->accuracy_usec
, 1),
254 prefix
, yes_no(t
->remain_after_elapse
),
255 prefix
, yes_no(t
->fixed_random_delay
),
256 prefix
, yes_no(t
->on_clock_change
),
257 prefix
, yes_no(t
->on_timezone_change
));
259 LIST_FOREACH(value
, v
, t
->values
)
260 if (v
->base
== TIMER_CALENDAR
) {
261 _cleanup_free_
char *p
= NULL
;
263 (void) calendar_spec_to_string(v
->calendar_spec
, &p
);
268 timer_base_to_string(v
->base
),
271 char timespan1
[FORMAT_TIMESPAN_MAX
];
276 timer_base_to_string(v
->base
),
277 format_timespan(timespan1
, sizeof(timespan1
), 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_unref(t
->monotonic_event_source
);
293 t
->realtime_event_source
= sd_event_source_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
], 0);
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
) {
351 char s
[FORMAT_TIMESPAN_MAX
];
357 if (t
->random_usec
== 0)
359 if (*v
== USEC_INFINITY
)
362 add
= (t
->fixed_random_delay
? timer_get_fixed_delay_hash(t
) : random_u64()) % t
->random_usec
;
364 if (*v
+ add
< *v
) /* overflow */
365 *v
= (usec_t
) -2; /* Highest possible value, that is not USEC_INFINITY */
369 log_unit_debug(UNIT(t
), "Adding %s random time.", format_timespan(s
, sizeof(s
), add
, 0));
372 static void timer_enter_waiting(Timer
*t
, bool time_change
) {
373 bool found_monotonic
= false, found_realtime
= false;
374 bool leave_around
= false;
382 trigger
= UNIT_TRIGGER(UNIT(t
));
384 log_unit_error(UNIT(t
), "Unit to trigger vanished.");
385 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
389 triple_timestamp_get(&ts
);
390 t
->next_elapse_monotonic_or_boottime
= t
->next_elapse_realtime
= 0;
392 LIST_FOREACH(value
, v
, t
->values
) {
396 if (v
->base
== TIMER_CALENDAR
) {
399 /* If we know the last time this was
400 * triggered, schedule the job based relative
401 * to that. If we don't, just start from
402 * the activation time. */
404 if (t
->last_trigger
.realtime
> 0)
405 b
= t
->last_trigger
.realtime
;
407 if (state_translation_table
[t
->state
] == UNIT_ACTIVE
)
408 b
= UNIT(t
)->inactive_exit_timestamp
.realtime
;
413 r
= calendar_spec_next_usec(v
->calendar_spec
, b
, &v
->next_elapse
);
417 /* To make the delay due to RandomizedDelaySec= work even at boot, if the scheduled
418 * time has already passed, set the time when systemd first started as the scheduled
419 * time. Note that we base this on the monotonic timestamp of the boot, not the
420 * realtime one, since the wallclock might have been off during boot. */
421 rebased
= map_clock_usec(UNIT(t
)->manager
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
].monotonic
,
422 CLOCK_MONOTONIC
, CLOCK_REALTIME
);
423 if (v
->next_elapse
< rebased
)
424 v
->next_elapse
= rebased
;
427 t
->next_elapse_realtime
= v
->next_elapse
;
429 t
->next_elapse_realtime
= MIN(t
->next_elapse_realtime
, v
->next_elapse
);
431 found_realtime
= true;
439 if (state_translation_table
[t
->state
] == UNIT_ACTIVE
)
440 base
= UNIT(t
)->inactive_exit_timestamp
.monotonic
;
446 if (detect_container() <= 0) {
447 /* CLOCK_MONOTONIC equals the uptime on Linux */
451 /* In a container we don't want to include the time the host
452 * was already up when the container started, so count from
453 * our own startup. */
456 base
= UNIT(t
)->manager
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
].monotonic
;
459 case TIMER_UNIT_ACTIVE
:
461 base
= MAX(trigger
->inactive_exit_timestamp
.monotonic
, t
->last_trigger
.monotonic
);
466 case TIMER_UNIT_INACTIVE
:
468 base
= MAX(trigger
->inactive_enter_timestamp
.monotonic
, t
->last_trigger
.monotonic
);
474 assert_not_reached("Unknown timer base");
477 v
->next_elapse
= usec_add(usec_shift_clock(base
, CLOCK_MONOTONIC
, TIMER_MONOTONIC_CLOCK(t
)), v
->value
);
479 if (dual_timestamp_is_set(&t
->last_trigger
) &&
481 v
->next_elapse
< triple_timestamp_by_clock(&ts
, TIMER_MONOTONIC_CLOCK(t
)) &&
482 IN_SET(v
->base
, TIMER_ACTIVE
, TIMER_BOOT
, TIMER_STARTUP
)) {
483 /* This is a one time trigger, disable it now */
488 if (!found_monotonic
)
489 t
->next_elapse_monotonic_or_boottime
= v
->next_elapse
;
491 t
->next_elapse_monotonic_or_boottime
= MIN(t
->next_elapse_monotonic_or_boottime
, v
->next_elapse
);
493 found_monotonic
= true;
497 if (!found_monotonic
&& !found_realtime
&& !t
->on_timezone_change
&& !t
->on_clock_change
) {
498 log_unit_debug(UNIT(t
), "Timer is elapsed.");
499 timer_enter_elapsed(t
, leave_around
);
503 if (found_monotonic
) {
504 char buf
[FORMAT_TIMESPAN_MAX
];
507 add_random(t
, &t
->next_elapse_monotonic_or_boottime
);
509 left
= usec_sub_unsigned(t
->next_elapse_monotonic_or_boottime
, triple_timestamp_by_clock(&ts
, TIMER_MONOTONIC_CLOCK(t
)));
510 log_unit_debug(UNIT(t
), "Monotonic timer elapses in %s.", format_timespan(buf
, sizeof(buf
), left
, 0));
512 if (t
->monotonic_event_source
) {
513 r
= sd_event_source_set_time(t
->monotonic_event_source
, t
->next_elapse_monotonic_or_boottime
);
517 r
= sd_event_source_set_enabled(t
->monotonic_event_source
, SD_EVENT_ONESHOT
);
522 r
= sd_event_add_time(
523 UNIT(t
)->manager
->event
,
524 &t
->monotonic_event_source
,
525 t
->wake_system
? CLOCK_BOOTTIME_ALARM
: CLOCK_MONOTONIC
,
526 t
->next_elapse_monotonic_or_boottime
, t
->accuracy_usec
,
531 (void) sd_event_source_set_description(t
->monotonic_event_source
, "timer-monotonic");
534 } else if (t
->monotonic_event_source
) {
536 r
= sd_event_source_set_enabled(t
->monotonic_event_source
, SD_EVENT_OFF
);
541 if (found_realtime
) {
542 char buf
[FORMAT_TIMESTAMP_MAX
];
544 add_random(t
, &t
->next_elapse_realtime
);
546 log_unit_debug(UNIT(t
), "Realtime timer elapses at %s.", format_timestamp(buf
, sizeof(buf
), t
->next_elapse_realtime
));
548 if (t
->realtime_event_source
) {
549 r
= sd_event_source_set_time(t
->realtime_event_source
, t
->next_elapse_realtime
);
553 r
= sd_event_source_set_enabled(t
->realtime_event_source
, SD_EVENT_ONESHOT
);
557 r
= sd_event_add_time(
558 UNIT(t
)->manager
->event
,
559 &t
->realtime_event_source
,
560 t
->wake_system
? CLOCK_REALTIME_ALARM
: CLOCK_REALTIME
,
561 t
->next_elapse_realtime
, t
->accuracy_usec
,
566 (void) sd_event_source_set_description(t
->realtime_event_source
, "timer-realtime");
569 } else if (t
->realtime_event_source
) {
571 r
= sd_event_source_set_enabled(t
->realtime_event_source
, SD_EVENT_OFF
);
576 timer_set_state(t
, TIMER_WAITING
);
580 log_unit_warning_errno(UNIT(t
), r
, "Failed to enter waiting state: %m");
581 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
584 static void timer_enter_running(Timer
*t
) {
585 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
591 /* Don't start job if we are supposed to go down */
592 if (unit_stop_pending(UNIT(t
)))
595 trigger
= UNIT_TRIGGER(UNIT(t
));
597 log_unit_error(UNIT(t
), "Unit to trigger vanished.");
598 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
602 r
= manager_add_job(UNIT(t
)->manager
, JOB_START
, trigger
, JOB_REPLACE
, NULL
, &error
, NULL
);
606 dual_timestamp_get(&t
->last_trigger
);
609 touch_file(t
->stamp_path
, true, t
->last_trigger
.realtime
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
611 timer_set_state(t
, TIMER_RUNNING
);
615 log_unit_warning(UNIT(t
), "Failed to queue unit startup job: %s", bus_error_message(&error
, r
));
616 timer_enter_dead(t
, TIMER_FAILURE_RESOURCES
);
619 static int timer_start(Unit
*u
) {
625 assert(IN_SET(t
->state
, TIMER_DEAD
, TIMER_FAILED
));
627 r
= unit_test_trigger_loaded(u
);
631 r
= unit_test_start_limit(u
);
633 timer_enter_dead(t
, TIMER_FAILURE_START_LIMIT_HIT
);
637 r
= unit_acquire_invocation_id(u
);
641 t
->last_trigger
= DUAL_TIMESTAMP_NULL
;
643 /* Reenable all timers that depend on unit activation time */
644 LIST_FOREACH(value
, v
, t
->values
)
645 if (v
->base
== TIMER_ACTIVE
)
651 if (stat(t
->stamp_path
, &st
) >= 0) {
654 /* Load the file timestamp, but only if it is actually in the past. If it is in the future,
655 * something is wrong with the system clock. */
657 ft
= timespec_load(&st
.st_mtim
);
658 if (ft
< now(CLOCK_REALTIME
))
659 t
->last_trigger
.realtime
= ft
;
661 char z
[FORMAT_TIMESTAMP_MAX
];
663 log_unit_warning(u
, "Not using persistent file timestamp %s as it is in the future.",
664 format_timestamp(z
, sizeof(z
), ft
));
667 } else if (errno
== ENOENT
)
668 /* The timer has never run before, make sure a stamp file exists. */
669 (void) touch_file(t
->stamp_path
, true, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
672 t
->result
= TIMER_SUCCESS
;
673 timer_enter_waiting(t
, false);
677 static int timer_stop(Unit
*u
) {
681 assert(IN_SET(t
->state
, TIMER_WAITING
, TIMER_RUNNING
, TIMER_ELAPSED
));
683 timer_enter_dead(t
, TIMER_SUCCESS
);
687 static int timer_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
694 (void) serialize_item(f
, "state", timer_state_to_string(t
->state
));
695 (void) serialize_item(f
, "result", timer_result_to_string(t
->result
));
697 if (t
->last_trigger
.realtime
> 0)
698 (void) serialize_usec(f
, "last-trigger-realtime", t
->last_trigger
.realtime
);
700 if (t
->last_trigger
.monotonic
> 0)
701 (void) serialize_usec(f
, "last-trigger-monotonic", t
->last_trigger
.monotonic
);
706 static int timer_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
714 if (streq(key
, "state")) {
717 state
= timer_state_from_string(value
);
719 log_unit_debug(u
, "Failed to parse state value: %s", value
);
721 t
->deserialized_state
= state
;
723 } else if (streq(key
, "result")) {
726 f
= timer_result_from_string(value
);
728 log_unit_debug(u
, "Failed to parse result value: %s", value
);
729 else if (f
!= TIMER_SUCCESS
)
732 } else if (streq(key
, "last-trigger-realtime"))
733 (void) deserialize_usec(value
, &t
->last_trigger
.realtime
);
734 else if (streq(key
, "last-trigger-monotonic"))
735 (void) deserialize_usec(value
, &t
->last_trigger
.monotonic
);
737 log_unit_debug(u
, "Unknown serialization key: %s", key
);
742 _pure_
static UnitActiveState
timer_active_state(Unit
*u
) {
745 return state_translation_table
[TIMER(u
)->state
];
748 _pure_
static const char *timer_sub_state_to_string(Unit
*u
) {
751 return timer_state_to_string(TIMER(u
)->state
);
754 static int timer_dispatch(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
755 Timer
*t
= TIMER(userdata
);
759 if (t
->state
!= TIMER_WAITING
)
762 log_unit_debug(UNIT(t
), "Timer elapsed.");
763 timer_enter_running(t
);
767 static void timer_trigger_notify(Unit
*u
, Unit
*other
) {
774 /* Filter out invocations with bogus state */
775 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
777 /* Reenable all timers that depend on unit state */
778 LIST_FOREACH(value
, v
, t
->values
)
779 if (IN_SET(v
->base
, TIMER_UNIT_ACTIVE
, TIMER_UNIT_INACTIVE
))
787 /* Recalculate sleep time */
788 timer_enter_waiting(t
, false);
793 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other
))) {
794 log_unit_debug(UNIT(t
), "Got notified about unit deactivation.");
795 timer_enter_waiting(t
, false);
804 assert_not_reached("Unknown timer state");
808 static void timer_reset_failed(Unit
*u
) {
813 if (t
->state
== TIMER_FAILED
)
814 timer_set_state(t
, TIMER_DEAD
);
816 t
->result
= TIMER_SUCCESS
;
819 static void timer_time_change(Unit
*u
) {
825 if (t
->state
!= TIMER_WAITING
)
828 /* If we appear to have triggered in the future, the system clock must
829 * have been set backwards. So let's rewind our own clock and allow
830 * the future trigger(s) to happen again :). Exactly the same as when
831 * you start a timer unit with Persistent=yes. */
832 ts
= now(CLOCK_REALTIME
);
833 if (t
->last_trigger
.realtime
> ts
)
834 t
->last_trigger
.realtime
= ts
;
836 if (t
->on_clock_change
) {
837 log_unit_debug(u
, "Time change, triggering activation.");
838 timer_enter_running(t
);
840 log_unit_debug(u
, "Time change, recalculating next elapse.");
841 timer_enter_waiting(t
, true);
845 static void timer_timezone_change(Unit
*u
) {
850 if (t
->state
!= TIMER_WAITING
)
853 if (t
->on_timezone_change
) {
854 log_unit_debug(u
, "Timezone change, triggering activation.");
855 timer_enter_running(t
);
857 log_unit_debug(u
, "Timezone change, recalculating next elapse.");
858 timer_enter_waiting(t
, false);
862 static int timer_clean(Unit
*u
, ExecCleanMask mask
) {
869 if (t
->state
!= TIMER_DEAD
)
872 if (!IN_SET(mask
, EXEC_CLEAN_STATE
))
875 r
= timer_setup_persistent(t
);
882 if (unlink(t
->stamp_path
) && errno
!= ENOENT
)
883 return log_unit_error_errno(u
, errno
, "Failed to clean stamp file of timer: %m");
888 static int timer_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
893 *ret
= t
->persistent
? EXEC_CLEAN_STATE
: 0;
897 static const char* const timer_base_table
[_TIMER_BASE_MAX
] = {
898 [TIMER_ACTIVE
] = "OnActiveSec",
899 [TIMER_BOOT
] = "OnBootSec",
900 [TIMER_STARTUP
] = "OnStartupSec",
901 [TIMER_UNIT_ACTIVE
] = "OnUnitActiveSec",
902 [TIMER_UNIT_INACTIVE
] = "OnUnitInactiveSec",
903 [TIMER_CALENDAR
] = "OnCalendar"
906 DEFINE_STRING_TABLE_LOOKUP(timer_base
, TimerBase
);
908 static const char* const timer_result_table
[_TIMER_RESULT_MAX
] = {
909 [TIMER_SUCCESS
] = "success",
910 [TIMER_FAILURE_RESOURCES
] = "resources",
911 [TIMER_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
914 DEFINE_STRING_TABLE_LOOKUP(timer_result
, TimerResult
);
916 const UnitVTable timer_vtable
= {
917 .object_size
= sizeof(Timer
),
923 .private_section
= "Timer",
925 .can_transient
= true,
933 .coldplug
= timer_coldplug
,
937 .start
= timer_start
,
940 .clean
= timer_clean
,
941 .can_clean
= timer_can_clean
,
943 .serialize
= timer_serialize
,
944 .deserialize_item
= timer_deserialize_item
,
946 .active_state
= timer_active_state
,
947 .sub_state_to_string
= timer_sub_state_to_string
,
949 .trigger_notify
= timer_trigger_notify
,
951 .reset_failed
= timer_reset_failed
,
952 .time_change
= timer_time_change
,
953 .timezone_change
= timer_timezone_change
,
955 .bus_set_property
= bus_timer_set_property
,