1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
7 #include "load-fragment.h"
10 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
11 [TARGET_DEAD] = UNIT_INACTIVE,
12 [TARGET_ACTIVE] = UNIT_ACTIVE
15 static const char* const state_string_table[_TARGET_STATE_MAX] = {
16 [TARGET_DEAD] = "dead",
17 [TARGET_ACTIVE] = "active"
20 static int target_init(Unit *u) {
24 /* Make sure this config file actually exists */
26 if ((r = unit_load_fragment_and_dropin(u)) <= 0)
27 return r < 0 ? r : -ENOENT;
32 static void target_dump(Unit *u, FILE *f, const char *prefix) {
33 Target *t = TARGET(u);
39 "%sTarget State: %s\n",
40 prefix, state_string_table[t->state]);
43 static void target_set_state(Target *t, TargetState state) {
44 TargetState old_state;
50 log_debug("%s changed %s → %s", unit_id(UNIT(t)), state_string_table[old_state], state_string_table[state]);
52 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
55 static int target_start(Unit *u) {
56 Target *t = TARGET(u);
59 assert(t->state == TARGET_DEAD);
61 target_set_state(t, TARGET_ACTIVE);
65 static int target_stop(Unit *u) {
66 Target *t = TARGET(u);
69 assert(t->state == TARGET_ACTIVE);
71 target_set_state(t, TARGET_DEAD);
75 static UnitActiveState target_active_state(Unit *u) {
78 return state_translation_table[TARGET(u)->state];
81 const UnitVTable target_vtable = {
88 .start = target_start,
91 .active_state = target_active_state