timer.c revision 5632e3743db350a67478acc107d76cdf648a1f99
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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);
}
static void timer_done(Unit *u) {
TimerValue *v;
assert(t);
while ((v = t->values)) {
free(v);
}
unit_unwatch_timer(u, &t->timer_watch);
}
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 = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true)) < 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);
if ((r = unit_load_fragment_and_dropin(u)) < 0)
return r;
if (!t->unit)
return r;
return r;
if (t->meta.default_dependencies)
if ((r = timer_add_default_dependencies(t)) < 0)
return r;
}
return timer_verify(t);
}
const char *prefix2;
char *p2;
TimerValue *v;
char
fprintf(f,
"%sTimer State: %s\n"
"%sUnit: %s\n",
fprintf(f,
"%s%s: %s\n",
timer_base_to_string(v->base),
}
assert(t);
if (state != TIMER_WAITING)
log_debug("%s changed %s -> %s",
}
static int timer_coldplug(Unit *u) {
assert(t);
if (t->deserialized_state != t->state) {
if (t->deserialized_state == TIMER_WAITING ||
t->deserialized_state == TIMER_RUNNING ||
t->deserialized_state == TIMER_ELAPSED)
timer_enter_waiting(t, false);
else
timer_set_state(t, t->deserialized_state);
}
return 0;
}
assert(t);
if (!success)
t->failure = true;
}
TimerValue *v;
bool found = false;
int r;
n = now(CLOCK_MONOTONIC);
if (v->disabled)
continue;
switch (v->base) {
case TIMER_ACTIVE:
} else
base = n;
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");
}
if (!initial && v->next_elapse < n) {
v->disabled = true;
continue;
}
if (!found)
t->next_elapse = v->next_elapse;
else
found = true;
}
if (!found) {
return;
}
goto fail;
return;
fail:
timer_enter_dead(t, false);
}
static void timer_enter_running(Timer *t) {
int r;
assert(t);
/* Don't start job if we are supposed to go down */
return;
if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0)
goto fail;
return;
fail:
timer_enter_dead(t, false);
}
static int timer_start(Unit *u) {
assert(t);
return -ENOENT;
t->failure = false;
timer_enter_waiting(t, true);
return 0;
}
static int timer_stop(Unit *u) {
assert(t);
timer_enter_dead(t, true);
return 0;
}
assert(u);
assert(f);
return 0;
}
assert(u);
else
t->deserialized_state = state;
} 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;
}
char *n;
int r;
Iterator i;
return;
char *k;
Unit *p;
Timer *t;
TimerValue *v;
if (!(k = unit_name_change_suffix(n, ".timer"))) {
r = -ENOMEM;
goto fail;
}
free(k);
if (!p)
continue;
continue;
t = TIMER(p);
if (t->unit != u)
continue;
/* 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_MAINTENANCE:
;
default:
assert_not_reached("Unknown timer state");
}
}
return;
fail:
}
static void timer_reset_maintenance(Unit *u) {
assert(t);
if (t->state == TIMER_MAINTENANCE)
t->failure = false;
}
static const char* const timer_state_table[_TIMER_STATE_MAX] = {
[TIMER_DEAD] = "dead",
[TIMER_WAITING] = "waiting",
[TIMER_RUNNING] = "running",
[TIMER_ELAPSED] = "elapsed",
[TIMER_MAINTENANCE] = "maintenance"
};
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"
};
const UnitVTable timer_vtable = {
.suffix = ".timer",
.init = timer_init,
.done = timer_done,
.load = timer_load,
.dump = timer_dump,
.start = timer_start,
.stop = timer_stop,
};