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