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