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_INACTIVE
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_dependency(UNIT(m), UNIT_BEFORE, UNIT(s), true)) < 0)
83 if ((r = unit_add_dependency(UNIT(s), UNIT_REQUIRES, UNIT(m), true)) < 0)
89 static int swap_add_mount_links(Swap *s) {
95 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
96 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
102 static int swap_add_target_links(Swap *s) {
109 if (s->from_fragment)
110 p = &s->parameters_fragment;
111 else if (s->from_etc_fstab)
112 p = &s->parameters_etc_fstab;
116 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, &tu)) < 0)
119 if (!p->noauto && p->handle && s->meta.manager->running_as == MANAGER_SYSTEM)
120 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
123 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
126 static int swap_verify(Swap *s) {
130 if (s->meta.load_state != UNIT_LOADED)
133 if (!(e = unit_name_from_path(s->what, ".swap")))
136 b = unit_has_name(UNIT(s), e);
140 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
147 static int swap_load(Unit *u) {
152 assert(u->meta.load_state == UNIT_STUB);
154 /* Load a .swap file */
155 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
158 if (u->meta.load_state == UNIT_LOADED) {
160 if (s->meta.fragment_path)
161 s->from_fragment = true;
164 if (s->parameters_fragment.what)
165 s->what = strdup(s->parameters_fragment.what);
166 else if (s->parameters_etc_fstab.what)
167 s->what = strdup(s->parameters_etc_fstab.what);
168 else if (s->parameters_proc_swaps.what)
169 s->what = strdup(s->parameters_proc_swaps.what);
171 s->what = unit_name_to_path(u->meta.id);
177 path_kill_slashes(s->what);
179 if (!s->meta.description)
180 if ((r = unit_set_description(u, s->what)) < 0)
183 if ((r = unit_add_node_link(u, s->what, u->meta.manager->running_as == MANAGER_SYSTEM)) < 0)
186 if ((r = swap_add_mount_links(s)) < 0)
189 if ((r = swap_add_target_links(s)) < 0)
193 return swap_verify(s);
196 static int swap_find(Manager *m, const char *what, Unit **_u) {
204 /* /proc/swaps and /etc/fstab might refer to this device by
205 * different names (e.g. one by uuid, the other by the kernel
206 * name), we hence need to look for all aliases we are aware
207 * of for this device */
209 if (!(e = unit_name_from_path(what, ".device")))
212 u = manager_get_unit(m, e);
219 SET_FOREACH(d, u->meta.names, i) {
222 if (!(e = unit_name_change_suffix(d, ".swap")))
225 k = manager_get_unit(m, e);
245 bool from_proc_swaps) {
247 char *e = NULL, *w = NULL;
255 if (!(e = unit_name_from_path(what, ".swap")))
258 if (!(u = manager_get_unit(m, e)))
259 if ((r = swap_find(m, what, &u)) < 0)
265 if (!(u = unit_new(m))) {
272 if ((r = unit_add_name(u, e)) < 0)
275 if (!(w = strdup(what))) {
280 if (from_proc_swaps) {
281 p = &SWAP(u)->parameters_proc_swaps;
282 SWAP(u)->from_proc_swaps = true;
284 p = &SWAP(u)->parameters_etc_fstab;
285 SWAP(u)->from_etc_fstab = true;
291 p->priority = priority;
296 unit_add_to_load_queue(u);
298 unit_add_to_dbus_queue(u);
314 static void swap_set_state(Swap *s, SwapState state) {
318 old_state = s->state;
321 if (state != old_state)
322 log_debug("%s changed %s -> %s",
324 swap_state_to_string(old_state),
325 swap_state_to_string(state));
327 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
330 static int swap_coldplug(Unit *u) {
332 SwapState new_state = SWAP_DEAD;
335 assert(s->state == SWAP_DEAD);
337 if (s->deserialized_state != s->state)
338 new_state = s->deserialized_state;
339 else if (s->from_proc_swaps)
340 new_state = SWAP_ACTIVE;
342 if (new_state != s->state)
343 swap_set_state(s, new_state);
348 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
355 if (s->from_proc_swaps)
356 p = &s->parameters_proc_swaps;
357 else if (s->from_fragment)
358 p = &s->parameters_fragment;
360 p = &s->parameters_etc_fstab;
368 "%sFrom /etc/fstab: %s\n"
369 "%sFrom /proc/swaps: %s\n"
370 "%sFrom fragment: %s\n",
371 prefix, swap_state_to_string(s->state),
374 prefix, yes_no(p->noauto),
375 prefix, yes_no(p->handle),
376 prefix, yes_no(s->from_etc_fstab),
377 prefix, yes_no(s->from_proc_swaps),
378 prefix, yes_no(s->from_fragment));
381 static void swap_enter_dead(Swap *s, bool success) {
384 swap_set_state(s, success ? SWAP_MAINTENANCE : SWAP_DEAD);
387 static int swap_start(Unit *u) {
393 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTENANCE);
395 if (s->from_fragment)
396 priority = s->parameters_fragment.priority;
397 else if (s->from_etc_fstab)
398 priority = s->parameters_etc_fstab.priority;
400 r = swapon(s->what, (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
402 if (r < 0 && errno != EBUSY) {
404 swap_enter_dead(s, false);
408 swap_set_state(s, SWAP_ACTIVE);
412 static int swap_stop(Unit *u) {
418 assert(s->state == SWAP_ACTIVE);
420 r = swapoff(s->what);
421 swap_enter_dead(s, r >= 0 || errno == EINVAL);
426 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
433 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
438 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
444 if (streq(key, "state")) {
447 if ((state = swap_state_from_string(value)) < 0)
448 log_debug("Failed to parse state value %s", value);
450 s->deserialized_state = state;
452 log_debug("Unknown serialization key '%s'", key);
457 static UnitActiveState swap_active_state(Unit *u) {
460 return state_translation_table[SWAP(u)->state];
463 static const char *swap_sub_state_to_string(Unit *u) {
466 return swap_state_to_string(SWAP(u)->state);
469 static bool swap_check_gc(Unit *u) {
474 return s->from_etc_fstab || s->from_proc_swaps;
477 static int swap_load_proc_swaps(Manager *m) {
478 rewind(m->proc_swaps);
480 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
483 char *dev = NULL, *d;
486 if ((k = fscanf(m->proc_swaps,
487 "%ms " /* device/file */
488 "%*s " /* type of swap */
489 "%*s " /* swap size */
491 "%i\n", /* priority */
492 &dev, &prio)) != 2) {
507 k = swap_add_one(m, d, prio, false, false, true);
517 static void swap_shutdown(Manager *m) {
521 fclose(m->proc_swaps);
522 m->proc_swaps = NULL;
526 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
527 [SWAP_DEAD] = "dead",
528 [SWAP_ACTIVE] = "active",
529 [SWAP_MAINTENANCE] = "maintenance"
532 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
534 static int swap_enumerate(Manager *m) {
539 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
542 if ((r = swap_load_proc_swaps(m)) < 0)
548 const UnitVTable swap_vtable = {
551 .no_instances = true,
558 .coldplug = swap_coldplug,
565 .serialize = swap_serialize,
566 .deserialize_item = swap_deserialize_item,
568 .active_state = swap_active_state,
569 .sub_state_to_string = swap_sub_state_to_string,
571 .check_gc = swap_check_gc,
573 .bus_message_handler = bus_swap_message_handler,
575 .enumerate = swap_enumerate,
576 .shutdown = swap_shutdown