]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/slice.c
core: enable transient unit support for slice units
[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 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);
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(Unit *u) {
129 Slice *s = SLICE(u);
130 int r;
131
132 assert(s);
133
134 r = unit_load_fragment_and_dropin_optional(u);
135 if (r < 0)
136 return r;
137
138 /* This is a new unit? Then let's add in some extras */
139 if (u->load_state == UNIT_LOADED) {
140
141 r = unit_patch_contexts(u);
142 if (r < 0)
143 return r;
144
145 r = slice_add_parent_slice(s);
146 if (r < 0)
147 return r;
148
149 r = slice_add_default_dependencies(s);
150 if (r < 0)
151 return r;
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 void 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 log_oom();
269 return;
270 }
271
272 r = unit_add_name(u, SPECIAL_ROOT_SLICE);
273 if (r < 0) {
274 unit_free(u);
275 log_error_errno(r, "Failed to add -.slice name");
276 return;
277 }
278 }
279
280 u->default_dependencies = false;
281 u->no_gc = true;
282 u->ignore_on_isolate = true;
283 u->refuse_manual_start = true;
284 u->refuse_manual_stop = true;
285 SLICE(u)->deserialized_state = SLICE_ACTIVE;
286
287 if (!u->description)
288 u->description = strdup("Root Slice");
289 if (!u->documentation)
290 (void) strv_extend(&u->documentation, "man:systemd.special(7)");
291
292 unit_add_to_load_queue(u);
293 unit_add_to_dbus_queue(u);
294 }
295
296 const UnitVTable slice_vtable = {
297 .object_size = sizeof(Slice),
298 .cgroup_context_offset = offsetof(Slice, cgroup_context),
299
300 .sections =
301 "Unit\0"
302 "Slice\0"
303 "Install\0",
304 .private_section = "Slice",
305
306 .no_alias = true,
307 .no_instances = true,
308 .can_transient = true,
309
310 .load = slice_load,
311
312 .coldplug = slice_coldplug,
313
314 .dump = slice_dump,
315
316 .start = slice_start,
317 .stop = slice_stop,
318
319 .kill = slice_kill,
320
321 .serialize = slice_serialize,
322 .deserialize_item = slice_deserialize_item,
323
324 .active_state = slice_active_state,
325 .sub_state_to_string = slice_sub_state_to_string,
326
327 .bus_vtable = bus_slice_vtable,
328 .bus_set_property = bus_slice_set_property,
329 .bus_commit_properties = bus_slice_commit_properties,
330
331 .enumerate = slice_enumerate,
332
333 .status_message_formats = {
334 .finished_start_job = {
335 [JOB_DONE] = "Created slice %s.",
336 },
337 .finished_stop_job = {
338 [JOB_DONE] = "Removed slice %s.",
339 },
340 },
341 };