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;
95 static void swap_unwatch_control_pid(Swap *s) {
98 if (s->control_pid <= 0)
101 unit_unwatch_pid(UNIT(s), s->control_pid);
105 static void swap_done(Unit *u) {
110 swap_unset_proc_swaps(s);
115 free(s->parameters_etc_fstab.what);
116 free(s->parameters_fragment.what);
117 s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL;
119 exec_context_done(&s->exec_context);
120 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
121 s->control_command = NULL;
123 swap_unwatch_control_pid(s);
125 unit_unwatch_timer(u, &s->timer_watch);
128 int swap_add_one_mount_link(Swap *s, Mount *m) {
134 if (s->meta.load_state != UNIT_LOADED ||
135 m->meta.load_state != UNIT_LOADED)
138 if (is_device_path(s->what))
141 if (!path_startswith(s->what, m->where))
144 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
150 static int swap_add_mount_links(Swap *s) {
156 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
157 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
163 static int swap_add_target_links(Swap *s) {
170 if (s->from_fragment)
171 p = &s->parameters_fragment;
172 else if (s->from_etc_fstab)
173 p = &s->parameters_etc_fstab;
177 if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
182 (p->handle || s->meta.manager->swap_auto) &&
184 s->meta.manager->running_as == MANAGER_SYSTEM)
185 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
188 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
191 static int swap_add_device_links(Swap *s) {
199 if (s->from_fragment)
200 p = &s->parameters_fragment;
201 else if (s->from_etc_fstab)
202 p = &s->parameters_etc_fstab;
206 if (is_device_path(s->what))
207 return unit_add_node_link(UNIT(s), s->what,
208 !p->noauto && p->nofail &&
209 s->meta.manager->running_as == MANAGER_SYSTEM);
211 /* File based swap devices need to be ordered after
212 * remount-rootfs.service, since they might need a
213 * writable file system. */
214 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true);
217 static int swap_add_default_dependencies(Swap *s) {
222 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
224 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
231 static int swap_verify(Swap *s) {
235 if (s->meta.load_state != UNIT_LOADED)
238 if (!(e = unit_name_from_path(s->what, ".swap")))
241 b = unit_has_name(UNIT(s), e);
245 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
249 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
250 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
257 static int swap_load(Unit *u) {
262 assert(u->meta.load_state == UNIT_STUB);
264 /* Load a .swap file */
265 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
268 if (u->meta.load_state == UNIT_LOADED) {
269 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
272 if (s->meta.fragment_path)
273 s->from_fragment = true;
276 if (s->parameters_fragment.what)
277 s->what = strdup(s->parameters_fragment.what);
278 else if (s->parameters_etc_fstab.what)
279 s->what = strdup(s->parameters_etc_fstab.what);
280 else if (s->parameters_proc_swaps.what)
281 s->what = strdup(s->parameters_proc_swaps.what);
283 s->what = unit_name_to_path(u->meta.id);
289 path_kill_slashes(s->what);
291 if (!s->meta.description)
292 if ((r = unit_set_description(u, s->what)) < 0)
295 if ((r = swap_add_device_links(s)) < 0)
298 if ((r = swap_add_mount_links(s)) < 0)
301 if ((r = swap_add_target_links(s)) < 0)
304 if ((r = unit_add_default_cgroups(u)) < 0)
307 if (s->meta.default_dependencies)
308 if ((r = swap_add_default_dependencies(s)) < 0)
312 return swap_verify(s);
318 const char *what_proc_swaps,
326 char *e = NULL, *wp = NULL;
334 if (!(e = unit_name_from_path(what, ".swap")))
337 u = manager_get_unit(m, e);
339 if (what_proc_swaps &&
341 SWAP(u)->from_proc_swaps &&
342 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
348 if (!(u = unit_new(m))) {
353 if ((r = unit_add_name(u, e)) < 0)
356 if (!(SWAP(u)->what = strdup(what))) {
361 unit_add_to_load_queue(u);
365 if (what_proc_swaps) {
368 p = &SWAP(u)->parameters_proc_swaps;
371 if (!(wp = strdup(what_proc_swaps))) {
376 if (!m->swaps_by_proc_swaps)
377 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
385 first = hashmap_get(m->swaps_by_proc_swaps, wp);
386 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
388 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
393 SWAP(u)->is_active = true;
394 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
397 SWAP(u)->from_proc_swaps = true;
400 p = &SWAP(u)->parameters_etc_fstab;
402 if (!(wp = strdup(what))) {
410 SWAP(u)->from_etc_fstab = true;
413 p->priority = priority;
418 unit_add_to_dbus_queue(u);
425 log_warning("Failed to load swap unit: %s", strerror(-r));
436 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
442 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
443 struct udev_device *d;
445 struct udev_list_entry *item = NULL, *first = NULL;
447 /* So this is a proper swap device. Create swap units
448 * for all names this swap device is known under */
450 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
453 if ((dn = udev_device_get_devnode(d)))
454 r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
456 /* Add additional units for all symlinks */
457 first = udev_device_get_devlinks_list_entry(d);
458 udev_list_entry_foreach(item, first) {
461 /* Don't bother with the /dev/block links */
462 p = udev_list_entry_get_name(item);
464 if (path_startswith(p, "/dev/block/"))
467 if (stat(p, &st) >= 0)
468 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
471 if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
475 udev_device_unref(d);
478 if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
484 static void swap_set_state(Swap *s, SwapState state) {
489 old_state = s->state;
492 if (state != SWAP_ACTIVATING &&
493 state != SWAP_ACTIVATING_SIGTERM &&
494 state != SWAP_ACTIVATING_SIGKILL &&
495 state != SWAP_DEACTIVATING &&
496 state != SWAP_DEACTIVATING_SIGTERM &&
497 state != SWAP_DEACTIVATING_SIGKILL) {
498 unit_unwatch_timer(UNIT(s), &s->timer_watch);
499 swap_unwatch_control_pid(s);
500 s->control_command = NULL;
501 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
504 if (state != old_state)
505 log_debug("%s changed %s -> %s",
507 swap_state_to_string(old_state),
508 swap_state_to_string(state));
510 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
513 static int swap_coldplug(Unit *u) {
515 SwapState new_state = SWAP_DEAD;
519 assert(s->state == SWAP_DEAD);
521 if (s->deserialized_state != s->state)
522 new_state = s->deserialized_state;
523 else if (s->from_proc_swaps)
524 new_state = SWAP_ACTIVE;
526 if (new_state != s->state) {
528 if (new_state == SWAP_ACTIVATING ||
529 new_state == SWAP_ACTIVATING_SIGTERM ||
530 new_state == SWAP_ACTIVATING_SIGKILL ||
531 new_state == SWAP_DEACTIVATING ||
532 new_state == SWAP_DEACTIVATING_SIGTERM ||
533 new_state == SWAP_DEACTIVATING_SIGKILL) {
535 if (s->control_pid <= 0)
538 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
541 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
545 swap_set_state(s, new_state);
551 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
558 if (s->from_proc_swaps)
559 p = &s->parameters_proc_swaps;
560 else if (s->from_fragment)
561 p = &s->parameters_fragment;
563 p = &s->parameters_etc_fstab;
572 "%sFrom /etc/fstab: %s\n"
573 "%sFrom /proc/swaps: %s\n"
574 "%sFrom fragment: %s\n",
575 prefix, swap_state_to_string(s->state),
578 prefix, yes_no(p->noauto),
579 prefix, yes_no(p->nofail),
580 prefix, yes_no(p->handle),
581 prefix, yes_no(s->from_etc_fstab),
582 prefix, yes_no(s->from_proc_swaps),
583 prefix, yes_no(s->from_fragment));
585 if (s->control_pid > 0)
587 "%sControl PID: %lu\n",
588 prefix, (unsigned long) s->control_pid);
590 exec_context_dump(&s->exec_context, f, prefix);
593 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
601 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
604 if ((r = exec_spawn(c,
608 s->meta.manager->environment,
612 s->meta.manager->confirm_spawn,
613 s->meta.cgroup_bondings,
617 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
618 /* FIXME: we need to do something here */
626 unit_unwatch_timer(UNIT(s), &s->timer_watch);
631 static void swap_enter_dead(Swap *s, bool success) {
637 swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD);
640 static void swap_enter_active(Swap *s, bool success) {
646 swap_set_state(s, SWAP_ACTIVE);
649 static void swap_enter_signal(Swap *s, SwapState state, bool success) {
652 bool wait_for_exit = false;
659 if (s->exec_context.kill_mode != KILL_NONE) {
660 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
661 state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
663 if (s->control_pid > 0) {
664 if (kill_and_sigcont(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
666 s->control_pid, sig) < 0 && errno != ESRCH)
668 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
670 wait_for_exit = true;
673 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
675 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
680 /* Exclude the control pid from being killed via the cgroup */
681 if (s->control_pid > 0)
682 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
685 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
686 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
687 log_warning("Failed to kill control group: %s", strerror(-r));
689 wait_for_exit = true;
696 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
699 swap_set_state(s, state);
701 swap_enter_dead(s, true);
706 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
708 swap_enter_dead(s, false);
714 static void swap_enter_activating(Swap *s) {
719 s->control_command_id = SWAP_EXEC_ACTIVATE;
720 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
722 if (s->from_fragment)
723 priority = s->parameters_fragment.priority;
724 else if (s->from_etc_fstab)
725 priority = s->parameters_etc_fstab.priority;
732 snprintf(p, sizeof(p), "%i", priority);
735 r = exec_command_set(
743 r = exec_command_set(
752 swap_unwatch_control_pid(s);
754 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
757 swap_set_state(s, SWAP_ACTIVATING);
762 log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r));
763 swap_enter_dead(s, false);
766 static void swap_enter_deactivating(Swap *s, bool success) {
774 s->control_command_id = SWAP_EXEC_DEACTIVATE;
775 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
777 if ((r = exec_command_set(
784 swap_unwatch_control_pid(s);
786 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
789 swap_set_state(s, SWAP_DEACTIVATING);
794 log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r));
795 swap_enter_active(s, false);
798 static int swap_start(Unit *u) {
803 /* We cannot fulfill this request right now, try again later
806 if (s->state == SWAP_DEACTIVATING ||
807 s->state == SWAP_DEACTIVATING_SIGTERM ||
808 s->state == SWAP_DEACTIVATING_SIGKILL ||
809 s->state == SWAP_ACTIVATING_SIGTERM ||
810 s->state == SWAP_ACTIVATING_SIGKILL)
813 if (s->state == SWAP_ACTIVATING)
816 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
819 swap_enter_activating(s);
823 static int swap_stop(Unit *u) {
828 if (s->state == SWAP_DEACTIVATING ||
829 s->state == SWAP_DEACTIVATING_SIGTERM ||
830 s->state == SWAP_DEACTIVATING_SIGKILL ||
831 s->state == SWAP_ACTIVATING_SIGTERM ||
832 s->state == SWAP_ACTIVATING_SIGKILL)
835 assert(s->state == SWAP_ACTIVATING ||
836 s->state == SWAP_ACTIVE);
838 swap_enter_deactivating(s, true);
842 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
849 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
850 unit_serialize_item(u, f, "failure", yes_no(s->failure));
852 if (s->control_pid > 0)
853 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
855 if (s->control_command_id >= 0)
856 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
861 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
867 if (streq(key, "state")) {
870 if ((state = swap_state_from_string(value)) < 0)
871 log_debug("Failed to parse state value %s", value);
873 s->deserialized_state = state;
874 } else if (streq(key, "failure")) {
877 if ((b = parse_boolean(value)) < 0)
878 log_debug("Failed to parse failure value %s", value);
880 s->failure = b || s->failure;
882 } else if (streq(key, "control-pid")) {
885 if (parse_pid(value, &pid) < 0)
886 log_debug("Failed to parse control-pid value %s", value);
888 s->control_pid = pid;
890 } else if (streq(key, "control-command")) {
893 if ((id = swap_exec_command_from_string(value)) < 0)
894 log_debug("Failed to parse exec-command value %s", value);
896 s->control_command_id = id;
897 s->control_command = s->exec_command + id;
901 log_debug("Unknown serialization key '%s'", key);
906 static UnitActiveState swap_active_state(Unit *u) {
909 return state_translation_table[SWAP(u)->state];
912 static const char *swap_sub_state_to_string(Unit *u) {
915 return swap_state_to_string(SWAP(u)->state);
918 static bool swap_check_gc(Unit *u) {
923 return s->from_etc_fstab || s->from_proc_swaps;
926 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
933 if (pid != s->control_pid)
938 success = is_clean_exit(code, status);
939 s->failure = s->failure || !success;
941 if (s->control_command) {
942 exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
943 s->control_command = NULL;
944 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
947 log_full(success ? LOG_DEBUG : LOG_NOTICE,
948 "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
952 case SWAP_ACTIVATING:
953 case SWAP_ACTIVATING_SIGTERM:
954 case SWAP_ACTIVATING_SIGKILL:
957 swap_enter_active(s, true);
959 swap_enter_dead(s, false);
962 case SWAP_DEACTIVATING:
963 case SWAP_DEACTIVATING_SIGKILL:
964 case SWAP_DEACTIVATING_SIGTERM:
967 swap_enter_dead(s, true);
969 swap_enter_dead(s, false);
973 assert_not_reached("Uh, control process died at wrong time.");
976 /* Notify clients about changed exit status */
977 unit_add_to_dbus_queue(u);
979 /* Request a reload of /proc/swaps, so that following units
980 * can follow our state change */
981 u->meta.manager->request_reload = true;
984 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
988 assert(elapsed == 1);
989 assert(w == &s->timer_watch);
993 case SWAP_ACTIVATING:
994 log_warning("%s activation timed out. Stopping.", u->meta.id);
995 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false);
998 case SWAP_DEACTIVATING:
999 log_warning("%s deactivation timed out. Stopping.", u->meta.id);
1000 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false);
1003 case SWAP_ACTIVATING_SIGTERM:
1004 if (s->exec_context.send_sigkill) {
1005 log_warning("%s activation timed out. Killing.", u->meta.id);
1006 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
1008 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1009 swap_enter_dead(s, false);
1013 case SWAP_DEACTIVATING_SIGTERM:
1014 if (s->exec_context.send_sigkill) {
1015 log_warning("%s deactivation timed out. Killing.", u->meta.id);
1016 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
1018 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1019 swap_enter_dead(s, false);
1023 case SWAP_ACTIVATING_SIGKILL:
1024 case SWAP_DEACTIVATING_SIGKILL:
1025 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id);
1026 swap_enter_dead(s, false);
1030 assert_not_reached("Timeout at wrong time.");
1034 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1040 rewind(m->proc_swaps);
1042 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1045 char *dev = NULL, *d;
1048 if ((k = fscanf(m->proc_swaps,
1049 "%ms " /* device/file */
1050 "%*s " /* type of swap */
1051 "%*s " /* swap size */
1053 "%i\n", /* priority */
1054 &dev, &prio)) != 2) {
1059 log_warning("Failed to parse /proc/swaps:%u.", i);
1070 k = swap_process_new_swap(m, d, prio, set_flags);
1080 int swap_dispatch_reload(Manager *m) {
1081 /* This function should go as soon as the kernel properly notifies us */
1083 if (_likely_(!m->request_reload))
1086 m->request_reload = false;
1088 return swap_fd_event(m, EPOLLPRI);
1091 int swap_fd_event(Manager *m, int events) {
1096 assert(events & EPOLLPRI);
1098 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1099 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1101 /* Reset flags, just in case, for late calls */
1102 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1103 Swap *swap = (Swap*) meta;
1105 swap->is_active = swap->just_activated = false;
1111 manager_dispatch_load_queue(m);
1113 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
1114 Swap *swap = (Swap*) meta;
1116 if (!swap->is_active) {
1117 /* This has just been deactivated */
1119 swap->from_proc_swaps = false;
1120 swap_unset_proc_swaps(swap);
1122 switch (swap->state) {
1125 swap_enter_dead(swap, true);
1129 swap_set_state(swap, swap->state);
1133 } else if (swap->just_activated) {
1135 /* New swap entry */
1137 switch (swap->state) {
1141 swap_enter_active(swap, true);
1145 /* Nothing really changed, but let's
1146 * issue an notification call
1147 * nonetheless, in case somebody is
1148 * waiting for this. */
1149 swap_set_state(swap, swap->state);
1154 /* Reset the flags for later calls */
1155 swap->is_active = swap->just_activated = false;
1161 static Unit *swap_following(Unit *u) {
1163 Swap *other, *first = NULL;
1167 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1170 /* Make everybody follow the unit that's named after the swap
1171 * device in the kernel */
1173 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1174 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1177 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1178 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1187 static int swap_following_set(Unit *u, Set **_set) {
1196 if (LIST_JUST_US(same_proc_swaps, s)) {
1201 if (!(set = set_new(NULL, NULL)))
1204 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1205 if ((r = set_put(set, other)) < 0)
1208 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1209 if ((r = set_put(set, other)) < 0)
1220 static void swap_shutdown(Manager *m) {
1223 if (m->proc_swaps) {
1224 fclose(m->proc_swaps);
1225 m->proc_swaps = NULL;
1228 hashmap_free(m->swaps_by_proc_swaps);
1229 m->swaps_by_proc_swaps = NULL;
1232 static int swap_enumerate(Manager *m) {
1234 struct epoll_event ev;
1237 if (!m->proc_swaps) {
1238 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1241 m->swap_watch.type = WATCH_SWAP;
1242 m->swap_watch.fd = fileno(m->proc_swaps);
1245 ev.events = EPOLLPRI;
1246 ev.data.ptr = &m->swap_watch;
1248 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1252 /* We rely on mount.c to load /etc/fstab for us */
1254 if ((r = swap_load_proc_swaps(m, false)) < 0)
1260 static void swap_reset_failed(Unit *u) {
1265 if (s->state == SWAP_FAILED)
1266 swap_set_state(s, SWAP_DEAD);
1271 static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1274 Set *pid_set = NULL;
1278 if (who == KILL_MAIN) {
1279 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1283 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1284 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1288 if (s->control_pid > 0)
1289 if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
1292 if (mode == KILL_CONTROL_GROUP) {
1295 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1298 /* Exclude the control pid from being killed via the cgroup */
1299 if (s->control_pid > 0)
1300 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
1305 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
1306 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1317 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1318 [SWAP_DEAD] = "dead",
1319 [SWAP_ACTIVATING] = "activating",
1320 [SWAP_ACTIVE] = "active",
1321 [SWAP_DEACTIVATING] = "deactivating",
1322 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1323 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1324 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1325 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1326 [SWAP_FAILED] = "failed"
1329 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1331 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1332 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1333 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1336 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1338 const UnitVTable swap_vtable = {
1342 .no_instances = true,
1344 .show_status = true,
1350 .coldplug = swap_coldplug,
1354 .start = swap_start,
1359 .serialize = swap_serialize,
1360 .deserialize_item = swap_deserialize_item,
1362 .active_state = swap_active_state,
1363 .sub_state_to_string = swap_sub_state_to_string,
1365 .check_gc = swap_check_gc,
1367 .sigchld_event = swap_sigchld_event,
1368 .timer_event = swap_timer_event,
1370 .reset_failed = swap_reset_failed,
1372 .bus_interface = "org.freedesktop.systemd1.Swap",
1373 .bus_message_handler = bus_swap_message_handler,
1374 .bus_invalidating_properties = bus_swap_invalidating_properties,
1376 .following = swap_following,
1377 .following_set = swap_following_set,
1379 .enumerate = swap_enumerate,
1380 .shutdown = swap_shutdown