1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 #include <sys/epoll.h>
32 #include "load-fragment.h"
33 #include "load-dropin.h"
34 #include "unit-name.h"
35 #include "dbus-swap.h"
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
43 static void swap_init(Unit *u) {
47 assert(s->meta.load_state == UNIT_STUB);
49 s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
52 static void swap_done(Unit *u) {
58 free(s->parameters_etc_fstab.what);
59 free(s->parameters_proc_swaps.what);
60 free(s->parameters_fragment.what);
63 int swap_add_one_mount_link(Swap *s, Mount *m) {
69 if (s->meta.load_state != UNIT_LOADED ||
70 m->meta.load_state != UNIT_LOADED)
73 if (!path_startswith(s->what, m->where))
76 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(s), true)) < 0)
79 if ((r = unit_add_dependency(UNIT(s), UNIT_REQUIRES, UNIT(m), true)) < 0)
85 static int swap_add_mount_links(Swap *s) {
91 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
92 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
98 static int swap_add_target_links(Swap *s) {
105 if (s->from_fragment)
106 p = &s->parameters_fragment;
107 else if (s->from_etc_fstab)
108 p = &s->parameters_etc_fstab;
112 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, &tu)) < 0)
115 if (!p->noauto && p->handle)
116 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
119 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
122 static int swap_verify(Swap *s) {
126 if (UNIT(s)->meta.load_state != UNIT_LOADED)
129 if (!(e = unit_name_from_path(s->what, ".swap")))
132 b = unit_has_name(UNIT(s), e);
136 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->meta.id);
143 static int swap_load(Unit *u) {
148 assert(u->meta.load_state == UNIT_STUB);
150 /* Load a .swap file */
151 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
154 if (u->meta.load_state == UNIT_LOADED) {
156 if (s->meta.fragment_path)
157 s->from_fragment = true;
160 if (s->parameters_fragment.what)
161 s->what = strdup(s->parameters_fragment.what);
162 else if (s->parameters_etc_fstab.what)
163 s->what = strdup(s->parameters_etc_fstab.what);
164 else if (s->parameters_proc_swaps.what)
165 s->what = strdup(s->parameters_proc_swaps.what);
167 s->what = unit_name_to_path(u->meta.id);
173 path_kill_slashes(s->what);
175 if (!s->meta.description)
176 if ((r = unit_set_description(u, s->what)) < 0)
179 if ((r = unit_add_node_link(u, s->what,
180 (u->meta.manager->running_as == MANAGER_INIT ||
181 u->meta.manager->running_as == MANAGER_SYSTEM))) < 0)
184 if ((r = swap_add_mount_links(s)) < 0)
187 if ((r = swap_add_target_links(s)) < 0)
191 return swap_verify(s);
194 static int swap_find(Manager *m, const char *what, Unit **_u) {
202 /* /proc/swaps and /etc/fstab might refer to this device by
203 * different names (e.g. one by uuid, the other by the kernel
204 * name), we hence need to look for all aliases we are aware
205 * of for this device */
207 if (!(e = unit_name_from_path(what, ".device")))
210 u = manager_get_unit(m, e);
217 SET_FOREACH(d, u->meta.names, i) {
220 if (!(e = unit_name_change_suffix(d, ".swap")))
223 k = manager_get_unit(m, e);
243 bool from_proc_swaps) {
245 char *e = NULL, *w = NULL;
253 if (!(e = unit_name_from_path(what, ".swap")))
256 if (!(u = manager_get_unit(m, e)))
257 if ((r = swap_find(m, what, &u)) < 0)
263 if (!(u = unit_new(m))) {
270 if ((r = unit_add_name(u, e)) < 0)
273 if (!(w = strdup(what))) {
278 if (from_proc_swaps) {
279 p = &SWAP(u)->parameters_proc_swaps;
280 SWAP(u)->from_proc_swaps = true;
282 p = &SWAP(u)->parameters_etc_fstab;
283 SWAP(u)->from_etc_fstab = true;
289 p->priority = priority;
294 unit_add_to_load_queue(u);
296 unit_add_to_dbus_queue(u);
312 static void swap_set_state(Swap *s, SwapState state) {
316 old_state = s->state;
319 if (state != old_state)
320 log_debug("%s changed %s -> %s",
322 swap_state_to_string(old_state),
323 swap_state_to_string(state));
325 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
328 static int swap_coldplug(Unit *u) {
330 SwapState new_state = SWAP_DEAD;
333 assert(s->state == SWAP_DEAD);
335 if (s->deserialized_state != s->state)
336 new_state = s->deserialized_state;
337 else if (s->from_proc_swaps)
338 new_state = SWAP_ACTIVE;
340 if (new_state != s->state)
341 swap_set_state(s, new_state);
346 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
353 if (s->from_proc_swaps)
354 p = &s->parameters_proc_swaps;
355 else if (s->from_fragment)
356 p = &s->parameters_fragment;
358 p = &s->parameters_etc_fstab;
366 "%sFrom /etc/fstab: %s\n"
367 "%sFrom /proc/swaps: %s\n"
368 "%sFrom fragment: %s\n",
369 prefix, swap_state_to_string(s->state),
372 prefix, yes_no(p->noauto),
373 prefix, yes_no(p->handle),
374 prefix, yes_no(s->from_etc_fstab),
375 prefix, yes_no(s->from_proc_swaps),
376 prefix, yes_no(s->from_fragment));
379 static void swap_enter_dead(Swap *s, bool success) {
382 swap_set_state(s, success ? SWAP_MAINTAINANCE : SWAP_DEAD);
385 static int swap_start(Unit *u) {
391 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTAINANCE);
393 if (s->from_fragment)
394 priority = s->parameters_fragment.priority;
395 else if (s->from_etc_fstab)
396 priority = s->parameters_etc_fstab.priority;
398 r = swapon(s->what, (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
400 if (r < 0 && errno != EBUSY) {
402 swap_enter_dead(s, false);
406 swap_set_state(s, SWAP_ACTIVE);
410 static int swap_stop(Unit *u) {
416 assert(s->state == SWAP_ACTIVE);
418 r = swapoff(s->what);
419 swap_enter_dead(s, r >= 0 || errno == EINVAL);
424 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
431 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
436 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
442 if (streq(key, "state")) {
445 if ((state = swap_state_from_string(value)) < 0)
446 log_debug("Failed to parse state value %s", value);
448 s->deserialized_state = state;
450 log_debug("Unknown serialization key '%s'", key);
455 static UnitActiveState swap_active_state(Unit *u) {
458 return state_translation_table[SWAP(u)->state];
461 static const char *swap_sub_state_to_string(Unit *u) {
464 return swap_state_to_string(SWAP(u)->state);
467 static bool swap_check_gc(Unit *u) {
472 return s->from_etc_fstab || s->from_proc_swaps;
475 static int swap_load_proc_swaps(Manager *m) {
476 rewind(m->proc_swaps);
478 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
481 char *dev = NULL, *d;
484 if ((k = fscanf(m->proc_swaps,
485 "%ms " /* device/file */
486 "%*s " /* type of swap */
487 "%*s " /* swap size */
489 "%i\n", /* priority */
490 &dev, &prio)) != 2) {
505 k = swap_add_one(m, d, prio, false, false, true);
515 static void swap_shutdown(Manager *m) {
519 fclose(m->proc_swaps);
520 m->proc_swaps = NULL;
524 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
525 [SWAP_DEAD] = "dead",
526 [SWAP_ACTIVE] = "active",
527 [SWAP_MAINTAINANCE] = "maintainance"
530 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
532 static int swap_enumerate(Manager *m) {
537 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
540 if ((r = swap_load_proc_swaps(m)) < 0)
546 const UnitVTable swap_vtable = {
549 .no_instances = true,
556 .coldplug = swap_coldplug,
563 .serialize = swap_serialize,
564 .deserialize_item = swap_deserialize_item,
566 .active_state = swap_active_state,
567 .sub_state_to_string = swap_sub_state_to_string,
569 .check_gc = swap_check_gc,
571 .bus_message_handler = bus_swap_message_handler,
573 .enumerate = swap_enumerate,
574 .shutdown = swap_shutdown