summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-11-23 21:37:58 +0100
committerLennart Poettering <lennart@poettering.net>2012-11-23 21:37:58 +0100
commit36697dc0199e25f09b78090fcf5f1cf8a3648ffd (patch)
tree7caef3bc1761327c366fc4a93a138e53fc692712
parent8a1175118e7a2e60a6ec42624f915e26e821f4e8 (diff)
timer: implement calendar time events
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am16
-rw-r--r--src/core/dbus-timer.c6
-rw-r--r--src/core/load-fragment-gperf.gperf.m41
-rw-r--r--src/core/load-fragment.c29
-rw-r--r--src/core/mount.c12
-rw-r--r--src/core/service.c13
-rw-r--r--src/core/socket.c15
-rw-r--r--src/core/swap.c6
-rw-r--r--src/core/timer.c196
-rw-r--r--src/core/timer.h16
-rw-r--r--src/core/unit.c10
-rw-r--r--src/core/unit.h2
-rw-r--r--src/shared/calendarspec.c927
-rw-r--r--src/shared/calendarspec.h57
-rw-r--r--src/test/test-calendarspec.c84
16 files changed, 1288 insertions, 103 deletions
diff --git a/.gitignore b/.gitignore
index 2291e5d20f..70cadf8756 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+/test-calendarspec
/test-catalog
/test-replace-var
/test-journal-enum
diff --git a/Makefile.am b/Makefile.am
index 804cc04e20..51bd7c7dec 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -833,7 +833,9 @@ libsystemd_shared_la_SOURCES = \
src/shared/hwclock.c \
src/shared/hwclock.h \
src/shared/time-dst.c \
- src/shared/time-dst.h
+ src/shared/time-dst.h \
+ src/shared/calendarspec.c \
+ src/shared/calendarspec.h
#-------------------------------------------------------------------------------
noinst_LTLIBRARIES += \
@@ -1218,7 +1220,8 @@ noinst_PROGRAMS += \
test-date \
test-sleep \
test-replace-var \
- test-sched-prio
+ test-sched-prio \
+ test-calendarspec
TESTS += \
test-job-type \
@@ -1229,7 +1232,8 @@ TESTS += \
test-date \
test-sleep \
test-replace-var \
- test-sched-prio
+ test-sched-prio \
+ test-calendarspec
EXTRA_DIST += \
test/sched_idle_bad.service \
@@ -1320,6 +1324,12 @@ test_replace_var_SOURCES = \
test_replace_var_LDADD = \
libsystemd-shared.la
+test_calendarspec_SOURCES = \
+ src/test/test-calendarspec.c
+
+test_calendarspec_LDADD = \
+ libsystemd-shared.la
+
test_daemon_SOURCES = \
src/test/test-daemon.c
diff --git a/src/core/dbus-timer.c b/src/core/dbus-timer.c
index 84b823c9a4..11d18cbd83 100644
--- a/src/core/dbus-timer.c
+++ b/src/core/dbus-timer.c
@@ -79,7 +79,8 @@ static int bus_timer_append_timers(DBusMessageIter *i, const char *property, voi
/* s/Sec/USec/ */
l = strlen(t);
- if (!(buf = new(char, l+2)))
+ buf = new(char, l+2);
+ if (!buf)
return -ENOMEM;
memcpy(buf, t, l-3);
@@ -121,7 +122,8 @@ static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_timer_append_timer_result, timer_resu
static const BusProperty bus_timer_properties[] = {
{ "Unit", bus_timer_append_unit, "s", 0 },
{ "Timers", bus_timer_append_timers, "a(stt)", 0 },
- { "NextElapseUSec", bus_property_append_usec, "t", offsetof(Timer, next_elapse) },
+ { "NextElapseUSec", bus_property_append_usec, "t", offsetof(Timer, next_elapse_monotonic) },
+ { "NextElapseUSecRealtime", bus_property_append_usec, "t", offsetof(Timer, next_elapse_realtime) },
{ "Result", bus_timer_append_timer_result,"s", offsetof(Timer, result) },
{ NULL, }
};
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index be16ba93c8..7212053ca7 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -234,6 +234,7 @@ Swap.TimeoutSec, config_parse_usec, 0,
EXEC_CONTEXT_CONFIG_ITEMS(Swap)m4_dnl
KILL_CONTEXT_CONFIG_ITEMS(Swap)m4_dnl
m4_dnl
+Timer.OnCalendar, config_parse_timer, 0, 0
Timer.OnActiveSec, config_parse_timer, 0, 0
Timer.OnBootSec, config_parse_timer, 0, 0
Timer.OnStartupSec, config_parse_timer, 0, 0
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 6933e1a21e..e35fdbc5ec 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -1136,30 +1136,47 @@ int config_parse_timer(
void *userdata) {
Timer *t = data;
- usec_t u;
+ usec_t u = 0;
TimerValue *v;
TimerBase b;
+ CalendarSpec *c = NULL;
+ clockid_t id;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
- if ((b = timer_base_from_string(lvalue)) < 0) {
+ b = timer_base_from_string(lvalue);
+ if (b < 0) {
log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
return 0;
}
- if (parse_usec(rvalue, &u) < 0) {
- log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
- return 0;
+ if (b == TIMER_CALENDAR) {
+ if (calendar_spec_from_string(rvalue, &c) < 0) {
+ log_error("[%s:%u] Failed to parse calendar specification, ignoring: %s", filename, line, rvalue);
+ return 0;
+ }
+
+ id = CLOCK_REALTIME;
+ } else {
+ if (parse_usec(rvalue, &u) < 0) {
+ log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
+ return 0;
+ }
+
+ id = CLOCK_MONOTONIC;
}
- if (!(v = new0(TimerValue, 1)))
+ v = new0(TimerValue, 1);
+ if (!v)
return -ENOMEM;
v->base = b;
+ v->clock_id = id;
v->value = u;
+ v->calendar_spec = c;
LIST_PREPEND(TimerValue, value, t->values, v);
diff --git a/src/core/mount.c b/src/core/mount.c
index 14f4863dc6..09a5d286d3 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -741,10 +741,12 @@ static int mount_coldplug(Unit *u) {
if (m->control_pid <= 0)
return -EBADMSG;
- if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0)
+ r = unit_watch_pid(UNIT(m), m->control_pid);
+ if (r < 0)
return r;
- if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
+ if (r < 0)
return r;
}
@@ -800,7 +802,8 @@ static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
assert(c);
assert(_pid);
- if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
+ if (r < 0)
goto fail;
if ((r = exec_spawn(c,
@@ -900,7 +903,8 @@ static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
}
if (wait_for_exit) {
- if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(m), CLOCK_MONOTONIC, true, m->timeout_usec, &m->timer_watch);
+ if (r < 0)
goto fail;
mount_set_state(m, state);
diff --git a/src/core/service.c b/src/core/service.c
index 34d24ffa92..25a568f70b 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -249,7 +249,7 @@ static void service_handle_watchdog(Service *s) {
return;
}
- r = unit_watch_timer(UNIT(s), s->watchdog_usec - offset, &s->watchdog_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
if (r < 0)
log_warning("%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r));
}
@@ -1599,7 +1599,8 @@ static int service_coldplug(Unit *u) {
k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
- if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
+ if (r < 0)
return r;
}
}
@@ -1744,7 +1745,7 @@ static int service_spawn(
}
if (timeout && s->timeout_start_usec) {
- r = unit_watch_timer(UNIT(s), s->timeout_start_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
if (r < 0)
goto fail;
} else
@@ -1899,7 +1900,7 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
!set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
) {
- r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
if (r < 0)
goto fail;
@@ -2012,7 +2013,7 @@ static void service_enter_signal(Service *s, ServiceState state, ServiceResult f
if (wait_for_exit) {
if (s->timeout_stop_usec > 0) {
- r = unit_watch_timer(UNIT(s), s->timeout_stop_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
if (r < 0)
goto fail;
}
@@ -2262,7 +2263,7 @@ static void service_enter_restart(Service *s) {
/* Don't restart things if we are going down anyway */
log_info("Stop job pending for unit, delaying automatic restart.");
- r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
if (r < 0)
goto fail;
diff --git a/src/core/socket.c b/src/core/socket.c
index 3d5791b114..9b5bcb6e7b 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -1146,10 +1146,12 @@ static int socket_coldplug(Unit *u) {
if (s->control_pid <= 0)
return -EBADMSG;
- if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
+ r = unit_watch_pid(UNIT(s), s->control_pid);
+ if (r < 0)
return r;
- if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
+ if (r < 0)
return r;
}
@@ -1181,10 +1183,12 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
assert(c);
assert(_pid);
- if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
+ if (r < 0)
goto fail;
- if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
+ argv = unit_full_printf_strv(UNIT(s), c->argv);
+ if (!argv) {
r = -ENOMEM;
goto fail;
}
@@ -1306,7 +1310,8 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
}
if (wait_for_exit) {
- if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
+ if (r < 0)
goto fail;
socket_set_state(s, state);
diff --git a/src/core/swap.c b/src/core/swap.c
index 97145a9974..bd40516c99 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -521,7 +521,7 @@ static int swap_coldplug(Unit *u) {
if (r < 0)
return r;
- r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
if (r < 0)
return r;
}
@@ -584,7 +584,7 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
assert(c);
assert(_pid);
- r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
if (r < 0)
goto fail;
@@ -689,7 +689,7 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
}
if (wait_for_exit) {
- r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch);
+ r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_usec, &s->timer_watch);
if (r < 0)
goto fail;
diff --git a/src/core/timer.c b/src/core/timer.c
index 7080b32c6b..54c0d0b330 100644
--- a/src/core/timer.c
+++ b/src/core/timer.c
@@ -42,7 +42,10 @@ static void timer_init(Unit *u) {
assert(u);
assert(u->load_state == UNIT_STUB);
- t->next_elapse = (usec_t) -1;
+ t->next_elapse_monotonic = (usec_t) -1;
+ t->next_elapse_realtime = (usec_t) -1;
+ watch_init(&t->monotonic_watch);
+ watch_init(&t->realtime_watch);
}
static void timer_done(Unit *u) {
@@ -53,10 +56,15 @@ static void timer_done(Unit *u) {
while ((v = t->values)) {
LIST_REMOVE(TimerValue, value, t->values, v);
+
+ if (v->calendar_spec)
+ calendar_spec_free(v->calendar_spec);
+
free(v);
}
- unit_unwatch_timer(u, &t->timer_watch);
+ unit_unwatch_timer(u, &t->monotonic_watch);
+ unit_unwatch_timer(u, &t->realtime_watch);
unit_ref_unset(&t->unit);
}
@@ -81,10 +89,12 @@ static int timer_add_default_dependencies(Timer *t) {
assert(t);
if (UNIT(t)->manager->running_as == SYSTEMD_SYSTEM) {
- if ((r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
+ r = unit_add_dependency_by_name(UNIT(t), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true);
+ if (r < 0)
return r;
- if ((r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
+ r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
+ if (r < 0)
return r;
}
@@ -98,7 +108,8 @@ static int timer_load(Unit *u) {
assert(u);
assert(u->load_state == UNIT_STUB);
- if ((r = unit_load_fragment_and_dropin(u)) < 0)
+ r = unit_load_fragment_and_dropin(u);
+ if (r < 0)
return r;
if (u->load_state == UNIT_LOADED) {
@@ -117,9 +128,11 @@ static int timer_load(Unit *u) {
if (r < 0)
return r;
- if (UNIT(t)->default_dependencies)
- if ((r = timer_add_default_dependencies(t)) < 0)
+ if (UNIT(t)->default_dependencies) {
+ r = timer_add_default_dependencies(t);
+ if (r < 0)
return r;
+ }
}
return timer_verify(t);
@@ -128,8 +141,6 @@ static int timer_load(Unit *u) {
static void timer_dump(Unit *u, FILE *f, const char *prefix) {
Timer *t = TIMER(u);
TimerValue *v;
- char
- timespan1[FORMAT_TIMESPAN_MAX];
fprintf(f,
"%sTimer State: %s\n"
@@ -139,12 +150,28 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) {
prefix, timer_result_to_string(t->result),
prefix, UNIT_DEREF(t->unit)->id);
- LIST_FOREACH(value, v, t->values)
- fprintf(f,
- "%s%s: %s\n",
- prefix,
- timer_base_to_string(v->base),
- strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
+ LIST_FOREACH(value, v, t->values) {
+
+ if (v->base == TIMER_CALENDAR) {
+ _cleanup_free_ char *p = NULL;
+
+ calendar_spec_to_string(v->calendar_spec, &p);
+
+ fprintf(f,
+ "%s%s: %s\n",
+ prefix,
+ timer_base_to_string(v->base),
+ strna(p));
+ } else {
+ char timespan1[FORMAT_TIMESPAN_MAX];
+
+ fprintf(f,
+ "%s%s: %s\n",
+ prefix,
+ timer_base_to_string(v->base),
+ strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
+ }
+ }
}
static void timer_set_state(Timer *t, TimerState state) {
@@ -154,8 +181,10 @@ static void timer_set_state(Timer *t, TimerState state) {
old_state = t->state;
t->state = state;
- if (state != TIMER_WAITING)
- unit_unwatch_timer(UNIT(t), &t->timer_watch);
+ if (state != TIMER_WAITING) {
+ unit_unwatch_timer(UNIT(t), &t->monotonic_watch);
+ unit_unwatch_timer(UNIT(t), &t->realtime_watch);
+ }
if (state != old_state)
log_debug("%s changed %s -> %s",
@@ -196,79 +225,117 @@ static void timer_enter_dead(Timer *t, TimerResult f) {
static void timer_enter_waiting(Timer *t, bool initial) {
TimerValue *v;
- usec_t base = 0, delay, n;
- bool found = false;
+ usec_t base = 0;
+ dual_timestamp ts;
+ bool found_monotonic = false, found_realtime = false;
int r;
- n = now(CLOCK_MONOTONIC);
+ dual_timestamp_get(&ts);
+ t->next_elapse_monotonic = t->next_elapse_realtime = 0;
LIST_FOREACH(value, v, t->values) {
if (v->disabled)
continue;
- switch (v->base) {
+ if (v->base == TIMER_CALENDAR) {
+
+ r = calendar_spec_next_usec(v->calendar_spec, ts.realtime, &v->next_elapse);
+ if (r < 0)
+ continue;
+
+ if (!initial && v->next_elapse < ts.realtime) {
+ v->disabled = true;
+ continue;
+ }
- case TIMER_ACTIVE:
- if (state_translation_table[t->state] == UNIT_ACTIVE)
- base = UNIT(t)->inactive_exit_timestamp.monotonic;
+ if (!found_realtime)
+ t->next_elapse_realtime = v->next_elapse;
else
- base = n;
- break;
+ t->next_elapse_realtime = MIN(t->next_elapse_realtime, v->next_elapse);
- case TIMER_BOOT:
- /* CLOCK_MONOTONIC equals the uptime on Linux */
- base = 0;
- break;
+ found_realtime = true;
- case TIMER_STARTUP:
- base = UNIT(t)->manager->userspace_timestamp.monotonic;
- break;
+ } else {
+ switch (v->base) {
- case TIMER_UNIT_ACTIVE:
+ case TIMER_ACTIVE:
+ if (state_translation_table[t->state] == UNIT_ACTIVE)
+ base = UNIT(t)->inactive_exit_timestamp.monotonic;
+ else
+ base = ts.monotonic;
+ break;
- if (UNIT_DEREF(t->unit)->inactive_exit_timestamp.monotonic <= 0)
- continue;
+ case TIMER_BOOT:
+ /* CLOCK_MONOTONIC equals the uptime on Linux */
+ base = 0;
+ break;
- base = UNIT_DEREF(t->unit)->inactive_exit_timestamp.monotonic;
- break;
+ case TIMER_STARTUP:
+ base = UNIT(t)->manager->userspace_timestamp.monotonic;
+ break;
- case TIMER_UNIT_INACTIVE:
+ case TIMER_UNIT_ACTIVE:
- if (UNIT_DEREF(t->unit)->inactive_enter_timestamp.monotonic <= 0)
- continue;
+ if (UNIT_DEREF(t->unit)->inactive_exit_timestamp.monotonic <= 0)
+ continue;
- base = UNIT_DEREF(t->unit)->inactive_enter_timestamp.monotonic;
- break;
+ base = UNIT_DEREF(t->unit)->inactive_exit_timestamp.monotonic;
+ break;
- default:
- assert_not_reached("Unknown timer base");
- }
+ case TIMER_UNIT_INACTIVE:
- v->next_elapse = base + v->value;
+ if (UNIT_DEREF(t->unit)->inactive_enter_timestamp.monotonic <= 0)
+ continue;
- if (!initial && v->next_elapse < n) {
- v->disabled = true;
- continue;
- }
+ base = UNIT_DEREF(t->unit)->inactive_enter_timestamp.monotonic;
+ break;
- if (!found)
- t->next_elapse = v->next_elapse;
- else
- t->next_elapse = MIN(t->next_elapse, v->next_elapse);
+ default:
+ assert_not_reached("Unknown timer base");
+ }
- found = true;
+ v->next_elapse = base + v->value;
+
+ if (!initial && v->next_elapse < ts.monotonic) {
+ v->disabled = true;
+ continue;
+ }
+
+ if (!found_monotonic)
+ t->next_elapse_monotonic = v->next_elapse;
+ else
+ t->next_elapse_monotonic = MIN(t->next_elapse_monotonic, v->next_elapse);
+
+ found_monotonic = true;
+ }
}
- if (!found) {
+ if (!found_monotonic && !found_realtime) {
+ log_debug("%s: Timer is elapsed.", UNIT(t)->id);
timer_set_state(t, TIMER_ELAPSED);
return;
}
- delay = n < t->next_elapse ? t->next_elapse - n : 0;
+ if (found_monotonic) {
+ char buf[FORMAT_TIMESPAN_MAX];
+ log_debug("%s: Monotonic timer elapses in %s the next time.", UNIT(t)->id, format_timespan(buf, sizeof(buf), t->next_elapse_monotonic - ts.monotonic));
- if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
- goto fail;
+ r = unit_watch_timer(UNIT(t), CLOCK_MONOTONIC, false, t->next_elapse_monotonic, &t->monotonic_watch);
+ if (r < 0)
+ goto fail;
+ } else
+ unit_unwatch_timer(UNIT(t), &t->monotonic_watch);
+
+ if (found_realtime) {
+ char buf[FORMAT_TIMESTAMP_MAX];
+ log_debug("%s: Realtime timer elapses at %s the next time.", UNIT(t)->id, format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
+
+ r = unit_watch_timer(UNIT(t), CLOCK_REALTIME, false, t->next_elapse_realtime, &t->realtime_watch);
+ if (r < 0)
+ goto fail;
+ } else
+ unit_unwatch_timer(UNIT(t), &t->realtime_watch);
timer_set_state(t, TIMER_WAITING);
return;
@@ -289,7 +356,8 @@ static void timer_enter_running(Timer *t) {
if (UNIT(t)->job && UNIT(t)->job->type == JOB_STOP)
return;
- if ((r = manager_add_job(UNIT(t)->manager, JOB_START, UNIT_DEREF(t->unit), JOB_REPLACE, true, &error, NULL)) < 0)
+ r = manager_add_job(UNIT(t)->manager, JOB_START, UNIT_DEREF(t->unit), JOB_REPLACE, true, &error, NULL);
+ if (r < 0)
goto fail;
timer_set_state(t, TIMER_RUNNING);
@@ -350,7 +418,8 @@ static int timer_deserialize_item(Unit *u, const char *key, const char *value, F
if (streq(key, "state")) {
TimerState state;
- if ((state = timer_state_from_string(value)) < 0)
+ state = timer_state_from_string(value);
+ if (state < 0)
log_debug("Failed to parse state value %s", value);
else
t->deserialized_state = state;
@@ -473,7 +542,8 @@ static const char* const timer_base_table[_TIMER_BASE_MAX] = {
[TIMER_BOOT] = "OnBootSec",
[TIMER_STARTUP] = "OnStartupSec",
[TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
- [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec"
+ [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
+ [TIMER_CALENDAR] = "OnCalendar"
};
DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
diff --git a/src/core/timer.h b/src/core/timer.h
index c6d1d42e44..57a514a68c 100644
--- a/src/core/timer.h
+++ b/src/core/timer.h
@@ -24,6 +24,7 @@
typedef struct Timer Timer;
#include "unit.h"
+#include "calendarspec.h"
typedef enum TimerState {
TIMER_DEAD,
@@ -41,18 +42,21 @@ typedef enum TimerBase {
TIMER_STARTUP,
TIMER_UNIT_ACTIVE,
TIMER_UNIT_INACTIVE,
+ TIMER_CALENDAR,
_TIMER_BASE_MAX,
_TIMER_BASE_INVALID = -1
} TimerBase;
typedef struct TimerValue {
+ TimerBase base;
+ bool disabled;
+ clockid_t clock_id;
+
usec_t value;
+ CalendarSpec *calendar_spec;
usec_t next_elapse;
LIST_FIELDS(struct TimerValue, value);
-
- TimerBase base;
- bool disabled;
} TimerValue;
typedef enum TimerResult {
@@ -66,12 +70,14 @@ struct Timer {
Unit meta;
LIST_HEAD(TimerValue, values);
- usec_t next_elapse;
+ usec_t next_elapse_monotonic;
+ usec_t next_elapse_realtime;
TimerState state, deserialized_state;
UnitRef unit;
- Watch timer_watch;
+ Watch monotonic_watch;
+ Watch realtime_watch;
TimerResult result;
};
diff --git a/src/core/unit.c b/src/core/unit.c
index 82dd617e35..45453dce64 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -1560,7 +1560,7 @@ void unit_unwatch_pid(Unit *u, pid_t pid) {
hashmap_remove_value(u->manager->watch_pids, LONG_TO_PTR(pid), u);
}
-int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
+int unit_watch_timer(Unit *u, clockid_t clock_id, bool relative, usec_t usec, Watch *w) {
struct itimerspec its;
int flags, fd;
bool ours;
@@ -1580,7 +1580,7 @@ int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
} else if (w->type == WATCH_INVALID) {
ours = true;
- fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
+ fd = timerfd_create(clock_id, TFD_NONBLOCK|TFD_CLOEXEC);
if (fd < 0)
return -errno;
} else
@@ -1588,7 +1588,7 @@ int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
zero(its);
- if (delay <= 0) {
+ if (usec <= 0) {
/* Set absolute time in the past, but not 0, since we
* don't want to disarm the timer */
its.it_value.tv_sec = 0;
@@ -1596,8 +1596,8 @@ int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
flags = TFD_TIMER_ABSTIME;
} else {
- timespec_store(&its.it_value, delay);
- flags = 0;
+ timespec_store(&its.it_value, usec);
+ flags = relative ? 0 : TFD_TIMER_ABSTIME;
}
/* This will also flush the elapse counter */
diff --git a/src/core/unit.h b/src/core/unit.h
index bf961c2aac..11804d8bcf 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -484,7 +484,7 @@ void unit_unwatch_fd(Unit *u, Watch *w);
int unit_watch_pid(Unit *u, pid_t pid);
void unit_unwatch_pid(Unit *u, pid_t pid);
-int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
+int unit_watch_timer(Unit *u, clockid_t, bool relative, usec_t usec, Watch *w);
void unit_unwatch_timer(Unit *u, Watch *w);
int unit_watch_bus_name(Unit *u, const char *name);
diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c
new file mode 100644
index 0000000000..3cddb0eed0
--- /dev/null
+++ b/src/shared/calendarspec.c
@@ -0,0 +1,927 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2012 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "calendarspec.h"
+
+static void free_chain(CalendarComponent *c) {
+ CalendarComponent *n;
+
+ while (c) {
+ n = c->next;
+ free(c);
+ c = n;
+ }
+}
+
+void calendar_spec_free(CalendarSpec *c) {
+ assert(c);
+
+ free_chain(c->year);
+ free_chain(c->month);
+ free_chain(c->day);
+ free_chain(c->hour);
+ free_chain(c->minute);
+ free_chain(c->second);
+
+ free(c);
+}
+
+static int component_compare(const void *_a, const void *_b) {
+ CalendarComponent * const *a = _a, * const *b = _b;
+
+ if ((*a)->value < (*b)->value)
+ return -1;
+ if ((*a)->value > (*b)->value)
+ return 1;
+
+ if ((*a)->repeat < (*b)->repeat)
+ return -1;
+ if ((*a)->repeat > (*b)->repeat)
+ return 1;
+
+ return 0;
+}
+
+static void sort_chain(CalendarComponent **c) {
+ unsigned n = 0, k;
+ CalendarComponent **b, *i, **j, *next;
+
+ assert(c);
+
+ for (i = *c; i; i = i->next)
+ n++;
+
+ if (n <= 1)
+ return;
+
+ j = b = alloca(sizeof(CalendarComponent*) * n);
+ for (i = *c; i; i = i->next)
+ *(j++) = i;
+
+ qsort(b, n, sizeof(CalendarComponent*), component_compare);
+
+ b[n-1]->next = NULL;
+ next = b[n-1];
+
+ /* Drop non-unique entries */
+ for (k = n-1; k > 0; k--) {
+ if (b[k-1]->value == next->value &&
+ b[k-1]->repeat == next->repeat) {
+ free(b[k-1]);
+ continue;
+ }
+
+ b[k-1]->next = next;
+ next = b[k-1];
+ }
+
+ *c = next;
+}
+
+static void fix_year(CalendarComponent *c) {
+ /* Turns 12 → 2012, 89 → 1989 */
+
+ while(c) {
+ CalendarComponent *n = c->next;
+
+ if (c->value >= 0 && c->value < 70)
+ c->value += 2000;
+
+ if (c->value >= 70 && c->value < 100)
+ c->value += 1900;
+
+ c = n;
+ }
+}
+
+int calendar_spec_normalize(CalendarSpec *c) {
+ assert(c);
+
+ if (c->weekdays_bits <= 0 || c->weekdays_bits >= 127)
+ c->weekdays_bits = -1;
+
+ fix_year(c->year);
+
+ sort_chain(&c->year);
+ sort_chain(&c->month);
+ sort_chain(&c->day);
+ sort_chain(&c->hour);
+ sort_chain(&c->minute);
+ sort_chain(&c->second);
+
+ return 0;
+}
+
+static bool chain_valid(CalendarComponent *c, int from, int to) {
+ if (!c)
+ return true;
+
+ if (c->value < from || c->value > to)
+ return false;
+
+ if (c->value + c->repeat > to)
+ return false;
+
+ if (c->next)
+ return chain_valid(c->next, from, to);
+
+ return true;
+}
+
+bool calendar_spec_valid(CalendarSpec *c) {
+ assert(c);
+
+ if (c->weekdays_bits > 127)
+ return false;
+
+ if (!chain_valid(c->year, 1970, 2199))
+ return false;
+
+ if (!chain_valid(c->month, 1, 12))
+ return false;
+
+ if (!chain_valid(c->day, 1, 31))
+ return false;
+
+ if (!chain_valid(c->hour, 0, 23))
+ return false;
+
+ if (!chain_valid(c->minute, 0, 59))
+ return false;
+
+ if (!chain_valid(c->second, 0, 59))
+ return false;
+
+ return true;
+}
+
+static void format_weekdays(FILE *f, const CalendarSpec *c) {
+ static const char *const days[] = {
+ "Mon",
+ "Tue",
+ "Wed",
+ "Thu",
+ "Fri",
+ "Sat",
+ "Sun"
+ };
+
+ int l, x;
+ bool need_colon = false;
+
+ assert(f);
+ assert(c);
+ assert(c->weekdays_bits > 0 && c->weekdays_bits <= 127);
+
+ for (x = 0, l = -1; x < (int) ELEMENTSOF(days); x++) {
+
+ if (c->weekdays_bits & (1 << x)) {
+
+ if (l < 0) {
+ if (need_colon)
+ fputc(',', f);
+ else
+ need_colon = true;
+
+ fputs(days[x], f);
+ l = x;
+ }
+
+ } else if (l >= 0) {
+
+ if (x > l + 1) {
+ fputc(x > l + 2 ? '-' : ',', f);
+ fputs(days[x-1], f);
+ }
+
+ l = -1;
+ }
+ }
+
+ if (l >= 0 && x > l + 1) {
+ fputc(x > l + 2 ? '-' : ',', f);
+ fputs(days[x-1], f);
+ }
+}
+
+static void format_chain(FILE *f, int space, const CalendarComponent *c) {
+ assert(f);
+
+ if (!c) {
+ fputc('*', f);
+ return;
+ }
+
+ assert(c->value >= 0);
+ fprintf(f, "%0*i", space, c->value);
+
+ if (c->repeat > 0)
+ fprintf(f, "/%i", c->repeat);
+
+ if (c->next) {
+ fputc(',', f);
+ format_chain(f, space, c->next);
+ }
+}
+
+int calendar_spec_to_string(const CalendarSpec *c, char **p) {
+ char *buf = NULL;
+ size_t sz = 0;
+ FILE *f;
+
+ assert(c);
+ assert(p);
+
+ f = open_memstream(&buf, &sz);
+ if (!f)
+ return -ENOMEM;
+
+ if (c->weekdays_bits > 0 && c->weekdays_bits <= 127) {
+ format_weekdays(f, c);
+ fputc(' ', f);
+ }
+
+ format_chain(f, 4, c->year);
+ fputc('-', f);
+ format_chain(f, 2, c->month);
+ fputc('-', f);
+ format_chain(f, 2, c->day);
+ fputc(' ', f);
+ format_chain(f, 2, c->hour);
+ fputc(':', f);
+ format_chain(f, 2, c->minute);
+ fputc(':', f);
+ format_chain(f, 2, c->second);
+
+ fflush(f);
+
+ if (ferror(f)) {
+ free(buf);
+ fclose(f);
+ return -ENOMEM;
+ }
+
+ fclose(f);
+
+ *p = buf;
+ return 0;
+}
+
+static int parse_weekdays(const char **p, CalendarSpec *c) {
+ static const struct {
+ const char *name;
+ const int nr;
+ } day_nr[] = {
+ { "Monday", 0 },
+ { "Mon", 0 },
+ { "Tuesday", 1 },
+ { "Tue", 1 },
+ { "Wednesday", 2 },
+ { "Wed", 2 },
+ { "Thursday", 3 },
+ { "Thu", 3 },
+ { "Friday", 4 },
+ { "Fri", 4 },
+ { "Saturday", 5 },
+ { "Sat", 5 },
+ { "Sunday", 6 },
+ { "Sun", 6 }
+ };
+
+ int l = -1;
+ bool first = true;
+
+ assert(p);
+ assert(*p);
+ assert(c);
+
+ for (;;) {
+ unsigned i;
+
+ if (!first && **p == ' ')
+ return 0;
+
+ for (i = 0; i < ELEMENTSOF(day_nr); i++) {
+ size_t skip;
+
+ if (!startswith_no_case(*p, day_nr[i].name))
+ continue;
+
+ skip = strlen(day_nr[i].name);
+
+ if ((*p)[skip] != '-' &&
+ (*p)[skip] != ',' &&
+ (*p)[skip] != ' ' &&
+ (*p)[skip] != 0)
+ return -EINVAL;
+
+ c->weekdays_bits |= 1 << day_nr[i].nr;
+
+ if (l >= 0) {
+ int j;
+
+ if (l > day_nr[i].nr)
+ return -EINVAL;
+
+ for (j = l + 1; j < day_nr[i].nr; j++)
+ c->weekdays_bits |= 1 << j;
+ }
+
+ *p += skip;
+ break;
+ }
+
+ /* Couldn't find this prefix, so let's assume the
+ weekday was not specified and let's continue with
+ the date */
+ if (i >= ELEMENTSOF(day_nr))
+ return first ? 0 : -EINVAL;
+
+ /* We reached the end of the string */
+ if (**p == 0)
+ return 0;
+
+ /* We reached the end of the weekday spec part */
+ if (**p == ' ') {
+ *p += strspn(*p, " ");
+ return 0;
+ }
+
+ if (**p == '-') {
+ if (l >= 0)
+ return -EINVAL;
+
+ l = day_nr[i].nr;
+ } else
+ l = -1;
+
+ *p += 1;
+ first = false;
+ }
+}
+
+static int prepend_component(const char **p, CalendarComponent **c) {
+ unsigned long value, repeat = 0;
+ char *e = NULL, *ee = NULL;
+ CalendarComponent *cc;
+
+ assert(p);
+ assert(c);
+
+ errno = 0;
+ value = strtoul(*p, &e, 10);
+ if (errno != 0)
+ return -errno;
+ if (e == *p)
+ return -EINVAL;
+ if ((unsigned long) (int) value != value)
+ return -ERANGE;
+
+ if (*e == '/') {
+ repeat = strtoul(e+1, &ee, 10);
+ if (errno != 0)
+ return -errno;
+ if (ee == e+1)
+ return -EINVAL;
+ if ((unsigned long) (int) repeat != repeat)
+ return -ERANGE;
+ if (repeat <= 0)
+ return -ERANGE;
+
+ e = ee;
+ }
+
+ if (*e != 0 && *e != ' ' && *e != ',' && *e != '-' && *e != ':')
+ return -EINVAL;
+
+ cc = new0(CalendarComponent, 1);
+ if (!cc)
+ return -ENOMEM;
+
+ cc->value = value;
+ cc->repeat = repeat;
+ cc->next = *c;
+
+ *p = e;
+ *c = cc;
+
+ if (*e ==',') {
+ *p += 1;
+ return prepend_component(p, c);
+ }
+
+ return 0;
+}
+
+static int parse_chain(const char **p, CalendarComponent **c) {
+ const char *t;
+ CalendarComponent *cc = NULL;
+ int r;
+
+ assert(p);
+ assert(c);
+
+ t = *p;
+
+ if (t[0] == '*') {
+ *p = t + 1;
+ *c = NULL;
+ return 0;
+ }
+
+ r = prepend_component(&t, &cc);
+ if (r < 0) {
+ free_chain(cc);
+ return r;
+ }
+
+ *p = t;
+ *c = cc;
+ return 0;
+}
+
+static int const_chain(int value, CalendarComponent **c) {
+ CalendarComponent *cc = NULL;
+
+ assert(c);
+
+ cc = new0(CalendarComponent, 1);
+ if (!cc)
+ return -ENOMEM;
+
+ cc->value = value;
+ cc->repeat = 0;
+ cc->next = NULL;
+
+ *c = cc;
+
+ return 0;
+}
+
+static int parse_date(const char **p, CalendarSpec *c) {
+ const char *t;
+ int r;
+ CalendarComponent *first, *second, *third;
+
+ assert(p);
+ assert(*p);
+ assert(c);
+
+ t = *p;
+
+ if (*t == 0)
+ return 0;
+
+ r = parse_chain(&t, &first);
+ if (r < 0)
+ return r;
+
+ /* Already the end? A ':' as separator? In that case this was a time, not a date */
+ if (*t == 0 || *t == ':') {
+ free_chain(first);
+ return 0;
+ }
+
+ if (*t != '-') {
+ free_chain(first);
+ return -EINVAL;
+ }
+
+ t++;
+ r = parse_chain(&t, &second);
+ if (r < 0) {
+ free_chain(first);
+ return r;
+ }
+
+ /* Got two parts, hence it's month and day */
+ if (*t == ' ' || *t == 0) {
+ *p = t + strspn(t, " ");
+ c->month = first;
+ c->day = second;
+ return 0;
+ }
+
+ if (*t != '-') {
+ free_chain(first);
+ free_chain(second);
+ return -EINVAL;
+ }
+
+ t++;
+ r = parse_chain(&t, &third);
+ if (r < 0) {
+ free_chain(first);
+ free_chain(second);
+ return r;
+ }
+
+ /* Got tree parts, hence it is year, month and day */
+ if (*t == ' ' || *t == 0) {
+ *p = t + strspn(t, " ");
+ c->year = first;
+ c->month = second;
+ c->day = third;
+ return 0;
+ }
+
+ free_chain(first);
+ free_chain(second);
+ free_chain(third);
+ return -EINVAL;
+}
+
+static int parse_time(const char **p, CalendarSpec *c) {
+ CalendarComponent *h = NULL, *m = NULL, *s = NULL;
+ const char *t;
+ int r;
+
+ assert(p);
+ assert(*p);
+ assert(c);
+
+ t = *p;
+
+ if (*t == 0) {
+ /* If no time is specified at all, but a date of some
+ * kind, then this means 00:00:00 */
+ if (c->day || c->weekdays_bits > 0)
+ goto null_hour;
+
+ goto finish;
+ }
+
+ r = parse_chain(&t, &h);
+ if (r < 0)
+ goto fail;
+
+ if (*t != ':') {
+ r = -EINVAL;
+ goto fail;
+ }
+
+ t++;
+ r = parse_chain(&t, &m);
+ if (r < 0)
+ goto fail;
+
+ /* Already at the end? Then it's hours and minutes, and seconds are 0 */
+ if (*t == 0) {
+ if (m != NULL)
+ goto null_second;
+
+ goto finish;
+ }
+
+ if (*t != ':') {
+ r = -EINVAL;
+ goto fail;
+ }
+
+ t++;
+ r = parse_chain(&t, &s);
+ if (r < 0)
+ goto fail;
+
+ /* At the end? Then it's hours, minutes and seconds */
+ if (*t == 0)
+ goto finish;
+
+ r = -EINVAL;
+ goto fail;
+
+null_hour:
+ r = const_chain(0, &h);
+ if (r < 0)
+ goto fail;
+
+ r = const_chain(0, &m);
+ if (r < 0)
+ goto fail;
+
+null_second:
+ r = const_chain(0, &s);
+ if (r < 0)
+ goto fail;
+
+finish:
+ *p = t;
+ c->hour = h;
+ c->minute = m;
+ c->second = s;
+ return 0;
+
+fail:
+ free_chain(h);
+ free_chain(m);
+ free_chain(s);
+ return r;
+}
+
+int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
+ CalendarSpec *c;
+ int r;
+
+ assert(p);
+ assert(spec);
+
+ if (isempty(p))
+ return -EINVAL;
+
+ c = new0(CalendarSpec, 1);
+ if (!c)
+ return -ENOMEM;
+
+ if (strcasecmp(p, "hourly") == 0) {
+ r = const_chain(0, &c->minute);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->second);
+ if (r < 0)
+ goto fail;
+
+ } else if (strcasecmp(p, "daily") == 0) {
+ r = const_chain(0, &c->hour);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->minute);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->second);
+ if (r < 0)
+ goto fail;
+
+ } else if (strcasecmp(p, "monthly") == 0) {
+ r = const_chain(1, &c->day);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->hour);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->minute);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->second);
+ if (r < 0)
+ goto fail;
+
+ } else if (strcasecmp(p, "weekly") == 0) {
+
+ c->weekdays_bits = 1;
+
+ r = const_chain(0, &c->hour);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->minute);
+ if (r < 0)
+ goto fail;
+ r = const_chain(0, &c->second);
+ if (r < 0)
+ goto fail;
+
+ } else {
+ r = parse_weekdays(&p, c);
+ if (r < 0)
+ goto fail;
+
+ r = parse_date(&p, c);
+ if (r < 0)
+ goto fail;
+
+ r = parse_time(&p, c);
+ if (r < 0)
+ goto fail;
+
+ if (*p != 0) {
+ r = -EINVAL;
+ goto fail;
+ }
+ }
+
+ r = calendar_spec_normalize(c);
+ if (r < 0)
+ goto fail;
+
+ if (!calendar_spec_valid(c)) {
+ r = -EINVAL;
+ goto fail;
+ }
+
+ *spec = c;
+ return 0;
+
+fail:
+ calendar_spec_free(c);
+ return r;
+}
+
+static int find_matching_component(const CalendarComponent *c, int *val) {
+ const CalendarComponent *n;
+ int d;
+ bool d_set = false;
+ int r;
+
+ assert(val);
+
+ if (!c)
+ return 0;
+
+ while (c) {
+ n = c->next;
+
+ if (c->value >= *val) {
+
+ if (!d_set || c->value < d) {
+ d = c->value;
+ d_set = true;
+ }
+
+ } else if (c->repeat > 0) {
+ int k;
+
+ k = c->value + c->repeat * ((*val - c->value + c->repeat -1) / c->repeat);
+
+ if (!d_set || k < d) {
+ d = k;
+ d_set = true;
+ }
+ }
+
+ c = n;
+ }
+
+ if (!d_set)
+ return -ENOENT;
+
+ r = *val != d;
+ *val = d;
+ return r;
+}
+
+static bool tm_out_of_bounds(const struct tm *tm) {
+ struct tm t;
+ assert(tm);
+
+ t = *tm;
+
+ if (mktime(&t) == (time_t) -1)
+ return true;
+
+ /* Did any normalization take place? If so, it was out of bounds before */
+ return
+ t.tm_year != tm->tm_year ||
+ t.tm_mon != tm->tm_mon ||
+ t.tm_mday != tm->tm_mday ||
+ t.tm_hour != tm->tm_hour ||
+ t.tm_min != tm->tm_min ||
+ t.tm_sec != tm->tm_sec;
+}
+
+static bool matches_weekday(int weekdays_bits, const struct tm *tm) {
+ struct tm t;
+ int k;
+
+ if (weekdays_bits < 0 || weekdays_bits >= 127)
+ return true;
+
+ t = *tm;
+ if (mktime(&t) == (time_t) -1)
+ return false;
+
+ k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
+ return (weekdays_bits & (1 << k));
+}
+
+static int find_next(const CalendarSpec *spec, struct tm *tm) {
+ struct tm c;
+ int r;
+
+ assert(spec);
+ assert(tm);
+
+ c = *tm;
+
+ for (;;) {
+ /* Normalize the current date */
+ mktime(&c);
+ c.tm_isdst = -1;
+
+ c.tm_year += 1900;
+ r = find_matching_component(spec->year, &c.tm_year);
+ c.tm_year -= 1900;
+
+ if (r > 0) {
+ c.tm_mon = 0;
+ c.tm_mday = 1;
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ }
+ if (r < 0 || tm_out_of_bounds(&c))
+ return r;
+
+ c.tm_mon += 1;
+ r = find_matching_component(spec->month, &c.tm_mon);
+ c.tm_mon -= 1;
+
+ if (r > 0) {
+ c.tm_mday = 1;
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ }
+ if (r < 0 || tm_out_of_bounds(&c)) {
+ c.tm_year ++;
+ c.tm_mon = 0;
+ c.tm_mday = 1;
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ continue;
+ }
+
+ r = find_matching_component(spec->day, &c.tm_mday);
+ if (r > 0)
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ if (r < 0 || tm_out_of_bounds(&c)) {
+ c.tm_mon ++;
+ c.tm_mday = 1;
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ continue;
+ }
+
+ if (!matches_weekday(spec->weekdays_bits, &c)) {
+ c.tm_mday++;
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ continue;
+ }
+
+ r = find_matching_component(spec->hour, &c.tm_hour);
+ if (r > 0)
+ c.tm_min = c.tm_sec = 0;
+ if (r < 0 || tm_out_of_bounds(&c)) {
+ c.tm_mday ++;
+ c.tm_hour = c.tm_min = c.tm_sec = 0;
+ continue;
+ }
+
+ r = find_matching_component(spec->minute, &c.tm_min);
+ if (r > 0)
+ c.tm_sec = 0;
+ if (r < 0 || tm_out_of_bounds(&c)) {
+ c.tm_hour ++;
+ c.tm_min = c.tm_sec = 0;
+ continue;
+ }
+
+ r = find_matching_component(spec->second, &c.tm_sec);
+ if (r < 0 || tm_out_of_bounds(&c)) {
+ c.tm_min ++;
+ c.tm_sec = 0;
+ continue;
+ }
+
+
+ *tm = c;
+ return 0;
+ }
+}
+
+int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
+ struct tm tm;
+ time_t t;
+ int r;
+
+ assert(spec);
+ assert(next);
+
+ t = (time_t) (usec / USEC_PER_SEC) + 1;
+ assert_se(localtime_r(&t, &tm));
+
+ r = find_next(spec, &tm);
+ if (r < 0)
+ return r;
+
+ t = mktime(&tm);
+ if (t == (time_t) -1)
+ return -EINVAL;
+
+
+ *next = (usec_t) t * USEC_PER_SEC;
+ return 0;
+}
diff --git a/src/shared/calendarspec.h b/src/shared/calendarspec.h
new file mode 100644
index 0000000000..7baf318249
--- /dev/null
+++ b/src/shared/calendarspec.h
@@ -0,0 +1,57 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2012 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+/* A structure for specifying (possibly repetitive) points in calendar
+ * time, a la cron */
+
+#include <stdbool.h>
+#include "util.h"
+
+typedef struct CalendarComponent {
+ int value;
+ int repeat;
+
+ struct CalendarComponent *next;
+} CalendarComponent;
+
+typedef struct CalendarSpec {
+ int weekdays_bits;
+
+ CalendarComponent *year;
+ CalendarComponent *month;
+ CalendarComponent *day;
+
+ CalendarComponent *hour;
+ CalendarComponent *minute;
+ CalendarComponent *second;
+} CalendarSpec;
+
+void calendar_spec_free(CalendarSpec *c);
+
+int calendar_spec_normalize(CalendarSpec *spec);
+bool calendar_spec_valid(CalendarSpec *spec);
+
+int calendar_spec_to_string(const CalendarSpec *spec, char **p);
+int calendar_spec_from_string(const char *p, CalendarSpec **spec);
+
+int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next);
diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c
new file mode 100644
index 0000000000..b0fec8b28a
--- /dev/null
+++ b/src/test/test-calendarspec.c
@@ -0,0 +1,84 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2012 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <string.h>
+
+#include "calendarspec.h"
+#include "util.h"
+
+static void test_one(const char *input, const char *output) {
+ CalendarSpec *c;
+ char *p;
+ usec_t u;
+ char buf[FORMAT_TIMESTAMP_MAX];
+ int r;
+
+ assert_se(calendar_spec_from_string(input, &c) >= 0);
+
+ assert_se(calendar_spec_to_string(c, &p) >= 0);
+ printf("\"%s\" → \"%s\"\n", input, p);
+
+ assert_se(streq(p, output));
+ free(p);
+
+ u = now(CLOCK_REALTIME);
+ r = calendar_spec_next_usec(c, u, &u);
+ printf("Next: %s\n", r < 0 ? strerror(-r) : format_timestamp(buf, sizeof(buf), u));
+
+ calendar_spec_free(c);
+}
+
+int main(int argc, char* argv[]) {
+ CalendarSpec *c;
+
+ test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon-Thu,Sat,Sun *-*-* 00:00:00");
+ test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00");
+ test_one("Wed *-1", "Wed *-*-01 00:00:00");
+ test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00");
+ test_one("Wed, 17:48", "Wed *-*-* 17:48:00");
+ test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue-Sat 2012-10-15 01:02:03");
+ test_one("*-*-7 0:0:0", "*-*-07 00:00:00");
+ test_one("10-15", "*-10-15 00:00:00");
+ test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00");
+ test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45");
+ test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00");
+ test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45");
+ test_one("03-05 08:05:40", "*-03-05 08:05:40");
+ test_one("08:05:40", "*-*-* 08:05:40");
+ test_one("05:40", "*-*-* 05:40:00");
+ test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40");
+ test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40");
+ test_one("2003-03-05 05:40", "2003-03-05 05:40:00");
+ test_one("2003-03-05", "2003-03-05 00:00:00");
+ test_one("03-05", "*-03-05 00:00:00");
+ test_one("hourly", "*-*-* *:00:00");
+ test_one("daily", "*-*-* 00:00:00");
+ test_one("monthly", "*-*-01 00:00:00");
+ test_one("weekly", "Mon *-*-* 00:00:00");
+ test_one("*:2/3", "*-*-* *:02/3:00");
+
+ assert_se(calendar_spec_from_string("test", &c) < 0);
+ assert_se(calendar_spec_from_string("", &c) < 0);
+ assert_se(calendar_spec_from_string("7", &c) < 0);
+ assert_se(calendar_spec_from_string("121212:1:2", &c) < 0);
+
+ return 0;
+}