]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/slice.c
Merge pull request #1501 from fbuihuu/fix-requires-mounts-for-directives
[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 "log.h"
25 #include "strv.h"
26 #include "special.h"
27 #include "unit-name.h"
28 #include "unit.h"
29 #include "slice.h"
30 #include "dbus-slice.h"
31
32 static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
33 [SLICE_DEAD] = UNIT_INACTIVE,
34 [SLICE_ACTIVE] = UNIT_ACTIVE
35 };
36
37 static void slice_set_state(Slice *t, SliceState state) {
38 SliceState old_state;
39 assert(t);
40
41 old_state = t->state;
42 t->state = state;
43
44 if (state != old_state)
45 log_debug("%s changed %s -> %s",
46 UNIT(t)->id,
47 slice_state_to_string(old_state),
48 slice_state_to_string(state));
49
50 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
51 }
52
53 static int slice_add_parent_slice(Slice *s) {
54 char *a, *dash;
55 Unit *parent;
56 int r;
57
58 assert(s);
59
60 if (UNIT_ISSET(UNIT(s)->slice))
61 return 0;
62
63 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
64 return 0;
65
66 a = strdupa(UNIT(s)->id);
67 dash = strrchr(a, '-');
68 if (dash)
69 strcpy(dash, ".slice");
70 else
71 a = (char*) SPECIAL_ROOT_SLICE;
72
73 r = manager_load_unit(UNIT(s)->manager, a, NULL, NULL, &parent);
74 if (r < 0)
75 return r;
76
77 unit_ref_set(&UNIT(s)->slice, parent);
78 return 0;
79 }
80
81 static int slice_add_default_dependencies(Slice *s) {
82 int r;
83
84 assert(s);
85
86 /* Make sure slices are unloaded on shutdown */
87 r = unit_add_two_dependencies_by_name(
88 UNIT(s),
89 UNIT_BEFORE, UNIT_CONFLICTS,
90 SPECIAL_SHUTDOWN_TARGET, NULL, true);
91 if (r < 0)
92 return r;
93
94 return 0;
95 }
96
97
98 static int slice_verify(Slice *s) {
99 _cleanup_free_ char *parent = NULL;
100 int r;
101
102 assert(s);
103
104 if (UNIT(s)->load_state != UNIT_LOADED)
105 return 0;
106
107 if (!slice_name_is_valid(UNIT(s)->id)) {
108 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
109 return -EINVAL;
110 }
111
112 r = slice_build_parent_slice(UNIT(s)->id, &parent);
113 if (r < 0)
114 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
115
116 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
117 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
118 return -EINVAL;
119 }
120
121 return 0;
122 }
123
124 static int slice_load(Unit *u) {
125 Slice *s = SLICE(u);
126 int r;
127
128 assert(s);
129
130 r = unit_load_fragment_and_dropin_optional(u);
131 if (r < 0)
132 return r;
133
134 /* This is a new unit? Then let's add in some extras */
135 if (u->load_state == UNIT_LOADED) {
136
137 r = unit_patch_contexts(u);
138 if (r < 0)
139 return r;
140
141 r = slice_add_parent_slice(s);
142 if (r < 0)
143 return r;
144
145 if (u->default_dependencies) {
146 r = slice_add_default_dependencies(s);
147 if (r < 0)
148 return r;
149 }
150 }
151
152 return slice_verify(s);
153 }
154
155 static int slice_coldplug(Unit *u) {
156 Slice *t = SLICE(u);
157
158 assert(t);
159 assert(t->state == SLICE_DEAD);
160
161 if (t->deserialized_state != t->state)
162 slice_set_state(t, t->deserialized_state);
163
164 return 0;
165 }
166
167 static void slice_dump(Unit *u, FILE *f, const char *prefix) {
168 Slice *t = SLICE(u);
169
170 assert(t);
171 assert(f);
172
173 fprintf(f,
174 "%sSlice State: %s\n",
175 prefix, slice_state_to_string(t->state));
176
177 cgroup_context_dump(&t->cgroup_context, f, prefix);
178 }
179
180 static int slice_start(Unit *u) {
181 Slice *t = SLICE(u);
182
183 assert(t);
184 assert(t->state == SLICE_DEAD);
185
186 (void) unit_realize_cgroup(u);
187 (void) unit_reset_cpu_usage(u);
188
189 slice_set_state(t, SLICE_ACTIVE);
190 return 1;
191 }
192
193 static int slice_stop(Unit *u) {
194 Slice *t = SLICE(u);
195
196 assert(t);
197 assert(t->state == SLICE_ACTIVE);
198
199 /* We do not need to destroy the cgroup explicitly,
200 * unit_notify() will do that for us anyway. */
201
202 slice_set_state(t, SLICE_DEAD);
203 return 1;
204 }
205
206 static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
207 return unit_kill_common(u, who, signo, -1, -1, error);
208 }
209
210 static int slice_serialize(Unit *u, FILE *f, FDSet *fds) {
211 Slice *s = SLICE(u);
212
213 assert(s);
214 assert(f);
215 assert(fds);
216
217 unit_serialize_item(u, f, "state", slice_state_to_string(s->state));
218 return 0;
219 }
220
221 static int slice_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
222 Slice *s = SLICE(u);
223
224 assert(u);
225 assert(key);
226 assert(value);
227 assert(fds);
228
229 if (streq(key, "state")) {
230 SliceState state;
231
232 state = slice_state_from_string(value);
233 if (state < 0)
234 log_debug("Failed to parse state value %s", value);
235 else
236 s->deserialized_state = state;
237
238 } else
239 log_debug("Unknown serialization key '%s'", key);
240
241 return 0;
242 }
243
244 _pure_ static UnitActiveState slice_active_state(Unit *u) {
245 assert(u);
246
247 return state_translation_table[SLICE(u)->state];
248 }
249
250 _pure_ static const char *slice_sub_state_to_string(Unit *u) {
251 assert(u);
252
253 return slice_state_to_string(SLICE(u)->state);
254 }
255
256 static int slice_enumerate(Manager *m) {
257 Unit *u;
258 int r;
259
260 assert(m);
261
262 u = manager_get_unit(m, SPECIAL_ROOT_SLICE);
263 if (!u) {
264 u = unit_new(m, sizeof(Slice));
265 if (!u)
266 return log_oom();
267
268 r = unit_add_name(u, SPECIAL_ROOT_SLICE);
269 if (r < 0) {
270 unit_free(u);
271 return log_error_errno(r, "Failed to add -.slice name");
272 }
273 }
274
275 u->default_dependencies = false;
276 u->no_gc = true;
277 SLICE(u)->deserialized_state = SLICE_ACTIVE;
278
279 if (!u->description)
280 u->description = strdup("Root Slice");
281 if (!u->documentation)
282 (void) strv_extend(&u->documentation, "man:systemd.special(7)");
283
284 unit_add_to_load_queue(u);
285 unit_add_to_dbus_queue(u);
286
287 return 0;
288 }
289
290 const UnitVTable slice_vtable = {
291 .object_size = sizeof(Slice),
292 .cgroup_context_offset = offsetof(Slice, cgroup_context),
293
294 .sections =
295 "Unit\0"
296 "Slice\0"
297 "Install\0",
298 .private_section = "Slice",
299
300 .no_alias = true,
301 .no_instances = true,
302
303 .load = slice_load,
304
305 .coldplug = slice_coldplug,
306
307 .dump = slice_dump,
308
309 .start = slice_start,
310 .stop = slice_stop,
311
312 .kill = slice_kill,
313
314 .serialize = slice_serialize,
315 .deserialize_item = slice_deserialize_item,
316
317 .active_state = slice_active_state,
318 .sub_state_to_string = slice_sub_state_to_string,
319
320 .bus_vtable = bus_slice_vtable,
321 .bus_set_property = bus_slice_set_property,
322 .bus_commit_properties = bus_slice_commit_properties,
323
324 .enumerate = slice_enumerate,
325
326 .status_message_formats = {
327 .finished_start_job = {
328 [JOB_DONE] = "Created slice %s.",
329 },
330 .finished_stop_job = {
331 [JOB_DONE] = "Removed slice %s.",
332 },
333 },
334 };