]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/slice.c
Add SPDX license identifiers to source files under the LGPL
[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 char *a, *dash;
63 Unit *parent;
64 int r;
65
66 assert(s);
67
68 if (UNIT_ISSET(UNIT(s)->slice))
69 return 0;
70
71 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
72 return 0;
73
74 a = strdupa(UNIT(s)->id);
75 dash = strrchr(a, '-');
76 if (dash)
77 strcpy(dash, ".slice");
78 else
79 a = (char*) SPECIAL_ROOT_SLICE;
80
81 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
82 if (r < 0)
83 return r;
84
85 unit_ref_set(&UNIT(s)->slice, parent);
86 return 0;
87 }
88
89 static int slice_add_default_dependencies(Slice *s) {
90 int r;
91
92 assert(s);
93
94 if (!UNIT(s)->default_dependencies)
95 return 0;
96
97 /* Make sure slices are unloaded on shutdown */
98 r = unit_add_two_dependencies_by_name(
99 UNIT(s),
100 UNIT_BEFORE, UNIT_CONFLICTS,
101 SPECIAL_SHUTDOWN_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT);
102 if (r < 0)
103 return r;
104
105 return 0;
106 }
107
108 static int slice_verify(Slice *s) {
109 _cleanup_free_ char *parent = NULL;
110 int r;
111
112 assert(s);
113
114 if (UNIT(s)->load_state != UNIT_LOADED)
115 return 0;
116
117 if (!slice_name_is_valid(UNIT(s)->id)) {
118 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
119 return -EINVAL;
120 }
121
122 r = slice_build_parent_slice(UNIT(s)->id, &parent);
123 if (r < 0)
124 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
125
126 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
127 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
128 return -EINVAL;
129 }
130
131 return 0;
132 }
133
134 static int slice_load_root_slice(Unit *u) {
135 assert(u);
136
137 if (!unit_has_name(u, SPECIAL_ROOT_SLICE))
138 return 0;
139
140 u->perpetual = true;
141
142 /* The root 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 u->ignore_on_isolate = true;
147
148 if (!u->description)
149 u->description = strdup("Root Slice");
150 if (!u->documentation)
151 u->documentation = strv_new("man:systemd.special(7)", NULL);
152
153 return 1;
154 }
155
156 static int slice_load(Unit *u) {
157 Slice *s = SLICE(u);
158 int r;
159
160 assert(s);
161 assert(u->load_state == UNIT_STUB);
162
163 r = slice_load_root_slice(u);
164 if (r < 0)
165 return r;
166 r = unit_load_fragment_and_dropin_optional(u);
167 if (r < 0)
168 return r;
169
170 /* This is a new unit? Then let's add in some extras */
171 if (u->load_state == UNIT_LOADED) {
172
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
186 return slice_verify(s);
187 }
188
189 static int slice_coldplug(Unit *u) {
190 Slice *t = SLICE(u);
191
192 assert(t);
193 assert(t->state == SLICE_DEAD);
194
195 if (t->deserialized_state != t->state)
196 slice_set_state(t, t->deserialized_state);
197
198 return 0;
199 }
200
201 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
202 Slice *t = SLICE(u);
203
204 assert(t);
205 assert(f);
206
207 fprintf(f,
208 "%sSlice State: %s\n",
209 prefix, slice_state_to_string(t->state));
210
211 cgroup_context_dump(&t->cgroup_context, f, prefix);
212 }
213
214 static int slice_start(Unit *u) {
215 Slice *t = SLICE(u);
216 int r;
217
218 assert(t);
219 assert(t->state == SLICE_DEAD);
220
221 r = unit_acquire_invocation_id(u);
222 if (r < 0)
223 return r;
224
225 (void) unit_realize_cgroup(u);
226 (void) unit_reset_cpu_accounting(u);
227 (void) unit_reset_ip_accounting(u);
228
229 slice_set_state(t, SLICE_ACTIVE);
230 return 1;
231 }
232
233 static int slice_stop(Unit *u) {
234 Slice *t = SLICE(u);
235
236 assert(t);
237 assert(t->state == SLICE_ACTIVE);
238
239 /* We do not need to destroy the cgroup explicitly,
240 * unit_notify() will do that for us anyway. */
241
242 slice_set_state(t, SLICE_DEAD);
243 return 1;
244 }
245
246 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
247 return unit_kill_common(u, who, signo, -1, -1, error);
248 }
249
250 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
251 Slice *s = SLICE(u);
252
253 assert(s);
254 assert(f);
255 assert(fds);
256
257 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
258 return 0;
259 }
260
261 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
262 Slice *s = SLICE(u);
263
264 assert(u);
265 assert(key);
266 assert(value);
267 assert(fds);
268
269 if (streq(key, "state")) {
270 SliceState state;
271
272 state = slice_state_from_string(value);
273 if (state < 0)
274 log_debug("Failed to parse state value %s", value);
275 else
276 s->deserialized_state = state;
277
278 } else
279 log_debug("Unknown serialization key '%s'", key);
280
281 return 0;
282 }
283
284 _pure_ static UnitActiveState slice_active_state(Unit *u) {
285 assert(u);
286
287 return state_translation_table[SLICE(u)->state];
288 }
289
290 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
291 assert(u);
292
293 return slice_state_to_string(SLICE(u)->state);
294 }
295
296 static void slice_enumerate(Manager *m) {
297 Unit *u;
298 int r;
299
300 assert(m);
301
302 u = manager_get_unit(m, SPECIAL_ROOT_SLICE);
303 if (!u) {
304 r = unit_new_for_name(m, sizeof(Slice), SPECIAL_ROOT_SLICE, &u);
305 if (r < 0) {
306 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_SLICE " unit: %m");
307 return;
308 }
309 }
310
311 u->perpetual = true;
312 SLICE(u)->deserialized_state = SLICE_ACTIVE;
313
314 unit_add_to_load_queue(u);
315 unit_add_to_dbus_queue(u);
316 }
317
318 const UnitVTable slice_vtable = {
319 .object_size = sizeof(Slice),
320 .cgroup_context_offset = offsetof(Slice, cgroup_context),
321
322 .sections =
323 "Unit\0"
324 "Slice\0"
325 "Install\0",
326 .private_section = "Slice",
327
328 .can_transient = true,
329
330 .init = slice_init,
331 .load = slice_load,
332
333 .coldplug = slice_coldplug,
334
335 .dump = slice_dump,
336
337 .start = slice_start,
338 .stop = slice_stop,
339
340 .kill = slice_kill,
341
342 .serialize = slice_serialize,
343 .deserialize_item = slice_deserialize_item,
344
345 .active_state = slice_active_state,
346 .sub_state_to_string = slice_sub_state_to_string,
347
348 .bus_vtable = bus_slice_vtable,
349 .bus_set_property = bus_slice_set_property,
350 .bus_commit_properties = bus_slice_commit_properties,
351
352 .enumerate = slice_enumerate,
353
354 .status_message_formats = {
355 .finished_start_job = {
356 [JOB_DONE] = "Created slice %s.",
357 },
358 .finished_stop_job = {
359 [JOB_DONE] = "Removed slice %s.",
360 },
361 },
362 };