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