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