2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "dbus-slice.h"
27 #include "string-util.h"
29 #include "unit-name.h"
32 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
33 [SLICE_DEAD
] = UNIT_INACTIVE
,
34 [SLICE_ACTIVE
] = UNIT_ACTIVE
37 static void slice_init(Unit
*u
) {
39 assert(u
->load_state
== UNIT_STUB
);
41 u
->ignore_on_isolate
= true;
44 static void slice_set_state(Slice
*t
, SliceState state
) {
51 if (state
!= old_state
)
52 log_debug("%s changed %s -> %s",
54 slice_state_to_string(old_state
),
55 slice_state_to_string(state
));
57 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
60 static int slice_add_parent_slice(Slice
*s
) {
67 if (UNIT_ISSET(UNIT(s
)->slice
))
70 if (unit_has_name(UNIT(s
), SPECIAL_ROOT_SLICE
))
73 a
= strdupa(UNIT(s
)->id
);
74 dash
= strrchr(a
, '-');
76 strcpy(dash
, ".slice");
78 a
= (char*) SPECIAL_ROOT_SLICE
;
80 r
= manager_load_unit(UNIT(s
)->manager
, a
, NULL
, NULL
, &parent
);
84 unit_ref_set(&UNIT(s
)->slice
, parent
);
88 static int slice_add_default_dependencies(Slice
*s
) {
93 if (!UNIT(s
)->default_dependencies
)
96 /* Make sure slices are unloaded on shutdown */
97 r
= unit_add_two_dependencies_by_name(
99 UNIT_BEFORE
, UNIT_CONFLICTS
,
100 SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
107 static int slice_verify(Slice
*s
) {
108 _cleanup_free_
char *parent
= NULL
;
113 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
116 if (!slice_name_is_valid(UNIT(s
)->id
)) {
117 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
121 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
123 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
125 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
126 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
133 static int slice_load_root_slice(Unit
*u
) {
136 if (!unit_has_name(u
, SPECIAL_ROOT_SLICE
))
141 /* The root 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;
145 u
->ignore_on_isolate
= true;
148 u
->description
= strdup("Root Slice");
149 if (!u
->documentation
)
150 u
->documentation
= strv_new("man:systemd.special(7)", NULL
);
155 static int slice_load(Unit
*u
) {
160 assert(u
->load_state
== UNIT_STUB
);
162 r
= slice_load_root_slice(u
);
165 r
= unit_load_fragment_and_dropin_optional(u
);
169 /* This is a new unit? Then let's add in some extras */
170 if (u
->load_state
== UNIT_LOADED
) {
172 r
= unit_patch_contexts(u
);
176 r
= slice_add_parent_slice(s
);
180 r
= slice_add_default_dependencies(s
);
185 return slice_verify(s
);
188 static int slice_coldplug(Unit
*u
) {
192 assert(t
->state
== SLICE_DEAD
);
194 if (t
->deserialized_state
!= t
->state
)
195 slice_set_state(t
, t
->deserialized_state
);
200 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
207 "%sSlice State: %s\n",
208 prefix
, slice_state_to_string(t
->state
));
210 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
213 static int slice_start(Unit
*u
) {
218 assert(t
->state
== SLICE_DEAD
);
220 r
= unit_acquire_invocation_id(u
);
224 (void) unit_realize_cgroup(u
);
225 (void) unit_reset_cpu_accounting(u
);
226 (void) unit_reset_ip_accounting(u
);
228 slice_set_state(t
, SLICE_ACTIVE
);
232 static int slice_stop(Unit
*u
) {
236 assert(t
->state
== SLICE_ACTIVE
);
238 /* We do not need to destroy the cgroup explicitly,
239 * unit_notify() will do that for us anyway. */
241 slice_set_state(t
, SLICE_DEAD
);
245 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
246 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
249 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
256 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
260 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
268 if (streq(key
, "state")) {
271 state
= slice_state_from_string(value
);
273 log_debug("Failed to parse state value %s", value
);
275 s
->deserialized_state
= state
;
278 log_debug("Unknown serialization key '%s'", key
);
283 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
286 return state_translation_table
[SLICE(u
)->state
];
289 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
292 return slice_state_to_string(SLICE(u
)->state
);
295 static void slice_enumerate(Manager
*m
) {
301 u
= manager_get_unit(m
, SPECIAL_ROOT_SLICE
);
303 r
= unit_new_for_name(m
, sizeof(Slice
), SPECIAL_ROOT_SLICE
, &u
);
305 log_error_errno(r
, "Failed to allocate the special " SPECIAL_ROOT_SLICE
" unit: %m");
311 SLICE(u
)->deserialized_state
= SLICE_ACTIVE
;
313 unit_add_to_load_queue(u
);
314 unit_add_to_dbus_queue(u
);
317 const UnitVTable slice_vtable
= {
318 .object_size
= sizeof(Slice
),
319 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
325 .private_section
= "Slice",
327 .can_transient
= true,
332 .coldplug
= slice_coldplug
,
336 .start
= slice_start
,
341 .serialize
= slice_serialize
,
342 .deserialize_item
= slice_deserialize_item
,
344 .active_state
= slice_active_state
,
345 .sub_state_to_string
= slice_sub_state_to_string
,
347 .bus_vtable
= bus_slice_vtable
,
348 .bus_set_property
= bus_slice_set_property
,
349 .bus_commit_properties
= bus_slice_commit_properties
,
351 .enumerate
= slice_enumerate
,
353 .status_message_formats
= {
354 .finished_start_job
= {
355 [JOB_DONE
] = "Created slice %s.",
357 .finished_stop_job
= {
358 [JOB_DONE
] = "Removed slice %s.",