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 return unit_add_node_link(UNIT(s), s->what,
203 !p->noauto && p->nofail &&
204 s->meta.manager->running_as == MANAGER_SYSTEM);
207 static int swap_add_default_dependencies(Swap *s) {
212 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
214 if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
217 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
224 static int swap_verify(Swap *s) {
228 if (s->meta.load_state != UNIT_LOADED)
231 if (!(e = unit_name_from_path(s->what, ".swap")))
234 b = unit_has_name(UNIT(s), e);
238 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
242 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
243 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
250 static int swap_load(Unit *u) {
255 assert(u->meta.load_state == UNIT_STUB);
257 /* Load a .swap file */
258 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
261 if (u->meta.load_state == UNIT_LOADED) {
263 if (s->meta.fragment_path)
264 s->from_fragment = true;
267 if (s->parameters_fragment.what)
268 s->what = strdup(s->parameters_fragment.what);
269 else if (s->parameters_etc_fstab.what)
270 s->what = strdup(s->parameters_etc_fstab.what);
271 else if (s->parameters_proc_swaps.what)
272 s->what = strdup(s->parameters_proc_swaps.what);
274 s->what = unit_name_to_path(u->meta.id);
280 path_kill_slashes(s->what);
282 if (!s->meta.description)
283 if ((r = unit_set_description(u, s->what)) < 0)
286 if ((r = swap_add_device_links(s)) < 0)
289 if ((r = swap_add_mount_links(s)) < 0)
292 if ((r = swap_add_target_links(s)) < 0)
295 if ((r = unit_add_default_cgroup(u)) < 0)
298 if (s->meta.default_dependencies)
299 if ((r = swap_add_default_dependencies(s)) < 0)
303 return swap_verify(s);
309 const char *what_proc_swaps,
317 char *e = NULL, *wp = NULL;
325 if (!(e = unit_name_from_path(what, ".swap")))
328 u = manager_get_unit(m, e);
330 if (what_proc_swaps &&
332 SWAP(u)->from_proc_swaps &&
333 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
339 if (!(u = unit_new(m))) {
344 if ((r = unit_add_name(u, e)) < 0)
347 if (!(SWAP(u)->what = strdup(what))) {
352 unit_add_to_load_queue(u);
356 if (what_proc_swaps) {
359 p = &SWAP(u)->parameters_proc_swaps;
362 if (!(wp = strdup(what_proc_swaps))) {
367 if (!m->swaps_by_proc_swaps)
368 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
376 first = hashmap_get(m->swaps_by_proc_swaps, wp);
377 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
379 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
384 SWAP(u)->is_active = true;
385 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
388 SWAP(u)->from_proc_swaps = true;
391 p = &SWAP(u)->parameters_etc_fstab;
393 if (!(wp = strdup(what))) {
401 SWAP(u)->from_etc_fstab = true;
404 p->priority = priority;
409 unit_add_to_dbus_queue(u);
416 log_warning("Failed to load swap unit: %s", strerror(-r));
427 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
433 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
434 struct udev_device *d;
436 struct udev_list_entry *item = NULL, *first = NULL;
438 /* So this is a proper swap device. Create swap units
439 * for all names this swap device is known under */
441 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
444 if ((dn = udev_device_get_devnode(d)))
445 r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
447 /* Add additional units for all symlinks */
448 first = udev_device_get_devlinks_list_entry(d);
449 udev_list_entry_foreach(item, first) {
452 /* Don't bother with the /dev/block links */
453 p = udev_list_entry_get_name(item);
455 if (path_startswith(p, "/dev/block/"))
458 if (stat(p, &st) >= 0)
459 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
462 if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
466 udev_device_unref(d);
469 if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
475 static void swap_set_state(Swap *s, SwapState state) {
480 old_state = s->state;
483 if (state != SWAP_ACTIVATING &&
484 state != SWAP_ACTIVATING_SIGTERM &&
485 state != SWAP_ACTIVATING_SIGKILL &&
486 state != SWAP_DEACTIVATING &&
487 state != SWAP_DEACTIVATING_SIGTERM &&
488 state != SWAP_DEACTIVATING_SIGKILL) {
489 unit_unwatch_timer(UNIT(s), &s->timer_watch);
490 swap_unwatch_control_pid(s);
491 s->control_command = NULL;
492 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
495 if (state != old_state)
496 log_debug("%s changed %s -> %s",
498 swap_state_to_string(old_state),
499 swap_state_to_string(state));
501 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
504 static int swap_coldplug(Unit *u) {
506 SwapState new_state = SWAP_DEAD;
510 assert(s->state == SWAP_DEAD);
512 if (s->deserialized_state != s->state)
513 new_state = s->deserialized_state;
514 else if (s->from_proc_swaps)
515 new_state = SWAP_ACTIVE;
517 if (new_state != s->state) {
519 if (new_state == SWAP_ACTIVATING ||
520 new_state == SWAP_ACTIVATING_SIGTERM ||
521 new_state == SWAP_ACTIVATING_SIGKILL ||
522 new_state == SWAP_DEACTIVATING ||
523 new_state == SWAP_DEACTIVATING_SIGTERM ||
524 new_state == SWAP_DEACTIVATING_SIGKILL) {
526 if (s->control_pid <= 0)
529 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
532 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
536 swap_set_state(s, new_state);
542 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
549 if (s->from_proc_swaps)
550 p = &s->parameters_proc_swaps;
551 else if (s->from_fragment)
552 p = &s->parameters_fragment;
554 p = &s->parameters_etc_fstab;
563 "%sFrom /etc/fstab: %s\n"
564 "%sFrom /proc/swaps: %s\n"
565 "%sFrom fragment: %s\n",
566 prefix, swap_state_to_string(s->state),
569 prefix, yes_no(p->noauto),
570 prefix, yes_no(p->nofail),
571 prefix, yes_no(p->handle),
572 prefix, yes_no(s->from_etc_fstab),
573 prefix, yes_no(s->from_proc_swaps),
574 prefix, yes_no(s->from_fragment));
576 if (s->control_pid > 0)
578 "%sControl PID: %lu\n",
579 prefix, (unsigned long) s->control_pid);
581 exec_context_dump(&s->exec_context, f, prefix);
584 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
592 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
595 if ((r = exec_spawn(c,
599 s->meta.manager->environment,
603 s->meta.manager->confirm_spawn,
604 s->meta.cgroup_bondings,
608 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
609 /* FIXME: we need to do something here */
617 unit_unwatch_timer(UNIT(s), &s->timer_watch);
622 static void swap_enter_dead(Swap *s, bool success) {
628 swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD);
631 static void swap_enter_active(Swap *s, bool success) {
637 swap_set_state(s, SWAP_ACTIVE);
640 static void swap_enter_signal(Swap *s, SwapState state, bool success) {
643 bool wait_for_exit = false;
650 if (s->exec_context.kill_mode != KILL_NONE) {
651 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
652 state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
654 if (s->control_pid > 0) {
655 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
657 s->control_pid, sig) < 0 && errno != ESRCH)
659 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
661 wait_for_exit = true;
664 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
666 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
671 /* Exclude the control pid from being killed via the cgroup */
672 if (s->control_pid > 0)
673 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
676 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
677 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
678 log_warning("Failed to kill control group: %s", strerror(-r));
680 wait_for_exit = true;
687 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
690 swap_set_state(s, state);
692 swap_enter_dead(s, true);
697 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
699 swap_enter_dead(s, false);
705 static void swap_enter_activating(Swap *s) {
710 s->control_command_id = SWAP_EXEC_ACTIVATE;
711 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
713 if (s->from_fragment)
714 priority = s->parameters_fragment.priority;
715 else if (s->from_etc_fstab)
716 priority = s->parameters_etc_fstab.priority;
723 snprintf(p, sizeof(p), "%i", priority);
726 r = exec_command_set(
734 r = exec_command_set(
743 swap_unwatch_control_pid(s);
745 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
748 swap_set_state(s, SWAP_ACTIVATING);
753 log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r));
754 swap_enter_dead(s, false);
757 static void swap_enter_deactivating(Swap *s, bool success) {
765 s->control_command_id = SWAP_EXEC_DEACTIVATE;
766 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
768 if ((r = exec_command_set(
775 swap_unwatch_control_pid(s);
777 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
780 swap_set_state(s, SWAP_DEACTIVATING);
785 log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r));
786 swap_enter_active(s, false);
789 static int swap_start(Unit *u) {
794 /* We cannot fulfill this request right now, try again later
797 if (s->state == SWAP_DEACTIVATING ||
798 s->state == SWAP_DEACTIVATING_SIGTERM ||
799 s->state == SWAP_DEACTIVATING_SIGKILL ||
800 s->state == SWAP_ACTIVATING_SIGTERM ||
801 s->state == SWAP_ACTIVATING_SIGKILL)
804 if (s->state == SWAP_ACTIVATING)
807 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
810 swap_enter_activating(s);
814 static int swap_stop(Unit *u) {
819 if (s->state == SWAP_DEACTIVATING ||
820 s->state == SWAP_DEACTIVATING_SIGTERM ||
821 s->state == SWAP_DEACTIVATING_SIGKILL ||
822 s->state == SWAP_ACTIVATING_SIGTERM ||
823 s->state == SWAP_ACTIVATING_SIGKILL)
826 assert(s->state == SWAP_ACTIVATING ||
827 s->state == SWAP_ACTIVE);
829 swap_enter_deactivating(s, true);
833 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
840 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
841 unit_serialize_item(u, f, "failure", yes_no(s->failure));
843 if (s->control_pid > 0)
844 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
846 if (s->control_command_id >= 0)
847 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
852 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
858 if (streq(key, "state")) {
861 if ((state = swap_state_from_string(value)) < 0)
862 log_debug("Failed to parse state value %s", value);
864 s->deserialized_state = state;
865 } else if (streq(key, "failure")) {
868 if ((b = parse_boolean(value)) < 0)
869 log_debug("Failed to parse failure value %s", value);
871 s->failure = b || s->failure;
873 } else if (streq(key, "control-pid")) {
876 if (parse_pid(value, &pid) < 0)
877 log_debug("Failed to parse control-pid value %s", value);
879 s->control_pid = pid;
881 } else if (streq(key, "control-command")) {
884 if ((id = swap_exec_command_from_string(value)) < 0)
885 log_debug("Failed to parse exec-command value %s", value);
887 s->control_command_id = id;
888 s->control_command = s->exec_command + id;
892 log_debug("Unknown serialization key '%s'", key);
897 static UnitActiveState swap_active_state(Unit *u) {
900 return state_translation_table[SWAP(u)->state];
903 static const char *swap_sub_state_to_string(Unit *u) {
906 return swap_state_to_string(SWAP(u)->state);
909 static bool swap_check_gc(Unit *u) {
914 return s->from_etc_fstab || s->from_proc_swaps;
917 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
924 if (pid != s->control_pid)
929 success = is_clean_exit(code, status);
930 s->failure = s->failure || !success;
932 if (s->control_command) {
933 exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
934 s->control_command = NULL;
935 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
938 log_full(success ? LOG_DEBUG : LOG_NOTICE,
939 "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
943 case SWAP_ACTIVATING:
944 case SWAP_ACTIVATING_SIGTERM:
945 case SWAP_ACTIVATING_SIGKILL:
948 swap_enter_active(s, true);
950 swap_enter_dead(s, false);
953 case SWAP_DEACTIVATING:
954 case SWAP_DEACTIVATING_SIGKILL:
955 case SWAP_DEACTIVATING_SIGTERM:
958 swap_enter_dead(s, true);
960 swap_enter_dead(s, false);
964 assert_not_reached("Uh, control process died at wrong time.");
967 /* Notify clients about changed exit status */
968 unit_add_to_dbus_queue(u);
970 /* Request a reload of /proc/swaps, so that following units
971 * can follow our state change */
972 u->meta.manager->request_reload = true;
975 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
979 assert(elapsed == 1);
980 assert(w == &s->timer_watch);
984 case SWAP_ACTIVATING:
985 log_warning("%s activation timed out. Stopping.", u->meta.id);
986 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false);
989 case SWAP_DEACTIVATING:
990 log_warning("%s deactivation timed out. Stopping.", u->meta.id);
991 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false);
994 case SWAP_ACTIVATING_SIGTERM:
995 log_warning("%s activation timed out. Killing.", u->meta.id);
996 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
999 case SWAP_DEACTIVATING_SIGTERM:
1000 log_warning("%s deactivation timed out. Killing.", u->meta.id);
1001 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
1004 case SWAP_ACTIVATING_SIGKILL:
1005 case SWAP_DEACTIVATING_SIGKILL:
1006 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id);
1007 swap_enter_dead(s, false);
1011 assert_not_reached("Timeout at wrong time.");
1015 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1021 rewind(m->proc_swaps);
1023 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1026 char *dev = NULL, *d;
1029 if ((k = fscanf(m->proc_swaps,
1030 "%ms " /* device/file */
1031 "%*s " /* type of swap */
1032 "%*s " /* swap size */
1034 "%i\n", /* priority */
1035 &dev, &prio)) != 2) {
1040 log_warning("Failed to parse /proc/swaps:%u.", i);
1051 k = swap_process_new_swap(m, d, prio, set_flags);
1061 int swap_dispatch_reload(Manager *m) {
1062 /* This function should go as soon as the kernel properly notifies us */
1064 if (_likely_(!m->request_reload))
1067 m->request_reload = false;
1069 return swap_fd_event(m, EPOLLPRI);
1072 int swap_fd_event(Manager *m, int events) {
1077 assert(events & EPOLLPRI);
1079 if ((r == swap_load_proc_swaps(m, true)) < 0) {
1080 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1082 /* Reset flags, just in case, for late calls */
1083 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1084 Swap *swap = (Swap*) meta;
1086 swap->is_active = swap->just_activated = false;
1092 manager_dispatch_load_queue(m);
1094 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1095 Swap *swap = (Swap*) meta;
1097 if (!swap->is_active) {
1098 /* This has just been deactivated */
1100 swap->from_proc_swaps = false;
1101 swap_unset_proc_swaps(swap);
1103 switch (swap->state) {
1106 swap_enter_dead(swap, true);
1110 swap_set_state(swap, swap->state);
1114 } else if (swap->just_activated) {
1116 /* New swap entry */
1118 switch (swap->state) {
1122 swap_enter_active(swap, true);
1126 /* Nothing really changed, but let's
1127 * issue an notification call
1128 * nonetheless, in case somebody is
1129 * waiting for this. */
1130 swap_set_state(swap, swap->state);
1135 /* Reset the flags for later calls */
1136 swap->is_active = swap->just_activated = false;
1142 static Unit *swap_following(Unit *u) {
1144 Swap *other, *first = NULL;
1148 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1151 /* Make everybody follow the unit that's named after the swap
1152 * device in the kernel */
1154 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1155 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1158 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1159 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1168 static void swap_shutdown(Manager *m) {
1171 if (m->proc_swaps) {
1172 fclose(m->proc_swaps);
1173 m->proc_swaps = NULL;
1176 hashmap_free(m->swaps_by_proc_swaps);
1177 m->swaps_by_proc_swaps = NULL;
1180 static int swap_enumerate(Manager *m) {
1182 struct epoll_event ev;
1185 if (!m->proc_swaps) {
1186 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1189 m->swap_watch.type = WATCH_SWAP;
1190 m->swap_watch.fd = fileno(m->proc_swaps);
1193 ev.events = EPOLLPRI;
1194 ev.data.ptr = &m->swap_watch;
1196 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1200 /* We rely on mount.c to load /etc/fstab for us */
1202 if ((r = swap_load_proc_swaps(m, false)) < 0)
1208 static void swap_reset_failed(Unit *u) {
1213 if (s->state == SWAP_FAILED)
1214 swap_set_state(s, SWAP_DEAD);
1219 static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1222 Set *pid_set = NULL;
1226 if (who == KILL_MAIN) {
1227 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1231 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1232 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1236 if (s->control_pid > 0)
1237 if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
1240 if (mode == KILL_CONTROL_GROUP) {
1243 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1246 /* Exclude the control pid from being killed via the cgroup */
1247 if (s->control_pid > 0)
1248 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
1253 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0)
1254 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1265 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1266 [SWAP_DEAD] = "dead",
1267 [SWAP_ACTIVATING] = "activating",
1268 [SWAP_ACTIVE] = "active",
1269 [SWAP_DEACTIVATING] = "deactivating",
1270 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1271 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1272 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1273 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1274 [SWAP_FAILED] = "failed"
1277 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1279 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1280 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1281 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1284 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1286 const UnitVTable swap_vtable = {
1290 .no_instances = true,
1292 .show_status = true,
1298 .coldplug = swap_coldplug,
1302 .start = swap_start,
1307 .serialize = swap_serialize,
1308 .deserialize_item = swap_deserialize_item,
1310 .active_state = swap_active_state,
1311 .sub_state_to_string = swap_sub_state_to_string,
1313 .check_gc = swap_check_gc,
1315 .sigchld_event = swap_sigchld_event,
1316 .timer_event = swap_timer_event,
1318 .reset_failed = swap_reset_failed,
1320 .bus_interface = "org.freedesktop.systemd1.Swap",
1321 .bus_message_handler = bus_swap_message_handler,
1322 .bus_invalidating_properties = bus_swap_invalidating_properties,
1324 .following = swap_following,
1326 .enumerate = swap_enumerate,
1327 .shutdown = swap_shutdown