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 #include "udev-util.h"
45 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
46 [SWAP_DEAD] = UNIT_INACTIVE,
47 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
48 [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
49 [SWAP_ACTIVE] = UNIT_ACTIVE,
50 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
51 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
52 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
53 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
54 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
55 [SWAP_FAILED] = UNIT_FAILED
58 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
59 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
61 static void swap_unset_proc_swaps(Swap *s) {
64 if (!s->from_proc_swaps)
67 free(s->parameters_proc_swaps.what);
68 s->parameters_proc_swaps.what = NULL;
70 s->from_proc_swaps = false;
73 static int swap_set_devnode(Swap *s, const char *devnode) {
80 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &string_hash_ops);
84 swaps = UNIT(s)->manager->swaps_by_devnode;
87 first = hashmap_get(swaps, s->devnode);
89 LIST_REMOVE(same_devnode, first, s);
91 hashmap_replace(swaps, first->devnode, first);
93 hashmap_remove(swaps, s->devnode);
100 s->devnode = strdup(devnode);
104 first = hashmap_get(swaps, s->devnode);
105 LIST_PREPEND(same_devnode, first, s);
107 return hashmap_replace(swaps, first->devnode, first);
113 static void swap_init(Unit *u) {
117 assert(UNIT(s)->load_state == UNIT_STUB);
119 s->timeout_usec = u->manager->default_timeout_start_usec;
121 s->exec_context.std_output = u->manager->default_std_output;
122 s->exec_context.std_error = u->manager->default_std_error;
124 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
126 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
128 u->ignore_on_isolate = true;
131 static void swap_unwatch_control_pid(Swap *s) {
134 if (s->control_pid <= 0)
137 unit_unwatch_pid(UNIT(s), s->control_pid);
141 static void swap_done(Unit *u) {
146 swap_unset_proc_swaps(s);
147 swap_set_devnode(s, NULL);
152 free(s->parameters_fragment.what);
153 s->parameters_fragment.what = NULL;
155 free(s->parameters_fragment.discard);
156 s->parameters_fragment.discard = NULL;
158 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
159 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
160 s->control_command = NULL;
162 swap_unwatch_control_pid(s);
164 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
167 static int swap_arm_timer(Swap *s) {
172 if (s->timeout_usec <= 0) {
173 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
177 if (s->timer_event_source) {
178 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_usec);
182 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
185 return sd_event_add_time(
186 UNIT(s)->manager->event,
187 &s->timer_event_source,
189 now(CLOCK_MONOTONIC) + s->timeout_usec, 0,
190 swap_dispatch_timer, s);
193 static int swap_add_device_links(Swap *s) {
201 if (s->from_fragment)
202 p = &s->parameters_fragment;
206 if (is_device_path(s->what))
207 return unit_add_node_link(UNIT(s), s->what, !p->noauto && UNIT(s)->manager->running_as == SYSTEMD_SYSTEM);
209 /* File based swap devices need to be ordered after
210 * systemd-remount-fs.service, since they might need a
211 * writable file system. */
212 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
215 static int swap_add_default_dependencies(Swap *s) {
216 bool nofail = false, noauto = false;
221 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
224 if (detect_container(NULL) > 0)
227 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
231 if (s->from_fragment) {
232 SwapParameters *p = &s->parameters_fragment;
240 r = unit_add_dependency_by_name_inverse(UNIT(s), UNIT_WANTS, SPECIAL_SWAP_TARGET, NULL, true);
242 r = unit_add_two_dependencies_by_name_inverse(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SWAP_TARGET, NULL, true);
250 static int swap_verify(Swap *s) {
252 _cleanup_free_ char *e = NULL;
254 if (UNIT(s)->load_state != UNIT_LOADED)
257 e = unit_name_from_path(s->what, ".swap");
261 b = unit_has_name(UNIT(s), e);
263 log_error_unit(UNIT(s)->id, "%s: Value of \"What\" and unit name do not match, not loading.", UNIT(s)->id);
267 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
268 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.", UNIT(s)->id);
275 static int swap_load_devnode(Swap *s) {
276 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
282 if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
285 d = udev_device_new_from_devnum(UNIT(s)->manager->udev, 'b', st.st_rdev);
289 p = udev_device_get_devnode(d);
293 return swap_set_devnode(s, p);
296 static int swap_load(Unit *u) {
301 assert(u->load_state == UNIT_STUB);
303 /* Load a .swap file */
304 r = unit_load_fragment_and_dropin_optional(u);
308 if (u->load_state == UNIT_LOADED) {
310 if (UNIT(s)->fragment_path)
311 s->from_fragment = true;
314 if (s->parameters_fragment.what)
315 s->what = strdup(s->parameters_fragment.what);
316 else if (s->parameters_proc_swaps.what)
317 s->what = strdup(s->parameters_proc_swaps.what);
319 s->what = unit_name_to_path(u->id);
325 path_kill_slashes(s->what);
327 if (!UNIT(s)->description) {
328 r = unit_set_description(u, s->what);
333 r = unit_require_mounts_for(UNIT(s), s->what);
337 r = swap_add_device_links(s);
341 r = swap_load_devnode(s);
345 r = unit_patch_contexts(u);
349 r = unit_add_exec_dependencies(u, &s->exec_context);
353 r = unit_add_default_slice(u, &s->cgroup_context);
357 if (UNIT(s)->default_dependencies) {
358 r = swap_add_default_dependencies(s);
364 return swap_verify(s);
367 static int swap_add_one(
370 const char *what_proc_swaps,
376 _cleanup_free_ char *e = NULL;
384 assert(what_proc_swaps);
386 e = unit_name_from_path(what, ".swap");
390 u = manager_get_unit(m, e);
393 SWAP(u)->from_proc_swaps &&
394 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
400 u = unit_new(m, sizeof(Swap));
404 r = unit_add_name(u, e);
408 SWAP(u)->what = strdup(what);
409 if (!SWAP(u)->what) {
414 unit_add_to_load_queue(u);
418 p = &SWAP(u)->parameters_proc_swaps;
421 p->what = strdup(what_proc_swaps);
429 SWAP(u)->is_active = true;
430 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
433 SWAP(u)->from_proc_swaps = true;
435 p->priority = priority;
439 unit_add_to_dbus_queue(u);
444 log_warning_unit(e, "Failed to load swap unit: %s", strerror(-r));
452 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
453 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
454 struct udev_list_entry *item = NULL, *first = NULL;
461 r = swap_add_one(m, device, device, prio, false, false, set_flags);
465 /* If this is a block device, then let's add duplicates for
466 * all other names of this block device */
467 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
470 d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev);
474 /* Add the main device node */
475 dn = udev_device_get_devnode(d);
476 if (dn && !streq(dn, device))
477 swap_add_one(m, dn, device, prio, false, false, set_flags);
479 /* Add additional units for all symlinks */
480 first = udev_device_get_devlinks_list_entry(d);
481 udev_list_entry_foreach(item, first) {
484 /* Don't bother with the /dev/block links */
485 p = udev_list_entry_get_name(item);
487 if (streq(p, device))
490 if (path_startswith(p, "/dev/block/"))
493 if (stat(p, &st) >= 0)
494 if (!S_ISBLK(st.st_mode) ||
495 st.st_rdev != udev_device_get_devnum(d))
498 swap_add_one(m, p, device, prio, false, false, set_flags);
504 static void swap_set_state(Swap *s, SwapState state) {
509 old_state = s->state;
512 if (state != SWAP_ACTIVATING &&
513 state != SWAP_ACTIVATING_SIGTERM &&
514 state != SWAP_ACTIVATING_SIGKILL &&
515 state != SWAP_ACTIVATING_DONE &&
516 state != SWAP_DEACTIVATING &&
517 state != SWAP_DEACTIVATING_SIGTERM &&
518 state != SWAP_DEACTIVATING_SIGKILL) {
519 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
520 swap_unwatch_control_pid(s);
521 s->control_command = NULL;
522 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
525 if (state != old_state)
526 log_debug_unit(UNIT(s)->id,
527 "%s changed %s -> %s",
529 swap_state_to_string(old_state),
530 swap_state_to_string(state));
532 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
535 static int swap_coldplug(Unit *u) {
537 SwapState new_state = SWAP_DEAD;
541 assert(s->state == SWAP_DEAD);
543 if (s->deserialized_state != s->state)
544 new_state = s->deserialized_state;
545 else if (s->from_proc_swaps)
546 new_state = SWAP_ACTIVE;
548 if (new_state == s->state)
551 if (new_state == SWAP_ACTIVATING ||
552 new_state == SWAP_ACTIVATING_SIGTERM ||
553 new_state == SWAP_ACTIVATING_SIGKILL ||
554 new_state == SWAP_ACTIVATING_DONE ||
555 new_state == SWAP_DEACTIVATING ||
556 new_state == SWAP_DEACTIVATING_SIGTERM ||
557 new_state == SWAP_DEACTIVATING_SIGKILL) {
559 if (s->control_pid <= 0)
562 r = unit_watch_pid(UNIT(s), s->control_pid);
566 r = swap_arm_timer(s);
571 swap_set_state(s, new_state);
575 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
582 if (s->from_proc_swaps)
583 p = &s->parameters_proc_swaps;
584 else if (s->from_fragment)
585 p = &s->parameters_fragment;
593 "%sFrom /proc/swaps: %s\n"
594 "%sFrom fragment: %s\n",
595 prefix, swap_state_to_string(s->state),
596 prefix, swap_result_to_string(s->result),
598 prefix, yes_no(s->from_proc_swaps),
599 prefix, yes_no(s->from_fragment));
602 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
611 prefix, yes_no(p->noauto),
612 prefix, yes_no(p->nofail),
613 prefix, p->discard ?: "none");
615 if (s->control_pid > 0)
617 "%sControl PID: "PID_FMT"\n",
618 prefix, s->control_pid);
620 exec_context_dump(&s->exec_context, f, prefix);
621 kill_context_dump(&s->kill_context, f, prefix);
624 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
627 ExecParameters exec_params = {
628 .apply_permissions = true,
629 .apply_chroot = true,
630 .apply_tty_stdin = true,
637 unit_realize_cgroup(UNIT(s));
639 r = unit_setup_exec_runtime(UNIT(s));
643 r = swap_arm_timer(s);
647 exec_params.environment = UNIT(s)->manager->environment;
648 exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
649 exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
650 exec_params.cgroup_path = UNIT(s)->cgroup_path;
651 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
652 exec_params.unit_id = UNIT(s)->id;
662 r = unit_watch_pid(UNIT(s), pid);
664 /* FIXME: we need to do something here */
672 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
677 static void swap_enter_dead(Swap *s, SwapResult f) {
680 if (f != SWAP_SUCCESS)
683 exec_runtime_destroy(s->exec_runtime);
684 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
686 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
688 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
691 static void swap_enter_active(Swap *s, SwapResult f) {
694 if (f != SWAP_SUCCESS)
697 swap_set_state(s, SWAP_ACTIVE);
700 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
705 if (f != SWAP_SUCCESS)
708 r = unit_kill_context(
711 state != SWAP_ACTIVATING_SIGTERM && state != SWAP_DEACTIVATING_SIGTERM,
719 r = swap_arm_timer(s);
723 swap_set_state(s, state);
724 } else if (state == SWAP_ACTIVATING_SIGTERM)
725 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_SUCCESS);
726 else if (state == SWAP_DEACTIVATING_SIGTERM)
727 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
729 swap_enter_dead(s, SWAP_SUCCESS);
734 log_warning_unit(UNIT(s)->id,
735 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
737 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
740 static void swap_enter_activating(Swap *s) {
746 s->control_command_id = SWAP_EXEC_ACTIVATE;
747 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
749 if (s->from_fragment) {
750 priority = s->parameters_fragment.priority;
751 discard = s->parameters_fragment.discard;
757 r = exec_command_set(s->control_command, "/sbin/swapon", NULL);
762 char p[DECIMAL_STR_MAX(int)];
764 sprintf(p, "%i", priority);
765 r = exec_command_append(s->control_command, "-p", p, NULL);
770 if (discard && !streq(discard, "none")) {
771 const char *discard_arg = "--discard";
773 if (!streq(discard, "all"))
774 discard_arg = strappenda("--discard=", discard);
776 r = exec_command_append(s->control_command, discard_arg, NULL);
781 r = exec_command_append(s->control_command, s->what, NULL);
785 swap_unwatch_control_pid(s);
787 r = swap_spawn(s, s->control_command, &s->control_pid);
791 swap_set_state(s, SWAP_ACTIVATING);
796 log_warning_unit(UNIT(s)->id,
797 "%s failed to run 'swapon' task: %s",
798 UNIT(s)->id, strerror(-r));
799 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
802 static void swap_enter_deactivating(Swap *s) {
807 s->control_command_id = SWAP_EXEC_DEACTIVATE;
808 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
810 r = exec_command_set(s->control_command,
817 swap_unwatch_control_pid(s);
819 r = swap_spawn(s, s->control_command, &s->control_pid);
823 swap_set_state(s, SWAP_DEACTIVATING);
828 log_warning_unit(UNIT(s)->id,
829 "%s failed to run 'swapoff' task: %s",
830 UNIT(s)->id, strerror(-r));
831 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
834 static int swap_start(Unit *u) {
839 /* We cannot fulfill this request right now, try again later
842 if (s->state == SWAP_DEACTIVATING ||
843 s->state == SWAP_DEACTIVATING_SIGTERM ||
844 s->state == SWAP_DEACTIVATING_SIGKILL ||
845 s->state == SWAP_ACTIVATING_SIGTERM ||
846 s->state == SWAP_ACTIVATING_SIGKILL)
849 if (s->state == SWAP_ACTIVATING)
852 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
854 if (detect_container(NULL) > 0)
857 s->result = SWAP_SUCCESS;
858 swap_enter_activating(s);
862 static int swap_stop(Unit *u) {
867 if (s->state == SWAP_DEACTIVATING ||
868 s->state == SWAP_DEACTIVATING_SIGTERM ||
869 s->state == SWAP_DEACTIVATING_SIGKILL ||
870 s->state == SWAP_ACTIVATING_SIGTERM ||
871 s->state == SWAP_ACTIVATING_SIGKILL)
874 assert(s->state == SWAP_ACTIVATING ||
875 s->state == SWAP_ACTIVATING_DONE ||
876 s->state == SWAP_ACTIVE);
878 if (detect_container(NULL) > 0)
881 swap_enter_deactivating(s);
885 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
892 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
893 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
895 if (s->control_pid > 0)
896 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
898 if (s->control_command_id >= 0)
899 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
904 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
910 if (streq(key, "state")) {
913 state = swap_state_from_string(value);
915 log_debug_unit(u->id, "Failed to parse state value %s", value);
917 s->deserialized_state = state;
918 } else if (streq(key, "result")) {
921 f = swap_result_from_string(value);
923 log_debug_unit(u->id, "Failed to parse result value %s", value);
924 else if (f != SWAP_SUCCESS)
926 } else if (streq(key, "control-pid")) {
929 if (parse_pid(value, &pid) < 0)
930 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
932 s->control_pid = pid;
934 } else if (streq(key, "control-command")) {
937 id = swap_exec_command_from_string(value);
939 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
941 s->control_command_id = id;
942 s->control_command = s->exec_command + id;
945 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
950 _pure_ static UnitActiveState swap_active_state(Unit *u) {
953 return state_translation_table[SWAP(u)->state];
956 _pure_ static const char *swap_sub_state_to_string(Unit *u) {
959 return swap_state_to_string(SWAP(u)->state);
962 _pure_ static bool swap_check_gc(Unit *u) {
967 return s->from_proc_swaps;
970 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
977 if (pid != s->control_pid)
982 if (is_clean_exit(code, status, NULL))
984 else if (code == CLD_EXITED)
985 f = SWAP_FAILURE_EXIT_CODE;
986 else if (code == CLD_KILLED)
987 f = SWAP_FAILURE_SIGNAL;
988 else if (code == CLD_DUMPED)
989 f = SWAP_FAILURE_CORE_DUMP;
991 assert_not_reached("Unknown code");
993 if (f != SWAP_SUCCESS)
996 if (s->control_command) {
997 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
999 s->control_command = NULL;
1000 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1003 log_full_unit(f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1005 "%s swap process exited, code=%s status=%i",
1006 u->id, sigchld_code_to_string(code), status);
1010 case SWAP_ACTIVATING:
1011 case SWAP_ACTIVATING_DONE:
1012 case SWAP_ACTIVATING_SIGTERM:
1013 case SWAP_ACTIVATING_SIGKILL:
1015 if (f == SWAP_SUCCESS)
1016 swap_enter_active(s, f);
1018 swap_enter_dead(s, f);
1021 case SWAP_DEACTIVATING:
1022 case SWAP_DEACTIVATING_SIGKILL:
1023 case SWAP_DEACTIVATING_SIGTERM:
1025 swap_enter_dead(s, f);
1029 assert_not_reached("Uh, control process died at wrong time.");
1032 /* Notify clients about changed exit status */
1033 unit_add_to_dbus_queue(u);
1036 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1037 Swap *s = SWAP(userdata);
1040 assert(s->timer_event_source == source);
1044 case SWAP_ACTIVATING:
1045 case SWAP_ACTIVATING_DONE:
1046 log_warning_unit(UNIT(s)->id, "%s activation timed out. Stopping.", UNIT(s)->id);
1047 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1050 case SWAP_DEACTIVATING:
1051 log_warning_unit(UNIT(s)->id, "%s deactivation timed out. Stopping.", UNIT(s)->id);
1052 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1055 case SWAP_ACTIVATING_SIGTERM:
1056 if (s->kill_context.send_sigkill) {
1057 log_warning_unit(UNIT(s)->id, "%s activation timed out. Killing.", UNIT(s)->id);
1058 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1060 log_warning_unit(UNIT(s)->id, "%s activation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
1061 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1065 case SWAP_DEACTIVATING_SIGTERM:
1066 if (s->kill_context.send_sigkill) {
1067 log_warning_unit(UNIT(s)->id, "%s deactivation timed out. Killing.", UNIT(s)->id);
1068 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1070 log_warning_unit(UNIT(s)->id, "%s deactivation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
1071 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1075 case SWAP_ACTIVATING_SIGKILL:
1076 case SWAP_DEACTIVATING_SIGKILL:
1077 log_warning_unit(UNIT(s)->id, "%s swap process still around after SIGKILL. Ignoring.", UNIT(s)->id);
1078 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1082 assert_not_reached("Timeout at wrong time.");
1088 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1094 rewind(m->proc_swaps);
1096 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1099 _cleanup_free_ char *dev = NULL, *d = NULL;
1102 k = fscanf(m->proc_swaps,
1103 "%ms " /* device/file */
1104 "%*s " /* type of swap */
1105 "%*s " /* swap size */
1107 "%i\n", /* priority */
1113 log_warning("Failed to parse /proc/swaps:%u", i);
1121 k = swap_process_new_swap(m, d, prio, set_flags);
1129 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1130 Manager *m = userdata;
1135 assert(revents & EPOLLPRI);
1137 r = swap_load_proc_swaps(m, true);
1139 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1141 /* Reset flags, just in case, for late calls */
1142 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1143 Swap *swap = SWAP(u);
1145 swap->is_active = swap->just_activated = false;
1151 manager_dispatch_load_queue(m);
1153 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1154 Swap *swap = SWAP(u);
1156 if (!swap->is_active) {
1157 /* This has just been deactivated */
1159 swap_unset_proc_swaps(swap);
1161 switch (swap->state) {
1164 swap_enter_dead(swap, SWAP_SUCCESS);
1169 swap_set_state(swap, swap->state);
1173 } else if (swap->just_activated) {
1175 /* New swap entry */
1177 switch (swap->state) {
1181 swap_enter_active(swap, SWAP_SUCCESS);
1184 case SWAP_ACTIVATING:
1185 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1189 /* Nothing really changed, but let's
1190 * issue an notification call
1191 * nonetheless, in case somebody is
1192 * waiting for this. */
1193 swap_set_state(swap, swap->state);
1198 /* Reset the flags for later calls */
1199 swap->is_active = swap->just_activated = false;
1205 static Unit *swap_following(Unit *u) {
1207 Swap *other, *first = NULL;
1211 /* If the user configured the swap through /etc/fstab or
1212 * a device unit, follow that. */
1214 if (s->from_fragment)
1217 LIST_FOREACH_AFTER(same_devnode, other, s)
1218 if (other->from_fragment)
1221 LIST_FOREACH_BEFORE(same_devnode, other, s)
1222 if (other->from_fragment)
1225 /* Otherwise make everybody follow the unit that's named after
1226 * the swap device in the kernel */
1228 if (streq_ptr(s->what, s->devnode))
1231 LIST_FOREACH_AFTER(same_devnode, other, s)
1232 if (streq_ptr(other->what, other->devnode))
1235 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1236 if (streq_ptr(other->what, other->devnode))
1242 /* Fall back to the first on the list */
1246 static int swap_following_set(Unit *u, Set **_set) {
1247 Swap *s = SWAP(u), *other;
1254 if (LIST_JUST_US(same_devnode, s)) {
1259 set = set_new(NULL);
1263 LIST_FOREACH_AFTER(same_devnode, other, s) {
1264 r = set_put(set, other);
1269 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1270 r = set_put(set, other);
1283 static void swap_shutdown(Manager *m) {
1286 m->swap_event_source = sd_event_source_unref(m->swap_event_source);
1288 if (m->proc_swaps) {
1289 fclose(m->proc_swaps);
1290 m->proc_swaps = NULL;
1293 hashmap_free(m->swaps_by_devnode);
1294 m->swaps_by_devnode = NULL;
1297 static int swap_enumerate(Manager *m) {
1302 if (!m->proc_swaps) {
1303 m->proc_swaps = fopen("/proc/swaps", "re");
1305 return errno == ENOENT ? 0 : -errno;
1307 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
1311 /* Dispatch this before we dispatch SIGCHLD, so that
1312 * we always get the events from /proc/swaps before
1313 * the SIGCHLD of /sbin/swapon. */
1314 r = sd_event_source_set_priority(m->swap_event_source, -10);
1319 r = swap_load_proc_swaps(m, false);
1330 int swap_process_new_device(Manager *m, struct udev_device *dev) {
1331 struct udev_list_entry *item = NULL, *first = NULL;
1332 _cleanup_free_ char *e = NULL;
1340 dn = udev_device_get_devnode(dev);
1344 e = unit_name_from_path(dn, ".swap");
1348 s = hashmap_get(m->units, e);
1350 r = swap_set_devnode(s, dn);
1352 first = udev_device_get_devlinks_list_entry(dev);
1353 udev_list_entry_foreach(item, first) {
1354 _cleanup_free_ char *n = NULL;
1356 n = unit_name_from_path(udev_list_entry_get_name(item), ".swap");
1360 s = hashmap_get(m->units, n);
1364 q = swap_set_devnode(s, dn);
1373 int swap_process_removed_device(Manager *m, struct udev_device *dev) {
1378 dn = udev_device_get_devnode(dev);
1382 while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1385 q = swap_set_devnode(s, NULL);
1393 static void swap_reset_failed(Unit *u) {
1398 if (s->state == SWAP_FAILED)
1399 swap_set_state(s, SWAP_DEAD);
1401 s->result = SWAP_SUCCESS;
1404 static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1405 return unit_kill_common(u, who, signo, -1, SWAP(u)->control_pid, error);
1408 static int swap_get_timeout(Unit *u, uint64_t *timeout) {
1412 if (!s->timer_event_source)
1415 r = sd_event_source_get_time(s->timer_event_source, timeout);
1422 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1423 [SWAP_DEAD] = "dead",
1424 [SWAP_ACTIVATING] = "activating",
1425 [SWAP_ACTIVATING_DONE] = "activating-done",
1426 [SWAP_ACTIVE] = "active",
1427 [SWAP_DEACTIVATING] = "deactivating",
1428 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1429 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1430 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1431 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1432 [SWAP_FAILED] = "failed"
1435 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1437 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1438 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1439 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1442 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1444 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1445 [SWAP_SUCCESS] = "success",
1446 [SWAP_FAILURE_RESOURCES] = "resources",
1447 [SWAP_FAILURE_TIMEOUT] = "timeout",
1448 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1449 [SWAP_FAILURE_SIGNAL] = "signal",
1450 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1453 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1455 const UnitVTable swap_vtable = {
1456 .object_size = sizeof(Swap),
1457 .exec_context_offset = offsetof(Swap, exec_context),
1458 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1459 .kill_context_offset = offsetof(Swap, kill_context),
1460 .exec_runtime_offset = offsetof(Swap, exec_runtime),
1466 .private_section = "Swap",
1469 .no_instances = true,
1475 .coldplug = swap_coldplug,
1479 .start = swap_start,
1484 .get_timeout = swap_get_timeout,
1486 .serialize = swap_serialize,
1487 .deserialize_item = swap_deserialize_item,
1489 .active_state = swap_active_state,
1490 .sub_state_to_string = swap_sub_state_to_string,
1492 .check_gc = swap_check_gc,
1494 .sigchld_event = swap_sigchld_event,
1496 .reset_failed = swap_reset_failed,
1498 .bus_interface = "org.freedesktop.systemd1.Swap",
1499 .bus_vtable = bus_swap_vtable,
1500 .bus_set_property = bus_swap_set_property,
1501 .bus_commit_properties = bus_swap_commit_properties,
1503 .following = swap_following,
1504 .following_set = swap_following_set,
1506 .enumerate = swap_enumerate,
1507 .shutdown = swap_shutdown,
1509 .status_message_formats = {
1510 .starting_stopping = {
1511 [0] = "Activating swap %s...",
1512 [1] = "Deactivating swap %s...",
1514 .finished_start_job = {
1515 [JOB_DONE] = "Activated swap %s.",
1516 [JOB_FAILED] = "Failed to activate swap %s.",
1517 [JOB_DEPENDENCY] = "Dependency failed for %s.",
1518 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1520 .finished_stop_job = {
1521 [JOB_DONE] = "Deactivated swap %s.",
1522 [JOB_FAILED] = "Failed deactivating swap %s.",
1523 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",