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"
44 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
45 [SWAP_DEAD] = UNIT_INACTIVE,
46 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
47 [SWAP_ACTIVE] = UNIT_ACTIVE,
48 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
49 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
50 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
51 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
52 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
53 [SWAP_FAILED] = UNIT_FAILED
56 static void swap_unset_proc_swaps(Swap *s) {
61 if (!s->parameters_proc_swaps.what)
64 /* Remove this unit from the chain of swaps which share the
65 * same kernel swap device. */
67 first = hashmap_get(UNIT(s)->manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
68 LIST_REMOVE(Swap, same_proc_swaps, first, s);
71 hashmap_remove_and_replace(UNIT(s)->manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
73 hashmap_remove(UNIT(s)->manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
75 free(s->parameters_proc_swaps.what);
76 s->parameters_proc_swaps.what = NULL;
79 static void swap_init(Unit *u) {
83 assert(UNIT(s)->load_state == UNIT_STUB);
85 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
87 exec_context_init(&s->exec_context);
88 s->exec_context.std_output = u->manager->default_std_output;
89 s->exec_context.std_error = u->manager->default_std_error;
90 kill_context_init(&s->kill_context);
92 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
94 s->timer_watch.type = WATCH_INVALID;
96 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
98 UNIT(s)->ignore_on_isolate = true;
101 static void swap_unwatch_control_pid(Swap *s) {
104 if (s->control_pid <= 0)
107 unit_unwatch_pid(UNIT(s), s->control_pid);
111 static void swap_done(Unit *u) {
116 swap_unset_proc_swaps(s);
121 free(s->parameters_fragment.what);
122 s->parameters_fragment.what = NULL;
124 exec_context_done(&s->exec_context);
125 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
126 s->control_command = NULL;
128 swap_unwatch_control_pid(s);
130 unit_unwatch_timer(u, &s->timer_watch);
133 int swap_add_one_mount_link(Swap *s, Mount *m) {
139 if (UNIT(s)->load_state != UNIT_LOADED ||
140 UNIT(m)->load_state != UNIT_LOADED)
143 if (is_device_path(s->what))
146 if (!path_startswith(s->what, m->where))
149 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
155 static int swap_add_mount_links(Swap *s) {
161 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_MOUNT])
162 if ((r = swap_add_one_mount_link(s, MOUNT(other))) < 0)
168 static int swap_add_target_links(Swap *s) {
174 if (!s->from_fragment)
177 if ((r = manager_load_unit(UNIT(s)->manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
180 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
183 static int swap_add_device_links(Swap *s) {
191 if (s->from_fragment)
192 p = &s->parameters_fragment;
196 if (is_device_path(s->what))
197 return unit_add_node_link(UNIT(s), s->what,
198 !p->noauto && p->nofail &&
199 UNIT(s)->manager->running_as == MANAGER_SYSTEM);
201 /* File based swap devices need to be ordered after
202 * systemd-remount-fs.service, since they might need a
203 * writable file system. */
204 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
207 static int swap_add_default_dependencies(Swap *s) {
212 if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
215 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
222 static int swap_verify(Swap *s) {
226 if (UNIT(s)->load_state != UNIT_LOADED)
229 if (!(e = unit_name_from_path(s->what, ".swap")))
232 b = unit_has_name(UNIT(s), e);
236 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->id);
240 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
241 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
248 static int swap_load(Unit *u) {
253 assert(u->load_state == UNIT_STUB);
255 /* Load a .swap file */
256 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
259 if (u->load_state == UNIT_LOADED) {
260 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
263 if (UNIT(s)->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_proc_swaps.what)
270 s->what = strdup(s->parameters_proc_swaps.what);
272 s->what = unit_name_to_path(u->id);
278 path_kill_slashes(s->what);
280 if (!UNIT(s)->description)
281 if ((r = unit_set_description(u, s->what)) < 0)
284 if ((r = swap_add_device_links(s)) < 0)
287 if ((r = swap_add_mount_links(s)) < 0)
290 if ((r = swap_add_target_links(s)) < 0)
293 if ((r = unit_add_default_cgroups(u)) < 0)
296 if (UNIT(s)->default_dependencies)
297 if ((r = swap_add_default_dependencies(s)) < 0)
300 r = unit_exec_context_defaults(u, &s->exec_context);
305 return swap_verify(s);
308 static int swap_add_one(
311 const char *what_proc_swaps,
318 char *e = NULL, *wp = NULL;
326 assert(what_proc_swaps);
328 e = unit_name_from_path(what, ".swap");
332 u = manager_get_unit(m, e);
335 SWAP(u)->from_proc_swaps &&
336 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
342 u = unit_new(m, sizeof(Swap));
348 r = unit_add_name(u, e);
352 SWAP(u)->what = strdup(what);
353 if (!SWAP(u)->what) {
358 unit_add_to_load_queue(u);
362 p = &SWAP(u)->parameters_proc_swaps;
365 if (!(wp = strdup(what_proc_swaps))) {
370 if (!m->swaps_by_proc_swaps)
371 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
379 first = hashmap_get(m->swaps_by_proc_swaps, wp);
380 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
382 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
387 SWAP(u)->is_active = true;
388 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
391 SWAP(u)->from_proc_swaps = true;
393 p->priority = priority;
397 unit_add_to_dbus_queue(u);
404 log_warning("Failed to load swap unit: %s", strerror(-r));
415 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
421 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
422 struct udev_device *d;
424 struct udev_list_entry *item = NULL, *first = NULL;
426 /* So this is a proper swap device. Create swap units
427 * for all names this swap device is known under */
429 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
432 dn = udev_device_get_devnode(d);
434 r = swap_add_one(m, dn, device, prio, false, false, set_flags);
436 /* Add additional units for all symlinks */
437 first = udev_device_get_devlinks_list_entry(d);
438 udev_list_entry_foreach(item, first) {
441 /* Don't bother with the /dev/block links */
442 p = udev_list_entry_get_name(item);
444 if (path_startswith(p, "/dev/block/"))
447 if (stat(p, &st) >= 0)
448 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
451 k = swap_add_one(m, p, device, prio, false, false, set_flags);
456 udev_device_unref(d);
459 k = swap_add_one(m, device, device, prio, false, false, set_flags);
466 static void swap_set_state(Swap *s, SwapState state) {
471 old_state = s->state;
474 if (state != SWAP_ACTIVATING &&
475 state != SWAP_ACTIVATING_SIGTERM &&
476 state != SWAP_ACTIVATING_SIGKILL &&
477 state != SWAP_DEACTIVATING &&
478 state != SWAP_DEACTIVATING_SIGTERM &&
479 state != SWAP_DEACTIVATING_SIGKILL) {
480 unit_unwatch_timer(UNIT(s), &s->timer_watch);
481 swap_unwatch_control_pid(s);
482 s->control_command = NULL;
483 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
486 if (state != old_state)
487 log_debug("%s changed %s -> %s",
489 swap_state_to_string(old_state),
490 swap_state_to_string(state));
492 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
495 static int swap_coldplug(Unit *u) {
497 SwapState new_state = SWAP_DEAD;
501 assert(s->state == SWAP_DEAD);
503 if (s->deserialized_state != s->state)
504 new_state = s->deserialized_state;
505 else if (s->from_proc_swaps)
506 new_state = SWAP_ACTIVE;
508 if (new_state != s->state) {
510 if (new_state == SWAP_ACTIVATING ||
511 new_state == SWAP_ACTIVATING_SIGTERM ||
512 new_state == SWAP_ACTIVATING_SIGKILL ||
513 new_state == SWAP_DEACTIVATING ||
514 new_state == SWAP_DEACTIVATING_SIGTERM ||
515 new_state == SWAP_DEACTIVATING_SIGKILL) {
517 if (s->control_pid <= 0)
520 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
523 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
527 swap_set_state(s, new_state);
533 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
540 if (s->from_proc_swaps)
541 p = &s->parameters_proc_swaps;
542 else if (s->from_fragment)
543 p = &s->parameters_fragment;
551 "%sFrom /proc/swaps: %s\n"
552 "%sFrom fragment: %s\n",
553 prefix, swap_state_to_string(s->state),
554 prefix, swap_result_to_string(s->result),
556 prefix, yes_no(s->from_proc_swaps),
557 prefix, yes_no(s->from_fragment));
565 prefix, yes_no(p->noauto),
566 prefix, yes_no(p->nofail));
568 if (s->control_pid > 0)
570 "%sControl PID: %lu\n",
571 prefix, (unsigned long) s->control_pid);
573 exec_context_dump(&s->exec_context, f, prefix);
574 kill_context_dump(&s->kill_context, f, prefix);
577 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
585 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
588 if ((r = exec_spawn(c,
592 UNIT(s)->manager->environment,
596 UNIT(s)->manager->confirm_spawn,
597 UNIT(s)->cgroup_bondings,
598 UNIT(s)->cgroup_attributes,
605 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
606 /* FIXME: we need to do something here */
614 unit_unwatch_timer(UNIT(s), &s->timer_watch);
619 static void swap_enter_dead(Swap *s, SwapResult f) {
622 if (f != SWAP_SUCCESS)
625 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
628 static void swap_enter_active(Swap *s, SwapResult f) {
631 if (f != SWAP_SUCCESS)
634 swap_set_state(s, SWAP_ACTIVE);
637 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
640 bool wait_for_exit = false;
644 if (f != SWAP_SUCCESS)
647 if (s->kill_context.kill_mode != KILL_NONE) {
648 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
649 state == SWAP_DEACTIVATING_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
651 if (s->control_pid > 0) {
652 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
654 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
656 wait_for_exit = true;
659 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
661 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
666 /* Exclude the control pid from being killed via the cgroup */
667 if (s->control_pid > 0)
668 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
671 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
673 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
674 log_warning("Failed to kill control group: %s", strerror(-r));
676 wait_for_exit = true;
684 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
687 swap_set_state(s, state);
689 swap_enter_dead(s, SWAP_SUCCESS);
694 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
696 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
702 static void swap_enter_activating(Swap *s) {
707 s->control_command_id = SWAP_EXEC_ACTIVATE;
708 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
710 if (s->from_fragment)
711 priority = s->parameters_fragment.priority;
718 snprintf(p, sizeof(p), "%i", priority);
721 r = exec_command_set(
729 r = exec_command_set(
738 swap_unwatch_control_pid(s);
740 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
743 swap_set_state(s, SWAP_ACTIVATING);
748 log_warning("%s failed to run 'swapon' task: %s", UNIT(s)->id, strerror(-r));
749 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
752 static void swap_enter_deactivating(Swap *s) {
757 s->control_command_id = SWAP_EXEC_DEACTIVATE;
758 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
760 if ((r = exec_command_set(
767 swap_unwatch_control_pid(s);
769 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
772 swap_set_state(s, SWAP_DEACTIVATING);
777 log_warning("%s failed to run 'swapoff' task: %s", UNIT(s)->id, strerror(-r));
778 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
781 static int swap_start(Unit *u) {
786 /* We cannot fulfill this request right now, try again later
789 if (s->state == SWAP_DEACTIVATING ||
790 s->state == SWAP_DEACTIVATING_SIGTERM ||
791 s->state == SWAP_DEACTIVATING_SIGKILL ||
792 s->state == SWAP_ACTIVATING_SIGTERM ||
793 s->state == SWAP_ACTIVATING_SIGKILL)
796 if (s->state == SWAP_ACTIVATING)
799 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
801 if (detect_container(NULL) > 0)
804 s->result = SWAP_SUCCESS;
805 swap_enter_activating(s);
809 static int swap_stop(Unit *u) {
814 if (s->state == SWAP_DEACTIVATING ||
815 s->state == SWAP_DEACTIVATING_SIGTERM ||
816 s->state == SWAP_DEACTIVATING_SIGKILL ||
817 s->state == SWAP_ACTIVATING_SIGTERM ||
818 s->state == SWAP_ACTIVATING_SIGKILL)
821 assert(s->state == SWAP_ACTIVATING ||
822 s->state == SWAP_ACTIVE);
824 if (detect_container(NULL) > 0)
827 swap_enter_deactivating(s);
831 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
838 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
839 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
841 if (s->control_pid > 0)
842 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
844 if (s->control_command_id >= 0)
845 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
850 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
856 if (streq(key, "state")) {
859 if ((state = swap_state_from_string(value)) < 0)
860 log_debug("Failed to parse state value %s", value);
862 s->deserialized_state = state;
863 } else if (streq(key, "result")) {
866 f = swap_result_from_string(value);
868 log_debug("Failed to parse result value %s", value);
869 else if (f != SWAP_SUCCESS)
871 } else if (streq(key, "control-pid")) {
874 if (parse_pid(value, &pid) < 0)
875 log_debug("Failed to parse control-pid value %s", value);
877 s->control_pid = pid;
879 } else if (streq(key, "control-command")) {
882 if ((id = swap_exec_command_from_string(value)) < 0)
883 log_debug("Failed to parse exec-command value %s", value);
885 s->control_command_id = id;
886 s->control_command = s->exec_command + id;
890 log_debug("Unknown serialization key '%s'", key);
895 static UnitActiveState swap_active_state(Unit *u) {
898 return state_translation_table[SWAP(u)->state];
901 static const char *swap_sub_state_to_string(Unit *u) {
904 return swap_state_to_string(SWAP(u)->state);
907 static bool swap_check_gc(Unit *u) {
912 return s->from_proc_swaps;
915 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
922 if (pid != s->control_pid)
927 if (is_clean_exit(code, status, NULL))
929 else if (code == CLD_EXITED)
930 f = SWAP_FAILURE_EXIT_CODE;
931 else if (code == CLD_KILLED)
932 f = SWAP_FAILURE_SIGNAL;
933 else if (code == CLD_DUMPED)
934 f = SWAP_FAILURE_CORE_DUMP;
936 assert_not_reached("Unknown code");
938 if (f != SWAP_SUCCESS)
941 if (s->control_command) {
942 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
944 s->control_command = NULL;
945 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
948 log_full(f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
949 "%s swap process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
953 case SWAP_ACTIVATING:
954 case SWAP_ACTIVATING_SIGTERM:
955 case SWAP_ACTIVATING_SIGKILL:
957 if (f == SWAP_SUCCESS)
958 swap_enter_active(s, f);
960 swap_enter_dead(s, f);
963 case SWAP_DEACTIVATING:
964 case SWAP_DEACTIVATING_SIGKILL:
965 case SWAP_DEACTIVATING_SIGTERM:
967 if (f == SWAP_SUCCESS)
968 swap_enter_dead(s, f);
970 swap_enter_dead(s, f);
974 assert_not_reached("Uh, control process died at wrong time.");
977 /* Notify clients about changed exit status */
978 unit_add_to_dbus_queue(u);
980 /* Request a reload of /proc/swaps, so that following units
981 * can follow our state change */
982 u->manager->request_reload = true;
985 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
989 assert(elapsed == 1);
990 assert(w == &s->timer_watch);
994 case SWAP_ACTIVATING:
995 log_warning("%s activation timed out. Stopping.", u->id);
996 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
999 case SWAP_DEACTIVATING:
1000 log_warning("%s deactivation timed out. Stopping.", u->id);
1001 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1004 case SWAP_ACTIVATING_SIGTERM:
1005 if (s->kill_context.send_sigkill) {
1006 log_warning("%s activation timed out. Killing.", u->id);
1007 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1009 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->id);
1010 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1014 case SWAP_DEACTIVATING_SIGTERM:
1015 if (s->kill_context.send_sigkill) {
1016 log_warning("%s deactivation timed out. Killing.", u->id);
1017 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1019 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->id);
1020 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1024 case SWAP_ACTIVATING_SIGKILL:
1025 case SWAP_DEACTIVATING_SIGKILL:
1026 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->id);
1027 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1031 assert_not_reached("Timeout at wrong time.");
1035 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1041 rewind(m->proc_swaps);
1043 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1046 char *dev = NULL, *d;
1049 if ((k = fscanf(m->proc_swaps,
1050 "%ms " /* device/file */
1051 "%*s " /* type of swap */
1052 "%*s " /* swap size */
1054 "%i\n", /* priority */
1055 &dev, &prio)) != 2) {
1060 log_warning("Failed to parse /proc/swaps:%u.", i);
1071 k = swap_process_new_swap(m, d, prio, set_flags);
1081 int swap_dispatch_reload(Manager *m) {
1082 /* This function should go as soon as the kernel properly notifies us */
1084 if (_likely_(!m->request_reload))
1087 m->request_reload = false;
1089 return swap_fd_event(m, EPOLLPRI);
1092 int swap_fd_event(Manager *m, int events) {
1097 assert(events & EPOLLPRI);
1099 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1100 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1102 /* Reset flags, just in case, for late calls */
1103 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1104 Swap *swap = SWAP(u);
1106 swap->is_active = swap->just_activated = false;
1112 manager_dispatch_load_queue(m);
1114 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1115 Swap *swap = SWAP(u);
1117 if (!swap->is_active) {
1118 /* This has just been deactivated */
1120 swap->from_proc_swaps = false;
1121 swap_unset_proc_swaps(swap);
1123 switch (swap->state) {
1126 swap_enter_dead(swap, SWAP_SUCCESS);
1130 swap_set_state(swap, swap->state);
1134 } else if (swap->just_activated) {
1136 /* New swap entry */
1138 switch (swap->state) {
1142 swap_enter_active(swap, SWAP_SUCCESS);
1146 /* Nothing really changed, but let's
1147 * issue an notification call
1148 * nonetheless, in case somebody is
1149 * waiting for this. */
1150 swap_set_state(swap, swap->state);
1155 /* Reset the flags for later calls */
1156 swap->is_active = swap->just_activated = false;
1162 static Unit *swap_following(Unit *u) {
1164 Swap *other, *first = NULL;
1168 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1171 /* Make everybody follow the unit that's named after the swap
1172 * device in the kernel */
1174 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1175 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1178 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1179 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1188 static int swap_following_set(Unit *u, Set **_set) {
1197 if (LIST_JUST_US(same_proc_swaps, s)) {
1202 if (!(set = set_new(NULL, NULL)))
1205 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1206 if ((r = set_put(set, other)) < 0)
1209 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1210 if ((r = set_put(set, other)) < 0)
1221 static void swap_shutdown(Manager *m) {
1224 if (m->proc_swaps) {
1225 fclose(m->proc_swaps);
1226 m->proc_swaps = NULL;
1229 hashmap_free(m->swaps_by_proc_swaps);
1230 m->swaps_by_proc_swaps = NULL;
1233 static int swap_enumerate(Manager *m) {
1235 struct epoll_event ev;
1238 if (!m->proc_swaps) {
1239 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1240 return (errno == ENOENT) ? 0 : -errno;
1242 m->swap_watch.type = WATCH_SWAP;
1243 m->swap_watch.fd = fileno(m->proc_swaps);
1246 ev.events = EPOLLPRI;
1247 ev.data.ptr = &m->swap_watch;
1249 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
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);
1267 s->result = SWAP_SUCCESS;
1270 static int swap_kill(Unit *u, KillWho who, 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 (who == KILL_CONTROL || who == KILL_ALL)
1288 if (s->control_pid > 0)
1289 if (kill(s->control_pid, signo) < 0)
1292 if (who == KILL_ALL) {
1295 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1299 /* Exclude the control pid from being killed via the cgroup */
1300 if (s->control_pid > 0) {
1301 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
1308 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
1309 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1320 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1321 [SWAP_DEAD] = "dead",
1322 [SWAP_ACTIVATING] = "activating",
1323 [SWAP_ACTIVE] = "active",
1324 [SWAP_DEACTIVATING] = "deactivating",
1325 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1326 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1327 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1328 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1329 [SWAP_FAILED] = "failed"
1332 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1334 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1335 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1336 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1339 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1341 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1342 [SWAP_SUCCESS] = "success",
1343 [SWAP_FAILURE_RESOURCES] = "resources",
1344 [SWAP_FAILURE_TIMEOUT] = "timeout",
1345 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1346 [SWAP_FAILURE_SIGNAL] = "signal",
1347 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1350 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1352 const UnitVTable swap_vtable = {
1353 .object_size = sizeof(Swap),
1360 .no_instances = true,
1366 .coldplug = swap_coldplug,
1370 .start = swap_start,
1375 .serialize = swap_serialize,
1376 .deserialize_item = swap_deserialize_item,
1378 .active_state = swap_active_state,
1379 .sub_state_to_string = swap_sub_state_to_string,
1381 .check_gc = swap_check_gc,
1383 .sigchld_event = swap_sigchld_event,
1384 .timer_event = swap_timer_event,
1386 .reset_failed = swap_reset_failed,
1388 .bus_interface = "org.freedesktop.systemd1.Swap",
1389 .bus_message_handler = bus_swap_message_handler,
1390 .bus_invalidating_properties = bus_swap_invalidating_properties,
1392 .following = swap_following,
1393 .following_set = swap_following_set,
1395 .enumerate = swap_enumerate,
1396 .shutdown = swap_shutdown,
1398 .status_message_formats = {
1399 .starting_stopping = {
1400 [0] = "Activating swap %s...",
1401 [1] = "Deactivating swap %s...",
1403 .finished_start_job = {
1404 [JOB_DONE] = "Activated swap %s.",
1405 [JOB_FAILED] = "Failed to activate swap %s.",
1406 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1407 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1409 .finished_stop_job = {
1410 [JOB_DONE] = "Deactivated swap %s.",
1411 [JOB_FAILED] = "Failed deactivating swap %s.",
1412 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",