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);
46 unit_cgroup_context_init_defaults(u, &s->cgroup_context);
49 static void slice_done(Unit *u) {
54 cgroup_context_done(&s->cgroup_context);
57 static void slice_set_state(Slice *t, SliceState state) {
64 if (state != old_state)
65 log_debug("%s changed %s -> %s",
67 slice_state_to_string(old_state),
68 slice_state_to_string(state));
70 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
73 static int slice_add_parent_slice(Slice *s) {
80 if (UNIT_ISSET(UNIT(s)->slice))
83 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
86 a = strdupa(UNIT(s)->id);
87 dash = strrchr(a, '-');
89 strcpy(dash, ".slice");
91 a = (char*) SPECIAL_ROOT_SLICE;
93 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
97 unit_ref_set(&UNIT(s)->slice, parent);
101 static int slice_add_default_dependencies(Slice *s) {
106 /* Make sure slices are unloaded on shutdown */
107 r = unit_add_two_dependencies_by_name(
109 UNIT_BEFORE, UNIT_CONFLICTS,
110 SPECIAL_SHUTDOWN_TARGET, NULL, true);
117 static int slice_verify(Slice *s) {
120 if (UNIT(s)->load_state != UNIT_LOADED)
123 if (UNIT_DEREF(UNIT(s)->slice)) {
126 a = strdupa(UNIT(s)->id);
127 dash = strrchr(a, '-');
129 strcpy(dash, ".slice");
131 a = (char*) SPECIAL_ROOT_SLICE;
133 if (!unit_has_name(UNIT_DEREF(UNIT(s)->slice), a)) {
134 log_error_unit(UNIT(s)->id,
135 "%s located outside its parent slice. Refusing.", UNIT(s)->id);
143 static int slice_load(Unit *u) {
149 r = unit_load_fragment_and_dropin_optional(u);
153 /* This is a new unit? Then let's add in some extras */
154 if (u->load_state == UNIT_LOADED) {
156 r = slice_add_parent_slice(s);
160 if (u->default_dependencies) {
161 r = slice_add_default_dependencies(s);
167 return slice_verify(s);
170 static int slice_coldplug(Unit *u) {
174 assert(t->state == SLICE_DEAD);
176 if (t->deserialized_state != t->state)
177 slice_set_state(t, t->deserialized_state);
182 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
189 "%sSlice State: %s\n",
190 prefix, slice_state_to_string(t->state));
192 cgroup_context_dump(&t->cgroup_context, f, prefix);
195 static int slice_start(Unit *u) {
199 assert(t->state == SLICE_DEAD);
201 unit_realize_cgroup(u);
203 slice_set_state(t, SLICE_ACTIVE);
207 static int slice_stop(Unit *u) {
211 assert(t->state == SLICE_ACTIVE);
213 /* We do not need to destroy the cgroup explicitly,
214 * unit_notify() will do that for us anyway. */
216 slice_set_state(t, SLICE_DEAD);
220 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
221 return unit_kill_common(u, who, signo, -1, -1, error);
224 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
231 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
235 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
243 if (streq(key, "state")) {
246 state = slice_state_from_string(value);
248 log_debug("Failed to parse state value %s", value);
250 s->deserialized_state = state;
253 log_debug("Unknown serialization key '%s'", key);
258 _pure_ static UnitActiveState slice_active_state(Unit *u) {
261 return state_translation_table[SLICE(u)->state];
264 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
267 return slice_state_to_string(SLICE(u)->state);
270 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
271 [SLICE_DEAD] = "dead",
272 [SLICE_ACTIVE] = "active"
275 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
277 const UnitVTable slice_vtable = {
278 .object_size = sizeof(Slice),
279 .cgroup_context_offset = offsetof(Slice, cgroup_context),
285 .private_section = "Slice",
288 .no_instances = true,
294 .coldplug = slice_coldplug,
298 .start = slice_start,
303 .serialize = slice_serialize,
304 .deserialize_item = slice_deserialize_item,
306 .active_state = slice_active_state,
307 .sub_state_to_string = slice_sub_state_to_string,
309 .bus_interface = "org.freedesktop.systemd1.Slice",
310 .bus_vtable = bus_slice_vtable,
311 .bus_set_property = bus_slice_set_property,
312 .bus_commit_properties = bus_slice_commit_properties,
314 .status_message_formats = {
315 .finished_start_job = {
316 [JOB_DONE] = "Created slice %s.",
317 [JOB_DEPENDENCY] = "Dependency failed for %s.",
319 .finished_stop_job = {
320 [JOB_DONE] = "Removed slice %s.",