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_done(Unit *u) {
51 static int swap_verify(Swap *s) {
55 if (UNIT(s)->meta.load_state != UNIT_LOADED)
58 if (!(e = unit_name_from_path(s->what, ".swap")))
61 b = unit_has_name(UNIT(s), e);
65 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->meta.id);
71 int swap_add_one_mount_link(Swap *s, Mount *m) {
77 if (s->meta.load_state != UNIT_LOADED ||
78 m->meta.load_state != UNIT_LOADED)
81 if (!path_startswith(s->what, m->where))
84 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(s), true)) < 0)
87 if ((r = unit_add_dependency(UNIT(s), UNIT_REQUIRES, UNIT(m), true)) < 0)
93 static int swap_add_mount_links(Swap *s) {
99 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
100 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
106 static int swap_add_target_links(Swap *s) {
107 Manager *m = s->meta.manager;
111 if ((r = manager_load_unit(m, SPECIAL_SWAP_TARGET, NULL, &tu)) < 0)
115 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
118 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
121 static int swap_load(Unit *u) {
126 assert(u->meta.load_state == UNIT_STUB);
128 /* Load a .swap file */
129 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
132 if (u->meta.load_state == UNIT_LOADED) {
134 if (!(s->what = unit_name_to_path(u->meta.id)))
137 path_kill_slashes(s->what);
139 if ((r = unit_add_node_link(u, s->what,
140 (u->meta.manager->running_as == MANAGER_INIT ||
141 u->meta.manager->running_as == MANAGER_SYSTEM))) < 0)
144 if ((r = swap_add_mount_links(s)) < 0)
147 if ((r = swap_add_target_links(s)) < 0)
151 return swap_verify(s);
154 int swap_add_one(Manager *m, const char *what, bool no_auto, int prio, bool from_proc_swaps) {
160 if (!(e = unit_name_from_path(what, ".swap")))
163 if (!(u = manager_get_unit(m, e))) {
166 if (!(u = unit_new(m))) {
171 r = unit_add_name(u, e);
177 if (!(SWAP(u)->what = strdup(what))) {
182 if ((r = unit_set_description(u, what)) < 0)
185 unit_add_to_load_queue(u);
187 SWAP(u)->from_proc_swaps_only = from_proc_swaps;
189 if (SWAP(u)->from_proc_swaps_only && !from_proc_swaps)
190 SWAP(u)->from_proc_swaps_only = false;
196 if (!from_proc_swaps)
197 SWAP(u)->no_auto = no_auto;
199 SWAP(u)->found_in_proc_swaps = true;
201 SWAP(u)->priority = prio;
212 static void swap_set_state(Swap *s, SwapState state) {
216 old_state = s->state;
219 if (state != old_state)
220 log_debug("%s changed %s -> %s",
222 swap_state_to_string(old_state),
223 swap_state_to_string(state));
225 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
228 static int swap_coldplug(Unit *u) {
230 SwapState new_state = SWAP_DEAD;
233 assert(s->state == SWAP_DEAD);
235 if (s->deserialized_state != s->state)
236 new_state = s->deserialized_state;
237 else if (s->found_in_proc_swaps)
238 new_state = SWAP_ACTIVE;
240 if (new_state != s->state)
241 swap_set_state(s, s->deserialized_state);
246 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
252 "%sAutomount State: %s\n"
256 prefix, swap_state_to_string(s->state),
259 prefix, yes_no(s->no_auto));
262 static void swap_enter_dead(Swap *s, bool success) {
265 swap_set_state(s, success ? SWAP_MAINTAINANCE : SWAP_DEAD);
268 static int swap_start(Unit *u) {
274 assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTAINANCE);
276 r = swapon(s->what, (s->priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
278 if (r < 0 && errno != EBUSY) {
280 swap_enter_dead(s, false);
284 swap_set_state(s, SWAP_ACTIVE);
288 static int swap_stop(Unit *u) {
294 assert(s->state == SWAP_ACTIVE);
296 r = swapoff(s->what);
297 swap_enter_dead(s, r >= 0 || errno == EINVAL);
302 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
309 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
314 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
320 if (streq(key, "state")) {
323 if ((state = swap_state_from_string(value)) < 0)
324 log_debug("Failed to parse state value %s", value);
326 s->deserialized_state = state;
328 log_debug("Unknown serialization key '%s'", key);
333 static UnitActiveState swap_active_state(Unit *u) {
336 return state_translation_table[SWAP(u)->state];
339 static const char *swap_sub_state_to_string(Unit *u) {
342 return swap_state_to_string(SWAP(u)->state);
345 static bool swap_check_gc(Unit *u) {
350 return !s->from_proc_swaps_only || s->found_in_proc_swaps;
353 static int swap_load_proc_swaps(Manager *m) {
356 rewind(m->proc_swaps);
358 (void) fscanf(m->proc_self_mountinfo, "%*s %*s %*s %*s %*s\n");
361 char *dev = NULL, *d;
364 k = fscanf(m->proc_self_mountinfo,
365 "%ms " /* device/file */
366 "%*s " /* type of swap */
367 "%*s " /* swap size */
369 "%d\n", /* priority */
379 if (!(d = cunescape(dev))) {
385 k = swap_add_one(m, d, false, prio, true);
393 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
394 Swap *s = (Swap*) meta;
396 if (s->state != SWAP_DEAD && s->state != SWAP_ACTIVE)
399 if ((s->state == SWAP_DEAD && !s->found_in_proc_swaps) ||
400 (s->state == SWAP_ACTIVE && s->found_in_proc_swaps))
403 swap_set_state(s, s->found_in_proc_swaps ? SWAP_ACTIVE : SWAP_DEAD);
405 /* Reset the flags for later calls */
406 s->found_in_proc_swaps = false;
410 static void swap_shutdown(Manager *m) {
414 fclose(m->proc_swaps);
415 m->proc_swaps = NULL;
419 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
420 [SWAP_DEAD] = "dead",
421 [SWAP_ACTIVE] = "active",
422 [SWAP_MAINTAINANCE] = "maintainance"
425 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
427 static int swap_enumerate(Manager *m) {
431 if (!m->proc_swaps &&
432 !(m->proc_swaps = fopen("/proc/swaps", "er")))
435 if ((r = swap_load_proc_swaps(m)) < 0)
441 const UnitVTable swap_vtable = {
445 .no_instances = true,
451 .coldplug = swap_coldplug,
458 .serialize = swap_serialize,
459 .deserialize_item = swap_deserialize_item,
461 .active_state = swap_active_state,
462 .sub_state_to_string = swap_sub_state_to_string,
464 .check_gc = swap_check_gc,
466 .bus_message_handler = bus_swap_message_handler,
468 .enumerate = swap_enumerate,
469 .shutdown = swap_shutdown