1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "dbus-slice.h"
10 #include "serialize.h"
13 #include "string-util.h"
16 #include "unit-name.h"
18 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
19 [SLICE_DEAD
] = UNIT_INACTIVE
,
20 [SLICE_ACTIVE
] = UNIT_ACTIVE
,
23 static void slice_init(Unit
*u
) {
24 Slice
*s
= ASSERT_PTR(SLICE(u
));
26 assert(u
->load_state
== UNIT_STUB
);
28 u
->ignore_on_isolate
= true;
29 s
->concurrency_hard_max
= UINT_MAX
;
30 s
->concurrency_soft_max
= UINT_MAX
;
33 static void slice_set_state(Slice
*s
, SliceState state
) {
38 if (s
->state
!= state
)
39 bus_unit_send_pending_change_signal(UNIT(s
), false);
44 if (state
!= old_state
)
45 log_unit_debug(UNIT(s
), "Changed %s -> %s",
46 slice_state_to_string(old_state
), slice_state_to_string(state
));
48 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
51 static int slice_add_parent_slice(Slice
*s
) {
52 Unit
*u
= UNIT(ASSERT_PTR(s
));
53 _cleanup_free_
char *a
= NULL
;
56 if (UNIT_GET_SLICE(u
))
59 r
= slice_build_parent_slice(u
->id
, &a
);
60 if (r
<= 0) /* 0 means root slice */
63 return unit_add_dependency_by_name(u
, UNIT_IN_SLICE
, a
, true, UNIT_DEPENDENCY_IMPLICIT
);
66 static int slice_add_default_dependencies(Slice
*s
) {
71 if (!UNIT(s
)->default_dependencies
)
74 /* Make sure slices are unloaded on shutdown */
75 r
= unit_add_two_dependencies_by_name(
77 UNIT_BEFORE
, UNIT_CONFLICTS
,
78 SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
85 static int slice_verify(Slice
*s
) {
86 _cleanup_free_
char *parent
= NULL
;
90 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
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
);
95 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
97 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
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
))
103 if (parent
? !unit_has_name(UNIT_GET_SLICE(UNIT(s
)), parent
) : !!UNIT_GET_SLICE(UNIT(s
)))
104 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Located outside of parent slice. Refusing.");
109 static int slice_load_root_slice(Unit
*u
) {
112 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
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. */
120 u
->default_dependencies
= false;
123 u
->description
= strdup("Root Slice");
124 if (!u
->documentation
)
125 u
->documentation
= strv_new("man:systemd.special(7)");
130 static int slice_load_system_slice(Unit
*u
) {
133 if (!MANAGER_IS_SYSTEM(u
->manager
))
135 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
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. */
143 u
->default_dependencies
= false;
146 u
->description
= strdup("System Slice");
147 if (!u
->documentation
)
148 u
->documentation
= strv_new("man:systemd.special(7)");
153 static int slice_load(Unit
*u
) {
154 Slice
*s
= ASSERT_PTR(SLICE(u
));
157 assert(u
->load_state
== UNIT_STUB
);
159 r
= slice_load_root_slice(u
);
162 r
= slice_load_system_slice(u
);
166 r
= unit_load_fragment_and_dropin(u
, false);
170 if (u
->load_state
!= UNIT_LOADED
)
173 /* This is a new unit? Then let's add in some extras */
174 r
= unit_patch_contexts(u
);
178 r
= slice_add_parent_slice(s
);
182 r
= slice_add_default_dependencies(s
);
186 if (!u
->description
) {
187 _cleanup_free_
char *tmp
= NULL
;
189 r
= unit_name_to_path(u
->id
, &tmp
);
190 if (r
>= 0) /* Failure is ignored… */
191 u
->description
= strjoin("Slice ", tmp
);
194 return slice_verify(s
);
197 static int slice_coldplug(Unit
*u
) {
198 Slice
*s
= ASSERT_PTR(SLICE(u
));
200 assert(s
->state
== SLICE_DEAD
);
202 if (s
->deserialized_state
!= s
->state
)
203 slice_set_state(s
, s
->deserialized_state
);
208 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
209 Slice
*s
= ASSERT_PTR(SLICE(u
));
216 "%sSlice State: %s\n",
217 prefix
, slice_state_to_string(s
->state
));
219 cgroup_context_dump(u
, f
, prefix
);
222 static int slice_start(Unit
*u
) {
223 Slice
*s
= ASSERT_PTR(SLICE(u
));
226 assert(s
->state
== SLICE_DEAD
);
228 r
= unit_acquire_invocation_id(u
);
232 (void) unit_realize_cgroup(u
);
233 (void) unit_reset_accounting(u
);
235 slice_set_state(s
, SLICE_ACTIVE
);
239 static int slice_stop(Unit
*u
) {
240 Slice
*s
= ASSERT_PTR(SLICE(u
));
242 assert(s
->state
== SLICE_ACTIVE
);
244 /* We do not need to destroy the cgroup explicitly,
245 * unit_notify() will do that for us anyway. */
247 slice_set_state(s
, SLICE_DEAD
);
251 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
252 Slice
*s
= ASSERT_PTR(SLICE(u
));
257 (void) serialize_item(f
, "state", slice_state_to_string(s
->state
));
262 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
263 Slice
*s
= ASSERT_PTR(SLICE(u
));
269 if (streq(key
, "state")) {
272 state
= slice_state_from_string(value
);
274 log_unit_debug(u
, "Failed to parse state: %s", value
);
276 s
->deserialized_state
= state
;
279 log_unit_debug(u
, "Unknown serialization key: %s", key
);
284 static UnitActiveState
slice_active_state(Unit
*u
) {
285 Slice
*s
= ASSERT_PTR(SLICE(u
));
287 return state_translation_table
[s
->state
];
290 static const char *slice_sub_state_to_string(Unit
*u
) {
291 Slice
*s
= ASSERT_PTR(SLICE(u
));
293 return slice_state_to_string(s
->state
);
296 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
303 u
= manager_get_unit(m
, name
);
305 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
307 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
311 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
313 unit_add_to_load_queue(u
);
314 unit_add_to_dbus_queue(u
);
322 static void slice_enumerate_perpetual(Manager
*m
) {
328 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
329 if (r
>= 0 && manager_owns_host_root_cgroup(m
)) {
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. */
336 s
->cgroup_context
.tasks_accounting
= true;
337 s
->cgroup_context
.memory_accounting
= true;
340 if (MANAGER_IS_SYSTEM(m
))
341 (void) slice_make_perpetual(m
, SPECIAL_SYSTEM_SLICE
, NULL
);
344 static bool slice_can_freeze(const Unit
*u
) {
348 UNIT_FOREACH_DEPENDENCY(member
, u
, UNIT_ATOM_SLICE_OF
)
349 if (!unit_can_freeze(member
))
355 static int slice_freezer_action(Unit
*s
, FreezerAction action
) {
356 FreezerAction child_action
;
361 assert(action
< _FREEZER_ACTION_MAX
);
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
368 log_unit_warning(s
, "Requested freezer operation is not supported by all children of the slice.");
372 if (action
== FREEZER_FREEZE
)
373 child_action
= FREEZER_PARENT_FREEZE
;
374 else if (action
== FREEZER_THAW
)
375 child_action
= FREEZER_PARENT_THAW
;
377 child_action
= action
;
380 UNIT_FOREACH_DEPENDENCY(member
, s
, UNIT_ATOM_SLICE_OF
)
381 if (UNIT_VTABLE(member
)->freezer_action
) {
382 r
= UNIT_VTABLE(member
)->freezer_action(member
, child_action
);
387 return unit_cgroup_freezer_action(s
, action
);
390 unsigned slice_get_currently_active(Slice
*slice
, Unit
*ignore
, bool with_pending
) {
391 Unit
*u
= ASSERT_PTR(UNIT(slice
));
393 /* If 'ignore' is non-NULL and a unit contained in this slice (or any below) we'll ignore it when
398 UNIT_FOREACH_DEPENDENCY(member
, u
, UNIT_ATOM_SLICE_OF
) {
399 if (member
== ignore
)
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
)))
406 if (member
->type
== UNIT_SLICE
)
407 n
+= slice_get_currently_active(SLICE(member
), ignore
, with_pending
);
413 bool slice_concurrency_soft_max_reached(Slice
*slice
, Unit
*ignore
) {
416 if (slice
->concurrency_soft_max
!= UINT_MAX
&&
417 slice_get_currently_active(slice
, ignore
, /* with_pending= */ false) >= slice
->concurrency_soft_max
)
420 Unit
*parent
= UNIT_GET_SLICE(UNIT(slice
));
422 return slice_concurrency_soft_max_reached(SLICE(parent
), ignore
);
427 bool slice_concurrency_hard_max_reached(Slice
*slice
, Unit
*ignore
) {
430 if (slice
->concurrency_hard_max
!= UINT_MAX
&&
431 slice_get_currently_active(slice
, ignore
, /* with_pending= */ true) >= slice
->concurrency_hard_max
)
434 Unit
*parent
= UNIT_GET_SLICE(UNIT(slice
));
436 return slice_concurrency_hard_max_reached(SLICE(parent
), ignore
);
441 const UnitVTable slice_vtable
= {
442 .object_size
= sizeof(Slice
),
443 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
444 .cgroup_runtime_offset
= offsetof(Slice
, cgroup_runtime
),
450 .private_section
= "Slice",
452 .can_transient
= true,
453 .can_set_managed_oom
= true,
458 .coldplug
= slice_coldplug
,
462 .start
= slice_start
,
465 .freezer_action
= slice_freezer_action
,
466 .can_freeze
= slice_can_freeze
,
468 .serialize
= slice_serialize
,
469 .deserialize_item
= slice_deserialize_item
,
471 .active_state
= slice_active_state
,
472 .sub_state_to_string
= slice_sub_state_to_string
,
474 .bus_set_property
= bus_slice_set_property
,
475 .bus_commit_properties
= bus_slice_commit_properties
,
477 .enumerate_perpetual
= slice_enumerate_perpetual
,
479 .status_message_formats
= {
480 .finished_start_job
= {
481 [JOB_DONE
] = "Created slice %s.",
483 .finished_stop_job
= {
484 [JOB_DONE
] = "Removed slice %s.",