chiark / gitweb /
remove unused includes
[elogind.git] / src / core / slice.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "unit.h"
25 #include "slice.h"
26 #include "log.h"
27 #include "dbus-slice.h"
28 #include "special.h"
29 #include "unit-name.h"
30
31 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
32         [SLICE_DEAD] = UNIT_INACTIVE,
33         [SLICE_ACTIVE] = UNIT_ACTIVE
34 };
35
36 static void slice_set_state(Slice *t, SliceState state) {
37         SliceState old_state;
38         assert(t);
39
40         old_state = t->state;
41         t->state = state;
42
43         if (state != old_state)
44                 log_debug("%s changed %s -> %s",
45                           UNIT(t)->id,
46                           slice_state_to_string(old_state),
47                           slice_state_to_string(state));
48
49         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
50 }
51
52 static int slice_add_parent_slice(Slice *s) {
53         char *a, *dash;
54         Unit *parent;
55         int r;
56
57         assert(s);
58
59         if (UNIT_ISSET(UNIT(s)->slice))
60                 return 0;
61
62         if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
63                 return 0;
64
65         a = strdupa(UNIT(s)->id);
66         dash = strrchr(a, '-');
67         if (dash)
68                 strcpy(dash, ".slice");
69         else
70                 a = (char*) SPECIAL_ROOT_SLICE;
71
72         r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
73         if (r < 0)
74                 return r;
75
76         unit_ref_set(&UNIT(s)->slice, parent);
77         return 0;
78 }
79
80 static int slice_add_default_dependencies(Slice *s) {
81         int r;
82
83         assert(s);
84
85         /* Make sure slices are unloaded on shutdown */
86         r = unit_add_two_dependencies_by_name(
87                         UNIT(s),
88                         UNIT_BEFORE, UNIT_CONFLICTS,
89                         SPECIAL_SHUTDOWN_TARGET, NULL, true);
90         if (r < 0)
91                 return r;
92
93         return 0;
94 }
95
96 static int slice_verify(Slice *s) {
97         assert(s);
98
99         if (UNIT(s)->load_state != UNIT_LOADED)
100                 return 0;
101
102         if (UNIT_DEREF(UNIT(s)->slice)) {
103                 char *a, *dash;
104
105                 a = strdupa(UNIT(s)->id);
106                 dash = strrchr(a, '-');
107                 if (dash)
108                         strcpy(dash, ".slice");
109                 else
110                         a = (char*) SPECIAL_ROOT_SLICE;
111
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);
115                         return -EINVAL;
116                 }
117         }
118
119         return 0;
120 }
121
122 static int slice_load(Unit *u) {
123         Slice *s = SLICE(u);
124         int r;
125
126         assert(s);
127
128         r = unit_load_fragment_and_dropin_optional(u);
129         if (r < 0)
130                 return r;
131
132         /* This is a new unit? Then let's add in some extras */
133         if (u->load_state == UNIT_LOADED) {
134
135                 r = unit_patch_contexts(u);
136                 if (r < 0)
137                         return r;
138
139                 r = slice_add_parent_slice(s);
140                 if (r < 0)
141                         return r;
142
143                 if (u->default_dependencies) {
144                         r = slice_add_default_dependencies(s);
145                         if (r < 0)
146                                 return r;
147                 }
148         }
149
150         return slice_verify(s);
151 }
152
153 static int slice_coldplug(Unit *u) {
154         Slice *t = SLICE(u);
155
156         assert(t);
157         assert(t->state == SLICE_DEAD);
158
159         if (t->deserialized_state != t->state)
160                 slice_set_state(t, t->deserialized_state);
161
162         return 0;
163 }
164
165 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
166         Slice *t = SLICE(u);
167
168         assert(t);
169         assert(f);
170
171         fprintf(f,
172                 "%sSlice State: %s\n",
173                 prefix, slice_state_to_string(t->state));
174
175         cgroup_context_dump(&t->cgroup_context, f, prefix);
176 }
177
178 static int slice_start(Unit *u) {
179         Slice *t = SLICE(u);
180
181         assert(t);
182         assert(t->state == SLICE_DEAD);
183
184         unit_realize_cgroup(u);
185
186         slice_set_state(t, SLICE_ACTIVE);
187         return 1;
188 }
189
190 static int slice_stop(Unit *u) {
191         Slice *t = SLICE(u);
192
193         assert(t);
194         assert(t->state == SLICE_ACTIVE);
195
196         /* We do not need to destroy the cgroup explicitly,
197          * unit_notify() will do that for us anyway. */
198
199         slice_set_state(t, SLICE_DEAD);
200         return 1;
201 }
202
203 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
204         return unit_kill_common(u, who, signo, -1, -1, error);
205 }
206
207 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
208         Slice *s = SLICE(u);
209
210         assert(s);
211         assert(f);
212         assert(fds);
213
214         unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
215         return 0;
216 }
217
218 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
219         Slice *s = SLICE(u);
220
221         assert(u);
222         assert(key);
223         assert(value);
224         assert(fds);
225
226         if (streq(key, "state")) {
227                 SliceState state;
228
229                 state = slice_state_from_string(value);
230                 if (state < 0)
231                         log_debug("Failed to parse state value %s", value);
232                 else
233                         s->deserialized_state = state;
234
235         } else
236                 log_debug("Unknown serialization key '%s'", key);
237
238         return 0;
239 }
240
241 _pure_ static UnitActiveState slice_active_state(Unit *u) {
242         assert(u);
243
244         return state_translation_table[SLICE(u)->state];
245 }
246
247 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
248         assert(u);
249
250         return slice_state_to_string(SLICE(u)->state);
251 }
252
253 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
254         [SLICE_DEAD] = "dead",
255         [SLICE_ACTIVE] = "active"
256 };
257
258 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
259
260 const UnitVTable slice_vtable = {
261         .object_size = sizeof(Slice),
262         .cgroup_context_offset = offsetof(Slice, cgroup_context),
263
264         .sections =
265                 "Unit\0"
266                 "Slice\0"
267                 "Install\0",
268         .private_section = "Slice",
269
270         .no_alias = true,
271         .no_instances = true,
272
273         .load = slice_load,
274
275         .coldplug = slice_coldplug,
276
277         .dump = slice_dump,
278
279         .start = slice_start,
280         .stop = slice_stop,
281
282         .kill = slice_kill,
283
284         .serialize = slice_serialize,
285         .deserialize_item = slice_deserialize_item,
286
287         .active_state = slice_active_state,
288         .sub_state_to_string = slice_sub_state_to_string,
289
290         .bus_interface = "org.freedesktop.systemd1.Slice",
291         .bus_vtable = bus_slice_vtable,
292         .bus_set_property = bus_slice_set_property,
293         .bus_commit_properties = bus_slice_commit_properties,
294
295         .status_message_formats = {
296                 .finished_start_job = {
297                         [JOB_DONE]       = "Created slice %s.",
298                         [JOB_DEPENDENCY] = "Dependency failed for %s.",
299                 },
300                 .finished_stop_job = {
301                         [JOB_DONE]       = "Removed slice %s.",
302                 },
303         },
304 };