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) {
62 if (!s->parameters_proc_swaps.what)
65 /* Remove this unit from the chain of swaps which share the
66 * same kernel swap device. */
67 swaps = UNIT(s)->manager->swaps_by_proc_swaps;
68 first = hashmap_get(swaps, s->parameters_proc_swaps.what);
69 LIST_REMOVE(Swap, same_proc_swaps, first, s);
72 hashmap_remove_and_replace(swaps,
73 s->parameters_proc_swaps.what,
74 first->parameters_proc_swaps.what,
77 hashmap_remove(swaps, s->parameters_proc_swaps.what);
79 free(s->parameters_proc_swaps.what);
80 s->parameters_proc_swaps.what = NULL;
83 static void swap_init(Unit *u) {
87 assert(UNIT(s)->load_state == UNIT_STUB);
89 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
91 exec_context_init(&s->exec_context);
92 s->exec_context.std_output = u->manager->default_std_output;
93 s->exec_context.std_error = u->manager->default_std_error;
94 kill_context_init(&s->kill_context);
96 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
98 s->timer_watch.type = WATCH_INVALID;
100 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
102 UNIT(s)->ignore_on_isolate = true;
105 static void swap_unwatch_control_pid(Swap *s) {
108 if (s->control_pid <= 0)
111 unit_unwatch_pid(UNIT(s), s->control_pid);
115 static void swap_done(Unit *u) {
120 swap_unset_proc_swaps(s);
125 free(s->parameters_fragment.what);
126 s->parameters_fragment.what = NULL;
128 exec_context_done(&s->exec_context);
129 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
130 s->control_command = NULL;
132 swap_unwatch_control_pid(s);
134 unit_unwatch_timer(u, &s->timer_watch);
137 int swap_add_one_mount_link(Swap *s, Mount *m) {
143 if (UNIT(s)->load_state != UNIT_LOADED ||
144 UNIT(m)->load_state != UNIT_LOADED)
147 if (is_device_path(s->what))
150 if (!path_startswith(s->what, m->where))
153 if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
159 static int swap_add_mount_links(Swap *s) {
165 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_MOUNT])
166 if ((r = swap_add_one_mount_link(s, MOUNT(other))) < 0)
172 static int swap_add_device_links(Swap *s) {
180 if (s->from_fragment)
181 p = &s->parameters_fragment;
185 if (is_device_path(s->what))
186 return unit_add_node_link(UNIT(s), s->what,
187 !p->noauto && p->nofail &&
188 UNIT(s)->manager->running_as == SYSTEMD_SYSTEM);
190 /* File based swap devices need to be ordered after
191 * systemd-remount-fs.service, since they might need a
192 * writable file system. */
193 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
196 static int swap_add_default_dependencies(Swap *s) {
201 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
204 if (detect_container(NULL) > 0)
207 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
214 static int swap_verify(Swap *s) {
218 if (UNIT(s)->load_state != UNIT_LOADED)
221 if (!(e = unit_name_from_path(s->what, ".swap")))
224 b = unit_has_name(UNIT(s), e);
228 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->id);
232 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
233 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
240 static int swap_load(Unit *u) {
245 assert(u->load_state == UNIT_STUB);
247 /* Load a .swap file */
248 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
251 if (u->load_state == UNIT_LOADED) {
252 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
255 if (UNIT(s)->fragment_path)
256 s->from_fragment = true;
259 if (s->parameters_fragment.what)
260 s->what = strdup(s->parameters_fragment.what);
261 else if (s->parameters_proc_swaps.what)
262 s->what = strdup(s->parameters_proc_swaps.what);
264 s->what = unit_name_to_path(u->id);
270 path_kill_slashes(s->what);
272 if (!UNIT(s)->description)
273 if ((r = unit_set_description(u, s->what)) < 0)
276 if ((r = swap_add_device_links(s)) < 0)
279 if ((r = swap_add_mount_links(s)) < 0)
282 if ((r = unit_add_default_cgroups(u)) < 0)
285 if (UNIT(s)->default_dependencies)
286 if ((r = swap_add_default_dependencies(s)) < 0)
289 r = unit_exec_context_defaults(u, &s->exec_context);
294 return swap_verify(s);
297 static int swap_add_one(
300 const char *what_proc_swaps,
307 char *e = NULL, *wp = NULL;
315 assert(what_proc_swaps);
317 e = unit_name_from_path(what, ".swap");
321 u = manager_get_unit(m, e);
324 SWAP(u)->from_proc_swaps &&
325 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
331 u = unit_new(m, sizeof(Swap));
337 r = unit_add_name(u, e);
341 SWAP(u)->what = strdup(what);
342 if (!SWAP(u)->what) {
347 unit_add_to_load_queue(u);
351 p = &SWAP(u)->parameters_proc_swaps;
354 if (!(wp = strdup(what_proc_swaps))) {
359 if (!m->swaps_by_proc_swaps)
360 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
368 first = hashmap_get(m->swaps_by_proc_swaps, wp);
369 LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
371 if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
376 SWAP(u)->is_active = true;
377 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
380 SWAP(u)->from_proc_swaps = true;
382 p->priority = priority;
386 unit_add_to_dbus_queue(u);
393 log_warning("Failed to load swap unit: %s", strerror(-r));
404 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
410 if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
411 struct udev_device *d;
413 struct udev_list_entry *item = NULL, *first = NULL;
415 /* So this is a proper swap device. Create swap units
416 * for all names this swap device is known under */
418 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
421 dn = udev_device_get_devnode(d);
423 r = swap_add_one(m, dn, device, prio, false, false, set_flags);
425 /* Add additional units for all symlinks */
426 first = udev_device_get_devlinks_list_entry(d);
427 udev_list_entry_foreach(item, first) {
430 /* Don't bother with the /dev/block links */
431 p = udev_list_entry_get_name(item);
433 if (path_startswith(p, "/dev/block/"))
436 if (stat(p, &st) >= 0)
437 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
440 k = swap_add_one(m, p, device, prio, false, false, set_flags);
445 udev_device_unref(d);
448 k = swap_add_one(m, device, device, prio, false, false, set_flags);
455 static void swap_set_state(Swap *s, SwapState state) {
460 old_state = s->state;
463 if (state != SWAP_ACTIVATING &&
464 state != SWAP_ACTIVATING_SIGTERM &&
465 state != SWAP_ACTIVATING_SIGKILL &&
466 state != SWAP_DEACTIVATING &&
467 state != SWAP_DEACTIVATING_SIGTERM &&
468 state != SWAP_DEACTIVATING_SIGKILL) {
469 unit_unwatch_timer(UNIT(s), &s->timer_watch);
470 swap_unwatch_control_pid(s);
471 s->control_command = NULL;
472 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
475 if (state != old_state)
476 log_debug("%s changed %s -> %s",
478 swap_state_to_string(old_state),
479 swap_state_to_string(state));
481 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
484 static int swap_coldplug(Unit *u) {
486 SwapState new_state = SWAP_DEAD;
490 assert(s->state == SWAP_DEAD);
492 if (s->deserialized_state != s->state)
493 new_state = s->deserialized_state;
494 else if (s->from_proc_swaps)
495 new_state = SWAP_ACTIVE;
497 if (new_state != s->state) {
499 if (new_state == SWAP_ACTIVATING ||
500 new_state == SWAP_ACTIVATING_SIGTERM ||
501 new_state == SWAP_ACTIVATING_SIGKILL ||
502 new_state == SWAP_DEACTIVATING ||
503 new_state == SWAP_DEACTIVATING_SIGTERM ||
504 new_state == SWAP_DEACTIVATING_SIGKILL) {
506 if (s->control_pid <= 0)
509 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
512 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
516 swap_set_state(s, new_state);
522 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
529 if (s->from_proc_swaps)
530 p = &s->parameters_proc_swaps;
531 else if (s->from_fragment)
532 p = &s->parameters_fragment;
540 "%sFrom /proc/swaps: %s\n"
541 "%sFrom fragment: %s\n",
542 prefix, swap_state_to_string(s->state),
543 prefix, swap_result_to_string(s->result),
545 prefix, yes_no(s->from_proc_swaps),
546 prefix, yes_no(s->from_fragment));
554 prefix, yes_no(p->noauto),
555 prefix, yes_no(p->nofail));
557 if (s->control_pid > 0)
559 "%sControl PID: %lu\n",
560 prefix, (unsigned long) s->control_pid);
562 exec_context_dump(&s->exec_context, f, prefix);
563 kill_context_dump(&s->kill_context, f, prefix);
566 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
574 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
577 if ((r = exec_spawn(c,
581 UNIT(s)->manager->environment,
585 UNIT(s)->manager->confirm_spawn,
586 UNIT(s)->cgroup_bondings,
587 UNIT(s)->cgroup_attributes,
594 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
595 /* FIXME: we need to do something here */
603 unit_unwatch_timer(UNIT(s), &s->timer_watch);
608 static void swap_enter_dead(Swap *s, SwapResult f) {
611 if (f != SWAP_SUCCESS)
614 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
617 static void swap_enter_active(Swap *s, SwapResult f) {
620 if (f != SWAP_SUCCESS)
623 swap_set_state(s, SWAP_ACTIVE);
626 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
629 bool wait_for_exit = false;
633 if (f != SWAP_SUCCESS)
636 if (s->kill_context.kill_mode != KILL_NONE) {
637 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
638 state == SWAP_DEACTIVATING_SIGTERM) ? s->kill_context.kill_signal : SIGKILL;
640 if (s->control_pid > 0) {
641 if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
643 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
645 wait_for_exit = true;
648 if (s->kill_context.kill_mode == KILL_CONTROL_GROUP) {
650 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
655 /* Exclude the control pid from being killed via the cgroup */
656 if (s->control_pid > 0)
657 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
660 r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
662 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
663 log_warning("Failed to kill control group: %s", strerror(-r));
665 wait_for_exit = true;
673 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
676 swap_set_state(s, state);
678 swap_enter_dead(s, SWAP_SUCCESS);
683 log_warning("%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
685 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
691 static void swap_enter_activating(Swap *s) {
696 s->control_command_id = SWAP_EXEC_ACTIVATE;
697 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
699 if (s->from_fragment)
700 priority = s->parameters_fragment.priority;
707 snprintf(p, sizeof(p), "%i", priority);
710 r = exec_command_set(
718 r = exec_command_set(
727 swap_unwatch_control_pid(s);
729 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
732 swap_set_state(s, SWAP_ACTIVATING);
737 log_warning("%s failed to run 'swapon' task: %s", UNIT(s)->id, strerror(-r));
738 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
741 static void swap_enter_deactivating(Swap *s) {
746 s->control_command_id = SWAP_EXEC_DEACTIVATE;
747 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
749 if ((r = exec_command_set(
756 swap_unwatch_control_pid(s);
758 if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
761 swap_set_state(s, SWAP_DEACTIVATING);
766 log_warning("%s failed to run 'swapoff' task: %s", UNIT(s)->id, strerror(-r));
767 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
770 static int swap_start(Unit *u) {
775 /* We cannot fulfill this request right now, try again later
778 if (s->state == SWAP_DEACTIVATING ||
779 s->state == SWAP_DEACTIVATING_SIGTERM ||
780 s->state == SWAP_DEACTIVATING_SIGKILL ||
781 s->state == SWAP_ACTIVATING_SIGTERM ||
782 s->state == SWAP_ACTIVATING_SIGKILL)
785 if (s->state == SWAP_ACTIVATING)
788 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
790 if (detect_container(NULL) > 0)
793 s->result = SWAP_SUCCESS;
794 swap_enter_activating(s);
798 static int swap_stop(Unit *u) {
803 if (s->state == SWAP_DEACTIVATING ||
804 s->state == SWAP_DEACTIVATING_SIGTERM ||
805 s->state == SWAP_DEACTIVATING_SIGKILL ||
806 s->state == SWAP_ACTIVATING_SIGTERM ||
807 s->state == SWAP_ACTIVATING_SIGKILL)
810 assert(s->state == SWAP_ACTIVATING ||
811 s->state == SWAP_ACTIVE);
813 if (detect_container(NULL) > 0)
816 swap_enter_deactivating(s);
820 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
827 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
828 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
830 if (s->control_pid > 0)
831 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
833 if (s->control_command_id >= 0)
834 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
839 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
845 if (streq(key, "state")) {
848 if ((state = swap_state_from_string(value)) < 0)
849 log_debug("Failed to parse state value %s", value);
851 s->deserialized_state = state;
852 } else if (streq(key, "result")) {
855 f = swap_result_from_string(value);
857 log_debug("Failed to parse result value %s", value);
858 else if (f != SWAP_SUCCESS)
860 } else if (streq(key, "control-pid")) {
863 if (parse_pid(value, &pid) < 0)
864 log_debug("Failed to parse control-pid value %s", value);
866 s->control_pid = pid;
868 } else if (streq(key, "control-command")) {
871 if ((id = swap_exec_command_from_string(value)) < 0)
872 log_debug("Failed to parse exec-command value %s", value);
874 s->control_command_id = id;
875 s->control_command = s->exec_command + id;
879 log_debug("Unknown serialization key '%s'", key);
884 static UnitActiveState swap_active_state(Unit *u) {
887 return state_translation_table[SWAP(u)->state];
890 static const char *swap_sub_state_to_string(Unit *u) {
893 return swap_state_to_string(SWAP(u)->state);
896 static bool swap_check_gc(Unit *u) {
901 return s->from_proc_swaps;
904 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
911 if (pid != s->control_pid)
916 if (is_clean_exit(code, status, NULL))
918 else if (code == CLD_EXITED)
919 f = SWAP_FAILURE_EXIT_CODE;
920 else if (code == CLD_KILLED)
921 f = SWAP_FAILURE_SIGNAL;
922 else if (code == CLD_DUMPED)
923 f = SWAP_FAILURE_CORE_DUMP;
925 assert_not_reached("Unknown code");
927 if (f != SWAP_SUCCESS)
930 if (s->control_command) {
931 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
933 s->control_command = NULL;
934 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
937 log_full(f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
938 "%s swap process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
942 case SWAP_ACTIVATING:
943 case SWAP_ACTIVATING_SIGTERM:
944 case SWAP_ACTIVATING_SIGKILL:
946 if (f == SWAP_SUCCESS)
947 swap_enter_active(s, f);
949 swap_enter_dead(s, f);
952 case SWAP_DEACTIVATING:
953 case SWAP_DEACTIVATING_SIGKILL:
954 case SWAP_DEACTIVATING_SIGTERM:
956 if (f == SWAP_SUCCESS)
957 swap_enter_dead(s, f);
959 swap_enter_dead(s, f);
963 assert_not_reached("Uh, control process died at wrong time.");
966 /* Notify clients about changed exit status */
967 unit_add_to_dbus_queue(u);
969 /* Request a reload of /proc/swaps, so that following units
970 * can follow our state change */
971 u->manager->request_reload = true;
974 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
978 assert(elapsed == 1);
979 assert(w == &s->timer_watch);
983 case SWAP_ACTIVATING:
984 log_warning("%s activation timed out. Stopping.", u->id);
985 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
988 case SWAP_DEACTIVATING:
989 log_warning("%s deactivation timed out. Stopping.", u->id);
990 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
993 case SWAP_ACTIVATING_SIGTERM:
994 if (s->kill_context.send_sigkill) {
995 log_warning("%s activation timed out. Killing.", u->id);
996 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
998 log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->id);
999 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1003 case SWAP_DEACTIVATING_SIGTERM:
1004 if (s->kill_context.send_sigkill) {
1005 log_warning("%s deactivation timed out. Killing.", u->id);
1006 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1008 log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->id);
1009 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1013 case SWAP_ACTIVATING_SIGKILL:
1014 case SWAP_DEACTIVATING_SIGKILL:
1015 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->id);
1016 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1020 assert_not_reached("Timeout at wrong time.");
1024 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1030 rewind(m->proc_swaps);
1032 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1035 char *dev = NULL, *d;
1038 if ((k = fscanf(m->proc_swaps,
1039 "%ms " /* device/file */
1040 "%*s " /* type of swap */
1041 "%*s " /* swap size */
1043 "%i\n", /* priority */
1044 &dev, &prio)) != 2) {
1049 log_warning("Failed to parse /proc/swaps:%u.", i);
1060 k = swap_process_new_swap(m, d, prio, set_flags);
1070 int swap_dispatch_reload(Manager *m) {
1071 /* This function should go as soon as the kernel properly notifies us */
1073 if (_likely_(!m->request_reload))
1076 m->request_reload = false;
1078 return swap_fd_event(m, EPOLLPRI);
1081 int swap_fd_event(Manager *m, int events) {
1086 assert(events & EPOLLPRI);
1088 if ((r = swap_load_proc_swaps(m, true)) < 0) {
1089 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1091 /* Reset flags, just in case, for late calls */
1092 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1093 Swap *swap = SWAP(u);
1095 swap->is_active = swap->just_activated = false;
1101 manager_dispatch_load_queue(m);
1103 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1104 Swap *swap = SWAP(u);
1106 if (!swap->is_active) {
1107 /* This has just been deactivated */
1109 swap->from_proc_swaps = false;
1110 swap_unset_proc_swaps(swap);
1112 switch (swap->state) {
1115 swap_enter_dead(swap, SWAP_SUCCESS);
1119 swap_set_state(swap, swap->state);
1123 } else if (swap->just_activated) {
1125 /* New swap entry */
1127 switch (swap->state) {
1131 swap_enter_active(swap, SWAP_SUCCESS);
1135 /* Nothing really changed, but let's
1136 * issue an notification call
1137 * nonetheless, in case somebody is
1138 * waiting for this. */
1139 swap_set_state(swap, swap->state);
1144 /* Reset the flags for later calls */
1145 swap->is_active = swap->just_activated = false;
1151 static Unit *swap_following(Unit *u) {
1153 Swap *other, *first = NULL;
1157 if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1160 /* Make everybody follow the unit that's named after the swap
1161 * device in the kernel */
1163 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1164 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1167 LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1168 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1177 static int swap_following_set(Unit *u, Set **_set) {
1186 if (LIST_JUST_US(same_proc_swaps, s)) {
1191 if (!(set = set_new(NULL, NULL)))
1194 LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1195 if ((r = set_put(set, other)) < 0)
1198 LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1199 if ((r = set_put(set, other)) < 0)
1210 static void swap_shutdown(Manager *m) {
1213 if (m->proc_swaps) {
1214 fclose(m->proc_swaps);
1215 m->proc_swaps = NULL;
1218 hashmap_free(m->swaps_by_proc_swaps);
1219 m->swaps_by_proc_swaps = NULL;
1222 static int swap_enumerate(Manager *m) {
1224 struct epoll_event ev;
1227 if (!m->proc_swaps) {
1228 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1229 return (errno == ENOENT) ? 0 : -errno;
1231 m->swap_watch.type = WATCH_SWAP;
1232 m->swap_watch.fd = fileno(m->proc_swaps);
1235 ev.events = EPOLLPRI;
1236 ev.data.ptr = &m->swap_watch;
1238 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1242 if ((r = swap_load_proc_swaps(m, false)) < 0)
1248 static void swap_reset_failed(Unit *u) {
1253 if (s->state == SWAP_FAILED)
1254 swap_set_state(s, SWAP_DEAD);
1256 s->result = SWAP_SUCCESS;
1259 static int swap_kill(Unit *u, KillWho who, int signo, DBusError *error) {
1262 Set *pid_set = NULL;
1266 if (who == KILL_MAIN) {
1267 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1271 if (s->control_pid <= 0 && who == KILL_CONTROL) {
1272 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1276 if (who == KILL_CONTROL || who == KILL_ALL)
1277 if (s->control_pid > 0)
1278 if (kill(s->control_pid, signo) < 0)
1281 if (who == KILL_ALL) {
1284 pid_set = set_new(trivial_hash_func, trivial_compare_func);
1288 /* Exclude the control pid from being killed via the cgroup */
1289 if (s->control_pid > 0) {
1290 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
1297 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
1298 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1309 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1310 [SWAP_DEAD] = "dead",
1311 [SWAP_ACTIVATING] = "activating",
1312 [SWAP_ACTIVE] = "active",
1313 [SWAP_DEACTIVATING] = "deactivating",
1314 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1315 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1316 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1317 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1318 [SWAP_FAILED] = "failed"
1321 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1323 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1324 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1325 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1328 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1330 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1331 [SWAP_SUCCESS] = "success",
1332 [SWAP_FAILURE_RESOURCES] = "resources",
1333 [SWAP_FAILURE_TIMEOUT] = "timeout",
1334 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1335 [SWAP_FAILURE_SIGNAL] = "signal",
1336 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1339 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1341 const UnitVTable swap_vtable = {
1342 .object_size = sizeof(Swap),
1343 .exec_context_offset = offsetof(Swap, exec_context),
1351 .no_instances = true,
1357 .coldplug = swap_coldplug,
1361 .start = swap_start,
1366 .serialize = swap_serialize,
1367 .deserialize_item = swap_deserialize_item,
1369 .active_state = swap_active_state,
1370 .sub_state_to_string = swap_sub_state_to_string,
1372 .check_gc = swap_check_gc,
1374 .sigchld_event = swap_sigchld_event,
1375 .timer_event = swap_timer_event,
1377 .reset_failed = swap_reset_failed,
1379 .bus_interface = "org.freedesktop.systemd1.Swap",
1380 .bus_message_handler = bus_swap_message_handler,
1381 .bus_invalidating_properties = bus_swap_invalidating_properties,
1383 .following = swap_following,
1384 .following_set = swap_following_set,
1386 .enumerate = swap_enumerate,
1387 .shutdown = swap_shutdown,
1389 .status_message_formats = {
1390 .starting_stopping = {
1391 [0] = "Activating swap %s...",
1392 [1] = "Deactivating swap %s...",
1394 .finished_start_job = {
1395 [JOB_DONE] = "Activated swap %s.",
1396 [JOB_FAILED] = "Failed to activate swap %s.",
1397 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1398 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1400 .finished_stop_job = {
1401 [JOB_DONE] = "Deactivated swap %s.",
1402 [JOB_FAILED] = "Failed deactivating swap %s.",
1403 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",