]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a016b922 LP |
2 | |
3 | #include <errno.h> | |
a016b922 | 4 | |
b5efdb8a | 5 | #include "alloc-util.h" |
07630cea | 6 | #include "dbus-slice.h" |
6fcbec6f | 7 | #include "dbus-unit.h" |
a016b922 | 8 | #include "log.h" |
d68c645b | 9 | #include "serialize.h" |
cf0fbc49 | 10 | #include "slice.h" |
a016b922 | 11 | #include "special.h" |
07630cea LP |
12 | #include "string-util.h" |
13 | #include "strv.h" | |
a016b922 | 14 | #include "unit-name.h" |
efdb0237 | 15 | #include "unit.h" |
a016b922 LP |
16 | |
17 | static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = { | |
18 | [SLICE_DEAD] = UNIT_INACTIVE, | |
19 | [SLICE_ACTIVE] = UNIT_ACTIVE | |
20 | }; | |
21 | ||
1b4cd0cf LP |
22 | static void slice_init(Unit *u) { |
23 | assert(u); | |
24 | assert(u->load_state == UNIT_STUB); | |
25 | ||
26 | u->ignore_on_isolate = true; | |
27 | } | |
28 | ||
a016b922 LP |
29 | static void slice_set_state(Slice *t, SliceState state) { |
30 | SliceState old_state; | |
31 | assert(t); | |
32 | ||
6fcbec6f LP |
33 | if (t->state != state) |
34 | bus_unit_send_pending_change_signal(UNIT(t), false); | |
35 | ||
a016b922 LP |
36 | old_state = t->state; |
37 | t->state = state; | |
38 | ||
39 | if (state != old_state) | |
40 | log_debug("%s changed %s -> %s", | |
41 | UNIT(t)->id, | |
42 | slice_state_to_string(old_state), | |
43 | slice_state_to_string(state)); | |
44 | ||
2ad2e41a | 45 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0); |
a016b922 LP |
46 | } |
47 | ||
4ad49000 | 48 | static int slice_add_parent_slice(Slice *s) { |
a7894207 ZJS |
49 | Unit *u = UNIT(s), *parent; |
50 | _cleanup_free_ char *a = NULL; | |
4ad49000 | 51 | int r; |
a016b922 LP |
52 | |
53 | assert(s); | |
54 | ||
a7894207 | 55 | if (UNIT_ISSET(u->slice)) |
a016b922 LP |
56 | return 0; |
57 | ||
a7894207 ZJS |
58 | r = slice_build_parent_slice(u->id, &a); |
59 | if (r <= 0) /* 0 means root slice */ | |
60 | return r; | |
a016b922 | 61 | |
a7894207 | 62 | r = manager_load_unit(u->manager, a, NULL, NULL, &parent); |
a016b922 LP |
63 | if (r < 0) |
64 | return r; | |
65 | ||
7f7d01ed | 66 | unit_ref_set(&u->slice, u, parent); |
a016b922 LP |
67 | return 0; |
68 | } | |
69 | ||
70 | static int slice_add_default_dependencies(Slice *s) { | |
71 | int r; | |
72 | ||
73 | assert(s); | |
74 | ||
4c9ea260 LP |
75 | if (!UNIT(s)->default_dependencies) |
76 | return 0; | |
77 | ||
a016b922 | 78 | /* Make sure slices are unloaded on shutdown */ |
6c12b52e LP |
79 | r = unit_add_two_dependencies_by_name( |
80 | UNIT(s), | |
81 | UNIT_BEFORE, UNIT_CONFLICTS, | |
5a724170 | 82 | SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a016b922 LP |
83 | if (r < 0) |
84 | return r; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | static int slice_verify(Slice *s) { | |
93c47472 LP |
90 | _cleanup_free_ char *parent = NULL; |
91 | int r; | |
92 | ||
a016b922 LP |
93 | assert(s); |
94 | ||
95 | if (UNIT(s)->load_state != UNIT_LOADED) | |
96 | return 0; | |
97 | ||
93c47472 | 98 | if (!slice_name_is_valid(UNIT(s)->id)) { |
f2341e0a | 99 | log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id); |
6f40aa45 | 100 | return -ENOEXEC; |
93c47472 LP |
101 | } |
102 | ||
103 | r = slice_build_parent_slice(UNIT(s)->id, &parent); | |
104 | if (r < 0) | |
f2341e0a | 105 | return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m"); |
93c47472 LP |
106 | |
107 | if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) { | |
f2341e0a | 108 | log_unit_error(UNIT(s), "Located outside of parent slice. Refusing."); |
6f40aa45 | 109 | return -ENOEXEC; |
a016b922 LP |
110 | } |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
8e4e851f LP |
115 | static int slice_load_root_slice(Unit *u) { |
116 | assert(u); | |
117 | ||
118 | if (!unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
119 | return 0; | |
120 | ||
f5869324 | 121 | u->perpetual = true; |
8e4e851f LP |
122 | |
123 | /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its | |
124 | * special semantics we synthesize it here, instead of relying on the unit file on disk. */ | |
125 | ||
126 | u->default_dependencies = false; | |
8e4e851f LP |
127 | |
128 | if (!u->description) | |
129 | u->description = strdup("Root Slice"); | |
130 | if (!u->documentation) | |
bea1a013 | 131 | u->documentation = strv_new("man:systemd.special(7)"); |
8e4e851f LP |
132 | |
133 | return 1; | |
134 | } | |
135 | ||
d8e5a933 AJ |
136 | static int slice_load_system_slice(Unit *u) { |
137 | assert(u); | |
138 | ||
139 | if (!MANAGER_IS_SYSTEM(u->manager)) | |
140 | return 0; | |
141 | if (!unit_has_name(u, SPECIAL_SYSTEM_SLICE)) | |
142 | return 0; | |
143 | ||
144 | u->perpetual = true; | |
145 | ||
146 | /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its | |
147 | * special semantics we synthesize it here, instead of relying on the unit file on disk. */ | |
148 | ||
149 | u->default_dependencies = false; | |
150 | ||
151 | if (!u->description) | |
152 | u->description = strdup("System Slice"); | |
153 | if (!u->documentation) | |
bea1a013 | 154 | u->documentation = strv_new("man:systemd.special(7)"); |
d8e5a933 AJ |
155 | |
156 | return 1; | |
157 | } | |
158 | ||
a016b922 LP |
159 | static int slice_load(Unit *u) { |
160 | Slice *s = SLICE(u); | |
161 | int r; | |
162 | ||
163 | assert(s); | |
4f4afc88 | 164 | assert(u->load_state == UNIT_STUB); |
a016b922 | 165 | |
8e4e851f LP |
166 | r = slice_load_root_slice(u); |
167 | if (r < 0) | |
168 | return r; | |
d8e5a933 AJ |
169 | r = slice_load_system_slice(u); |
170 | if (r < 0) | |
171 | return r; | |
172 | ||
4ad49000 | 173 | r = unit_load_fragment_and_dropin_optional(u); |
a016b922 LP |
174 | if (r < 0) |
175 | return r; | |
176 | ||
177 | /* This is a new unit? Then let's add in some extras */ | |
178 | if (u->load_state == UNIT_LOADED) { | |
179 | ||
598459ce LP |
180 | r = unit_patch_contexts(u); |
181 | if (r < 0) | |
182 | return r; | |
183 | ||
4ad49000 | 184 | r = slice_add_parent_slice(s); |
a016b922 LP |
185 | if (r < 0) |
186 | return r; | |
187 | ||
4c9ea260 LP |
188 | r = slice_add_default_dependencies(s); |
189 | if (r < 0) | |
190 | return r; | |
a016b922 LP |
191 | } |
192 | ||
193 | return slice_verify(s); | |
194 | } | |
195 | ||
be847e82 | 196 | static int slice_coldplug(Unit *u) { |
a016b922 LP |
197 | Slice *t = SLICE(u); |
198 | ||
199 | assert(t); | |
200 | assert(t->state == SLICE_DEAD); | |
201 | ||
202 | if (t->deserialized_state != t->state) | |
203 | slice_set_state(t, t->deserialized_state); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | static void slice_dump(Unit *u, FILE *f, const char *prefix) { | |
209 | Slice *t = SLICE(u); | |
210 | ||
211 | assert(t); | |
212 | assert(f); | |
213 | ||
214 | fprintf(f, | |
215 | "%sSlice State: %s\n", | |
216 | prefix, slice_state_to_string(t->state)); | |
4ad49000 | 217 | |
bc0623df | 218 | cgroup_context_dump(UNIT(t), f, prefix); |
a016b922 LP |
219 | } |
220 | ||
221 | static int slice_start(Unit *u) { | |
222 | Slice *t = SLICE(u); | |
4b58153d | 223 | int r; |
a016b922 LP |
224 | |
225 | assert(t); | |
226 | assert(t->state == SLICE_DEAD); | |
227 | ||
4b58153d LP |
228 | r = unit_acquire_invocation_id(u); |
229 | if (r < 0) | |
230 | return r; | |
231 | ||
5ad096b3 | 232 | (void) unit_realize_cgroup(u); |
9b2559a1 | 233 | (void) unit_reset_accounting(u); |
a016b922 LP |
234 | |
235 | slice_set_state(t, SLICE_ACTIVE); | |
82a2b6bb | 236 | return 1; |
a016b922 LP |
237 | } |
238 | ||
239 | static int slice_stop(Unit *u) { | |
240 | Slice *t = SLICE(u); | |
241 | ||
242 | assert(t); | |
243 | assert(t->state == SLICE_ACTIVE); | |
244 | ||
4ad49000 LP |
245 | /* We do not need to destroy the cgroup explicitly, |
246 | * unit_notify() will do that for us anyway. */ | |
a016b922 LP |
247 | |
248 | slice_set_state(t, SLICE_DEAD); | |
82a2b6bb | 249 | return 1; |
a016b922 LP |
250 | } |
251 | ||
718db961 | 252 | static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
a016b922 LP |
253 | return unit_kill_common(u, who, signo, -1, -1, error); |
254 | } | |
255 | ||
256 | static int slice_serialize(Unit *u, FILE *f, FDSet *fds) { | |
257 | Slice *s = SLICE(u); | |
258 | ||
259 | assert(s); | |
260 | assert(f); | |
261 | assert(fds); | |
262 | ||
d68c645b LP |
263 | (void) serialize_item(f, "state", slice_state_to_string(s->state)); |
264 | ||
a016b922 LP |
265 | return 0; |
266 | } | |
267 | ||
268 | static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
269 | Slice *s = SLICE(u); | |
270 | ||
271 | assert(u); | |
272 | assert(key); | |
273 | assert(value); | |
274 | assert(fds); | |
275 | ||
276 | if (streq(key, "state")) { | |
277 | SliceState state; | |
278 | ||
279 | state = slice_state_from_string(value); | |
280 | if (state < 0) | |
281 | log_debug("Failed to parse state value %s", value); | |
282 | else | |
283 | s->deserialized_state = state; | |
284 | ||
285 | } else | |
286 | log_debug("Unknown serialization key '%s'", key); | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | _pure_ static UnitActiveState slice_active_state(Unit *u) { | |
292 | assert(u); | |
293 | ||
294 | return state_translation_table[SLICE(u)->state]; | |
295 | } | |
296 | ||
297 | _pure_ static const char *slice_sub_state_to_string(Unit *u) { | |
298 | assert(u); | |
299 | ||
300 | return slice_state_to_string(SLICE(u)->state); | |
301 | } | |
302 | ||
cc6271f1 | 303 | static int slice_make_perpetual(Manager *m, const char *name, Unit **ret) { |
efdb0237 LP |
304 | Unit *u; |
305 | int r; | |
306 | ||
307 | assert(m); | |
cc6271f1 | 308 | assert(name); |
efdb0237 | 309 | |
d8e5a933 | 310 | u = manager_get_unit(m, name); |
efdb0237 | 311 | if (!u) { |
d8e5a933 | 312 | r = unit_new_for_name(m, sizeof(Slice), name, &u); |
cc6271f1 LP |
313 | if (r < 0) |
314 | return log_error_errno(r, "Failed to allocate the special %s unit: %m", name); | |
efdb0237 LP |
315 | } |
316 | ||
f5869324 | 317 | u->perpetual = true; |
efdb0237 LP |
318 | SLICE(u)->deserialized_state = SLICE_ACTIVE; |
319 | ||
efdb0237 LP |
320 | unit_add_to_load_queue(u); |
321 | unit_add_to_dbus_queue(u); | |
cc6271f1 LP |
322 | |
323 | if (ret) | |
324 | *ret = u; | |
325 | ||
326 | return 0; | |
efdb0237 LP |
327 | } |
328 | ||
04eb582a | 329 | static void slice_enumerate_perpetual(Manager *m) { |
cc6271f1 LP |
330 | Unit *u; |
331 | int r; | |
332 | ||
d8e5a933 AJ |
333 | assert(m); |
334 | ||
cc6271f1 | 335 | r = slice_make_perpetual(m, SPECIAL_ROOT_SLICE, &u); |
611c4f8a | 336 | if (r >= 0 && manager_owns_host_root_cgroup(m)) { |
cc6271f1 LP |
337 | Slice *s = SLICE(u); |
338 | ||
339 | /* If we are managing the root cgroup then this means our root slice covers the whole system, which | |
340 | * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's | |
341 | * hence turn accounting on here, so that our APIs to query this data are available. */ | |
342 | ||
343 | s->cgroup_context.cpu_accounting = true; | |
344 | s->cgroup_context.tasks_accounting = true; | |
345 | s->cgroup_context.memory_accounting = true; | |
346 | } | |
d8e5a933 AJ |
347 | |
348 | if (MANAGER_IS_SYSTEM(m)) | |
cc6271f1 | 349 | (void) slice_make_perpetual(m, SPECIAL_SYSTEM_SLICE, NULL); |
d8e5a933 AJ |
350 | } |
351 | ||
a016b922 LP |
352 | const UnitVTable slice_vtable = { |
353 | .object_size = sizeof(Slice), | |
718db961 LP |
354 | .cgroup_context_offset = offsetof(Slice, cgroup_context), |
355 | ||
a016b922 LP |
356 | .sections = |
357 | "Unit\0" | |
358 | "Slice\0" | |
359 | "Install\0", | |
4ad49000 | 360 | .private_section = "Slice", |
4ad49000 | 361 | |
17f62e9b | 362 | .can_transient = true, |
a016b922 | 363 | |
1b4cd0cf | 364 | .init = slice_init, |
a016b922 | 365 | .load = slice_load, |
4ad49000 | 366 | |
a016b922 LP |
367 | .coldplug = slice_coldplug, |
368 | ||
369 | .dump = slice_dump, | |
370 | ||
371 | .start = slice_start, | |
372 | .stop = slice_stop, | |
373 | ||
374 | .kill = slice_kill, | |
375 | ||
376 | .serialize = slice_serialize, | |
377 | .deserialize_item = slice_deserialize_item, | |
378 | ||
379 | .active_state = slice_active_state, | |
380 | .sub_state_to_string = slice_sub_state_to_string, | |
381 | ||
718db961 | 382 | .bus_vtable = bus_slice_vtable, |
8e2af478 LP |
383 | .bus_set_property = bus_slice_set_property, |
384 | .bus_commit_properties = bus_slice_commit_properties, | |
a016b922 | 385 | |
04eb582a | 386 | .enumerate_perpetual = slice_enumerate_perpetual, |
efdb0237 | 387 | |
a016b922 LP |
388 | .status_message_formats = { |
389 | .finished_start_job = { | |
4ad49000 | 390 | [JOB_DONE] = "Created slice %s.", |
a016b922 LP |
391 | }, |
392 | .finished_stop_job = { | |
4ad49000 | 393 | [JOB_DONE] = "Removed slice %s.", |
a016b922 LP |
394 | }, |
395 | }, | |
396 | }; |