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->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_default_dependencies(Swap *s) {
128 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
130 if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
133 /* Note that by default we don't disable swap devices
134 * on shutdown. i.e. there is no umount.target
141 static int swap_verify(Swap *s) {
145 if (s->meta.load_state != UNIT_LOADED)
148 if (!(e = unit_name_from_path(s->what, ".swap")))
151 b = unit_has_name(UNIT(s), e);
155 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
162 static int swap_load(Unit *u) {
167 assert(u->meta.load_state == UNIT_STUB);
169 /* Load a .swap file */
170 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
173 if (u->meta.load_state == UNIT_LOADED) {
175 if (s->meta.fragment_path)
176 s->from_fragment = true;
179 if (s->parameters_fragment.what)
180 s->what = strdup(s->parameters_fragment.what);
181 else if (s->parameters_etc_fstab.what)
182 s->what = strdup(s->parameters_etc_fstab.what);
183 else if (s->parameters_proc_swaps.what)
184 s->what = strdup(s->parameters_proc_swaps.what);
186 s->what = unit_name_to_path(u->meta.id);
192 path_kill_slashes(s->what);
194 if (!s->meta.description)
195 if ((r = unit_set_description(u, s->what)) < 0)
198 if ((r = unit_add_node_link(u, s->what,
199 u->meta.manager->running_as == MANAGER_SYSTEM &&
200 u->meta.manager->swap_on_plug)) < 0)
203 if ((r = swap_add_mount_links(s)) < 0)
206 if ((r = swap_add_target_links(s)) < 0)
209 if (s->meta.default_dependencies)
210 if ((r = swap_add_default_dependencies(s)) < 0)
214 return swap_verify(s);
217 static int swap_find(Manager *m, const char *what, Unit **_u) {
225 /* /proc/swaps and /etc/fstab might refer to this device by
226 * different names (e.g. one by uuid, the other by the kernel
227 * name), we hence need to look for all aliases we are aware
228 * of for this device */
230 if (!(e = unit_name_from_path(what, ".device")))
233 u = manager_get_unit(m, e);
240 SET_FOREACH(d, u->meta.names, i) {
243 if (!(e = unit_name_change_suffix(d, ".swap")))
246 k = manager_get_unit(m, e);
266 bool from_proc_swaps) {
268 char *e = NULL, *w = NULL;
276 if (!(e = unit_name_from_path(what, ".swap")))
279 if (!(u = manager_get_unit(m, e)))
280 if ((r = swap_find(m, what, &u)) < 0)
286 if (!(u = unit_new(m))) {
293 if ((r = unit_add_name(u, e)) < 0)
296 if (!(w = strdup(what))) {
301 if (from_proc_swaps) {
302 p = &SWAP(u)->parameters_proc_swaps;
303 SWAP(u)->from_proc_swaps = true;
305 p = &SWAP(u)->parameters_etc_fstab;
306 SWAP(u)->from_etc_fstab = true;
312 p->priority = priority;
317 unit_add_to_load_queue(u);
319 unit_add_to_dbus_queue(u);
335 static void swap_set_state(Swap *s, SwapState state) {
339 old_state = s->state;
342 if (state != old_state)
343 log_debug("%s changed %s -> %s",
345 swap_state_to_string(old_state),
346 swap_state_to_string(state));
348 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
351 static int swap_coldplug(Unit *u) {
353 SwapState new_state = SWAP_DEAD;
356 assert(s->state == SWAP_DEAD);
358 if (s->deserialized_state != s->state)
359 new_state = s->deserialized_state;
360 else if (s->from_proc_swaps)
361 new_state = SWAP_ACTIVE;
363 if (new_state != s->state)
364 swap_set_state(s, new_state);
369 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
376 if (s->from_proc_swaps)
377 p = &s->parameters_proc_swaps;
378 else if (s->from_fragment)
379 p = &s->parameters_fragment;
381 p = &s->parameters_etc_fstab;
389 "%sFrom /etc/fstab: %s\n"
390 "%sFrom /proc/swaps: %s\n"
391 "%sFrom fragment: %s\n",
392 prefix, swap_state_to_string(s->state),
395 prefix, yes_no(p->noauto),
396 prefix, yes_no(p->handle),
397 prefix, yes_no(s->from_etc_fstab),
398 prefix, yes_no(s->from_proc_swaps),
399 prefix, yes_no(s->from_fragment));
402 static void swap_enter_dead(Swap *s, bool success) {
405 swap_set_state(s, success ? SWAP_DEAD : SWAP_MAINTENANCE);
408 static int swap_start(Unit *u) {
414 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTENANCE);
416 if (s->from_fragment)
417 priority = s->parameters_fragment.priority;
418 else if (s->from_etc_fstab)
419 priority = s->parameters_etc_fstab.priority;
421 r = swapon(s->what, (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
423 if (r < 0 && errno != EBUSY) {
425 swap_enter_dead(s, false);
429 swap_set_state(s, SWAP_ACTIVE);
433 static int swap_stop(Unit *u) {
439 assert(s->state == SWAP_ACTIVE);
441 r = swapoff(s->what);
442 swap_enter_dead(s, r >= 0 || errno == EINVAL);
447 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
454 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
459 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
465 if (streq(key, "state")) {
468 if ((state = swap_state_from_string(value)) < 0)
469 log_debug("Failed to parse state value %s", value);
471 s->deserialized_state = state;
473 log_debug("Unknown serialization key '%s'", key);
478 static UnitActiveState swap_active_state(Unit *u) {
481 return state_translation_table[SWAP(u)->state];
484 static const char *swap_sub_state_to_string(Unit *u) {
487 return swap_state_to_string(SWAP(u)->state);
490 static bool swap_check_gc(Unit *u) {
495 return s->from_etc_fstab || s->from_proc_swaps;
498 static int swap_load_proc_swaps(Manager *m) {
499 rewind(m->proc_swaps);
501 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
504 char *dev = NULL, *d;
507 if ((k = fscanf(m->proc_swaps,
508 "%ms " /* device/file */
509 "%*s " /* type of swap */
510 "%*s " /* swap size */
512 "%i\n", /* priority */
513 &dev, &prio)) != 2) {
528 k = swap_add_one(m, d, prio, false, false, true);
538 static void swap_shutdown(Manager *m) {
542 fclose(m->proc_swaps);
543 m->proc_swaps = NULL;
547 static int swap_enumerate(Manager *m) {
552 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
555 if ((r = swap_load_proc_swaps(m)) < 0)
561 static void swap_reset_maintenance(Unit *u) {
566 if (s->state == SWAP_MAINTENANCE)
567 swap_set_state(s, SWAP_DEAD);
570 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
571 [SWAP_DEAD] = "dead",
572 [SWAP_ACTIVE] = "active",
573 [SWAP_MAINTENANCE] = "maintenance"
576 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
578 const UnitVTable swap_vtable = {
581 .no_instances = true,
589 .coldplug = swap_coldplug,
596 .serialize = swap_serialize,
597 .deserialize_item = swap_deserialize_item,
599 .active_state = swap_active_state,
600 .sub_state_to_string = swap_sub_state_to_string,
602 .check_gc = swap_check_gc,
604 .bus_interface = "org.freedesktop.systemd1.Swap",
605 .bus_message_handler = bus_swap_message_handler,
606 .bus_invalidating_properties = bus_swap_invalidating_properties,
608 .reset_maintenance = swap_reset_maintenance,
610 .enumerate = swap_enumerate,
611 .shutdown = swap_shutdown