]> git.proxmox.com Git - systemd.git/blame - src/core/slice.c
Imported Upstream version 226
[systemd.git] / src / core / slice.c
CommitLineData
14228c0d
MB
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>
14228c0d 23
14228c0d 24#include "log.h"
d9dfd233 25#include "strv.h"
14228c0d
MB
26#include "special.h"
27#include "unit-name.h"
d9dfd233
MP
28#include "unit.h"
29#include "slice.h"
30#include "dbus-slice.h"
14228c0d
MB
31
32static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
33 [SLICE_DEAD] = UNIT_INACTIVE,
34 [SLICE_ACTIVE] = UNIT_ACTIVE
35};
36
14228c0d
MB
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
53static int slice_add_parent_slice(Slice *s) {
54 char *a, *dash;
55 Unit *parent;
56 int r;
57
58 assert(s);
59
60 if (UNIT_ISSET(UNIT(s)->slice))
61 return 0;
62
63 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
64 return 0;
65
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;
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 */
87 r = unit_add_two_dependencies_by_name(
88 UNIT(s),
89 UNIT_BEFORE, UNIT_CONFLICTS,
90 SPECIAL_SHUTDOWN_TARGET, NULL, true);
91 if (r < 0)
92 return r;
93
94 return 0;
95}
96
e3bff60a 97
14228c0d 98static int slice_verify(Slice *s) {
e3bff60a
MP
99 _cleanup_free_ char *parent = NULL;
100 int r;
101
14228c0d
MB
102 assert(s);
103
104 if (UNIT(s)->load_state != UNIT_LOADED)
105 return 0;
106
e3bff60a
MP
107 if (!slice_name_is_valid(UNIT(s)->id)) {
108 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
109 return -EINVAL;
110 }
14228c0d 111
e3bff60a
MP
112 r = slice_build_parent_slice(UNIT(s)->id, &parent);
113 if (r < 0)
114 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
14228c0d 115
e3bff60a
MP
116 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
117 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
118 return -EINVAL;
14228c0d
MB
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
130 r = unit_load_fragment_and_dropin_optional(u);
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
60f067b4
JS
137 r = unit_patch_contexts(u);
138 if (r < 0)
139 return r;
140
14228c0d
MB
141 r = slice_add_parent_slice(s);
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 }
150 }
151
152 return slice_verify(s);
153}
154
155static int slice_coldplug(Unit *u) {
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));
176
177 cgroup_context_dump(&t->cgroup_context, f, prefix);
178}
179
180static int slice_start(Unit *u) {
181 Slice *t = SLICE(u);
182
183 assert(t);
184 assert(t->state == SLICE_DEAD);
185
e3bff60a
MP
186 (void) unit_realize_cgroup(u);
187 (void) unit_reset_cpu_usage(u);
14228c0d
MB
188
189 slice_set_state(t, SLICE_ACTIVE);
e735f4d4 190 return 1;
14228c0d
MB
191}
192
193static int slice_stop(Unit *u) {
194 Slice *t = SLICE(u);
195
196 assert(t);
197 assert(t->state == SLICE_ACTIVE);
198
199 /* We do not need to destroy the cgroup explicitly,
200 * unit_notify() will do that for us anyway. */
201
202 slice_set_state(t, SLICE_DEAD);
e735f4d4 203 return 1;
14228c0d
MB
204}
205
60f067b4 206static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
14228c0d
MB
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
d9dfd233
MP
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
14228c0d
MB
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),
60f067b4
JS
299 .cgroup_context_offset = offsetof(Slice, cgroup_context),
300
14228c0d
MB
301 .sections =
302 "Unit\0"
303 "Slice\0"
304 "Install\0",
14228c0d 305 .private_section = "Slice",
14228c0d
MB
306
307 .no_alias = true,
308 .no_instances = true,
309
14228c0d 310 .load = slice_load,
14228c0d
MB
311
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
60f067b4 327 .bus_vtable = bus_slice_vtable,
14228c0d
MB
328 .bus_set_property = bus_slice_set_property,
329 .bus_commit_properties = bus_slice_commit_properties,
330
d9dfd233
MP
331 .enumerate = slice_enumerate,
332
14228c0d
MB
333 .status_message_formats = {
334 .finished_start_job = {
335 [JOB_DONE] = "Created slice %s.",
14228c0d
MB
336 },
337 .finished_stop_job = {
338 [JOB_DONE] = "Removed slice %s.",
339 },
340 },
341};