1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "dbus-slice.h"
29 #include "unit-name.h"
31 static const UnitActiveState state_translation_table
[_SLICE_STATE_MAX
] = {
32 [SLICE_DEAD
] = UNIT_INACTIVE
,
33 [SLICE_ACTIVE
] = UNIT_ACTIVE
36 static void slice_set_state(Slice
*t
, SliceState state
) {
43 if (state
!= old_state
)
44 log_debug("%s changed %s -> %s",
46 slice_state_to_string(old_state
),
47 slice_state_to_string(state
));
49 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
52 static int slice_add_parent_slice(Slice
*s
) {
59 if (UNIT_ISSET(UNIT(s
)->slice
))
62 if (unit_has_name(UNIT(s
), SPECIAL_ROOT_SLICE
))
65 a
= strdupa(UNIT(s
)->id
);
66 dash
= strrchr(a
, '-');
68 strcpy(dash
, ".slice");
70 a
= (char*) SPECIAL_ROOT_SLICE
;
72 r
= manager_load_unit(UNIT(s
)->manager
, a
, NULL
, NULL
, &parent
);
76 unit_ref_set(&UNIT(s
)->slice
, parent
);
80 static int slice_add_default_dependencies(Slice
*s
) {
85 /* Make sure slices are unloaded on shutdown */
86 r
= unit_add_two_dependencies_by_name(
88 UNIT_BEFORE
, UNIT_CONFLICTS
,
89 SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
97 static int slice_verify(Slice
*s
) {
98 _cleanup_free_
char *parent
= NULL
;
103 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
106 if (!slice_name_is_valid(UNIT(s
)->id
)) {
107 log_unit_error(UNIT(s
), "Slice name %s is not valid. Refusing.", UNIT(s
)->id
);
111 r
= slice_build_parent_slice(UNIT(s
)->id
, &parent
);
113 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine parent slice: %m");
115 if (parent
? !unit_has_name(UNIT_DEREF(UNIT(s
)->slice
), parent
) : UNIT_ISSET(UNIT(s
)->slice
)) {
116 log_unit_error(UNIT(s
), "Located outside of parent slice. Refusing.");
123 static int slice_load(Unit
*u
) {
129 r
= unit_load_fragment_and_dropin_optional(u
);
133 /* This is a new unit? Then let's add in some extras */
134 if (u
->load_state
== UNIT_LOADED
) {
136 r
= unit_patch_contexts(u
);
140 r
= slice_add_parent_slice(s
);
144 if (u
->default_dependencies
) {
145 r
= slice_add_default_dependencies(s
);
151 return slice_verify(s
);
154 static int slice_coldplug(Unit
*u
) {
158 assert(t
->state
== SLICE_DEAD
);
160 if (t
->deserialized_state
!= t
->state
)
161 slice_set_state(t
, t
->deserialized_state
);
166 static void slice_dump(Unit
*u
, FILE *f
, const char *prefix
) {
173 "%sSlice State: %s\n",
174 prefix
, slice_state_to_string(t
->state
));
176 cgroup_context_dump(&t
->cgroup_context
, f
, prefix
);
179 static int slice_start(Unit
*u
) {
183 assert(t
->state
== SLICE_DEAD
);
185 (void) unit_realize_cgroup(u
);
186 (void) unit_reset_cpu_usage(u
);
188 slice_set_state(t
, SLICE_ACTIVE
);
192 static int slice_stop(Unit
*u
) {
196 assert(t
->state
== SLICE_ACTIVE
);
198 /* We do not need to destroy the cgroup explicitly,
199 * unit_notify() will do that for us anyway. */
201 slice_set_state(t
, SLICE_DEAD
);
205 static int slice_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
206 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
209 static int slice_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
216 unit_serialize_item(u
, f
, "state", slice_state_to_string(s
->state
));
220 static int slice_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
228 if (streq(key
, "state")) {
231 state
= slice_state_from_string(value
);
233 log_debug("Failed to parse state value %s", value
);
235 s
->deserialized_state
= state
;
238 log_debug("Unknown serialization key '%s'", key
);
243 _pure_
static UnitActiveState
slice_active_state(Unit
*u
) {
246 return state_translation_table
[SLICE(u
)->state
];
249 _pure_
static const char *slice_sub_state_to_string(Unit
*u
) {
252 return slice_state_to_string(SLICE(u
)->state
);
255 static const char* const slice_state_table
[_SLICE_STATE_MAX
] = {
256 [SLICE_DEAD
] = "dead",
257 [SLICE_ACTIVE
] = "active"
260 DEFINE_STRING_TABLE_LOOKUP(slice_state
, SliceState
);
262 const UnitVTable slice_vtable
= {
263 .object_size
= sizeof(Slice
),
264 .cgroup_context_offset
= offsetof(Slice
, cgroup_context
),
270 .private_section
= "Slice",
273 .no_instances
= true,
277 .coldplug
= slice_coldplug
,
281 .start
= slice_start
,
286 .serialize
= slice_serialize
,
287 .deserialize_item
= slice_deserialize_item
,
289 .active_state
= slice_active_state
,
290 .sub_state_to_string
= slice_sub_state_to_string
,
292 .bus_interface
= "org.freedesktop.systemd1.Slice",
293 .bus_vtable
= bus_slice_vtable
,
294 .bus_set_property
= bus_slice_set_property
,
295 .bus_commit_properties
= bus_slice_commit_properties
,
297 .status_message_formats
= {
298 .finished_start_job
= {
299 [JOB_DONE
] = "Created slice %s.",
301 .finished_stop_job
= {
302 [JOB_DONE
] = "Removed slice %s.",