]>
Commit | Line | Data |
---|---|---|
c22cbe26 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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> |
d46de8a1 | 24 | |
87f0e418 | 25 | #include "unit.h" |
c22cbe26 LP |
26 | #include "target.h" |
27 | #include "load-fragment.h" | |
fa068367 | 28 | #include "log.h" |
4139c1b2 | 29 | #include "dbus-target.h" |
514f4ef5 | 30 | #include "special.h" |
c22cbe26 | 31 | |
fa068367 LP |
32 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
33 | [TARGET_DEAD] = UNIT_INACTIVE, | |
34 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
35 | }; | |
36 | ||
fa068367 LP |
37 | static void target_set_state(Target *t, TargetState state) { |
38 | TargetState old_state; | |
39 | assert(t); | |
40 | ||
41 | old_state = t->state; | |
42 | t->state = state; | |
43 | ||
e537352b | 44 | if (state != old_state) |
40d50879 | 45 | log_debug("%s changed %s -> %s", |
4cd1fbcc | 46 | t->meta.id, |
a16e1123 LP |
47 | target_state_to_string(old_state), |
48 | target_state_to_string(state)); | |
c22cbe26 | 49 | |
fa068367 LP |
50 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]); |
51 | } | |
52 | ||
a40eb732 LP |
53 | static int target_add_default_dependencies(Target *t) { |
54 | Iterator i; | |
55 | Unit *other; | |
56 | int r; | |
57 | ||
772a599e LP |
58 | /* Imply ordering for requirement dependencies on target |
59 | * units. Note that when the user created a contradicting | |
60 | * ordering manually we won't add anything in here to make | |
61 | * sure we don't create a loop. */ | |
a40eb732 LP |
62 | |
63 | SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES], i) | |
772a599e LP |
64 | if (!set_get(t->meta.dependencies[UNIT_BEFORE], other)) |
65 | if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0) | |
66 | return r; | |
a40eb732 | 67 | SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
772a599e LP |
68 | if (!set_get(t->meta.dependencies[UNIT_BEFORE], other)) |
69 | if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0) | |
70 | return r; | |
a40eb732 | 71 | SET_FOREACH(other, t->meta.dependencies[UNIT_WANTS], i) |
772a599e LP |
72 | if (!set_get(t->meta.dependencies[UNIT_BEFORE], other)) |
73 | if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0) | |
74 | return r; | |
a40eb732 LP |
75 | |
76 | return 0; | |
77 | } | |
78 | ||
79 | static int target_load(Unit *u) { | |
80 | Target *t = TARGET(u); | |
81 | int r; | |
82 | ||
83 | assert(t); | |
84 | ||
85 | if ((r = unit_load_fragment_and_dropin(u)) < 0) | |
86 | return r; | |
87 | ||
88 | /* This is a new unit? Then let's add in some extras */ | |
89 | if (u->meta.load_state == UNIT_LOADED) { | |
90 | if (u->meta.default_dependencies) | |
91 | if ((r = target_add_default_dependencies(t)) < 0) | |
92 | return r; | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
a16e1123 LP |
98 | static int target_coldplug(Unit *u) { |
99 | Target *t = TARGET(u); | |
100 | ||
101 | assert(t); | |
102 | assert(t->state == TARGET_DEAD); | |
103 | ||
104 | if (t->deserialized_state != t->state) | |
105 | target_set_state(t, t->deserialized_state); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | static void target_dump(Unit *u, FILE *f, const char *prefix) { | |
111 | Target *t = TARGET(u); | |
112 | ||
113 | assert(t); | |
114 | assert(f); | |
115 | ||
116 | fprintf(f, | |
117 | "%sTarget State: %s\n", | |
118 | prefix, target_state_to_string(t->state)); | |
119 | } | |
120 | ||
fa068367 LP |
121 | static int target_start(Unit *u) { |
122 | Target *t = TARGET(u); | |
123 | ||
124 | assert(t); | |
125 | assert(t->state == TARGET_DEAD); | |
126 | ||
127 | target_set_state(t, TARGET_ACTIVE); | |
128 | return 0; | |
129 | } | |
c22cbe26 | 130 | |
fa068367 LP |
131 | static int target_stop(Unit *u) { |
132 | Target *t = TARGET(u); | |
133 | ||
134 | assert(t); | |
135 | assert(t->state == TARGET_ACTIVE); | |
136 | ||
137 | target_set_state(t, TARGET_DEAD); | |
138 | return 0; | |
c22cbe26 LP |
139 | } |
140 | ||
a16e1123 LP |
141 | static int target_serialize(Unit *u, FILE *f, FDSet *fds) { |
142 | Target *s = TARGET(u); | |
143 | ||
144 | assert(s); | |
145 | assert(f); | |
146 | assert(fds); | |
147 | ||
148 | unit_serialize_item(u, f, "state", target_state_to_string(s->state)); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
153 | Target *s = TARGET(u); | |
154 | ||
155 | assert(u); | |
156 | assert(key); | |
157 | assert(value); | |
158 | assert(fds); | |
159 | ||
160 | if (streq(key, "state")) { | |
161 | TargetState state; | |
162 | ||
163 | if ((state = target_state_from_string(value)) < 0) | |
164 | log_debug("Failed to parse state value %s", value); | |
165 | else | |
166 | s->deserialized_state = state; | |
167 | ||
168 | } else | |
169 | log_debug("Unknown serialization key '%s'", key); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
87f0e418 | 174 | static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
175 | assert(u); |
176 | ||
177 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
178 | } |
179 | ||
10a94420 LP |
180 | static const char *target_sub_state_to_string(Unit *u) { |
181 | assert(u); | |
182 | ||
a16e1123 | 183 | return target_state_to_string(TARGET(u)->state); |
10a94420 LP |
184 | } |
185 | ||
e293f0f0 LP |
186 | int target_get_runlevel(Target *t) { |
187 | ||
188 | static const struct { | |
189 | const char *special; | |
190 | const int runlevel; | |
191 | } table[] = { | |
192 | { SPECIAL_RUNLEVEL5_TARGET, '5' }, | |
193 | { SPECIAL_RUNLEVEL4_TARGET, '4' }, | |
194 | { SPECIAL_RUNLEVEL3_TARGET, '3' }, | |
195 | { SPECIAL_RUNLEVEL2_TARGET, '2' }, | |
514f4ef5 LP |
196 | { SPECIAL_RESCUE_TARGET, '1' }, |
197 | { SPECIAL_POWEROFF_TARGET, '0' }, | |
198 | { SPECIAL_REBOOT_TARGET, '6' }, | |
e293f0f0 LP |
199 | }; |
200 | ||
201 | unsigned i; | |
202 | ||
203 | assert(t); | |
204 | ||
205 | /* Tries to determine if this is a SysV runlevel and returns | |
206 | * it if that is so. */ | |
207 | ||
208 | for (i = 0; i < ELEMENTSOF(table); i++) | |
209 | if (unit_has_name(UNIT(t), table[i].special)) | |
210 | return table[i].runlevel; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
a16e1123 LP |
215 | static const char* const target_state_table[_TARGET_STATE_MAX] = { |
216 | [TARGET_DEAD] = "dead", | |
217 | [TARGET_ACTIVE] = "active" | |
218 | }; | |
219 | ||
220 | DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState); | |
221 | ||
87f0e418 | 222 | const UnitVTable target_vtable = { |
c22cbe26 LP |
223 | .suffix = ".target", |
224 | ||
a40eb732 | 225 | .load = target_load, |
a16e1123 | 226 | .coldplug = target_coldplug, |
fa068367 LP |
227 | |
228 | .dump = target_dump, | |
229 | ||
230 | .start = target_start, | |
231 | .stop = target_stop, | |
c22cbe26 | 232 | |
a16e1123 LP |
233 | .serialize = target_serialize, |
234 | .deserialize_item = target_deserialize_item, | |
235 | ||
10a94420 | 236 | .active_state = target_active_state, |
4139c1b2 LP |
237 | .sub_state_to_string = target_sub_state_to_string, |
238 | ||
239 | .bus_message_handler = bus_target_message_handler | |
c22cbe26 | 240 | }; |