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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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 #include "path-util.h"
43 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
44 [SWAP_DEAD] = UNIT_INACTIVE,
45 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
46 [SWAP_ACTIVE] = UNIT_ACTIVE,
47 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
48 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
49 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
50 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
51 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
52 [SWAP_FAILED] = UNIT_FAILED
55 static void swap_unset_proc_swaps(Swap *s) {
60 if (!s->parameters_proc_swaps.what)
63 /* Remove this unit from the chain of swaps which share the
64 * same kernel swap device. */
66 first = hashmap_get(UNIT(s)->manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
67 LIST_REMOVE(Swap, same_proc_swaps, first, s);
70 hashmap_remove_and_replace(UNIT(s)->manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
72 hashmap_remove(UNIT(s)->manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
74 free(s->parameters_proc_swaps.what);
75 s->parameters_proc_swaps.what = NULL;
78 static void swap_init(Unit *u) {
82 assert(UNIT(s)->load_state == UNIT_STUB);
84 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
86 exec_context_init(&s->exec_context);
87 s->exec_context.std_output = u->manager->default_std_output;
88 s->exec_context.std_error = u->manager->default_std_error;
89 kill_context_init(&s->kill_context);
91 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
93 s->timer_watch.type = WATCH_INVALID;
95 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
97 UNIT(s)->ignore_on_isolate = true;
100 static void swap_unwatch_control_pid(Swap *s) {
103 if (s->control_pid <= 0)
106 unit_unwatch_pid(UNIT(s), s->control_pid);
110 static void swap_done(Unit *u) {
115 swap_unset_proc_swaps(s);
120 free(s->parameters_fragment.what);
121 s->parameters_fragment.what = NULL;
123 exec_context_done(&s->exec_context);
124 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
125 s->control_command = NULL;
127 swap_unwatch_control_pid(s);
129 unit_unwatch_timer(u, &s->timer_watch);
132 int swap_add_one_mount_link(Swap *s, Mount *m) {
138 if (UNIT(s)->load_state != UNIT_LOADED ||
139 UNIT(m)->load_state != UNIT_LOADED)
142 if (is_device_path(s->what))
145 if (!path_startswith(s->what, m->where))
148 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
154 static int swap_add_mount_links(Swap *s) {
160 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_MOUNT])
161 if ((r = swap_add_one_mount_link(s, MOUNT(other))) < 0)
167 static int swap_add_target_links(Swap *s) {
173 if (!s->from_fragment)
176 if ((r = manager_load_unit(UNIT(s)->manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
179 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
182 static int swap_add_device_links(Swap *s) {
190 if (s->from_fragment)
191 p = &s->parameters_fragment;
195 if (is_device_path(s->what))
196 return unit_add_node_link(UNIT(s), s->what,
197 !p->noauto && p->nofail &&
198 UNIT(s)->manager->running_as == MANAGER_SYSTEM);
200 /* File based swap devices need to be ordered after
201 * systemd-remount-fs.service, since they might need a
202 * writable file system. */
203 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
206 static int swap_add_default_dependencies(Swap *s) {
211 if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
214 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
221 static int swap_verify(Swap *s) {
225 if (UNIT(s)->load_state != UNIT_LOADED)
228 if (!(e = unit_name_from_path(s->what, ".swap")))
231 b = unit_has_name(UNIT(s), e);
235 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->id);
239 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
240 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
247 static int swap_load(Unit *u) {
252 assert(u->load_state == UNIT_STUB);
254 /* Load a .swap file */
255 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
258 if (u->load_state == UNIT_LOADED) {
259 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
262 if (UNIT(s)->fragment_path)
263 s->from_fragment = true;
266 if (s->parameters_fragment.what)
267 s->what = strdup(s->parameters_fragment.what);
268 else if (s->parameters_proc_swaps.what)
269 s->what = strdup(s->parameters_proc_swaps.what);
271 s->what = unit_name_to_path(u->id);
277 path_kill_slashes(s->what);
279 if (!UNIT(s)->description)
280 if ((r = unit_set_description(u, s->what)) < 0)
283 if ((r = swap_add_device_links(s)) < 0)
286 if ((r = swap_add_mount_links(s)) < 0)
289 if ((r = swap_add_target_links(s)) < 0)
292 if ((r = unit_add_default_cgroups(u)) < 0)
295 if (UNIT(s)->default_dependencies)
296 if ((r = swap_add_default_dependencies(s)) < 0)
299 r = unit_exec_context_defaults(u, &s->exec_context);
304 return swap_verify(s);
307 static int swap_add_one(
310 const char *what_proc_swaps,
317 char *e = NULL, *wp = NULL;
325 assert(what_proc_swaps);
327 e = unit_name_from_path(what, ".swap");
331 u = manager_get_unit(m, e);
334 SWAP(u)->from_proc_swaps &&
335 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
341 u = unit_new(m, sizeof(Swap));
347 r = unit_add_name(u, e);
351 SWAP(u)->what = strdup(what);
352 if (!SWAP(u)->what) {
357 unit_add_to_load_queue(u);
361 p = &SWAP(u)->parameters_proc_swaps;
364 if (!(wp = strdup(what_proc_swaps))) {
369 if (!m->swaps_by_proc_swaps)
370 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
378 first = hashmap_get(m->swaps_by_proc_swaps, wp);
379 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
381 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
386 SWAP(u)->is_active = true;
387 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
390 SWAP(u)->from_proc_swaps = true;
392 p->priority = priority;
396 unit_add_to_dbus_queue(u);
403 log_warning("Failed to load swap unit: %s", strerror(-r));
414 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
420 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
421 struct udev_device *d;
423 struct udev_list_entry *item = NULL, *first = NULL;
425 /* So this is a proper swap device. Create swap units
426 * for all names this swap device is known under */
428 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
431 dn = udev_device_get_devnode(d);
433 r = swap_add_one(m, dn, device, prio, false, false, set_flags);
435 /* Add additional units for all symlinks */
436 first = udev_device_get_devlinks_list_entry(d);
437 udev_list_entry_foreach(item, first) {
440 /* Don't bother with the /dev/block links */
441 p = udev_list_entry_get_name(item);
443 if (path_startswith(p, "/dev/block/"))
446 if (stat(p, &st) >= 0)
447 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
450 k = swap_add_one(m, p, device, prio, false, false, set_flags);
455 udev_device_unref(d);
458 k = swap_add_one(m, device, device, prio, false, false, set_flags);
465 static void swap_set_state(Swap *s, SwapState state) {
470 old_state = s->state;
473 if (state != SWAP_ACTIVATING &&
474 state != SWAP_ACTIVATING_SIGTERM &&
475 state != SWAP_ACTIVATING_SIGKILL &&
476 state != SWAP_DEACTIVATING &&
477 state != SWAP_DEACTIVATING_SIGTERM &&
478 state != SWAP_DEACTIVATING_SIGKILL) {
479 unit_unwatch_timer(UNIT(s), &s->timer_watch);
480 swap_unwatch_control_pid(s);
481 s->control_command = NULL;
482 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
485 if (state != old_state)
486 log_debug("%s changed %s -> %s",
488 swap_state_to_string(old_state),
489 swap_state_to_string(state));
491 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
494 static int swap_coldplug(Unit *u) {
496 SwapState new_state = SWAP_DEAD;
500 assert(s->state == SWAP_DEAD);
502 if (s->deserialized_state != s->state)
503 new_state = s->deserialized_state;
504 else if (s->from_proc_swaps)
505 new_state = SWAP_ACTIVE;
507 if (new_state != s->state) {
509 if (new_state == SWAP_ACTIVATING ||
510 new_state == SWAP_ACTIVATING_SIGTERM ||
511 new_state == SWAP_ACTIVATING_SIGKILL ||
512 new_state == SWAP_DEACTIVATING ||
513 new_state == SWAP_DEACTIVATING_SIGTERM ||
514 new_state == SWAP_DEACTIVATING_SIGKILL) {
516 if (s->control_pid <= 0)
519 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
522 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
526 swap_set_state(s, new_state);
532 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
539 if (s->from_proc_swaps)
540 p = &s->parameters_proc_swaps;
541 else if (s->from_fragment)
542 p = &s->parameters_fragment;
550 "%sFrom /proc/swaps: %s\n"
551 "%sFrom fragment: %s\n",
552 prefix, swap_state_to_string(s->state),
553 prefix, swap_result_to_string(s->result),
555 prefix, yes_no(s->from_proc_swaps),
556 prefix, yes_no(s->from_fragment));
564 prefix, yes_no(p->noauto),
565 prefix, yes_no(p->nofail));
567 if (s->control_pid > 0)
569 "%sControl PID: %lu\n",
570 prefix, (unsigned long) s->control_pid);
572 exec_context_dump(&s->exec_context, f, prefix);
573 kill_context_dump(&s->kill_context, f, prefix);
576 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
584 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
587 if ((r = exec_spawn(c,
591 UNIT(s)->manager->environment,
595 UNIT(s)->manager->confirm_spawn,
596 UNIT(s)->cgroup_bondings,
597 UNIT(s)->cgroup_attributes,
604 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
605 /* FIXME: we need to do something here */
613 unit_unwatch_timer(UNIT(s), &s->timer_watch);
618 static void swap_enter_dead(Swap *s, SwapResult f) {
621 if (f != SWAP_SUCCESS)
624 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
627 static void swap_enter_active(Swap *s, SwapResult f) {
630 if (f != SWAP_SUCCESS)
633 swap_set_state(s, SWAP_ACTIVE);
636 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
639 bool wait_for_exit = false;
643 if (f != SWAP_SUCCESS)
646 if (s->kill_context.kill_mode != KILL_NONE) {
647 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
648 state == SWAP_DEACTIVATING_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
650 if (s->control_pid > 0) {
651 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
653 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
655 wait_for_exit = true;
658 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
660 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
665 /* Exclude the control pid from being killed via the cgroup */
666 if (s->control_pid > 0)
667 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
670 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
672 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
673 log_warning("Failed to kill control group: %s", strerror(-r));
675 wait_for_exit = true;
683 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
686 swap_set_state(s, state);
688 swap_enter_dead(s, SWAP_SUCCESS);
693 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
695 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
701 static void swap_enter_activating(Swap *s) {
706 s->control_command_id = SWAP_EXEC_ACTIVATE;
707 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
709 if (s->from_fragment)
710 priority = s->parameters_fragment.priority;
717 snprintf(p, sizeof(p), "%i", priority);
720 r = exec_command_set(
728 r = exec_command_set(
737 swap_unwatch_control_pid(s);
739 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
742 swap_set_state(s, SWAP_ACTIVATING);
747 log_warning("%s failed to run 'swapon' task: %s", UNIT(s)->id, strerror(-r));
748 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
751 static void swap_enter_deactivating(Swap *s) {
756 s->control_command_id = SWAP_EXEC_DEACTIVATE;
757 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
759 if ((r = exec_command_set(
766 swap_unwatch_control_pid(s);
768 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
771 swap_set_state(s, SWAP_DEACTIVATING);
776 log_warning("%s failed to run 'swapoff' task: %s", UNIT(s)->id, strerror(-r));
777 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
780 static int swap_start(Unit *u) {
785 /* We cannot fulfill this request right now, try again later
788 if (s->state == SWAP_DEACTIVATING ||
789 s->state == SWAP_DEACTIVATING_SIGTERM ||
790 s->state == SWAP_DEACTIVATING_SIGKILL ||
791 s->state == SWAP_ACTIVATING_SIGTERM ||
792 s->state == SWAP_ACTIVATING_SIGKILL)
795 if (s->state == SWAP_ACTIVATING)
798 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
800 s->result = SWAP_SUCCESS;
801 swap_enter_activating(s);
805 static int swap_stop(Unit *u) {
810 if (s->state == SWAP_DEACTIVATING ||
811 s->state == SWAP_DEACTIVATING_SIGTERM ||
812 s->state == SWAP_DEACTIVATING_SIGKILL ||
813 s->state == SWAP_ACTIVATING_SIGTERM ||
814 s->state == SWAP_ACTIVATING_SIGKILL)
817 assert(s->state == SWAP_ACTIVATING ||
818 s->state == SWAP_ACTIVE);
820 swap_enter_deactivating(s);
824 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
831 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
832 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
834 if (s->control_pid > 0)
835 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
837 if (s->control_command_id >= 0)
838 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
843 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
849 if (streq(key, "state")) {
852 if ((state = swap_state_from_string(value)) < 0)
853 log_debug("Failed to parse state value %s", value);
855 s->deserialized_state = state;
856 } else if (streq(key, "result")) {
859 f = swap_result_from_string(value);
861 log_debug("Failed to parse result value %s", value);
862 else if (f != SWAP_SUCCESS)
864 } else if (streq(key, "control-pid")) {
867 if (parse_pid(value, &pid) < 0)
868 log_debug("Failed to parse control-pid value %s", value);
870 s->control_pid = pid;
872 } else if (streq(key, "control-command")) {
875 if ((id = swap_exec_command_from_string(value)) < 0)
876 log_debug("Failed to parse exec-command value %s", value);
878 s->control_command_id = id;
879 s->control_command = s->exec_command + id;
883 log_debug("Unknown serialization key '%s'", key);
888 static UnitActiveState swap_active_state(Unit *u) {
891 return state_translation_table[SWAP(u)->state];
894 static const char *swap_sub_state_to_string(Unit *u) {
897 return swap_state_to_string(SWAP(u)->state);
900 static bool swap_check_gc(Unit *u) {
905 return s->from_proc_swaps;
908 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
915 if (pid != s->control_pid)
920 if (is_clean_exit(code, status, NULL))
922 else if (code == CLD_EXITED)
923 f = SWAP_FAILURE_EXIT_CODE;
924 else if (code == CLD_KILLED)
925 f = SWAP_FAILURE_SIGNAL;
926 else if (code == CLD_DUMPED)
927 f = SWAP_FAILURE_CORE_DUMP;
929 assert_not_reached("Unknown code");
931 if (f != SWAP_SUCCESS)
934 if (s->control_command) {
935 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
937 s->control_command = NULL;
938 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
941 log_full(f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
942 "%s swap process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
946 case SWAP_ACTIVATING:
947 case SWAP_ACTIVATING_SIGTERM:
948 case SWAP_ACTIVATING_SIGKILL:
950 if (f == SWAP_SUCCESS)
951 swap_enter_active(s, f);
953 swap_enter_dead(s, f);
956 case SWAP_DEACTIVATING:
957 case SWAP_DEACTIVATING_SIGKILL:
958 case SWAP_DEACTIVATING_SIGTERM:
960 if (f == SWAP_SUCCESS)
961 swap_enter_dead(s, f);
963 swap_enter_dead(s, f);
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->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->id);
989 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
992 case SWAP_DEACTIVATING:
993 log_warning("%s deactivation timed out. Stopping.", u->id);
994 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
997 case SWAP_ACTIVATING_SIGTERM:
998 if (s->kill_context.send_sigkill) {
999 log_warning("%s activation timed out. Killing.", u->id);
1000 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1002 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->id);
1003 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1007 case SWAP_DEACTIVATING_SIGTERM:
1008 if (s->kill_context.send_sigkill) {
1009 log_warning("%s deactivation timed out. Killing.", u->id);
1010 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1012 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->id);
1013 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1017 case SWAP_ACTIVATING_SIGKILL:
1018 case SWAP_DEACTIVATING_SIGKILL:
1019 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->id);
1020 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1024 assert_not_reached("Timeout at wrong time.");
1028 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1034 rewind(m->proc_swaps);
1036 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1039 char *dev = NULL, *d;
1042 if ((k = fscanf(m->proc_swaps,
1043 "%ms " /* device/file */
1044 "%*s " /* type of swap */
1045 "%*s " /* swap size */
1047 "%i\n", /* priority */
1048 &dev, &prio)) != 2) {
1053 log_warning("Failed to parse /proc/swaps:%u.", i);
1064 k = swap_process_new_swap(m, d, prio, set_flags);
1074 int swap_dispatch_reload(Manager *m) {
1075 /* This function should go as soon as the kernel properly notifies us */
1077 if (_likely_(!m->request_reload))
1080 m->request_reload = false;
1082 return swap_fd_event(m, EPOLLPRI);
1085 int swap_fd_event(Manager *m, int events) {
1090 assert(events & EPOLLPRI);
1092 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1093 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1095 /* Reset flags, just in case, for late calls */
1096 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1097 Swap *swap = SWAP(u);
1099 swap->is_active = swap->just_activated = false;
1105 manager_dispatch_load_queue(m);
1107 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1108 Swap *swap = SWAP(u);
1110 if (!swap->is_active) {
1111 /* This has just been deactivated */
1113 swap->from_proc_swaps = false;
1114 swap_unset_proc_swaps(swap);
1116 switch (swap->state) {
1119 swap_enter_dead(swap, SWAP_SUCCESS);
1123 swap_set_state(swap, swap->state);
1127 } else if (swap->just_activated) {
1129 /* New swap entry */
1131 switch (swap->state) {
1135 swap_enter_active(swap, SWAP_SUCCESS);
1139 /* Nothing really changed, but let's
1140 * issue an notification call
1141 * nonetheless, in case somebody is
1142 * waiting for this. */
1143 swap_set_state(swap, swap->state);
1148 /* Reset the flags for later calls */
1149 swap->is_active = swap->just_activated = false;
1155 static Unit *swap_following(Unit *u) {
1157 Swap *other, *first = NULL;
1161 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1164 /* Make everybody follow the unit that's named after the swap
1165 * device in the kernel */
1167 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1168 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1171 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1172 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1181 static int swap_following_set(Unit *u, Set **_set) {
1190 if (LIST_JUST_US(same_proc_swaps, s)) {
1195 if (!(set = set_new(NULL, NULL)))
1198 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1199 if ((r = set_put(set, other)) < 0)
1202 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1203 if ((r = set_put(set, other)) < 0)
1214 static void swap_shutdown(Manager *m) {
1217 if (m->proc_swaps) {
1218 fclose(m->proc_swaps);
1219 m->proc_swaps = NULL;
1222 hashmap_free(m->swaps_by_proc_swaps);
1223 m->swaps_by_proc_swaps = NULL;
1226 static int swap_enumerate(Manager *m) {
1228 struct epoll_event ev;
1231 if (!m->proc_swaps) {
1232 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1233 return (errno == ENOENT) ? 0 : -errno;
1235 m->swap_watch.type = WATCH_SWAP;
1236 m->swap_watch.fd = fileno(m->proc_swaps);
1239 ev.events = EPOLLPRI;
1240 ev.data.ptr = &m->swap_watch;
1242 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1246 if ((r = swap_load_proc_swaps(m, false)) < 0)
1252 static void swap_reset_failed(Unit *u) {
1257 if (s->state == SWAP_FAILED)
1258 swap_set_state(s, SWAP_DEAD);
1260 s->result = SWAP_SUCCESS;
1263 static int swap_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1266 Set *pid_set = NULL;
1270 if (who == KILL_MAIN) {
1271 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1275 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1276 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1280 if (who == KILL_CONTROL || who == KILL_ALL)
1281 if (s->control_pid > 0)
1282 if (kill(s->control_pid, signo) < 0)
1285 if (who == KILL_ALL) {
1288 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1292 /* Exclude the control pid from being killed via the cgroup */
1293 if (s->control_pid > 0) {
1294 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
1301 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
1302 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1313 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1314 [SWAP_DEAD] = "dead",
1315 [SWAP_ACTIVATING] = "activating",
1316 [SWAP_ACTIVE] = "active",
1317 [SWAP_DEACTIVATING] = "deactivating",
1318 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1319 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1320 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1321 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1322 [SWAP_FAILED] = "failed"
1325 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1327 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1328 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1329 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1332 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1334 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1335 [SWAP_SUCCESS] = "success",
1336 [SWAP_FAILURE_RESOURCES] = "resources",
1337 [SWAP_FAILURE_TIMEOUT] = "timeout",
1338 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1339 [SWAP_FAILURE_SIGNAL] = "signal",
1340 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1343 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1345 const UnitVTable swap_vtable = {
1346 .object_size = sizeof(Swap),
1353 .no_instances = true,
1359 .coldplug = swap_coldplug,
1363 .start = swap_start,
1368 .serialize = swap_serialize,
1369 .deserialize_item = swap_deserialize_item,
1371 .active_state = swap_active_state,
1372 .sub_state_to_string = swap_sub_state_to_string,
1374 .check_gc = swap_check_gc,
1376 .sigchld_event = swap_sigchld_event,
1377 .timer_event = swap_timer_event,
1379 .reset_failed = swap_reset_failed,
1381 .bus_interface = "org.freedesktop.systemd1.Swap",
1382 .bus_message_handler = bus_swap_message_handler,
1383 .bus_invalidating_properties = bus_swap_invalidating_properties,
1385 .following = swap_following,
1386 .following_set = swap_following_set,
1388 .enumerate = swap_enumerate,
1389 .shutdown = swap_shutdown,
1391 .status_message_formats = {
1392 .starting_stopping = {
1393 [0] = "Activating swap %s...",
1394 [1] = "Deactivating swap %s...",
1396 .finished_start_job = {
1397 [JOB_DONE] = "Activated swap %s.",
1398 [JOB_FAILED] = "Failed to activate swap %s.",
1399 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1400 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1402 .finished_stop_job = {
1403 [JOB_DONE] = "Deactivated swap %s.",
1404 [JOB_FAILED] = "Failed deactivating swap %s.",
1405 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",