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