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/>.
28 #include "load-fragment.h"
30 #include "dbus-slice.h"
32 #include "unit-name.h"
34 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
35 [SLICE_DEAD] = UNIT_INACTIVE,
36 [SLICE_ACTIVE] = UNIT_ACTIVE
39 static void slice_init(Unit *u) {
43 assert(u->load_state == UNIT_STUB);
45 cgroup_context_init(&s->cgroup_context);
48 static void slice_done(Unit *u) {
53 cgroup_context_done(&s->cgroup_context);
56 static void slice_set_state(Slice *t, SliceState state) {
63 if (state != old_state)
64 log_debug("%s changed %s -> %s",
66 slice_state_to_string(old_state),
67 slice_state_to_string(state));
69 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
72 static int slice_add_parent_slice(Slice *s) {
79 if (UNIT_ISSET(UNIT(s)->slice))
82 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
85 a = strdupa(UNIT(s)->id);
86 dash = strrchr(a, '-');
88 strcpy(dash, ".slice");
90 a = (char*) SPECIAL_ROOT_SLICE;
92 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
96 unit_ref_set(&UNIT(s)->slice, parent);
100 static int slice_add_default_dependencies(Slice *s) {
105 /* Make sure slices are unloaded on shutdown */
106 r = unit_add_dependency_by_name(UNIT(s), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
113 static int slice_verify(Slice *s) {
116 if (UNIT(s)->load_state != UNIT_LOADED)
119 if (UNIT_DEREF(UNIT(s)->slice)) {
122 a = strdupa(UNIT(s)->id);
123 dash = strrchr(a, '-');
125 strcpy(dash, ".slice");
127 a = (char*) SPECIAL_ROOT_SLICE;
129 if (!unit_has_name(UNIT_DEREF(UNIT(s)->slice), a)) {
130 log_error_unit(UNIT(s)->id,
131 "%s located outside its parent slice. Refusing.", UNIT(s)->id);
139 static int slice_load(Unit *u) {
145 r = unit_load_fragment_and_dropin_optional(u);
149 /* This is a new unit? Then let's add in some extras */
150 if (u->load_state == UNIT_LOADED) {
152 r = slice_add_parent_slice(s);
156 if (u->default_dependencies) {
157 r = slice_add_default_dependencies(s);
163 return slice_verify(s);
166 static int slice_coldplug(Unit *u) {
170 assert(t->state == SLICE_DEAD);
172 if (t->deserialized_state != t->state)
173 slice_set_state(t, t->deserialized_state);
178 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
185 "%sSlice State: %s\n",
186 prefix, slice_state_to_string(t->state));
188 cgroup_context_dump(&t->cgroup_context, f, prefix);
191 static int slice_start(Unit *u) {
195 assert(t->state == SLICE_DEAD);
197 unit_realize_cgroup(u);
199 slice_set_state(t, SLICE_ACTIVE);
203 static int slice_stop(Unit *u) {
207 assert(t->state == SLICE_ACTIVE);
209 /* We do not need to destroy the cgroup explicitly,
210 * unit_notify() will do that for us anyway. */
212 slice_set_state(t, SLICE_DEAD);
216 static int slice_kill(Unit *u, KillWho who, int signo, DBusError *error) {
217 return unit_kill_common(u, who, signo, -1, -1, error);
220 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
227 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
231 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
239 if (streq(key, "state")) {
242 state = slice_state_from_string(value);
244 log_debug("Failed to parse state value %s", value);
246 s->deserialized_state = state;
249 log_debug("Unknown serialization key '%s'", key);
254 _pure_ static UnitActiveState slice_active_state(Unit *u) {
257 return state_translation_table[SLICE(u)->state];
260 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
263 return slice_state_to_string(SLICE(u)->state);
266 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
267 [SLICE_DEAD] = "dead",
268 [SLICE_ACTIVE] = "active"
271 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
273 const UnitVTable slice_vtable = {
274 .object_size = sizeof(Slice),
280 .private_section = "Slice",
281 .cgroup_context_offset = offsetof(Slice, cgroup_context),
284 .no_instances = true,
290 .coldplug = slice_coldplug,
294 .start = slice_start,
299 .serialize = slice_serialize,
300 .deserialize_item = slice_deserialize_item,
302 .active_state = slice_active_state,
303 .sub_state_to_string = slice_sub_state_to_string,
305 .bus_interface = "org.freedesktop.systemd1.Slice",
306 .bus_message_handler = bus_slice_message_handler,
307 .bus_set_property = bus_slice_set_property,
308 .bus_commit_properties = bus_slice_commit_properties,
310 .status_message_formats = {
311 .finished_start_job = {
312 [JOB_DONE] = "Created slice %s.",
313 [JOB_DEPENDENCY] = "Dependency failed for %s.",
315 .finished_stop_job = {
316 [JOB_DONE] = "Removed slice %s.",