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
, 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;
140 u
->ignore_on_isolate
= true;
143 u
->description
= strdup("Root Slice");
144 if (!u
->documentation
)
145 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
150 static int slice_load(Unit
*u
) {
155 assert(u
->load_state
== UNIT_STUB
);
157 r
= slice_load_root_slice(u
);
160 r
= unit_load_fragment_and_dropin_optional(u
);
164 /* This is a new unit? Then let's add in some extras */
165 if (u
->load_state
== UNIT_LOADED
) {
167 r
= unit_patch_contexts(u
);
171 r
= slice_add_parent_slice(s
);
175 r
= slice_add_default_dependencies(s
);
180 return slice_verify(s
);
183 static int slice_coldplug(Unit
*u
) {
187 assert(t
->state
== SLICE_DEAD
);
189 if (t
->deserialized_state
!= t
->state
)
190 slice_set_state(t
, t
->deserialized_state
);
195 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
202 "%sSlice State: %s\n",
203 prefix
, slice_state_to_string(t
->state
));
205 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
208 static int slice_start(Unit
*u
) {
213 assert(t
->state
== SLICE_DEAD
);
215 r
= unit_acquire_invocation_id(u
);
219 (void) unit_realize_cgroup(u
);
220 (void) unit_reset_cpu_accounting(u
);
221 (void) unit_reset_ip_accounting(u
);
223 slice_set_state(t
, SLICE_ACTIVE
);
227 static int slice_stop(Unit
*u
) {
231 assert(t
->state
== SLICE_ACTIVE
);
233 /* We do not need to destroy the cgroup explicitly,
234 * unit_notify() will do that for us anyway. */
236 slice_set_state(t
, SLICE_DEAD
);
240 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
241 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
244 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
251 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
255 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
263 if (streq(key
, "state")) {
266 state
= slice_state_from_string(value
);
268 log_debug("Failed to parse state value %s", value
);
270 s
->deserialized_state
= state
;
273 log_debug("Unknown serialization key '%s'", key
);
278 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
281 return state_translation_table
[SLICE(u
)->state
];
284 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
287 return slice_state_to_string(SLICE(u
)->state
);
290 static void slice_enumerate(Manager
*m
) {
296 u
= manager_get_unit(m
, SPECIAL_ROOT_SLICE
);
298 r
= unit_new_for_name(m
, sizeof(Slice
), SPECIAL_ROOT_SLICE
, &u
);
300 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_SLICE
" unit: %m");
306 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
308 unit_add_to_load_queue(u
);
309 unit_add_to_dbus_queue(u
);
312 const UnitVTable slice_vtable
= {
313 .object_size
= sizeof(Slice
),
314 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
320 .private_section
= "Slice",
322 .can_transient
= true,
327 .coldplug
= slice_coldplug
,
331 .start
= slice_start
,
336 .serialize
= slice_serialize
,
337 .deserialize_item
= slice_deserialize_item
,
339 .active_state
= slice_active_state
,
340 .sub_state_to_string
= slice_sub_state_to_string
,
342 .bus_vtable
= bus_slice_vtable
,
343 .bus_set_property
= bus_slice_set_property
,
344 .bus_commit_properties
= bus_slice_commit_properties
,
346 .enumerate
= slice_enumerate
,
348 .status_message_formats
= {
349 .finished_start_job
= {
350 [JOB_DONE
] = "Created slice %s.",
352 .finished_stop_job
= {
353 [JOB_DONE
] = "Removed slice %s.",