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>
33 #include "load-fragment.h"
34 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "dbus-swap.h"
38 #include "bus-errors.h"
39 #include "exit-status.h"
42 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
43 [SWAP_DEAD] = UNIT_INACTIVE,
44 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
45 [SWAP_ACTIVE] = UNIT_ACTIVE,
46 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
47 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
48 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
49 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
50 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
51 [SWAP_FAILED] = UNIT_FAILED
54 static void swap_unset_proc_swaps(Swap *s) {
59 if (!s->parameters_proc_swaps.what)
62 /* Remove this unit from the chain of swaps which share the
63 * same kernel swap device. */
65 first = hashmap_get(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
66 LIST_REMOVE(Swap, same_proc_swaps, first, s);
69 hashmap_remove_and_replace(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
71 hashmap_remove(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
73 free(s->parameters_proc_swaps.what);
74 s->parameters_proc_swaps.what = NULL;
77 static void swap_init(Unit *u) {
81 assert(s->meta.load_state == UNIT_STUB);
83 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
85 exec_context_init(&s->exec_context);
86 s->exec_context.std_output = EXEC_OUTPUT_KMSG;
88 s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
90 s->timer_watch.type = WATCH_INVALID;
92 s->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
94 s->meta.ignore_on_isolate = true;
97 static void swap_unwatch_control_pid(Swap *s) {
100 if (s->control_pid <= 0)
103 unit_unwatch_pid(UNIT(s), s->control_pid);
107 static void swap_done(Unit *u) {
112 swap_unset_proc_swaps(s);
117 free(s->parameters_etc_fstab.what);
118 free(s->parameters_fragment.what);
119 s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL;
121 exec_context_done(&s->exec_context);
122 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
123 s->control_command = NULL;
125 swap_unwatch_control_pid(s);
127 unit_unwatch_timer(u, &s->timer_watch);
130 int swap_add_one_mount_link(Swap *s, Mount *m) {
136 if (s->meta.load_state != UNIT_LOADED ||
137 m->meta.load_state != UNIT_LOADED)
140 if (is_device_path(s->what))
143 if (!path_startswith(s->what, m->where))
146 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
152 static int swap_add_mount_links(Swap *s) {
158 LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_MOUNT])
159 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
165 static int swap_add_target_links(Swap *s) {
172 if (s->from_fragment)
173 p = &s->parameters_fragment;
174 else if (s->from_etc_fstab)
175 p = &s->parameters_etc_fstab;
179 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
184 (p->handle || s->meta.manager->swap_auto) &&
186 s->meta.manager->running_as == MANAGER_SYSTEM)
187 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
190 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
193 static int swap_add_device_links(Swap *s) {
201 if (s->from_fragment)
202 p = &s->parameters_fragment;
203 else if (s->from_etc_fstab)
204 p = &s->parameters_etc_fstab;
208 if (is_device_path(s->what))
209 return unit_add_node_link(UNIT(s), s->what,
210 !p->noauto && p->nofail &&
211 s->meta.manager->running_as == MANAGER_SYSTEM);
213 /* File based swap devices need to be ordered after
214 * remount-rootfs.service, since they might need a
215 * writable file system. */
216 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true);
219 static int swap_add_default_dependencies(Swap *s) {
224 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
226 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
233 static int swap_verify(Swap *s) {
237 if (s->meta.load_state != UNIT_LOADED)
240 if (!(e = unit_name_from_path(s->what, ".swap")))
243 b = unit_has_name(UNIT(s), e);
247 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
251 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
252 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
259 static int swap_load(Unit *u) {
264 assert(u->meta.load_state == UNIT_STUB);
266 /* Load a .swap file */
267 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
270 if (u->meta.load_state == UNIT_LOADED) {
271 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
274 if (s->meta.fragment_path)
275 s->from_fragment = true;
278 if (s->parameters_fragment.what)
279 s->what = strdup(s->parameters_fragment.what);
280 else if (s->parameters_etc_fstab.what)
281 s->what = strdup(s->parameters_etc_fstab.what);
282 else if (s->parameters_proc_swaps.what)
283 s->what = strdup(s->parameters_proc_swaps.what);
285 s->what = unit_name_to_path(u->meta.id);
291 path_kill_slashes(s->what);
293 if (!s->meta.description)
294 if ((r = unit_set_description(u, s->what)) < 0)
297 if ((r = swap_add_device_links(s)) < 0)
300 if ((r = swap_add_mount_links(s)) < 0)
303 if ((r = swap_add_target_links(s)) < 0)
306 if ((r = unit_add_default_cgroups(u)) < 0)
309 if (s->meta.default_dependencies)
310 if ((r = swap_add_default_dependencies(s)) < 0)
314 return swap_verify(s);
320 const char *what_proc_swaps,
328 char *e = NULL, *wp = NULL;
336 if (!(e = unit_name_from_path(what, ".swap")))
339 u = manager_get_unit(m, e);
341 if (what_proc_swaps &&
343 SWAP(u)->from_proc_swaps &&
344 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
350 if (!(u = unit_new(m))) {
355 if ((r = unit_add_name(u, e)) < 0)
358 if (!(SWAP(u)->what = strdup(what))) {
363 unit_add_to_load_queue(u);
367 if (what_proc_swaps) {
370 p = &SWAP(u)->parameters_proc_swaps;
373 if (!(wp = strdup(what_proc_swaps))) {
378 if (!m->swaps_by_proc_swaps)
379 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
387 first = hashmap_get(m->swaps_by_proc_swaps, wp);
388 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
390 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
395 SWAP(u)->is_active = true;
396 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
399 SWAP(u)->from_proc_swaps = true;
402 p = &SWAP(u)->parameters_etc_fstab;
404 if (!(wp = strdup(what))) {
412 SWAP(u)->from_etc_fstab = true;
415 p->priority = priority;
420 unit_add_to_dbus_queue(u);
427 log_warning("Failed to load swap unit: %s", strerror(-r));
438 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
444 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
445 struct udev_device *d;
447 struct udev_list_entry *item = NULL, *first = NULL;
449 /* So this is a proper swap device. Create swap units
450 * for all names this swap device is known under */
452 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
455 if ((dn = udev_device_get_devnode(d)))
456 r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
458 /* Add additional units for all symlinks */
459 first = udev_device_get_devlinks_list_entry(d);
460 udev_list_entry_foreach(item, first) {
463 /* Don't bother with the /dev/block links */
464 p = udev_list_entry_get_name(item);
466 if (path_startswith(p, "/dev/block/"))
469 if (stat(p, &st) >= 0)
470 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
473 if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
477 udev_device_unref(d);
480 if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
486 static void swap_set_state(Swap *s, SwapState state) {
491 old_state = s->state;
494 if (state != SWAP_ACTIVATING &&
495 state != SWAP_ACTIVATING_SIGTERM &&
496 state != SWAP_ACTIVATING_SIGKILL &&
497 state != SWAP_DEACTIVATING &&
498 state != SWAP_DEACTIVATING_SIGTERM &&
499 state != SWAP_DEACTIVATING_SIGKILL) {
500 unit_unwatch_timer(UNIT(s), &s->timer_watch);
501 swap_unwatch_control_pid(s);
502 s->control_command = NULL;
503 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
506 if (state != old_state)
507 log_debug("%s changed %s -> %s",
509 swap_state_to_string(old_state),
510 swap_state_to_string(state));
512 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
515 static int swap_coldplug(Unit *u) {
517 SwapState new_state = SWAP_DEAD;
521 assert(s->state == SWAP_DEAD);
523 if (s->deserialized_state != s->state)
524 new_state = s->deserialized_state;
525 else if (s->from_proc_swaps)
526 new_state = SWAP_ACTIVE;
528 if (new_state != s->state) {
530 if (new_state == SWAP_ACTIVATING ||
531 new_state == SWAP_ACTIVATING_SIGTERM ||
532 new_state == SWAP_ACTIVATING_SIGKILL ||
533 new_state == SWAP_DEACTIVATING ||
534 new_state == SWAP_DEACTIVATING_SIGTERM ||
535 new_state == SWAP_DEACTIVATING_SIGKILL) {
537 if (s->control_pid <= 0)
540 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
543 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
547 swap_set_state(s, new_state);
553 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
560 if (s->from_proc_swaps)
561 p = &s->parameters_proc_swaps;
562 else if (s->from_fragment)
563 p = &s->parameters_fragment;
565 p = &s->parameters_etc_fstab;
574 "%sFrom /etc/fstab: %s\n"
575 "%sFrom /proc/swaps: %s\n"
576 "%sFrom fragment: %s\n",
577 prefix, swap_state_to_string(s->state),
580 prefix, yes_no(p->noauto),
581 prefix, yes_no(p->nofail),
582 prefix, yes_no(p->handle),
583 prefix, yes_no(s->from_etc_fstab),
584 prefix, yes_no(s->from_proc_swaps),
585 prefix, yes_no(s->from_fragment));
587 if (s->control_pid > 0)
589 "%sControl PID: %lu\n",
590 prefix, (unsigned long) s->control_pid);
592 exec_context_dump(&s->exec_context, f, prefix);
595 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
603 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
606 if ((r = exec_spawn(c,
610 s->meta.manager->environment,
614 s->meta.manager->confirm_spawn,
615 s->meta.cgroup_bondings,
616 s->meta.cgroup_attributes,
620 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
621 /* FIXME: we need to do something here */
629 unit_unwatch_timer(UNIT(s), &s->timer_watch);
634 static void swap_enter_dead(Swap *s, bool success) {
640 swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD);
643 static void swap_enter_active(Swap *s, bool success) {
649 swap_set_state(s, SWAP_ACTIVE);
652 static void swap_enter_signal(Swap *s, SwapState state, bool success) {
655 bool wait_for_exit = false;
662 if (s->exec_context.kill_mode != KILL_NONE) {
663 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
664 state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
666 if (s->control_pid > 0) {
667 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
669 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
671 wait_for_exit = true;
674 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
676 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
681 /* Exclude the control pid from being killed via the cgroup */
682 if (s->control_pid > 0)
683 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
686 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
687 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
688 log_warning("Failed to kill control group: %s", strerror(-r));
690 wait_for_exit = true;
698 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
701 swap_set_state(s, state);
703 swap_enter_dead(s, true);
708 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
710 swap_enter_dead(s, false);
716 static void swap_enter_activating(Swap *s) {
721 s->control_command_id = SWAP_EXEC_ACTIVATE;
722 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
724 if (s->from_fragment)
725 priority = s->parameters_fragment.priority;
726 else if (s->from_etc_fstab)
727 priority = s->parameters_etc_fstab.priority;
734 snprintf(p, sizeof(p), "%i", priority);
737 r = exec_command_set(
745 r = exec_command_set(
754 swap_unwatch_control_pid(s);
756 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
759 swap_set_state(s, SWAP_ACTIVATING);
764 log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r));
765 swap_enter_dead(s, false);
768 static void swap_enter_deactivating(Swap *s, bool success) {
776 s->control_command_id = SWAP_EXEC_DEACTIVATE;
777 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
779 if ((r = exec_command_set(
786 swap_unwatch_control_pid(s);
788 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
791 swap_set_state(s, SWAP_DEACTIVATING);
796 log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r));
797 swap_enter_active(s, false);
800 static int swap_start(Unit *u) {
805 /* We cannot fulfill this request right now, try again later
808 if (s->state == SWAP_DEACTIVATING ||
809 s->state == SWAP_DEACTIVATING_SIGTERM ||
810 s->state == SWAP_DEACTIVATING_SIGKILL ||
811 s->state == SWAP_ACTIVATING_SIGTERM ||
812 s->state == SWAP_ACTIVATING_SIGKILL)
815 if (s->state == SWAP_ACTIVATING)
818 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
821 swap_enter_activating(s);
825 static int swap_stop(Unit *u) {
830 if (s->state == SWAP_DEACTIVATING ||
831 s->state == SWAP_DEACTIVATING_SIGTERM ||
832 s->state == SWAP_DEACTIVATING_SIGKILL ||
833 s->state == SWAP_ACTIVATING_SIGTERM ||
834 s->state == SWAP_ACTIVATING_SIGKILL)
837 assert(s->state == SWAP_ACTIVATING ||
838 s->state == SWAP_ACTIVE);
840 swap_enter_deactivating(s, true);
844 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
851 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
852 unit_serialize_item(u, f, "failure", yes_no(s->failure));
854 if (s->control_pid > 0)
855 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
857 if (s->control_command_id >= 0)
858 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
863 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
869 if (streq(key, "state")) {
872 if ((state = swap_state_from_string(value)) < 0)
873 log_debug("Failed to parse state value %s", value);
875 s->deserialized_state = state;
876 } else if (streq(key, "failure")) {
879 if ((b = parse_boolean(value)) < 0)
880 log_debug("Failed to parse failure value %s", value);
882 s->failure = b || s->failure;
884 } else if (streq(key, "control-pid")) {
887 if (parse_pid(value, &pid) < 0)
888 log_debug("Failed to parse control-pid value %s", value);
890 s->control_pid = pid;
892 } else if (streq(key, "control-command")) {
895 if ((id = swap_exec_command_from_string(value)) < 0)
896 log_debug("Failed to parse exec-command value %s", value);
898 s->control_command_id = id;
899 s->control_command = s->exec_command + id;
903 log_debug("Unknown serialization key '%s'", key);
908 static UnitActiveState swap_active_state(Unit *u) {
911 return state_translation_table[SWAP(u)->state];
914 static const char *swap_sub_state_to_string(Unit *u) {
917 return swap_state_to_string(SWAP(u)->state);
920 static bool swap_check_gc(Unit *u) {
925 return s->from_etc_fstab || s->from_proc_swaps;
928 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
935 if (pid != s->control_pid)
940 success = is_clean_exit(code, status);
941 s->failure = s->failure || !success;
943 if (s->control_command) {
944 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
945 s->control_command = NULL;
946 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
949 log_full(success ? LOG_DEBUG : LOG_NOTICE,
950 "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
954 case SWAP_ACTIVATING:
955 case SWAP_ACTIVATING_SIGTERM:
956 case SWAP_ACTIVATING_SIGKILL:
959 swap_enter_active(s, true);
961 swap_enter_dead(s, false);
964 case SWAP_DEACTIVATING:
965 case SWAP_DEACTIVATING_SIGKILL:
966 case SWAP_DEACTIVATING_SIGTERM:
969 swap_enter_dead(s, true);
971 swap_enter_dead(s, false);
975 assert_not_reached("Uh, control process died at wrong time.");
978 /* Notify clients about changed exit status */
979 unit_add_to_dbus_queue(u);
981 /* Request a reload of /proc/swaps, so that following units
982 * can follow our state change */
983 u->meta.manager->request_reload = true;
986 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
990 assert(elapsed == 1);
991 assert(w == &s->timer_watch);
995 case SWAP_ACTIVATING:
996 log_warning("%s activation timed out. Stopping.", u->meta.id);
997 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false);
1000 case SWAP_DEACTIVATING:
1001 log_warning("%s deactivation timed out. Stopping.", u->meta.id);
1002 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false);
1005 case SWAP_ACTIVATING_SIGTERM:
1006 if (s->exec_context.send_sigkill) {
1007 log_warning("%s activation timed out. Killing.", u->meta.id);
1008 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
1010 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1011 swap_enter_dead(s, false);
1015 case SWAP_DEACTIVATING_SIGTERM:
1016 if (s->exec_context.send_sigkill) {
1017 log_warning("%s deactivation timed out. Killing.", u->meta.id);
1018 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
1020 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1021 swap_enter_dead(s, false);
1025 case SWAP_ACTIVATING_SIGKILL:
1026 case SWAP_DEACTIVATING_SIGKILL:
1027 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id);
1028 swap_enter_dead(s, false);
1032 assert_not_reached("Timeout at wrong time.");
1036 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1042 rewind(m->proc_swaps);
1044 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1047 char *dev = NULL, *d;
1050 if ((k = fscanf(m->proc_swaps,
1051 "%ms " /* device/file */
1052 "%*s " /* type of swap */
1053 "%*s " /* swap size */
1055 "%i\n", /* priority */
1056 &dev, &prio)) != 2) {
1061 log_warning("Failed to parse /proc/swaps:%u.", i);
1072 k = swap_process_new_swap(m, d, prio, set_flags);
1082 int swap_dispatch_reload(Manager *m) {
1083 /* This function should go as soon as the kernel properly notifies us */
1085 if (_likely_(!m->request_reload))
1088 m->request_reload = false;
1090 return swap_fd_event(m, EPOLLPRI);
1093 int swap_fd_event(Manager *m, int events) {
1098 assert(events & EPOLLPRI);
1100 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1101 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1103 /* Reset flags, just in case, for late calls */
1104 LIST_FOREACH(units_by_type, meta, m->units_by_type[UNIT_SWAP]) {
1105 Swap *swap = (Swap*) meta;
1107 swap->is_active = swap->just_activated = false;
1113 manager_dispatch_load_queue(m);
1115 LIST_FOREACH(units_by_type, meta, m->units_by_type[UNIT_SWAP]) {
1116 Swap *swap = (Swap*) meta;
1118 if (!swap->is_active) {
1119 /* This has just been deactivated */
1121 swap->from_proc_swaps = false;
1122 swap_unset_proc_swaps(swap);
1124 switch (swap->state) {
1127 swap_enter_dead(swap, true);
1131 swap_set_state(swap, swap->state);
1135 } else if (swap->just_activated) {
1137 /* New swap entry */
1139 switch (swap->state) {
1143 swap_enter_active(swap, true);
1147 /* Nothing really changed, but let's
1148 * issue an notification call
1149 * nonetheless, in case somebody is
1150 * waiting for this. */
1151 swap_set_state(swap, swap->state);
1156 /* Reset the flags for later calls */
1157 swap->is_active = swap->just_activated = false;
1163 static Unit *swap_following(Unit *u) {
1165 Swap *other, *first = NULL;
1169 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1172 /* Make everybody follow the unit that's named after the swap
1173 * device in the kernel */
1175 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1176 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1179 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1180 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1189 static int swap_following_set(Unit *u, Set **_set) {
1198 if (LIST_JUST_US(same_proc_swaps, s)) {
1203 if (!(set = set_new(NULL, NULL)))
1206 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1207 if ((r = set_put(set, other)) < 0)
1210 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1211 if ((r = set_put(set, other)) < 0)
1222 static void swap_shutdown(Manager *m) {
1225 if (m->proc_swaps) {
1226 fclose(m->proc_swaps);
1227 m->proc_swaps = NULL;
1230 hashmap_free(m->swaps_by_proc_swaps);
1231 m->swaps_by_proc_swaps = NULL;
1234 static int swap_enumerate(Manager *m) {
1236 struct epoll_event ev;
1239 if (!m->proc_swaps) {
1240 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1241 return (errno == ENOENT) ? 0 : -errno;
1243 m->swap_watch.type = WATCH_SWAP;
1244 m->swap_watch.fd = fileno(m->proc_swaps);
1247 ev.events = EPOLLPRI;
1248 ev.data.ptr = &m->swap_watch;
1250 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1254 /* We rely on mount.c to load /etc/fstab for us */
1256 if ((r = swap_load_proc_swaps(m, false)) < 0)
1262 static void swap_reset_failed(Unit *u) {
1267 if (s->state == SWAP_FAILED)
1268 swap_set_state(s, SWAP_DEAD);
1273 static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1276 Set *pid_set = NULL;
1280 if (who == KILL_MAIN) {
1281 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1285 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1286 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1290 if (who == KILL_CONTROL || who == KILL_ALL)
1291 if (s->control_pid > 0)
1292 if (kill(s->control_pid, signo) < 0)
1295 if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
1298 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1301 /* Exclude the control pid from being killed via the cgroup */
1302 if (s->control_pid > 0)
1303 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
1308 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
1309 if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1320 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1321 [SWAP_DEAD] = "dead",
1322 [SWAP_ACTIVATING] = "activating",
1323 [SWAP_ACTIVE] = "active",
1324 [SWAP_DEACTIVATING] = "deactivating",
1325 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1326 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1327 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1328 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1329 [SWAP_FAILED] = "failed"
1332 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1334 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1335 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1336 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1339 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1341 const UnitVTable swap_vtable = {
1349 .no_instances = true,
1350 .show_status = true,
1356 .coldplug = swap_coldplug,
1360 .start = swap_start,
1365 .serialize = swap_serialize,
1366 .deserialize_item = swap_deserialize_item,
1368 .active_state = swap_active_state,
1369 .sub_state_to_string = swap_sub_state_to_string,
1371 .check_gc = swap_check_gc,
1373 .sigchld_event = swap_sigchld_event,
1374 .timer_event = swap_timer_event,
1376 .reset_failed = swap_reset_failed,
1378 .bus_interface = "org.freedesktop.systemd1.Swap",
1379 .bus_message_handler = bus_swap_message_handler,
1380 .bus_invalidating_properties = bus_swap_invalidating_properties,
1382 .following = swap_following,
1383 .following_set = swap_following_set,
1385 .enumerate = swap_enumerate,
1386 .shutdown = swap_shutdown