chiark / gitweb /
getty: do not auto-spawn getty's on VC ttys if console=ttyN is used
[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;
85         int r;
86
87         assert(t);
88
89         if (!unit_has_name(UNIT(t), SPECIAL_GETTY_TARGET))
90                 return 0;
91
92         /* Automatically add in a serial getty on the kernel
93          * console */
94         if (t->meta.manager->console && !tty_is_vc(t->meta.manager->console)) {
95
96                 /* We assume that gettys on virtual terminals are
97                  * started via manual configuration and do this magic
98                  * only for non-VC terminals. */
99
100                 log_debug("Automatically adding serial getty for %s", t->meta.manager->console);
101                 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, t->meta.manager->console)))
102                         return -ENOMEM;
103
104                 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
105                 free(n);
106
107                 if (r < 0)
108                         return r;
109         }
110
111         /* Automatically add in a serial getty on the first
112          * virtualizer console */
113         if (access("/sys/class/tty/hvc0", F_OK) == 0) {
114                 log_debug("Automatic adding serial getty for hvc0");
115                 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, "hvc0")))
116                         return -ENOMEM;
117
118                 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
119                 free(n);
120
121                 if (r < 0)
122                         return r;
123         }
124
125         return 0;
126 }
127
128 static int target_load(Unit *u) {
129         Target *t = TARGET(u);
130         int r;
131
132         assert(t);
133
134         if ((r = unit_load_fragment_and_dropin(u)) < 0)
135                 return r;
136
137         /* This is a new unit? Then let's add in some extras */
138         if (u->meta.load_state == UNIT_LOADED) {
139                 if (u->meta.default_dependencies)
140                         if ((r = target_add_default_dependencies(t)) < 0)
141                                 return r;
142
143                 if ((r = target_add_getty_dependencies(t)) < 0)
144                         return r;
145         }
146
147         return 0;
148 }
149
150 static int target_coldplug(Unit *u) {
151         Target *t = TARGET(u);
152
153         assert(t);
154         assert(t->state == TARGET_DEAD);
155
156         if (t->deserialized_state != t->state)
157                 target_set_state(t, t->deserialized_state);
158
159         return 0;
160 }
161
162 static void target_dump(Unit *u, FILE *f, const char *prefix) {
163         Target *t = TARGET(u);
164
165         assert(t);
166         assert(f);
167
168         fprintf(f,
169                 "%sTarget State: %s\n",
170                 prefix, target_state_to_string(t->state));
171 }
172
173 static int target_start(Unit *u) {
174         Target *t = TARGET(u);
175
176         assert(t);
177         assert(t->state == TARGET_DEAD);
178
179         target_set_state(t, TARGET_ACTIVE);
180         return 0;
181 }
182
183 static int target_stop(Unit *u) {
184         Target *t = TARGET(u);
185
186         assert(t);
187         assert(t->state == TARGET_ACTIVE);
188
189         target_set_state(t, TARGET_DEAD);
190         return 0;
191 }
192
193 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
194         Target *s = TARGET(u);
195
196         assert(s);
197         assert(f);
198         assert(fds);
199
200         unit_serialize_item(u, f, "state", target_state_to_string(s->state));
201         return 0;
202 }
203
204 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
205         Target *s = TARGET(u);
206
207         assert(u);
208         assert(key);
209         assert(value);
210         assert(fds);
211
212         if (streq(key, "state")) {
213                 TargetState state;
214
215                 if ((state = target_state_from_string(value)) < 0)
216                         log_debug("Failed to parse state value %s", value);
217                 else
218                         s->deserialized_state = state;
219
220         } else
221                 log_debug("Unknown serialization key '%s'", key);
222
223         return 0;
224 }
225
226 static UnitActiveState target_active_state(Unit *u) {
227         assert(u);
228
229         return state_translation_table[TARGET(u)->state];
230 }
231
232 static const char *target_sub_state_to_string(Unit *u) {
233         assert(u);
234
235         return target_state_to_string(TARGET(u)->state);
236 }
237
238 static const char* const target_state_table[_TARGET_STATE_MAX] = {
239         [TARGET_DEAD] = "dead",
240         [TARGET_ACTIVE] = "active"
241 };
242
243 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
244
245 const UnitVTable target_vtable = {
246         .suffix = ".target",
247
248         .load = target_load,
249         .coldplug = target_coldplug,
250
251         .dump = target_dump,
252
253         .start = target_start,
254         .stop = target_stop,
255
256         .serialize = target_serialize,
257         .deserialize_item = target_deserialize_item,
258
259         .active_state = target_active_state,
260         .sub_state_to_string = target_sub_state_to_string,
261
262         .bus_interface = "org.freedesktop.systemd1.Target",
263         .bus_message_handler = bus_target_message_handler
264 };