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"
41 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
42 [SWAP_DEAD] = UNIT_INACTIVE,
43 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
44 [SWAP_ACTIVE] = UNIT_ACTIVE,
45 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
46 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
47 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
48 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
49 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
50 [SWAP_FAILED] = UNIT_FAILED
53 static void swap_unset_proc_swaps(Swap *s) {
58 if (!s->parameters_proc_swaps.what)
61 /* Remove this unit from the chain of swaps which share the
62 * same kernel swap device. */
64 first = hashmap_get(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
65 LIST_REMOVE(Swap, same_proc_swaps, first, s);
68 hashmap_remove_and_replace(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
70 hashmap_remove(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
72 free(s->parameters_proc_swaps.what);
73 s->parameters_proc_swaps.what = NULL;
76 static void swap_init(Unit *u) {
80 assert(s->meta.load_state == UNIT_STUB);
82 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
84 exec_context_init(&s->exec_context);
85 s->exec_context.std_output = EXEC_OUTPUT_KMSG;
87 s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
89 s->timer_watch.type = WATCH_INVALID;
91 s->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
94 static void swap_unwatch_control_pid(Swap *s) {
97 if (s->control_pid <= 0)
100 unit_unwatch_pid(UNIT(s), s->control_pid);
104 static void swap_done(Unit *u) {
109 swap_unset_proc_swaps(s);
114 free(s->parameters_etc_fstab.what);
115 free(s->parameters_fragment.what);
116 s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL;
118 exec_context_done(&s->exec_context);
119 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
120 s->control_command = NULL;
122 swap_unwatch_control_pid(s);
124 unit_unwatch_timer(u, &s->timer_watch);
127 int swap_add_one_mount_link(Swap *s, Mount *m) {
133 if (s->meta.load_state != UNIT_LOADED ||
134 m->meta.load_state != UNIT_LOADED)
137 if (is_device_path(s->what))
140 if (!path_startswith(s->what, m->where))
143 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
149 static int swap_add_mount_links(Swap *s) {
155 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
156 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
162 static int swap_add_target_links(Swap *s) {
169 if (s->from_fragment)
170 p = &s->parameters_fragment;
171 else if (s->from_etc_fstab)
172 p = &s->parameters_etc_fstab;
176 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
181 (p->handle || s->meta.manager->swap_auto) &&
183 s->meta.manager->running_as == MANAGER_SYSTEM)
184 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
187 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
190 static int swap_add_device_links(Swap *s) {
198 if (s->from_fragment)
199 p = &s->parameters_fragment;
200 else if (s->from_etc_fstab)
201 p = &s->parameters_etc_fstab;
205 if (is_device_path(s->what))
206 return unit_add_node_link(UNIT(s), s->what,
207 !p->noauto && p->nofail &&
208 s->meta.manager->running_as == MANAGER_SYSTEM);
210 /* File based swap devices need to be ordered after
211 * remount-rootfs.service, since they might need a
212 * writable file system. */
213 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true);
216 static int swap_add_default_dependencies(Swap *s) {
221 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
223 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
230 static int swap_verify(Swap *s) {
234 if (s->meta.load_state != UNIT_LOADED)
237 if (!(e = unit_name_from_path(s->what, ".swap")))
240 b = unit_has_name(UNIT(s), e);
244 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
248 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
249 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
256 static int swap_load(Unit *u) {
261 assert(u->meta.load_state == UNIT_STUB);
263 /* Load a .swap file */
264 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
267 if (u->meta.load_state == UNIT_LOADED) {
268 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
271 if (s->meta.fragment_path)
272 s->from_fragment = true;
275 if (s->parameters_fragment.what)
276 s->what = strdup(s->parameters_fragment.what);
277 else if (s->parameters_etc_fstab.what)
278 s->what = strdup(s->parameters_etc_fstab.what);
279 else if (s->parameters_proc_swaps.what)
280 s->what = strdup(s->parameters_proc_swaps.what);
282 s->what = unit_name_to_path(u->meta.id);
288 path_kill_slashes(s->what);
290 if (!s->meta.description)
291 if ((r = unit_set_description(u, s->what)) < 0)
294 if ((r = swap_add_device_links(s)) < 0)
297 if ((r = swap_add_mount_links(s)) < 0)
300 if ((r = swap_add_target_links(s)) < 0)
303 if ((r = unit_add_default_cgroups(u)) < 0)
306 if (s->meta.default_dependencies)
307 if ((r = swap_add_default_dependencies(s)) < 0)
311 return swap_verify(s);
317 const char *what_proc_swaps,
325 char *e = NULL, *wp = NULL;
333 if (!(e = unit_name_from_path(what, ".swap")))
336 u = manager_get_unit(m, e);
338 if (what_proc_swaps &&
340 SWAP(u)->from_proc_swaps &&
341 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
347 if (!(u = unit_new(m))) {
352 if ((r = unit_add_name(u, e)) < 0)
355 if (!(SWAP(u)->what = strdup(what))) {
360 unit_add_to_load_queue(u);
364 if (what_proc_swaps) {
367 p = &SWAP(u)->parameters_proc_swaps;
370 if (!(wp = strdup(what_proc_swaps))) {
375 if (!m->swaps_by_proc_swaps)
376 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
384 first = hashmap_get(m->swaps_by_proc_swaps, wp);
385 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
387 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
392 SWAP(u)->is_active = true;
393 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
396 SWAP(u)->from_proc_swaps = true;
399 p = &SWAP(u)->parameters_etc_fstab;
401 if (!(wp = strdup(what))) {
409 SWAP(u)->from_etc_fstab = true;
412 p->priority = priority;
417 unit_add_to_dbus_queue(u);
424 log_warning("Failed to load swap unit: %s", strerror(-r));
435 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
441 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
442 struct udev_device *d;
444 struct udev_list_entry *item = NULL, *first = NULL;
446 /* So this is a proper swap device. Create swap units
447 * for all names this swap device is known under */
449 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
452 if ((dn = udev_device_get_devnode(d)))
453 r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
455 /* Add additional units for all symlinks */
456 first = udev_device_get_devlinks_list_entry(d);
457 udev_list_entry_foreach(item, first) {
460 /* Don't bother with the /dev/block links */
461 p = udev_list_entry_get_name(item);
463 if (path_startswith(p, "/dev/block/"))
466 if (stat(p, &st) >= 0)
467 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
470 if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
474 udev_device_unref(d);
477 if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
483 static void swap_set_state(Swap *s, SwapState state) {
488 old_state = s->state;
491 if (state != SWAP_ACTIVATING &&
492 state != SWAP_ACTIVATING_SIGTERM &&
493 state != SWAP_ACTIVATING_SIGKILL &&
494 state != SWAP_DEACTIVATING &&
495 state != SWAP_DEACTIVATING_SIGTERM &&
496 state != SWAP_DEACTIVATING_SIGKILL) {
497 unit_unwatch_timer(UNIT(s), &s->timer_watch);
498 swap_unwatch_control_pid(s);
499 s->control_command = NULL;
500 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
503 if (state != old_state)
504 log_debug("%s changed %s -> %s",
506 swap_state_to_string(old_state),
507 swap_state_to_string(state));
509 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
512 static int swap_coldplug(Unit *u) {
514 SwapState new_state = SWAP_DEAD;
518 assert(s->state == SWAP_DEAD);
520 if (s->deserialized_state != s->state)
521 new_state = s->deserialized_state;
522 else if (s->from_proc_swaps)
523 new_state = SWAP_ACTIVE;
525 if (new_state != s->state) {
527 if (new_state == SWAP_ACTIVATING ||
528 new_state == SWAP_ACTIVATING_SIGTERM ||
529 new_state == SWAP_ACTIVATING_SIGKILL ||
530 new_state == SWAP_DEACTIVATING ||
531 new_state == SWAP_DEACTIVATING_SIGTERM ||
532 new_state == SWAP_DEACTIVATING_SIGKILL) {
534 if (s->control_pid <= 0)
537 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
540 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
544 swap_set_state(s, new_state);
550 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
557 if (s->from_proc_swaps)
558 p = &s->parameters_proc_swaps;
559 else if (s->from_fragment)
560 p = &s->parameters_fragment;
562 p = &s->parameters_etc_fstab;
571 "%sFrom /etc/fstab: %s\n"
572 "%sFrom /proc/swaps: %s\n"
573 "%sFrom fragment: %s\n",
574 prefix, swap_state_to_string(s->state),
577 prefix, yes_no(p->noauto),
578 prefix, yes_no(p->nofail),
579 prefix, yes_no(p->handle),
580 prefix, yes_no(s->from_etc_fstab),
581 prefix, yes_no(s->from_proc_swaps),
582 prefix, yes_no(s->from_fragment));
584 if (s->control_pid > 0)
586 "%sControl PID: %lu\n",
587 prefix, (unsigned long) s->control_pid);
589 exec_context_dump(&s->exec_context, f, prefix);
592 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
600 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
603 if ((r = exec_spawn(c,
607 s->meta.manager->environment,
611 s->meta.manager->confirm_spawn,
612 s->meta.cgroup_bondings,
616 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
617 /* FIXME: we need to do something here */
625 unit_unwatch_timer(UNIT(s), &s->timer_watch);
630 static void swap_enter_dead(Swap *s, bool success) {
636 swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD);
639 static void swap_enter_active(Swap *s, bool success) {
645 swap_set_state(s, SWAP_ACTIVE);
648 static void swap_enter_signal(Swap *s, SwapState state, bool success) {
651 bool wait_for_exit = false;
658 if (s->exec_context.kill_mode != KILL_NONE) {
659 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
660 state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
662 if (s->control_pid > 0) {
663 if (kill_and_sigcont(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
665 s->control_pid, sig) < 0 && errno != ESRCH)
667 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
669 wait_for_exit = true;
672 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
674 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
679 /* Exclude the control pid from being killed via the cgroup */
680 if (s->control_pid > 0)
681 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
684 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
685 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
686 log_warning("Failed to kill control group: %s", strerror(-r));
688 wait_for_exit = true;
695 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
698 swap_set_state(s, state);
700 swap_enter_dead(s, true);
705 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
707 swap_enter_dead(s, false);
713 static void swap_enter_activating(Swap *s) {
718 s->control_command_id = SWAP_EXEC_ACTIVATE;
719 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
721 if (s->from_fragment)
722 priority = s->parameters_fragment.priority;
723 else if (s->from_etc_fstab)
724 priority = s->parameters_etc_fstab.priority;
731 snprintf(p, sizeof(p), "%i", priority);
734 r = exec_command_set(
742 r = exec_command_set(
751 swap_unwatch_control_pid(s);
753 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
756 swap_set_state(s, SWAP_ACTIVATING);
761 log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r));
762 swap_enter_dead(s, false);
765 static void swap_enter_deactivating(Swap *s, bool success) {
773 s->control_command_id = SWAP_EXEC_DEACTIVATE;
774 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
776 if ((r = exec_command_set(
783 swap_unwatch_control_pid(s);
785 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
788 swap_set_state(s, SWAP_DEACTIVATING);
793 log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r));
794 swap_enter_active(s, false);
797 static int swap_start(Unit *u) {
802 /* We cannot fulfill this request right now, try again later
805 if (s->state == SWAP_DEACTIVATING ||
806 s->state == SWAP_DEACTIVATING_SIGTERM ||
807 s->state == SWAP_DEACTIVATING_SIGKILL ||
808 s->state == SWAP_ACTIVATING_SIGTERM ||
809 s->state == SWAP_ACTIVATING_SIGKILL)
812 if (s->state == SWAP_ACTIVATING)
815 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
818 swap_enter_activating(s);
822 static int swap_stop(Unit *u) {
827 if (s->state == SWAP_DEACTIVATING ||
828 s->state == SWAP_DEACTIVATING_SIGTERM ||
829 s->state == SWAP_DEACTIVATING_SIGKILL ||
830 s->state == SWAP_ACTIVATING_SIGTERM ||
831 s->state == SWAP_ACTIVATING_SIGKILL)
834 assert(s->state == SWAP_ACTIVATING ||
835 s->state == SWAP_ACTIVE);
837 swap_enter_deactivating(s, true);
841 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
848 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
849 unit_serialize_item(u, f, "failure", yes_no(s->failure));
851 if (s->control_pid > 0)
852 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
854 if (s->control_command_id >= 0)
855 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
860 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
866 if (streq(key, "state")) {
869 if ((state = swap_state_from_string(value)) < 0)
870 log_debug("Failed to parse state value %s", value);
872 s->deserialized_state = state;
873 } else if (streq(key, "failure")) {
876 if ((b = parse_boolean(value)) < 0)
877 log_debug("Failed to parse failure value %s", value);
879 s->failure = b || s->failure;
881 } else if (streq(key, "control-pid")) {
884 if (parse_pid(value, &pid) < 0)
885 log_debug("Failed to parse control-pid value %s", value);
887 s->control_pid = pid;
889 } else if (streq(key, "control-command")) {
892 if ((id = swap_exec_command_from_string(value)) < 0)
893 log_debug("Failed to parse exec-command value %s", value);
895 s->control_command_id = id;
896 s->control_command = s->exec_command + id;
900 log_debug("Unknown serialization key '%s'", key);
905 static UnitActiveState swap_active_state(Unit *u) {
908 return state_translation_table[SWAP(u)->state];
911 static const char *swap_sub_state_to_string(Unit *u) {
914 return swap_state_to_string(SWAP(u)->state);
917 static bool swap_check_gc(Unit *u) {
922 return s->from_etc_fstab || s->from_proc_swaps;
925 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
932 if (pid != s->control_pid)
937 success = is_clean_exit(code, status);
938 s->failure = s->failure || !success;
940 if (s->control_command) {
941 exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
942 s->control_command = NULL;
943 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
946 log_full(success ? LOG_DEBUG : LOG_NOTICE,
947 "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
951 case SWAP_ACTIVATING:
952 case SWAP_ACTIVATING_SIGTERM:
953 case SWAP_ACTIVATING_SIGKILL:
956 swap_enter_active(s, true);
958 swap_enter_dead(s, false);
961 case SWAP_DEACTIVATING:
962 case SWAP_DEACTIVATING_SIGKILL:
963 case SWAP_DEACTIVATING_SIGTERM:
966 swap_enter_dead(s, true);
968 swap_enter_dead(s, false);
972 assert_not_reached("Uh, control process died at wrong time.");
975 /* Notify clients about changed exit status */
976 unit_add_to_dbus_queue(u);
978 /* Request a reload of /proc/swaps, so that following units
979 * can follow our state change */
980 u->meta.manager->request_reload = true;
983 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
987 assert(elapsed == 1);
988 assert(w == &s->timer_watch);
992 case SWAP_ACTIVATING:
993 log_warning("%s activation timed out. Stopping.", u->meta.id);
994 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false);
997 case SWAP_DEACTIVATING:
998 log_warning("%s deactivation timed out. Stopping.", u->meta.id);
999 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false);
1002 case SWAP_ACTIVATING_SIGTERM:
1003 if (s->exec_context.send_sigkill) {
1004 log_warning("%s activation timed out. Killing.", u->meta.id);
1005 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
1007 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1008 swap_enter_dead(s, false);
1012 case SWAP_DEACTIVATING_SIGTERM:
1013 if (s->exec_context.send_sigkill) {
1014 log_warning("%s deactivation timed out. Killing.", u->meta.id);
1015 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
1017 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1018 swap_enter_dead(s, false);
1022 case SWAP_ACTIVATING_SIGKILL:
1023 case SWAP_DEACTIVATING_SIGKILL:
1024 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id);
1025 swap_enter_dead(s, false);
1029 assert_not_reached("Timeout at wrong time.");
1033 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1039 rewind(m->proc_swaps);
1041 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1044 char *dev = NULL, *d;
1047 if ((k = fscanf(m->proc_swaps,
1048 "%ms " /* device/file */
1049 "%*s " /* type of swap */
1050 "%*s " /* swap size */
1052 "%i\n", /* priority */
1053 &dev, &prio)) != 2) {
1058 log_warning("Failed to parse /proc/swaps:%u.", i);
1069 k = swap_process_new_swap(m, d, prio, set_flags);
1079 int swap_dispatch_reload(Manager *m) {
1080 /* This function should go as soon as the kernel properly notifies us */
1082 if (_likely_(!m->request_reload))
1085 m->request_reload = false;
1087 return swap_fd_event(m, EPOLLPRI);
1090 int swap_fd_event(Manager *m, int events) {
1095 assert(events & EPOLLPRI);
1097 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1098 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1100 /* Reset flags, just in case, for late calls */
1101 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1102 Swap *swap = (Swap*) meta;
1104 swap->is_active = swap->just_activated = false;
1110 manager_dispatch_load_queue(m);
1112 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1113 Swap *swap = (Swap*) meta;
1115 if (!swap->is_active) {
1116 /* This has just been deactivated */
1118 swap->from_proc_swaps = false;
1119 swap_unset_proc_swaps(swap);
1121 switch (swap->state) {
1124 swap_enter_dead(swap, true);
1128 swap_set_state(swap, swap->state);
1132 } else if (swap->just_activated) {
1134 /* New swap entry */
1136 switch (swap->state) {
1140 swap_enter_active(swap, true);
1144 /* Nothing really changed, but let's
1145 * issue an notification call
1146 * nonetheless, in case somebody is
1147 * waiting for this. */
1148 swap_set_state(swap, swap->state);
1153 /* Reset the flags for later calls */
1154 swap->is_active = swap->just_activated = false;
1160 static Unit *swap_following(Unit *u) {
1162 Swap *other, *first = NULL;
1166 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1169 /* Make everybody follow the unit that's named after the swap
1170 * device in the kernel */
1172 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1173 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1176 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1177 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1186 static int swap_following_set(Unit *u, Set **_set) {
1195 if (LIST_JUST_US(same_proc_swaps, s)) {
1200 if (!(set = set_new(NULL, NULL)))
1203 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1204 if ((r = set_put(set, other)) < 0)
1207 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1208 if ((r = set_put(set, other)) < 0)
1219 static void swap_shutdown(Manager *m) {
1222 if (m->proc_swaps) {
1223 fclose(m->proc_swaps);
1224 m->proc_swaps = NULL;
1227 hashmap_free(m->swaps_by_proc_swaps);
1228 m->swaps_by_proc_swaps = NULL;
1231 static int swap_enumerate(Manager *m) {
1233 struct epoll_event ev;
1236 if (!m->proc_swaps) {
1237 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1240 m->swap_watch.type = WATCH_SWAP;
1241 m->swap_watch.fd = fileno(m->proc_swaps);
1244 ev.events = EPOLLPRI;
1245 ev.data.ptr = &m->swap_watch;
1247 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1251 /* We rely on mount.c to load /etc/fstab for us */
1253 if ((r = swap_load_proc_swaps(m, false)) < 0)
1259 static void swap_reset_failed(Unit *u) {
1264 if (s->state == SWAP_FAILED)
1265 swap_set_state(s, SWAP_DEAD);
1270 static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1273 Set *pid_set = NULL;
1277 if (who == KILL_MAIN) {
1278 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1282 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1283 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1287 if (s->control_pid > 0)
1288 if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
1291 if (mode == KILL_CONTROL_GROUP) {
1294 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1297 /* Exclude the control pid from being killed via the cgroup */
1298 if (s->control_pid > 0)
1299 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
1304 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
1305 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1316 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1317 [SWAP_DEAD] = "dead",
1318 [SWAP_ACTIVATING] = "activating",
1319 [SWAP_ACTIVE] = "active",
1320 [SWAP_DEACTIVATING] = "deactivating",
1321 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1322 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1323 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1324 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1325 [SWAP_FAILED] = "failed"
1328 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1330 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1331 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1332 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1335 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1337 const UnitVTable swap_vtable = {
1341 .no_instances = true,
1343 .show_status = true,
1349 .coldplug = swap_coldplug,
1353 .start = swap_start,
1358 .serialize = swap_serialize,
1359 .deserialize_item = swap_deserialize_item,
1361 .active_state = swap_active_state,
1362 .sub_state_to_string = swap_sub_state_to_string,
1364 .check_gc = swap_check_gc,
1366 .sigchld_event = swap_sigchld_event,
1367 .timer_event = swap_timer_event,
1369 .reset_failed = swap_reset_failed,
1371 .bus_interface = "org.freedesktop.systemd1.Swap",
1372 .bus_message_handler = bus_swap_message_handler,
1373 .bus_invalidating_properties = bus_swap_invalidating_properties,
1375 .following = swap_following,
1376 .following_set = swap_following_set,
1378 .enumerate = swap_enumerate,
1379 .shutdown = swap_shutdown