]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/slice.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / core / slice.c
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>
23
24 #include "alloc-util.h"
25 #include "dbus-slice.h"
26 #include "log.h"
27 #include "special.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "unit-name.h"
31 #include "unit.h"
32 #include "slice.h"
33
34 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
35 [SLICE_DEAD] = UNIT_INACTIVE,
36 [SLICE_ACTIVE] = UNIT_ACTIVE
37 };
38
39 static 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
55 static int slice_add_parent_slice(Slice *s) {
56 char *a, *dash;
57 Unit *parent;
58 int r;
59
60 assert(s);
61
62 if (UNIT_ISSET(UNIT(s)->slice))
63 return 0;
64
65 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
66 return 0;
67
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;
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
83 static int slice_add_default_dependencies(Slice *s) {
84 int r;
85
86 assert(s);
87
88 /* Make sure slices are unloaded on shutdown */
89 r = unit_add_two_dependencies_by_name(
90 UNIT(s),
91 UNIT_BEFORE, UNIT_CONFLICTS,
92 SPECIAL_SHUTDOWN_TARGET, NULL, true);
93 if (r < 0)
94 return r;
95
96 return 0;
97 }
98
99
100 static int slice_verify(Slice *s) {
101 _cleanup_free_ char *parent = NULL;
102 int r;
103
104 assert(s);
105
106 if (UNIT(s)->load_state != UNIT_LOADED)
107 return 0;
108
109 if (!slice_name_is_valid(UNIT(s)->id)) {
110 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
111 return -EINVAL;
112 }
113
114 r = slice_build_parent_slice(UNIT(s)->id, &parent);
115 if (r < 0)
116 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
117
118 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
119 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
120 return -EINVAL;
121 }
122
123 return 0;
124 }
125
126 static int slice_load(Unit *u) {
127 Slice *s = SLICE(u);
128 int r;
129
130 assert(s);
131
132 r = unit_load_fragment_and_dropin_optional(u);
133 if (r < 0)
134 return r;
135
136 /* This is a new unit? Then let's add in some extras */
137 if (u->load_state == UNIT_LOADED) {
138
139 r = unit_patch_contexts(u);
140 if (r < 0)
141 return r;
142
143 r = slice_add_parent_slice(s);
144 if (r < 0)
145 return r;
146
147 if (u->default_dependencies) {
148 r = slice_add_default_dependencies(s);
149 if (r < 0)
150 return r;
151 }
152 }
153
154 return slice_verify(s);
155 }
156
157 static int slice_coldplug(Unit *u) {
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
169 static 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));
178
179 cgroup_context_dump(&t->cgroup_context, f, prefix);
180 }
181
182 static int slice_start(Unit *u) {
183 Slice *t = SLICE(u);
184
185 assert(t);
186 assert(t->state == SLICE_DEAD);
187
188 (void) unit_realize_cgroup(u);
189 (void) unit_reset_cpu_usage(u);
190
191 slice_set_state(t, SLICE_ACTIVE);
192 return 1;
193 }
194
195 static int slice_stop(Unit *u) {
196 Slice *t = SLICE(u);
197
198 assert(t);
199 assert(t->state == SLICE_ACTIVE);
200
201 /* We do not need to destroy the cgroup explicitly,
202 * unit_notify() will do that for us anyway. */
203
204 slice_set_state(t, SLICE_DEAD);
205 return 1;
206 }
207
208 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
209 return unit_kill_common(u, who, signo, -1, -1, error);
210 }
211
212 static 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
223 static 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
258 static int slice_enumerate(Manager *m) {
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));
267 if (!u)
268 return log_oom();
269
270 r = unit_add_name(u, SPECIAL_ROOT_SLICE);
271 if (r < 0) {
272 unit_free(u);
273 return log_error_errno(r, "Failed to add -.slice name");
274 }
275 }
276
277 u->default_dependencies = false;
278 u->no_gc = true;
279 u->ignore_on_isolate = true;
280 u->refuse_manual_start = true;
281 u->refuse_manual_stop = true;
282 SLICE(u)->deserialized_state = SLICE_ACTIVE;
283
284 if (!u->description)
285 u->description = strdup("Root Slice");
286 if (!u->documentation)
287 (void) strv_extend(&u->documentation, "man:systemd.special(7)");
288
289 unit_add_to_load_queue(u);
290 unit_add_to_dbus_queue(u);
291
292 return 0;
293 }
294
295 const UnitVTable slice_vtable = {
296 .object_size = sizeof(Slice),
297 .cgroup_context_offset = offsetof(Slice, cgroup_context),
298
299 .sections =
300 "Unit\0"
301 "Slice\0"
302 "Install\0",
303 .private_section = "Slice",
304
305 .no_alias = true,
306 .no_instances = true,
307
308 .load = slice_load,
309
310 .coldplug = slice_coldplug,
311
312 .dump = slice_dump,
313
314 .start = slice_start,
315 .stop = slice_stop,
316
317 .kill = slice_kill,
318
319 .serialize = slice_serialize,
320 .deserialize_item = slice_deserialize_item,
321
322 .active_state = slice_active_state,
323 .sub_state_to_string = slice_sub_state_to_string,
324
325 .bus_vtable = bus_slice_vtable,
326 .bus_set_property = bus_slice_set_property,
327 .bus_commit_properties = bus_slice_commit_properties,
328
329 .enumerate = slice_enumerate,
330
331 .status_message_formats = {
332 .finished_start_job = {
333 [JOB_DONE] = "Created slice %s.",
334 },
335 .finished_stop_job = {
336 [JOB_DONE] = "Removed slice %s.",
337 },
338 },
339 };