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);
96 static int slice_verify(Slice *s) {
99 if (UNIT(s)->load_state != UNIT_LOADED)
102 if (UNIT_DEREF(UNIT(s)->slice)) {
105 a = strdupa(UNIT(s)->id);
106 dash = strrchr(a, '-');
108 strcpy(dash, ".slice");
110 a = (char*) SPECIAL_ROOT_SLICE;
112 if (!unit_has_name(UNIT_DEREF(UNIT(s)->slice), a)) {
113 log_unit_error(UNIT(s)->id,
114 "%s located outside its parent slice. Refusing.", UNIT(s)->id);
122 static int slice_load(Unit *u) {
128 r = unit_load_fragment_and_dropin_optional(u);
132 /* This is a new unit? Then let's add in some extras */
133 if (u->load_state == UNIT_LOADED) {
135 r = unit_patch_contexts(u);
139 r = slice_add_parent_slice(s);
143 if (u->default_dependencies) {
144 r = slice_add_default_dependencies(s);
150 return slice_verify(s);
153 static int slice_coldplug(Unit *u, Hashmap *deferred_work) {
157 assert(t->state == SLICE_DEAD);
159 if (t->deserialized_state != t->state)
160 slice_set_state(t, t->deserialized_state);
165 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
172 "%sSlice State: %s\n",
173 prefix, slice_state_to_string(t->state));
175 cgroup_context_dump(&t->cgroup_context, f, prefix);
178 static int slice_start(Unit *u) {
182 assert(t->state == SLICE_DEAD);
184 (void) unit_realize_cgroup(u);
185 (void) unit_reset_cpu_usage(u);
187 slice_set_state(t, SLICE_ACTIVE);
191 static int slice_stop(Unit *u) {
195 assert(t->state == SLICE_ACTIVE);
197 /* We do not need to destroy the cgroup explicitly,
198 * unit_notify() will do that for us anyway. */
200 slice_set_state(t, SLICE_DEAD);
204 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
205 return unit_kill_common(u, who, signo, -1, -1, error);
208 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
215 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
219 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
227 if (streq(key, "state")) {
230 state = slice_state_from_string(value);
232 log_debug("Failed to parse state value %s", value);
234 s->deserialized_state = state;
237 log_debug("Unknown serialization key '%s'", key);
242 _pure_ static UnitActiveState slice_active_state(Unit *u) {
245 return state_translation_table[SLICE(u)->state];
248 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
251 return slice_state_to_string(SLICE(u)->state);
254 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
255 [SLICE_DEAD] = "dead",
256 [SLICE_ACTIVE] = "active"
259 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
261 const UnitVTable slice_vtable = {
262 .object_size = sizeof(Slice),
263 .cgroup_context_offset = offsetof(Slice, cgroup_context),
269 .private_section = "Slice",
272 .no_instances = true,
276 .coldplug = slice_coldplug,
280 .start = slice_start,
285 .serialize = slice_serialize,
286 .deserialize_item = slice_deserialize_item,
288 .active_state = slice_active_state,
289 .sub_state_to_string = slice_sub_state_to_string,
291 .bus_interface = "org.freedesktop.systemd1.Slice",
292 .bus_vtable = bus_slice_vtable,
293 .bus_set_property = bus_slice_set_property,
294 .bus_commit_properties = bus_slice_commit_properties,
296 .status_message_formats = {
297 .finished_start_job = {
298 [JOB_DONE] = "Created slice %s.",
299 [JOB_DEPENDENCY] = "Dependency failed for %s.",
301 .finished_stop_job = {
302 [JOB_DONE] = "Removed slice %s.",