/* SPDX-License-Identifier: LGPL-2.1+ */
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
#include <errno.h>
#include "alloc-util.h"
static int timer_verify(Timer *t) {
assert(t);
+ assert(UNIT(t)->load_state == UNIT_LOADED);
- if (UNIT(t)->load_state != UNIT_LOADED)
- return 0;
-
- if (!t->values) {
+ if (!t->values && !t->on_clock_change && !t->on_timezone_change) {
log_unit_error(UNIT(t), "Timer unit lacks value setting. Refusing.");
return -ENOEXEC;
}
if (r < 0)
return r;
- t->stamp_path = strappend("/var/lib/systemd/timers/stamp-", UNIT(t)->id);
+ t->stamp_path = strjoin("/var/lib/systemd/timers/stamp-", UNIT(t)->id);
} else {
const char *e;
assert(u);
assert(u->load_state == UNIT_STUB);
- r = unit_load_fragment_and_dropin(u);
+ r = unit_load_fragment_and_dropin(u, true);
if (r < 0)
return r;
- if (u->load_state == UNIT_LOADED) {
+ if (u->load_state != UNIT_LOADED)
+ return 0;
- r = timer_add_trigger_dependencies(t);
- if (r < 0)
- return r;
+ /* This is a new unit? Then let's add in some extras */
+ r = timer_add_trigger_dependencies(t);
+ if (r < 0)
+ return r;
- r = timer_setup_persistent(t);
- if (r < 0)
- return r;
+ r = timer_setup_persistent(t);
+ if (r < 0)
+ return r;
- r = timer_add_default_dependencies(t);
- if (r < 0)
- return r;
- }
+ r = timer_add_default_dependencies(t);
+ if (r < 0)
+ return r;
return timer_verify(t);
}
"%sPersistent: %s\n"
"%sWakeSystem: %s\n"
"%sAccuracy: %s\n"
- "%sRemainAfterElapse: %s\n",
+ "%sRemainAfterElapse: %s\n"
+ "%sOnClockChange: %s\n"
+ "%sOnTimeZoneChange: %s\n",
prefix, timer_state_to_string(t->state),
prefix, timer_result_to_string(t->result),
prefix, trigger ? trigger->id : "n/a",
prefix, yes_no(t->persistent),
prefix, yes_no(t->wake_system),
prefix, format_timespan(buf, sizeof(buf), t->accuracy_usec, 1),
- prefix, yes_no(t->remain_after_elapse));
+ prefix, yes_no(t->remain_after_elapse),
+ prefix, yes_no(t->on_clock_change),
+ prefix, yes_no(t->on_timezone_change));
LIST_FOREACH(value, v, t->values) {
case TIMER_UNIT_ACTIVE:
leave_around = true;
- base = trigger->inactive_exit_timestamp.monotonic;
-
- if (base <= 0)
- base = t->last_trigger.monotonic;
-
+ base = MAX(trigger->inactive_exit_timestamp.monotonic, t->last_trigger.monotonic);
if (base <= 0)
continue;
- base = MAX(base, t->last_trigger.monotonic);
-
break;
case TIMER_UNIT_INACTIVE:
leave_around = true;
- base = trigger->inactive_enter_timestamp.monotonic;
-
- if (base <= 0)
- base = t->last_trigger.monotonic;
-
+ base = MAX(trigger->inactive_enter_timestamp.monotonic, t->last_trigger.monotonic);
if (base <= 0)
continue;
- base = MAX(base, t->last_trigger.monotonic);
-
break;
default:
}
}
- if (!found_monotonic && !found_realtime) {
+ if (!found_monotonic && !found_realtime && !t->on_timezone_change && !t->on_clock_change) {
log_unit_debug(UNIT(t), "Timer is elapsed.");
timer_enter_elapsed(t, leave_around);
return;
return;
}
- r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, &error, NULL);
+ r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, NULL, &error, NULL);
if (r < 0)
goto fail;
static int timer_start(Unit *u) {
Timer *t = TIMER(u);
TimerValue *v;
- Unit *trigger;
int r;
assert(t);
assert(IN_SET(t->state, TIMER_DEAD, TIMER_FAILED));
- trigger = UNIT_TRIGGER(u);
- if (!trigger || trigger->load_state != UNIT_LOADED) {
- log_unit_error(u, "Refusing to start, unit to trigger not loaded.");
- return -ENOENT;
- }
+ r = unit_test_trigger_loaded(u);
+ if (r < 0)
+ return r;
- r = unit_start_limit_test(u);
+ r = unit_test_start_limit(u);
if (r < 0) {
timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
return r;
if (t->last_trigger.realtime > ts)
t->last_trigger.realtime = ts;
- log_unit_debug(u, "Time change, recalculating next elapse.");
- timer_enter_waiting(t, true);
+ if (t->on_clock_change) {
+ log_unit_debug(u, "Time change, triggering activation.");
+ timer_enter_running(t);
+ } else {
+ log_unit_debug(u, "Time change, recalculating next elapse.");
+ timer_enter_waiting(t, true);
+ }
}
static void timer_timezone_change(Unit *u) {
if (t->state != TIMER_WAITING)
return;
- log_unit_debug(u, "Timezone change, recalculating next elapse.");
- timer_enter_waiting(t, false);
+ if (t->on_timezone_change) {
+ log_unit_debug(u, "Timezone change, triggering activation.");
+ timer_enter_running(t);
+ } else {
+ log_unit_debug(u, "Timezone change, recalculating next elapse.");
+ timer_enter_waiting(t, false);
+ }
+}
+
+static int timer_clean(Unit *u, ExecCleanMask mask) {
+ Timer *t = TIMER(u);
+ int r;
+
+ assert(t);
+ assert(mask != 0);
+
+ if (t->state != TIMER_DEAD)
+ return -EBUSY;
+
+ if (!IN_SET(mask, EXEC_CLEAN_STATE))
+ return -EUNATCH;
+
+ r = timer_setup_persistent(t);
+ if (r < 0)
+ return r;
+
+ if (!t->stamp_path)
+ return -EUNATCH;
+
+ if (unlink(t->stamp_path) && errno != ENOENT)
+ return log_unit_error_errno(u, errno, "Failed to clean stamp file of timer: %m");
+
+ return 0;
+}
+
+static int timer_can_clean(Unit *u, ExecCleanMask *ret) {
+ Timer *t = TIMER(u);
+
+ assert(t);
+
+ *ret = t->persistent ? EXEC_CLEAN_STATE : 0;
+ return 0;
}
static const char* const timer_base_table[_TIMER_BASE_MAX] = {
"Install\0",
.private_section = "Timer",
+ .can_transient = true,
+ .can_fail = true,
+ .can_trigger = true,
+
.init = timer_init,
.done = timer_done,
.load = timer_load,
.start = timer_start,
.stop = timer_stop,
+ .clean = timer_clean,
+ .can_clean = timer_can_clean,
+
.serialize = timer_serialize,
.deserialize_item = timer_deserialize_item,
.bus_vtable = bus_timer_vtable,
.bus_set_property = bus_timer_set_property,
-
- .can_transient = true,
};