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