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