]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
c22cbe26 | 2 | |
a7334b09 LP |
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 | ||
d46de8a1 | 22 | #include <errno.h> |
23a177ef | 23 | #include <signal.h> |
b2bb3dbe | 24 | #include <unistd.h> |
d46de8a1 | 25 | |
87f0e418 | 26 | #include "unit.h" |
c22cbe26 LP |
27 | #include "target.h" |
28 | #include "load-fragment.h" | |
fa068367 | 29 | #include "log.h" |
4139c1b2 | 30 | #include "dbus-target.h" |
514f4ef5 | 31 | #include "special.h" |
b2bb3dbe | 32 | #include "unit-name.h" |
c22cbe26 | 33 | |
fa068367 LP |
34 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
35 | [TARGET_DEAD] = UNIT_INACTIVE, | |
36 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
37 | }; | |
38 | ||
fa068367 LP |
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 | ||
e537352b | 46 | if (state != old_state) |
40d50879 | 47 | log_debug("%s changed %s -> %s", |
4cd1fbcc | 48 | t->meta.id, |
a16e1123 LP |
49 | target_state_to_string(old_state), |
50 | target_state_to_string(state)); | |
c22cbe26 | 51 | |
fa068367 LP |
52 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); |
53 | } | |
54 | ||
a40eb732 | 55 | static int target_add_default_dependencies(Target *t) { |
98bc2000 | 56 | assert(t); |
a40eb732 | 57 | |
b401e1fb LP |
58 | /* Make sure targets are unloaded on shutdown */ |
59 | return unit_add_dependency_by_name(UNIT(t), UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
a40eb732 LP |
60 | } |
61 | ||
b2bb3dbe LP |
62 | static int target_add_getty_dependencies(Target *t) { |
63 | char *n; | |
64 | int r; | |
65 | ||
66 | assert(t); | |
67 | ||
68 | if (!unit_has_name(UNIT(t), SPECIAL_GETTY_TARGET)) | |
69 | return 0; | |
70 | ||
71 | /* Automatically add in a serial getty on the kernel | |
72 | * console */ | |
73 | if (t->meta.manager->console) { | |
74 | log_debug("Automatically adding serial getty for %s", t->meta.manager->console); | |
75 | if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, t->meta.manager->console))) | |
76 | return -ENOMEM; | |
77 | ||
78 | r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true); | |
79 | free(n); | |
80 | ||
81 | if (r < 0) | |
82 | return r; | |
83 | } | |
84 | ||
85 | /* Automatically add in a serial getty on the first | |
86 | * virtualizer console */ | |
87 | if (access("/sys/class/tty/hvc0", F_OK) == 0) { | |
88 | log_debug("Automatic adding serial getty for hvc0"); | |
89 | if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, "hvc0"))) | |
90 | return -ENOMEM; | |
91 | ||
92 | r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true); | |
93 | free(n); | |
94 | ||
95 | if (r < 0) | |
96 | return r; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
a40eb732 LP |
102 | static int target_load(Unit *u) { |
103 | Target *t = TARGET(u); | |
104 | int r; | |
105 | ||
106 | assert(t); | |
107 | ||
108 | if ((r = unit_load_fragment_and_dropin(u)) < 0) | |
109 | return r; | |
110 | ||
111 | /* This is a new unit? Then let's add in some extras */ | |
112 | if (u->meta.load_state == UNIT_LOADED) { | |
113 | if (u->meta.default_dependencies) | |
114 | if ((r = target_add_default_dependencies(t)) < 0) | |
115 | return r; | |
b2bb3dbe LP |
116 | |
117 | if ((r = target_add_getty_dependencies(t)) < 0) | |
118 | return r; | |
a40eb732 LP |
119 | } |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
a16e1123 LP |
124 | static int target_coldplug(Unit *u) { |
125 | Target *t = TARGET(u); | |
126 | ||
127 | assert(t); | |
128 | assert(t->state == TARGET_DEAD); | |
129 | ||
130 | if (t->deserialized_state != t->state) | |
131 | target_set_state(t, t->deserialized_state); | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | static void target_dump(Unit *u, FILE *f, const char *prefix) { | |
137 | Target *t = TARGET(u); | |
138 | ||
139 | assert(t); | |
140 | assert(f); | |
141 | ||
142 | fprintf(f, | |
143 | "%sTarget State: %s\n", | |
144 | prefix, target_state_to_string(t->state)); | |
145 | } | |
146 | ||
fa068367 LP |
147 | static int target_start(Unit *u) { |
148 | Target *t = TARGET(u); | |
149 | ||
150 | assert(t); | |
151 | assert(t->state == TARGET_DEAD); | |
152 | ||
153 | target_set_state(t, TARGET_ACTIVE); | |
154 | return 0; | |
155 | } | |
c22cbe26 | 156 | |
fa068367 LP |
157 | static int target_stop(Unit *u) { |
158 | Target *t = TARGET(u); | |
159 | ||
160 | assert(t); | |
161 | assert(t->state == TARGET_ACTIVE); | |
162 | ||
163 | target_set_state(t, TARGET_DEAD); | |
164 | return 0; | |
c22cbe26 LP |
165 | } |
166 | ||
a16e1123 LP |
167 | static int target_serialize(Unit *u, FILE *f, FDSet *fds) { |
168 | Target *s = TARGET(u); | |
169 | ||
170 | assert(s); | |
171 | assert(f); | |
172 | assert(fds); | |
173 | ||
174 | unit_serialize_item(u, f, "state", target_state_to_string(s->state)); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
179 | Target *s = TARGET(u); | |
180 | ||
181 | assert(u); | |
182 | assert(key); | |
183 | assert(value); | |
184 | assert(fds); | |
185 | ||
186 | if (streq(key, "state")) { | |
187 | TargetState state; | |
188 | ||
189 | if ((state = target_state_from_string(value)) < 0) | |
190 | log_debug("Failed to parse state value %s", value); | |
191 | else | |
192 | s->deserialized_state = state; | |
193 | ||
194 | } else | |
195 | log_debug("Unknown serialization key '%s'", key); | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
87f0e418 | 200 | static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
201 | assert(u); |
202 | ||
203 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
204 | } |
205 | ||
10a94420 LP |
206 | static const char *target_sub_state_to_string(Unit *u) { |
207 | assert(u); | |
208 | ||
a16e1123 | 209 | return target_state_to_string(TARGET(u)->state); |
10a94420 LP |
210 | } |
211 | ||
a16e1123 LP |
212 | static const char* const target_state_table[_TARGET_STATE_MAX] = { |
213 | [TARGET_DEAD] = "dead", | |
214 | [TARGET_ACTIVE] = "active" | |
215 | }; | |
216 | ||
217 | DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState); | |
218 | ||
87f0e418 | 219 | const UnitVTable target_vtable = { |
c22cbe26 LP |
220 | .suffix = ".target", |
221 | ||
a40eb732 | 222 | .load = target_load, |
a16e1123 | 223 | .coldplug = target_coldplug, |
fa068367 LP |
224 | |
225 | .dump = target_dump, | |
226 | ||
227 | .start = target_start, | |
228 | .stop = target_stop, | |
c22cbe26 | 229 | |
a16e1123 LP |
230 | .serialize = target_serialize, |
231 | .deserialize_item = target_deserialize_item, | |
232 | ||
10a94420 | 233 | .active_state = target_active_state, |
4139c1b2 LP |
234 | .sub_state_to_string = target_sub_state_to_string, |
235 | ||
c4e2ceae | 236 | .bus_interface = "org.freedesktop.systemd1.Target", |
4139c1b2 | 237 | .bus_message_handler = bus_target_message_handler |
c22cbe26 | 238 | }; |