]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/slice.c
core: use ASSERT_PTR(CAST(u)) everywhere
[thirdparty/systemd.git] / src / core / slice.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4
5 #include "alloc-util.h"
6 #include "dbus-slice.h"
7 #include "dbus-unit.h"
8 #include "fd-util.h"
9 #include "log.h"
10 #include "serialize.h"
11 #include "slice.h"
12 #include "special.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "unit-name.h"
16 #include "unit.h"
17
18 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
19 [SLICE_DEAD] = UNIT_INACTIVE,
20 [SLICE_ACTIVE] = UNIT_ACTIVE
21 };
22
23 static void slice_init(Unit *u) {
24 assert(u);
25 assert(u->load_state == UNIT_STUB);
26
27 u->ignore_on_isolate = true;
28 }
29
30 static void slice_set_state(Slice *t, SliceState state) {
31 SliceState old_state;
32
33 assert(t);
34
35 if (t->state != state)
36 bus_unit_send_pending_change_signal(UNIT(t), false);
37
38 old_state = t->state;
39 t->state = state;
40
41 if (state != old_state)
42 log_debug("%s changed %s -> %s",
43 UNIT(t)->id,
44 slice_state_to_string(old_state),
45 slice_state_to_string(state));
46
47 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
48 }
49
50 static int slice_add_parent_slice(Slice *s) {
51 Unit *u = UNIT(ASSERT_PTR(s));
52 _cleanup_free_ char *a = NULL;
53 int r;
54
55 if (UNIT_GET_SLICE(u))
56 return 0;
57
58 r = slice_build_parent_slice(u->id, &a);
59 if (r <= 0) /* 0 means root slice */
60 return r;
61
62 return unit_add_dependency_by_name(u, UNIT_IN_SLICE, a, true, UNIT_DEPENDENCY_IMPLICIT);
63 }
64
65 static int slice_add_default_dependencies(Slice *s) {
66 int r;
67
68 assert(s);
69
70 if (!UNIT(s)->default_dependencies)
71 return 0;
72
73 /* Make sure slices are unloaded on shutdown */
74 r = unit_add_two_dependencies_by_name(
75 UNIT(s),
76 UNIT_BEFORE, UNIT_CONFLICTS,
77 SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
78 if (r < 0)
79 return r;
80
81 return 0;
82 }
83
84 static int slice_verify(Slice *s) {
85 _cleanup_free_ char *parent = NULL;
86 int r;
87
88 assert(s);
89 assert(UNIT(s)->load_state == UNIT_LOADED);
90
91 if (!slice_name_is_valid(UNIT(s)->id))
92 return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
93
94 r = slice_build_parent_slice(UNIT(s)->id, &parent);
95 if (r < 0)
96 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
97
98 /* If recursive errors are to be ignored, the parent slice should not be verified */
99 if (UNIT(s)->manager && FLAGS_SET(UNIT(s)->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
100 return 0;
101
102 if (parent ? !unit_has_name(UNIT_GET_SLICE(UNIT(s)), parent) : !!UNIT_GET_SLICE(UNIT(s)))
103 return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Located outside of parent slice. Refusing.");
104
105 return 0;
106 }
107
108 static int slice_load_root_slice(Unit *u) {
109 assert(u);
110
111 if (!unit_has_name(u, SPECIAL_ROOT_SLICE))
112 return 0;
113
114 u->perpetual = true;
115
116 /* The root slice is a bit special. For example it is always running and cannot be terminated. Because of its
117 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
118
119 u->default_dependencies = false;
120
121 if (!u->description)
122 u->description = strdup("Root Slice");
123 if (!u->documentation)
124 u->documentation = strv_new("man:systemd.special(7)");
125
126 return 1;
127 }
128
129 static int slice_load_system_slice(Unit *u) {
130 assert(u);
131
132 if (!MANAGER_IS_SYSTEM(u->manager))
133 return 0;
134 if (!unit_has_name(u, SPECIAL_SYSTEM_SLICE))
135 return 0;
136
137 u->perpetual = true;
138
139 /* The system slice is a bit special. For example it is always running and cannot be terminated. Because of its
140 * special semantics we synthesize it here, instead of relying on the unit file on disk. */
141
142 u->default_dependencies = false;
143
144 if (!u->description)
145 u->description = strdup("System Slice");
146 if (!u->documentation)
147 u->documentation = strv_new("man:systemd.special(7)");
148
149 return 1;
150 }
151
152 static int slice_load(Unit *u) {
153 Slice *s = ASSERT_PTR(SLICE(u));
154 int r;
155
156 assert(u->load_state == UNIT_STUB);
157
158 r = slice_load_root_slice(u);
159 if (r < 0)
160 return r;
161 r = slice_load_system_slice(u);
162 if (r < 0)
163 return r;
164
165 r = unit_load_fragment_and_dropin(u, false);
166 if (r < 0)
167 return r;
168
169 if (u->load_state != UNIT_LOADED)
170 return 0;
171
172 /* This is a new unit? Then let's add in some extras */
173 r = unit_patch_contexts(u);
174 if (r < 0)
175 return r;
176
177 r = slice_add_parent_slice(s);
178 if (r < 0)
179 return r;
180
181 r = slice_add_default_dependencies(s);
182 if (r < 0)
183 return r;
184
185 if (!u->description) {
186 _cleanup_free_ char *tmp = NULL;
187
188 r = unit_name_to_path(u->id, &tmp);
189 if (r >= 0) /* Failure is ignored… */
190 u->description = strjoin("Slice ", tmp);
191 }
192
193 return slice_verify(s);
194 }
195
196 static int slice_coldplug(Unit *u) {
197 Slice *s = ASSERT_PTR(SLICE(u));
198
199 assert(s->state == SLICE_DEAD);
200
201 if (s->deserialized_state != s->state)
202 slice_set_state(s, s->deserialized_state);
203
204 return 0;
205 }
206
207 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
208 Slice *s = ASSERT_PTR(SLICE(u));
209
210 assert(s);
211 assert(f);
212 assert(prefix);
213
214 fprintf(f,
215 "%sSlice State: %s\n",
216 prefix, slice_state_to_string(s->state));
217
218 cgroup_context_dump(u, f, prefix);
219 }
220
221 static int slice_start(Unit *u) {
222 Slice *s = ASSERT_PTR(SLICE(u));
223 int r;
224
225 assert(s->state == SLICE_DEAD);
226
227 r = unit_acquire_invocation_id(u);
228 if (r < 0)
229 return r;
230
231 (void) unit_realize_cgroup(u);
232 (void) unit_reset_accounting(u);
233
234 slice_set_state(s, SLICE_ACTIVE);
235 return 1;
236 }
237
238 static int slice_stop(Unit *u) {
239 Slice *s = ASSERT_PTR(SLICE(u));
240
241 assert(s->state == SLICE_ACTIVE);
242
243 /* We do not need to destroy the cgroup explicitly,
244 * unit_notify() will do that for us anyway. */
245
246 slice_set_state(s, SLICE_DEAD);
247 return 1;
248 }
249
250 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
251 Slice *s = ASSERT_PTR(SLICE(u));
252
253 assert(f);
254 assert(fds);
255
256 (void) serialize_item(f, "state", slice_state_to_string(s->state));
257
258 return 0;
259 }
260
261 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
262 Slice *s = ASSERT_PTR(SLICE(u));
263
264 assert(key);
265 assert(value);
266 assert(fds);
267
268 if (streq(key, "state")) {
269 SliceState state;
270
271 state = slice_state_from_string(value);
272 if (state < 0)
273 log_unit_debug(u, "Failed to parse state: %s", value);
274 else
275 s->deserialized_state = state;
276
277 } else
278 log_unit_debug(u, "Unknown serialization key: %s", key);
279
280 return 0;
281 }
282
283 static UnitActiveState slice_active_state(Unit *u) {
284 Slice *s = ASSERT_PTR(SLICE(u));
285
286 return state_translation_table[s->state];
287 }
288
289 static const char *slice_sub_state_to_string(Unit *u) {
290 Slice *s = ASSERT_PTR(SLICE(u));
291
292 return slice_state_to_string(s->state);
293 }
294
295 static int slice_make_perpetual(Manager *m, const char *name, Unit **ret) {
296 Unit *u;
297 int r;
298
299 assert(m);
300 assert(name);
301
302 u = manager_get_unit(m, name);
303 if (!u) {
304 r = unit_new_for_name(m, sizeof(Slice), name, &u);
305 if (r < 0)
306 return log_error_errno(r, "Failed to allocate the special %s unit: %m", name);
307 }
308
309 u->perpetual = true;
310 SLICE(u)->deserialized_state = SLICE_ACTIVE;
311
312 unit_add_to_load_queue(u);
313 unit_add_to_dbus_queue(u);
314
315 if (ret)
316 *ret = u;
317
318 return 0;
319 }
320
321 static void slice_enumerate_perpetual(Manager *m) {
322 Unit *u;
323 int r;
324
325 assert(m);
326
327 r = slice_make_perpetual(m, SPECIAL_ROOT_SLICE, &u);
328 if (r >= 0 && manager_owns_host_root_cgroup(m)) {
329 Slice *s = SLICE(u);
330
331 /* If we are managing the root cgroup then this means our root slice covers the whole system, which
332 * means the kernel will track CPU/tasks/memory for us anyway, and it is all available in /proc. Let's
333 * hence turn accounting on here, so that our APIs to query this data are available. */
334
335 s->cgroup_context.cpu_accounting = true;
336 s->cgroup_context.tasks_accounting = true;
337 s->cgroup_context.memory_accounting = true;
338 }
339
340 if (MANAGER_IS_SYSTEM(m))
341 (void) slice_make_perpetual(m, SPECIAL_SYSTEM_SLICE, NULL);
342 }
343
344 static bool slice_can_freeze(Unit *s) {
345 Unit *member;
346
347 assert(s);
348
349 UNIT_FOREACH_DEPENDENCY(member, s, UNIT_ATOM_SLICE_OF)
350 if (!unit_can_freeze(member))
351 return false;
352 return true;
353 }
354
355 static int slice_freezer_action(Unit *s, FreezerAction action) {
356 FreezerAction child_action;
357 Unit *member;
358 int r;
359
360 assert(s);
361 assert(IN_SET(action, FREEZER_FREEZE, FREEZER_PARENT_FREEZE,
362 FREEZER_THAW, FREEZER_PARENT_THAW));
363
364 if (action == FREEZER_FREEZE && !slice_can_freeze(s)) {
365 /* We're intentionally only checking for FREEZER_FREEZE here and ignoring the
366 * _BY_PARENT variant. If we're being frozen by parent, that means someone has
367 * already checked if we can be frozen further up the call stack. No point to
368 * redo that work */
369 log_unit_warning(s, "Requested freezer operation is not supported by all children of the slice");
370 return 0;
371 }
372
373 if (action == FREEZER_FREEZE)
374 child_action = FREEZER_PARENT_FREEZE;
375 else if (action == FREEZER_THAW)
376 child_action = FREEZER_PARENT_THAW;
377 else
378 child_action = action;
379
380 UNIT_FOREACH_DEPENDENCY(member, s, UNIT_ATOM_SLICE_OF) {
381 if (UNIT_VTABLE(member)->freezer_action)
382 r = UNIT_VTABLE(member)->freezer_action(member, child_action);
383 else
384 /* Only thawing will reach here, since freezing checks for a method in can_freeze */
385 r = 0;
386 if (r < 0)
387 return r;
388 }
389
390 return unit_cgroup_freezer_action(s, action);
391 }
392
393 const UnitVTable slice_vtable = {
394 .object_size = sizeof(Slice),
395 .cgroup_context_offset = offsetof(Slice, cgroup_context),
396 .cgroup_runtime_offset = offsetof(Slice, cgroup_runtime),
397
398 .sections =
399 "Unit\0"
400 "Slice\0"
401 "Install\0",
402 .private_section = "Slice",
403
404 .can_transient = true,
405 .can_set_managed_oom = true,
406
407 .init = slice_init,
408 .load = slice_load,
409
410 .coldplug = slice_coldplug,
411
412 .dump = slice_dump,
413
414 .start = slice_start,
415 .stop = slice_stop,
416
417 .freezer_action = slice_freezer_action,
418 .can_freeze = slice_can_freeze,
419
420 .serialize = slice_serialize,
421 .deserialize_item = slice_deserialize_item,
422
423 .active_state = slice_active_state,
424 .sub_state_to_string = slice_sub_state_to_string,
425
426 .bus_set_property = bus_slice_set_property,
427 .bus_commit_properties = bus_slice_commit_properties,
428
429 .enumerate_perpetual = slice_enumerate_perpetual,
430
431 .status_message_formats = {
432 .finished_start_job = {
433 [JOB_DONE] = "Created slice %s.",
434 },
435 .finished_stop_job = {
436 [JOB_DONE] = "Removed slice %s.",
437 },
438 },
439 };