]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
a016b922
LP
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>
a016b922 23
07630cea 24#include "dbus-slice.h"
a016b922 25#include "log.h"
a016b922 26#include "special.h"
07630cea
LP
27#include "string-util.h"
28#include "strv.h"
a016b922 29#include "unit-name.h"
efdb0237
LP
30#include "unit.h"
31#include "slice.h"
a016b922
LP
32
33static const UnitActiveState state_translation_table[_SLICE_STATE_MAX] = {
34 [SLICE_DEAD] = UNIT_INACTIVE,
35 [SLICE_ACTIVE] = UNIT_ACTIVE
36};
37
38static 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
4ad49000 54static int slice_add_parent_slice(Slice *s) {
a016b922 55 char *a, *dash;
a016b922 56 Unit *parent;
4ad49000 57 int r;
a016b922
LP
58
59 assert(s);
60
4ad49000 61 if (UNIT_ISSET(UNIT(s)->slice))
a016b922
LP
62 return 0;
63
4ad49000 64 if (unit_has_name(UNIT(s), SPECIAL_ROOT_SLICE))
a016b922
LP
65 return 0;
66
4ad49000
LP
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;
a016b922
LP
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
82static int slice_add_default_dependencies(Slice *s) {
83 int r;
84
85 assert(s);
86
87 /* Make sure slices are unloaded on shutdown */
6c12b52e
LP
88 r = unit_add_two_dependencies_by_name(
89 UNIT(s),
90 UNIT_BEFORE, UNIT_CONFLICTS,
91 SPECIAL_SHUTDOWN_TARGET, NULL, true);
a016b922
LP
92 if (r < 0)
93 return r;
94
95 return 0;
96}
97
93c47472 98
a016b922 99static int slice_verify(Slice *s) {
93c47472
LP
100 _cleanup_free_ char *parent = NULL;
101 int r;
102
a016b922
LP
103 assert(s);
104
105 if (UNIT(s)->load_state != UNIT_LOADED)
106 return 0;
107
93c47472 108 if (!slice_name_is_valid(UNIT(s)->id)) {
f2341e0a 109 log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id);
93c47472
LP
110 return -EINVAL;
111 }
112
113 r = slice_build_parent_slice(UNIT(s)->id, &parent);
114 if (r < 0)
f2341e0a 115 return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m");
93c47472
LP
116
117 if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) {
f2341e0a 118 log_unit_error(UNIT(s), "Located outside of parent slice. Refusing.");
93c47472 119 return -EINVAL;
a016b922
LP
120 }
121
122 return 0;
123}
124
125static int slice_load(Unit *u) {
126 Slice *s = SLICE(u);
127 int r;
128
129 assert(s);
130
4ad49000 131 r = unit_load_fragment_and_dropin_optional(u);
a016b922
LP
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
598459ce
LP
138 r = unit_patch_contexts(u);
139 if (r < 0)
140 return r;
141
4ad49000 142 r = slice_add_parent_slice(s);
a016b922
LP
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 }
a016b922
LP
151 }
152
153 return slice_verify(s);
154}
155
be847e82 156static int slice_coldplug(Unit *u) {
a016b922
LP
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
168static 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));
4ad49000
LP
177
178 cgroup_context_dump(&t->cgroup_context, f, prefix);
a016b922
LP
179}
180
181static int slice_start(Unit *u) {
182 Slice *t = SLICE(u);
a016b922
LP
183
184 assert(t);
185 assert(t->state == SLICE_DEAD);
186
5ad096b3
LP
187 (void) unit_realize_cgroup(u);
188 (void) unit_reset_cpu_usage(u);
a016b922
LP
189
190 slice_set_state(t, SLICE_ACTIVE);
82a2b6bb 191 return 1;
a016b922
LP
192}
193
194static int slice_stop(Unit *u) {
195 Slice *t = SLICE(u);
196
197 assert(t);
198 assert(t->state == SLICE_ACTIVE);
199
4ad49000
LP
200 /* We do not need to destroy the cgroup explicitly,
201 * unit_notify() will do that for us anyway. */
a016b922
LP
202
203 slice_set_state(t, SLICE_DEAD);
82a2b6bb 204 return 1;
a016b922
LP
205}
206
718db961 207static int slice_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
a016b922
LP
208 return unit_kill_common(u, who, signo, -1, -1, error);
209}
210
211static 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
222static 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
efdb0237
LP
257static 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;
41780022
LP
278 u->ignore_on_isolate = true;
279 u->refuse_manual_start = true;
280 u->refuse_manual_stop = true;
efdb0237
LP
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
a016b922
LP
294const UnitVTable slice_vtable = {
295 .object_size = sizeof(Slice),
718db961
LP
296 .cgroup_context_offset = offsetof(Slice, cgroup_context),
297
a016b922
LP
298 .sections =
299 "Unit\0"
300 "Slice\0"
301 "Install\0",
4ad49000 302 .private_section = "Slice",
4ad49000 303
a016b922
LP
304 .no_alias = true,
305 .no_instances = true,
306
307 .load = slice_load,
4ad49000 308
a016b922
LP
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
718db961 324 .bus_vtable = bus_slice_vtable,
8e2af478
LP
325 .bus_set_property = bus_slice_set_property,
326 .bus_commit_properties = bus_slice_commit_properties,
a016b922 327
efdb0237
LP
328 .enumerate = slice_enumerate,
329
a016b922
LP
330 .status_message_formats = {
331 .finished_start_job = {
4ad49000 332 [JOB_DONE] = "Created slice %s.",
a016b922
LP
333 },
334 .finished_stop_job = {
4ad49000 335 [JOB_DONE] = "Removed slice %s.",
a016b922
LP
336 },
337 },
338};