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_slice_link(Slice *s) {
62 if (UNIT_DEREF(UNIT(s)->slice))
65 a = strdupa(UNIT(s)->id);
67 dash = strrchr(a, '-');
71 strcpy(dash, ".slice");
73 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
77 unit_ref_set(&UNIT(s)->slice, parent);
81 static int slice_add_default_dependencies(Slice *s) {
86 /* Make sure slices are unloaded on shutdown */
87 r = unit_add_dependency_by_name(UNIT(s), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
94 static int slice_verify(Slice *s) {
97 if (UNIT(s)->load_state != UNIT_LOADED)
100 if (UNIT_DEREF(UNIT(s)->slice)) {
103 a = strdupa(UNIT(s)->id);
104 dash = strrchr(a, '-');
106 strcpy(dash, ".slice");
108 if (!unit_has_name(UNIT_DEREF(UNIT(s)->slice), a)) {
109 log_error_unit(UNIT(s)->id,
110 "%s located outside its parent slice. Refusing.", UNIT(s)->id);
119 static int slice_load(Unit *u) {
125 r = unit_load_fragment_and_dropin(u);
129 /* This is a new unit? Then let's add in some extras */
130 if (u->load_state == UNIT_LOADED) {
132 r = slice_add_slice_link(s);
136 if (u->default_dependencies) {
137 r = slice_add_default_dependencies(s);
142 r = unit_add_default_cgroups(UNIT(s));
147 return slice_verify(s);
150 static int slice_coldplug(Unit *u) {
154 assert(t->state == SLICE_DEAD);
156 if (t->deserialized_state != t->state)
157 slice_set_state(t, t->deserialized_state);
162 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
169 "%sSlice State: %s\n",
170 prefix, slice_state_to_string(t->state));
173 static int slice_start(Unit *u) {
178 assert(t->state == SLICE_DEAD);
180 r = cgroup_bonding_realize_list(u->cgroup_bondings);
184 cgroup_attribute_apply_list(u->cgroup_attributes, u->cgroup_bondings);
186 slice_set_state(t, SLICE_ACTIVE);
190 static int slice_stop(Unit *u) {
194 assert(t->state == SLICE_ACTIVE);
196 /* We do not need to trim the cgroup explicitly, unit_notify()
197 * will do that for us anyway. */
199 slice_set_state(t, SLICE_DEAD);
203 static int slice_kill(Unit *u, KillWho who, int signo, DBusError *error) {
204 return unit_kill_common(u, who, signo, -1, -1, error);
207 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
214 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
218 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
226 if (streq(key, "state")) {
229 state = slice_state_from_string(value);
231 log_debug("Failed to parse state value %s", value);
233 s->deserialized_state = state;
236 log_debug("Unknown serialization key '%s'", key);
241 _pure_ static UnitActiveState slice_active_state(Unit *u) {
244 return state_translation_table[SLICE(u)->state];
247 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
250 return slice_state_to_string(SLICE(u)->state);
253 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
254 [SLICE_DEAD] = "dead",
255 [SLICE_ACTIVE] = "active"
258 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
260 const UnitVTable slice_vtable = {
261 .object_size = sizeof(Slice),
268 .no_instances = true,
271 .coldplug = slice_coldplug,
275 .start = slice_start,
280 .serialize = slice_serialize,
281 .deserialize_item = slice_deserialize_item,
283 .active_state = slice_active_state,
284 .sub_state_to_string = slice_sub_state_to_string,
286 .bus_interface = "org.freedesktop.systemd1.Slice",
287 .bus_message_handler = bus_slice_message_handler,
289 .status_message_formats = {
290 .finished_start_job = {
291 [JOB_DONE] = "Installed slice %s.",
292 [JOB_DEPENDENCY] = "Dependency failed for %s.",
294 .finished_stop_job = {
295 [JOB_DONE] = "Deinstalled slice %s.",