]>
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; | |
86 | ||
87 | assert(t); | |
88 | ||
4c9ea260 LP |
89 | if (!UNIT(t)->default_dependencies) |
90 | return 0; | |
91 | ||
35d8c19a | 92 | r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_TIMERS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
e3d84721 LP |
93 | if (r < 0) |
94 | return r; | |
2a77d31d | 95 | |
463d0d15 | 96 | if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) { |
5a724170 | 97 | r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
36697dc0 | 98 | if (r < 0) |
6c155fe3 | 99 | return r; |
19f8d037 | 100 | |
fe934b42 | 101 | LIST_FOREACH(value, v, t->values) { |
fe934b42 LP |
102 | if (v->base != TIMER_CALENDAR) |
103 | continue; | |
104 | ||
105 | FOREACH_STRING(target, SPECIAL_TIME_SYNC_TARGET, SPECIAL_TIME_SET_TARGET) { | |
106 | r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, target, true, UNIT_DEPENDENCY_DEFAULT); | |
19f8d037 TGR |
107 | if (r < 0) |
108 | return r; | |
19f8d037 | 109 | } |
fe934b42 LP |
110 | |
111 | break; | |
112 | } | |
2a77d31d | 113 | } |
6c155fe3 | 114 | |
5a724170 | 115 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
eef85c4a LP |
116 | } |
117 | ||
118 | static int timer_add_trigger_dependencies(Timer *t) { | |
119 | Unit *x; | |
120 | int r; | |
121 | ||
122 | assert(t); | |
123 | ||
bc32241e | 124 | if (UNIT_TRIGGER(UNIT(t))) |
eef85c4a LP |
125 | return 0; |
126 | ||
127 | r = unit_load_related_unit(UNIT(t), ".service", &x); | |
128 | if (r < 0) | |
129 | return r; | |
130 | ||
131 | return unit_add_two_dependencies(UNIT(t), UNIT_BEFORE, UNIT_TRIGGERS, x, true, UNIT_DEPENDENCY_IMPLICIT); | |
6c155fe3 LP |
132 | } |
133 | ||
06642d17 LP |
134 | static int timer_setup_persistent(Timer *t) { |
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 | |
eef85c4a | 144 | r = unit_require_mounts_for(UNIT(t), "/var/lib/systemd/timers", UNIT_DEPENDENCY_FILE); |
06642d17 LP |
145 | if (r < 0) |
146 | return r; | |
147 | ||
b910cc72 | 148 | t->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) | |
605405c6 | 154 | t->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 | |
605405c6 | 163 | t->stamp_path = strjoin(h, "/.local/share/systemd/timers/stamp-", UNIT(t)->id); |
06642d17 LP |
164 | } |
165 | } | |
166 | ||
167 | if (!t->stamp_path) | |
168 | return log_oom(); | |
169 | ||
170 | return 0; | |
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); | |
195 | siphash24_compress(&machine_id, sizeof(sd_id128_t), &state); | |
196 | siphash24_compress_boolean(MANAGER_IS_SYSTEM(UNIT(t)->manager), &state); | |
197 | siphash24_compress(&uid, sizeof(uid_t), &state); | |
198 | siphash24_compress_string(UNIT(t)->id, &state); | |
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 | |
2ad2e41a | 300 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0); |
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."); | |
382 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); | |
383 | return; | |
384 | } | |
385 | ||
c54be90b | 386 | triple_timestamp_get(&ts); |
dedabea4 | 387 | t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0; |
871d7de4 LP |
388 | |
389 | LIST_FOREACH(value, v, t->values) { | |
871d7de4 LP |
390 | if (v->disabled) |
391 | continue; | |
392 | ||
36697dc0 | 393 | if (v->base == TIMER_CALENDAR) { |
58afc4f8 | 394 | usec_t b, rebased; |
06642d17 LP |
395 | |
396 | /* If we know the last time this was | |
397 | * triggered, schedule the job based relative | |
9ea9faff AJ |
398 | * to that. If we don't, just start from |
399 | * the activation time. */ | |
400 | ||
401 | if (t->last_trigger.realtime > 0) | |
402 | b = t->last_trigger.realtime; | |
403 | else { | |
404 | if (state_translation_table[t->state] == UNIT_ACTIVE) | |
405 | b = UNIT(t)->inactive_exit_timestamp.realtime; | |
406 | else | |
407 | b = ts.realtime; | |
408 | } | |
06642d17 LP |
409 | |
410 | r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse); | |
36697dc0 LP |
411 | if (r < 0) |
412 | continue; | |
413 | ||
58afc4f8 LP |
414 | /* To make the delay due to RandomizedDelaySec= work even at boot, if the scheduled |
415 | * time has already passed, set the time when systemd first started as the scheduled | |
416 | * time. Note that we base this on the monotonic timestamp of the boot, not the | |
417 | * realtime one, since the wallclock might have been off during boot. */ | |
418 | rebased = map_clock_usec(UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic, | |
419 | CLOCK_MONOTONIC, CLOCK_REALTIME); | |
420 | if (v->next_elapse < rebased) | |
421 | v->next_elapse = rebased; | |
a87c1d3a | 422 | |
36697dc0 LP |
423 | if (!found_realtime) |
424 | t->next_elapse_realtime = v->next_elapse; | |
10717a1a | 425 | else |
36697dc0 | 426 | t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse); |
871d7de4 | 427 | |
36697dc0 | 428 | found_realtime = true; |
871d7de4 | 429 | |
60933bb8 AJ |
430 | } else { |
431 | usec_t base; | |
c54be90b | 432 | |
36697dc0 | 433 | switch (v->base) { |
871d7de4 | 434 | |
36697dc0 LP |
435 | case TIMER_ACTIVE: |
436 | if (state_translation_table[t->state] == UNIT_ACTIVE) | |
437 | base = UNIT(t)->inactive_exit_timestamp.monotonic; | |
438 | else | |
c54be90b | 439 | base = ts.monotonic; |
36697dc0 | 440 | break; |
871d7de4 | 441 | |
36697dc0 | 442 | case TIMER_BOOT: |
c1d9ba99 MS |
443 | if (detect_container() <= 0) { |
444 | /* CLOCK_MONOTONIC equals the uptime on Linux */ | |
445 | base = 0; | |
446 | break; | |
447 | } | |
448 | /* In a container we don't want to include the time the host | |
449 | * was already up when the container started, so count from | |
ec251fe7 | 450 | * our own startup. */ |
4831981d | 451 | _fallthrough_; |
36697dc0 | 452 | case TIMER_STARTUP: |
9f9f0342 | 453 | base = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
36697dc0 | 454 | break; |
871d7de4 | 455 | |
36697dc0 | 456 | case TIMER_UNIT_ACTIVE: |
3e0c30ac | 457 | leave_around = true; |
525b95f1 | 458 | base = MAX(trigger->inactive_exit_timestamp.monotonic, t->last_trigger.monotonic); |
e41e1943 | 459 | if (base <= 0) |
36697dc0 | 460 | continue; |
36697dc0 | 461 | break; |
871d7de4 | 462 | |
36697dc0 | 463 | case TIMER_UNIT_INACTIVE: |
3e0c30ac | 464 | leave_around = true; |
525b95f1 | 465 | base = MAX(trigger->inactive_enter_timestamp.monotonic, t->last_trigger.monotonic); |
e41e1943 | 466 | if (base <= 0) |
36697dc0 | 467 | continue; |
36697dc0 | 468 | break; |
871d7de4 | 469 | |
36697dc0 | 470 | default: |
04499a70 | 471 | assert_not_reached(); |
36697dc0 | 472 | } |
871d7de4 | 473 | |
c54be90b | 474 | v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value); |
36697dc0 | 475 | |
aa1f95d2 MK |
476 | if (dual_timestamp_is_set(&t->last_trigger) && |
477 | !time_change && | |
c54be90b LP |
478 | v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) && |
479 | IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) { | |
e41e1943 | 480 | /* This is a one time trigger, disable it now */ |
36697dc0 LP |
481 | v->disabled = true; |
482 | continue; | |
483 | } | |
484 | ||
485 | if (!found_monotonic) | |
dedabea4 | 486 | t->next_elapse_monotonic_or_boottime = v->next_elapse; |
36697dc0 | 487 | else |
dedabea4 | 488 | t->next_elapse_monotonic_or_boottime = MIN(t->next_elapse_monotonic_or_boottime, v->next_elapse); |
36697dc0 LP |
489 | |
490 | found_monotonic = true; | |
491 | } | |
871d7de4 LP |
492 | } |
493 | ||
efebb613 | 494 | if (!found_monotonic && !found_realtime && !t->on_timezone_change && !t->on_clock_change) { |
f2341e0a | 495 | log_unit_debug(UNIT(t), "Timer is elapsed."); |
3e0c30ac | 496 | timer_enter_elapsed(t, leave_around); |
871d7de4 LP |
497 | return; |
498 | } | |
499 | ||
36697dc0 | 500 | if (found_monotonic) { |
3e0c30ac | 501 | usec_t left; |
dedabea4 | 502 | |
744c7693 LP |
503 | add_random(t, &t->next_elapse_monotonic_or_boottime); |
504 | ||
c54be90b | 505 | left = usec_sub_unsigned(t->next_elapse_monotonic_or_boottime, triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t))); |
5291f26d | 506 | log_unit_debug(UNIT(t), "Monotonic timer elapses in %s.", FORMAT_TIMESPAN(left, 0)); |
871d7de4 | 507 | |
718db961 | 508 | if (t->monotonic_event_source) { |
dedabea4 | 509 | r = sd_event_source_set_time(t->monotonic_event_source, t->next_elapse_monotonic_or_boottime); |
718db961 LP |
510 | if (r < 0) |
511 | goto fail; | |
512 | ||
513 | r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_ONESHOT); | |
cbf60d0a LP |
514 | if (r < 0) |
515 | goto fail; | |
516 | } else { | |
517 | ||
6a0f1f6d LP |
518 | r = sd_event_add_time( |
519 | UNIT(t)->manager->event, | |
520 | &t->monotonic_event_source, | |
dedabea4 LP |
521 | t->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC, |
522 | t->next_elapse_monotonic_or_boottime, t->accuracy_usec, | |
6a0f1f6d | 523 | timer_dispatch, t); |
cbf60d0a LP |
524 | if (r < 0) |
525 | goto fail; | |
718db961 | 526 | |
cbf60d0a LP |
527 | (void) sd_event_source_set_description(t->monotonic_event_source, "timer-monotonic"); |
528 | } | |
7dfbe2e3 | 529 | |
718db961 | 530 | } else if (t->monotonic_event_source) { |
718db961 | 531 | |
06642d17 | 532 | r = sd_event_source_set_enabled(t->monotonic_event_source, SD_EVENT_OFF); |
718db961 LP |
533 | if (r < 0) |
534 | goto fail; | |
535 | } | |
36697dc0 LP |
536 | |
537 | if (found_realtime) { | |
744c7693 LP |
538 | add_random(t, &t->next_elapse_realtime); |
539 | ||
04f5c018 | 540 | log_unit_debug(UNIT(t), "Realtime timer elapses at %s.", FORMAT_TIMESTAMP(t->next_elapse_realtime)); |
36697dc0 | 541 | |
718db961 LP |
542 | if (t->realtime_event_source) { |
543 | r = sd_event_source_set_time(t->realtime_event_source, t->next_elapse_realtime); | |
544 | if (r < 0) | |
545 | goto fail; | |
546 | ||
547 | r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_ONESHOT); | |
cbf60d0a LP |
548 | if (r < 0) |
549 | goto fail; | |
550 | } else { | |
6a0f1f6d LP |
551 | r = sd_event_add_time( |
552 | UNIT(t)->manager->event, | |
553 | &t->realtime_event_source, | |
dedabea4 | 554 | t->wake_system ? CLOCK_REALTIME_ALARM : CLOCK_REALTIME, |
6a0f1f6d LP |
555 | t->next_elapse_realtime, t->accuracy_usec, |
556 | timer_dispatch, t); | |
cbf60d0a LP |
557 | if (r < 0) |
558 | goto fail; | |
718db961 | 559 | |
cbf60d0a LP |
560 | (void) sd_event_source_set_description(t->realtime_event_source, "timer-realtime"); |
561 | } | |
7dfbe2e3 | 562 | |
718db961 | 563 | } else if (t->realtime_event_source) { |
718db961 | 564 | |
06642d17 | 565 | r = sd_event_source_set_enabled(t->realtime_event_source, SD_EVENT_OFF); |
718db961 LP |
566 | if (r < 0) |
567 | goto fail; | |
568 | } | |
871d7de4 LP |
569 | |
570 | timer_set_state(t, TIMER_WAITING); | |
571 | return; | |
572 | ||
573 | fail: | |
f2341e0a | 574 | log_unit_warning_errno(UNIT(t), r, "Failed to enter waiting state: %m"); |
067d72c9 | 575 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); |
871d7de4 LP |
576 | } |
577 | ||
578 | static void timer_enter_running(Timer *t) { | |
4afd3348 | 579 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e903182e | 580 | Unit *trigger; |
871d7de4 | 581 | int r; |
398ef8ba | 582 | |
871d7de4 LP |
583 | assert(t); |
584 | ||
ba3e67a7 | 585 | /* Don't start job if we are supposed to go down */ |
31afa0a4 | 586 | if (unit_stop_pending(UNIT(t))) |
ba3e67a7 LP |
587 | return; |
588 | ||
e903182e LP |
589 | trigger = UNIT_TRIGGER(UNIT(t)); |
590 | if (!trigger) { | |
591 | log_unit_error(UNIT(t), "Unit to trigger vanished."); | |
592 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); | |
593 | return; | |
594 | } | |
595 | ||
50cbaba4 | 596 | r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, NULL); |
36697dc0 | 597 | if (r < 0) |
871d7de4 LP |
598 | goto fail; |
599 | ||
06642d17 LP |
600 | dual_timestamp_get(&t->last_trigger); |
601 | ||
472fc28f | 602 | if (t->stamp_path) |
ee735086 | 603 | touch_file(t->stamp_path, true, t->last_trigger.realtime, UID_INVALID, GID_INVALID, MODE_INVALID); |
e41e1943 | 604 | |
871d7de4 LP |
605 | timer_set_state(t, TIMER_RUNNING); |
606 | return; | |
607 | ||
608 | fail: | |
f2341e0a | 609 | log_unit_warning(UNIT(t), "Failed to queue unit startup job: %s", bus_error_message(&error, r)); |
067d72c9 | 610 | timer_enter_dead(t, TIMER_FAILURE_RESOURCES); |
871d7de4 LP |
611 | } |
612 | ||
613 | static int timer_start(Unit *u) { | |
614 | Timer *t = TIMER(u); | |
07299350 | 615 | int r; |
871d7de4 LP |
616 | |
617 | assert(t); | |
3742095b | 618 | assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED)); |
01f78473 | 619 | |
a4191c9f LP |
620 | r = unit_test_trigger_loaded(u); |
621 | if (r < 0) | |
622 | return r; | |
871d7de4 | 623 | |
4b58153d LP |
624 | r = unit_acquire_invocation_id(u); |
625 | if (r < 0) | |
626 | return r; | |
627 | ||
06642d17 LP |
628 | t->last_trigger = DUAL_TIMESTAMP_NULL; |
629 | ||
779042e7 MC |
630 | /* Reenable all timers that depend on unit activation time */ |
631 | LIST_FOREACH(value, v, t->values) | |
632 | if (v->base == TIMER_ACTIVE) | |
633 | v->disabled = false; | |
634 | ||
06642d17 LP |
635 | if (t->stamp_path) { |
636 | struct stat st; | |
637 | ||
77542a79 LP |
638 | if (stat(t->stamp_path, &st) >= 0) { |
639 | usec_t ft; | |
640 | ||
641 | /* Load the file timestamp, but only if it is actually in the past. If it is in the future, | |
642 | * something is wrong with the system clock. */ | |
643 | ||
644 | ft = timespec_load(&st.st_mtim); | |
645 | if (ft < now(CLOCK_REALTIME)) | |
646 | t->last_trigger.realtime = ft; | |
04f5c018 | 647 | else |
77542a79 | 648 | log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.", |
04f5c018 | 649 | FORMAT_TIMESTAMP(ft)); |
77542a79 LP |
650 | |
651 | } else if (errno == ENOENT) | |
2762ce2d | 652 | /* The timer has never run before, make sure a stamp file exists. */ |
4b58153d | 653 | (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID); |
06642d17 LP |
654 | } |
655 | ||
067d72c9 | 656 | t->result = TIMER_SUCCESS; |
aa1f95d2 | 657 | timer_enter_waiting(t, false); |
82a2b6bb | 658 | return 1; |
871d7de4 LP |
659 | } |
660 | ||
661 | static int timer_stop(Unit *u) { | |
662 | Timer *t = TIMER(u); | |
663 | ||
664 | assert(t); | |
3742095b | 665 | assert(IN_SET(t->state, TIMER_WAITING, TIMER_RUNNING, TIMER_ELAPSED)); |
871d7de4 | 666 | |
067d72c9 | 667 | timer_enter_dead(t, TIMER_SUCCESS); |
82a2b6bb | 668 | return 1; |
871d7de4 LP |
669 | } |
670 | ||
671 | static int timer_serialize(Unit *u, FILE *f, FDSet *fds) { | |
672 | Timer *t = TIMER(u); | |
673 | ||
674 | assert(u); | |
675 | assert(f); | |
676 | assert(fds); | |
677 | ||
d68c645b LP |
678 | (void) serialize_item(f, "state", timer_state_to_string(t->state)); |
679 | (void) serialize_item(f, "result", timer_result_to_string(t->result)); | |
871d7de4 | 680 | |
06642d17 | 681 | if (t->last_trigger.realtime > 0) |
d68c645b | 682 | (void) serialize_usec(f, "last-trigger-realtime", t->last_trigger.realtime); |
06642d17 LP |
683 | |
684 | if (t->last_trigger.monotonic > 0) | |
d68c645b | 685 | (void) serialize_usec(f, "last-trigger-monotonic", t->last_trigger.monotonic); |
06642d17 | 686 | |
871d7de4 LP |
687 | return 0; |
688 | } | |
689 | ||
690 | static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
691 | Timer *t = TIMER(u); | |
692 | ||
693 | assert(u); | |
694 | assert(key); | |
695 | assert(value); | |
696 | assert(fds); | |
697 | ||
698 | if (streq(key, "state")) { | |
699 | TimerState state; | |
700 | ||
36697dc0 LP |
701 | state = timer_state_from_string(value); |
702 | if (state < 0) | |
f2341e0a | 703 | log_unit_debug(u, "Failed to parse state value: %s", value); |
871d7de4 LP |
704 | else |
705 | t->deserialized_state = state; | |
d68c645b | 706 | |
067d72c9 LP |
707 | } else if (streq(key, "result")) { |
708 | TimerResult f; | |
709 | ||
710 | f = timer_result_from_string(value); | |
711 | if (f < 0) | |
f2341e0a | 712 | log_unit_debug(u, "Failed to parse result value: %s", value); |
067d72c9 LP |
713 | else if (f != TIMER_SUCCESS) |
714 | t->result = f; | |
06642d17 | 715 | |
d68c645b LP |
716 | } else if (streq(key, "last-trigger-realtime")) |
717 | (void) deserialize_usec(value, &t->last_trigger.realtime); | |
718 | else if (streq(key, "last-trigger-monotonic")) | |
719 | (void) deserialize_usec(value, &t->last_trigger.monotonic); | |
720 | else | |
f2341e0a | 721 | log_unit_debug(u, "Unknown serialization key: %s", key); |
871d7de4 LP |
722 | |
723 | return 0; | |
034c6ed7 LP |
724 | } |
725 | ||
44a6b1b6 | 726 | _pure_ static UnitActiveState timer_active_state(Unit *u) { |
871d7de4 LP |
727 | assert(u); |
728 | ||
729 | return state_translation_table[TIMER(u)->state]; | |
730 | } | |
731 | ||
44a6b1b6 | 732 | _pure_ static const char *timer_sub_state_to_string(Unit *u) { |
871d7de4 LP |
733 | assert(u); |
734 | ||
735 | return timer_state_to_string(TIMER(u)->state); | |
736 | } | |
737 | ||
718db961 LP |
738 | static int timer_dispatch(sd_event_source *s, uint64_t usec, void *userdata) { |
739 | Timer *t = TIMER(userdata); | |
871d7de4 LP |
740 | |
741 | assert(t); | |
5cb5a6ff | 742 | |
871d7de4 | 743 | if (t->state != TIMER_WAITING) |
718db961 | 744 | return 0; |
5cb5a6ff | 745 | |
f2341e0a | 746 | log_unit_debug(UNIT(t), "Timer elapsed."); |
871d7de4 | 747 | timer_enter_running(t); |
718db961 | 748 | return 0; |
5cb5a6ff LP |
749 | } |
750 | ||
3ecaa09b LP |
751 | static void timer_trigger_notify(Unit *u, Unit *other) { |
752 | Timer *t = TIMER(u); | |
871d7de4 | 753 | |
3ecaa09b LP |
754 | assert(u); |
755 | assert(other); | |
871d7de4 | 756 | |
0377cd29 LP |
757 | /* Filter out invocations with bogus state */ |
758 | assert(UNIT_IS_LOAD_COMPLETE(other->load_state)); | |
871d7de4 | 759 | |
3ecaa09b LP |
760 | /* Reenable all timers that depend on unit state */ |
761 | LIST_FOREACH(value, v, t->values) | |
3742095b | 762 | if (IN_SET(v->base, TIMER_UNIT_ACTIVE, TIMER_UNIT_INACTIVE)) |
3ecaa09b | 763 | v->disabled = false; |
01f78473 | 764 | |
3ecaa09b | 765 | switch (t->state) { |
871d7de4 | 766 | |
3ecaa09b LP |
767 | case TIMER_WAITING: |
768 | case TIMER_ELAPSED: | |
871d7de4 | 769 | |
3ecaa09b | 770 | /* Recalculate sleep time */ |
aa1f95d2 | 771 | timer_enter_waiting(t, false); |
3ecaa09b | 772 | break; |
871d7de4 | 773 | |
3ecaa09b | 774 | case TIMER_RUNNING: |
871d7de4 | 775 | |
3ecaa09b | 776 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { |
f2341e0a | 777 | log_unit_debug(UNIT(t), "Got notified about unit deactivation."); |
aa1f95d2 | 778 | timer_enter_waiting(t, false); |
3ecaa09b LP |
779 | } |
780 | break; | |
871d7de4 | 781 | |
3ecaa09b LP |
782 | case TIMER_DEAD: |
783 | case TIMER_FAILED: | |
784 | break; | |
871d7de4 | 785 | |
3ecaa09b | 786 | default: |
04499a70 | 787 | assert_not_reached(); |
871d7de4 | 788 | } |
871d7de4 LP |
789 | } |
790 | ||
fdf20a31 | 791 | static void timer_reset_failed(Unit *u) { |
5632e374 LP |
792 | Timer *t = TIMER(u); |
793 | ||
794 | assert(t); | |
795 | ||
fdf20a31 | 796 | if (t->state == TIMER_FAILED) |
5632e374 LP |
797 | timer_set_state(t, TIMER_DEAD); |
798 | ||
067d72c9 | 799 | t->result = TIMER_SUCCESS; |
5632e374 LP |
800 | } |
801 | ||
8742514c LP |
802 | static void timer_time_change(Unit *u) { |
803 | Timer *t = TIMER(u); | |
13f512d3 | 804 | usec_t ts; |
8742514c LP |
805 | |
806 | assert(u); | |
807 | ||
808 | if (t->state != TIMER_WAITING) | |
809 | return; | |
810 | ||
13f512d3 AJ |
811 | /* If we appear to have triggered in the future, the system clock must |
812 | * have been set backwards. So let's rewind our own clock and allow | |
813 | * the future trigger(s) to happen again :). Exactly the same as when | |
814 | * you start a timer unit with Persistent=yes. */ | |
815 | ts = now(CLOCK_REALTIME); | |
816 | if (t->last_trigger.realtime > ts) | |
817 | t->last_trigger.realtime = ts; | |
818 | ||
efebb613 LP |
819 | if (t->on_clock_change) { |
820 | log_unit_debug(u, "Time change, triggering activation."); | |
821 | timer_enter_running(t); | |
822 | } else { | |
823 | log_unit_debug(u, "Time change, recalculating next elapse."); | |
824 | timer_enter_waiting(t, true); | |
825 | } | |
8742514c LP |
826 | } |
827 | ||
bbf5fd8e LP |
828 | static void timer_timezone_change(Unit *u) { |
829 | Timer *t = TIMER(u); | |
830 | ||
831 | assert(u); | |
832 | ||
833 | if (t->state != TIMER_WAITING) | |
834 | return; | |
835 | ||
efebb613 LP |
836 | if (t->on_timezone_change) { |
837 | log_unit_debug(u, "Timezone change, triggering activation."); | |
838 | timer_enter_running(t); | |
839 | } else { | |
840 | log_unit_debug(u, "Timezone change, recalculating next elapse."); | |
841 | timer_enter_waiting(t, false); | |
842 | } | |
bbf5fd8e LP |
843 | } |
844 | ||
89f6fe7b LP |
845 | static int timer_clean(Unit *u, ExecCleanMask mask) { |
846 | Timer *t = TIMER(u); | |
847 | int r; | |
848 | ||
849 | assert(t); | |
850 | assert(mask != 0); | |
851 | ||
852 | if (t->state != TIMER_DEAD) | |
853 | return -EBUSY; | |
854 | ||
855 | if (!IN_SET(mask, EXEC_CLEAN_STATE)) | |
856 | return -EUNATCH; | |
857 | ||
858 | r = timer_setup_persistent(t); | |
859 | if (r < 0) | |
860 | return r; | |
861 | ||
862 | if (!t->stamp_path) | |
863 | return -EUNATCH; | |
864 | ||
865 | if (unlink(t->stamp_path) && errno != ENOENT) | |
866 | return log_unit_error_errno(u, errno, "Failed to clean stamp file of timer: %m"); | |
867 | ||
868 | return 0; | |
869 | } | |
870 | ||
871 | static int timer_can_clean(Unit *u, ExecCleanMask *ret) { | |
872 | Timer *t = TIMER(u); | |
873 | ||
874 | assert(t); | |
875 | ||
876 | *ret = t->persistent ? EXEC_CLEAN_STATE : 0; | |
877 | return 0; | |
878 | } | |
879 | ||
705578c3 | 880 | static int timer_can_start(Unit *u) { |
9727f242 DDM |
881 | Timer *t = TIMER(u); |
882 | int r; | |
883 | ||
884 | assert(t); | |
885 | ||
886 | r = unit_test_start_limit(u); | |
887 | if (r < 0) { | |
888 | timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT); | |
889 | return r; | |
890 | } | |
891 | ||
705578c3 | 892 | return 1; |
9727f242 DDM |
893 | } |
894 | ||
871d7de4 | 895 | static const char* const timer_base_table[_TIMER_BASE_MAX] = { |
48d83e33 ZJS |
896 | [TIMER_ACTIVE] = "OnActiveSec", |
897 | [TIMER_BOOT] = "OnBootSec", | |
898 | [TIMER_STARTUP] = "OnStartupSec", | |
899 | [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec", | |
36697dc0 | 900 | [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec", |
48d83e33 | 901 | [TIMER_CALENDAR] = "OnCalendar" |
871d7de4 LP |
902 | }; |
903 | ||
904 | DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase); | |
905 | ||
067d72c9 | 906 | static const char* const timer_result_table[_TIMER_RESULT_MAX] = { |
40f41f34 DDM |
907 | [TIMER_SUCCESS] = "success", |
908 | [TIMER_FAILURE_RESOURCES] = "resources", | |
909 | [TIMER_FAILURE_START_LIMIT_HIT] = "start-limit-hit", | |
067d72c9 LP |
910 | }; |
911 | ||
912 | DEFINE_STRING_TABLE_LOOKUP(timer_result, TimerResult); | |
913 | ||
87f0e418 | 914 | const UnitVTable timer_vtable = { |
7d17cfbc | 915 | .object_size = sizeof(Timer), |
718db961 | 916 | |
f975e971 LP |
917 | .sections = |
918 | "Unit\0" | |
919 | "Timer\0" | |
920 | "Install\0", | |
d8a812d1 | 921 | .private_section = "Timer", |
5cb5a6ff | 922 | |
c80a9a33 LP |
923 | .can_transient = true, |
924 | .can_fail = true, | |
925 | .can_trigger = true, | |
926 | ||
871d7de4 | 927 | .init = timer_init, |
034c6ed7 | 928 | .done = timer_done, |
871d7de4 LP |
929 | .load = timer_load, |
930 | ||
931 | .coldplug = timer_coldplug, | |
932 | ||
933 | .dump = timer_dump, | |
934 | ||
935 | .start = timer_start, | |
936 | .stop = timer_stop, | |
937 | ||
89f6fe7b LP |
938 | .clean = timer_clean, |
939 | .can_clean = timer_can_clean, | |
940 | ||
871d7de4 LP |
941 | .serialize = timer_serialize, |
942 | .deserialize_item = timer_deserialize_item, | |
943 | ||
944 | .active_state = timer_active_state, | |
945 | .sub_state_to_string = timer_sub_state_to_string, | |
946 | ||
3ecaa09b LP |
947 | .trigger_notify = timer_trigger_notify, |
948 | ||
fdf20a31 | 949 | .reset_failed = timer_reset_failed, |
8742514c | 950 | .time_change = timer_time_change, |
bbf5fd8e | 951 | .timezone_change = timer_timezone_change, |
5632e374 | 952 | |
d8a812d1 | 953 | .bus_set_property = bus_timer_set_property, |
9727f242 | 954 | |
705578c3 | 955 | .can_start = timer_can_start, |
5cb5a6ff | 956 | }; |