1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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"
38 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
39 [SWAP_DEAD] = UNIT_INACTIVE,
40 [SWAP_ACTIVE] = UNIT_ACTIVE,
41 [SWAP_MAINTENANCE] = UNIT_MAINTENANCE
44 static void swap_init(Unit *u) {
48 assert(s->meta.load_state == UNIT_STUB);
50 s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
53 static void swap_done(Unit *u) {
59 free(s->parameters_etc_fstab.what);
60 free(s->parameters_proc_swaps.what);
61 free(s->parameters_fragment.what);
64 int swap_add_one_mount_link(Swap *s, Mount *m) {
70 if (s->meta.load_state != UNIT_LOADED ||
71 m->meta.load_state != UNIT_LOADED)
74 if (is_device_path(s->what))
77 if (!path_startswith(s->what, m->where))
80 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
86 static int swap_add_mount_links(Swap *s) {
92 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
93 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
99 static int swap_add_target_links(Swap *s) {
106 if (s->from_fragment)
107 p = &s->parameters_fragment;
108 else if (s->from_etc_fstab)
109 p = &s->parameters_etc_fstab;
113 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
116 if (!p->noauto && (p->handle || s->meta.manager->swap_auto) && s->meta.manager->running_as == MANAGER_SYSTEM)
117 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
120 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
123 static int swap_add_device_links(Swap *s) {
131 if (s->from_fragment)
132 p = &s->parameters_fragment;
133 else if (s->from_etc_fstab)
134 p = &s->parameters_etc_fstab;
138 return unit_add_node_link(UNIT(s), s->what,
139 !p->noauto && p->nofail &&
140 s->meta.manager->running_as == MANAGER_SYSTEM);
143 static int swap_add_default_dependencies(Swap *s) {
148 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
150 if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
153 /* Note that by default we don't disable swap devices
154 * on shutdown. i.e. there is no umount.target
161 static int swap_verify(Swap *s) {
165 if (s->meta.load_state != UNIT_LOADED)
168 if (!(e = unit_name_from_path(s->what, ".swap")))
171 b = unit_has_name(UNIT(s), e);
175 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
182 static int swap_load(Unit *u) {
187 assert(u->meta.load_state == UNIT_STUB);
189 /* Load a .swap file */
190 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
193 if (u->meta.load_state == UNIT_LOADED) {
195 if (s->meta.fragment_path)
196 s->from_fragment = true;
199 if (s->parameters_fragment.what)
200 s->what = strdup(s->parameters_fragment.what);
201 else if (s->parameters_etc_fstab.what)
202 s->what = strdup(s->parameters_etc_fstab.what);
203 else if (s->parameters_proc_swaps.what)
204 s->what = strdup(s->parameters_proc_swaps.what);
206 s->what = unit_name_to_path(u->meta.id);
212 path_kill_slashes(s->what);
214 if (!s->meta.description)
215 if ((r = unit_set_description(u, s->what)) < 0)
219 if ((r = swap_add_device_links(s)) < 0)
222 if ((r = swap_add_mount_links(s)) < 0)
225 if ((r = swap_add_target_links(s)) < 0)
228 if (s->meta.default_dependencies)
229 if ((r = swap_add_default_dependencies(s)) < 0)
233 return swap_verify(s);
236 static int swap_find(Manager *m, const char *what, Unit **_u) {
244 /* /proc/swaps and /etc/fstab might refer to this device by
245 * different names (e.g. one by uuid, the other by the kernel
246 * name), we hence need to look for all aliases we are aware
247 * of for this device */
249 if (!(e = unit_name_from_path(what, ".device")))
252 u = manager_get_unit(m, e);
259 SET_FOREACH(d, u->meta.names, i) {
262 if (!(e = unit_name_change_suffix(d, ".swap")))
265 k = manager_get_unit(m, e);
286 bool from_proc_swaps) {
288 char *e = NULL, *w = NULL;
296 if (!(e = unit_name_from_path(what, ".swap")))
299 if (!(u = manager_get_unit(m, e)))
300 if ((r = swap_find(m, what, &u)) < 0)
306 if (!(u = unit_new(m))) {
313 if ((r = unit_add_name(u, e)) < 0)
316 if (!(w = strdup(what))) {
321 if (from_proc_swaps) {
322 p = &SWAP(u)->parameters_proc_swaps;
323 SWAP(u)->from_proc_swaps = true;
325 p = &SWAP(u)->parameters_etc_fstab;
326 SWAP(u)->from_etc_fstab = true;
332 p->priority = priority;
338 unit_add_to_load_queue(u);
340 unit_add_to_dbus_queue(u);
356 static void swap_set_state(Swap *s, SwapState state) {
360 old_state = s->state;
363 if (state != old_state)
364 log_debug("%s changed %s -> %s",
366 swap_state_to_string(old_state),
367 swap_state_to_string(state));
369 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
372 static int swap_coldplug(Unit *u) {
374 SwapState new_state = SWAP_DEAD;
377 assert(s->state == SWAP_DEAD);
379 if (s->deserialized_state != s->state)
380 new_state = s->deserialized_state;
381 else if (s->from_proc_swaps)
382 new_state = SWAP_ACTIVE;
384 if (new_state != s->state)
385 swap_set_state(s, new_state);
390 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
397 if (s->from_proc_swaps)
398 p = &s->parameters_proc_swaps;
399 else if (s->from_fragment)
400 p = &s->parameters_fragment;
402 p = &s->parameters_etc_fstab;
411 "%sFrom /etc/fstab: %s\n"
412 "%sFrom /proc/swaps: %s\n"
413 "%sFrom fragment: %s\n",
414 prefix, swap_state_to_string(s->state),
417 prefix, yes_no(p->noauto),
418 prefix, yes_no(p->nofail),
419 prefix, yes_no(p->handle),
420 prefix, yes_no(s->from_etc_fstab),
421 prefix, yes_no(s->from_proc_swaps),
422 prefix, yes_no(s->from_fragment));
425 static void swap_enter_dead(Swap *s, bool success) {
428 swap_set_state(s, success ? SWAP_DEAD : SWAP_MAINTENANCE);
431 static int swap_start(Unit *u) {
437 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTENANCE);
439 if (s->from_fragment)
440 priority = s->parameters_fragment.priority;
441 else if (s->from_etc_fstab)
442 priority = s->parameters_etc_fstab.priority;
444 r = swapon(s->what, (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
446 if (r < 0 && errno != EBUSY) {
448 swap_enter_dead(s, false);
452 swap_set_state(s, SWAP_ACTIVE);
456 static int swap_stop(Unit *u) {
462 assert(s->state == SWAP_ACTIVE);
464 r = swapoff(s->what);
465 swap_enter_dead(s, r >= 0 || errno == EINVAL);
470 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
477 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
482 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
488 if (streq(key, "state")) {
491 if ((state = swap_state_from_string(value)) < 0)
492 log_debug("Failed to parse state value %s", value);
494 s->deserialized_state = state;
496 log_debug("Unknown serialization key '%s'", key);
501 static UnitActiveState swap_active_state(Unit *u) {
504 return state_translation_table[SWAP(u)->state];
507 static const char *swap_sub_state_to_string(Unit *u) {
510 return swap_state_to_string(SWAP(u)->state);
513 static bool swap_check_gc(Unit *u) {
518 return s->from_etc_fstab || s->from_proc_swaps;
521 static int swap_load_proc_swaps(Manager *m) {
522 rewind(m->proc_swaps);
524 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
527 char *dev = NULL, *d;
530 if ((k = fscanf(m->proc_swaps,
531 "%ms " /* device/file */
532 "%*s " /* type of swap */
533 "%*s " /* swap size */
535 "%i\n", /* priority */
536 &dev, &prio)) != 2) {
551 k = swap_add_one(m, d, prio, false, false, false, true);
561 static void swap_shutdown(Manager *m) {
565 fclose(m->proc_swaps);
566 m->proc_swaps = NULL;
570 static int swap_enumerate(Manager *m) {
575 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
578 if ((r = swap_load_proc_swaps(m)) < 0)
584 static void swap_reset_maintenance(Unit *u) {
589 if (s->state == SWAP_MAINTENANCE)
590 swap_set_state(s, SWAP_DEAD);
593 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
594 [SWAP_DEAD] = "dead",
595 [SWAP_ACTIVE] = "active",
596 [SWAP_MAINTENANCE] = "maintenance"
599 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
601 const UnitVTable swap_vtable = {
604 .no_instances = true,
612 .coldplug = swap_coldplug,
619 .serialize = swap_serialize,
620 .deserialize_item = swap_deserialize_item,
622 .active_state = swap_active_state,
623 .sub_state_to_string = swap_sub_state_to_string,
625 .check_gc = swap_check_gc,
627 .bus_interface = "org.freedesktop.systemd1.Swap",
628 .bus_message_handler = bus_swap_message_handler,
629 .bus_invalidating_properties = bus_swap_invalidating_properties,
631 .reset_maintenance = swap_reset_maintenance,
633 .enumerate = swap_enumerate,
634 .shutdown = swap_shutdown