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"
15 #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
) {
25 assert(u
->load_state
== UNIT_STUB
);
27 u
->ignore_on_isolate
= true;
30 static void slice_set_state(Slice
*t
, SliceState state
) {
35 if (t
->state
!= state
)
36 bus_unit_send_pending_change_signal(UNIT(t
), false);
41 if (state
!= old_state
)
42 log_debug("%s changed %s -> %s",
44 slice_state_to_string(old_state
),
45 slice_state_to_string(state
));
47 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
50 static int slice_add_parent_slice(Slice
*s
) {
51 Unit
*u
= UNIT(ASSERT_PTR(s
));
52 _cleanup_free_
char *a
= NULL
;
55 if (UNIT_GET_SLICE(u
))
58 r
= slice_build_parent_slice(u
->id
, &a
);
59 if (r
<= 0) /* 0 means root slice */
62 return unit_add_dependency_by_name(u
, UNIT_IN_SLICE
, a
, true, UNIT_DEPENDENCY_IMPLICIT
);
65 static int slice_add_default_dependencies(Slice
*s
) {
70 if (!UNIT(s
)->default_dependencies
)
73 /* Make sure slices are unloaded on shutdown */
74 r
= unit_add_two_dependencies_by_name(
76 UNIT_BEFORE
, UNIT_CONFLICTS
,
77 SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
84 static int slice_verify(Slice
*s
) {
85 _cleanup_free_
char *parent
= NULL
;
89 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
91 if (!slice_name_is_valid(UNIT(s
)->id
))
92 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
94 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
96 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
98 /* If recursive errors are to be ignored, the parent slice should not be verified */
99 if (UNIT(s
)->manager
&& FLAGS_SET(UNIT(s
)->manager
->test_run_flags
, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES
))
102 if (parent
? !unit_has_name(UNIT_GET_SLICE(UNIT(s
)), parent
) : !!UNIT_GET_SLICE(UNIT(s
)))
103 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Located outside of parent slice. Refusing.");
108 static int slice_load_root_slice(Unit
*u
) {
111 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
116 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
117 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
119 u
->default_dependencies
= false;
122 u
->description
= strdup("Root Slice");
123 if (!u
->documentation
)
124 u
->documentation
= strv_new("man:systemd.special(7)");
129 static int slice_load_system_slice(Unit
*u
) {
132 if (!MANAGER_IS_SYSTEM(u
->manager
))
134 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
139 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
140 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
142 u
->default_dependencies
= false;
145 u
->description
= strdup("System Slice");
146 if (!u
->documentation
)
147 u
->documentation
= strv_new("man:systemd.special(7)");
152 static int slice_load(Unit
*u
) {
153 Slice
*s
= ASSERT_PTR(SLICE(u
));
156 assert(u
->load_state
== UNIT_STUB
);
158 r
= slice_load_root_slice(u
);
161 r
= slice_load_system_slice(u
);
165 r
= unit_load_fragment_and_dropin(u
, false);
169 if (u
->load_state
!= UNIT_LOADED
)
172 /* This is a new unit? Then let's add in some extras */
173 r
= unit_patch_contexts(u
);
177 r
= slice_add_parent_slice(s
);
181 r
= slice_add_default_dependencies(s
);
185 if (!u
->description
) {
186 _cleanup_free_
char *tmp
= NULL
;
188 r
= unit_name_to_path(u
->id
, &tmp
);
189 if (r
>= 0) /* Failure is ignored… */
190 u
->description
= strjoin("Slice ", tmp
);
193 return slice_verify(s
);
196 static int slice_coldplug(Unit
*u
) {
197 Slice
*s
= ASSERT_PTR(SLICE(u
));
199 assert(s
->state
== SLICE_DEAD
);
201 if (s
->deserialized_state
!= s
->state
)
202 slice_set_state(s
, s
->deserialized_state
);
207 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
208 Slice
*s
= ASSERT_PTR(SLICE(u
));
215 "%sSlice State: %s\n",
216 prefix
, slice_state_to_string(s
->state
));
218 cgroup_context_dump(u
, f
, prefix
);
221 static int slice_start(Unit
*u
) {
222 Slice
*s
= ASSERT_PTR(SLICE(u
));
225 assert(s
->state
== SLICE_DEAD
);
227 r
= unit_acquire_invocation_id(u
);
231 (void) unit_realize_cgroup(u
);
232 (void) unit_reset_accounting(u
);
234 slice_set_state(s
, SLICE_ACTIVE
);
238 static int slice_stop(Unit
*u
) {
239 Slice
*s
= ASSERT_PTR(SLICE(u
));
241 assert(s
->state
== SLICE_ACTIVE
);
243 /* We do not need to destroy the cgroup explicitly,
244 * unit_notify() will do that for us anyway. */
246 slice_set_state(s
, SLICE_DEAD
);
250 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
251 Slice
*s
= ASSERT_PTR(SLICE(u
));
256 (void) serialize_item(f
, "state", slice_state_to_string(s
->state
));
261 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
262 Slice
*s
= ASSERT_PTR(SLICE(u
));
268 if (streq(key
, "state")) {
271 state
= slice_state_from_string(value
);
273 log_unit_debug(u
, "Failed to parse state: %s", value
);
275 s
->deserialized_state
= state
;
278 log_unit_debug(u
, "Unknown serialization key: %s", key
);
283 static UnitActiveState
slice_active_state(Unit
*u
) {
284 Slice
*s
= ASSERT_PTR(SLICE(u
));
286 return state_translation_table
[s
->state
];
289 static const char *slice_sub_state_to_string(Unit
*u
) {
290 Slice
*s
= ASSERT_PTR(SLICE(u
));
292 return slice_state_to_string(s
->state
);
295 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
302 u
= manager_get_unit(m
, name
);
304 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
306 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
310 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
312 unit_add_to_load_queue(u
);
313 unit_add_to_dbus_queue(u
);
321 static void slice_enumerate_perpetual(Manager
*m
) {
327 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
328 if (r
>= 0 && manager_owns_host_root_cgroup(m
)) {
331 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
332 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
333 * hence turn accounting on here, so that our APIs to query this data are available. */
335 s
->cgroup_context
.cpu_accounting
= true;
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(Unit
*s
) {
349 UNIT_FOREACH_DEPENDENCY(member
, s
, UNIT_ATOM_SLICE_OF
)
350 if (!unit_can_freeze(member
))
355 static int slice_freezer_action(Unit
*s
, FreezerAction action
) {
356 FreezerAction child_action
;
361 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_PARENT_FREEZE
,
362 FREEZER_THAW
, FREEZER_PARENT_THAW
));
364 if (action
== FREEZER_FREEZE
&& !slice_can_freeze(s
)) {
365 /* We're intentionally only checking for FREEZER_FREEZE here and ignoring the
366 * _BY_PARENT variant. If we're being frozen by parent, that means someone has
367 * already checked if we can be frozen further up the call stack. No point to
369 log_unit_warning(s
, "Requested freezer operation is not supported by all children of the slice");
373 if (action
== FREEZER_FREEZE
)
374 child_action
= FREEZER_PARENT_FREEZE
;
375 else if (action
== FREEZER_THAW
)
376 child_action
= FREEZER_PARENT_THAW
;
378 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
);
384 /* Only thawing will reach here, since freezing checks for a method in can_freeze */
390 return unit_cgroup_freezer_action(s
, action
);
393 const UnitVTable slice_vtable
= {
394 .object_size
= sizeof(Slice
),
395 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
396 .cgroup_runtime_offset
= offsetof(Slice
, cgroup_runtime
),
402 .private_section
= "Slice",
404 .can_transient
= true,
405 .can_set_managed_oom
= true,
410 .coldplug
= slice_coldplug
,
414 .start
= slice_start
,
417 .freezer_action
= slice_freezer_action
,
418 .can_freeze
= slice_can_freeze
,
420 .serialize
= slice_serialize
,
421 .deserialize_item
= slice_deserialize_item
,
423 .active_state
= slice_active_state
,
424 .sub_state_to_string
= slice_sub_state_to_string
,
426 .bus_set_property
= bus_slice_set_property
,
427 .bus_commit_properties
= bus_slice_commit_properties
,
429 .enumerate_perpetual
= slice_enumerate_perpetual
,
431 .status_message_formats
= {
432 .finished_start_job
= {
433 [JOB_DONE
] = "Created slice %s.",
435 .finished_stop_job
= {
436 [JOB_DONE
] = "Removed slice %s.",