]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a016b922 | 2 | |
836e4e7e | 3 | #include <stdio.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" |
4ea4abb6 | 9 | #include "manager.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" | |
efdb0237 | 15 | #include "unit.h" |
1cf40697 | 16 | #include "unit-name.h" |
a016b922 LP |
17 | |
18 | static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = { | |
17f6b640 YW |
19 | [SLICE_DEAD] = UNIT_INACTIVE, |
20 | [SLICE_ACTIVE] = UNIT_ACTIVE, | |
a016b922 LP |
21 | }; |
22 | ||
1b4cd0cf | 23 | static void slice_init(Unit *u) { |
9ca16f6f LP |
24 | Slice *s = ASSERT_PTR(SLICE(u)); |
25 | ||
1b4cd0cf LP |
26 | assert(u->load_state == UNIT_STUB); |
27 | ||
28 | u->ignore_on_isolate = true; | |
9ca16f6f LP |
29 | s->concurrency_hard_max = UINT_MAX; |
30 | s->concurrency_soft_max = UINT_MAX; | |
1b4cd0cf LP |
31 | } |
32 | ||
b7e4e152 | 33 | static void slice_set_state(Slice *s, SliceState state) { |
a016b922 | 34 | SliceState old_state; |
e9fa1bf7 | 35 | |
b7e4e152 | 36 | assert(s); |
a016b922 | 37 | |
b7e4e152 MY |
38 | if (s->state != state) |
39 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
6fcbec6f | 40 | |
b7e4e152 MY |
41 | old_state = s->state; |
42 | s->state = state; | |
a016b922 LP |
43 | |
44 | if (state != old_state) | |
b7e4e152 MY |
45 | log_unit_debug(UNIT(s), "Changed %s -> %s", |
46 | slice_state_to_string(old_state), slice_state_to_string(state)); | |
a016b922 | 47 | |
b7e4e152 | 48 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true); |
a016b922 LP |
49 | } |
50 | ||
4ad49000 | 51 | static int slice_add_parent_slice(Slice *s) { |
e9fa1bf7 | 52 | Unit *u = UNIT(ASSERT_PTR(s)); |
a7894207 | 53 | _cleanup_free_ char *a = NULL; |
4ad49000 | 54 | int r; |
a016b922 | 55 | |
12f64221 | 56 | if (UNIT_GET_SLICE(u)) |
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 | |
d219a2b0 | 63 | return unit_add_dependency_by_name(u, UNIT_IN_SLICE, a, true, UNIT_DEPENDENCY_IMPLICIT); |
a016b922 LP |
64 | } |
65 | ||
66 | static int slice_add_default_dependencies(Slice *s) { | |
3835b9aa LB |
67 | int r; |
68 | ||
a016b922 LP |
69 | assert(s); |
70 | ||
4c9ea260 LP |
71 | if (!UNIT(s)->default_dependencies) |
72 | return 0; | |
73 | ||
a016b922 | 74 | /* Make sure slices are unloaded on shutdown */ |
3835b9aa LB |
75 | r = unit_add_two_dependencies_by_name( |
76 | UNIT(s), | |
77 | UNIT_BEFORE, UNIT_CONFLICTS, | |
78 | SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
79 | if (r < 0) | |
80 | return r; | |
81 | ||
82 | return 0; | |
a016b922 LP |
83 | } |
84 | ||
85 | static int slice_verify(Slice *s) { | |
93c47472 LP |
86 | _cleanup_free_ char *parent = NULL; |
87 | int r; | |
88 | ||
a016b922 | 89 | assert(s); |
75193d41 | 90 | assert(UNIT(s)->load_state == UNIT_LOADED); |
a016b922 | 91 | |
d85ff944 YW |
92 | if (!slice_name_is_valid(UNIT(s)->id)) |
93 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Slice name %s is not valid. Refusing.", UNIT(s)->id); | |
93c47472 LP |
94 | |
95 | r = slice_build_parent_slice(UNIT(s)->id, &parent); | |
96 | if (r < 0) | |
f2341e0a | 97 | return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m"); |
93c47472 | 98 | |
f660c7fa MG |
99 | /* If recursive errors are to be ignored, the parent slice should not be verified */ |
100 | if (UNIT(s)->manager && FLAGS_SET(UNIT(s)->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES)) | |
101 | return 0; | |
102 | ||
12f64221 | 103 | if (parent ? !unit_has_name(UNIT_GET_SLICE(UNIT(s)), parent) : !!UNIT_GET_SLICE(UNIT(s))) |
d85ff944 | 104 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Located outside of parent slice. Refusing."); |
a016b922 LP |
105 | |
106 | return 0; | |
107 | } | |
108 | ||
8e4e851f LP |
109 | static int slice_load_root_slice(Unit *u) { |
110 | assert(u); | |
111 | ||
112 | if (!unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
113 | return 0; | |
114 | ||
f5869324 | 115 | u->perpetual = true; |
8e4e851f LP |
116 | |
117 | /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its | |
118 | * special semantics we synthesize it here, instead of relying on the unit file on disk. */ | |
119 | ||
120 | u->default_dependencies = false; | |
8e4e851f LP |
121 | |
122 | if (!u->description) | |
123 | u->description = strdup("Root Slice"); | |
124 | if (!u->documentation) | |
bea1a013 | 125 | u->documentation = strv_new("man:systemd.special(7)"); |
8e4e851f LP |
126 | |
127 | return 1; | |
128 | } | |
129 | ||
d8e5a933 AJ |
130 | static int slice_load_system_slice(Unit *u) { |
131 | assert(u); | |
132 | ||
133 | if (!MANAGER_IS_SYSTEM(u->manager)) | |
134 | return 0; | |
135 | if (!unit_has_name(u, SPECIAL_SYSTEM_SLICE)) | |
136 | return 0; | |
137 | ||
138 | u->perpetual = true; | |
139 | ||
140 | /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its | |
141 | * special semantics we synthesize it here, instead of relying on the unit file on disk. */ | |
142 | ||
143 | u->default_dependencies = false; | |
144 | ||
145 | if (!u->description) | |
146 | u->description = strdup("System Slice"); | |
147 | if (!u->documentation) | |
bea1a013 | 148 | u->documentation = strv_new("man:systemd.special(7)"); |
d8e5a933 AJ |
149 | |
150 | return 1; | |
151 | } | |
152 | ||
a016b922 | 153 | static int slice_load(Unit *u) { |
e9fa1bf7 | 154 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 LP |
155 | int r; |
156 | ||
4f4afc88 | 157 | assert(u->load_state == UNIT_STUB); |
a016b922 | 158 | |
8e4e851f LP |
159 | r = slice_load_root_slice(u); |
160 | if (r < 0) | |
161 | return r; | |
d8e5a933 AJ |
162 | r = slice_load_system_slice(u); |
163 | if (r < 0) | |
164 | return r; | |
165 | ||
c3620770 | 166 | r = unit_load_fragment_and_dropin(u, false); |
a016b922 LP |
167 | if (r < 0) |
168 | return r; | |
169 | ||
75193d41 ZJS |
170 | if (u->load_state != UNIT_LOADED) |
171 | return 0; | |
a016b922 | 172 | |
75193d41 ZJS |
173 | /* This is a new unit? Then let's add in some extras */ |
174 | r = unit_patch_contexts(u); | |
175 | if (r < 0) | |
176 | return r; | |
598459ce | 177 | |
75193d41 ZJS |
178 | r = slice_add_parent_slice(s); |
179 | if (r < 0) | |
180 | return r; | |
a016b922 | 181 | |
75193d41 ZJS |
182 | r = slice_add_default_dependencies(s); |
183 | if (r < 0) | |
184 | return r; | |
a016b922 | 185 | |
4dd21726 ZJS |
186 | if (!u->description) { |
187 | _cleanup_free_ char *tmp = NULL; | |
188 | ||
189 | r = unit_name_to_path(u->id, &tmp); | |
190 | if (r >= 0) /* Failure is ignored… */ | |
191 | u->description = strjoin("Slice ", tmp); | |
192 | } | |
193 | ||
a016b922 LP |
194 | return slice_verify(s); |
195 | } | |
196 | ||
be847e82 | 197 | static int slice_coldplug(Unit *u) { |
e9fa1bf7 | 198 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 199 | |
e9fa1bf7 | 200 | assert(s->state == SLICE_DEAD); |
a016b922 | 201 | |
e9fa1bf7 MY |
202 | if (s->deserialized_state != s->state) |
203 | slice_set_state(s, s->deserialized_state); | |
a016b922 LP |
204 | |
205 | return 0; | |
206 | } | |
207 | ||
208 | static void slice_dump(Unit *u, FILE *f, const char *prefix) { | |
e9fa1bf7 | 209 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 210 | |
e9fa1bf7 | 211 | assert(s); |
a016b922 | 212 | assert(f); |
e9fa1bf7 | 213 | assert(prefix); |
a016b922 LP |
214 | |
215 | fprintf(f, | |
216 | "%sSlice State: %s\n", | |
e9fa1bf7 | 217 | prefix, slice_state_to_string(s->state)); |
4ad49000 | 218 | |
e9fa1bf7 | 219 | cgroup_context_dump(u, f, prefix); |
a016b922 LP |
220 | } |
221 | ||
222 | static int slice_start(Unit *u) { | |
e9fa1bf7 | 223 | Slice *s = ASSERT_PTR(SLICE(u)); |
4b58153d | 224 | int r; |
a016b922 | 225 | |
e9fa1bf7 | 226 | assert(s->state == SLICE_DEAD); |
a016b922 | 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 | 234 | |
e9fa1bf7 | 235 | slice_set_state(s, SLICE_ACTIVE); |
82a2b6bb | 236 | return 1; |
a016b922 LP |
237 | } |
238 | ||
239 | static int slice_stop(Unit *u) { | |
e9fa1bf7 | 240 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 241 | |
e9fa1bf7 | 242 | assert(s->state == SLICE_ACTIVE); |
a016b922 | 243 | |
4ad49000 LP |
244 | /* We do not need to destroy the cgroup explicitly, |
245 | * unit_notify() will do that for us anyway. */ | |
a016b922 | 246 | |
e9fa1bf7 | 247 | slice_set_state(s, SLICE_DEAD); |
82a2b6bb | 248 | return 1; |
a016b922 LP |
249 | } |
250 | ||
a016b922 | 251 | static int slice_serialize(Unit *u, FILE *f, FDSet *fds) { |
e9fa1bf7 | 252 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 253 | |
a016b922 LP |
254 | assert(f); |
255 | assert(fds); | |
256 | ||
d68c645b LP |
257 | (void) serialize_item(f, "state", slice_state_to_string(s->state)); |
258 | ||
a016b922 LP |
259 | return 0; |
260 | } | |
261 | ||
262 | static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
e9fa1bf7 | 263 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 264 | |
a016b922 LP |
265 | assert(key); |
266 | assert(value); | |
267 | assert(fds); | |
268 | ||
269 | if (streq(key, "state")) { | |
270 | SliceState state; | |
271 | ||
272 | state = slice_state_from_string(value); | |
273 | if (state < 0) | |
e9fa1bf7 | 274 | log_unit_debug(u, "Failed to parse state: %s", value); |
a016b922 LP |
275 | else |
276 | s->deserialized_state = state; | |
277 | ||
278 | } else | |
e9fa1bf7 | 279 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a016b922 LP |
280 | |
281 | return 0; | |
282 | } | |
283 | ||
d1e8e8b5 | 284 | static UnitActiveState slice_active_state(Unit *u) { |
e9fa1bf7 | 285 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 286 | |
e9fa1bf7 | 287 | return state_translation_table[s->state]; |
a016b922 LP |
288 | } |
289 | ||
d1e8e8b5 | 290 | static const char *slice_sub_state_to_string(Unit *u) { |
e9fa1bf7 | 291 | Slice *s = ASSERT_PTR(SLICE(u)); |
a016b922 | 292 | |
e9fa1bf7 | 293 | return slice_state_to_string(s->state); |
a016b922 LP |
294 | } |
295 | ||
cc6271f1 | 296 | static int slice_make_perpetual(Manager *m, const char *name, Unit **ret) { |
efdb0237 LP |
297 | Unit *u; |
298 | int r; | |
299 | ||
300 | assert(m); | |
cc6271f1 | 301 | assert(name); |
efdb0237 | 302 | |
d8e5a933 | 303 | u = manager_get_unit(m, name); |
efdb0237 | 304 | if (!u) { |
d8e5a933 | 305 | r = unit_new_for_name(m, sizeof(Slice), name, &u); |
cc6271f1 LP |
306 | if (r < 0) |
307 | return log_error_errno(r, "Failed to allocate the special %s unit: %m", name); | |
efdb0237 LP |
308 | } |
309 | ||
f5869324 | 310 | u->perpetual = true; |
efdb0237 LP |
311 | SLICE(u)->deserialized_state = SLICE_ACTIVE; |
312 | ||
efdb0237 LP |
313 | unit_add_to_load_queue(u); |
314 | unit_add_to_dbus_queue(u); | |
cc6271f1 LP |
315 | |
316 | if (ret) | |
317 | *ret = u; | |
318 | ||
319 | return 0; | |
efdb0237 LP |
320 | } |
321 | ||
04eb582a | 322 | static void slice_enumerate_perpetual(Manager *m) { |
cc6271f1 LP |
323 | Unit *u; |
324 | int r; | |
325 | ||
d8e5a933 AJ |
326 | assert(m); |
327 | ||
cc6271f1 | 328 | r = slice_make_perpetual(m, SPECIAL_ROOT_SLICE, &u); |
611c4f8a | 329 | if (r >= 0 && manager_owns_host_root_cgroup(m)) { |
cc6271f1 LP |
330 | Slice *s = SLICE(u); |
331 | ||
332 | /* If we are managing the root cgroup then this means our root slice covers the whole system, which | |
333 | * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's | |
334 | * hence turn accounting on here, so that our APIs to query this data are available. */ | |
335 | ||
cc6271f1 LP |
336 | s->cgroup_context.tasks_accounting = true; |
337 | s->cgroup_context.memory_accounting = true; | |
338 | } | |
d8e5a933 AJ |
339 | |
340 | if (MANAGER_IS_SYSTEM(m)) | |
cc6271f1 | 341 | (void) slice_make_perpetual(m, SPECIAL_SYSTEM_SLICE, NULL); |
d8e5a933 AJ |
342 | } |
343 | ||
b21bebbe MY |
344 | static bool slice_can_freeze(const Unit *u) { |
345 | assert(u); | |
d9e45bc3 | 346 | |
b21bebbe MY |
347 | Unit *member; |
348 | UNIT_FOREACH_DEPENDENCY(member, u, UNIT_ATOM_SLICE_OF) | |
16b6af6a | 349 | if (!unit_can_freeze(member)) |
5dc0c21b | 350 | return false; |
b21bebbe | 351 | |
d9e45bc3 MS |
352 | return true; |
353 | } | |
354 | ||
355 | static int slice_freezer_action(Unit *s, FreezerAction action) { | |
16b6af6a | 356 | FreezerAction child_action; |
d9e45bc3 MS |
357 | int r; |
358 | ||
359 | assert(s); | |
21fed6ea MY |
360 | assert(action >= 0); |
361 | assert(action < _FREEZER_ACTION_MAX); | |
16b6af6a AV |
362 | |
363 | if (action == FREEZER_FREEZE && !slice_can_freeze(s)) { | |
364 | /* We're intentionally only checking for FREEZER_FREEZE here and ignoring the | |
365 | * _BY_PARENT variant. If we're being frozen by parent, that means someone has | |
366 | * already checked if we can be frozen further up the call stack. No point to | |
367 | * redo that work */ | |
f375a968 | 368 | log_unit_warning(s, "Requested freezer operation is not supported by all children of the slice."); |
93c5b904 YW |
369 | return 0; |
370 | } | |
d9e45bc3 | 371 | |
16b6af6a AV |
372 | if (action == FREEZER_FREEZE) |
373 | child_action = FREEZER_PARENT_FREEZE; | |
374 | else if (action == FREEZER_THAW) | |
375 | child_action = FREEZER_PARENT_THAW; | |
376 | else | |
377 | child_action = action; | |
fcb0878f | 378 | |
f375a968 MY |
379 | Unit *member; |
380 | UNIT_FOREACH_DEPENDENCY(member, s, UNIT_ATOM_SLICE_OF) | |
381 | if (UNIT_VTABLE(member)->freezer_action) { | |
16b6af6a | 382 | r = UNIT_VTABLE(member)->freezer_action(member, child_action); |
f375a968 MY |
383 | if (r < 0) |
384 | return r; | |
385 | } | |
d9e45bc3 | 386 | |
d171e72e | 387 | return unit_cgroup_freezer_action(s, action); |
d9e45bc3 MS |
388 | } |
389 | ||
9ca16f6f LP |
390 | unsigned slice_get_currently_active(Slice *slice, Unit *ignore, bool with_pending) { |
391 | Unit *u = ASSERT_PTR(UNIT(slice)); | |
392 | ||
393 | /* If 'ignore' is non-NULL and a unit contained in this slice (or any below) we'll ignore it when | |
394 | * counting. */ | |
395 | ||
396 | unsigned n = 0; | |
397 | Unit *member; | |
398 | UNIT_FOREACH_DEPENDENCY(member, u, UNIT_ATOM_SLICE_OF) { | |
399 | if (member == ignore) | |
400 | continue; | |
401 | ||
402 | if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(member)) || | |
403 | (with_pending && member->job && IN_SET(member->job->type, JOB_START, JOB_RESTART, JOB_RELOAD))) | |
404 | n++; | |
405 | ||
406 | if (member->type == UNIT_SLICE) | |
407 | n += slice_get_currently_active(SLICE(member), ignore, with_pending); | |
408 | } | |
409 | ||
410 | return n; | |
411 | } | |
412 | ||
413 | bool slice_concurrency_soft_max_reached(Slice *slice, Unit *ignore) { | |
414 | assert(slice); | |
415 | ||
416 | if (slice->concurrency_soft_max != UINT_MAX && | |
417 | slice_get_currently_active(slice, ignore, /* with_pending= */ false) >= slice->concurrency_soft_max) | |
418 | return true; | |
419 | ||
420 | Unit *parent = UNIT_GET_SLICE(UNIT(slice)); | |
421 | if (parent) | |
422 | return slice_concurrency_soft_max_reached(SLICE(parent), ignore); | |
423 | ||
424 | return false; | |
425 | } | |
426 | ||
427 | bool slice_concurrency_hard_max_reached(Slice *slice, Unit *ignore) { | |
428 | assert(slice); | |
429 | ||
430 | if (slice->concurrency_hard_max != UINT_MAX && | |
431 | slice_get_currently_active(slice, ignore, /* with_pending= */ true) >= slice->concurrency_hard_max) | |
432 | return true; | |
433 | ||
434 | Unit *parent = UNIT_GET_SLICE(UNIT(slice)); | |
435 | if (parent) | |
436 | return slice_concurrency_hard_max_reached(SLICE(parent), ignore); | |
437 | ||
438 | return false; | |
439 | } | |
440 | ||
a016b922 LP |
441 | const UnitVTable slice_vtable = { |
442 | .object_size = sizeof(Slice), | |
718db961 | 443 | .cgroup_context_offset = offsetof(Slice, cgroup_context), |
9cc54544 | 444 | .cgroup_runtime_offset = offsetof(Slice, cgroup_runtime), |
718db961 | 445 | |
a016b922 LP |
446 | .sections = |
447 | "Unit\0" | |
448 | "Slice\0" | |
449 | "Install\0", | |
4ad49000 | 450 | .private_section = "Slice", |
4ad49000 | 451 | |
17f62e9b | 452 | .can_transient = true, |
4d824a4e | 453 | .can_set_managed_oom = true, |
a016b922 | 454 | |
1b4cd0cf | 455 | .init = slice_init, |
a016b922 | 456 | .load = slice_load, |
4ad49000 | 457 | |
a016b922 LP |
458 | .coldplug = slice_coldplug, |
459 | ||
460 | .dump = slice_dump, | |
461 | ||
462 | .start = slice_start, | |
463 | .stop = slice_stop, | |
464 | ||
16b6af6a | 465 | .freezer_action = slice_freezer_action, |
d9e45bc3 MS |
466 | .can_freeze = slice_can_freeze, |
467 | ||
a016b922 LP |
468 | .serialize = slice_serialize, |
469 | .deserialize_item = slice_deserialize_item, | |
470 | ||
471 | .active_state = slice_active_state, | |
472 | .sub_state_to_string = slice_sub_state_to_string, | |
473 | ||
8e2af478 LP |
474 | .bus_set_property = bus_slice_set_property, |
475 | .bus_commit_properties = bus_slice_commit_properties, | |
a016b922 | 476 | |
04eb582a | 477 | .enumerate_perpetual = slice_enumerate_perpetual, |
efdb0237 | 478 | |
a016b922 LP |
479 | .status_message_formats = { |
480 | .finished_start_job = { | |
4ad49000 | 481 | [JOB_DONE] = "Created slice %s.", |
a016b922 LP |
482 | }, |
483 | .finished_stop_job = { | |
4ad49000 | 484 | [JOB_DONE] = "Removed slice %s.", |
a016b922 LP |
485 | }, |
486 | }, | |
487 | }; |