chiark / gitweb /
unit: distuingish mandatory from triggering conditions
[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], true);
53 }
54
55 static int target_add_default_dependencies(Target *t) {
56         Iterator i;
57         Unit *other;
58         int r;
59
60         assert(t);
61
62         /* Imply ordering for requirement dependencies on target
63          * units. Note that when the user created a contradicting
64          * ordering manually we won't add anything in here to make
65          * sure we don't create a loop. */
66
67         SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES], i)
68                 if ((r = unit_add_default_target_dependency(other, UNIT(t))) < 0)
69                     return r;
70
71         SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
72                 if ((r = unit_add_default_target_dependency(other, UNIT(t))) < 0)
73                     return r;
74
75         SET_FOREACH(other, t->meta.dependencies[UNIT_WANTS], i)
76                 if ((r = unit_add_default_target_dependency(other, UNIT(t))) < 0)
77                     return r;
78
79         /* Make sure targets are unloaded on shutdown */
80         return unit_add_dependency_by_name(UNIT(t), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
81 }
82
83 static int target_add_getty_dependencies(Target *t) {
84         char *n, *active;
85         int r;
86
87         assert(t);
88
89         if (!unit_has_name(UNIT(t), SPECIAL_GETTY_TARGET))
90                 return 0;
91
92         if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
93                 const char *tty;
94
95                 truncate_nl(active);
96                 if ((tty = strrchr(active, ' ')))
97                         tty ++;
98                 else
99                         tty = active;
100
101                 /* Automatically add in a serial getty on the kernel
102                  * console */
103                 if (!tty_is_vc(tty)) {
104
105                         /* We assume that gettys on virtual terminals are
106                          * started via manual configuration and do this magic
107                          * only for non-VC terminals. */
108
109                         log_debug("Automatically adding serial getty for /dev/%s", tty);
110                         if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, tty))) {
111                                 free(active);
112                                 return -ENOMEM;
113                         }
114
115                         r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
116                         free(n);
117
118                         if (r < 0) {
119                                 free(active);
120                                 return r;
121                         }
122                 }
123
124                 free(active);
125         }
126
127         /* Automatically add in a serial getty on the first
128          * virtualizer console */
129         if (access("/sys/class/tty/hvc0", F_OK) == 0) {
130                 log_debug("Automatic adding serial getty for hvc0");
131                 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, "hvc0")))
132                         return -ENOMEM;
133
134                 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
135                 free(n);
136
137                 if (r < 0)
138                         return r;
139         }
140
141         return 0;
142 }
143
144 static int target_load(Unit *u) {
145         Target *t = TARGET(u);
146         int r;
147
148         assert(t);
149
150         if ((r = unit_load_fragment_and_dropin(u)) < 0)
151                 return r;
152
153         /* This is a new unit? Then let's add in some extras */
154         if (u->meta.load_state == UNIT_LOADED) {
155                 if (u->meta.default_dependencies)
156                         if ((r = target_add_default_dependencies(t)) < 0)
157                                 return r;
158
159                 if ((r = target_add_getty_dependencies(t)) < 0)
160                         return r;
161         }
162
163         return 0;
164 }
165
166 static int target_coldplug(Unit *u) {
167         Target *t = TARGET(u);
168
169         assert(t);
170         assert(t->state == TARGET_DEAD);
171
172         if (t->deserialized_state != t->state)
173                 target_set_state(t, t->deserialized_state);
174
175         return 0;
176 }
177
178 static void target_dump(Unit *u, FILE *f, const char *prefix) {
179         Target *t = TARGET(u);
180
181         assert(t);
182         assert(f);
183
184         fprintf(f,
185                 "%sTarget State: %s\n",
186                 prefix, target_state_to_string(t->state));
187 }
188
189 static int target_start(Unit *u) {
190         Target *t = TARGET(u);
191
192         assert(t);
193         assert(t->state == TARGET_DEAD);
194
195         target_set_state(t, TARGET_ACTIVE);
196         return 0;
197 }
198
199 static int target_stop(Unit *u) {
200         Target *t = TARGET(u);
201
202         assert(t);
203         assert(t->state == TARGET_ACTIVE);
204
205         target_set_state(t, TARGET_DEAD);
206         return 0;
207 }
208
209 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
210         Target *s = TARGET(u);
211
212         assert(s);
213         assert(f);
214         assert(fds);
215
216         unit_serialize_item(u, f, "state", target_state_to_string(s->state));
217         return 0;
218 }
219
220 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
221         Target *s = TARGET(u);
222
223         assert(u);
224         assert(key);
225         assert(value);
226         assert(fds);
227
228         if (streq(key, "state")) {
229                 TargetState state;
230
231                 if ((state = target_state_from_string(value)) < 0)
232                         log_debug("Failed to parse state value %s", value);
233                 else
234                         s->deserialized_state = state;
235
236         } else
237                 log_debug("Unknown serialization key '%s'", key);
238
239         return 0;
240 }
241
242 static UnitActiveState target_active_state(Unit *u) {
243         assert(u);
244
245         return state_translation_table[TARGET(u)->state];
246 }
247
248 static const char *target_sub_state_to_string(Unit *u) {
249         assert(u);
250
251         return target_state_to_string(TARGET(u)->state);
252 }
253
254 static const char* const target_state_table[_TARGET_STATE_MAX] = {
255         [TARGET_DEAD] = "dead",
256         [TARGET_ACTIVE] = "active"
257 };
258
259 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
260
261 const UnitVTable target_vtable = {
262         .suffix = ".target",
263
264         .load = target_load,
265         .coldplug = target_coldplug,
266
267         .dump = target_dump,
268
269         .start = target_start,
270         .stop = target_stop,
271
272         .serialize = target_serialize,
273         .deserialize_item = target_deserialize_item,
274
275         .active_state = target_active_state,
276         .sub_state_to_string = target_sub_state_to_string,
277
278         .bus_interface = "org.freedesktop.systemd1.Target",
279         .bus_message_handler = bus_target_message_handler
280 };