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"
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, u->meta.manager->running_as == MANAGER_SYSTEM)) < 0)
201 if ((r = swap_add_mount_links(s)) < 0)
204 if ((r = swap_add_target_links(s)) < 0)
207 if (s->meta.default_dependencies)
208 if ((r = swap_add_default_dependencies(s)) < 0)
212 return swap_verify(s);
215 static int swap_find(Manager *m, const char *what, Unit **_u) {
223 /* /proc/swaps and /etc/fstab might refer to this device by
224 * different names (e.g. one by uuid, the other by the kernel
225 * name), we hence need to look for all aliases we are aware
226 * of for this device */
228 if (!(e = unit_name_from_path(what, ".device")))
231 u = manager_get_unit(m, e);
238 SET_FOREACH(d, u->meta.names, i) {
241 if (!(e = unit_name_change_suffix(d, ".swap")))
244 k = manager_get_unit(m, e);
264 bool from_proc_swaps) {
266 char *e = NULL, *w = NULL;
274 if (!(e = unit_name_from_path(what, ".swap")))
277 if (!(u = manager_get_unit(m, e)))
278 if ((r = swap_find(m, what, &u)) < 0)
284 if (!(u = unit_new(m))) {
291 if ((r = unit_add_name(u, e)) < 0)
294 if (!(w = strdup(what))) {
299 if (from_proc_swaps) {
300 p = &SWAP(u)->parameters_proc_swaps;
301 SWAP(u)->from_proc_swaps = true;
303 p = &SWAP(u)->parameters_etc_fstab;
304 SWAP(u)->from_etc_fstab = true;
310 p->priority = priority;
315 unit_add_to_load_queue(u);
317 unit_add_to_dbus_queue(u);
333 static void swap_set_state(Swap *s, SwapState state) {
337 old_state = s->state;
340 if (state != old_state)
341 log_debug("%s changed %s -> %s",
343 swap_state_to_string(old_state),
344 swap_state_to_string(state));
346 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
349 static int swap_coldplug(Unit *u) {
351 SwapState new_state = SWAP_DEAD;
354 assert(s->state == SWAP_DEAD);
356 if (s->deserialized_state != s->state)
357 new_state = s->deserialized_state;
358 else if (s->from_proc_swaps)
359 new_state = SWAP_ACTIVE;
361 if (new_state != s->state)
362 swap_set_state(s, new_state);
367 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
374 if (s->from_proc_swaps)
375 p = &s->parameters_proc_swaps;
376 else if (s->from_fragment)
377 p = &s->parameters_fragment;
379 p = &s->parameters_etc_fstab;
387 "%sFrom /etc/fstab: %s\n"
388 "%sFrom /proc/swaps: %s\n"
389 "%sFrom fragment: %s\n",
390 prefix, swap_state_to_string(s->state),
393 prefix, yes_no(p->noauto),
394 prefix, yes_no(p->handle),
395 prefix, yes_no(s->from_etc_fstab),
396 prefix, yes_no(s->from_proc_swaps),
397 prefix, yes_no(s->from_fragment));
400 static void swap_enter_dead(Swap *s, bool success) {
403 swap_set_state(s, success ? SWAP_MAINTENANCE : SWAP_DEAD);
406 static int swap_start(Unit *u) {
412 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTENANCE);
414 if (s->from_fragment)
415 priority = s->parameters_fragment.priority;
416 else if (s->from_etc_fstab)
417 priority = s->parameters_etc_fstab.priority;
419 r = swapon(s->what, (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
421 if (r < 0 && errno != EBUSY) {
423 swap_enter_dead(s, false);
427 swap_set_state(s, SWAP_ACTIVE);
431 static int swap_stop(Unit *u) {
437 assert(s->state == SWAP_ACTIVE);
439 r = swapoff(s->what);
440 swap_enter_dead(s, r >= 0 || errno == EINVAL);
445 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
452 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
457 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
463 if (streq(key, "state")) {
466 if ((state = swap_state_from_string(value)) < 0)
467 log_debug("Failed to parse state value %s", value);
469 s->deserialized_state = state;
471 log_debug("Unknown serialization key '%s'", key);
476 static UnitActiveState swap_active_state(Unit *u) {
479 return state_translation_table[SWAP(u)->state];
482 static const char *swap_sub_state_to_string(Unit *u) {
485 return swap_state_to_string(SWAP(u)->state);
488 static bool swap_check_gc(Unit *u) {
493 return s->from_etc_fstab || s->from_proc_swaps;
496 static int swap_load_proc_swaps(Manager *m) {
497 rewind(m->proc_swaps);
499 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
502 char *dev = NULL, *d;
505 if ((k = fscanf(m->proc_swaps,
506 "%ms " /* device/file */
507 "%*s " /* type of swap */
508 "%*s " /* swap size */
510 "%i\n", /* priority */
511 &dev, &prio)) != 2) {
526 k = swap_add_one(m, d, prio, false, false, true);
536 static void swap_shutdown(Manager *m) {
540 fclose(m->proc_swaps);
541 m->proc_swaps = NULL;
545 static int swap_enumerate(Manager *m) {
550 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
553 if ((r = swap_load_proc_swaps(m)) < 0)
559 static void swap_reset_maintenance(Unit *u) {
564 if (s->state == SWAP_MAINTENANCE)
565 swap_set_state(s, SWAP_DEAD);
568 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
569 [SWAP_DEAD] = "dead",
570 [SWAP_ACTIVE] = "active",
571 [SWAP_MAINTENANCE] = "maintenance"
574 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
576 const UnitVTable swap_vtable = {
579 .no_instances = true,
587 .coldplug = swap_coldplug,
594 .serialize = swap_serialize,
595 .deserialize_item = swap_deserialize_item,
597 .active_state = swap_active_state,
598 .sub_state_to_string = swap_sub_state_to_string,
600 .check_gc = swap_check_gc,
602 .bus_message_handler = bus_swap_message_handler,
604 .reset_maintenance = swap_reset_maintenance,
606 .enumerate = swap_enumerate,
607 .shutdown = swap_shutdown