timer.c revision 779042e772d2459f7649b34a164902dc456f1bab
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <errno.h>
#include "unit.h"
#include "unit-name.h"
#include "timer.h"
#include "dbus-timer.h"
#include "special.h"
#include "bus-util.h"
#include "bus-error.h"
#include "mkdir.h"
[TIMER_DEAD] = UNIT_INACTIVE,
[TIMER_WAITING] = UNIT_ACTIVE,
[TIMER_RUNNING] = UNIT_ACTIVE,
[TIMER_ELAPSED] = UNIT_ACTIVE,
};
static void timer_init(Unit *u) {
assert(u);
}
void timer_free_values(Timer *t) {
TimerValue *v;
assert(t);
while ((v = t->values)) {
if (v->calendar_spec)
free(v);
}
}
static void timer_done(Unit *u) {
assert(t);
free(t->stamp_path);
}
static int timer_verify(Timer *t) {
assert(t);
return 0;
if (!t->values) {
return -EINVAL;
}
return 0;
}
static int timer_add_default_dependencies(Timer *t) {
int r;
TimerValue *v;
assert(t);
if (r < 0)
return r;
r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;
if (v->base == TIMER_CALENDAR) {
if (r < 0)
return r;
break;
}
}
}
return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
}
static int timer_setup_persistent(Timer *t) {
int r;
assert(t);
if (!t->persistent)
return 0;
if (r < 0)
return r;
} else {
const char *e;
e = getenv("XDG_DATA_HOME");
if (e)
else {
_cleanup_free_ char *h = NULL;
r = get_home_dir(&h);
if (r < 0) {
return r;
}
}
}
if (!t->stamp_path)
return log_oom();
return 0;
}
static int timer_load(Unit *u) {
int r;
assert(u);
r = unit_load_fragment_and_dropin(u);
if (r < 0)
return r;
if (u->load_state == UNIT_LOADED) {
Unit *x;
r = unit_load_related_unit(u, ".service", &x);
if (r < 0)
return r;
if (r < 0)
return r;
}
r = timer_setup_persistent(t);
if (r < 0)
return r;
if (u->default_dependencies) {
r = timer_add_default_dependencies(t);
if (r < 0)
return r;
}
}
return timer_verify(t);
}
char buf[FORMAT_TIMESPAN_MAX];
TimerValue *v;
trigger = UNIT_TRIGGER(u);
fprintf(f,
"%sTimer State: %s\n"
"%sResult: %s\n"
"%sUnit: %s\n"
"%sPersistent: %s\n"
"%sWakeSystem: %s\n"
"%sAccuracy: %s\n",
if (v->base == TIMER_CALENDAR) {
_cleanup_free_ char *p = NULL;
calendar_spec_to_string(v->calendar_spec, &p);
fprintf(f,
"%s%s: %s\n",
timer_base_to_string(v->base),
strna(p));
} else {
char timespan1[FORMAT_TIMESPAN_MAX];
fprintf(f,
"%s%s: %s\n",
timer_base_to_string(v->base),
}
}
}
assert(t);
if (state != TIMER_WAITING) {
}
}
static int timer_coldplug(Unit *u) {
assert(t);
if (t->deserialized_state != t->state) {
if (t->deserialized_state == TIMER_WAITING)
timer_enter_waiting(t, false);
else
timer_set_state(t, t->deserialized_state);
}
return 0;
}
assert(t);
if (f != TIMER_SUCCESS)
t->result = f;
}
usec_t a, b;
if (t <= 0)
return 0;
a = now(CLOCK_BOOTTIME);
b = now(CLOCK_MONOTONIC);
if (t + a > b)
return t + a - b;
else
return 0;
}
bool found_monotonic = false, found_realtime = false;
TimerValue *v;
int r;
/* If we shall wake the system we use the boottime clock
* rather than the monotonic clock. */
t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0;
if (v->disabled)
continue;
if (v->base == TIMER_CALENDAR) {
usec_t b;
/* If we know the last time this was
* triggered, schedule the job based relative
* to that. If we don't just start from
* now. */
if (r < 0)
continue;
if (!found_realtime)
t->next_elapse_realtime = v->next_elapse;
else
found_realtime = true;
} else {
switch (v->base) {
case TIMER_ACTIVE:
else
base = ts_monotonic;
break;
case TIMER_BOOT:
/* CLOCK_MONOTONIC equals the uptime on Linux */
base = 0;
break;
case TIMER_STARTUP:
break;
case TIMER_UNIT_ACTIVE:
if (base <= 0)
if (base <= 0)
continue;
break;
case TIMER_UNIT_INACTIVE:
if (base <= 0)
if (base <= 0)
continue;
break;
default:
assert_not_reached("Unknown timer base");
}
if (t->wake_system)
if (!initial && v->next_elapse < ts_monotonic && IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
/* This is a one time trigger, disable it now */
v->disabled = true;
continue;
}
if (!found_monotonic)
else
found_monotonic = true;
}
}
if (!found_monotonic && !found_realtime) {
return;
}
if (found_monotonic) {
char buf[FORMAT_TIMESPAN_MAX];
format_timespan(buf, sizeof(buf), t->next_elapse_monotonic_or_boottime > ts_monotonic ? t->next_elapse_monotonic_or_boottime - ts_monotonic : 0, 0));
if (t->monotonic_event_source) {
if (r < 0)
goto fail;
} else
r = sd_event_add_time(
timer_dispatch, t);
if (r < 0)
goto fail;
} else if (t->monotonic_event_source) {
if (r < 0)
goto fail;
}
if (found_realtime) {
char buf[FORMAT_TIMESTAMP_MAX];
log_debug_unit(UNIT(t)->id, "%s: Realtime timer elapses at %s.", UNIT(t)->id, format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
if (t->realtime_event_source) {
if (r < 0)
goto fail;
} else
r = sd_event_add_time(
t->next_elapse_realtime, t->accuracy_usec,
timer_dispatch, t);
if (r < 0)
goto fail;
} else if (t->realtime_event_source) {
if (r < 0)
goto fail;
}
return;
fail:
}
static void timer_enter_running(Timer *t) {
int r;
assert(t);
/* Don't start job if we are supposed to go down */
if (unit_stop_pending(UNIT(t)))
return;
if (r < 0)
goto fail;
if (t->stamp_path)
return;
fail:
"%s failed to queue unit startup job: %s",
}
static int timer_start(Unit *u) {
TimerValue *v;
assert(t);
return -ENOENT;
/* Reenable all timers that depend on unit activation time */
if (v->base == TIMER_ACTIVE)
v->disabled = false;
if (t->stamp_path) {
/* The timer has never run before,
* make sure a stamp file exists.
*/
}
t->result = TIMER_SUCCESS;
timer_enter_waiting(t, true);
return 0;
}
static int timer_stop(Unit *u) {
assert(t);
return 0;
}
assert(u);
assert(f);
if (t->last_trigger.realtime > 0)
if (t->last_trigger.monotonic > 0)
return 0;
}
int r;
assert(u);
if (state < 0)
else
t->deserialized_state = state;
TimerResult f;
if (f < 0)
else if (f != TIMER_SUCCESS)
t->result = f;
if (r < 0)
if (r < 0)
} else
return 0;
}
assert(u);
}
assert(u);
}
assert(t);
if (t->state != TIMER_WAITING)
return 0;
return 0;
}
TimerValue *v;
assert(u);
return;
/* Reenable all timers that depend on unit state */
if (v->base == TIMER_UNIT_ACTIVE ||
v->base == TIMER_UNIT_INACTIVE)
v->disabled = false;
switch (t->state) {
case TIMER_WAITING:
case TIMER_ELAPSED:
/* Recalculate sleep time */
timer_enter_waiting(t, false);
break;
case TIMER_RUNNING:
timer_enter_waiting(t, false);
}
break;
case TIMER_DEAD:
case TIMER_FAILED:
break;
default:
assert_not_reached("Unknown timer state");
}
}
static void timer_reset_failed(Unit *u) {
assert(t);
if (t->state == TIMER_FAILED)
t->result = TIMER_SUCCESS;
}
static void timer_time_change(Unit *u) {
assert(u);
if (t->state != TIMER_WAITING)
return;
timer_enter_waiting(t, false);
}
static const char* const timer_state_table[_TIMER_STATE_MAX] = {
[TIMER_DEAD] = "dead",
[TIMER_WAITING] = "waiting",
[TIMER_RUNNING] = "running",
[TIMER_ELAPSED] = "elapsed",
[TIMER_FAILED] = "failed"
};
static const char* const timer_base_table[_TIMER_BASE_MAX] = {
[TIMER_ACTIVE] = "OnActiveSec",
[TIMER_BOOT] = "OnBootSec",
[TIMER_STARTUP] = "OnStartupSec",
[TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
[TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec",
[TIMER_CALENDAR] = "OnCalendar"
};
static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
[TIMER_SUCCESS] = "success",
[TIMER_FAILURE_RESOURCES] = "resources"
};
const UnitVTable timer_vtable = {
.object_size = sizeof(Timer),
.sections =
"Unit\0"
"Timer\0"
"Install\0",
.init = timer_init,
.done = timer_done,
.load = timer_load,
.dump = timer_dump,
.start = timer_start,
.stop = timer_stop,
.bus_interface = "org.freedesktop.systemd1.Timer",
};