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