]>
Commit | Line | Data |
---|---|---|
c22cbe26 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
87f0e418 | 3 | #include "unit.h" |
c22cbe26 LP |
4 | #include "target.h" |
5 | #include "load-fragment.h" | |
fa068367 | 6 | #include "log.h" |
c22cbe26 | 7 | |
fa068367 LP |
8 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
9 | [TARGET_DEAD] = UNIT_INACTIVE, | |
10 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
11 | }; | |
12 | ||
13 | static const char* const state_string_table[_TARGET_STATE_MAX] = { | |
14 | [TARGET_DEAD] = "dead", | |
15 | [TARGET_ACTIVE] = "active" | |
16 | }; | |
17 | ||
18 | static void target_dump(Unit *u, FILE *f, const char *prefix) { | |
19 | Target *t = TARGET(u); | |
20 | ||
21 | assert(t); | |
22 | assert(f); | |
23 | ||
24 | fprintf(f, | |
25 | "%sTarget State: %s\n", | |
26 | prefix, state_string_table[t->state]); | |
27 | } | |
28 | ||
29 | static void target_set_state(Target *t, TargetState state) { | |
30 | TargetState old_state; | |
31 | assert(t); | |
32 | ||
33 | old_state = t->state; | |
34 | t->state = state; | |
35 | ||
36 | log_debug("%s changing %s → %s", unit_id(UNIT(t)), state_string_table[old_state], state_string_table[state]); | |
c22cbe26 | 37 | |
fa068367 LP |
38 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); |
39 | } | |
40 | ||
41 | static int target_start(Unit *u) { | |
42 | Target *t = TARGET(u); | |
43 | ||
44 | assert(t); | |
45 | assert(t->state == TARGET_DEAD); | |
46 | ||
47 | target_set_state(t, TARGET_ACTIVE); | |
48 | return 0; | |
49 | } | |
c22cbe26 | 50 | |
fa068367 LP |
51 | static int target_stop(Unit *u) { |
52 | Target *t = TARGET(u); | |
53 | ||
54 | assert(t); | |
55 | assert(t->state == TARGET_ACTIVE); | |
56 | ||
57 | target_set_state(t, TARGET_DEAD); | |
58 | return 0; | |
c22cbe26 LP |
59 | } |
60 | ||
87f0e418 | 61 | static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
62 | assert(u); |
63 | ||
64 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
65 | } |
66 | ||
87f0e418 | 67 | const UnitVTable target_vtable = { |
c22cbe26 LP |
68 | .suffix = ".target", |
69 | ||
0301abf4 | 70 | .init = unit_load_fragment_and_dropin, |
fa068367 LP |
71 | |
72 | .dump = target_dump, | |
73 | ||
74 | .start = target_start, | |
75 | .stop = target_stop, | |
c22cbe26 LP |
76 | |
77 | .active_state = target_active_state | |
78 | }; |