chiark / gitweb /
f1f656e6dd0f2fc0d5c72e36e169a17cac9c1520
[elogind.git] / src / target.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #include <unistd.h>
25
26 #include "unit.h"
27 #include "target.h"
28 #include "load-fragment.h"
29 #include "log.h"
30 #include "dbus-target.h"
31 #include "special.h"
32 #include "unit-name.h"
33
34 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
35         [TARGET_DEAD] = UNIT_INACTIVE,
36         [TARGET_ACTIVE] = UNIT_ACTIVE
37 };
38
39 static void target_set_state(Target *t, TargetState state) {
40         TargetState old_state;
41         assert(t);
42
43         old_state = t->state;
44         t->state = state;
45
46         if (state != old_state)
47                 log_debug("%s changed %s -> %s",
48                           t->meta.id,
49                           target_state_to_string(old_state),
50                           target_state_to_string(state));
51
52         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
53 }
54
55 static int target_add_default_dependencies(Target *t) {
56         assert(t);
57
58         /* Make sure targets are unloaded on shutdown */
59         return unit_add_dependency_by_name(UNIT(t), UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
60 }
61
62 static int target_add_getty_dependencies(Target *t) {
63         char *n;
64         int r;
65
66         assert(t);
67
68         if (!unit_has_name(UNIT(t), SPECIAL_GETTY_TARGET))
69                 return 0;
70
71         /* Automatically add in a serial getty on the kernel
72          * console */
73         if (t->meta.manager->console) {
74                 log_debug("Automatically adding serial getty for %s", t->meta.manager->console);
75                 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, t->meta.manager->console)))
76                         return -ENOMEM;
77
78                 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
79                 free(n);
80
81                 if (r < 0)
82                         return r;
83         }
84
85         /* Automatically add in a serial getty on the first
86          * virtualizer console */
87         if (access("/sys/class/tty/hvc0", F_OK) == 0) {
88                 log_debug("Automatic adding serial getty for hvc0");
89                 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, "hvc0")))
90                         return -ENOMEM;
91
92                 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
93                 free(n);
94
95                 if (r < 0)
96                         return r;
97         }
98
99         return 0;
100 }
101
102 static int target_load(Unit *u) {
103         Target *t = TARGET(u);
104         int r;
105
106         assert(t);
107
108         if ((r = unit_load_fragment_and_dropin(u)) < 0)
109                 return r;
110
111         /* This is a new unit? Then let's add in some extras */
112         if (u->meta.load_state == UNIT_LOADED) {
113                 if (u->meta.default_dependencies)
114                         if ((r = target_add_default_dependencies(t)) < 0)
115                                 return r;
116
117                 if ((r = target_add_getty_dependencies(t)) < 0)
118                         return r;
119         }
120
121         return 0;
122 }
123
124 static int target_coldplug(Unit *u) {
125         Target *t = TARGET(u);
126
127         assert(t);
128         assert(t->state == TARGET_DEAD);
129
130         if (t->deserialized_state != t->state)
131                 target_set_state(t, t->deserialized_state);
132
133         return 0;
134 }
135
136 static void target_dump(Unit *u, FILE *f, const char *prefix) {
137         Target *t = TARGET(u);
138
139         assert(t);
140         assert(f);
141
142         fprintf(f,
143                 "%sTarget State: %s\n",
144                 prefix, target_state_to_string(t->state));
145 }
146
147 static int target_start(Unit *u) {
148         Target *t = TARGET(u);
149
150         assert(t);
151         assert(t->state == TARGET_DEAD);
152
153         target_set_state(t, TARGET_ACTIVE);
154         return 0;
155 }
156
157 static int target_stop(Unit *u) {
158         Target *t = TARGET(u);
159
160         assert(t);
161         assert(t->state == TARGET_ACTIVE);
162
163         target_set_state(t, TARGET_DEAD);
164         return 0;
165 }
166
167 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
168         Target *s = TARGET(u);
169
170         assert(s);
171         assert(f);
172         assert(fds);
173
174         unit_serialize_item(u, f, "state", target_state_to_string(s->state));
175         return 0;
176 }
177
178 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
179         Target *s = TARGET(u);
180
181         assert(u);
182         assert(key);
183         assert(value);
184         assert(fds);
185
186         if (streq(key, "state")) {
187                 TargetState state;
188
189                 if ((state = target_state_from_string(value)) < 0)
190                         log_debug("Failed to parse state value %s", value);
191                 else
192                         s->deserialized_state = state;
193
194         } else
195                 log_debug("Unknown serialization key '%s'", key);
196
197         return 0;
198 }
199
200 static UnitActiveState target_active_state(Unit *u) {
201         assert(u);
202
203         return state_translation_table[TARGET(u)->state];
204 }
205
206 static const char *target_sub_state_to_string(Unit *u) {
207         assert(u);
208
209         return target_state_to_string(TARGET(u)->state);
210 }
211
212 static const char* const target_state_table[_TARGET_STATE_MAX] = {
213         [TARGET_DEAD] = "dead",
214         [TARGET_ACTIVE] = "active"
215 };
216
217 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
218
219 const UnitVTable target_vtable = {
220         .suffix = ".target",
221
222         .load = target_load,
223         .coldplug = target_coldplug,
224
225         .dump = target_dump,
226
227         .start = target_start,
228         .stop = target_stop,
229
230         .serialize = target_serialize,
231         .deserialize_item = target_deserialize_item,
232
233         .active_state = target_active_state,
234         .sub_state_to_string = target_sub_state_to_string,
235
236         .bus_interface = "org.freedesktop.systemd1.Target",
237         .bus_message_handler = bus_target_message_handler
238 };