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"
40 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
41 [SWAP_DEAD] = UNIT_INACTIVE,
42 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
43 [SWAP_ACTIVE] = UNIT_ACTIVE,
44 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
45 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
46 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
47 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
48 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
49 [SWAP_FAILED] = UNIT_FAILED
52 static void swap_unset_proc_swaps(Swap *s) {
57 if (!s->parameters_proc_swaps.what)
60 /* Remove this unit from the chain of swaps which share the
61 * same kernel swap device. */
63 first = hashmap_get(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
64 LIST_REMOVE(Swap, same_proc_swaps, first, s);
67 hashmap_remove_and_replace(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
69 hashmap_remove(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
71 free(s->parameters_proc_swaps.what);
72 s->parameters_proc_swaps.what = NULL;
75 static void swap_init(Unit *u) {
79 assert(s->meta.load_state == UNIT_STUB);
81 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
83 exec_context_init(&s->exec_context);
85 s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
87 s->timer_watch.type = WATCH_INVALID;
89 s->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
92 static void swap_unwatch_control_pid(Swap *s) {
95 if (s->control_pid <= 0)
98 unit_unwatch_pid(UNIT(s), s->control_pid);
102 static void swap_done(Unit *u) {
107 swap_unset_proc_swaps(s);
112 free(s->parameters_etc_fstab.what);
113 free(s->parameters_fragment.what);
114 s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL;
116 exec_context_done(&s->exec_context);
117 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
118 s->control_command = NULL;
120 swap_unwatch_control_pid(s);
122 unit_unwatch_timer(u, &s->timer_watch);
125 int swap_add_one_mount_link(Swap *s, Mount *m) {
131 if (s->meta.load_state != UNIT_LOADED ||
132 m->meta.load_state != UNIT_LOADED)
135 if (is_device_path(s->what))
138 if (!path_startswith(s->what, m->where))
141 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
147 static int swap_add_mount_links(Swap *s) {
153 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
154 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
160 static int swap_add_target_links(Swap *s) {
167 if (s->from_fragment)
168 p = &s->parameters_fragment;
169 else if (s->from_etc_fstab)
170 p = &s->parameters_etc_fstab;
174 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
178 (p->handle || s->meta.manager->swap_auto) &&
180 s->meta.manager->running_as == MANAGER_SYSTEM)
181 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
184 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
187 static int swap_add_device_links(Swap *s) {
195 if (s->from_fragment)
196 p = &s->parameters_fragment;
197 else if (s->from_etc_fstab)
198 p = &s->parameters_etc_fstab;
202 if (is_device_path(s->what))
203 return unit_add_node_link(UNIT(s), s->what,
204 !p->noauto && p->nofail &&
205 s->meta.manager->running_as == MANAGER_SYSTEM);
207 /* File based swap devices need to be ordered after
208 * remount-rootfs.service, since they might need a
209 * writable file system. */
210 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true);
213 static int swap_add_default_dependencies(Swap *s) {
218 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
220 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
227 static int swap_verify(Swap *s) {
231 if (s->meta.load_state != UNIT_LOADED)
234 if (!(e = unit_name_from_path(s->what, ".swap")))
237 b = unit_has_name(UNIT(s), e);
241 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
245 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
246 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
253 static int swap_load(Unit *u) {
258 assert(u->meta.load_state == UNIT_STUB);
260 /* Load a .swap file */
261 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
264 if (u->meta.load_state == UNIT_LOADED) {
266 if (s->meta.fragment_path)
267 s->from_fragment = true;
270 if (s->parameters_fragment.what)
271 s->what = strdup(s->parameters_fragment.what);
272 else if (s->parameters_etc_fstab.what)
273 s->what = strdup(s->parameters_etc_fstab.what);
274 else if (s->parameters_proc_swaps.what)
275 s->what = strdup(s->parameters_proc_swaps.what);
277 s->what = unit_name_to_path(u->meta.id);
283 path_kill_slashes(s->what);
285 if (!s->meta.description)
286 if ((r = unit_set_description(u, s->what)) < 0)
289 if ((r = swap_add_device_links(s)) < 0)
292 if ((r = swap_add_mount_links(s)) < 0)
295 if ((r = swap_add_target_links(s)) < 0)
298 if ((r = unit_add_default_cgroups(u)) < 0)
301 if (s->meta.default_dependencies)
302 if ((r = swap_add_default_dependencies(s)) < 0)
306 return swap_verify(s);
312 const char *what_proc_swaps,
320 char *e = NULL, *wp = NULL;
328 if (!(e = unit_name_from_path(what, ".swap")))
331 u = manager_get_unit(m, e);
333 if (what_proc_swaps &&
335 SWAP(u)->from_proc_swaps &&
336 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
342 if (!(u = unit_new(m))) {
347 if ((r = unit_add_name(u, e)) < 0)
350 if (!(SWAP(u)->what = strdup(what))) {
355 unit_add_to_load_queue(u);
359 if (what_proc_swaps) {
362 p = &SWAP(u)->parameters_proc_swaps;
365 if (!(wp = strdup(what_proc_swaps))) {
370 if (!m->swaps_by_proc_swaps)
371 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
379 first = hashmap_get(m->swaps_by_proc_swaps, wp);
380 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
382 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
387 SWAP(u)->is_active = true;
388 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
391 SWAP(u)->from_proc_swaps = true;
394 p = &SWAP(u)->parameters_etc_fstab;
396 if (!(wp = strdup(what))) {
404 SWAP(u)->from_etc_fstab = true;
407 p->priority = priority;
412 unit_add_to_dbus_queue(u);
419 log_warning("Failed to load swap unit: %s", strerror(-r));
430 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
436 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
437 struct udev_device *d;
439 struct udev_list_entry *item = NULL, *first = NULL;
441 /* So this is a proper swap device. Create swap units
442 * for all names this swap device is known under */
444 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
447 if ((dn = udev_device_get_devnode(d)))
448 r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
450 /* Add additional units for all symlinks */
451 first = udev_device_get_devlinks_list_entry(d);
452 udev_list_entry_foreach(item, first) {
455 /* Don't bother with the /dev/block links */
456 p = udev_list_entry_get_name(item);
458 if (path_startswith(p, "/dev/block/"))
461 if (stat(p, &st) >= 0)
462 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
465 if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
469 udev_device_unref(d);
472 if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
478 static void swap_set_state(Swap *s, SwapState state) {
483 old_state = s->state;
486 if (state != SWAP_ACTIVATING &&
487 state != SWAP_ACTIVATING_SIGTERM &&
488 state != SWAP_ACTIVATING_SIGKILL &&
489 state != SWAP_DEACTIVATING &&
490 state != SWAP_DEACTIVATING_SIGTERM &&
491 state != SWAP_DEACTIVATING_SIGKILL) {
492 unit_unwatch_timer(UNIT(s), &s->timer_watch);
493 swap_unwatch_control_pid(s);
494 s->control_command = NULL;
495 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
498 if (state != old_state)
499 log_debug("%s changed %s -> %s",
501 swap_state_to_string(old_state),
502 swap_state_to_string(state));
504 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
507 static int swap_coldplug(Unit *u) {
509 SwapState new_state = SWAP_DEAD;
513 assert(s->state == SWAP_DEAD);
515 if (s->deserialized_state != s->state)
516 new_state = s->deserialized_state;
517 else if (s->from_proc_swaps)
518 new_state = SWAP_ACTIVE;
520 if (new_state != s->state) {
522 if (new_state == SWAP_ACTIVATING ||
523 new_state == SWAP_ACTIVATING_SIGTERM ||
524 new_state == SWAP_ACTIVATING_SIGKILL ||
525 new_state == SWAP_DEACTIVATING ||
526 new_state == SWAP_DEACTIVATING_SIGTERM ||
527 new_state == SWAP_DEACTIVATING_SIGKILL) {
529 if (s->control_pid <= 0)
532 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
535 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
539 swap_set_state(s, new_state);
545 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
552 if (s->from_proc_swaps)
553 p = &s->parameters_proc_swaps;
554 else if (s->from_fragment)
555 p = &s->parameters_fragment;
557 p = &s->parameters_etc_fstab;
566 "%sFrom /etc/fstab: %s\n"
567 "%sFrom /proc/swaps: %s\n"
568 "%sFrom fragment: %s\n",
569 prefix, swap_state_to_string(s->state),
572 prefix, yes_no(p->noauto),
573 prefix, yes_no(p->nofail),
574 prefix, yes_no(p->handle),
575 prefix, yes_no(s->from_etc_fstab),
576 prefix, yes_no(s->from_proc_swaps),
577 prefix, yes_no(s->from_fragment));
579 if (s->control_pid > 0)
581 "%sControl PID: %lu\n",
582 prefix, (unsigned long) s->control_pid);
584 exec_context_dump(&s->exec_context, f, prefix);
587 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
595 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
598 if ((r = exec_spawn(c,
602 s->meta.manager->environment,
606 s->meta.manager->confirm_spawn,
607 s->meta.cgroup_bondings,
611 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
612 /* FIXME: we need to do something here */
620 unit_unwatch_timer(UNIT(s), &s->timer_watch);
625 static void swap_enter_dead(Swap *s, bool success) {
631 swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD);
634 static void swap_enter_active(Swap *s, bool success) {
640 swap_set_state(s, SWAP_ACTIVE);
643 static void swap_enter_signal(Swap *s, SwapState state, bool success) {
646 bool wait_for_exit = false;
653 if (s->exec_context.kill_mode != KILL_NONE) {
654 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
655 state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
657 if (s->control_pid > 0) {
658 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
660 s->control_pid, sig) < 0 && errno != ESRCH)
662 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
664 wait_for_exit = true;
667 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
669 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
674 /* Exclude the control pid from being killed via the cgroup */
675 if (s->control_pid > 0)
676 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
679 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
680 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
681 log_warning("Failed to kill control group: %s", strerror(-r));
683 wait_for_exit = true;
690 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
693 swap_set_state(s, state);
695 swap_enter_dead(s, true);
700 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
702 swap_enter_dead(s, false);
708 static void swap_enter_activating(Swap *s) {
713 s->control_command_id = SWAP_EXEC_ACTIVATE;
714 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
716 if (s->from_fragment)
717 priority = s->parameters_fragment.priority;
718 else if (s->from_etc_fstab)
719 priority = s->parameters_etc_fstab.priority;
726 snprintf(p, sizeof(p), "%i", priority);
729 r = exec_command_set(
737 r = exec_command_set(
746 swap_unwatch_control_pid(s);
748 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
751 swap_set_state(s, SWAP_ACTIVATING);
756 log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r));
757 swap_enter_dead(s, false);
760 static void swap_enter_deactivating(Swap *s, bool success) {
768 s->control_command_id = SWAP_EXEC_DEACTIVATE;
769 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
771 if ((r = exec_command_set(
778 swap_unwatch_control_pid(s);
780 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
783 swap_set_state(s, SWAP_DEACTIVATING);
788 log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r));
789 swap_enter_active(s, false);
792 static int swap_start(Unit *u) {
797 /* We cannot fulfill this request right now, try again later
800 if (s->state == SWAP_DEACTIVATING ||
801 s->state == SWAP_DEACTIVATING_SIGTERM ||
802 s->state == SWAP_DEACTIVATING_SIGKILL ||
803 s->state == SWAP_ACTIVATING_SIGTERM ||
804 s->state == SWAP_ACTIVATING_SIGKILL)
807 if (s->state == SWAP_ACTIVATING)
810 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
813 swap_enter_activating(s);
817 static int swap_stop(Unit *u) {
822 if (s->state == SWAP_DEACTIVATING ||
823 s->state == SWAP_DEACTIVATING_SIGTERM ||
824 s->state == SWAP_DEACTIVATING_SIGKILL ||
825 s->state == SWAP_ACTIVATING_SIGTERM ||
826 s->state == SWAP_ACTIVATING_SIGKILL)
829 assert(s->state == SWAP_ACTIVATING ||
830 s->state == SWAP_ACTIVE);
832 swap_enter_deactivating(s, true);
836 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
843 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
844 unit_serialize_item(u, f, "failure", yes_no(s->failure));
846 if (s->control_pid > 0)
847 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
849 if (s->control_command_id >= 0)
850 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
855 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
861 if (streq(key, "state")) {
864 if ((state = swap_state_from_string(value)) < 0)
865 log_debug("Failed to parse state value %s", value);
867 s->deserialized_state = state;
868 } else if (streq(key, "failure")) {
871 if ((b = parse_boolean(value)) < 0)
872 log_debug("Failed to parse failure value %s", value);
874 s->failure = b || s->failure;
876 } else if (streq(key, "control-pid")) {
879 if (parse_pid(value, &pid) < 0)
880 log_debug("Failed to parse control-pid value %s", value);
882 s->control_pid = pid;
884 } else if (streq(key, "control-command")) {
887 if ((id = swap_exec_command_from_string(value)) < 0)
888 log_debug("Failed to parse exec-command value %s", value);
890 s->control_command_id = id;
891 s->control_command = s->exec_command + id;
895 log_debug("Unknown serialization key '%s'", key);
900 static UnitActiveState swap_active_state(Unit *u) {
903 return state_translation_table[SWAP(u)->state];
906 static const char *swap_sub_state_to_string(Unit *u) {
909 return swap_state_to_string(SWAP(u)->state);
912 static bool swap_check_gc(Unit *u) {
917 return s->from_etc_fstab || s->from_proc_swaps;
920 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
927 if (pid != s->control_pid)
932 success = is_clean_exit(code, status);
933 s->failure = s->failure || !success;
935 if (s->control_command) {
936 exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
937 s->control_command = NULL;
938 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
941 log_full(success ? LOG_DEBUG : LOG_NOTICE,
942 "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
946 case SWAP_ACTIVATING:
947 case SWAP_ACTIVATING_SIGTERM:
948 case SWAP_ACTIVATING_SIGKILL:
951 swap_enter_active(s, true);
953 swap_enter_dead(s, false);
956 case SWAP_DEACTIVATING:
957 case SWAP_DEACTIVATING_SIGKILL:
958 case SWAP_DEACTIVATING_SIGTERM:
961 swap_enter_dead(s, true);
963 swap_enter_dead(s, false);
967 assert_not_reached("Uh, control process died at wrong time.");
970 /* Notify clients about changed exit status */
971 unit_add_to_dbus_queue(u);
973 /* Request a reload of /proc/swaps, so that following units
974 * can follow our state change */
975 u->meta.manager->request_reload = true;
978 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
982 assert(elapsed == 1);
983 assert(w == &s->timer_watch);
987 case SWAP_ACTIVATING:
988 log_warning("%s activation timed out. Stopping.", u->meta.id);
989 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false);
992 case SWAP_DEACTIVATING:
993 log_warning("%s deactivation timed out. Stopping.", u->meta.id);
994 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false);
997 case SWAP_ACTIVATING_SIGTERM:
998 log_warning("%s activation timed out. Killing.", u->meta.id);
999 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
1002 case SWAP_DEACTIVATING_SIGTERM:
1003 log_warning("%s deactivation timed out. Killing.", u->meta.id);
1004 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
1007 case SWAP_ACTIVATING_SIGKILL:
1008 case SWAP_DEACTIVATING_SIGKILL:
1009 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id);
1010 swap_enter_dead(s, false);
1014 assert_not_reached("Timeout at wrong time.");
1018 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1024 rewind(m->proc_swaps);
1026 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1029 char *dev = NULL, *d;
1032 if ((k = fscanf(m->proc_swaps,
1033 "%ms " /* device/file */
1034 "%*s " /* type of swap */
1035 "%*s " /* swap size */
1037 "%i\n", /* priority */
1038 &dev, &prio)) != 2) {
1043 log_warning("Failed to parse /proc/swaps:%u.", i);
1054 k = swap_process_new_swap(m, d, prio, set_flags);
1064 int swap_dispatch_reload(Manager *m) {
1065 /* This function should go as soon as the kernel properly notifies us */
1067 if (_likely_(!m->request_reload))
1070 m->request_reload = false;
1072 return swap_fd_event(m, EPOLLPRI);
1075 int swap_fd_event(Manager *m, int events) {
1080 assert(events & EPOLLPRI);
1082 if ((r == swap_load_proc_swaps(m, true)) < 0) {
1083 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1085 /* Reset flags, just in case, for late calls */
1086 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1087 Swap *swap = (Swap*) meta;
1089 swap->is_active = swap->just_activated = false;
1095 manager_dispatch_load_queue(m);
1097 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1098 Swap *swap = (Swap*) meta;
1100 if (!swap->is_active) {
1101 /* This has just been deactivated */
1103 swap->from_proc_swaps = false;
1104 swap_unset_proc_swaps(swap);
1106 switch (swap->state) {
1109 swap_enter_dead(swap, true);
1113 swap_set_state(swap, swap->state);
1117 } else if (swap->just_activated) {
1119 /* New swap entry */
1121 switch (swap->state) {
1125 swap_enter_active(swap, true);
1129 /* Nothing really changed, but let's
1130 * issue an notification call
1131 * nonetheless, in case somebody is
1132 * waiting for this. */
1133 swap_set_state(swap, swap->state);
1138 /* Reset the flags for later calls */
1139 swap->is_active = swap->just_activated = false;
1145 static Unit *swap_following(Unit *u) {
1147 Swap *other, *first = NULL;
1151 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1154 /* Make everybody follow the unit that's named after the swap
1155 * device in the kernel */
1157 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1158 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1161 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1162 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1171 static int swap_following_set(Unit *u, Set **_set) {
1180 if (LIST_JUST_US(same_proc_swaps, s)) {
1185 if (!(set = set_new(NULL, NULL)))
1188 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1189 if ((r = set_put(set, other)) < 0)
1192 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1193 if ((r = set_put(set, other)) < 0)
1204 static void swap_shutdown(Manager *m) {
1207 if (m->proc_swaps) {
1208 fclose(m->proc_swaps);
1209 m->proc_swaps = NULL;
1212 hashmap_free(m->swaps_by_proc_swaps);
1213 m->swaps_by_proc_swaps = NULL;
1216 static int swap_enumerate(Manager *m) {
1218 struct epoll_event ev;
1221 if (!m->proc_swaps) {
1222 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1225 m->swap_watch.type = WATCH_SWAP;
1226 m->swap_watch.fd = fileno(m->proc_swaps);
1229 ev.events = EPOLLPRI;
1230 ev.data.ptr = &m->swap_watch;
1232 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1236 /* We rely on mount.c to load /etc/fstab for us */
1238 if ((r = swap_load_proc_swaps(m, false)) < 0)
1244 static void swap_reset_failed(Unit *u) {
1249 if (s->state == SWAP_FAILED)
1250 swap_set_state(s, SWAP_DEAD);
1255 static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1258 Set *pid_set = NULL;
1262 if (who == KILL_MAIN) {
1263 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1267 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1268 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1272 if (s->control_pid > 0)
1273 if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
1276 if (mode == KILL_CONTROL_GROUP) {
1279 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1282 /* Exclude the control pid from being killed via the cgroup */
1283 if (s->control_pid > 0)
1284 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
1289 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0)
1290 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1301 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1302 [SWAP_DEAD] = "dead",
1303 [SWAP_ACTIVATING] = "activating",
1304 [SWAP_ACTIVE] = "active",
1305 [SWAP_DEACTIVATING] = "deactivating",
1306 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1307 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1308 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1309 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1310 [SWAP_FAILED] = "failed"
1313 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1315 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1316 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1317 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1320 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1322 const UnitVTable swap_vtable = {
1326 .no_instances = true,
1328 .show_status = true,
1334 .coldplug = swap_coldplug,
1338 .start = swap_start,
1343 .serialize = swap_serialize,
1344 .deserialize_item = swap_deserialize_item,
1346 .active_state = swap_active_state,
1347 .sub_state_to_string = swap_sub_state_to_string,
1349 .check_gc = swap_check_gc,
1351 .sigchld_event = swap_sigchld_event,
1352 .timer_event = swap_timer_event,
1354 .reset_failed = swap_reset_failed,
1356 .bus_interface = "org.freedesktop.systemd1.Swap",
1357 .bus_message_handler = bus_swap_message_handler,
1358 .bus_invalidating_properties = bus_swap_invalidating_properties,
1360 .following = swap_following,
1361 .following_set = swap_following_set,
1363 .enumerate = swap_enumerate,
1364 .shutdown = swap_shutdown