]>
Commit | Line | Data |
---|---|---|
a016b922 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <errno.h> | |
a016b922 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
07630cea | 23 | #include "dbus-slice.h" |
a016b922 | 24 | #include "log.h" |
cf0fbc49 | 25 | #include "slice.h" |
a016b922 | 26 | #include "special.h" |
07630cea LP |
27 | #include "string-util.h" |
28 | #include "strv.h" | |
a016b922 | 29 | #include "unit-name.h" |
efdb0237 | 30 | #include "unit.h" |
a016b922 LP |
31 | |
32 | static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = { | |
33 | [SLICE_DEAD] = UNIT_INACTIVE, | |
34 | [SLICE_ACTIVE] = UNIT_ACTIVE | |
35 | }; | |
36 | ||
1b4cd0cf LP |
37 | static void slice_init(Unit *u) { |
38 | assert(u); | |
39 | assert(u->load_state == UNIT_STUB); | |
40 | ||
41 | u->ignore_on_isolate = true; | |
42 | } | |
43 | ||
a016b922 LP |
44 | static void slice_set_state(Slice *t, SliceState state) { |
45 | SliceState old_state; | |
46 | assert(t); | |
47 | ||
48 | old_state = t->state; | |
49 | t->state = state; | |
50 | ||
51 | if (state != old_state) | |
52 | log_debug("%s changed %s -> %s", | |
53 | UNIT(t)->id, | |
54 | slice_state_to_string(old_state), | |
55 | slice_state_to_string(state)); | |
56 | ||
57 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true); | |
58 | } | |
59 | ||
4ad49000 | 60 | static int slice_add_parent_slice(Slice *s) { |
a016b922 | 61 | char *a, *dash; |
a016b922 | 62 | Unit *parent; |
4ad49000 | 63 | int r; |
a016b922 LP |
64 | |
65 | assert(s); | |
66 | ||
4ad49000 | 67 | if (UNIT_ISSET(UNIT(s)->slice)) |
a016b922 LP |
68 | return 0; |
69 | ||
4ad49000 | 70 | if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE)) |
a016b922 LP |
71 | return 0; |
72 | ||
4ad49000 LP |
73 | a = strdupa(UNIT(s)->id); |
74 | dash = strrchr(a, '-'); | |
75 | if (dash) | |
76 | strcpy(dash, ".slice"); | |
77 | else | |
78 | a = (char*) SPECIAL_ROOT_SLICE; | |
a016b922 LP |
79 | |
80 | r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent); | |
81 | if (r < 0) | |
82 | return r; | |
83 | ||
84 | unit_ref_set(&UNIT(s)->slice, parent); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | static int slice_add_default_dependencies(Slice *s) { | |
89 | int r; | |
90 | ||
91 | assert(s); | |
92 | ||
4c9ea260 LP |
93 | if (!UNIT(s)->default_dependencies) |
94 | return 0; | |
95 | ||
a016b922 | 96 | /* Make sure slices are unloaded on shutdown */ |
6c12b52e LP |
97 | r = unit_add_two_dependencies_by_name( |
98 | UNIT(s), | |
99 | UNIT_BEFORE, UNIT_CONFLICTS, | |
100 | SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
a016b922 LP |
101 | if (r < 0) |
102 | return r; | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | static int slice_verify(Slice *s) { | |
93c47472 LP |
108 | _cleanup_free_ char *parent = NULL; |
109 | int r; | |
110 | ||
a016b922 LP |
111 | assert(s); |
112 | ||
113 | if (UNIT(s)->load_state != UNIT_LOADED) | |
114 | return 0; | |
115 | ||
93c47472 | 116 | if (!slice_name_is_valid(UNIT(s)->id)) { |
f2341e0a | 117 | log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id); |
93c47472 LP |
118 | return -EINVAL; |
119 | } | |
120 | ||
121 | r = slice_build_parent_slice(UNIT(s)->id, &parent); | |
122 | if (r < 0) | |
f2341e0a | 123 | return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m"); |
93c47472 LP |
124 | |
125 | if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) { | |
f2341e0a | 126 | log_unit_error(UNIT(s), "Located outside of parent slice. Refusing."); |
93c47472 | 127 | return -EINVAL; |
a016b922 LP |
128 | } |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
8e4e851f LP |
133 | static int slice_load_root_slice(Unit *u) { |
134 | assert(u); | |
135 | ||
136 | if (!unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
137 | return 0; | |
138 | ||
f5869324 | 139 | u->perpetual = true; |
8e4e851f LP |
140 | |
141 | /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its | |
142 | * special semantics we synthesize it here, instead of relying on the unit file on disk. */ | |
143 | ||
144 | u->default_dependencies = false; | |
145 | u->ignore_on_isolate = true; | |
8e4e851f LP |
146 | |
147 | if (!u->description) | |
148 | u->description = strdup("Root Slice"); | |
149 | if (!u->documentation) | |
150 | u->documentation = strv_new("man:systemd.special(7)", NULL); | |
151 | ||
152 | return 1; | |
153 | } | |
154 | ||
a016b922 LP |
155 | static int slice_load(Unit *u) { |
156 | Slice *s = SLICE(u); | |
157 | int r; | |
158 | ||
159 | assert(s); | |
4f4afc88 | 160 | assert(u->load_state == UNIT_STUB); |
a016b922 | 161 | |
8e4e851f LP |
162 | r = slice_load_root_slice(u); |
163 | if (r < 0) | |
164 | return r; | |
4ad49000 | 165 | r = unit_load_fragment_and_dropin_optional(u); |
a016b922 LP |
166 | if (r < 0) |
167 | return r; | |
168 | ||
169 | /* This is a new unit? Then let's add in some extras */ | |
170 | if (u->load_state == UNIT_LOADED) { | |
171 | ||
598459ce LP |
172 | r = unit_patch_contexts(u); |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
4ad49000 | 176 | r = slice_add_parent_slice(s); |
a016b922 LP |
177 | if (r < 0) |
178 | return r; | |
179 | ||
4c9ea260 LP |
180 | r = slice_add_default_dependencies(s); |
181 | if (r < 0) | |
182 | return r; | |
a016b922 LP |
183 | } |
184 | ||
185 | return slice_verify(s); | |
186 | } | |
187 | ||
be847e82 | 188 | static int slice_coldplug(Unit *u) { |
a016b922 LP |
189 | Slice *t = SLICE(u); |
190 | ||
191 | assert(t); | |
192 | assert(t->state == SLICE_DEAD); | |
193 | ||
194 | if (t->deserialized_state != t->state) | |
195 | slice_set_state(t, t->deserialized_state); | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
200 | static void slice_dump(Unit *u, FILE *f, const char *prefix) { | |
201 | Slice *t = SLICE(u); | |
202 | ||
203 | assert(t); | |
204 | assert(f); | |
205 | ||
206 | fprintf(f, | |
207 | "%sSlice State: %s\n", | |
208 | prefix, slice_state_to_string(t->state)); | |
4ad49000 LP |
209 | |
210 | cgroup_context_dump(&t->cgroup_context, f, prefix); | |
a016b922 LP |
211 | } |
212 | ||
213 | static int slice_start(Unit *u) { | |
214 | Slice *t = SLICE(u); | |
4b58153d | 215 | int r; |
a016b922 LP |
216 | |
217 | assert(t); | |
218 | assert(t->state == SLICE_DEAD); | |
219 | ||
4b58153d LP |
220 | r = unit_acquire_invocation_id(u); |
221 | if (r < 0) | |
222 | return r; | |
223 | ||
5ad096b3 LP |
224 | (void) unit_realize_cgroup(u); |
225 | (void) unit_reset_cpu_usage(u); | |
a016b922 LP |
226 | |
227 | slice_set_state(t, SLICE_ACTIVE); | |
82a2b6bb | 228 | return 1; |
a016b922 LP |
229 | } |
230 | ||
231 | static int slice_stop(Unit *u) { | |
232 | Slice *t = SLICE(u); | |
233 | ||
234 | assert(t); | |
235 | assert(t->state == SLICE_ACTIVE); | |
236 | ||
4ad49000 LP |
237 | /* We do not need to destroy the cgroup explicitly, |
238 | * unit_notify() will do that for us anyway. */ | |
a016b922 LP |
239 | |
240 | slice_set_state(t, SLICE_DEAD); | |
82a2b6bb | 241 | return 1; |
a016b922 LP |
242 | } |
243 | ||
718db961 | 244 | static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
a016b922 LP |
245 | return unit_kill_common(u, who, signo, -1, -1, error); |
246 | } | |
247 | ||
248 | static int slice_serialize(Unit *u, FILE *f, FDSet *fds) { | |
249 | Slice *s = SLICE(u); | |
250 | ||
251 | assert(s); | |
252 | assert(f); | |
253 | assert(fds); | |
254 | ||
255 | unit_serialize_item(u, f, "state", slice_state_to_string(s->state)); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
260 | Slice *s = SLICE(u); | |
261 | ||
262 | assert(u); | |
263 | assert(key); | |
264 | assert(value); | |
265 | assert(fds); | |
266 | ||
267 | if (streq(key, "state")) { | |
268 | SliceState state; | |
269 | ||
270 | state = slice_state_from_string(value); | |
271 | if (state < 0) | |
272 | log_debug("Failed to parse state value %s", value); | |
273 | else | |
274 | s->deserialized_state = state; | |
275 | ||
276 | } else | |
277 | log_debug("Unknown serialization key '%s'", key); | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | _pure_ static UnitActiveState slice_active_state(Unit *u) { | |
283 | assert(u); | |
284 | ||
285 | return state_translation_table[SLICE(u)->state]; | |
286 | } | |
287 | ||
288 | _pure_ static const char *slice_sub_state_to_string(Unit *u) { | |
289 | assert(u); | |
290 | ||
291 | return slice_state_to_string(SLICE(u)->state); | |
292 | } | |
293 | ||
ba64af90 | 294 | static void slice_enumerate(Manager *m) { |
efdb0237 LP |
295 | Unit *u; |
296 | int r; | |
297 | ||
298 | assert(m); | |
299 | ||
300 | u = manager_get_unit(m, SPECIAL_ROOT_SLICE); | |
301 | if (!u) { | |
a581e45a | 302 | r = unit_new_for_name(m, sizeof(Slice), SPECIAL_ROOT_SLICE, &u); |
efdb0237 | 303 | if (r < 0) { |
a581e45a | 304 | log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_SLICE " unit: %m"); |
ba64af90 | 305 | return; |
efdb0237 LP |
306 | } |
307 | } | |
308 | ||
f5869324 | 309 | u->perpetual = true; |
efdb0237 LP |
310 | SLICE(u)->deserialized_state = SLICE_ACTIVE; |
311 | ||
efdb0237 LP |
312 | unit_add_to_load_queue(u); |
313 | unit_add_to_dbus_queue(u); | |
efdb0237 LP |
314 | } |
315 | ||
a016b922 LP |
316 | const UnitVTable slice_vtable = { |
317 | .object_size = sizeof(Slice), | |
718db961 LP |
318 | .cgroup_context_offset = offsetof(Slice, cgroup_context), |
319 | ||
a016b922 LP |
320 | .sections = |
321 | "Unit\0" | |
322 | "Slice\0" | |
323 | "Install\0", | |
4ad49000 | 324 | .private_section = "Slice", |
4ad49000 | 325 | |
17f62e9b | 326 | .can_transient = true, |
a016b922 | 327 | |
1b4cd0cf | 328 | .init = slice_init, |
a016b922 | 329 | .load = slice_load, |
4ad49000 | 330 | |
a016b922 LP |
331 | .coldplug = slice_coldplug, |
332 | ||
333 | .dump = slice_dump, | |
334 | ||
335 | .start = slice_start, | |
336 | .stop = slice_stop, | |
337 | ||
338 | .kill = slice_kill, | |
339 | ||
340 | .serialize = slice_serialize, | |
341 | .deserialize_item = slice_deserialize_item, | |
342 | ||
343 | .active_state = slice_active_state, | |
344 | .sub_state_to_string = slice_sub_state_to_string, | |
345 | ||
718db961 | 346 | .bus_vtable = bus_slice_vtable, |
8e2af478 LP |
347 | .bus_set_property = bus_slice_set_property, |
348 | .bus_commit_properties = bus_slice_commit_properties, | |
a016b922 | 349 | |
efdb0237 LP |
350 | .enumerate = slice_enumerate, |
351 | ||
a016b922 LP |
352 | .status_message_formats = { |
353 | .finished_start_job = { | |
4ad49000 | 354 | [JOB_DONE] = "Created slice %s.", |
a016b922 LP |
355 | }, |
356 | .finished_stop_job = { | |
4ad49000 | 357 | [JOB_DONE] = "Removed slice %s.", |
a016b922 LP |
358 | }, |
359 | }, | |
360 | }; |