1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "dbus-slice.h"
28 #include "string-util.h"
30 #include "unit-name.h"
33 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
34 [SLICE_DEAD
] = UNIT_INACTIVE
,
35 [SLICE_ACTIVE
] = UNIT_ACTIVE
38 static void slice_init(Unit
*u
) {
40 assert(u
->load_state
== UNIT_STUB
);
42 u
->ignore_on_isolate
= true;
45 static void slice_set_state(Slice
*t
, SliceState state
) {
52 if (state
!= old_state
)
53 log_debug("%s changed %s -> %s",
55 slice_state_to_string(old_state
),
56 slice_state_to_string(state
));
58 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
61 static int slice_add_parent_slice(Slice
*s
) {
62 Unit
*u
= UNIT(s
), *parent
;
63 _cleanup_free_
char *a
= NULL
;
68 if (UNIT_ISSET(u
->slice
))
71 r
= slice_build_parent_slice(u
->id
, &a
);
72 if (r
<= 0) /* 0 means root slice */
75 r
= manager_load_unit(u
->manager
, a
, NULL
, NULL
, &parent
);
79 unit_ref_set(&u
->slice
, u
, parent
);
83 static int slice_add_default_dependencies(Slice
*s
) {
88 if (!UNIT(s
)->default_dependencies
)
91 /* Make sure slices are unloaded on shutdown */
92 r
= unit_add_two_dependencies_by_name(
94 UNIT_BEFORE
, UNIT_CONFLICTS
,
95 SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
102 static int slice_verify(Slice
*s
) {
103 _cleanup_free_
char *parent
= NULL
;
108 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
111 if (!slice_name_is_valid(UNIT(s
)->id
)) {
112 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
116 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
118 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
120 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
121 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
128 static int slice_load_root_slice(Unit
*u
) {
131 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
136 /* The root 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("Root Slice");
143 if (!u
->documentation
)
144 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
149 static int slice_load_system_slice(Unit
*u
) {
152 if (!MANAGER_IS_SYSTEM(u
->manager
))
154 if (!unit_has_name(u
, SPECIAL_SYSTEM_SLICE
))
159 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
160 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
162 u
->default_dependencies
= false;
165 u
->description
= strdup("System Slice");
166 if (!u
->documentation
)
167 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
172 static int slice_load(Unit
*u
) {
177 assert(u
->load_state
== UNIT_STUB
);
179 r
= slice_load_root_slice(u
);
182 r
= slice_load_system_slice(u
);
186 r
= unit_load_fragment_and_dropin_optional(u
);
190 /* This is a new unit? Then let's add in some extras */
191 if (u
->load_state
== UNIT_LOADED
) {
193 r
= unit_patch_contexts(u
);
197 r
= slice_add_parent_slice(s
);
201 r
= slice_add_default_dependencies(s
);
206 return slice_verify(s
);
209 static int slice_coldplug(Unit
*u
) {
213 assert(t
->state
== SLICE_DEAD
);
215 if (t
->deserialized_state
!= t
->state
)
216 slice_set_state(t
, t
->deserialized_state
);
221 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
228 "%sSlice State: %s\n",
229 prefix
, slice_state_to_string(t
->state
));
231 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
234 static int slice_start(Unit
*u
) {
239 assert(t
->state
== SLICE_DEAD
);
241 r
= unit_acquire_invocation_id(u
);
245 (void) unit_realize_cgroup(u
);
246 (void) unit_reset_cpu_accounting(u
);
247 (void) unit_reset_ip_accounting(u
);
249 slice_set_state(t
, SLICE_ACTIVE
);
253 static int slice_stop(Unit
*u
) {
257 assert(t
->state
== SLICE_ACTIVE
);
259 /* We do not need to destroy the cgroup explicitly,
260 * unit_notify() will do that for us anyway. */
262 slice_set_state(t
, SLICE_DEAD
);
266 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
267 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
270 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
277 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
281 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
289 if (streq(key
, "state")) {
292 state
= slice_state_from_string(value
);
294 log_debug("Failed to parse state value %s", value
);
296 s
->deserialized_state
= state
;
299 log_debug("Unknown serialization key '%s'", key
);
304 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
307 return state_translation_table
[SLICE(u
)->state
];
310 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
313 return slice_state_to_string(SLICE(u
)->state
);
316 static int slice_make_perpetual(Manager
*m
, const char *name
, Unit
**ret
) {
323 u
= manager_get_unit(m
, name
);
325 r
= unit_new_for_name(m
, sizeof(Slice
), name
, &u
);
327 return log_error_errno(r
, "Failed to allocate the special %s unit: %m", name
);
331 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
333 unit_add_to_load_queue(u
);
334 unit_add_to_dbus_queue(u
);
342 static void slice_enumerate(Manager
*m
) {
348 r
= slice_make_perpetual(m
, SPECIAL_ROOT_SLICE
, &u
);
349 if (r
>= 0 && manager_owns_root_cgroup(m
)) {
352 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
353 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
354 * hence turn accounting on here, so that our APIs to query this data are available. */
356 s
->cgroup_context
.cpu_accounting
= true;
357 s
->cgroup_context
.tasks_accounting
= true;
358 s
->cgroup_context
.memory_accounting
= true;
361 if (MANAGER_IS_SYSTEM(m
))
362 (void) slice_make_perpetual(m
, SPECIAL_SYSTEM_SLICE
, NULL
);
365 const UnitVTable slice_vtable
= {
366 .object_size
= sizeof(Slice
),
367 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
373 .private_section
= "Slice",
375 .can_transient
= true,
380 .coldplug
= slice_coldplug
,
384 .start
= slice_start
,
389 .serialize
= slice_serialize
,
390 .deserialize_item
= slice_deserialize_item
,
392 .active_state
= slice_active_state
,
393 .sub_state_to_string
= slice_sub_state_to_string
,
395 .bus_vtable
= bus_slice_vtable
,
396 .bus_set_property
= bus_slice_set_property
,
397 .bus_commit_properties
= bus_slice_commit_properties
,
399 .enumerate
= slice_enumerate
,
401 .status_message_formats
= {
402 .finished_start_job
= {
403 [JOB_DONE
] = "Created slice %s.",
405 .finished_stop_job
= {
406 [JOB_DONE
] = "Removed slice %s.",