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
) {
34 if (t
->state
!= state
)
35 bus_unit_send_pending_change_signal(UNIT(t
), false);
40 if (state
!= old_state
)
41 log_debug("%s changed %s -> %s",
43 slice_state_to_string(old_state
),
44 slice_state_to_string(state
));
46 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
49 static int slice_add_parent_slice(Slice
*s
) {
51 _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 (parent
? !unit_has_name(UNIT_GET_SLICE(UNIT(s
)), parent
) : !!UNIT_GET_SLICE(UNIT(s
)))
100 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Located outside of parent slice. Refusing.");
105 static int slice_load_root_slice(Unit
*u
) {
108 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
113 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
114 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
116 u
->default_dependencies
= false;
119 u
->description
= strdup("Root Slice");
120 if (!u
->documentation
)
121 u
->documentation
= strv_new("man:systemd.special(7)");
126 static int slice_load_system_slice(Unit
*u
) {
129 if (!MANAGER_IS_SYSTEM(u
->manager
))
131 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
136 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
137 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
139 u
->default_dependencies
= false;
142 u
->description
= strdup("System Slice");
143 if (!u
->documentation
)
144 u
->documentation
= strv_new("man:systemd.special(7)");
149 static int slice_load(Unit
*u
) {
154 assert(u
->load_state
== UNIT_STUB
);
156 r
= slice_load_root_slice(u
);
159 r
= slice_load_system_slice(u
);
163 r
= unit_load_fragment_and_dropin(u
, false);
167 if (u
->load_state
!= UNIT_LOADED
)
170 /* This is a new unit? Then let's add in some extras */
171 r
= unit_patch_contexts(u
);
175 r
= slice_add_parent_slice(s
);
179 r
= slice_add_default_dependencies(s
);
183 if (!u
->description
) {
184 _cleanup_free_
char *tmp
= NULL
;
186 r
= unit_name_to_path(u
->id
, &tmp
);
187 if (r
>= 0) /* Failure is ignored… */
188 u
->description
= strjoin("Slice ", tmp
);
191 return slice_verify(s
);
194 static int slice_coldplug(Unit
*u
) {
198 assert(t
->state
== SLICE_DEAD
);
200 if (t
->deserialized_state
!= t
->state
)
201 slice_set_state(t
, t
->deserialized_state
);
206 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
213 "%sSlice State: %s\n",
214 prefix
, slice_state_to_string(t
->state
));
216 cgroup_context_dump(UNIT(t
), f
, prefix
);
219 static int slice_start(Unit
*u
) {
224 assert(t
->state
== SLICE_DEAD
);
226 r
= unit_acquire_invocation_id(u
);
230 (void) unit_realize_cgroup(u
);
231 (void) unit_reset_accounting(u
);
233 slice_set_state(t
, SLICE_ACTIVE
);
237 static int slice_stop(Unit
*u
) {
241 assert(t
->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(t
, SLICE_DEAD
);
250 static int slice_kill(Unit
*u
, KillWho who
, int signo
, int code
, int value
, sd_bus_error
*error
) {
251 return unit_kill_common(u
, who
, signo
, code
, value
, -1, -1, error
);
254 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
261 (void) serialize_item(f
, "state", slice_state_to_string(s
->state
));
266 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
274 if (streq(key
, "state")) {
277 state
= slice_state_from_string(value
);
279 log_debug("Failed to parse state value %s", value
);
281 s
->deserialized_state
= state
;
284 log_debug("Unknown serialization key '%s'", key
);
289 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
292 return state_translation_table
[SLICE(u
)->state
];
295 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
298 return slice_state_to_string(SLICE(u
)->state
);
301 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
308 u
= manager_get_unit(m
, name
);
310 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
312 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
316 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
318 unit_add_to_load_queue(u
);
319 unit_add_to_dbus_queue(u
);
327 static void slice_enumerate_perpetual(Manager
*m
) {
333 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
334 if (r
>= 0 && manager_owns_host_root_cgroup(m
)) {
337 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
338 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
339 * hence turn accounting on here, so that our APIs to query this data are available. */
341 s
->cgroup_context
.cpu_accounting
= true;
342 s
->cgroup_context
.tasks_accounting
= true;
343 s
->cgroup_context
.memory_accounting
= true;
346 if (MANAGER_IS_SYSTEM(m
))
347 (void) slice_make_perpetual(m
, SPECIAL_SYSTEM_SLICE
, NULL
);
350 static bool slice_freezer_action_supported_by_children(Unit
*s
) {
355 UNIT_FOREACH_DEPENDENCY(member
, s
, UNIT_ATOM_SLICE_OF
) {
357 if (member
->type
== UNIT_SLICE
&&
358 !slice_freezer_action_supported_by_children(member
))
361 if (!UNIT_VTABLE(member
)->freeze
)
368 static int slice_freezer_action(Unit
*s
, FreezerAction action
) {
373 assert(IN_SET(action
, FREEZER_FREEZE
, FREEZER_THAW
));
375 if (action
== FREEZER_FREEZE
&& !slice_freezer_action_supported_by_children(s
)) {
376 log_unit_warning(s
, "Requested freezer operation is not supported by all children of the slice");
380 UNIT_FOREACH_DEPENDENCY(member
, s
, UNIT_ATOM_SLICE_OF
) {
381 if (!member
->cgroup_realized
)
384 if (action
== FREEZER_FREEZE
)
385 r
= UNIT_VTABLE(member
)->freeze(member
);
386 else if (UNIT_VTABLE(member
)->thaw
)
387 r
= UNIT_VTABLE(member
)->thaw(member
);
389 /* Thawing is requested but no corresponding method is available, ignore. */
395 return unit_cgroup_freezer_action(s
, action
);
398 static int slice_freeze(Unit
*s
) {
401 return slice_freezer_action(s
, FREEZER_FREEZE
);
404 static int slice_thaw(Unit
*s
) {
407 return slice_freezer_action(s
, FREEZER_THAW
);
410 static bool slice_can_freeze(Unit
*s
) {
413 return slice_freezer_action_supported_by_children(s
);
416 const UnitVTable slice_vtable
= {
417 .object_size
= sizeof(Slice
),
418 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
424 .private_section
= "Slice",
426 .can_transient
= true,
427 .can_set_managed_oom
= true,
432 .coldplug
= slice_coldplug
,
436 .start
= slice_start
,
441 .freeze
= slice_freeze
,
443 .can_freeze
= slice_can_freeze
,
445 .serialize
= slice_serialize
,
446 .deserialize_item
= slice_deserialize_item
,
448 .active_state
= slice_active_state
,
449 .sub_state_to_string
= slice_sub_state_to_string
,
451 .bus_set_property
= bus_slice_set_property
,
452 .bus_commit_properties
= bus_slice_commit_properties
,
454 .enumerate_perpetual
= slice_enumerate_perpetual
,
456 .status_message_formats
= {
457 .finished_start_job
= {
458 [JOB_DONE
] = "Created slice %s.",
460 .finished_stop_job
= {
461 [JOB_DONE
] = "Removed slice %s.",