chiark / gitweb /
core: add new .slice unit type for partitioning systems
[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 #include <signal.h>
24 #include <unistd.h>
25
26 #include "unit.h"
27 #include "slice.h"
28 #include "load-fragment.h"
29 #include "log.h"
30 #include "dbus-slice.h"
31 #include "special.h"
32 #include "unit-name.h"
33
34 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
35         [SLICE_DEAD] = UNIT_INACTIVE,
36         [SLICE_ACTIVE] = UNIT_ACTIVE
37 };
38
39 static void slice_set_state(Slice *t, SliceState state) {
40         SliceState old_state;
41         assert(t);
42
43         old_state = t->state;
44         t->state = state;
45
46         if (state != old_state)
47                 log_debug("%s changed %s -> %s",
48                           UNIT(t)->id,
49                           slice_state_to_string(old_state),
50                           slice_state_to_string(state));
51
52         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
53 }
54
55 static int slice_add_slice_link(Slice *s) {
56         char *a, *dash;
57         int r;
58         Unit *parent;
59
60         assert(s);
61
62         if (UNIT_DEREF(UNIT(s)->slice))
63                 return 0;
64
65         a = strdupa(UNIT(s)->id);
66
67         dash = strrchr(a, '-');
68         if (!dash)
69                 return 0;
70
71         strcpy(dash, ".slice");
72
73         r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
74         if (r < 0)
75                 return r;
76
77         unit_ref_set(&UNIT(s)->slice, parent);
78         return 0;
79 }
80
81 static int slice_add_default_dependencies(Slice *s) {
82         int r;
83
84         assert(s);
85
86         /* Make sure slices are unloaded on shutdown */
87         r = unit_add_dependency_by_name(UNIT(s), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
88         if (r < 0)
89                 return r;
90
91         return 0;
92 }
93
94 static int slice_verify(Slice *s) {
95         assert(s);
96
97         if (UNIT(s)->load_state != UNIT_LOADED)
98                 return 0;
99
100         if (UNIT_DEREF(UNIT(s)->slice)) {
101                 char *a, *dash;
102
103                 a = strdupa(UNIT(s)->id);
104                 dash = strrchr(a, '-');
105                 if (dash) {
106                         strcpy(dash, ".slice");
107
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);
111                                 return -EINVAL;
112                         }
113                 }
114         }
115
116         return 0;
117 }
118
119 static int slice_load(Unit *u) {
120         Slice *s = SLICE(u);
121         int r;
122
123         assert(s);
124
125         r = unit_load_fragment_and_dropin(u);
126         if (r < 0)
127                 return r;
128
129         /* This is a new unit? Then let's add in some extras */
130         if (u->load_state == UNIT_LOADED) {
131
132                 r = slice_add_slice_link(s);
133                 if (r < 0)
134                         return r;
135
136                 if (u->default_dependencies) {
137                         r = slice_add_default_dependencies(s);
138                         if (r < 0)
139                                 return r;
140                 }
141
142                 r = unit_add_default_cgroups(UNIT(s));
143                 if (r < 0)
144                         return r;
145         }
146
147         return slice_verify(s);
148 }
149
150 static int slice_coldplug(Unit *u) {
151         Slice *t = SLICE(u);
152
153         assert(t);
154         assert(t->state == SLICE_DEAD);
155
156         if (t->deserialized_state != t->state)
157                 slice_set_state(t, t->deserialized_state);
158
159         return 0;
160 }
161
162 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
163         Slice *t = SLICE(u);
164
165         assert(t);
166         assert(f);
167
168         fprintf(f,
169                 "%sSlice State: %s\n",
170                 prefix, slice_state_to_string(t->state));
171 }
172
173 static int slice_start(Unit *u) {
174         Slice *t = SLICE(u);
175         int r;
176
177         assert(t);
178         assert(t->state == SLICE_DEAD);
179
180         r = cgroup_bonding_realize_list(u->cgroup_bondings);
181         if (r < 0)
182                 return r;
183
184         cgroup_attribute_apply_list(u->cgroup_attributes, u->cgroup_bondings);
185
186         slice_set_state(t, SLICE_ACTIVE);
187         return 0;
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 trim the cgroup explicitly, unit_notify()
197          * will do that for us anyway. */
198
199         slice_set_state(t, SLICE_DEAD);
200         return 0;
201 }
202
203 static int slice_kill(Unit *u, KillWho who, int signo, DBusError *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         .sections =
263                 "Unit\0"
264                 "Slice\0"
265                 "Install\0",
266
267         .no_alias = true,
268         .no_instances = true,
269
270         .load = slice_load,
271         .coldplug = slice_coldplug,
272
273         .dump = slice_dump,
274
275         .start = slice_start,
276         .stop = slice_stop,
277
278         .kill = slice_kill,
279
280         .serialize = slice_serialize,
281         .deserialize_item = slice_deserialize_item,
282
283         .active_state = slice_active_state,
284         .sub_state_to_string = slice_sub_state_to_string,
285
286         .bus_interface = "org.freedesktop.systemd1.Slice",
287         .bus_message_handler = bus_slice_message_handler,
288
289         .status_message_formats = {
290                 .finished_start_job = {
291                         [JOB_DONE]       = "Installed slice %s.",
292                         [JOB_DEPENDENCY] = "Dependency failed for %s.",
293                 },
294                 .finished_stop_job = {
295                         [JOB_DONE]       = "Deinstalled slice %s.",
296                 },
297         },
298 };