Skip to content

Commit

Permalink
timer: implement calendar time events
Browse files Browse the repository at this point in the history
  • Loading branch information
poettering committed Nov 23, 2012
1 parent 8a11751 commit 36697dc
Show file tree
Hide file tree
Showing 16 changed files with 1,288 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/test-calendarspec
/test-catalog
/test-replace-var
/test-journal-enum
Expand Down
16 changes: 13 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 += \
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions src/core/dbus-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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, }
};
Expand Down
1 change: 1 addition & 0 deletions src/core/load-fragment-gperf.gperf.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions src/core/load-fragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 8 additions & 4 deletions src/core/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/core/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down
15 changes: 10 additions & 5 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/core/swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 36697dc

Please sign in to comment.