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