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