timer.c revision c5962bd1d11553425e3784ee00aa8d101c1b80fc
/*-*- 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-errors.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);
watch_init(&t->monotonic_watch);
watch_init(&t->realtime_watch);
}
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);
unit_unwatch_timer(u, &t->monotonic_watch);
unit_unwatch_timer(u, &t->realtime_watch);
unit_ref_unset(&t->unit);
}
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;
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;
}
return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
}
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) {
if (!UNIT_DEREF(t->unit)) {
Unit *x;
r = unit_load_related_unit(u, ".service", &x);
if (r < 0)
return r;
unit_ref_set(&t->unit, x);
}
if (r < 0)
return r;
if (UNIT(t)->default_dependencies) {
r = timer_add_default_dependencies(t);
if (r < 0)
return r;
}
}
return timer_verify(t);
}
TimerValue *v;
fprintf(f,
"%sTimer State: %s\n"
"%sResult: %s\n"
"%sUnit: %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;
}
TimerValue *v;
bool found_monotonic = false, found_realtime = false;
int r;
t->next_elapse_monotonic = t->next_elapse_realtime = 0;
if (v->disabled)
continue;
if (v->base == TIMER_CALENDAR) {
if (r < 0)
continue;
v->disabled = true;
continue;
}
if (!found_realtime)
t->next_elapse_realtime = v->next_elapse;
else
found_realtime = true;
} else {
switch (v->base) {
case TIMER_ACTIVE:
else
break;
case TIMER_BOOT:
/* CLOCK_MONOTONIC equals the uptime on Linux */
base = 0;
break;
case TIMER_STARTUP:
break;
case TIMER_UNIT_ACTIVE:
continue;
break;
case TIMER_UNIT_INACTIVE:
continue;
break;
default:
assert_not_reached("Unknown timer base");
}
v->disabled = true;
continue;
}
if (!found_monotonic)
t->next_elapse_monotonic = v->next_elapse;
else
found_monotonic = true;
}
}
if (!found_monotonic && !found_realtime) {
return;
}
if (found_monotonic) {
char buf[FORMAT_TIMESPAN_MAX];
"%s: Monotonic timer elapses in %s.",
format_timespan(buf, sizeof(buf), t->next_elapse_monotonic > ts.monotonic ? t->next_elapse_monotonic - ts.monotonic : 0));
r = unit_watch_timer(UNIT(t), CLOCK_MONOTONIC, false, t->next_elapse_monotonic, &t->monotonic_watch);
if (r < 0)
goto fail;
} else
if (found_realtime) {
char buf[FORMAT_TIMESTAMP_MAX];
"%s: Realtime timer elapses at %s.",
if (r < 0)
goto fail;
} else
return;
fail:
"%s failed to enter waiting state: %s",
}
static void timer_enter_running(Timer *t) {
int r;
assert(t);
/* Don't start job if we are supposed to go down */
return;
r = manager_add_job(UNIT(t)->manager, JOB_START, UNIT_DEREF(t->unit), JOB_REPLACE, true, &error, NULL);
if (r < 0)
goto fail;
return;
fail:
"%s failed to queue unit startup job: %s",
}
static int timer_start(Unit *u) {
assert(t);
return -ENOENT;
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);
return 0;
}
assert(u);
if (state < 0)
else
t->deserialized_state = state;
TimerResult f;
if (f < 0)
else if (f != TIMER_SUCCESS)
t->result = f;
} else
return 0;
}
assert(u);
}
static const char *timer_sub_state_to_string(Unit *u) {
assert(u);
}
assert(t);
if (t->state != TIMER_WAITING)
return;
}
Iterator i;
Unit *k;
if (u->type == UNIT_TIMER)
return;
Timer *t;
TimerValue *v;
if (k->type != UNIT_TIMER)
continue;
if (k->load_state != UNIT_LOADED)
continue;
t = TIMER(k);
/* 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:
if (UNIT_IS_INACTIVE_OR_FAILED(new_state)) {
"%s got notified about unit deactivation.",
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;
log_debug_unit(u->id,
"%s: time change, recalculating next elapse.", u->id);
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",
};