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
) {
68 if (UNIT_ISSET(UNIT(s
)->slice
))
71 if (unit_has_name(UNIT(s
), SPECIAL_ROOT_SLICE
))
74 a
= strdupa(UNIT(s
)->id
);
75 dash
= strrchr(a
, '-');
77 strcpy(dash
, ".slice");
79 a
= (char*) SPECIAL_ROOT_SLICE
;
81 r
= manager_load_unit(UNIT(s
)->manager
, a
, NULL
, NULL
, &parent
);
85 unit_ref_set(&UNIT(s
)->slice
, parent
);
89 static int slice_add_default_dependencies(Slice
*s
) {
94 if (!UNIT(s
)->default_dependencies
)
97 /* Make sure slices are unloaded on shutdown */
98 r
= unit_add_two_dependencies_by_name(
100 UNIT_BEFORE
, UNIT_CONFLICTS
,
101 SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
108 static int slice_verify(Slice
*s
) {
109 _cleanup_free_
char *parent
= NULL
;
114 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
117 if (!slice_name_is_valid(UNIT(s
)->id
)) {
118 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
122 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
124 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
126 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
127 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
134 static int slice_load_root_slice(Unit
*u
) {
137 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
142 /* The root 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;
146 u
->ignore_on_isolate
= true;
149 u
->description
= strdup("Root Slice");
150 if (!u
->documentation
)
151 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
156 static int slice_load(Unit
*u
) {
161 assert(u
->load_state
== UNIT_STUB
);
163 r
= slice_load_root_slice(u
);
166 r
= unit_load_fragment_and_dropin_optional(u
);
170 /* This is a new unit? Then let's add in some extras */
171 if (u
->load_state
== UNIT_LOADED
) {
173 r
= unit_patch_contexts(u
);
177 r
= slice_add_parent_slice(s
);
181 r
= slice_add_default_dependencies(s
);
186 return slice_verify(s
);
189 static int slice_coldplug(Unit
*u
) {
193 assert(t
->state
== SLICE_DEAD
);
195 if (t
->deserialized_state
!= t
->state
)
196 slice_set_state(t
, t
->deserialized_state
);
201 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
208 "%sSlice State: %s\n",
209 prefix
, slice_state_to_string(t
->state
));
211 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
214 static int slice_start(Unit
*u
) {
219 assert(t
->state
== SLICE_DEAD
);
221 r
= unit_acquire_invocation_id(u
);
225 (void) unit_realize_cgroup(u
);
226 (void) unit_reset_cpu_accounting(u
);
227 (void) unit_reset_ip_accounting(u
);
229 slice_set_state(t
, SLICE_ACTIVE
);
233 static int slice_stop(Unit
*u
) {
237 assert(t
->state
== SLICE_ACTIVE
);
239 /* We do not need to destroy the cgroup explicitly,
240 * unit_notify() will do that for us anyway. */
242 slice_set_state(t
, SLICE_DEAD
);
246 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
247 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
250 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
257 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
261 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
269 if (streq(key
, "state")) {
272 state
= slice_state_from_string(value
);
274 log_debug("Failed to parse state value %s", value
);
276 s
->deserialized_state
= state
;
279 log_debug("Unknown serialization key '%s'", key
);
284 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
287 return state_translation_table
[SLICE(u
)->state
];
290 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
293 return slice_state_to_string(SLICE(u
)->state
);
296 static void slice_enumerate(Manager
*m
) {
302 u
= manager_get_unit(m
, SPECIAL_ROOT_SLICE
);
304 r
= unit_new_for_name(m
, sizeof(Slice
), SPECIAL_ROOT_SLICE
, &u
);
306 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_SLICE
" unit: %m");
312 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
314 unit_add_to_load_queue(u
);
315 unit_add_to_dbus_queue(u
);
318 const UnitVTable slice_vtable
= {
319 .object_size
= sizeof(Slice
),
320 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
326 .private_section
= "Slice",
328 .can_transient
= true,
333 .coldplug
= slice_coldplug
,
337 .start
= slice_start
,
342 .serialize
= slice_serialize
,
343 .deserialize_item
= slice_deserialize_item
,
345 .active_state
= slice_active_state
,
346 .sub_state_to_string
= slice_sub_state_to_string
,
348 .bus_vtable
= bus_slice_vtable
,
349 .bus_set_property
= bus_slice_set_property
,
350 .bus_commit_properties
= bus_slice_commit_properties
,
352 .enumerate
= slice_enumerate
,
354 .status_message_formats
= {
355 .finished_start_job
= {
356 [JOB_DONE
] = "Created slice %s.",
358 .finished_stop_job
= {
359 [JOB_DONE
] = "Removed slice %s.",