1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "dbus-slice.h"
11 #include "string-util.h"
13 #include "unit-name.h"
16 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
17 [SLICE_DEAD
] = UNIT_INACTIVE
,
18 [SLICE_ACTIVE
] = UNIT_ACTIVE
21 static void slice_init(Unit
*u
) {
23 assert(u
->load_state
== UNIT_STUB
);
25 u
->ignore_on_isolate
= true;
28 static void slice_set_state(Slice
*t
, SliceState state
) {
35 if (state
!= old_state
)
36 log_debug("%s changed %s -> %s",
38 slice_state_to_string(old_state
),
39 slice_state_to_string(state
));
41 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
44 static int slice_add_parent_slice(Slice
*s
) {
45 Unit
*u
= UNIT(s
), *parent
;
46 _cleanup_free_
char *a
= NULL
;
51 if (UNIT_ISSET(u
->slice
))
54 r
= slice_build_parent_slice(u
->id
, &a
);
55 if (r
<= 0) /* 0 means root slice */
58 r
= manager_load_unit(u
->manager
, a
, NULL
, NULL
, &parent
);
62 unit_ref_set(&u
->slice
, u
, parent
);
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
;
91 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
94 if (!slice_name_is_valid(UNIT(s
)->id
)) {
95 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
99 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
101 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
103 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
104 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
111 static int slice_load_root_slice(Unit
*u
) {
114 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
119 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
120 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
122 u
->default_dependencies
= false;
125 u
->description
= strdup("Root Slice");
126 if (!u
->documentation
)
127 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
132 static int slice_load_system_slice(Unit
*u
) {
135 if (!MANAGER_IS_SYSTEM(u
->manager
))
137 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
142 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
143 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
145 u
->default_dependencies
= false;
148 u
->description
= strdup("System Slice");
149 if (!u
->documentation
)
150 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
155 static int slice_load(Unit
*u
) {
160 assert(u
->load_state
== UNIT_STUB
);
162 r
= slice_load_root_slice(u
);
165 r
= slice_load_system_slice(u
);
169 r
= unit_load_fragment_and_dropin_optional(u
);
173 /* This is a new unit? Then let's add in some extras */
174 if (u
->load_state
== UNIT_LOADED
) {
176 r
= unit_patch_contexts(u
);
180 r
= slice_add_parent_slice(s
);
184 r
= slice_add_default_dependencies(s
);
189 return slice_verify(s
);
192 static int slice_coldplug(Unit
*u
) {
196 assert(t
->state
== SLICE_DEAD
);
198 if (t
->deserialized_state
!= t
->state
)
199 slice_set_state(t
, t
->deserialized_state
);
204 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
211 "%sSlice State: %s\n",
212 prefix
, slice_state_to_string(t
->state
));
214 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
217 static int slice_start(Unit
*u
) {
222 assert(t
->state
== SLICE_DEAD
);
224 r
= unit_acquire_invocation_id(u
);
228 (void) unit_realize_cgroup(u
);
229 (void) unit_reset_cpu_accounting(u
);
230 (void) unit_reset_ip_accounting(u
);
232 slice_set_state(t
, SLICE_ACTIVE
);
236 static int slice_stop(Unit
*u
) {
240 assert(t
->state
== SLICE_ACTIVE
);
242 /* We do not need to destroy the cgroup explicitly,
243 * unit_notify() will do that for us anyway. */
245 slice_set_state(t
, SLICE_DEAD
);
249 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
250 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
253 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
260 (void) serialize_item(f
, "state", slice_state_to_string(s
->state
));
265 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
273 if (streq(key
, "state")) {
276 state
= slice_state_from_string(value
);
278 log_debug("Failed to parse state value %s", value
);
280 s
->deserialized_state
= state
;
283 log_debug("Unknown serialization key '%s'", key
);
288 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
291 return state_translation_table
[SLICE(u
)->state
];
294 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
297 return slice_state_to_string(SLICE(u
)->state
);
300 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
307 u
= manager_get_unit(m
, name
);
309 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
311 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
315 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
317 unit_add_to_load_queue(u
);
318 unit_add_to_dbus_queue(u
);
326 static void slice_enumerate_perpetual(Manager
*m
) {
332 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
333 if (r
>= 0 && manager_owns_root_cgroup(m
)) {
336 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
337 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
338 * hence turn accounting on here, so that our APIs to query this data are available. */
340 s
->cgroup_context
.cpu_accounting
= true;
341 s
->cgroup_context
.tasks_accounting
= true;
342 s
->cgroup_context
.memory_accounting
= true;
345 if (MANAGER_IS_SYSTEM(m
))
346 (void) slice_make_perpetual(m
, SPECIAL_SYSTEM_SLICE
, NULL
);
349 const UnitVTable slice_vtable
= {
350 .object_size
= sizeof(Slice
),
351 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
357 .private_section
= "Slice",
359 .can_transient
= true,
364 .coldplug
= slice_coldplug
,
368 .start
= slice_start
,
373 .serialize
= slice_serialize
,
374 .deserialize_item
= slice_deserialize_item
,
376 .active_state
= slice_active_state
,
377 .sub_state_to_string
= slice_sub_state_to_string
,
379 .bus_vtable
= bus_slice_vtable
,
380 .bus_set_property
= bus_slice_set_property
,
381 .bus_commit_properties
= bus_slice_commit_properties
,
383 .enumerate_perpetual
= slice_enumerate_perpetual
,
385 .status_message_formats
= {
386 .finished_start_job
= {
387 [JOB_DONE
] = "Created slice %s.",
389 .finished_stop_job
= {
390 [JOB_DONE
] = "Removed slice %s.",