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