]>
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; | |
bd8f585b | 45 | t->accuracy_usec = u->manager->default_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 | ||
54 | while ((v = t->values)) { | |
71fda00f | 55 | LIST_REMOVE(value, t->values, v); |
3e044c49 | 56 | calendar_spec_free(v->calendar_spec); |
871d7de4 LP |
57 | free(v); |
58 | } | |
74051b9b LP |
59 | } |
60 | ||
61 | static void timer_done(Unit *u) { | |
62 | Timer *t = TIMER(u); | |
63 | ||
64 | assert(t); | |
65 | ||
66 | timer_free_values(t); | |
871d7de4 | 67 | |
5dcadb4c ZJS |
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); | |
06642d17 LP |
70 | |
71 | free(t->stamp_path); | |
871d7de4 LP |
72 | } |
73 | ||
74 | static int timer_verify(Timer *t) { | |
75 | assert(t); | |
75193d41 | 76 | assert(UNIT(t)->load_state == UNIT_LOADED); |
871d7de4 | 77 | |
d85ff944 YW |
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."); | |
871d7de4 LP |
80 | |
81 | return 0; | |
82 | } | |
83 | ||
6c155fe3 LP |
84 | static int timer_add_default_dependencies(Timer *t) { |
85 | int r; | |
19f8d037 | 86 | TimerValue *v; |
6c155fe3 LP |
87 | |
88 | assert(t); | |
89 | ||
4c9ea260 LP |
90 | if (!UNIT(t)->default_dependencies) |
91 | return 0; | |
92 | ||
35d8c19a | 93 | r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_TIMERS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 LP |
94 | if (r < 0) |
95 | return r; | |
2a77d31d | 96 | |
463d0d15 | 97 | if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) { |
5a724170 | 98 | r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
36697dc0 | 99 | if (r < 0) |
6c155fe3 | 100 | return r; |
19f8d037 | 101 | |
fe934b42 LP |
102 | LIST_FOREACH(value, v, t->values) { |
103 | const char *target; | |
104 | ||
105 | if (v->base != TIMER_CALENDAR) | |
106 | continue; | |
107 | ||
108 | FOREACH_STRING(target, SPECIAL_TIME_SYNC_TARGET, SPECIAL_TIME_SET_TARGET) { | |
109 | r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, target, true, UNIT_DEPENDENCY_DEFAULT); | |
19f8d037 TGR |
110 | if (r < 0) |
111 | return r; | |
19f8d037 | 112 | } |
fe934b42 LP |
113 | |
114 | break; | |
115 | } | |
2a77d31d | 116 | } |
6c155fe3 | 117 | |
5a724170 | 118 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
eef85c4a LP |
119 | } |
120 | ||
121 | static int timer_add_trigger_dependencies(Timer *t) { | |
122 | Unit *x; | |
123 | int r; | |
124 | ||
125 | assert(t); | |
126 | ||
bc32241e | 127 | if (UNIT_TRIGGER(UNIT(t))) |
eef85c4a LP |
128 | return 0; |
129 | ||
130 | r = unit_load_related_unit(UNIT(t), ".service", &x); | |
131 | if (r < 0) | |
132 | return r; | |
133 | ||
134 | return unit_add_two_dependencies(UNIT(t), UNIT_BEFORE, UNIT_TRIGGERS, x, true, UNIT_DEPENDENCY_IMPLICIT); | |
6c155fe3 LP |
135 | } |
136 | ||
06642d17 LP |
137 | static int timer_setup_persistent(Timer *t) { |
138 | int r; | |
139 | ||
140 | assert(t); | |
141 | ||
142 | if (!t->persistent) | |
143 | return 0; | |
144 | ||
463d0d15 | 145 | if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) { |
06642d17 | 146 | |
eef85c4a | 147 | r = unit_require_mounts_for(UNIT(t), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE); |
06642d17 LP |
148 | if (r < 0) |
149 | return r; | |
150 | ||
b910cc72 | 151 | t->stamp_path = strjoin("/var/lib/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
152 | } else { |
153 | const char *e; | |
154 | ||
155 | e = getenv("XDG_DATA_HOME"); | |
156 | if (e) | |
605405c6 | 157 | t->stamp_path = strjoin(e, "/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
158 | else { |
159 | ||
160 | _cleanup_free_ char *h = NULL; | |
161 | ||
162 | r = get_home_dir(&h); | |
23bbb0de | 163 | if (r < 0) |
f2341e0a | 164 | return log_unit_error_errno(UNIT(t), r, "Failed to determine home directory: %m"); |
06642d17 | 165 | |
605405c6 | 166 | t->stamp_path = strjoin(h, "/.local/share/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
167 | } |
168 | } | |
169 | ||
170 | if (!t->stamp_path) | |
171 | return log_oom(); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
acf24a1a KG |
176 | static uint64_t timer_get_fixed_delay_hash(Timer *t) { |
177 | static const uint8_t hash_key[] = { | |
178 | 0x51, 0x0a, 0xdb, 0x76, 0x29, 0x51, 0x42, 0xc2, | |
179 | 0x80, 0x35, 0xea, 0xe6, 0x8e, 0x3a, 0x37, 0xbd | |
180 | }; | |
181 | ||
182 | struct siphash state; | |
183 | sd_id128_t machine_id; | |
184 | uid_t uid; | |
185 | int r; | |
186 | ||
187 | assert(t); | |
188 | ||
189 | uid = getuid(); | |
190 | r = sd_id128_get_machine(&machine_id); | |
191 | if (r < 0) { | |
192 | log_unit_debug_errno(UNIT(t), r, | |
193 | "Failed to get machine ID for the fixed delay calculation, proceeding with 0: %m"); | |
194 | machine_id = SD_ID128_NULL; | |
195 | } | |
196 | ||
197 | siphash24_init(&state, hash_key); | |
198 | siphash24_compress(&machine_id, sizeof(sd_id128_t), &state); | |
199 | siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t)->manager), &state); | |
200 | siphash24_compress(&uid, sizeof(uid_t), &state); | |
201 | siphash24_compress_string(UNIT(t)->id, &state); | |
202 | ||
203 | return siphash24_finalize(&state); | |
204 | } | |
205 | ||
871d7de4 LP |
206 | static int timer_load(Unit *u) { |
207 | Timer *t = TIMER(u); | |
208 | int r; | |
209 | ||
210 | assert(u); | |
ac155bb8 | 211 | assert(u->load_state == UNIT_STUB); |
871d7de4 | 212 | |
c3620770 | 213 | r = unit_load_fragment_and_dropin(u, true); |
36697dc0 | 214 | if (r < 0) |
871d7de4 LP |
215 | return r; |
216 | ||
75193d41 ZJS |
217 | if (u->load_state != UNIT_LOADED) |
218 | return 0; | |
871d7de4 | 219 | |
75193d41 ZJS |
220 | /* This is a new unit? Then let's add in some extras */ |
221 | r = timer_add_trigger_dependencies(t); | |
222 | if (r < 0) | |
223 | return r; | |
57020a3a | 224 | |
75193d41 ZJS |
225 | r = timer_setup_persistent(t); |
226 | if (r < 0) | |
227 | return r; | |
06642d17 | 228 | |
75193d41 ZJS |
229 | r = timer_add_default_dependencies(t); |
230 | if (r < 0) | |
231 | return r; | |
871d7de4 LP |
232 | |
233 | return timer_verify(t); | |
234 | } | |
235 | ||
236 | static void timer_dump(Unit *u, FILE *f, const char *prefix) { | |
9f5eb56a | 237 | char buf[FORMAT_TIMESPAN_MAX]; |
871d7de4 | 238 | Timer *t = TIMER(u); |
3ecaa09b | 239 | Unit *trigger; |
871d7de4 | 240 | TimerValue *v; |
034c6ed7 | 241 | |
3ecaa09b LP |
242 | trigger = UNIT_TRIGGER(u); |
243 | ||
871d7de4 LP |
244 | fprintf(f, |
245 | "%sTimer State: %s\n" | |
067d72c9 | 246 | "%sResult: %s\n" |
9f5eb56a | 247 | "%sUnit: %s\n" |
dedabea4 LP |
248 | "%sPersistent: %s\n" |
249 | "%sWakeSystem: %s\n" | |
3e0c30ac | 250 | "%sAccuracy: %s\n" |
efebb613 | 251 | "%sRemainAfterElapse: %s\n" |
acf24a1a | 252 | "%sFixedRandomDelay: %s\n" |
efebb613 | 253 | "%sOnClockChange: %s\n" |
0810e396 | 254 | "%sOnTimeZoneChange: %s\n", |
871d7de4 | 255 | prefix, timer_state_to_string(t->state), |
067d72c9 | 256 | prefix, timer_result_to_string(t->result), |
9f5eb56a | 257 | prefix, trigger ? trigger->id : "n/a", |
dedabea4 LP |
258 | prefix, yes_no(t->persistent), |
259 | prefix, yes_no(t->wake_system), | |
3e0c30ac | 260 | prefix, format_timespan(buf, sizeof(buf), t->accuracy_usec, 1), |
efebb613 | 261 | prefix, yes_no(t->remain_after_elapse), |
acf24a1a | 262 | prefix, yes_no(t->fixed_random_delay), |
efebb613 LP |
263 | prefix, yes_no(t->on_clock_change), |
264 | prefix, yes_no(t->on_timezone_change)); | |
871d7de4 | 265 | |
2762ce2d | 266 | LIST_FOREACH(value, v, t->values) |
36697dc0 LP |
267 | if (v->base == TIMER_CALENDAR) { |
268 | _cleanup_free_ char *p = NULL; | |
269 | ||
25cb94d4 | 270 | (void) calendar_spec_to_string(v->calendar_spec, &p); |
36697dc0 LP |
271 | |
272 | fprintf(f, | |
273 | "%s%s: %s\n", | |
274 | prefix, | |
275 | timer_base_to_string(v->base), | |
276 | strna(p)); | |
277 | } else { | |
278 | char timespan1[FORMAT_TIMESPAN_MAX]; | |
279 | ||
280 | fprintf(f, | |
281 | "%s%s: %s\n", | |
282 | prefix, | |
283 | timer_base_to_string(v->base), | |
b1d6dcf5 | 284 | format_timespan(timespan1, sizeof(timespan1), v->value, 0)); |
36697dc0 | 285 | } |
871d7de4 LP |
286 | } |
287 | ||
288 | static void timer_set_state(Timer *t, TimerState state) { | |
289 | TimerState old_state; | |
034c6ed7 | 290 | assert(t); |
871d7de4 | 291 | |
6fcbec6f LP |
292 | if (t->state != state) |
293 | bus_unit_send_pending_change_signal(UNIT(t), false); | |
294 | ||
871d7de4 LP |
295 | old_state = t->state; |
296 | t->state = state; | |
297 | ||
36697dc0 | 298 | if (state != TIMER_WAITING) { |
5dcadb4c ZJS |
299 | t->monotonic_event_source = sd_event_source_disable_unref(t->monotonic_event_source); |
300 | t->realtime_event_source = sd_event_source_disable_unref(t->realtime_event_source); | |
6e2c9ce1 ZJS |
301 | t->next_elapse_monotonic_or_boottime = USEC_INFINITY; |
302 | t->next_elapse_realtime = USEC_INFINITY; | |
36697dc0 | 303 | } |
871d7de4 LP |
304 | |
305 | if (state != old_state) | |
f2341e0a | 306 | log_unit_debug(UNIT(t), "Changed %s -> %s", timer_state_to_string(old_state), timer_state_to_string(state)); |
871d7de4 | 307 | |
2ad2e41a | 308 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0); |
871d7de4 LP |
309 | } |
310 | ||
aa1f95d2 | 311 | static void timer_enter_waiting(Timer *t, bool time_change); |
871d7de4 | 312 | |
be847e82 | 313 | static int timer_coldplug(Unit *u) { |
871d7de4 LP |
314 | Timer *t = TIMER(u); |
315 | ||
316 | assert(t); | |
317 | assert(t->state == TIMER_DEAD); | |
318 | ||
3e0c30ac LP |
319 | if (t->deserialized_state == t->state) |
320 | return 0; | |
871d7de4 | 321 | |
3e0c30ac | 322 | if (t->deserialized_state == TIMER_WAITING) |
aa1f95d2 | 323 | timer_enter_waiting(t, false); |
3e0c30ac LP |
324 | else |
325 | timer_set_state(t, t->deserialized_state); | |
871d7de4 LP |
326 | |
327 | return 0; | |
328 | } | |
329 | ||
067d72c9 | 330 | static void timer_enter_dead(Timer *t, TimerResult f) { |
871d7de4 LP |
331 | assert(t); |
332 | ||
a0fef983 | 333 | if (t->result == TIMER_SUCCESS) |
067d72c9 | 334 | t->result = f; |
871d7de4 | 335 | |
aac99f30 | 336 | unit_log_result(UNIT(t), t->result == TIMER_SUCCESS, timer_result_to_string(t->result)); |
067d72c9 | 337 | timer_set_state(t, t->result != TIMER_SUCCESS ? TIMER_FAILED : TIMER_DEAD); |
871d7de4 LP |
338 | } |
339 | ||
3e0c30ac LP |
340 | static void timer_enter_elapsed(Timer *t, bool leave_around) { |
341 | assert(t); | |
342 | ||
343 | /* If a unit is marked with RemainAfterElapse=yes we leave it | |
344 | * around even after it elapsed once, so that starting it | |
345 | * later again does not necessarily mean immediate | |
346 | * retriggering. We unconditionally leave units with | |
347 | * TIMER_UNIT_ACTIVE or TIMER_UNIT_INACTIVE triggers around, | |
348 | * since they might be restarted automatically at any time | |
349 | * later on. */ | |
350 | ||
351 | if (t->remain_after_elapse || leave_around) | |
352 | timer_set_state(t, TIMER_ELAPSED); | |
353 | else | |
354 | timer_enter_dead(t, TIMER_SUCCESS); | |
355 | } | |
356 | ||
744c7693 LP |
357 | static void add_random(Timer *t, usec_t *v) { |
358 | char s[FORMAT_TIMESPAN_MAX]; | |
359 | usec_t add; | |
360 | ||
361 | assert(t); | |
3f51aec8 | 362 | assert(v); |
744c7693 LP |
363 | |
364 | if (t->random_usec == 0) | |
365 | return; | |
366 | if (*v == USEC_INFINITY) | |
367 | return; | |
368 | ||
acf24a1a | 369 | add = (t->fixed_random_delay ? timer_get_fixed_delay_hash(t) : random_u64()) % t->random_usec; |
744c7693 LP |
370 | |
371 | if (*v + add < *v) /* overflow */ | |
372 | *v = (usec_t) -2; /* Highest possible value, that is not USEC_INFINITY */ | |
373 | else | |
374 | *v += add; | |
375 | ||
382852fd | 376 | log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0)); |
744c7693 LP |
377 | } |
378 | ||
aa1f95d2 | 379 | static void timer_enter_waiting(Timer *t, bool time_change) { |
36697dc0 | 380 | bool found_monotonic = false, found_realtime = false; |
3e0c30ac | 381 | bool leave_around = false; |
c54be90b | 382 | triple_timestamp ts; |
dedabea4 | 383 | TimerValue *v; |
e903182e | 384 | Unit *trigger; |
871d7de4 LP |
385 | int r; |
386 | ||
e903182e LP |
387 | assert(t); |
388 | ||
389 | trigger = UNIT_TRIGGER(UNIT(t)); | |
390 | if (!trigger) { | |
391 | log_unit_error(UNIT(t), "Unit to trigger vanished."); | |
392 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); | |
393 | return; | |
394 | } | |
395 | ||
c54be90b | 396 | triple_timestamp_get(&ts); |
dedabea4 | 397 | t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0; |
871d7de4 LP |
398 | |
399 | LIST_FOREACH(value, v, t->values) { | |
871d7de4 LP |
400 | if (v->disabled) |
401 | continue; | |
402 | ||
36697dc0 | 403 | if (v->base == TIMER_CALENDAR) { |
58afc4f8 | 404 | usec_t b, rebased; |
06642d17 LP |
405 | |
406 | /* If we know the last time this was | |
407 | * triggered, schedule the job based relative | |
9ea9faff AJ |
408 | * to that. If we don't, just start from |
409 | * the activation time. */ | |
410 | ||
411 | if (t->last_trigger.realtime > 0) | |
412 | b = t->last_trigger.realtime; | |
413 | else { | |
414 | if (state_translation_table[t->state] == UNIT_ACTIVE) | |
415 | b = UNIT(t)->inactive_exit_timestamp.realtime; | |
416 | else | |
417 | b = ts.realtime; | |
418 | } | |
06642d17 LP |
419 | |
420 | r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse); | |
36697dc0 LP |
421 | if (r < 0) |
422 | continue; | |
423 | ||
58afc4f8 LP |
424 | /* To make the delay due to RandomizedDelaySec= work even at boot, if the scheduled |
425 | * time has already passed, set the time when systemd first started as the scheduled | |
426 | * time. Note that we base this on the monotonic timestamp of the boot, not the | |
427 | * realtime one, since the wallclock might have been off during boot. */ | |
428 | rebased = map_clock_usec(UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic, | |
429 | CLOCK_MONOTONIC, CLOCK_REALTIME); | |
430 | if (v->next_elapse < rebased) | |
431 | v->next_elapse = rebased; | |
a87c1d3a | 432 | |
36697dc0 LP |
433 | if (!found_realtime) |
434 | t->next_elapse_realtime = v->next_elapse; | |
10717a1a | 435 | else |
36697dc0 | 436 | t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse); |
871d7de4 | 437 | |
36697dc0 | 438 | found_realtime = true; |
871d7de4 | 439 | |
60933bb8 AJ |
440 | } else { |
441 | usec_t base; | |
c54be90b | 442 | |
36697dc0 | 443 | switch (v->base) { |
871d7de4 | 444 | |
36697dc0 LP |
445 | case TIMER_ACTIVE: |
446 | if (state_translation_table[t->state] == UNIT_ACTIVE) | |
447 | base = UNIT(t)->inactive_exit_timestamp.monotonic; | |
448 | else | |
c54be90b | 449 | base = ts.monotonic; |
36697dc0 | 450 | break; |
871d7de4 | 451 | |
36697dc0 | 452 | case TIMER_BOOT: |
c1d9ba99 MS |
453 | if (detect_container() <= 0) { |
454 | /* CLOCK_MONOTONIC equals the uptime on Linux */ | |
455 | base = 0; | |
456 | break; | |
457 | } | |
458 | /* In a container we don't want to include the time the host | |
459 | * was already up when the container started, so count from | |
ec251fe7 | 460 | * our own startup. */ |
4831981d | 461 | _fallthrough_; |
36697dc0 | 462 | case TIMER_STARTUP: |
9f9f0342 | 463 | base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
36697dc0 | 464 | break; |
871d7de4 | 465 | |
36697dc0 | 466 | case TIMER_UNIT_ACTIVE: |
3e0c30ac | 467 | leave_around = true; |
525b95f1 | 468 | base = MAX(trigger->inactive_exit_timestamp.monotonic, t->last_trigger.monotonic); |
e41e1943 | 469 | if (base <= 0) |
36697dc0 | 470 | continue; |
36697dc0 | 471 | break; |
871d7de4 | 472 | |
36697dc0 | 473 | case TIMER_UNIT_INACTIVE: |
3e0c30ac | 474 | leave_around = true; |
525b95f1 | 475 | base = MAX(trigger->inactive_enter_timestamp.monotonic, t->last_trigger.monotonic); |
e41e1943 | 476 | if (base <= 0) |
36697dc0 | 477 | continue; |
36697dc0 | 478 | break; |
871d7de4 | 479 | |
36697dc0 LP |
480 | default: |
481 | assert_not_reached("Unknown timer base"); | |
482 | } | |
871d7de4 | 483 | |
c54be90b | 484 | v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value); |
36697dc0 | 485 | |
aa1f95d2 MK |
486 | if (dual_timestamp_is_set(&t->last_trigger) && |
487 | !time_change && | |
c54be90b LP |
488 | v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) && |
489 | IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) { | |
e41e1943 | 490 | /* This is a one time trigger, disable it now */ |
36697dc0 LP |
491 | v->disabled = true; |
492 | continue; | |
493 | } | |
494 | ||
495 | if (!found_monotonic) | |
dedabea4 | 496 | t->next_elapse_monotonic_or_boottime = v->next_elapse; |
36697dc0 | 497 | else |
dedabea4 | 498 | t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse); |
36697dc0 LP |
499 | |
500 | found_monotonic = true; | |
501 | } | |
871d7de4 LP |
502 | } |
503 | ||
efebb613 | 504 | if (!found_monotonic && !found_realtime && !t->on_timezone_change && !t->on_clock_change) { |
f2341e0a | 505 | log_unit_debug(UNIT(t), "Timer is elapsed."); |
3e0c30ac | 506 | timer_enter_elapsed(t, leave_around); |
871d7de4 LP |
507 | return; |
508 | } | |
509 | ||
36697dc0 LP |
510 | if (found_monotonic) { |
511 | char buf[FORMAT_TIMESPAN_MAX]; | |
3e0c30ac | 512 | usec_t left; |
dedabea4 | 513 | |
744c7693 LP |
514 | add_random(t, &t->next_elapse_monotonic_or_boottime); |
515 | ||
c54be90b | 516 | left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t))); |
3e0c30ac | 517 | log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", format_timespan(buf, sizeof(buf), left, 0)); |
871d7de4 | 518 | |
718db961 | 519 | if (t->monotonic_event_source) { |
dedabea4 | 520 | r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime); |
718db961 LP |
521 | if (r < 0) |
522 | goto fail; | |
523 | ||
524 | r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT); | |
cbf60d0a LP |
525 | if (r < 0) |
526 | goto fail; | |
527 | } else { | |
528 | ||
6a0f1f6d LP |
529 | r = sd_event_add_time( |
530 | UNIT(t)->manager->event, | |
531 | &t->monotonic_event_source, | |
dedabea4 LP |
532 | t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC, |
533 | t->next_elapse_monotonic_or_boottime, t->accuracy_usec, | |
6a0f1f6d | 534 | timer_dispatch, t); |
cbf60d0a LP |
535 | if (r < 0) |
536 | goto fail; | |
718db961 | 537 | |
cbf60d0a LP |
538 | (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic"); |
539 | } | |
7dfbe2e3 | 540 | |
718db961 | 541 | } else if (t->monotonic_event_source) { |
718db961 | 542 | |
06642d17 | 543 | r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF); |
718db961 LP |
544 | if (r < 0) |
545 | goto fail; | |
546 | } | |
36697dc0 LP |
547 | |
548 | if (found_realtime) { | |
744c7693 LP |
549 | add_random(t, &t->next_elapse_realtime); |
550 | ||
04f5c018 | 551 | log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", FORMAT_TIMESTAMP(t->next_elapse_realtime)); |
36697dc0 | 552 | |
718db961 LP |
553 | if (t->realtime_event_source) { |
554 | r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime); | |
555 | if (r < 0) | |
556 | goto fail; | |
557 | ||
558 | r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT); | |
cbf60d0a LP |
559 | if (r < 0) |
560 | goto fail; | |
561 | } else { | |
6a0f1f6d LP |
562 | r = sd_event_add_time( |
563 | UNIT(t)->manager->event, | |
564 | &t->realtime_event_source, | |
dedabea4 | 565 | t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME, |
6a0f1f6d LP |
566 | t->next_elapse_realtime, t->accuracy_usec, |
567 | timer_dispatch, t); | |
cbf60d0a LP |
568 | if (r < 0) |
569 | goto fail; | |
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); |
718db961 LP |
577 | if (r < 0) |
578 | goto fail; | |
579 | } | |
871d7de4 LP |
580 | |
581 | timer_set_state(t, TIMER_WAITING); | |
582 | return; | |
583 | ||
584 | fail: | |
f2341e0a | 585 | log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m"); |
067d72c9 | 586 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); |
871d7de4 LP |
587 | } |
588 | ||
589 | static void timer_enter_running(Timer *t) { | |
4afd3348 | 590 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e903182e | 591 | Unit *trigger; |
871d7de4 | 592 | int r; |
398ef8ba | 593 | |
871d7de4 LP |
594 | assert(t); |
595 | ||
ba3e67a7 | 596 | /* Don't start job if we are supposed to go down */ |
31afa0a4 | 597 | if (unit_stop_pending(UNIT(t))) |
ba3e67a7 LP |
598 | return; |
599 | ||
e903182e LP |
600 | trigger = UNIT_TRIGGER(UNIT(t)); |
601 | if (!trigger) { | |
602 | log_unit_error(UNIT(t), "Unit to trigger vanished."); | |
603 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); | |
604 | return; | |
605 | } | |
606 | ||
50cbaba4 | 607 | r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, NULL); |
36697dc0 | 608 | if (r < 0) |
871d7de4 LP |
609 | goto fail; |
610 | ||
06642d17 LP |
611 | dual_timestamp_get(&t->last_trigger); |
612 | ||
472fc28f | 613 | if (t->stamp_path) |
ee735086 | 614 | touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID); |
e41e1943 | 615 | |
871d7de4 LP |
616 | timer_set_state(t, TIMER_RUNNING); |
617 | return; | |
618 | ||
619 | fail: | |
f2341e0a | 620 | log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r)); |
067d72c9 | 621 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); |
871d7de4 LP |
622 | } |
623 | ||
624 | static int timer_start(Unit *u) { | |
625 | Timer *t = TIMER(u); | |
779042e7 | 626 | TimerValue *v; |
07299350 | 627 | int r; |
871d7de4 LP |
628 | |
629 | assert(t); | |
3742095b | 630 | assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED)); |
01f78473 | 631 | |
a4191c9f LP |
632 | r = unit_test_trigger_loaded(u); |
633 | if (r < 0) | |
634 | return r; | |
871d7de4 | 635 | |
97a3f4ee | 636 | r = unit_test_start_limit(u); |
07299350 LP |
637 | if (r < 0) { |
638 | timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT); | |
639 | return r; | |
640 | } | |
641 | ||
4b58153d LP |
642 | r = unit_acquire_invocation_id(u); |
643 | if (r < 0) | |
644 | return r; | |
645 | ||
06642d17 LP |
646 | t->last_trigger = DUAL_TIMESTAMP_NULL; |
647 | ||
779042e7 MC |
648 | /* Reenable all timers that depend on unit activation time */ |
649 | LIST_FOREACH(value, v, t->values) | |
650 | if (v->base == TIMER_ACTIVE) | |
651 | v->disabled = false; | |
652 | ||
06642d17 LP |
653 | if (t->stamp_path) { |
654 | struct stat st; | |
655 | ||
77542a79 LP |
656 | if (stat(t->stamp_path, &st) >= 0) { |
657 | usec_t ft; | |
658 | ||
659 | /* Load the file timestamp, but only if it is actually in the past. If it is in the future, | |
660 | * something is wrong with the system clock. */ | |
661 | ||
662 | ft = timespec_load(&st.st_mtim); | |
663 | if (ft < now(CLOCK_REALTIME)) | |
664 | t->last_trigger.realtime = ft; | |
04f5c018 | 665 | else |
77542a79 | 666 | log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.", |
04f5c018 | 667 | FORMAT_TIMESTAMP(ft)); |
77542a79 LP |
668 | |
669 | } else if (errno == ENOENT) | |
2762ce2d | 670 | /* The timer has never run before, make sure a stamp file exists. */ |
4b58153d | 671 | (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID); |
06642d17 LP |
672 | } |
673 | ||
067d72c9 | 674 | t->result = TIMER_SUCCESS; |
aa1f95d2 | 675 | timer_enter_waiting(t, false); |
82a2b6bb | 676 | return 1; |
871d7de4 LP |
677 | } |
678 | ||
679 | static int timer_stop(Unit *u) { | |
680 | Timer *t = TIMER(u); | |
681 | ||
682 | assert(t); | |
3742095b | 683 | assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED)); |
871d7de4 | 684 | |
067d72c9 | 685 | timer_enter_dead(t, TIMER_SUCCESS); |
82a2b6bb | 686 | return 1; |
871d7de4 LP |
687 | } |
688 | ||
689 | static int timer_serialize(Unit *u, FILE *f, FDSet *fds) { | |
690 | Timer *t = TIMER(u); | |
691 | ||
692 | assert(u); | |
693 | assert(f); | |
694 | assert(fds); | |
695 | ||
d68c645b LP |
696 | (void) serialize_item(f, "state", timer_state_to_string(t->state)); |
697 | (void) serialize_item(f, "result", timer_result_to_string(t->result)); | |
871d7de4 | 698 | |
06642d17 | 699 | if (t->last_trigger.realtime > 0) |
d68c645b | 700 | (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime); |
06642d17 LP |
701 | |
702 | if (t->last_trigger.monotonic > 0) | |
d68c645b | 703 | (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic); |
06642d17 | 704 | |
871d7de4 LP |
705 | return 0; |
706 | } | |
707 | ||
708 | static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
709 | Timer *t = TIMER(u); | |
710 | ||
711 | assert(u); | |
712 | assert(key); | |
713 | assert(value); | |
714 | assert(fds); | |
715 | ||
716 | if (streq(key, "state")) { | |
717 | TimerState state; | |
718 | ||
36697dc0 LP |
719 | state = timer_state_from_string(value); |
720 | if (state < 0) | |
f2341e0a | 721 | log_unit_debug(u, "Failed to parse state value: %s", value); |
871d7de4 LP |
722 | else |
723 | t->deserialized_state = state; | |
d68c645b | 724 | |
067d72c9 LP |
725 | } else if (streq(key, "result")) { |
726 | TimerResult f; | |
727 | ||
728 | f = timer_result_from_string(value); | |
729 | if (f < 0) | |
f2341e0a | 730 | log_unit_debug(u, "Failed to parse result value: %s", value); |
067d72c9 LP |
731 | else if (f != TIMER_SUCCESS) |
732 | t->result = f; | |
06642d17 | 733 | |
d68c645b LP |
734 | } else if (streq(key, "last-trigger-realtime")) |
735 | (void) deserialize_usec(value, &t->last_trigger.realtime); | |
736 | else if (streq(key, "last-trigger-monotonic")) | |
737 | (void) deserialize_usec(value, &t->last_trigger.monotonic); | |
738 | else | |
f2341e0a | 739 | log_unit_debug(u, "Unknown serialization key: %s", key); |
871d7de4 LP |
740 | |
741 | return 0; | |
034c6ed7 LP |
742 | } |
743 | ||
44a6b1b6 | 744 | _pure_ static UnitActiveState timer_active_state(Unit *u) { |
871d7de4 LP |
745 | assert(u); |
746 | ||
747 | return state_translation_table[TIMER(u)->state]; | |
748 | } | |
749 | ||
44a6b1b6 | 750 | _pure_ static const char *timer_sub_state_to_string(Unit *u) { |
871d7de4 LP |
751 | assert(u); |
752 | ||
753 | return timer_state_to_string(TIMER(u)->state); | |
754 | } | |
755 | ||
718db961 LP |
756 | static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) { |
757 | Timer *t = TIMER(userdata); | |
871d7de4 LP |
758 | |
759 | assert(t); | |
5cb5a6ff | 760 | |
871d7de4 | 761 | if (t->state != TIMER_WAITING) |
718db961 | 762 | return 0; |
5cb5a6ff | 763 | |
f2341e0a | 764 | log_unit_debug(UNIT(t), "Timer elapsed."); |
871d7de4 | 765 | timer_enter_running(t); |
718db961 | 766 | return 0; |
5cb5a6ff LP |
767 | } |
768 | ||
3ecaa09b LP |
769 | static void timer_trigger_notify(Unit *u, Unit *other) { |
770 | Timer *t = TIMER(u); | |
771 | TimerValue *v; | |
871d7de4 | 772 | |
3ecaa09b LP |
773 | assert(u); |
774 | assert(other); | |
871d7de4 | 775 | |
0377cd29 LP |
776 | /* Filter out invocations with bogus state */ |
777 | assert(UNIT_IS_LOAD_COMPLETE(other->load_state)); | |
871d7de4 | 778 | |
3ecaa09b LP |
779 | /* Reenable all timers that depend on unit state */ |
780 | LIST_FOREACH(value, v, t->values) | |
3742095b | 781 | if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE)) |
3ecaa09b | 782 | v->disabled = false; |
01f78473 | 783 | |
3ecaa09b | 784 | switch (t->state) { |
871d7de4 | 785 | |
3ecaa09b LP |
786 | case TIMER_WAITING: |
787 | case TIMER_ELAPSED: | |
871d7de4 | 788 | |
3ecaa09b | 789 | /* Recalculate sleep time */ |
aa1f95d2 | 790 | timer_enter_waiting(t, false); |
3ecaa09b | 791 | break; |
871d7de4 | 792 | |
3ecaa09b | 793 | case TIMER_RUNNING: |
871d7de4 | 794 | |
3ecaa09b | 795 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { |
f2341e0a | 796 | log_unit_debug(UNIT(t), "Got notified about unit deactivation."); |
aa1f95d2 | 797 | timer_enter_waiting(t, false); |
3ecaa09b LP |
798 | } |
799 | break; | |
871d7de4 | 800 | |
3ecaa09b LP |
801 | case TIMER_DEAD: |
802 | case TIMER_FAILED: | |
803 | break; | |
871d7de4 | 804 | |
3ecaa09b LP |
805 | default: |
806 | assert_not_reached("Unknown timer state"); | |
871d7de4 | 807 | } |
871d7de4 LP |
808 | } |
809 | ||
fdf20a31 | 810 | static void timer_reset_failed(Unit *u) { |
5632e374 LP |
811 | Timer *t = TIMER(u); |
812 | ||
813 | assert(t); | |
814 | ||
fdf20a31 | 815 | if (t->state == TIMER_FAILED) |
5632e374 LP |
816 | timer_set_state(t, TIMER_DEAD); |
817 | ||
067d72c9 | 818 | t->result = TIMER_SUCCESS; |
5632e374 LP |
819 | } |
820 | ||
8742514c LP |
821 | static void timer_time_change(Unit *u) { |
822 | Timer *t = TIMER(u); | |
13f512d3 | 823 | usec_t ts; |
8742514c LP |
824 | |
825 | assert(u); | |
826 | ||
827 | if (t->state != TIMER_WAITING) | |
828 | return; | |
829 | ||
13f512d3 AJ |
830 | /* If we appear to have triggered in the future, the system clock must |
831 | * have been set backwards. So let's rewind our own clock and allow | |
832 | * the future trigger(s) to happen again :). Exactly the same as when | |
833 | * you start a timer unit with Persistent=yes. */ | |
834 | ts = now(CLOCK_REALTIME); | |
835 | if (t->last_trigger.realtime > ts) | |
836 | t->last_trigger.realtime = ts; | |
837 | ||
efebb613 LP |
838 | if (t->on_clock_change) { |
839 | log_unit_debug(u, "Time change, triggering activation."); | |
840 | timer_enter_running(t); | |
841 | } else { | |
842 | log_unit_debug(u, "Time change, recalculating next elapse."); | |
843 | timer_enter_waiting(t, true); | |
844 | } | |
8742514c LP |
845 | } |
846 | ||
bbf5fd8e LP |
847 | static void timer_timezone_change(Unit *u) { |
848 | Timer *t = TIMER(u); | |
849 | ||
850 | assert(u); | |
851 | ||
852 | if (t->state != TIMER_WAITING) | |
853 | return; | |
854 | ||
efebb613 LP |
855 | if (t->on_timezone_change) { |
856 | log_unit_debug(u, "Timezone change, triggering activation."); | |
857 | timer_enter_running(t); | |
858 | } else { | |
859 | log_unit_debug(u, "Timezone change, recalculating next elapse."); | |
860 | timer_enter_waiting(t, false); | |
861 | } | |
bbf5fd8e LP |
862 | } |
863 | ||
89f6fe7b LP |
864 | static int timer_clean(Unit *u, ExecCleanMask mask) { |
865 | Timer *t = TIMER(u); | |
866 | int r; | |
867 | ||
868 | assert(t); | |
869 | assert(mask != 0); | |
870 | ||
871 | if (t->state != TIMER_DEAD) | |
872 | return -EBUSY; | |
873 | ||
874 | if (!IN_SET(mask, EXEC_CLEAN_STATE)) | |
875 | return -EUNATCH; | |
876 | ||
877 | r = timer_setup_persistent(t); | |
878 | if (r < 0) | |
879 | return r; | |
880 | ||
881 | if (!t->stamp_path) | |
882 | return -EUNATCH; | |
883 | ||
884 | if (unlink(t->stamp_path) && errno != ENOENT) | |
885 | return log_unit_error_errno(u, errno, "Failed to clean stamp file of timer: %m"); | |
886 | ||
887 | return 0; | |
888 | } | |
889 | ||
890 | static int timer_can_clean(Unit *u, ExecCleanMask *ret) { | |
891 | Timer *t = TIMER(u); | |
892 | ||
893 | assert(t); | |
894 | ||
895 | *ret = t->persistent ? EXEC_CLEAN_STATE : 0; | |
896 | return 0; | |
897 | } | |
898 | ||
871d7de4 | 899 | static const char* const timer_base_table[_TIMER_BASE_MAX] = { |
03fae018 LP |
900 | [TIMER_ACTIVE] = "OnActiveSec", |
901 | [TIMER_BOOT] = "OnBootSec", | |
902 | [TIMER_STARTUP] = "OnStartupSec", | |
903 | [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec", | |
36697dc0 LP |
904 | [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec", |
905 | [TIMER_CALENDAR] = "OnCalendar" | |
871d7de4 LP |
906 | }; |
907 | ||
908 | DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase); | |
909 | ||
067d72c9 LP |
910 | static const char* const timer_result_table[_TIMER_RESULT_MAX] = { |
911 | [TIMER_SUCCESS] = "success", | |
07299350 LP |
912 | [TIMER_FAILURE_RESOURCES] = "resources", |
913 | [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit", | |
067d72c9 LP |
914 | }; |
915 | ||
916 | DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult); | |
917 | ||
87f0e418 | 918 | const UnitVTable timer_vtable = { |
7d17cfbc | 919 | .object_size = sizeof(Timer), |
718db961 | 920 | |
f975e971 LP |
921 | .sections = |
922 | "Unit\0" | |
923 | "Timer\0" | |
924 | "Install\0", | |
d8a812d1 | 925 | .private_section = "Timer", |
5cb5a6ff | 926 | |
c80a9a33 LP |
927 | .can_transient = true, |
928 | .can_fail = true, | |
929 | .can_trigger = true, | |
930 | ||
871d7de4 | 931 | .init = timer_init, |
034c6ed7 | 932 | .done = timer_done, |
871d7de4 LP |
933 | .load = timer_load, |
934 | ||
935 | .coldplug = timer_coldplug, | |
936 | ||
937 | .dump = timer_dump, | |
938 | ||
939 | .start = timer_start, | |
940 | .stop = timer_stop, | |
941 | ||
89f6fe7b LP |
942 | .clean = timer_clean, |
943 | .can_clean = timer_can_clean, | |
944 | ||
871d7de4 LP |
945 | .serialize = timer_serialize, |
946 | .deserialize_item = timer_deserialize_item, | |
947 | ||
948 | .active_state = timer_active_state, | |
949 | .sub_state_to_string = timer_sub_state_to_string, | |
950 | ||
3ecaa09b LP |
951 | .trigger_notify = timer_trigger_notify, |
952 | ||
fdf20a31 | 953 | .reset_failed = timer_reset_failed, |
8742514c | 954 | .time_change = timer_time_change, |
bbf5fd8e | 955 | .timezone_change = timer_timezone_change, |
5632e374 | 956 | |
d8a812d1 | 957 | .bus_set_property = bus_timer_set_property, |
5cb5a6ff | 958 | }; |