]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/slice.c
Merge pull request #4474 from poettering/nsswitch
[thirdparty/systemd.git] / src / core / slice.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21
22 #include "alloc-util.h"
23 #include "dbus-slice.h"
24 #include "log.h"
25 #include "slice.h"
26 #include "special.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unit-name.h"
30 #include "unit.h"
31
32 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
33 [SLICE_DEAD] = UNIT_INACTIVE,
34 [SLICE_ACTIVE] = UNIT_ACTIVE
35 };
36
37 static void slice_init(Unit *u) {
38 assert(u);
39 assert(u->load_state == UNIT_STUB);
40
41 u->ignore_on_isolate = true;
42 }
43
44 static void slice_set_state(Slice *t, SliceState state) {
45 SliceState old_state;
46 assert(t);
47
48 old_state = t->state;
49 t->state = state;
50
51 if (state != old_state)
52 log_debug("%s changed %s -> %s",
53 UNIT(t)->id,
54 slice_state_to_string(old_state),
55 slice_state_to_string(state));
56
57 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
58 }
59
60 static int slice_add_parent_slice(Slice *s) {
61 char *a, *dash;
62 Unit *parent;
63 int r;
64
65 assert(s);
66
67 if (UNIT_ISSET(UNIT(s)->slice))
68 return 0;
69
70 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
71 return 0;
72
73 a = strdupa(UNIT(s)->id);
74 dash = strrchr(a, '-');
75 if (dash)
76 strcpy(dash, ".slice");
77 else
78 a = (char*) SPECIAL_ROOT_SLICE;
79
80 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
81 if (r < 0)
82 return r;
83
84 unit_ref_set(&UNIT(s)->slice, parent);
85 return 0;
86 }
87
88 static int slice_add_default_dependencies(Slice *s) {
89 int r;
90
91 assert(s);
92
93 if (!UNIT(s)->default_dependencies)
94 return 0;
95
96 /* Make sure slices are unloaded on shutdown */
97 r = unit_add_two_dependencies_by_name(
98 UNIT(s),
99 UNIT_BEFORE, UNIT_CONFLICTS,
100 SPECIAL_SHUTDOWN_TARGET, NULL, true);
101 if (r < 0)
102 return r;
103
104 return 0;
105 }
106
107 static int slice_verify(Slice *s) {
108 _cleanup_free_ char *parent = NULL;
109 int r;
110
111 assert(s);
112
113 if (UNIT(s)->load_state != UNIT_LOADED)
114 return 0;
115
116 if (!slice_name_is_valid(UNIT(s)->id)) {
117 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
118 return -EINVAL;
119 }
120
121 r = slice_build_parent_slice(UNIT(s)->id, &parent);
122 if (r < 0)
123 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
124
125 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
126 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
127 return -EINVAL;
128 }
129
130 return 0;
131 }
132
133 static int slice_load_root_slice(Unit *u) {
134 assert(u);
135
136 if (!unit_has_name(u, SPECIAL_ROOT_SLICE))
137 return 0;
138
139 u->no_gc = true;
140
141 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
142 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
143
144 u->default_dependencies = false;
145 u->ignore_on_isolate = true;
146 u->refuse_manual_start = true;
147 u->refuse_manual_stop = true;
148
149 if (!u->description)
150 u->description = strdup("Root Slice");
151 if (!u->documentation)
152 u->documentation = strv_new("man:systemd.special(7)", NULL);
153
154 return 1;
155 }
156
157 static int slice_load(Unit *u) {
158 Slice *s = SLICE(u);
159 int r;
160
161 assert(s);
162 assert(u->load_state == UNIT_STUB);
163
164 r = slice_load_root_slice(u);
165 if (r < 0)
166 return r;
167 r = unit_load_fragment_and_dropin_optional(u);
168 if (r < 0)
169 return r;
170
171 /* This is a new unit? Then let's add in some extras */
172 if (u->load_state == UNIT_LOADED) {
173
174 r = unit_patch_contexts(u);
175 if (r < 0)
176 return r;
177
178 r = slice_add_parent_slice(s);
179 if (r < 0)
180 return r;
181
182 r = slice_add_default_dependencies(s);
183 if (r < 0)
184 return r;
185 }
186
187 return slice_verify(s);
188 }
189
190 static int slice_coldplug(Unit *u) {
191 Slice *t = SLICE(u);
192
193 assert(t);
194 assert(t->state == SLICE_DEAD);
195
196 if (t->deserialized_state != t->state)
197 slice_set_state(t, t->deserialized_state);
198
199 return 0;
200 }
201
202 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
203 Slice *t = SLICE(u);
204
205 assert(t);
206 assert(f);
207
208 fprintf(f,
209 "%sSlice State: %s\n",
210 prefix, slice_state_to_string(t->state));
211
212 cgroup_context_dump(&t->cgroup_context, f, prefix);
213 }
214
215 static int slice_start(Unit *u) {
216 Slice *t = SLICE(u);
217 int r;
218
219 assert(t);
220 assert(t->state == SLICE_DEAD);
221
222 r = unit_acquire_invocation_id(u);
223 if (r < 0)
224 return r;
225
226 (void) unit_realize_cgroup(u);
227 (void) unit_reset_cpu_usage(u);
228
229 slice_set_state(t, SLICE_ACTIVE);
230 return 1;
231 }
232
233 static int slice_stop(Unit *u) {
234 Slice *t = SLICE(u);
235
236 assert(t);
237 assert(t->state == SLICE_ACTIVE);
238
239 /* We do not need to destroy the cgroup explicitly,
240 * unit_notify() will do that for us anyway. */
241
242 slice_set_state(t, SLICE_DEAD);
243 return 1;
244 }
245
246 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
247 return unit_kill_common(u, who, signo, -1, -1, error);
248 }
249
250 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
251 Slice *s = SLICE(u);
252
253 assert(s);
254 assert(f);
255 assert(fds);
256
257 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
258 return 0;
259 }
260
261 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
262 Slice *s = SLICE(u);
263
264 assert(u);
265 assert(key);
266 assert(value);
267 assert(fds);
268
269 if (streq(key, "state")) {
270 SliceState state;
271
272 state = slice_state_from_string(value);
273 if (state < 0)
274 log_debug("Failed to parse state value %s", value);
275 else
276 s->deserialized_state = state;
277
278 } else
279 log_debug("Unknown serialization key '%s'", key);
280
281 return 0;
282 }
283
284 _pure_ static UnitActiveState slice_active_state(Unit *u) {
285 assert(u);
286
287 return state_translation_table[SLICE(u)->state];
288 }
289
290 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
291 assert(u);
292
293 return slice_state_to_string(SLICE(u)->state);
294 }
295
296 static void slice_enumerate(Manager *m) {
297 Unit *u;
298 int r;
299
300 assert(m);
301
302 u = manager_get_unit(m, SPECIAL_ROOT_SLICE);
303 if (!u) {
304 u = unit_new(m, sizeof(Slice));
305 if (!u) {
306 log_oom();
307 return;
308 }
309
310 r = unit_add_name(u, SPECIAL_ROOT_SLICE);
311 if (r < 0) {
312 unit_free(u);
313 log_error_errno(r, "Failed to add the "SPECIAL_ROOT_SLICE " name: %m");
314 return;
315 }
316 }
317
318 u->no_gc = true;
319 SLICE(u)->deserialized_state = SLICE_ACTIVE;
320
321 unit_add_to_load_queue(u);
322 unit_add_to_dbus_queue(u);
323 }
324
325 const UnitVTable slice_vtable = {
326 .object_size = sizeof(Slice),
327 .cgroup_context_offset = offsetof(Slice, cgroup_context),
328
329 .sections =
330 "Unit\0"
331 "Slice\0"
332 "Install\0",
333 .private_section = "Slice",
334
335 .can_transient = true,
336
337 .init = slice_init,
338 .load = slice_load,
339
340 .coldplug = slice_coldplug,
341
342 .dump = slice_dump,
343
344 .start = slice_start,
345 .stop = slice_stop,
346
347 .kill = slice_kill,
348
349 .serialize = slice_serialize,
350 .deserialize_item = slice_deserialize_item,
351
352 .active_state = slice_active_state,
353 .sub_state_to_string = slice_sub_state_to_string,
354
355 .bus_vtable = bus_slice_vtable,
356 .bus_set_property = bus_slice_set_property,
357 .bus_commit_properties = bus_slice_commit_properties,
358
359 .enumerate = slice_enumerate,
360
361 .status_message_formats = {
362 .finished_start_job = {
363 [JOB_DONE] = "Created slice %s.",
364 },
365 .finished_stop_job = {
366 [JOB_DONE] = "Removed slice %s.",
367 },
368 },
369 };