]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
ca78ad1d ZJS |
3 | #include <sys/stat.h> |
4 | #include <sys/types.h> | |
5 | #include <unistd.h> | |
6 | ||
d46de8a1 LP |
7 | #include <errno.h> |
8 | ||
b5efdb8a | 9 | #include "alloc-util.h" |
07630cea LP |
10 | #include "bus-error.h" |
11 | #include "bus-util.h" | |
871d7de4 | 12 | #include "dbus-timer.h" |
6fcbec6f | 13 | #include "dbus-unit.h" |
f4f15635 | 14 | #include "fs-util.h" |
6bedfcbb | 15 | #include "parse-util.h" |
744c7693 | 16 | #include "random-util.h" |
d68c645b | 17 | #include "serialize.h" |
a40eb732 | 18 | #include "special.h" |
8b43440b | 19 | #include "string-table.h" |
07630cea | 20 | #include "string-util.h" |
b1d4f8e1 | 21 | #include "timer.h" |
07630cea LP |
22 | #include "unit-name.h" |
23 | #include "unit.h" | |
b1d4f8e1 | 24 | #include "user-util.h" |
c1d9ba99 | 25 | #include "virt.h" |
871d7de4 LP |
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, | |
fdf20a31 | 32 | [TIMER_FAILED] = UNIT_FAILED |
871d7de4 LP |
33 | }; |
34 | ||
718db961 LP |
35 | static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata); |
36 | ||
871d7de4 LP |
37 | static void timer_init(Unit *u) { |
38 | Timer *t = TIMER(u); | |
39 | ||
40 | assert(u); | |
ac155bb8 | 41 | assert(u->load_state == UNIT_STUB); |
871d7de4 | 42 | |
3a43da28 KS |
43 | t->next_elapse_monotonic_or_boottime = USEC_INFINITY; |
44 | t->next_elapse_realtime = USEC_INFINITY; | |
c9e120e0 | 45 | t->accuracy_usec = u->manager->defaults.timer_accuracy_usec; |
3e0c30ac | 46 | t->remain_after_elapse = true; |
871d7de4 | 47 | } |
5cb5a6ff | 48 | |
74051b9b | 49 | void timer_free_values(Timer *t) { |
871d7de4 LP |
50 | TimerValue *v; |
51 | ||
52 | assert(t); | |
53 | ||
52e3671b | 54 | while ((v = LIST_POP(value, t->values))) { |
3e044c49 | 55 | calendar_spec_free(v->calendar_spec); |
871d7de4 LP |
56 | free(v); |
57 | } | |
74051b9b LP |
58 | } |
59 | ||
60 | static void timer_done(Unit *u) { | |
61 | Timer *t = TIMER(u); | |
62 | ||
63 | assert(t); | |
64 | ||
65 | timer_free_values(t); | |
871d7de4 | 66 | |
5dcadb4c ZJS |
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); | |
06642d17 | 69 | |
756491af | 70 | t->stamp_path = mfree(t->stamp_path); |
871d7de4 LP |
71 | } |
72 | ||
73 | static int timer_verify(Timer *t) { | |
74 | assert(t); | |
75193d41 | 75 | assert(UNIT(t)->load_state == UNIT_LOADED); |
871d7de4 | 76 | |
d85ff944 YW |
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."); | |
871d7de4 LP |
79 | |
80 | return 0; | |
81 | } | |
82 | ||
6c155fe3 LP |
83 | static int timer_add_default_dependencies(Timer *t) { |
84 | int r; | |
85 | ||
86 | assert(t); | |
87 | ||
4c9ea260 LP |
88 | if (!UNIT(t)->default_dependencies) |
89 | return 0; | |
90 | ||
35d8c19a | 91 | r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_TIMERS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 LP |
92 | if (r < 0) |
93 | return r; | |
2a77d31d | 94 | |
463d0d15 | 95 | if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) { |
3835b9aa | 96 | r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
36697dc0 | 97 | if (r < 0) |
6c155fe3 | 98 | return r; |
19f8d037 | 99 | |
fe934b42 | 100 | LIST_FOREACH(value, v, t->values) { |
fe934b42 LP |
101 | if (v->base != TIMER_CALENDAR) |
102 | continue; | |
103 | ||
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); | |
19f8d037 TGR |
106 | if (r < 0) |
107 | return r; | |
19f8d037 | 108 | } |
fe934b42 LP |
109 | |
110 | break; | |
111 | } | |
2a77d31d | 112 | } |
6c155fe3 | 113 | |
3835b9aa | 114 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
eef85c4a LP |
115 | } |
116 | ||
117 | static int timer_add_trigger_dependencies(Timer *t) { | |
118 | Unit *x; | |
119 | int r; | |
120 | ||
121 | assert(t); | |
122 | ||
bc32241e | 123 | if (UNIT_TRIGGER(UNIT(t))) |
eef85c4a LP |
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); | |
6c155fe3 LP |
131 | } |
132 | ||
06642d17 | 133 | static int timer_setup_persistent(Timer *t) { |
d3ab7b80 | 134 | _cleanup_free_ char *stamp_path = NULL; |
06642d17 LP |
135 | int r; |
136 | ||
137 | assert(t); | |
138 | ||
139 | if (!t->persistent) | |
140 | return 0; | |
141 | ||
463d0d15 | 142 | if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) { |
06642d17 | 143 | |
9e615fa3 | 144 | r = unit_add_mounts_for(UNIT(t), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE, UNIT_MOUNT_REQUIRES); |
06642d17 LP |
145 | if (r < 0) |
146 | return r; | |
147 | ||
d3ab7b80 | 148 | stamp_path = strjoin("/var/lib/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
149 | } else { |
150 | const char *e; | |
151 | ||
152 | e = getenv("XDG_DATA_HOME"); | |
153 | if (e) | |
d3ab7b80 | 154 | stamp_path = strjoin(e, "/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
155 | else { |
156 | ||
157 | _cleanup_free_ char *h = NULL; | |
158 | ||
159 | r = get_home_dir(&h); | |
23bbb0de | 160 | if (r < 0) |
f2341e0a | 161 | return log_unit_error_errno(UNIT(t), r, "Failed to determine home directory: %m"); |
06642d17 | 162 | |
d3ab7b80 | 163 | stamp_path = strjoin(h, "/.local/share/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
164 | } |
165 | } | |
166 | ||
d3ab7b80 | 167 | if (!stamp_path) |
06642d17 LP |
168 | return log_oom(); |
169 | ||
d3ab7b80 | 170 | return free_and_replace(t->stamp_path, stamp_path); |
06642d17 LP |
171 | } |
172 | ||
acf24a1a KG |
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 | |
177 | }; | |
178 | ||
179 | struct siphash state; | |
180 | sd_id128_t machine_id; | |
181 | uid_t uid; | |
182 | int r; | |
183 | ||
184 | assert(t); | |
185 | ||
186 | uid = getuid(); | |
187 | r = sd_id128_get_machine(&machine_id); | |
188 | if (r < 0) { | |
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; | |
192 | } | |
193 | ||
194 | siphash24_init(&state, hash_key); | |
c01a5c05 | 195 | siphash24_compress_typesafe(machine_id, &state); |
acf24a1a | 196 | siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t)->manager), &state); |
c01a5c05 | 197 | siphash24_compress_typesafe(uid, &state); |
acf24a1a KG |
198 | siphash24_compress_string(UNIT(t)->id, &state); |
199 | ||
200 | return siphash24_finalize(&state); | |
201 | } | |
202 | ||
871d7de4 LP |
203 | static int timer_load(Unit *u) { |
204 | Timer *t = TIMER(u); | |
205 | int r; | |
206 | ||
207 | assert(u); | |
ac155bb8 | 208 | assert(u->load_state == UNIT_STUB); |
871d7de4 | 209 | |
c3620770 | 210 | r = unit_load_fragment_and_dropin(u, true); |
36697dc0 | 211 | if (r < 0) |
871d7de4 LP |
212 | return r; |
213 | ||
75193d41 ZJS |
214 | if (u->load_state != UNIT_LOADED) |
215 | return 0; | |
871d7de4 | 216 | |
75193d41 ZJS |
217 | /* This is a new unit? Then let's add in some extras */ |
218 | r = timer_add_trigger_dependencies(t); | |
219 | if (r < 0) | |
220 | return r; | |
57020a3a | 221 | |
75193d41 ZJS |
222 | r = timer_setup_persistent(t); |
223 | if (r < 0) | |
224 | return r; | |
06642d17 | 225 | |
75193d41 ZJS |
226 | r = timer_add_default_dependencies(t); |
227 | if (r < 0) | |
228 | return r; | |
871d7de4 LP |
229 | |
230 | return timer_verify(t); | |
231 | } | |
232 | ||
233 | static void timer_dump(Unit *u, FILE *f, const char *prefix) { | |
234 | Timer *t = TIMER(u); | |
3ecaa09b | 235 | Unit *trigger; |
034c6ed7 | 236 | |
3ecaa09b LP |
237 | trigger = UNIT_TRIGGER(u); |
238 | ||
871d7de4 LP |
239 | fprintf(f, |
240 | "%sTimer State: %s\n" | |
067d72c9 | 241 | "%sResult: %s\n" |
9f5eb56a | 242 | "%sUnit: %s\n" |
dedabea4 LP |
243 | "%sPersistent: %s\n" |
244 | "%sWakeSystem: %s\n" | |
3e0c30ac | 245 | "%sAccuracy: %s\n" |
efebb613 | 246 | "%sRemainAfterElapse: %s\n" |
acf24a1a | 247 | "%sFixedRandomDelay: %s\n" |
efebb613 | 248 | "%sOnClockChange: %s\n" |
0810e396 | 249 | "%sOnTimeZoneChange: %s\n", |
871d7de4 | 250 | prefix, timer_state_to_string(t->state), |
067d72c9 | 251 | prefix, timer_result_to_string(t->result), |
9f5eb56a | 252 | prefix, trigger ? trigger->id : "n/a", |
dedabea4 LP |
253 | prefix, yes_no(t->persistent), |
254 | prefix, yes_no(t->wake_system), | |
5291f26d | 255 | prefix, FORMAT_TIMESPAN(t->accuracy_usec, 1), |
efebb613 | 256 | prefix, yes_no(t->remain_after_elapse), |
acf24a1a | 257 | prefix, yes_no(t->fixed_random_delay), |
efebb613 LP |
258 | prefix, yes_no(t->on_clock_change), |
259 | prefix, yes_no(t->on_timezone_change)); | |
871d7de4 | 260 | |
2762ce2d | 261 | LIST_FOREACH(value, v, t->values) |
36697dc0 LP |
262 | if (v->base == TIMER_CALENDAR) { |
263 | _cleanup_free_ char *p = NULL; | |
264 | ||
25cb94d4 | 265 | (void) calendar_spec_to_string(v->calendar_spec, &p); |
36697dc0 LP |
266 | |
267 | fprintf(f, | |
268 | "%s%s: %s\n", | |
269 | prefix, | |
270 | timer_base_to_string(v->base), | |
271 | strna(p)); | |
5291f26d | 272 | } else |
36697dc0 LP |
273 | fprintf(f, |
274 | "%s%s: %s\n", | |
275 | prefix, | |
276 | timer_base_to_string(v->base), | |
5291f26d | 277 | FORMAT_TIMESPAN(v->value, 0)); |
871d7de4 LP |
278 | } |
279 | ||
280 | static void timer_set_state(Timer *t, TimerState state) { | |
281 | TimerState old_state; | |
034c6ed7 | 282 | assert(t); |
871d7de4 | 283 | |
6fcbec6f LP |
284 | if (t->state != state) |
285 | bus_unit_send_pending_change_signal(UNIT(t), false); | |
286 | ||
871d7de4 LP |
287 | old_state = t->state; |
288 | t->state = state; | |
289 | ||
36697dc0 | 290 | if (state != TIMER_WAITING) { |
5dcadb4c ZJS |
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); | |
6e2c9ce1 ZJS |
293 | t->next_elapse_monotonic_or_boottime = USEC_INFINITY; |
294 | t->next_elapse_realtime = USEC_INFINITY; | |
36697dc0 | 295 | } |
871d7de4 LP |
296 | |
297 | if (state != old_state) | |
f2341e0a | 298 | log_unit_debug(UNIT(t), "Changed %s -> %s", timer_state_to_string(old_state), timer_state_to_string(state)); |
871d7de4 | 299 | |
96b09de5 | 300 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true); |
871d7de4 LP |
301 | } |
302 | ||
aa1f95d2 | 303 | static void timer_enter_waiting(Timer *t, bool time_change); |
871d7de4 | 304 | |
be847e82 | 305 | static int timer_coldplug(Unit *u) { |
871d7de4 LP |
306 | Timer *t = TIMER(u); |
307 | ||
308 | assert(t); | |
309 | assert(t->state == TIMER_DEAD); | |
310 | ||
3e0c30ac LP |
311 | if (t->deserialized_state == t->state) |
312 | return 0; | |
871d7de4 | 313 | |
3e0c30ac | 314 | if (t->deserialized_state == TIMER_WAITING) |
aa1f95d2 | 315 | timer_enter_waiting(t, false); |
3e0c30ac LP |
316 | else |
317 | timer_set_state(t, t->deserialized_state); | |
871d7de4 LP |
318 | |
319 | return 0; | |
320 | } | |
321 | ||
067d72c9 | 322 | static void timer_enter_dead(Timer *t, TimerResult f) { |
871d7de4 LP |
323 | assert(t); |
324 | ||
a0fef983 | 325 | if (t->result == TIMER_SUCCESS) |
067d72c9 | 326 | t->result = f; |
871d7de4 | 327 | |
aac99f30 | 328 | unit_log_result(UNIT(t), t->result == TIMER_SUCCESS, timer_result_to_string(t->result)); |
067d72c9 | 329 | timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD); |
871d7de4 LP |
330 | } |
331 | ||
3e0c30ac LP |
332 | static void timer_enter_elapsed(Timer *t, bool leave_around) { |
333 | assert(t); | |
334 | ||
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 | |
341 | * later on. */ | |
342 | ||
343 | if (t->remain_after_elapse || leave_around) | |
344 | timer_set_state(t, TIMER_ELAPSED); | |
345 | else | |
346 | timer_enter_dead(t, TIMER_SUCCESS); | |
347 | } | |
348 | ||
744c7693 | 349 | static void add_random(Timer *t, usec_t *v) { |
744c7693 LP |
350 | usec_t add; |
351 | ||
352 | assert(t); | |
3f51aec8 | 353 | assert(v); |
744c7693 LP |
354 | |
355 | if (t->random_usec == 0) | |
356 | return; | |
357 | if (*v == USEC_INFINITY) | |
358 | return; | |
359 | ||
acf24a1a | 360 | add = (t->fixed_random_delay ? timer_get_fixed_delay_hash(t) : random_u64()) % t->random_usec; |
744c7693 LP |
361 | |
362 | if (*v + add < *v) /* overflow */ | |
363 | *v = (usec_t) -2; /* Highest possible value, that is not USEC_INFINITY */ | |
364 | else | |
365 | *v += add; | |
366 | ||
5291f26d | 367 | log_unit_debug(UNIT(t), "Adding %s random time.", FORMAT_TIMESPAN(add, 0)); |
744c7693 LP |
368 | } |
369 | ||
aa1f95d2 | 370 | static void timer_enter_waiting(Timer *t, bool time_change) { |
36697dc0 | 371 | bool found_monotonic = false, found_realtime = false; |
3e0c30ac | 372 | bool leave_around = false; |
c54be90b | 373 | triple_timestamp ts; |
e903182e | 374 | Unit *trigger; |
871d7de4 LP |
375 | int r; |
376 | ||
e903182e LP |
377 | assert(t); |
378 | ||
379 | trigger = UNIT_TRIGGER(UNIT(t)); | |
380 | if (!trigger) { | |
381 | log_unit_error(UNIT(t), "Unit to trigger vanished."); | |
e7912a08 | 382 | goto fail; |
e903182e LP |
383 | } |
384 | ||
fa5a0251 | 385 | triple_timestamp_now(&ts); |
dedabea4 | 386 | t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0; |
871d7de4 LP |
387 | |
388 | LIST_FOREACH(value, v, t->values) { | |
871d7de4 LP |
389 | if (v->disabled) |
390 | continue; | |
391 | ||
36697dc0 | 392 | if (v->base == TIMER_CALENDAR) { |
58afc4f8 | 393 | usec_t b, rebased; |
06642d17 | 394 | |
0bf1d0ff LB |
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. */ | |
399 | ||
e21f75af | 400 | if (dual_timestamp_is_set(&t->last_trigger)) |
9ea9faff | 401 | b = t->last_trigger.realtime; |
6546045f DDM |
402 | else if (dual_timestamp_is_set(&UNIT(t)->inactive_exit_timestamp)) |
403 | b = UNIT(t)->inactive_exit_timestamp.realtime; | |
404 | else | |
405 | b = ts.realtime; | |
06642d17 LP |
406 | |
407 | r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse); | |
36697dc0 LP |
408 | if (r < 0) |
409 | continue; | |
410 | ||
58afc4f8 LP |
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; | |
a87c1d3a | 419 | |
36697dc0 LP |
420 | if (!found_realtime) |
421 | t->next_elapse_realtime = v->next_elapse; | |
10717a1a | 422 | else |
36697dc0 | 423 | t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse); |
871d7de4 | 424 | |
36697dc0 | 425 | found_realtime = true; |
871d7de4 | 426 | |
60933bb8 AJ |
427 | } else { |
428 | usec_t base; | |
c54be90b | 429 | |
36697dc0 | 430 | switch (v->base) { |
871d7de4 | 431 | |
36697dc0 LP |
432 | case TIMER_ACTIVE: |
433 | if (state_translation_table[t->state] == UNIT_ACTIVE) | |
434 | base = UNIT(t)->inactive_exit_timestamp.monotonic; | |
435 | else | |
c54be90b | 436 | base = ts.monotonic; |
36697dc0 | 437 | break; |
871d7de4 | 438 | |
36697dc0 | 439 | case TIMER_BOOT: |
c1d9ba99 MS |
440 | if (detect_container() <= 0) { |
441 | /* CLOCK_MONOTONIC equals the uptime on Linux */ | |
442 | base = 0; | |
443 | break; | |
444 | } | |
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 | |
ec251fe7 | 447 | * our own startup. */ |
4831981d | 448 | _fallthrough_; |
36697dc0 | 449 | case TIMER_STARTUP: |
9f9f0342 | 450 | base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
36697dc0 | 451 | break; |
871d7de4 | 452 | |
36697dc0 | 453 | case TIMER_UNIT_ACTIVE: |
3e0c30ac | 454 | leave_around = true; |
525b95f1 | 455 | base = MAX(trigger->inactive_exit_timestamp.monotonic, t->last_trigger.monotonic); |
e41e1943 | 456 | if (base <= 0) |
36697dc0 | 457 | continue; |
36697dc0 | 458 | break; |
871d7de4 | 459 | |
36697dc0 | 460 | case TIMER_UNIT_INACTIVE: |
3e0c30ac | 461 | leave_around = true; |
525b95f1 | 462 | base = MAX(trigger->inactive_enter_timestamp.monotonic, t->last_trigger.monotonic); |
e41e1943 | 463 | if (base <= 0) |
36697dc0 | 464 | continue; |
36697dc0 | 465 | break; |
871d7de4 | 466 | |
36697dc0 | 467 | default: |
04499a70 | 468 | assert_not_reached(); |
36697dc0 | 469 | } |
871d7de4 | 470 | |
c54be90b | 471 | v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value); |
36697dc0 | 472 | |
aa1f95d2 MK |
473 | if (dual_timestamp_is_set(&t->last_trigger) && |
474 | !time_change && | |
c54be90b LP |
475 | v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) && |
476 | IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) { | |
e41e1943 | 477 | /* This is a one time trigger, disable it now */ |
36697dc0 LP |
478 | v->disabled = true; |
479 | continue; | |
480 | } | |
481 | ||
482 | if (!found_monotonic) | |
dedabea4 | 483 | t->next_elapse_monotonic_or_boottime = v->next_elapse; |
36697dc0 | 484 | else |
dedabea4 | 485 | t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse); |
36697dc0 LP |
486 | |
487 | found_monotonic = true; | |
488 | } | |
871d7de4 LP |
489 | } |
490 | ||
efebb613 | 491 | if (!found_monotonic && !found_realtime && !t->on_timezone_change && !t->on_clock_change) { |
f2341e0a | 492 | log_unit_debug(UNIT(t), "Timer is elapsed."); |
3e0c30ac | 493 | timer_enter_elapsed(t, leave_around); |
871d7de4 LP |
494 | return; |
495 | } | |
496 | ||
36697dc0 | 497 | if (found_monotonic) { |
3e0c30ac | 498 | usec_t left; |
dedabea4 | 499 | |
744c7693 LP |
500 | add_random(t, &t->next_elapse_monotonic_or_boottime); |
501 | ||
c54be90b | 502 | left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t))); |
5291f26d | 503 | log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", FORMAT_TIMESPAN(left, 0)); |
871d7de4 | 504 | |
718db961 | 505 | if (t->monotonic_event_source) { |
dedabea4 | 506 | r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime); |
e7912a08 LP |
507 | if (r < 0) { |
508 | log_unit_warning_errno(UNIT(t), r, "Failed to reschedule monotonic event source: %m"); | |
718db961 | 509 | goto fail; |
e7912a08 | 510 | } |
718db961 LP |
511 | |
512 | r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT); | |
e7912a08 LP |
513 | if (r < 0) { |
514 | log_unit_warning_errno(UNIT(t), r, "Failed to enable monotonic event source: %m"); | |
cbf60d0a | 515 | goto fail; |
e7912a08 | 516 | } |
cbf60d0a LP |
517 | } else { |
518 | ||
6a0f1f6d LP |
519 | r = sd_event_add_time( |
520 | UNIT(t)->manager->event, | |
521 | &t->monotonic_event_source, | |
dedabea4 LP |
522 | t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC, |
523 | t->next_elapse_monotonic_or_boottime, t->accuracy_usec, | |
6a0f1f6d | 524 | timer_dispatch, t); |
e7912a08 LP |
525 | if (r < 0) { |
526 | log_unit_warning_errno(UNIT(t), r, "Failed to add monotonic event source: %m"); | |
cbf60d0a | 527 | goto fail; |
e7912a08 | 528 | } |
718db961 | 529 | |
cbf60d0a LP |
530 | (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic"); |
531 | } | |
7dfbe2e3 | 532 | |
718db961 | 533 | } else if (t->monotonic_event_source) { |
718db961 | 534 | |
06642d17 | 535 | r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF); |
e7912a08 LP |
536 | if (r < 0) { |
537 | log_unit_warning_errno(UNIT(t), r, "Failed to disable monotonic event source: %m"); | |
718db961 | 538 | goto fail; |
e7912a08 | 539 | } |
718db961 | 540 | } |
36697dc0 LP |
541 | |
542 | if (found_realtime) { | |
744c7693 LP |
543 | add_random(t, &t->next_elapse_realtime); |
544 | ||
04f5c018 | 545 | log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", FORMAT_TIMESTAMP(t->next_elapse_realtime)); |
36697dc0 | 546 | |
718db961 LP |
547 | if (t->realtime_event_source) { |
548 | r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime); | |
e7912a08 LP |
549 | if (r < 0) { |
550 | log_unit_warning_errno(UNIT(t), r, "Failed to reschedule realtime event source: %m"); | |
718db961 | 551 | goto fail; |
e7912a08 | 552 | } |
718db961 LP |
553 | |
554 | r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT); | |
e7912a08 LP |
555 | if (r < 0) { |
556 | log_unit_warning_errno(UNIT(t), r, "Failed to enable realtime event source: %m"); | |
cbf60d0a | 557 | goto fail; |
e7912a08 | 558 | } |
cbf60d0a | 559 | } else { |
6a0f1f6d LP |
560 | r = sd_event_add_time( |
561 | UNIT(t)->manager->event, | |
562 | &t->realtime_event_source, | |
dedabea4 | 563 | t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME, |
6a0f1f6d LP |
564 | t->next_elapse_realtime, t->accuracy_usec, |
565 | timer_dispatch, t); | |
e7912a08 LP |
566 | if (r < 0) { |
567 | log_unit_warning_errno(UNIT(t), r, "Failed to add realtime event source: %m"); | |
cbf60d0a | 568 | goto fail; |
e7912a08 | 569 | } |
718db961 | 570 | |
cbf60d0a LP |
571 | (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime"); |
572 | } | |
7dfbe2e3 | 573 | |
718db961 | 574 | } else if (t->realtime_event_source) { |
718db961 | 575 | |
06642d17 | 576 | r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF); |
e7912a08 LP |
577 | if (r < 0) { |
578 | log_unit_warning_errno(UNIT(t), r, "Failed to disable realtime event source: %m"); | |
718db961 | 579 | goto fail; |
e7912a08 | 580 | } |
718db961 | 581 | } |
871d7de4 LP |
582 | |
583 | timer_set_state(t, TIMER_WAITING); | |
584 | return; | |
585 | ||
586 | fail: | |
067d72c9 | 587 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); |
871d7de4 LP |
588 | } |
589 | ||
590 | static void timer_enter_running(Timer *t) { | |
c8bc7519 | 591 | _cleanup_(activation_details_unrefp) ActivationDetails *details = NULL; |
4afd3348 | 592 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e903182e | 593 | Unit *trigger; |
c8bc7519 | 594 | Job *job; |
871d7de4 | 595 | int r; |
398ef8ba | 596 | |
871d7de4 LP |
597 | assert(t); |
598 | ||
ba3e67a7 | 599 | /* Don't start job if we are supposed to go down */ |
31afa0a4 | 600 | if (unit_stop_pending(UNIT(t))) |
ba3e67a7 LP |
601 | return; |
602 | ||
e903182e LP |
603 | trigger = UNIT_TRIGGER(UNIT(t)); |
604 | if (!trigger) { | |
605 | log_unit_error(UNIT(t), "Unit to trigger vanished."); | |
e7912a08 | 606 | goto fail; |
e903182e LP |
607 | } |
608 | ||
c8bc7519 LB |
609 | details = activation_details_new(UNIT(t)); |
610 | if (!details) { | |
e7912a08 | 611 | log_oom(); |
c8bc7519 LB |
612 | goto fail; |
613 | } | |
614 | ||
615 | r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, &job); | |
e7912a08 LP |
616 | if (r < 0) { |
617 | log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r)); | |
871d7de4 | 618 | goto fail; |
e7912a08 | 619 | } |
871d7de4 | 620 | |
fa5a0251 | 621 | dual_timestamp_now(&t->last_trigger); |
c8bc7519 LB |
622 | ACTIVATION_DETAILS_TIMER(details)->last_trigger = t->last_trigger; |
623 | ||
624 | job_set_activation_details(job, details); | |
06642d17 | 625 | |
472fc28f | 626 | if (t->stamp_path) |
ee735086 | 627 | touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID); |
e41e1943 | 628 | |
871d7de4 LP |
629 | timer_set_state(t, TIMER_RUNNING); |
630 | return; | |
631 | ||
632 | fail: | |
067d72c9 | 633 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); |
871d7de4 LP |
634 | } |
635 | ||
636 | static int timer_start(Unit *u) { | |
637 | Timer *t = TIMER(u); | |
07299350 | 638 | int r; |
871d7de4 LP |
639 | |
640 | assert(t); | |
3742095b | 641 | assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED)); |
01f78473 | 642 | |
a4191c9f LP |
643 | r = unit_test_trigger_loaded(u); |
644 | if (r < 0) | |
645 | return r; | |
871d7de4 | 646 | |
4b58153d LP |
647 | r = unit_acquire_invocation_id(u); |
648 | if (r < 0) | |
649 | return r; | |
650 | ||
06642d17 LP |
651 | t->last_trigger = DUAL_TIMESTAMP_NULL; |
652 | ||
779042e7 MC |
653 | /* Reenable all timers that depend on unit activation time */ |
654 | LIST_FOREACH(value, v, t->values) | |
655 | if (v->base == TIMER_ACTIVE) | |
656 | v->disabled = false; | |
657 | ||
06642d17 LP |
658 | if (t->stamp_path) { |
659 | struct stat st; | |
660 | ||
77542a79 LP |
661 | if (stat(t->stamp_path, &st) >= 0) { |
662 | usec_t ft; | |
663 | ||
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. */ | |
666 | ||
667 | ft = timespec_load(&st.st_mtim); | |
668 | if (ft < now(CLOCK_REALTIME)) | |
669 | t->last_trigger.realtime = ft; | |
04f5c018 | 670 | else |
77542a79 | 671 | log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.", |
04f5c018 | 672 | FORMAT_TIMESTAMP(ft)); |
77542a79 LP |
673 | |
674 | } else if (errno == ENOENT) | |
2762ce2d | 675 | /* The timer has never run before, make sure a stamp file exists. */ |
4b58153d | 676 | (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID); |
06642d17 LP |
677 | } |
678 | ||
067d72c9 | 679 | t->result = TIMER_SUCCESS; |
aa1f95d2 | 680 | timer_enter_waiting(t, false); |
82a2b6bb | 681 | return 1; |
871d7de4 LP |
682 | } |
683 | ||
684 | static int timer_stop(Unit *u) { | |
685 | Timer *t = TIMER(u); | |
686 | ||
687 | assert(t); | |
3742095b | 688 | assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED)); |
871d7de4 | 689 | |
067d72c9 | 690 | timer_enter_dead(t, TIMER_SUCCESS); |
82a2b6bb | 691 | return 1; |
871d7de4 LP |
692 | } |
693 | ||
694 | static int timer_serialize(Unit *u, FILE *f, FDSet *fds) { | |
695 | Timer *t = TIMER(u); | |
696 | ||
697 | assert(u); | |
698 | assert(f); | |
699 | assert(fds); | |
700 | ||
d68c645b LP |
701 | (void) serialize_item(f, "state", timer_state_to_string(t->state)); |
702 | (void) serialize_item(f, "result", timer_result_to_string(t->result)); | |
871d7de4 | 703 | |
eba1cf56 | 704 | if (dual_timestamp_is_set(&t->last_trigger)) |
d68c645b | 705 | (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime); |
06642d17 LP |
706 | |
707 | if (t->last_trigger.monotonic > 0) | |
d68c645b | 708 | (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic); |
06642d17 | 709 | |
871d7de4 LP |
710 | return 0; |
711 | } | |
712 | ||
713 | static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
714 | Timer *t = TIMER(u); | |
715 | ||
716 | assert(u); | |
717 | assert(key); | |
718 | assert(value); | |
719 | assert(fds); | |
720 | ||
721 | if (streq(key, "state")) { | |
722 | TimerState state; | |
723 | ||
36697dc0 LP |
724 | state = timer_state_from_string(value); |
725 | if (state < 0) | |
f2341e0a | 726 | log_unit_debug(u, "Failed to parse state value: %s", value); |
871d7de4 LP |
727 | else |
728 | t->deserialized_state = state; | |
d68c645b | 729 | |
067d72c9 LP |
730 | } else if (streq(key, "result")) { |
731 | TimerResult f; | |
732 | ||
733 | f = timer_result_from_string(value); | |
734 | if (f < 0) | |
f2341e0a | 735 | log_unit_debug(u, "Failed to parse result value: %s", value); |
067d72c9 LP |
736 | else if (f != TIMER_SUCCESS) |
737 | t->result = f; | |
06642d17 | 738 | |
d68c645b LP |
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); | |
743 | else | |
f2341e0a | 744 | log_unit_debug(u, "Unknown serialization key: %s", key); |
871d7de4 LP |
745 | |
746 | return 0; | |
034c6ed7 LP |
747 | } |
748 | ||
d1e8e8b5 | 749 | static UnitActiveState timer_active_state(Unit *u) { |
871d7de4 LP |
750 | assert(u); |
751 | ||
752 | return state_translation_table[TIMER(u)->state]; | |
753 | } | |
754 | ||
d1e8e8b5 | 755 | static const char *timer_sub_state_to_string(Unit *u) { |
871d7de4 LP |
756 | assert(u); |
757 | ||
758 | return timer_state_to_string(TIMER(u)->state); | |
759 | } | |
760 | ||
718db961 LP |
761 | static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) { |
762 | Timer *t = TIMER(userdata); | |
871d7de4 LP |
763 | |
764 | assert(t); | |
5cb5a6ff | 765 | |
871d7de4 | 766 | if (t->state != TIMER_WAITING) |
718db961 | 767 | return 0; |
5cb5a6ff | 768 | |
f2341e0a | 769 | log_unit_debug(UNIT(t), "Timer elapsed."); |
871d7de4 | 770 | timer_enter_running(t); |
718db961 | 771 | return 0; |
5cb5a6ff LP |
772 | } |
773 | ||
3ecaa09b LP |
774 | static void timer_trigger_notify(Unit *u, Unit *other) { |
775 | Timer *t = TIMER(u); | |
871d7de4 | 776 | |
3ecaa09b LP |
777 | assert(u); |
778 | assert(other); | |
871d7de4 | 779 | |
0377cd29 LP |
780 | /* Filter out invocations with bogus state */ |
781 | assert(UNIT_IS_LOAD_COMPLETE(other->load_state)); | |
871d7de4 | 782 | |
3ecaa09b LP |
783 | /* Reenable all timers that depend on unit state */ |
784 | LIST_FOREACH(value, v, t->values) | |
3742095b | 785 | if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE)) |
3ecaa09b | 786 | v->disabled = false; |
01f78473 | 787 | |
3ecaa09b | 788 | switch (t->state) { |
871d7de4 | 789 | |
3ecaa09b LP |
790 | case TIMER_WAITING: |
791 | case TIMER_ELAPSED: | |
871d7de4 | 792 | |
3ecaa09b | 793 | /* Recalculate sleep time */ |
aa1f95d2 | 794 | timer_enter_waiting(t, false); |
3ecaa09b | 795 | break; |
871d7de4 | 796 | |
3ecaa09b | 797 | case TIMER_RUNNING: |
871d7de4 | 798 | |
3ecaa09b | 799 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { |
f2341e0a | 800 | log_unit_debug(UNIT(t), "Got notified about unit deactivation."); |
aa1f95d2 | 801 | timer_enter_waiting(t, false); |
3ecaa09b LP |
802 | } |
803 | break; | |
871d7de4 | 804 | |
3ecaa09b LP |
805 | case TIMER_DEAD: |
806 | case TIMER_FAILED: | |
807 | break; | |
871d7de4 | 808 | |
3ecaa09b | 809 | default: |
04499a70 | 810 | assert_not_reached(); |
871d7de4 | 811 | } |
871d7de4 LP |
812 | } |
813 | ||
fdf20a31 | 814 | static void timer_reset_failed(Unit *u) { |
5632e374 LP |
815 | Timer *t = TIMER(u); |
816 | ||
817 | assert(t); | |
818 | ||
fdf20a31 | 819 | if (t->state == TIMER_FAILED) |
5632e374 LP |
820 | timer_set_state(t, TIMER_DEAD); |
821 | ||
067d72c9 | 822 | t->result = TIMER_SUCCESS; |
5632e374 LP |
823 | } |
824 | ||
8742514c LP |
825 | static void timer_time_change(Unit *u) { |
826 | Timer *t = TIMER(u); | |
13f512d3 | 827 | usec_t ts; |
8742514c LP |
828 | |
829 | assert(u); | |
830 | ||
831 | if (t->state != TIMER_WAITING) | |
832 | return; | |
833 | ||
13f512d3 AJ |
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 | |
0923b425 | 836 | * the future triggers to happen again :). Exactly the same as when |
13f512d3 AJ |
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; | |
841 | ||
efebb613 LP |
842 | if (t->on_clock_change) { |
843 | log_unit_debug(u, "Time change, triggering activation."); | |
844 | timer_enter_running(t); | |
845 | } else { | |
846 | log_unit_debug(u, "Time change, recalculating next elapse."); | |
847 | timer_enter_waiting(t, true); | |
848 | } | |
8742514c LP |
849 | } |
850 | ||
bbf5fd8e LP |
851 | static void timer_timezone_change(Unit *u) { |
852 | Timer *t = TIMER(u); | |
853 | ||
854 | assert(u); | |
855 | ||
856 | if (t->state != TIMER_WAITING) | |
857 | return; | |
858 | ||
efebb613 LP |
859 | if (t->on_timezone_change) { |
860 | log_unit_debug(u, "Timezone change, triggering activation."); | |
861 | timer_enter_running(t); | |
862 | } else { | |
863 | log_unit_debug(u, "Timezone change, recalculating next elapse."); | |
864 | timer_enter_waiting(t, false); | |
865 | } | |
bbf5fd8e LP |
866 | } |
867 | ||
89f6fe7b LP |
868 | static int timer_clean(Unit *u, ExecCleanMask mask) { |
869 | Timer *t = TIMER(u); | |
870 | int r; | |
871 | ||
872 | assert(t); | |
873 | assert(mask != 0); | |
874 | ||
875 | if (t->state != TIMER_DEAD) | |
876 | return -EBUSY; | |
877 | ||
4f06325c | 878 | if (mask != EXEC_CLEAN_STATE) |
89f6fe7b LP |
879 | return -EUNATCH; |
880 | ||
881 | r = timer_setup_persistent(t); | |
882 | if (r < 0) | |
883 | return r; | |
884 | ||
885 | if (!t->stamp_path) | |
886 | return -EUNATCH; | |
887 | ||
888 | if (unlink(t->stamp_path) && errno != ENOENT) | |
889 | return log_unit_error_errno(u, errno, "Failed to clean stamp file of timer: %m"); | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | static int timer_can_clean(Unit *u, ExecCleanMask *ret) { | |
895 | Timer *t = TIMER(u); | |
896 | ||
897 | assert(t); | |
4fb8f1e8 | 898 | assert(ret); |
89f6fe7b LP |
899 | |
900 | *ret = t->persistent ? EXEC_CLEAN_STATE : 0; | |
901 | return 0; | |
902 | } | |
903 | ||
705578c3 | 904 | static int timer_can_start(Unit *u) { |
9727f242 DDM |
905 | Timer *t = TIMER(u); |
906 | int r; | |
907 | ||
908 | assert(t); | |
909 | ||
910 | r = unit_test_start_limit(u); | |
911 | if (r < 0) { | |
912 | timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT); | |
913 | return r; | |
914 | } | |
915 | ||
705578c3 | 916 | return 1; |
9727f242 DDM |
917 | } |
918 | ||
c8bc7519 LB |
919 | static void activation_details_timer_serialize(ActivationDetails *details, FILE *f) { |
920 | ActivationDetailsTimer *t = ACTIVATION_DETAILS_TIMER(details); | |
921 | ||
922 | assert(details); | |
923 | assert(f); | |
924 | assert(t); | |
925 | ||
926 | (void) serialize_dual_timestamp(f, "activation-details-timer-last-trigger", &t->last_trigger); | |
927 | } | |
928 | ||
929 | static int activation_details_timer_deserialize(const char *key, const char *value, ActivationDetails **details) { | |
930 | int r; | |
931 | ||
932 | assert(key); | |
933 | assert(value); | |
934 | ||
935 | if (!details || !*details) | |
936 | return -EINVAL; | |
937 | ||
938 | ActivationDetailsTimer *t = ACTIVATION_DETAILS_TIMER(*details); | |
939 | if (!t) | |
940 | return -EINVAL; | |
941 | ||
942 | if (!streq(key, "activation-details-timer-last-trigger")) | |
943 | return -EINVAL; | |
944 | ||
945 | r = deserialize_dual_timestamp(value, &t->last_trigger); | |
946 | if (r < 0) | |
947 | return r; | |
948 | ||
949 | return 0; | |
950 | } | |
951 | ||
952 | static int activation_details_timer_append_env(ActivationDetails *details, char ***strv) { | |
953 | ActivationDetailsTimer *t = ACTIVATION_DETAILS_TIMER(details); | |
954 | int r; | |
955 | ||
956 | assert(details); | |
957 | assert(strv); | |
958 | assert(t); | |
959 | ||
960 | if (!dual_timestamp_is_set(&t->last_trigger)) | |
961 | return 0; | |
962 | ||
6457ce15 | 963 | r = strv_extendf(strv, "TRIGGER_TIMER_REALTIME_USEC=" USEC_FMT, t->last_trigger.realtime); |
c8bc7519 LB |
964 | if (r < 0) |
965 | return r; | |
966 | ||
6457ce15 | 967 | r = strv_extendf(strv, "TRIGGER_TIMER_MONOTONIC_USEC=" USEC_FMT, t->last_trigger.monotonic); |
c8bc7519 LB |
968 | if (r < 0) |
969 | return r; | |
970 | ||
971 | return 2; /* Return the number of variables added to the env block */ | |
972 | } | |
973 | ||
974 | static int activation_details_timer_append_pair(ActivationDetails *details, char ***strv) { | |
975 | ActivationDetailsTimer *t = ACTIVATION_DETAILS_TIMER(details); | |
976 | int r; | |
977 | ||
978 | assert(details); | |
979 | assert(strv); | |
980 | assert(t); | |
981 | ||
982 | if (!dual_timestamp_is_set(&t->last_trigger)) | |
983 | return 0; | |
984 | ||
985 | r = strv_extend(strv, "trigger_timer_realtime_usec"); | |
986 | if (r < 0) | |
987 | return r; | |
988 | ||
6457ce15 | 989 | r = strv_extendf(strv, USEC_FMT, t->last_trigger.realtime); |
c8bc7519 LB |
990 | if (r < 0) |
991 | return r; | |
992 | ||
993 | r = strv_extend(strv, "trigger_timer_monotonic_usec"); | |
994 | if (r < 0) | |
995 | return r; | |
996 | ||
6457ce15 | 997 | r = strv_extendf(strv, USEC_FMT, t->last_trigger.monotonic); |
c8bc7519 LB |
998 | if (r < 0) |
999 | return r; | |
1000 | ||
1001 | return 2; /* Return the number of pairs added to the env block */ | |
1002 | } | |
1003 | ||
f8a990a0 DDM |
1004 | uint64_t timer_next_elapse_monotonic(const Timer *t) { |
1005 | assert(t); | |
1006 | ||
1007 | return (uint64_t) usec_shift_clock(t->next_elapse_monotonic_or_boottime, | |
1008 | TIMER_MONOTONIC_CLOCK(t), CLOCK_MONOTONIC); | |
1009 | } | |
1010 | ||
871d7de4 | 1011 | static const char* const timer_base_table[_TIMER_BASE_MAX] = { |
48d83e33 ZJS |
1012 | [TIMER_ACTIVE] = "OnActiveSec", |
1013 | [TIMER_BOOT] = "OnBootSec", | |
1014 | [TIMER_STARTUP] = "OnStartupSec", | |
1015 | [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec", | |
36697dc0 | 1016 | [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec", |
48d83e33 | 1017 | [TIMER_CALENDAR] = "OnCalendar" |
871d7de4 LP |
1018 | }; |
1019 | ||
1020 | DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase); | |
1021 | ||
f8a990a0 DDM |
1022 | char* timer_base_to_usec_string(TimerBase i) { |
1023 | _cleanup_free_ char *buf = NULL; | |
1024 | const char *s; | |
1025 | size_t l; | |
1026 | ||
1027 | s = timer_base_to_string(i); | |
1028 | ||
1029 | if (endswith(s, "Sec")) { | |
1030 | /* s/Sec/USec/ */ | |
1031 | l = strlen(s); | |
1032 | buf = new(char, l+2); | |
1033 | if (!buf) | |
1034 | return NULL; | |
1035 | ||
1036 | memcpy(buf, s, l-3); | |
1037 | memcpy(buf+l-3, "USec", 5); | |
1038 | } else { | |
1039 | buf = strdup(s); | |
1040 | if (!buf) | |
1041 | return NULL; | |
1042 | } | |
1043 | ||
1044 | return TAKE_PTR(buf); | |
1045 | } | |
1046 | ||
067d72c9 | 1047 | static const char* const timer_result_table[_TIMER_RESULT_MAX] = { |
40f41f34 DDM |
1048 | [TIMER_SUCCESS] = "success", |
1049 | [TIMER_FAILURE_RESOURCES] = "resources", | |
1050 | [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit", | |
067d72c9 LP |
1051 | }; |
1052 | ||
1053 | DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult); | |
1054 | ||
87f0e418 | 1055 | const UnitVTable timer_vtable = { |
7d17cfbc | 1056 | .object_size = sizeof(Timer), |
718db961 | 1057 | |
f975e971 LP |
1058 | .sections = |
1059 | "Unit\0" | |
1060 | "Timer\0" | |
1061 | "Install\0", | |
d8a812d1 | 1062 | .private_section = "Timer", |
5cb5a6ff | 1063 | |
c80a9a33 LP |
1064 | .can_transient = true, |
1065 | .can_fail = true, | |
1066 | .can_trigger = true, | |
1067 | ||
871d7de4 | 1068 | .init = timer_init, |
034c6ed7 | 1069 | .done = timer_done, |
871d7de4 LP |
1070 | .load = timer_load, |
1071 | ||
1072 | .coldplug = timer_coldplug, | |
1073 | ||
1074 | .dump = timer_dump, | |
1075 | ||
1076 | .start = timer_start, | |
1077 | .stop = timer_stop, | |
1078 | ||
89f6fe7b LP |
1079 | .clean = timer_clean, |
1080 | .can_clean = timer_can_clean, | |
1081 | ||
871d7de4 LP |
1082 | .serialize = timer_serialize, |
1083 | .deserialize_item = timer_deserialize_item, | |
1084 | ||
1085 | .active_state = timer_active_state, | |
1086 | .sub_state_to_string = timer_sub_state_to_string, | |
1087 | ||
3ecaa09b LP |
1088 | .trigger_notify = timer_trigger_notify, |
1089 | ||
fdf20a31 | 1090 | .reset_failed = timer_reset_failed, |
8742514c | 1091 | .time_change = timer_time_change, |
bbf5fd8e | 1092 | .timezone_change = timer_timezone_change, |
5632e374 | 1093 | |
d8a812d1 | 1094 | .bus_set_property = bus_timer_set_property, |
9727f242 | 1095 | |
705578c3 | 1096 | .can_start = timer_can_start, |
5cb5a6ff | 1097 | }; |
c8bc7519 LB |
1098 | |
1099 | const ActivationDetailsVTable activation_details_timer_vtable = { | |
1100 | .object_size = sizeof(ActivationDetailsTimer), | |
1101 | ||
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, | |
1106 | }; |