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_set_state(Slice *t, SliceState state) {
46 if (state != old_state)
47 log_debug("%s changed %s -> %s",
49 slice_state_to_string(old_state),
50 slice_state_to_string(state));
52 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
55 static int slice_add_parent_slice(Slice *s) {
62 if (UNIT_ISSET(UNIT(s)->slice))
65 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
68 a = strdupa(UNIT(s)->id);
69 dash = strrchr(a, '-');
71 strcpy(dash, ".slice");
73 a = (char*) SPECIAL_ROOT_SLICE;
75 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
79 unit_ref_set(&UNIT(s)->slice, parent);
83 static int slice_add_default_dependencies(Slice *s) {
88 /* Make sure slices are unloaded on shutdown */
89 r = unit_add_two_dependencies_by_name(
91 UNIT_BEFORE, UNIT_CONFLICTS,
92 SPECIAL_SHUTDOWN_TARGET, NULL, true);
99 static int slice_verify(Slice *s) {
102 if (UNIT(s)->load_state != UNIT_LOADED)
105 if (UNIT_DEREF(UNIT(s)->slice)) {
108 a = strdupa(UNIT(s)->id);
109 dash = strrchr(a, '-');
111 strcpy(dash, ".slice");
113 a = (char*) SPECIAL_ROOT_SLICE;
115 if (!unit_has_name(UNIT_DEREF(UNIT(s)->slice), a)) {
116 log_unit_error(UNIT(s)->id,
117 "%s located outside its parent slice. Refusing.", UNIT(s)->id);
125 static int slice_load(Unit *u) {
131 r = unit_load_fragment_and_dropin_optional(u);
135 /* This is a new unit? Then let's add in some extras */
136 if (u->load_state == UNIT_LOADED) {
138 r = unit_patch_contexts(u);
142 r = slice_add_parent_slice(s);
146 if (u->default_dependencies) {
147 r = slice_add_default_dependencies(s);
153 return slice_verify(s);
156 static int slice_coldplug(Unit *u) {
160 assert(t->state == SLICE_DEAD);
162 if (t->deserialized_state != t->state)
163 slice_set_state(t, t->deserialized_state);
168 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
175 "%sSlice State: %s\n",
176 prefix, slice_state_to_string(t->state));
178 cgroup_context_dump(&t->cgroup_context, f, prefix);
181 static int slice_start(Unit *u) {
185 assert(t->state == SLICE_DEAD);
187 unit_realize_cgroup(u);
189 slice_set_state(t, SLICE_ACTIVE);
193 static int slice_stop(Unit *u) {
197 assert(t->state == SLICE_ACTIVE);
199 /* We do not need to destroy the cgroup explicitly,
200 * unit_notify() will do that for us anyway. */
202 slice_set_state(t, SLICE_DEAD);
206 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
207 return unit_kill_common(u, who, signo, -1, -1, error);
210 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
217 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
221 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
229 if (streq(key, "state")) {
232 state = slice_state_from_string(value);
234 log_debug("Failed to parse state value %s", value);
236 s->deserialized_state = state;
239 log_debug("Unknown serialization key '%s'", key);
244 _pure_ static UnitActiveState slice_active_state(Unit *u) {
247 return state_translation_table[SLICE(u)->state];
250 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
253 return slice_state_to_string(SLICE(u)->state);
256 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
257 [SLICE_DEAD] = "dead",
258 [SLICE_ACTIVE] = "active"
261 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
263 const UnitVTable slice_vtable = {
264 .object_size = sizeof(Slice),
265 .cgroup_context_offset = offsetof(Slice, cgroup_context),
271 .private_section = "Slice",
274 .no_instances = true,
278 .coldplug = slice_coldplug,
282 .start = slice_start,
287 .serialize = slice_serialize,
288 .deserialize_item = slice_deserialize_item,
290 .active_state = slice_active_state,
291 .sub_state_to_string = slice_sub_state_to_string,
293 .bus_interface = "org.freedesktop.systemd1.Slice",
294 .bus_vtable = bus_slice_vtable,
295 .bus_set_property = bus_slice_set_property,
296 .bus_commit_properties = bus_slice_commit_properties,
298 .status_message_formats = {
299 .finished_start_job = {
300 [JOB_DONE] = "Created slice %s.",
301 [JOB_DEPENDENCY] = "Dependency failed for %s.",
303 .finished_stop_job = {
304 [JOB_DONE] = "Removed slice %s.",