chiark / gitweb /
utmp: properly initialize local variables
[elogind.git] / src / target.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <signal.h>
24
25 #include "unit.h"
26 #include "target.h"
27 #include "load-fragment.h"
28 #include "log.h"
29 #include "dbus-target.h"
30 #include "special.h"
31
32 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
33         [TARGET_DEAD] = UNIT_INACTIVE,
34         [TARGET_ACTIVE] = UNIT_ACTIVE
35 };
36
37 static void target_set_state(Target *t, TargetState state) {
38         TargetState old_state;
39         assert(t);
40
41         old_state = t->state;
42         t->state = state;
43
44         if (state != old_state)
45                 log_debug("%s changed %s -> %s",
46                           UNIT(t)->meta.id,
47                           target_state_to_string(old_state),
48                           target_state_to_string(state));
49
50         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
51 }
52
53 static int target_coldplug(Unit *u) {
54         Target *t = TARGET(u);
55
56         assert(t);
57         assert(t->state == TARGET_DEAD);
58
59         if (t->deserialized_state != t->state)
60                 target_set_state(t, t->deserialized_state);
61
62         return 0;
63 }
64
65 static void target_dump(Unit *u, FILE *f, const char *prefix) {
66         Target *t = TARGET(u);
67
68         assert(t);
69         assert(f);
70
71         fprintf(f,
72                 "%sTarget State: %s\n",
73                 prefix, target_state_to_string(t->state));
74 }
75
76 static int target_start(Unit *u) {
77         Target *t = TARGET(u);
78
79         assert(t);
80         assert(t->state == TARGET_DEAD);
81
82         target_set_state(t, TARGET_ACTIVE);
83         return 0;
84 }
85
86 static int target_stop(Unit *u) {
87         Target *t = TARGET(u);
88
89         assert(t);
90         assert(t->state == TARGET_ACTIVE);
91
92         target_set_state(t, TARGET_DEAD);
93         return 0;
94 }
95
96 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
97         Target *s = TARGET(u);
98
99         assert(s);
100         assert(f);
101         assert(fds);
102
103         unit_serialize_item(u, f, "state", target_state_to_string(s->state));
104         return 0;
105 }
106
107 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
108         Target *s = TARGET(u);
109
110         assert(u);
111         assert(key);
112         assert(value);
113         assert(fds);
114
115         if (streq(key, "state")) {
116                 TargetState state;
117
118                 if ((state = target_state_from_string(value)) < 0)
119                         log_debug("Failed to parse state value %s", value);
120                 else
121                         s->deserialized_state = state;
122
123         } else
124                 log_debug("Unknown serialization key '%s'", key);
125
126         return 0;
127 }
128
129 static UnitActiveState target_active_state(Unit *u) {
130         assert(u);
131
132         return state_translation_table[TARGET(u)->state];
133 }
134
135 static const char *target_sub_state_to_string(Unit *u) {
136         assert(u);
137
138         return target_state_to_string(TARGET(u)->state);
139 }
140
141 int target_get_runlevel(Target *t) {
142
143         static const struct {
144                 const char *special;
145                 const int runlevel;
146         } table[] = {
147                 { SPECIAL_RUNLEVEL5_TARGET, '5' },
148                 { SPECIAL_RUNLEVEL4_TARGET, '4' },
149                 { SPECIAL_RUNLEVEL3_TARGET, '3' },
150                 { SPECIAL_RUNLEVEL2_TARGET, '2' },
151                 { SPECIAL_RESCUE_TARGET,    '1' },
152                 { SPECIAL_POWEROFF_TARGET,  '0' },
153                 { SPECIAL_REBOOT_TARGET,    '6' },
154         };
155
156         unsigned i;
157
158         assert(t);
159
160         /* Tries to determine if this is a SysV runlevel and returns
161          * it if that is so. */
162
163         for (i = 0; i < ELEMENTSOF(table); i++)
164                 if (unit_has_name(UNIT(t), table[i].special))
165                         return table[i].runlevel;
166
167         return 0;
168 }
169
170 static const char* const target_state_table[_TARGET_STATE_MAX] = {
171         [TARGET_DEAD] = "dead",
172         [TARGET_ACTIVE] = "active"
173 };
174
175 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
176
177 const UnitVTable target_vtable = {
178         .suffix = ".target",
179
180         .load = unit_load_fragment_and_dropin,
181         .coldplug = target_coldplug,
182
183         .dump = target_dump,
184
185         .start = target_start,
186         .stop = target_stop,
187
188         .serialize = target_serialize,
189         .deserialize_item = target_deserialize_item,
190
191         .active_state = target_active_state,
192         .sub_state_to_string = target_sub_state_to_string,
193
194         .bus_message_handler = bus_target_message_handler
195 };