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