chiark / gitweb /
core: do not spawn jobs or touch other units during coldplugging
[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, Hashmap *deferred_work) {
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         (void) unit_realize_cgroup(u);
185         (void) unit_reset_cpu_usage(u);
186
187         slice_set_state(t, SLICE_ACTIVE);
188         return 1;
189 }
190
191 static int slice_stop(Unit *u) {
192         Slice *t = SLICE(u);
193
194         assert(t);
195         assert(t->state == SLICE_ACTIVE);
196
197         /* We do not need to destroy the cgroup explicitly,
198          * unit_notify() will do that for us anyway. */
199
200         slice_set_state(t, SLICE_DEAD);
201         return 1;
202 }
203
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);
206 }
207
208 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
209         Slice *s = SLICE(u);
210
211         assert(s);
212         assert(f);
213         assert(fds);
214
215         unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
216         return 0;
217 }
218
219 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
220         Slice *s = SLICE(u);
221
222         assert(u);
223         assert(key);
224         assert(value);
225         assert(fds);
226
227         if (streq(key, "state")) {
228                 SliceState state;
229
230                 state = slice_state_from_string(value);
231                 if (state < 0)
232                         log_debug("Failed to parse state value %s", value);
233                 else
234                         s->deserialized_state = state;
235
236         } else
237                 log_debug("Unknown serialization key '%s'", key);
238
239         return 0;
240 }
241
242 _pure_ static UnitActiveState slice_active_state(Unit *u) {
243         assert(u);
244
245         return state_translation_table[SLICE(u)->state];
246 }
247
248 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
249         assert(u);
250
251         return slice_state_to_string(SLICE(u)->state);
252 }
253
254 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
255         [SLICE_DEAD] = "dead",
256         [SLICE_ACTIVE] = "active"
257 };
258
259 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
260
261 const UnitVTable slice_vtable = {
262         .object_size = sizeof(Slice),
263         .cgroup_context_offset = offsetof(Slice, cgroup_context),
264
265         .sections =
266                 "Unit\0"
267                 "Slice\0"
268                 "Install\0",
269         .private_section = "Slice",
270
271         .no_alias = true,
272         .no_instances = true,
273
274         .load = slice_load,
275
276         .coldplug = slice_coldplug,
277
278         .dump = slice_dump,
279
280         .start = slice_start,
281         .stop = slice_stop,
282
283         .kill = slice_kill,
284
285         .serialize = slice_serialize,
286         .deserialize_item = slice_deserialize_item,
287
288         .active_state = slice_active_state,
289         .sub_state_to_string = slice_sub_state_to_string,
290
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,
295
296         .status_message_formats = {
297                 .finished_start_job = {
298                         [JOB_DONE]       = "Created slice %s.",
299                         [JOB_DEPENDENCY] = "Dependency failed for %s.",
300                 },
301                 .finished_stop_job = {
302                         [JOB_DONE]       = "Removed slice %s.",
303                 },
304         },
305 };