chiark / gitweb /
fixes to the dbus code to make GetAll() in the properties iface work
[elogind.git] / target.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <errno.h>
4
5 #include "unit.h"
6 #include "target.h"
7 #include "load-fragment.h"
8 #include "log.h"
9
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
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
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
50         log_debug("%s changed %s → %s", unit_id(UNIT(t)), state_string_table[old_state], state_string_table[state]);
51
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 }
64
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;
73 }
74
75 static UnitActiveState target_active_state(Unit *u) {
76         assert(u);
77
78         return state_translation_table[TARGET(u)->state];
79 }
80
81 const UnitVTable target_vtable = {
82         .suffix = ".target",
83
84         .init = target_init,
85
86         .dump = target_dump,
87
88         .start = target_start,
89         .stop = target_stop,
90
91         .active_state = target_active_state
92 };