chiark / gitweb /
minor cleanup
[elogind.git] / target.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include "unit.h"
4 #include "target.h"
5 #include "load-fragment.h"
6 #include "log.h"
7
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 changed %s → %s", unit_id(UNIT(t)), state_string_table[old_state], state_string_table[state]);
37
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 }
50
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;
59 }
60
61 static UnitActiveState target_active_state(Unit *u) {
62         assert(u);
63
64         return state_translation_table[TARGET(u)->state];
65 }
66
67 const UnitVTable target_vtable = {
68         .suffix = ".target",
69
70         .init = unit_load_fragment_and_dropin,
71
72         .dump = target_dump,
73
74         .start = target_start,
75         .stop = target_stop,
76
77         .active_state = target_active_state
78 };