1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "dbus-slice.h"
12 #include "string-util.h"
14 #include "unit-name.h"
17 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
18 [SLICE_DEAD
] = UNIT_INACTIVE
,
19 [SLICE_ACTIVE
] = UNIT_ACTIVE
22 static void slice_init(Unit
*u
) {
24 assert(u
->load_state
== UNIT_STUB
);
26 u
->ignore_on_isolate
= true;
29 static void slice_set_state(Slice
*t
, SliceState state
) {
33 if (t
->state
!= state
)
34 bus_unit_send_pending_change_signal(UNIT(t
), false);
39 if (state
!= old_state
)
40 log_debug("%s changed %s -> %s",
42 slice_state_to_string(old_state
),
43 slice_state_to_string(state
));
45 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
48 static int slice_add_parent_slice(Slice
*s
) {
49 Unit
*u
= UNIT(s
), *parent
;
50 _cleanup_free_
char *a
= NULL
;
55 if (UNIT_ISSET(u
->slice
))
58 r
= slice_build_parent_slice(u
->id
, &a
);
59 if (r
<= 0) /* 0 means root slice */
62 r
= manager_load_unit(u
->manager
, a
, NULL
, NULL
, &parent
);
66 unit_ref_set(&u
->slice
, u
, parent
);
70 static int slice_add_default_dependencies(Slice
*s
) {
75 if (!UNIT(s
)->default_dependencies
)
78 /* Make sure slices are unloaded on shutdown */
79 r
= unit_add_two_dependencies_by_name(
81 UNIT_BEFORE
, UNIT_CONFLICTS
,
82 SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
89 static int slice_verify(Slice
*s
) {
90 _cleanup_free_
char *parent
= NULL
;
95 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
98 if (!slice_name_is_valid(UNIT(s
)->id
)) {
99 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
103 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
105 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
107 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
108 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
115 static int slice_load_root_slice(Unit
*u
) {
118 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
123 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
124 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
126 u
->default_dependencies
= false;
129 u
->description
= strdup("Root Slice");
130 if (!u
->documentation
)
131 u
->documentation
= strv_new("man:systemd.special(7)");
136 static int slice_load_system_slice(Unit
*u
) {
139 if (!MANAGER_IS_SYSTEM(u
->manager
))
141 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
146 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
147 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
149 u
->default_dependencies
= false;
152 u
->description
= strdup("System Slice");
153 if (!u
->documentation
)
154 u
->documentation
= strv_new("man:systemd.special(7)");
159 static int slice_load(Unit
*u
) {
164 assert(u
->load_state
== UNIT_STUB
);
166 r
= slice_load_root_slice(u
);
169 r
= slice_load_system_slice(u
);
173 r
= unit_load_fragment_and_dropin_optional(u
);
177 /* This is a new unit? Then let's add in some extras */
178 if (u
->load_state
== UNIT_LOADED
) {
180 r
= unit_patch_contexts(u
);
184 r
= slice_add_parent_slice(s
);
188 r
= slice_add_default_dependencies(s
);
193 return slice_verify(s
);
196 static int slice_coldplug(Unit
*u
) {
200 assert(t
->state
== SLICE_DEAD
);
202 if (t
->deserialized_state
!= t
->state
)
203 slice_set_state(t
, t
->deserialized_state
);
208 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
215 "%sSlice State: %s\n",
216 prefix
, slice_state_to_string(t
->state
));
218 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
221 static int slice_start(Unit
*u
) {
226 assert(t
->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(t
, SLICE_ACTIVE
);
239 static int slice_stop(Unit
*u
) {
243 assert(t
->state
== SLICE_ACTIVE
);
245 /* We do not need to destroy the cgroup explicitly,
246 * unit_notify() will do that for us anyway. */
248 slice_set_state(t
, SLICE_DEAD
);
252 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
253 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
256 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
263 (void) serialize_item(f
, "state", slice_state_to_string(s
->state
));
268 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
276 if (streq(key
, "state")) {
279 state
= slice_state_from_string(value
);
281 log_debug("Failed to parse state value %s", value
);
283 s
->deserialized_state
= state
;
286 log_debug("Unknown serialization key '%s'", key
);
291 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
294 return state_translation_table
[SLICE(u
)->state
];
297 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
300 return slice_state_to_string(SLICE(u
)->state
);
303 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
310 u
= manager_get_unit(m
, name
);
312 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
314 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
318 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
320 unit_add_to_load_queue(u
);
321 unit_add_to_dbus_queue(u
);
329 static void slice_enumerate_perpetual(Manager
*m
) {
335 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
336 if (r
>= 0 && manager_owns_host_root_cgroup(m
)) {
339 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
340 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
341 * hence turn accounting on here, so that our APIs to query this data are available. */
343 s
->cgroup_context
.cpu_accounting
= true;
344 s
->cgroup_context
.tasks_accounting
= true;
345 s
->cgroup_context
.memory_accounting
= true;
348 if (MANAGER_IS_SYSTEM(m
))
349 (void) slice_make_perpetual(m
, SPECIAL_SYSTEM_SLICE
, NULL
);
352 const UnitVTable slice_vtable
= {
353 .object_size
= sizeof(Slice
),
354 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
360 .private_section
= "Slice",
362 .can_transient
= true,
367 .coldplug
= slice_coldplug
,
371 .start
= slice_start
,
376 .serialize
= slice_serialize
,
377 .deserialize_item
= slice_deserialize_item
,
379 .active_state
= slice_active_state
,
380 .sub_state_to_string
= slice_sub_state_to_string
,
382 .bus_vtable
= bus_slice_vtable
,
383 .bus_set_property
= bus_slice_set_property
,
384 .bus_commit_properties
= bus_slice_commit_properties
,
386 .enumerate_perpetual
= slice_enumerate_perpetual
,
388 .status_message_formats
= {
389 .finished_start_job
= {
390 [JOB_DONE
] = "Created slice %s.",
392 .finished_stop_job
= {
393 [JOB_DONE
] = "Removed slice %s.",