]> git.ipfire.org Git - thirdparty/systemd.git/blob - swap.c
units: rework automatic dependency logic between automounts, mounts, sockets, swaps
[thirdparty/systemd.git] / swap.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/epoll.h>
27 #include <sys/stat.h>
28 #include <sys/swap.h>
29
30 #include "unit.h"
31 #include "swap.h"
32 #include "load-fragment.h"
33 #include "load-dropin.h"
34 #include "unit-name.h"
35 #include "dbus-swap.h"
36
37 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
38 [SWAP_DEAD] = UNIT_INACTIVE,
39 [SWAP_ACTIVE] = UNIT_ACTIVE,
40 [SWAP_MAINTAINANCE] = UNIT_INACTIVE
41 };
42
43 static void swap_done(Unit *u) {
44 Swap *s = SWAP(u);
45
46 assert(s);
47
48 free(s->what);
49 }
50
51 static int swap_verify(Swap *s) {
52 bool b;
53 char *e;
54
55 if (UNIT(s)->meta.load_state != UNIT_LOADED)
56 return 0;
57
58 if (!(e = unit_name_from_path(s->what, ".swap")))
59 return -ENOMEM;
60
61 b = unit_has_name(UNIT(s), e);
62 free(e);
63
64 if (!b) {
65 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->meta.id);
66 return -EINVAL;
67 }
68 return 0;
69 }
70
71 int swap_add_one_mount_link(Swap *s, Mount *m) {
72 int r;
73
74 assert(s);
75 assert(m);
76
77 if (s->meta.load_state != UNIT_LOADED ||
78 m->meta.load_state != UNIT_LOADED)
79 return 0;
80
81 if (!path_startswith(s->what, m->where))
82 return 0;
83
84 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(s), true)) < 0)
85 return r;
86
87 if ((r = unit_add_dependency(UNIT(s), UNIT_REQUIRES, UNIT(m), true)) < 0)
88 return r;
89
90 return 0;
91 }
92
93 static int swap_add_mount_links(Swap *s) {
94 Meta *other;
95 int r;
96
97 assert(s);
98
99 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
100 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
101 return r;
102
103 return 0;
104 }
105
106 static int swap_add_target_links(Swap *s) {
107 Manager *m = s->meta.manager;
108 Unit *tu;
109 int r;
110
111 if ((r = manager_load_unit(m, SPECIAL_SWAP_TARGET, NULL, &tu)) < 0)
112 return r;
113
114 if (!s->no_auto)
115 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
116 return r;
117
118 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
119 }
120
121 static int swap_load(Unit *u) {
122 int r;
123 Swap *s = SWAP(u);
124
125 assert(s);
126 assert(u->meta.load_state == UNIT_STUB);
127
128 /* Load a .swap file */
129 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
130 return r;
131
132 if (u->meta.load_state == UNIT_LOADED) {
133 if (!s->what)
134 if (!(s->what = unit_name_to_path(u->meta.id)))
135 return -ENOMEM;
136
137 path_kill_slashes(s->what);
138
139 if ((r = unit_add_node_link(u, s->what,
140 (u->meta.manager->running_as == MANAGER_INIT ||
141 u->meta.manager->running_as == MANAGER_SYSTEM))) < 0)
142 return r;
143
144 if ((r = swap_add_mount_links(s)) < 0)
145 return r;
146
147 if ((r = swap_add_target_links(s)) < 0)
148 return r;
149 }
150
151 return swap_verify(s);
152 }
153
154 int swap_add_one(Manager *m, const char *what, bool no_auto, int prio, bool from_proc_swaps) {
155 Unit *u;
156 char *e;
157 bool delete;
158 int r;
159
160 if (!(e = unit_name_from_path(what, ".swap")))
161 return -ENOMEM;
162
163 if (!(u = manager_get_unit(m, e))) {
164 delete = true;
165
166 if (!(u = unit_new(m))) {
167 free(e);
168 return -ENOMEM;
169 }
170
171 r = unit_add_name(u, e);
172 free(e);
173
174 if (r < 0)
175 goto fail;
176
177 if (!(SWAP(u)->what = strdup(what))) {
178 r = -ENOMEM;
179 goto fail;
180 }
181
182 if ((r = unit_set_description(u, what)) < 0)
183 goto fail;
184
185 unit_add_to_load_queue(u);
186
187 SWAP(u)->from_proc_swaps_only = from_proc_swaps;
188 } else {
189 if (SWAP(u)->from_proc_swaps_only && !from_proc_swaps)
190 SWAP(u)->from_proc_swaps_only = false;
191
192 delete = false;
193 free(e);
194 }
195
196 if (!from_proc_swaps)
197 SWAP(u)->no_auto = no_auto;
198 else
199 SWAP(u)->found_in_proc_swaps = true;
200
201 SWAP(u)->priority = prio;
202
203 return 0;
204
205 fail:
206 if (delete)
207 unit_free(u);
208
209 return 0;
210 }
211
212 static void swap_set_state(Swap *s, SwapState state) {
213 SwapState old_state;
214 assert(s);
215
216 old_state = s->state;
217 s->state = state;
218
219 if (state != old_state)
220 log_debug("%s changed %s -> %s",
221 UNIT(s)->meta.id,
222 swap_state_to_string(old_state),
223 swap_state_to_string(state));
224
225 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
226 }
227
228 static int swap_coldplug(Unit *u) {
229 Swap *s = SWAP(u);
230 SwapState new_state = SWAP_DEAD;
231
232 assert(s);
233 assert(s->state == SWAP_DEAD);
234
235 if (s->deserialized_state != s->state)
236 new_state = s->deserialized_state;
237 else if (s->found_in_proc_swaps)
238 new_state = SWAP_ACTIVE;
239
240 if (new_state != s->state)
241 swap_set_state(s, s->deserialized_state);
242
243 return 0;
244 }
245
246 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
247 Swap *s = SWAP(u);
248
249 assert(s);
250
251 fprintf(f,
252 "%sAutomount State: %s\n"
253 "%sWhat: %s\n"
254 "%sPriority: %i\n"
255 "%sNoAuto: %s\n",
256 prefix, swap_state_to_string(s->state),
257 prefix, s->what,
258 prefix, s->priority,
259 prefix, yes_no(s->no_auto));
260 }
261
262 static void swap_enter_dead(Swap *s, bool success) {
263 assert(s);
264
265 swap_set_state(s, success ? SWAP_MAINTAINANCE : SWAP_DEAD);
266 }
267
268 static int swap_start(Unit *u) {
269 Swap *s = SWAP(u);
270 int r;
271
272 assert(s);
273
274 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTAINANCE);
275
276 r = swapon(s->what, (s->priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
277
278 if (r < 0 && errno != EBUSY) {
279 r = -errno;
280 swap_enter_dead(s, false);
281 return r;
282 }
283
284 swap_set_state(s, SWAP_ACTIVE);
285 return 0;
286 }
287
288 static int swap_stop(Unit *u) {
289 Swap *s = SWAP(u);
290 int r;
291
292 assert(s);
293
294 assert(s->state == SWAP_ACTIVE);
295
296 r = swapoff(s->what);
297 swap_enter_dead(s, r >= 0 || errno == EINVAL);
298
299 return 0;
300 }
301
302 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
303 Swap *s = SWAP(u);
304
305 assert(s);
306 assert(f);
307 assert(fds);
308
309 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
310
311 return 0;
312 }
313
314 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
315 Swap *s = SWAP(u);
316
317 assert(s);
318 assert(fds);
319
320 if (streq(key, "state")) {
321 SwapState state;
322
323 if ((state = swap_state_from_string(value)) < 0)
324 log_debug("Failed to parse state value %s", value);
325 else
326 s->deserialized_state = state;
327 } else
328 log_debug("Unknown serialization key '%s'", key);
329
330 return 0;
331 }
332
333 static UnitActiveState swap_active_state(Unit *u) {
334 assert(u);
335
336 return state_translation_table[SWAP(u)->state];
337 }
338
339 static const char *swap_sub_state_to_string(Unit *u) {
340 assert(u);
341
342 return swap_state_to_string(SWAP(u)->state);
343 }
344
345 static bool swap_check_gc(Unit *u) {
346 Swap *s = SWAP(u);
347
348 assert(s);
349
350 return !s->from_proc_swaps_only || s->found_in_proc_swaps;
351 }
352
353 static int swap_load_proc_swaps(Manager *m) {
354 Meta *meta;
355
356 rewind(m->proc_swaps);
357
358 (void) fscanf(m->proc_self_mountinfo, "%*s %*s %*s %*s %*s\n");
359
360 for (;;) {
361 char *dev = NULL, *d;
362 int prio = 0, k;
363
364 k = fscanf(m->proc_self_mountinfo,
365 "%ms " /* device/file */
366 "%*s " /* type of swap */
367 "%*s " /* swap size */
368 "%*s " /* used */
369 "%d\n", /* priority */
370 &dev, &prio);
371
372 if (k != 2) {
373 if (k == EOF)
374 k = 0;
375
376 free(dev);
377 return -EBADMSG;
378 }
379 if (!(d = cunescape(dev))) {
380 free(dev);
381 k = -ENOMEM;
382 return k;
383 }
384
385 k = swap_add_one(m, d, false, prio, true);
386 free(dev);
387 free(d);
388
389 if (k < 0)
390 return k;
391 }
392
393 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
394 Swap *s = (Swap*) meta;
395
396 if (s->state != SWAP_DEAD && s->state != SWAP_ACTIVE)
397 continue;
398
399 if ((s->state == SWAP_DEAD && !s->found_in_proc_swaps) ||
400 (s->state == SWAP_ACTIVE && s->found_in_proc_swaps))
401 continue;
402
403 swap_set_state(s, s->found_in_proc_swaps ? SWAP_ACTIVE : SWAP_DEAD);
404
405 /* Reset the flags for later calls */
406 s->found_in_proc_swaps = false;
407 }
408 }
409
410 static void swap_shutdown(Manager *m) {
411 assert(m);
412
413 if (m->proc_swaps) {
414 fclose(m->proc_swaps);
415 m->proc_swaps = NULL;
416 }
417 }
418
419 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
420 [SWAP_DEAD] = "dead",
421 [SWAP_ACTIVE] = "active",
422 [SWAP_MAINTAINANCE] = "maintainance"
423 };
424
425 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
426
427 static int swap_enumerate(Manager *m) {
428 int r;
429 assert(m);
430
431 if (!m->proc_swaps &&
432 !(m->proc_swaps = fopen("/proc/swaps", "er")))
433 return -errno;
434
435 if ((r = swap_load_proc_swaps(m)) < 0)
436 swap_shutdown(m);
437
438 return r;
439 }
440
441 const UnitVTable swap_vtable = {
442 .suffix = ".swap",
443
444 .no_alias = true,
445 .no_instances = true,
446 .no_isolate = true,
447
448 .load = swap_load,
449 .done = swap_done,
450
451 .coldplug = swap_coldplug,
452
453 .dump = swap_dump,
454
455 .start = swap_start,
456 .stop = swap_stop,
457
458 .serialize = swap_serialize,
459 .deserialize_item = swap_deserialize_item,
460
461 .active_state = swap_active_state,
462 .sub_state_to_string = swap_sub_state_to_string,
463
464 .check_gc = swap_check_gc,
465
466 .bus_message_handler = bus_swap_message_handler,
467
468 .enumerate = swap_enumerate,
469 .shutdown = swap_shutdown
470 };