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