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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU 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"
39 static const UnitActiveState state_translation_table
[_SWAP_STATE_MAX
] = {
40 [SWAP_DEAD
] = UNIT_INACTIVE
,
41 [SWAP_ACTIVATING
] = UNIT_ACTIVATING
,
42 [SWAP_ACTIVE
] = UNIT_ACTIVE
,
43 [SWAP_DEACTIVATING
] = UNIT_DEACTIVATING
,
44 [SWAP_ACTIVATING_SIGTERM
] = UNIT_DEACTIVATING
,
45 [SWAP_ACTIVATING_SIGKILL
] = UNIT_DEACTIVATING
,
46 [SWAP_DEACTIVATING_SIGTERM
] = UNIT_DEACTIVATING
,
47 [SWAP_DEACTIVATING_SIGKILL
] = UNIT_DEACTIVATING
,
48 [SWAP_FAILED
] = UNIT_FAILED
51 static void swap_unset_proc_swaps(Swap
*s
) {
56 if (!s
->parameters_proc_swaps
.what
)
59 /* Remove this unit from the chain of swaps which share the
60 * same kernel swap device. */
62 first
= hashmap_get(s
->meta
.manager
->swaps_by_proc_swaps
, s
->parameters_proc_swaps
.what
);
63 LIST_REMOVE(Swap
, same_proc_swaps
, first
, s
);
66 hashmap_remove_and_replace(s
->meta
.manager
->swaps_by_proc_swaps
, s
->parameters_proc_swaps
.what
, first
->parameters_proc_swaps
.what
, first
);
68 hashmap_remove(s
->meta
.manager
->swaps_by_proc_swaps
, s
->parameters_proc_swaps
.what
);
70 free(s
->parameters_proc_swaps
.what
);
71 s
->parameters_proc_swaps
.what
= NULL
;
74 static void swap_init(Unit
*u
) {
78 assert(s
->meta
.load_state
== UNIT_STUB
);
80 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
82 exec_context_init(&s
->exec_context
);
84 s
->parameters_etc_fstab
.priority
= s
->parameters_proc_swaps
.priority
= s
->parameters_fragment
.priority
= -1;
86 s
->timer_watch
.type
= WATCH_INVALID
;
88 s
->control_command_id
= _MOUNT_EXEC_COMMAND_INVALID
;
91 static void swap_unwatch_control_pid(Swap
*s
) {
94 if (s
->control_pid
<= 0)
97 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
101 static void swap_done(Unit
*u
) {
106 swap_unset_proc_swaps(s
);
111 free(s
->parameters_etc_fstab
.what
);
112 free(s
->parameters_fragment
.what
);
113 s
->parameters_etc_fstab
.what
= s
->parameters_fragment
.what
= NULL
;
115 exec_context_done(&s
->exec_context
);
116 exec_command_done_array(s
->exec_command
, _SWAP_EXEC_COMMAND_MAX
);
117 s
->control_command
= NULL
;
119 swap_unwatch_control_pid(s
);
121 unit_unwatch_timer(u
, &s
->timer_watch
);
124 int swap_add_one_mount_link(Swap
*s
, Mount
*m
) {
130 if (s
->meta
.load_state
!= UNIT_LOADED
||
131 m
->meta
.load_state
!= UNIT_LOADED
)
134 if (is_device_path(s
->what
))
137 if (!path_startswith(s
->what
, m
->where
))
140 if ((r
= unit_add_two_dependencies(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
146 static int swap_add_mount_links(Swap
*s
) {
152 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_MOUNT
])
153 if ((r
= swap_add_one_mount_link(s
, (Mount
*) other
)) < 0)
159 static int swap_add_target_links(Swap
*s
) {
166 if (s
->from_fragment
)
167 p
= &s
->parameters_fragment
;
168 else if (s
->from_etc_fstab
)
169 p
= &s
->parameters_etc_fstab
;
173 if ((r
= manager_load_unit(s
->meta
.manager
, SPECIAL_SWAP_TARGET
, NULL
, NULL
, &tu
)) < 0)
177 (p
->handle
|| s
->meta
.manager
->swap_auto
) &&
179 s
->meta
.manager
->running_as
== MANAGER_SYSTEM
)
180 if ((r
= unit_add_dependency(tu
, UNIT_WANTS
, UNIT(s
), true)) < 0)
183 return unit_add_dependency(UNIT(s
), UNIT_BEFORE
, tu
, true);
186 static int swap_add_device_links(Swap
*s
) {
194 if (s
->from_fragment
)
195 p
= &s
->parameters_fragment
;
196 else if (s
->from_etc_fstab
)
197 p
= &s
->parameters_etc_fstab
;
201 return unit_add_node_link(UNIT(s
), s
->what
,
202 !p
->noauto
&& p
->nofail
&&
203 s
->meta
.manager
->running_as
== MANAGER_SYSTEM
);
206 static int swap_add_default_dependencies(Swap
*s
) {
211 if (s
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
213 if ((r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_SYSINIT_TARGET
, NULL
, true)) < 0)
216 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTED_BY
, SPECIAL_UMOUNT_TARGET
, NULL
, true)) < 0)
223 static int swap_verify(Swap
*s
) {
227 if (s
->meta
.load_state
!= UNIT_LOADED
)
230 if (!(e
= unit_name_from_path(s
->what
, ".swap")))
233 b
= unit_has_name(UNIT(s
), e
);
237 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s
->meta
.id
);
241 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
242 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s
->meta
.id
);
249 static int swap_load(Unit
*u
) {
254 assert(u
->meta
.load_state
== UNIT_STUB
);
256 /* Load a .swap file */
257 if ((r
= unit_load_fragment_and_dropin_optional(u
)) < 0)
260 if (u
->meta
.load_state
== UNIT_LOADED
) {
262 if (s
->meta
.fragment_path
)
263 s
->from_fragment
= true;
266 if (s
->parameters_fragment
.what
)
267 s
->what
= strdup(s
->parameters_fragment
.what
);
268 else if (s
->parameters_etc_fstab
.what
)
269 s
->what
= strdup(s
->parameters_etc_fstab
.what
);
270 else if (s
->parameters_proc_swaps
.what
)
271 s
->what
= strdup(s
->parameters_proc_swaps
.what
);
273 s
->what
= unit_name_to_path(u
->meta
.id
);
279 path_kill_slashes(s
->what
);
281 if (!s
->meta
.description
)
282 if ((r
= unit_set_description(u
, s
->what
)) < 0)
286 if ((r
= swap_add_device_links(s
)) < 0)
289 if ((r
= swap_add_mount_links(s
)) < 0)
292 if ((r
= swap_add_target_links(s
)) < 0)
295 if (s
->meta
.default_dependencies
)
296 if ((r
= swap_add_default_dependencies(s
)) < 0)
300 return swap_verify(s
);
306 const char *what_proc_swaps
,
314 char *e
= NULL
, *wp
= NULL
;
322 if (!(e
= unit_name_from_path(what
, ".swap")))
325 u
= manager_get_unit(m
, e
);
327 if (what_proc_swaps
&&
329 SWAP(u
)->from_proc_swaps
&&
330 !path_equal(SWAP(u
)->parameters_proc_swaps
.what
, what_proc_swaps
))
336 if (!(u
= unit_new(m
))) {
341 if ((r
= unit_add_name(u
, e
)) < 0)
344 if (!(SWAP(u
)->what
= strdup(what
))) {
349 unit_add_to_load_queue(u
);
353 if (what_proc_swaps
) {
356 p
= &SWAP(u
)->parameters_proc_swaps
;
359 if (!(wp
= strdup(what_proc_swaps
))) {
364 if (!m
->swaps_by_proc_swaps
)
365 if (!(m
->swaps_by_proc_swaps
= hashmap_new(string_hash_func
, string_compare_func
))) {
373 first
= hashmap_get(m
->swaps_by_proc_swaps
, wp
);
374 LIST_PREPEND(Swap
, same_proc_swaps
, first
, SWAP(u
));
376 if ((r
= hashmap_replace(m
->swaps_by_proc_swaps
, wp
, first
)) < 0)
381 SWAP(u
)->is_active
= true;
382 SWAP(u
)->just_activated
= !SWAP(u
)->from_proc_swaps
;
385 SWAP(u
)->from_proc_swaps
= true;
388 p
= &SWAP(u
)->parameters_etc_fstab
;
390 if (!(wp
= strdup(what
))) {
398 SWAP(u
)->from_etc_fstab
= true;
401 p
->priority
= priority
;
406 unit_add_to_dbus_queue(u
);
413 log_warning("Failed to load swap unit: %s", strerror(-r
));
424 static int swap_process_new_swap(Manager
*m
, const char *device
, int prio
, bool set_flags
) {
430 if (stat(device
, &st
) >= 0 && S_ISBLK(st
.st_mode
)) {
431 struct udev_device
*d
;
433 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
435 /* So this is a proper swap device. Create swap units
436 * for all names this swap device is known under */
438 if (!(d
= udev_device_new_from_devnum(m
->udev
, 'b', st
.st_rdev
)))
441 if ((dn
= udev_device_get_devnode(d
)))
442 r
= swap_add_one(m
, dn
, device
, prio
, false, false, false, set_flags
);
444 /* Add additional units for all symlinks */
445 first
= udev_device_get_devlinks_list_entry(d
);
446 udev_list_entry_foreach(item
, first
) {
449 /* Don't bother with the /dev/block links */
450 p
= udev_list_entry_get_name(item
);
452 if (path_startswith(p
, "/dev/block/"))
455 if (stat(p
, &st
) >= 0)
456 if ((!S_ISBLK(st
.st_mode
)) || st
.st_rdev
!= udev_device_get_devnum(d
))
459 if ((k
= swap_add_one(m
, p
, device
, prio
, false, false, false, set_flags
)) < 0)
463 udev_device_unref(d
);
466 if ((k
= swap_add_one(m
, device
, device
, prio
, false, false, false, set_flags
)) < 0)
472 static void swap_set_state(Swap
*s
, SwapState state
) {
477 old_state
= s
->state
;
480 if (state
!= SWAP_ACTIVATING
&&
481 state
!= SWAP_ACTIVATING_SIGTERM
&&
482 state
!= SWAP_ACTIVATING_SIGKILL
&&
483 state
!= SWAP_DEACTIVATING
&&
484 state
!= SWAP_DEACTIVATING_SIGTERM
&&
485 state
!= SWAP_DEACTIVATING_SIGKILL
) {
486 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
487 swap_unwatch_control_pid(s
);
488 s
->control_command
= NULL
;
489 s
->control_command_id
= _SWAP_EXEC_COMMAND_INVALID
;
492 if (state
!= old_state
)
493 log_debug("%s changed %s -> %s",
495 swap_state_to_string(old_state
),
496 swap_state_to_string(state
));
498 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
501 static int swap_coldplug(Unit
*u
) {
503 SwapState new_state
= SWAP_DEAD
;
507 assert(s
->state
== SWAP_DEAD
);
509 if (s
->deserialized_state
!= s
->state
)
510 new_state
= s
->deserialized_state
;
511 else if (s
->from_proc_swaps
)
512 new_state
= SWAP_ACTIVE
;
514 if (new_state
!= s
->state
) {
516 if (new_state
== SWAP_ACTIVATING
||
517 new_state
== SWAP_ACTIVATING_SIGTERM
||
518 new_state
== SWAP_ACTIVATING_SIGKILL
||
519 new_state
== SWAP_DEACTIVATING
||
520 new_state
== SWAP_DEACTIVATING_SIGTERM
||
521 new_state
== SWAP_DEACTIVATING_SIGKILL
) {
523 if (s
->control_pid
<= 0)
526 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
529 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
533 swap_set_state(s
, new_state
);
539 static void swap_dump(Unit
*u
, FILE *f
, const char *prefix
) {
546 if (s
->from_proc_swaps
)
547 p
= &s
->parameters_proc_swaps
;
548 else if (s
->from_fragment
)
549 p
= &s
->parameters_fragment
;
551 p
= &s
->parameters_etc_fstab
;
560 "%sFrom /etc/fstab: %s\n"
561 "%sFrom /proc/swaps: %s\n"
562 "%sFrom fragment: %s\n",
563 prefix
, swap_state_to_string(s
->state
),
566 prefix
, yes_no(p
->noauto
),
567 prefix
, yes_no(p
->nofail
),
568 prefix
, yes_no(p
->handle
),
569 prefix
, yes_no(s
->from_etc_fstab
),
570 prefix
, yes_no(s
->from_proc_swaps
),
571 prefix
, yes_no(s
->from_fragment
));
573 if (s
->control_pid
> 0)
575 "%sControl PID: %lu\n",
576 prefix
, (unsigned long) s
->control_pid
);
578 exec_context_dump(&s
->exec_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 s
->meta
.manager
->environment
,
600 s
->meta
.manager
->confirm_spawn
,
601 s
->meta
.cgroup_bondings
,
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
, bool success
) {
625 swap_set_state(s
, s
->failure
? SWAP_FAILED
: SWAP_DEAD
);
628 static void swap_enter_active(Swap
*s
, bool success
) {
634 swap_set_state(s
, SWAP_ACTIVE
);
637 static void swap_enter_signal(Swap
*s
, SwapState state
, bool success
) {
640 bool wait_for_exit
= false;
647 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
648 int sig
= (state
== SWAP_ACTIVATING_SIGTERM
||
649 state
== SWAP_DEACTIVATING_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
651 if (s
->control_pid
> 0) {
652 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
654 s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
656 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
658 wait_for_exit
= true;
661 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
663 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
668 /* Exclude the control pid from being killed via the cgroup */
669 if (s
->control_pid
> 0)
670 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
673 if ((r
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, sig
, pid_set
)) < 0) {
674 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
675 log_warning("Failed to kill control group: %s", strerror(-r
));
677 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
, true);
694 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
696 swap_enter_dead(s
, false);
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
;
712 else if (s
->from_etc_fstab
)
713 priority
= s
->parameters_etc_fstab
.priority
;
720 snprintf(p
, sizeof(p
), "%i", priority
);
723 r
= exec_command_set(
731 r
= exec_command_set(
740 swap_unwatch_control_pid(s
);
742 if ((r
= swap_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
745 swap_set_state(s
, SWAP_ACTIVATING
);
750 log_warning("%s failed to run 'swapon' task: %s", s
->meta
.id
, strerror(-r
));
751 swap_enter_dead(s
, false);
754 static void swap_enter_deactivating(Swap
*s
, bool success
) {
762 s
->control_command_id
= SWAP_EXEC_DEACTIVATE
;
763 s
->control_command
= s
->exec_command
+ SWAP_EXEC_DEACTIVATE
;
765 if ((r
= exec_command_set(
772 swap_unwatch_control_pid(s
);
774 if ((r
= swap_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
777 swap_set_state(s
, SWAP_DEACTIVATING
);
782 log_warning("%s failed to run 'swapoff' task: %s", s
->meta
.id
, strerror(-r
));
783 swap_enter_active(s
, false);
786 static int swap_start(Unit
*u
) {
791 /* We cannot fulfill this request right now, try again later
794 if (s
->state
== SWAP_DEACTIVATING
||
795 s
->state
== SWAP_DEACTIVATING_SIGTERM
||
796 s
->state
== SWAP_DEACTIVATING_SIGKILL
||
797 s
->state
== SWAP_ACTIVATING_SIGTERM
||
798 s
->state
== SWAP_ACTIVATING_SIGKILL
)
801 if (s
->state
== SWAP_ACTIVATING
)
804 assert(s
->state
== SWAP_DEAD
|| s
->state
== SWAP_FAILED
);
807 swap_enter_activating(s
);
811 static int swap_stop(Unit
*u
) {
816 if (s
->state
== SWAP_DEACTIVATING
||
817 s
->state
== SWAP_DEACTIVATING_SIGTERM
||
818 s
->state
== SWAP_DEACTIVATING_SIGKILL
||
819 s
->state
== SWAP_ACTIVATING_SIGTERM
||
820 s
->state
== SWAP_ACTIVATING_SIGKILL
)
823 assert(s
->state
== SWAP_ACTIVATING
||
824 s
->state
== SWAP_ACTIVE
);
826 swap_enter_deactivating(s
, true);
830 static int swap_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
837 unit_serialize_item(u
, f
, "state", swap_state_to_string(s
->state
));
838 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
840 if (s
->control_pid
> 0)
841 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
843 if (s
->control_command_id
>= 0)
844 unit_serialize_item(u
, f
, "control-command", swap_exec_command_to_string(s
->control_command_id
));
849 static int swap_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
855 if (streq(key
, "state")) {
858 if ((state
= swap_state_from_string(value
)) < 0)
859 log_debug("Failed to parse state value %s", value
);
861 s
->deserialized_state
= state
;
862 } else if (streq(key
, "failure")) {
865 if ((b
= parse_boolean(value
)) < 0)
866 log_debug("Failed to parse failure value %s", value
);
868 s
->failure
= b
|| s
->failure
;
870 } else if (streq(key
, "control-pid")) {
873 if (parse_pid(value
, &pid
) < 0)
874 log_debug("Failed to parse control-pid value %s", value
);
876 s
->control_pid
= pid
;
878 } else if (streq(key
, "control-command")) {
881 if ((id
= swap_exec_command_from_string(value
)) < 0)
882 log_debug("Failed to parse exec-command value %s", value
);
884 s
->control_command_id
= id
;
885 s
->control_command
= s
->exec_command
+ id
;
889 log_debug("Unknown serialization key '%s'", key
);
894 static UnitActiveState
swap_active_state(Unit
*u
) {
897 return state_translation_table
[SWAP(u
)->state
];
900 static const char *swap_sub_state_to_string(Unit
*u
) {
903 return swap_state_to_string(SWAP(u
)->state
);
906 static bool swap_check_gc(Unit
*u
) {
911 return s
->from_etc_fstab
|| s
->from_proc_swaps
;
914 static void swap_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
921 if (pid
!= s
->control_pid
)
926 success
= is_clean_exit(code
, status
);
927 s
->failure
= s
->failure
|| !success
;
929 if (s
->control_command
) {
930 exec_status_exit(&s
->control_command
->exec_status
, pid
, code
, status
, s
->exec_context
.utmp_id
);
931 s
->control_command
= NULL
;
932 s
->control_command_id
= _SWAP_EXEC_COMMAND_INVALID
;
935 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
936 "%s swap process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
940 case SWAP_ACTIVATING
:
941 case SWAP_ACTIVATING_SIGTERM
:
942 case SWAP_ACTIVATING_SIGKILL
:
945 swap_enter_active(s
, true);
947 swap_enter_dead(s
, false);
950 case SWAP_DEACTIVATING
:
951 case SWAP_DEACTIVATING_SIGKILL
:
952 case SWAP_DEACTIVATING_SIGTERM
:
955 swap_enter_dead(s
, true);
957 swap_enter_dead(s
, false);
961 assert_not_reached("Uh, control process died at wrong time.");
964 /* Notify clients about changed exit status */
965 unit_add_to_dbus_queue(u
);
967 /* Request a reload of /proc/swaps, so that following units
968 * can follow our state change */
969 u
->meta
.manager
->request_reload
= true;
972 static void swap_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
976 assert(elapsed
== 1);
977 assert(w
== &s
->timer_watch
);
981 case SWAP_ACTIVATING
:
982 log_warning("%s activation timed out. Stopping.", u
->meta
.id
);
983 swap_enter_signal(s
, SWAP_ACTIVATING_SIGTERM
, false);
986 case SWAP_DEACTIVATING
:
987 log_warning("%s deactivation timed out. Stopping.", u
->meta
.id
);
988 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGTERM
, false);
991 case SWAP_ACTIVATING_SIGTERM
:
992 log_warning("%s activation timed out. Killing.", u
->meta
.id
);
993 swap_enter_signal(s
, SWAP_ACTIVATING_SIGKILL
, false);
996 case SWAP_DEACTIVATING_SIGTERM
:
997 log_warning("%s deactivation timed out. Killing.", u
->meta
.id
);
998 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGKILL
, false);
1001 case SWAP_ACTIVATING_SIGKILL
:
1002 case SWAP_DEACTIVATING_SIGKILL
:
1003 log_warning("%s swap process still around after SIGKILL. Ignoring.", u
->meta
.id
);
1004 swap_enter_dead(s
, false);
1008 assert_not_reached("Timeout at wrong time.");
1012 static int swap_load_proc_swaps(Manager
*m
, bool set_flags
) {
1018 rewind(m
->proc_swaps
);
1020 (void) fscanf(m
->proc_swaps
, "%*s %*s %*s %*s %*s\n");
1023 char *dev
= NULL
, *d
;
1026 if ((k
= fscanf(m
->proc_swaps
,
1027 "%ms " /* device/file */
1028 "%*s " /* type of swap */
1029 "%*s " /* swap size */
1031 "%i\n", /* priority */
1032 &dev
, &prio
)) != 2) {
1037 log_warning("Failed to parse /proc/swaps:%u.", i
);
1048 k
= swap_process_new_swap(m
, d
, prio
, set_flags
);
1058 int swap_dispatch_reload(Manager
*m
) {
1059 /* This function should go as soon as the kernel properly notifies us */
1061 if (_likely_(!m
->request_reload
))
1064 m
->request_reload
= false;
1066 return swap_fd_event(m
, EPOLLPRI
);
1069 int swap_fd_event(Manager
*m
, int events
) {
1074 assert(events
& EPOLLPRI
);
1076 if ((r
== swap_load_proc_swaps(m
, true)) < 0) {
1077 log_error("Failed to reread /proc/swaps: %s", strerror(-r
));
1079 /* Reset flags, just in case, for late calls */
1080 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_SWAP
]) {
1081 Swap
*swap
= (Swap
*) meta
;
1083 swap
->is_active
= swap
->just_activated
= false;
1089 manager_dispatch_load_queue(m
);
1091 LIST_FOREACH(units_per_type
, meta
, m
->units_per_type
[UNIT_SWAP
]) {
1092 Swap
*swap
= (Swap
*) meta
;
1094 if (!swap
->is_active
) {
1095 /* This has just been deactivated */
1097 swap
->from_proc_swaps
= false;
1098 swap_unset_proc_swaps(swap
);
1100 switch (swap
->state
) {
1103 swap_enter_dead(swap
, true);
1107 swap_set_state(swap
, swap
->state
);
1111 } else if (swap
->just_activated
) {
1113 /* New swap entry */
1115 switch (swap
->state
) {
1119 swap_enter_active(swap
, true);
1123 /* Nothing really changed, but let's
1124 * issue an notification call
1125 * nonetheless, in case somebody is
1126 * waiting for this. */
1127 swap_set_state(swap
, swap
->state
);
1132 /* Reset the flags for later calls */
1133 swap
->is_active
= swap
->just_activated
= false;
1139 static Unit
*swap_following(Unit
*u
) {
1141 Swap
*other
, *first
= NULL
;
1145 if (streq_ptr(s
->what
, s
->parameters_proc_swaps
.what
))
1148 /* Make everybody follow the unit that's named after the swap
1149 * device in the kernel */
1151 LIST_FOREACH_AFTER(same_proc_swaps
, other
, s
)
1152 if (streq_ptr(other
->what
, other
->parameters_proc_swaps
.what
))
1155 LIST_FOREACH_BEFORE(same_proc_swaps
, other
, s
) {
1156 if (streq_ptr(other
->what
, other
->parameters_proc_swaps
.what
))
1165 static void swap_shutdown(Manager
*m
) {
1168 if (m
->proc_swaps
) {
1169 fclose(m
->proc_swaps
);
1170 m
->proc_swaps
= NULL
;
1173 hashmap_free(m
->swaps_by_proc_swaps
);
1174 m
->swaps_by_proc_swaps
= NULL
;
1177 static int swap_enumerate(Manager
*m
) {
1179 struct epoll_event ev
;
1182 if (!m
->proc_swaps
) {
1183 if (!(m
->proc_swaps
= fopen("/proc/swaps", "re")))
1186 m
->swap_watch
.type
= WATCH_SWAP
;
1187 m
->swap_watch
.fd
= fileno(m
->proc_swaps
);
1190 ev
.events
= EPOLLPRI
;
1191 ev
.data
.ptr
= &m
->swap_watch
;
1193 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->swap_watch
.fd
, &ev
) < 0)
1197 /* We rely on mount.c to load /etc/fstab for us */
1199 if ((r
= swap_load_proc_swaps(m
, false)) < 0)
1205 static void swap_reset_failed(Unit
*u
) {
1210 if (s
->state
== SWAP_FAILED
)
1211 swap_set_state(s
, SWAP_DEAD
);
1216 static const char* const swap_state_table
[_SWAP_STATE_MAX
] = {
1217 [SWAP_DEAD
] = "dead",
1218 [SWAP_ACTIVATING
] = "activating",
1219 [SWAP_ACTIVE
] = "active",
1220 [SWAP_DEACTIVATING
] = "deactivating",
1221 [SWAP_ACTIVATING_SIGTERM
] = "activating-sigterm",
1222 [SWAP_ACTIVATING_SIGKILL
] = "activating-sigkill",
1223 [SWAP_DEACTIVATING_SIGTERM
] = "deactivating-sigterm",
1224 [SWAP_DEACTIVATING_SIGKILL
] = "deactivating-sigkill",
1225 [SWAP_FAILED
] = "failed"
1228 DEFINE_STRING_TABLE_LOOKUP(swap_state
, SwapState
);
1230 static const char* const swap_exec_command_table
[_SWAP_EXEC_COMMAND_MAX
] = {
1231 [SWAP_EXEC_ACTIVATE
] = "ExecActivate",
1232 [SWAP_EXEC_DEACTIVATE
] = "ExecDeactivate",
1235 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command
, SwapExecCommand
);
1237 const UnitVTable swap_vtable
= {
1241 .no_instances
= true,
1243 .show_status
= true,
1249 .coldplug
= swap_coldplug
,
1253 .start
= swap_start
,
1256 .serialize
= swap_serialize
,
1257 .deserialize_item
= swap_deserialize_item
,
1259 .active_state
= swap_active_state
,
1260 .sub_state_to_string
= swap_sub_state_to_string
,
1262 .check_gc
= swap_check_gc
,
1264 .sigchld_event
= swap_sigchld_event
,
1265 .timer_event
= swap_timer_event
,
1267 .reset_failed
= swap_reset_failed
,
1269 .bus_interface
= "org.freedesktop.systemd1.Swap",
1270 .bus_message_handler
= bus_swap_message_handler
,
1271 .bus_invalidating_properties
= bus_swap_invalidating_properties
,
1273 .following
= swap_following
,
1275 .enumerate
= swap_enumerate
,
1276 .shutdown
= swap_shutdown