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