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 r = manager_load_unit(UNIT(s)->manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu);
181 return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
184 static int swap_add_device_links(Swap *s) {
192 if (s->from_fragment)
193 p = &s->parameters_fragment;
197 if (is_device_path(s->what))
198 return unit_add_node_link(UNIT(s), s->what,
199 !p->noauto && p->nofail &&
200 UNIT(s)->manager->running_as == SYSTEMD_SYSTEM);
202 /* File based swap devices need to be ordered after
203 * systemd-remount-fs.service, since they might need a
204 * writable file system. */
205 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
208 static int swap_add_default_dependencies(Swap *s) {
213 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
216 if (detect_container(NULL) > 0)
219 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
226 static int swap_verify(Swap *s) {
230 if (UNIT(s)->load_state != UNIT_LOADED)
233 if (!(e = unit_name_from_path(s->what, ".swap")))
236 b = unit_has_name(UNIT(s), e);
240 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->id);
244 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
245 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
252 static int swap_load(Unit *u) {
257 assert(u->load_state == UNIT_STUB);
259 /* Load a .swap file */
260 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
263 if (u->load_state == UNIT_LOADED) {
264 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
267 if (UNIT(s)->fragment_path)
268 s->from_fragment = true;
271 if (s->parameters_fragment.what)
272 s->what = strdup(s->parameters_fragment.what);
273 else if (s->parameters_proc_swaps.what)
274 s->what = strdup(s->parameters_proc_swaps.what);
276 s->what = unit_name_to_path(u->id);
282 path_kill_slashes(s->what);
284 if (!UNIT(s)->description)
285 if ((r = unit_set_description(u, s->what)) < 0)
288 if ((r = swap_add_device_links(s)) < 0)
291 if ((r = swap_add_mount_links(s)) < 0)
294 if ((r = swap_add_target_links(s)) < 0)
297 if ((r = unit_add_default_cgroups(u)) < 0)
300 if (UNIT(s)->default_dependencies)
301 if ((r = swap_add_default_dependencies(s)) < 0)
304 r = unit_exec_context_defaults(u, &s->exec_context);
309 return swap_verify(s);
312 static int swap_add_one(
315 const char *what_proc_swaps,
322 char *e = NULL, *wp = NULL;
330 assert(what_proc_swaps);
332 e = unit_name_from_path(what, ".swap");
336 u = manager_get_unit(m, e);
339 SWAP(u)->from_proc_swaps &&
340 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
346 u = unit_new(m, sizeof(Swap));
352 r = unit_add_name(u, e);
356 SWAP(u)->what = strdup(what);
357 if (!SWAP(u)->what) {
362 unit_add_to_load_queue(u);
366 p = &SWAP(u)->parameters_proc_swaps;
369 if (!(wp = strdup(what_proc_swaps))) {
374 if (!m->swaps_by_proc_swaps)
375 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
383 first = hashmap_get(m->swaps_by_proc_swaps, wp);
384 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
386 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
391 SWAP(u)->is_active = true;
392 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
395 SWAP(u)->from_proc_swaps = true;
397 p->priority = priority;
401 unit_add_to_dbus_queue(u);
408 log_warning("Failed to load swap unit: %s", strerror(-r));
419 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
425 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
426 struct udev_device *d;
428 struct udev_list_entry *item = NULL, *first = NULL;
430 /* So this is a proper swap device. Create swap units
431 * for all names this swap device is known under */
433 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
436 dn = udev_device_get_devnode(d);
438 r = swap_add_one(m, dn, device, prio, false, false, set_flags);
440 /* Add additional units for all symlinks */
441 first = udev_device_get_devlinks_list_entry(d);
442 udev_list_entry_foreach(item, first) {
445 /* Don't bother with the /dev/block links */
446 p = udev_list_entry_get_name(item);
448 if (path_startswith(p, "/dev/block/"))
451 if (stat(p, &st) >= 0)
452 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
455 k = swap_add_one(m, p, device, prio, false, false, set_flags);
460 udev_device_unref(d);
463 k = swap_add_one(m, device, device, prio, false, false, set_flags);
470 static void swap_set_state(Swap *s, SwapState state) {
475 old_state = s->state;
478 if (state != SWAP_ACTIVATING &&
479 state != SWAP_ACTIVATING_SIGTERM &&
480 state != SWAP_ACTIVATING_SIGKILL &&
481 state != SWAP_DEACTIVATING &&
482 state != SWAP_DEACTIVATING_SIGTERM &&
483 state != SWAP_DEACTIVATING_SIGKILL) {
484 unit_unwatch_timer(UNIT(s), &s->timer_watch);
485 swap_unwatch_control_pid(s);
486 s->control_command = NULL;
487 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
490 if (state != old_state)
491 log_debug("%s changed %s -> %s",
493 swap_state_to_string(old_state),
494 swap_state_to_string(state));
496 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
499 static int swap_coldplug(Unit *u) {
501 SwapState new_state = SWAP_DEAD;
505 assert(s->state == SWAP_DEAD);
507 if (s->deserialized_state != s->state)
508 new_state = s->deserialized_state;
509 else if (s->from_proc_swaps)
510 new_state = SWAP_ACTIVE;
512 if (new_state != s->state) {
514 if (new_state == SWAP_ACTIVATING ||
515 new_state == SWAP_ACTIVATING_SIGTERM ||
516 new_state == SWAP_ACTIVATING_SIGKILL ||
517 new_state == SWAP_DEACTIVATING ||
518 new_state == SWAP_DEACTIVATING_SIGTERM ||
519 new_state == SWAP_DEACTIVATING_SIGKILL) {
521 if (s->control_pid <= 0)
524 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
527 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
531 swap_set_state(s, new_state);
537 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
544 if (s->from_proc_swaps)
545 p = &s->parameters_proc_swaps;
546 else if (s->from_fragment)
547 p = &s->parameters_fragment;
555 "%sFrom /proc/swaps: %s\n"
556 "%sFrom fragment: %s\n",
557 prefix, swap_state_to_string(s->state),
558 prefix, swap_result_to_string(s->result),
560 prefix, yes_no(s->from_proc_swaps),
561 prefix, yes_no(s->from_fragment));
569 prefix, yes_no(p->noauto),
570 prefix, yes_no(p->nofail));
572 if (s->control_pid > 0)
574 "%sControl PID: %lu\n",
575 prefix, (unsigned long) s->control_pid);
577 exec_context_dump(&s->exec_context, f, prefix);
578 kill_context_dump(&s->kill_context, f, prefix);
581 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
589 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
592 if ((r = exec_spawn(c,
596 UNIT(s)->manager->environment,
600 UNIT(s)->manager->confirm_spawn,
601 UNIT(s)->cgroup_bondings,
602 UNIT(s)->cgroup_attributes,
609 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
610 /* FIXME: we need to do something here */
618 unit_unwatch_timer(UNIT(s), &s->timer_watch);
623 static void swap_enter_dead(Swap *s, SwapResult f) {
626 if (f != SWAP_SUCCESS)
629 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
632 static void swap_enter_active(Swap *s, SwapResult f) {
635 if (f != SWAP_SUCCESS)
638 swap_set_state(s, SWAP_ACTIVE);
641 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
644 bool wait_for_exit = false;
648 if (f != SWAP_SUCCESS)
651 if (s->kill_context.kill_mode != KILL_NONE) {
652 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
653 state == SWAP_DEACTIVATING_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
655 if (s->control_pid > 0) {
656 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
658 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
660 wait_for_exit = true;
663 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
665 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
670 /* Exclude the control pid from being killed via the cgroup */
671 if (s->control_pid > 0)
672 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
675 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
677 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
678 log_warning("Failed to kill control group: %s", strerror(-r));
680 wait_for_exit = true;
688 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
691 swap_set_state(s, state);
693 swap_enter_dead(s, SWAP_SUCCESS);
698 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
700 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
706 static void swap_enter_activating(Swap *s) {
711 s->control_command_id = SWAP_EXEC_ACTIVATE;
712 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
714 if (s->from_fragment)
715 priority = s->parameters_fragment.priority;
722 snprintf(p, sizeof(p), "%i", priority);
725 r = exec_command_set(
733 r = exec_command_set(
742 swap_unwatch_control_pid(s);
744 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
747 swap_set_state(s, SWAP_ACTIVATING);
752 log_warning("%s failed to run 'swapon' task: %s", UNIT(s)->id, strerror(-r));
753 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
756 static void swap_enter_deactivating(Swap *s) {
761 s->control_command_id = SWAP_EXEC_DEACTIVATE;
762 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
764 if ((r = exec_command_set(
771 swap_unwatch_control_pid(s);
773 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
776 swap_set_state(s, SWAP_DEACTIVATING);
781 log_warning("%s failed to run 'swapoff' task: %s", UNIT(s)->id, strerror(-r));
782 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
785 static int swap_start(Unit *u) {
790 /* We cannot fulfill this request right now, try again later
793 if (s->state == SWAP_DEACTIVATING ||
794 s->state == SWAP_DEACTIVATING_SIGTERM ||
795 s->state == SWAP_DEACTIVATING_SIGKILL ||
796 s->state == SWAP_ACTIVATING_SIGTERM ||
797 s->state == SWAP_ACTIVATING_SIGKILL)
800 if (s->state == SWAP_ACTIVATING)
803 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
805 if (detect_container(NULL) > 0)
808 s->result = SWAP_SUCCESS;
809 swap_enter_activating(s);
813 static int swap_stop(Unit *u) {
818 if (s->state == SWAP_DEACTIVATING ||
819 s->state == SWAP_DEACTIVATING_SIGTERM ||
820 s->state == SWAP_DEACTIVATING_SIGKILL ||
821 s->state == SWAP_ACTIVATING_SIGTERM ||
822 s->state == SWAP_ACTIVATING_SIGKILL)
825 assert(s->state == SWAP_ACTIVATING ||
826 s->state == SWAP_ACTIVE);
828 if (detect_container(NULL) > 0)
831 swap_enter_deactivating(s);
835 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
842 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
843 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
845 if (s->control_pid > 0)
846 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
848 if (s->control_command_id >= 0)
849 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
854 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
860 if (streq(key, "state")) {
863 if ((state = swap_state_from_string(value)) < 0)
864 log_debug("Failed to parse state value %s", value);
866 s->deserialized_state = state;
867 } else if (streq(key, "result")) {
870 f = swap_result_from_string(value);
872 log_debug("Failed to parse result value %s", value);
873 else if (f != SWAP_SUCCESS)
875 } else if (streq(key, "control-pid")) {
878 if (parse_pid(value, &pid) < 0)
879 log_debug("Failed to parse control-pid value %s", value);
881 s->control_pid = pid;
883 } else if (streq(key, "control-command")) {
886 if ((id = swap_exec_command_from_string(value)) < 0)
887 log_debug("Failed to parse exec-command value %s", value);
889 s->control_command_id = id;
890 s->control_command = s->exec_command + id;
894 log_debug("Unknown serialization key '%s'", key);
899 static UnitActiveState swap_active_state(Unit *u) {
902 return state_translation_table[SWAP(u)->state];
905 static const char *swap_sub_state_to_string(Unit *u) {
908 return swap_state_to_string(SWAP(u)->state);
911 static bool swap_check_gc(Unit *u) {
916 return s->from_proc_swaps;
919 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
926 if (pid != s->control_pid)
931 if (is_clean_exit(code, status, NULL))
933 else if (code == CLD_EXITED)
934 f = SWAP_FAILURE_EXIT_CODE;
935 else if (code == CLD_KILLED)
936 f = SWAP_FAILURE_SIGNAL;
937 else if (code == CLD_DUMPED)
938 f = SWAP_FAILURE_CORE_DUMP;
940 assert_not_reached("Unknown code");
942 if (f != SWAP_SUCCESS)
945 if (s->control_command) {
946 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
948 s->control_command = NULL;
949 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
952 log_full(f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
953 "%s swap process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
957 case SWAP_ACTIVATING:
958 case SWAP_ACTIVATING_SIGTERM:
959 case SWAP_ACTIVATING_SIGKILL:
961 if (f == SWAP_SUCCESS)
962 swap_enter_active(s, f);
964 swap_enter_dead(s, f);
967 case SWAP_DEACTIVATING:
968 case SWAP_DEACTIVATING_SIGKILL:
969 case SWAP_DEACTIVATING_SIGTERM:
971 if (f == SWAP_SUCCESS)
972 swap_enter_dead(s, f);
974 swap_enter_dead(s, f);
978 assert_not_reached("Uh, control process died at wrong time.");
981 /* Notify clients about changed exit status */
982 unit_add_to_dbus_queue(u);
984 /* Request a reload of /proc/swaps, so that following units
985 * can follow our state change */
986 u->manager->request_reload = true;
989 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
993 assert(elapsed == 1);
994 assert(w == &s->timer_watch);
998 case SWAP_ACTIVATING:
999 log_warning("%s activation timed out. Stopping.", u->id);
1000 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1003 case SWAP_DEACTIVATING:
1004 log_warning("%s deactivation timed out. Stopping.", u->id);
1005 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1008 case SWAP_ACTIVATING_SIGTERM:
1009 if (s->kill_context.send_sigkill) {
1010 log_warning("%s activation timed out. Killing.", u->id);
1011 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1013 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->id);
1014 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1018 case SWAP_DEACTIVATING_SIGTERM:
1019 if (s->kill_context.send_sigkill) {
1020 log_warning("%s deactivation timed out. Killing.", u->id);
1021 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1023 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->id);
1024 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1028 case SWAP_ACTIVATING_SIGKILL:
1029 case SWAP_DEACTIVATING_SIGKILL:
1030 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->id);
1031 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1035 assert_not_reached("Timeout at wrong time.");
1039 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1045 rewind(m->proc_swaps);
1047 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1050 char *dev = NULL, *d;
1053 if ((k = fscanf(m->proc_swaps,
1054 "%ms " /* device/file */
1055 "%*s " /* type of swap */
1056 "%*s " /* swap size */
1058 "%i\n", /* priority */
1059 &dev, &prio)) != 2) {
1064 log_warning("Failed to parse /proc/swaps:%u.", i);
1075 k = swap_process_new_swap(m, d, prio, set_flags);
1085 int swap_dispatch_reload(Manager *m) {
1086 /* This function should go as soon as the kernel properly notifies us */
1088 if (_likely_(!m->request_reload))
1091 m->request_reload = false;
1093 return swap_fd_event(m, EPOLLPRI);
1096 int swap_fd_event(Manager *m, int events) {
1101 assert(events & EPOLLPRI);
1103 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1104 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1106 /* Reset flags, just in case, for late calls */
1107 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1108 Swap *swap = SWAP(u);
1110 swap->is_active = swap->just_activated = false;
1116 manager_dispatch_load_queue(m);
1118 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1119 Swap *swap = SWAP(u);
1121 if (!swap->is_active) {
1122 /* This has just been deactivated */
1124 swap->from_proc_swaps = false;
1125 swap_unset_proc_swaps(swap);
1127 switch (swap->state) {
1130 swap_enter_dead(swap, SWAP_SUCCESS);
1134 swap_set_state(swap, swap->state);
1138 } else if (swap->just_activated) {
1140 /* New swap entry */
1142 switch (swap->state) {
1146 swap_enter_active(swap, SWAP_SUCCESS);
1150 /* Nothing really changed, but let's
1151 * issue an notification call
1152 * nonetheless, in case somebody is
1153 * waiting for this. */
1154 swap_set_state(swap, swap->state);
1159 /* Reset the flags for later calls */
1160 swap->is_active = swap->just_activated = false;
1166 static Unit *swap_following(Unit *u) {
1168 Swap *other, *first = NULL;
1172 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1175 /* Make everybody follow the unit that's named after the swap
1176 * device in the kernel */
1178 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1179 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1182 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1183 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1192 static int swap_following_set(Unit *u, Set **_set) {
1201 if (LIST_JUST_US(same_proc_swaps, s)) {
1206 if (!(set = set_new(NULL, NULL)))
1209 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1210 if ((r = set_put(set, other)) < 0)
1213 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1214 if ((r = set_put(set, other)) < 0)
1225 static void swap_shutdown(Manager *m) {
1228 if (m->proc_swaps) {
1229 fclose(m->proc_swaps);
1230 m->proc_swaps = NULL;
1233 hashmap_free(m->swaps_by_proc_swaps);
1234 m->swaps_by_proc_swaps = NULL;
1237 static int swap_enumerate(Manager *m) {
1239 struct epoll_event ev;
1242 if (!m->proc_swaps) {
1243 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1244 return (errno == ENOENT) ? 0 : -errno;
1246 m->swap_watch.type = WATCH_SWAP;
1247 m->swap_watch.fd = fileno(m->proc_swaps);
1250 ev.events = EPOLLPRI;
1251 ev.data.ptr = &m->swap_watch;
1253 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1257 if ((r = swap_load_proc_swaps(m, false)) < 0)
1263 static void swap_reset_failed(Unit *u) {
1268 if (s->state == SWAP_FAILED)
1269 swap_set_state(s, SWAP_DEAD);
1271 s->result = SWAP_SUCCESS;
1274 static int swap_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1277 Set *pid_set = NULL;
1281 if (who == KILL_MAIN) {
1282 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1286 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1287 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1291 if (who == KILL_CONTROL || who == KILL_ALL)
1292 if (s->control_pid > 0)
1293 if (kill(s->control_pid, signo) < 0)
1296 if (who == KILL_ALL) {
1299 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1303 /* Exclude the control pid from being killed via the cgroup */
1304 if (s->control_pid > 0) {
1305 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
1312 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
1313 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1324 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1325 [SWAP_DEAD] = "dead",
1326 [SWAP_ACTIVATING] = "activating",
1327 [SWAP_ACTIVE] = "active",
1328 [SWAP_DEACTIVATING] = "deactivating",
1329 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1330 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1331 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1332 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1333 [SWAP_FAILED] = "failed"
1336 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1338 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1339 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1340 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1343 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1345 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1346 [SWAP_SUCCESS] = "success",
1347 [SWAP_FAILURE_RESOURCES] = "resources",
1348 [SWAP_FAILURE_TIMEOUT] = "timeout",
1349 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1350 [SWAP_FAILURE_SIGNAL] = "signal",
1351 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1354 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1356 const UnitVTable swap_vtable = {
1357 .object_size = sizeof(Swap),
1358 .exec_context_offset = offsetof(Swap, exec_context),
1366 .no_instances = true,
1372 .coldplug = swap_coldplug,
1376 .start = swap_start,
1381 .serialize = swap_serialize,
1382 .deserialize_item = swap_deserialize_item,
1384 .active_state = swap_active_state,
1385 .sub_state_to_string = swap_sub_state_to_string,
1387 .check_gc = swap_check_gc,
1389 .sigchld_event = swap_sigchld_event,
1390 .timer_event = swap_timer_event,
1392 .reset_failed = swap_reset_failed,
1394 .bus_interface = "org.freedesktop.systemd1.Swap",
1395 .bus_message_handler = bus_swap_message_handler,
1396 .bus_invalidating_properties = bus_swap_invalidating_properties,
1398 .following = swap_following,
1399 .following_set = swap_following_set,
1401 .enumerate = swap_enumerate,
1402 .shutdown = swap_shutdown,
1404 .status_message_formats = {
1405 .starting_stopping = {
1406 [0] = "Activating swap %s...",
1407 [1] = "Deactivating swap %s...",
1409 .finished_start_job = {
1410 [JOB_DONE] = "Activated swap %s.",
1411 [JOB_FAILED] = "Failed to activate swap %s.",
1412 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1413 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1415 .finished_stop_job = {
1416 [JOB_DONE] = "Deactivated swap %s.",
1417 [JOB_FAILED] = "Failed deactivating swap %s.",
1418 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",