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