1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "dbus-slice.h"
10 #include "string-util.h"
12 #include "unit-name.h"
15 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
16 [SLICE_DEAD
] = UNIT_INACTIVE
,
17 [SLICE_ACTIVE
] = UNIT_ACTIVE
20 static void slice_init(Unit
*u
) {
22 assert(u
->load_state
== UNIT_STUB
);
24 u
->ignore_on_isolate
= true;
27 static void slice_set_state(Slice
*t
, SliceState state
) {
34 if (state
!= old_state
)
35 log_debug("%s changed %s -> %s",
37 slice_state_to_string(old_state
),
38 slice_state_to_string(state
));
40 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
43 static int slice_add_parent_slice(Slice
*s
) {
44 Unit
*u
= UNIT(s
), *parent
;
45 _cleanup_free_
char *a
= NULL
;
50 if (UNIT_ISSET(u
->slice
))
53 r
= slice_build_parent_slice(u
->id
, &a
);
54 if (r
<= 0) /* 0 means root slice */
57 r
= manager_load_unit(u
->manager
, a
, NULL
, NULL
, &parent
);
61 unit_ref_set(&u
->slice
, u
, parent
);
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
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
84 static int slice_verify(Slice
*s
) {
85 _cleanup_free_
char *parent
= NULL
;
90 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
93 if (!slice_name_is_valid(UNIT(s
)->id
)) {
94 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
98 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
100 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
102 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
103 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
110 static int slice_load_root_slice(Unit
*u
) {
113 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
118 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
119 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
121 u
->default_dependencies
= false;
124 u
->description
= strdup("Root Slice");
125 if (!u
->documentation
)
126 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
131 static int slice_load_system_slice(Unit
*u
) {
134 if (!MANAGER_IS_SYSTEM(u
->manager
))
136 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
141 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
142 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
144 u
->default_dependencies
= false;
147 u
->description
= strdup("System Slice");
148 if (!u
->documentation
)
149 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
154 static int slice_load(Unit
*u
) {
159 assert(u
->load_state
== UNIT_STUB
);
161 r
= slice_load_root_slice(u
);
164 r
= slice_load_system_slice(u
);
168 r
= unit_load_fragment_and_dropin_optional(u
);
172 /* This is a new unit? Then let's add in some extras */
173 if (u
->load_state
== UNIT_LOADED
) {
175 r
= unit_patch_contexts(u
);
179 r
= slice_add_parent_slice(s
);
183 r
= slice_add_default_dependencies(s
);
188 return slice_verify(s
);
191 static int slice_coldplug(Unit
*u
) {
195 assert(t
->state
== SLICE_DEAD
);
197 if (t
->deserialized_state
!= t
->state
)
198 slice_set_state(t
, t
->deserialized_state
);
203 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
210 "%sSlice State: %s\n",
211 prefix
, slice_state_to_string(t
->state
));
213 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
216 static int slice_start(Unit
*u
) {
221 assert(t
->state
== SLICE_DEAD
);
223 r
= unit_acquire_invocation_id(u
);
227 (void) unit_realize_cgroup(u
);
228 (void) unit_reset_cpu_accounting(u
);
229 (void) unit_reset_ip_accounting(u
);
231 slice_set_state(t
, SLICE_ACTIVE
);
235 static int slice_stop(Unit
*u
) {
239 assert(t
->state
== SLICE_ACTIVE
);
241 /* We do not need to destroy the cgroup explicitly,
242 * unit_notify() will do that for us anyway. */
244 slice_set_state(t
, SLICE_DEAD
);
248 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
249 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
252 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
259 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
263 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
271 if (streq(key
, "state")) {
274 state
= slice_state_from_string(value
);
276 log_debug("Failed to parse state value %s", value
);
278 s
->deserialized_state
= state
;
281 log_debug("Unknown serialization key '%s'", key
);
286 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
289 return state_translation_table
[SLICE(u
)->state
];
292 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
295 return slice_state_to_string(SLICE(u
)->state
);
298 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
305 u
= manager_get_unit(m
, name
);
307 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
309 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
313 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
315 unit_add_to_load_queue(u
);
316 unit_add_to_dbus_queue(u
);
324 static void slice_enumerate_perpetual(Manager
*m
) {
330 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
331 if (r
>= 0 && manager_owns_root_cgroup(m
)) {
334 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
335 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
336 * hence turn accounting on here, so that our APIs to query this data are available. */
338 s
->cgroup_context
.cpu_accounting
= true;
339 s
->cgroup_context
.tasks_accounting
= true;
340 s
->cgroup_context
.memory_accounting
= true;
343 if (MANAGER_IS_SYSTEM(m
))
344 (void) slice_make_perpetual(m
, SPECIAL_SYSTEM_SLICE
, NULL
);
347 const UnitVTable slice_vtable
= {
348 .object_size
= sizeof(Slice
),
349 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
355 .private_section
= "Slice",
357 .can_transient
= true,
362 .coldplug
= slice_coldplug
,
366 .start
= slice_start
,
371 .serialize
= slice_serialize
,
372 .deserialize_item
= slice_deserialize_item
,
374 .active_state
= slice_active_state
,
375 .sub_state_to_string
= slice_sub_state_to_string
,
377 .bus_vtable
= bus_slice_vtable
,
378 .bus_set_property
= bus_slice_set_property
,
379 .bus_commit_properties
= bus_slice_commit_properties
,
381 .enumerate_perpetual
= slice_enumerate_perpetual
,
383 .status_message_formats
= {
384 .finished_start_job
= {
385 [JOB_DONE
] = "Created slice %s.",
387 .finished_stop_job
= {
388 [JOB_DONE
] = "Removed slice %s.",