]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/slice.c
core: unified cgroup hierarchy support
[thirdparty/systemd.git] / src / core / slice.c
CommitLineData
a016b922
LP
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>
a016b922 23
a016b922 24#include "log.h"
efdb0237 25#include "strv.h"
a016b922
LP
26#include "special.h"
27#include "unit-name.h"
efdb0237
LP
28#include "unit.h"
29#include "slice.h"
30#include "dbus-slice.h"
a016b922
LP
31
32static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
33 [SLICE_DEAD] = UNIT_INACTIVE,
34 [SLICE_ACTIVE] = UNIT_ACTIVE
35};
36
37static void slice_set_state(Slice *t, SliceState state) {
38 SliceState old_state;
39 assert(t);
40
41 old_state = t->state;
42 t->state = state;
43
44 if (state != old_state)
45 log_debug("%s changed %s -> %s",
46 UNIT(t)->id,
47 slice_state_to_string(old_state),
48 slice_state_to_string(state));
49
50 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
51}
52
4ad49000 53static int slice_add_parent_slice(Slice *s) {
a016b922 54 char *a, *dash;
a016b922 55 Unit *parent;
4ad49000 56 int r;
a016b922
LP
57
58 assert(s);
59
4ad49000 60 if (UNIT_ISSET(UNIT(s)->slice))
a016b922
LP
61 return 0;
62
4ad49000 63 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
a016b922
LP
64 return 0;
65
4ad49000
LP
66 a = strdupa(UNIT(s)->id);
67 dash = strrchr(a, '-');
68 if (dash)
69 strcpy(dash, ".slice");
70 else
71 a = (char*) SPECIAL_ROOT_SLICE;
a016b922
LP
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
81static int slice_add_default_dependencies(Slice *s) {
82 int r;
83
84 assert(s);
85
86 /* Make sure slices are unloaded on shutdown */
6c12b52e
LP
87 r = unit_add_two_dependencies_by_name(
88 UNIT(s),
89 UNIT_BEFORE, UNIT_CONFLICTS,
90 SPECIAL_SHUTDOWN_TARGET, NULL, true);
a016b922
LP
91 if (r < 0)
92 return r;
93
94 return 0;
95}
96
93c47472 97
a016b922 98static int slice_verify(Slice *s) {
93c47472
LP
99 _cleanup_free_ char *parent = NULL;
100 int r;
101
a016b922
LP
102 assert(s);
103
104 if (UNIT(s)->load_state != UNIT_LOADED)
105 return 0;
106
93c47472 107 if (!slice_name_is_valid(UNIT(s)->id)) {
f2341e0a 108 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
93c47472
LP
109 return -EINVAL;
110 }
111
112 r = slice_build_parent_slice(UNIT(s)->id, &parent);
113 if (r < 0)
f2341e0a 114 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
93c47472
LP
115
116 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
f2341e0a 117 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
93c47472 118 return -EINVAL;
a016b922
LP
119 }
120
121 return 0;
122}
123
124static int slice_load(Unit *u) {
125 Slice *s = SLICE(u);
126 int r;
127
128 assert(s);
129
4ad49000 130 r = unit_load_fragment_and_dropin_optional(u);
a016b922
LP
131 if (r < 0)
132 return r;
133
134 /* This is a new unit? Then let's add in some extras */
135 if (u->load_state == UNIT_LOADED) {
136
598459ce
LP
137 r = unit_patch_contexts(u);
138 if (r < 0)
139 return r;
140
4ad49000 141 r = slice_add_parent_slice(s);
a016b922
LP
142 if (r < 0)
143 return r;
144
145 if (u->default_dependencies) {
146 r = slice_add_default_dependencies(s);
147 if (r < 0)
148 return r;
149 }
a016b922
LP
150 }
151
152 return slice_verify(s);
153}
154
be847e82 155static int slice_coldplug(Unit *u) {
a016b922
LP
156 Slice *t = SLICE(u);
157
158 assert(t);
159 assert(t->state == SLICE_DEAD);
160
161 if (t->deserialized_state != t->state)
162 slice_set_state(t, t->deserialized_state);
163
164 return 0;
165}
166
167static void slice_dump(Unit *u, FILE *f, const char *prefix) {
168 Slice *t = SLICE(u);
169
170 assert(t);
171 assert(f);
172
173 fprintf(f,
174 "%sSlice State: %s\n",
175 prefix, slice_state_to_string(t->state));
4ad49000
LP
176
177 cgroup_context_dump(&t->cgroup_context, f, prefix);
a016b922
LP
178}
179
180static int slice_start(Unit *u) {
181 Slice *t = SLICE(u);
a016b922
LP
182
183 assert(t);
184 assert(t->state == SLICE_DEAD);
185
5ad096b3
LP
186 (void) unit_realize_cgroup(u);
187 (void) unit_reset_cpu_usage(u);
a016b922
LP
188
189 slice_set_state(t, SLICE_ACTIVE);
82a2b6bb 190 return 1;
a016b922
LP
191}
192
193static int slice_stop(Unit *u) {
194 Slice *t = SLICE(u);
195
196 assert(t);
197 assert(t->state == SLICE_ACTIVE);
198
4ad49000
LP
199 /* We do not need to destroy the cgroup explicitly,
200 * unit_notify() will do that for us anyway. */
a016b922
LP
201
202 slice_set_state(t, SLICE_DEAD);
82a2b6bb 203 return 1;
a016b922
LP
204}
205
718db961 206static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
a016b922
LP
207 return unit_kill_common(u, who, signo, -1, -1, error);
208}
209
210static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
211 Slice *s = SLICE(u);
212
213 assert(s);
214 assert(f);
215 assert(fds);
216
217 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
218 return 0;
219}
220
221static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
222 Slice *s = SLICE(u);
223
224 assert(u);
225 assert(key);
226 assert(value);
227 assert(fds);
228
229 if (streq(key, "state")) {
230 SliceState state;
231
232 state = slice_state_from_string(value);
233 if (state < 0)
234 log_debug("Failed to parse state value %s", value);
235 else
236 s->deserialized_state = state;
237
238 } else
239 log_debug("Unknown serialization key '%s'", key);
240
241 return 0;
242}
243
244_pure_ static UnitActiveState slice_active_state(Unit *u) {
245 assert(u);
246
247 return state_translation_table[SLICE(u)->state];
248}
249
250_pure_ static const char *slice_sub_state_to_string(Unit *u) {
251 assert(u);
252
253 return slice_state_to_string(SLICE(u)->state);
254}
255
efdb0237
LP
256static int slice_enumerate(Manager *m) {
257 Unit *u;
258 int r;
259
260 assert(m);
261
262 u = manager_get_unit(m, SPECIAL_ROOT_SLICE);
263 if (!u) {
264 u = unit_new(m, sizeof(Slice));
265 if (!u)
266 return log_oom();
267
268 r = unit_add_name(u, SPECIAL_ROOT_SLICE);
269 if (r < 0) {
270 unit_free(u);
271 return log_error_errno(r, "Failed to add -.slice name");
272 }
273 }
274
275 u->default_dependencies = false;
276 u->no_gc = true;
277 SLICE(u)->deserialized_state = SLICE_ACTIVE;
278
279 if (!u->description)
280 u->description = strdup("Root Slice");
281 if (!u->documentation)
282 (void) strv_extend(&u->documentation, "man:systemd.special(7)");
283
284 unit_add_to_load_queue(u);
285 unit_add_to_dbus_queue(u);
286
287 return 0;
288}
289
a016b922
LP
290static const char* const slice_state_table[_SLICE_STATE_MAX] = {
291 [SLICE_DEAD] = "dead",
292 [SLICE_ACTIVE] = "active"
293};
294
295DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
296
297const UnitVTable slice_vtable = {
298 .object_size = sizeof(Slice),
718db961
LP
299 .cgroup_context_offset = offsetof(Slice, cgroup_context),
300
a016b922
LP
301 .sections =
302 "Unit\0"
303 "Slice\0"
304 "Install\0",
4ad49000 305 .private_section = "Slice",
4ad49000 306
a016b922
LP
307 .no_alias = true,
308 .no_instances = true,
309
310 .load = slice_load,
4ad49000 311
a016b922
LP
312 .coldplug = slice_coldplug,
313
314 .dump = slice_dump,
315
316 .start = slice_start,
317 .stop = slice_stop,
318
319 .kill = slice_kill,
320
321 .serialize = slice_serialize,
322 .deserialize_item = slice_deserialize_item,
323
324 .active_state = slice_active_state,
325 .sub_state_to_string = slice_sub_state_to_string,
326
718db961 327 .bus_vtable = bus_slice_vtable,
8e2af478
LP
328 .bus_set_property = bus_slice_set_property,
329 .bus_commit_properties = bus_slice_commit_properties,
a016b922 330
efdb0237
LP
331 .enumerate = slice_enumerate,
332
a016b922
LP
333 .status_message_formats = {
334 .finished_start_job = {
4ad49000 335 [JOB_DONE] = "Created slice %s.",
a016b922
LP
336 },
337 .finished_stop_job = {
4ad49000 338 [JOB_DONE] = "Removed slice %s.",
a016b922
LP
339 },
340 },
341};