-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
#include "unit-name.h"
#include "timer.h"
#include "dbus-timer.h"
+#include "special.h"
+#include "bus-errors.h"
static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
[TIMER_DEAD] = UNIT_INACTIVE,
[TIMER_WAITING] = UNIT_ACTIVE,
[TIMER_RUNNING] = UNIT_ACTIVE,
[TIMER_ELAPSED] = UNIT_ACTIVE,
- [TIMER_MAINTAINANCE] = UNIT_INACTIVE
+ [TIMER_MAINTENANCE] = UNIT_MAINTENANCE
};
static void timer_init(Unit *u) {
assert(u->meta.load_state == UNIT_STUB);
t->next_elapse = (usec_t) -1;
- t->timer_watch.type = WATCH_INVALID;
}
static void timer_done(Unit *u) {
static int timer_verify(Timer *t) {
assert(t);
- if (UNIT(t)->meta.load_state != UNIT_LOADED)
+ if (t->meta.load_state != UNIT_LOADED)
return 0;
if (!t->values) {
return 0;
}
+static int timer_add_default_dependencies(Timer *t) {
+ int r;
+
+ assert(t);
+
+ if (t->meta.manager->running_as == MANAGER_SYSTEM)
+ 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_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
+}
+
static int timer_load(Unit *u) {
Timer *t = TIMER(u);
int r;
if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
return r;
+
+ if (t->meta.default_dependencies)
+ if ((r = timer_add_default_dependencies(t)) < 0)
+ return r;
}
return timer_verify(t);
static void timer_dump(Unit *u, FILE *f, const char *prefix) {
Timer *t = TIMER(u);
- const char *prefix2;
- char *p2;
TimerValue *v;
char
timespan1[FORMAT_TIMESPAN_MAX];
- p2 = strappend(prefix, "\t");
- prefix2 = p2 ? p2 : prefix;
-
fprintf(f,
"%sTimer State: %s\n"
"%sUnit: %s\n",
prefix,
timer_base_to_string(v->base),
strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
-
- free(p2);
}
static void timer_set_state(Timer *t, TimerState state) {
if (!success)
t->failure = true;
- timer_set_state(t, t->failure ? TIMER_MAINTAINANCE : TIMER_DEAD);
+ timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD);
}
static void timer_enter_waiting(Timer *t, bool initial) {
}
static void timer_enter_running(Timer *t) {
+ DBusError error;
int r;
+
assert(t);
+ dbus_error_init(&error);
+
+ /* Don't start job if we are supposed to go down */
+ if (t->meta.job && t->meta.job->type == JOB_STOP)
+ return;
- if ((r = manager_add_job(UNIT(t)->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0)
+ if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, &error, NULL)) < 0)
goto fail;
timer_set_state(t, TIMER_RUNNING);
return;
fail:
- log_warning("%s failed to queue unit startup job: %s", t->meta.id, strerror(-r));
+ log_warning("%s failed to queue unit startup job: %s", t->meta.id, bus_error(&error, r));
timer_enter_dead(t, false);
+
+ dbus_error_free(&error);
}
static int timer_start(Unit *u) {
Timer *t = TIMER(u);
assert(t);
- assert(t->state == TIMER_DEAD);
+ assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE);
+
+ if (t->unit->meta.load_state != UNIT_LOADED)
+ return -ENOENT;
+ t->failure = false;
timer_enter_waiting(t, true);
return 0;
}
if (!p)
continue;
+ if (p->meta.load_state != UNIT_LOADED)
+ continue;
+
t = TIMER(p);
- if (t->meta.load_state != UNIT_LOADED)
+ if (t->unit != u)
continue;
/* Reenable all timers that depend on unit state */
case TIMER_RUNNING:
- if (new_state == UNIT_INACTIVE) {
+ if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) {
log_debug("%s got notified about unit deactivation.", t->meta.id);
timer_enter_waiting(t, false);
}
break;
case TIMER_DEAD:
- case TIMER_MAINTAINANCE:
+ case TIMER_MAINTENANCE:
;
default:
log_error("Failed find timer unit: %s", strerror(-r));
}
+static void timer_reset_maintenance(Unit *u) {
+ Timer *t = TIMER(u);
+
+ assert(t);
+
+ if (t->state == TIMER_MAINTENANCE)
+ timer_set_state(t, TIMER_DEAD);
+
+ 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_MAINTAINANCE] = "maintainance"
+ [TIMER_MAINTENANCE] = "maintenance"
};
DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
static const char* const timer_base_table[_TIMER_BASE_MAX] = {
- [TIMER_ACTIVE] = "OnActive",
- [TIMER_BOOT] = "OnBoot",
- [TIMER_STARTUP] = "OnStartup",
- [TIMER_UNIT_ACTIVE] = "OnUnitActive",
- [TIMER_UNIT_INACTIVE] = "OnUnitInactive"
+ [TIMER_ACTIVE] = "OnActiveSec",
+ [TIMER_BOOT] = "OnBootSec",
+ [TIMER_STARTUP] = "OnStartupSec",
+ [TIMER_UNIT_ACTIVE] = "OnUnitActiveSec",
+ [TIMER_UNIT_INACTIVE] = "OnUnitInactiveSec"
};
DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
.timer_event = timer_timer_event,
+ .reset_maintenance = timer_reset_maintenance,
+
.bus_message_handler = bus_timer_message_handler
};