1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include "alloc-util.h"
16 #include "dbus-swap.h"
19 #include "exit-status.h"
21 #include "format-util.h"
22 #include "fstab-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
27 #include "string-table.h"
28 #include "string-util.h"
30 #include "udev-util.h"
31 #include "unit-name.h"
35 static const UnitActiveState state_translation_table
[_SWAP_STATE_MAX
] = {
36 [SWAP_DEAD
] = UNIT_INACTIVE
,
37 [SWAP_ACTIVATING
] = UNIT_ACTIVATING
,
38 [SWAP_ACTIVATING_DONE
] = UNIT_ACTIVE
,
39 [SWAP_ACTIVE
] = UNIT_ACTIVE
,
40 [SWAP_DEACTIVATING
] = UNIT_DEACTIVATING
,
41 [SWAP_DEACTIVATING_SIGTERM
] = UNIT_DEACTIVATING
,
42 [SWAP_DEACTIVATING_SIGKILL
] = UNIT_DEACTIVATING
,
43 [SWAP_FAILED
] = UNIT_FAILED
46 static int swap_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
47 static int swap_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
49 static bool SWAP_STATE_WITH_PROCESS(SwapState state
) {
54 SWAP_DEACTIVATING_SIGTERM
,
55 SWAP_DEACTIVATING_SIGKILL
);
58 static void swap_unset_proc_swaps(Swap
*s
) {
61 if (!s
->from_proc_swaps
)
64 s
->parameters_proc_swaps
.what
= mfree(s
->parameters_proc_swaps
.what
);
66 s
->from_proc_swaps
= false;
69 static int swap_set_devnode(Swap
*s
, const char *devnode
) {
76 r
= hashmap_ensure_allocated(&UNIT(s
)->manager
->swaps_by_devnode
, &path_hash_ops
);
80 swaps
= UNIT(s
)->manager
->swaps_by_devnode
;
83 first
= hashmap_get(swaps
, s
->devnode
);
85 LIST_REMOVE(same_devnode
, first
, s
);
87 hashmap_replace(swaps
, first
->devnode
, first
);
89 hashmap_remove(swaps
, s
->devnode
);
91 s
->devnode
= mfree(s
->devnode
);
95 s
->devnode
= strdup(devnode
);
99 first
= hashmap_get(swaps
, s
->devnode
);
100 LIST_PREPEND(same_devnode
, first
, s
);
102 return hashmap_replace(swaps
, first
->devnode
, first
);
108 static void swap_init(Unit
*u
) {
112 assert(UNIT(s
)->load_state
== UNIT_STUB
);
114 s
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
116 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
117 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
119 s
->parameters_proc_swaps
.priority
= s
->parameters_fragment
.priority
= -1;
121 s
->control_command_id
= _SWAP_EXEC_COMMAND_INVALID
;
123 u
->ignore_on_isolate
= true;
126 static void swap_unwatch_control_pid(Swap
*s
) {
129 if (s
->control_pid
<= 0)
132 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
136 static void swap_done(Unit
*u
) {
141 swap_unset_proc_swaps(s
);
142 swap_set_devnode(s
, NULL
);
144 s
->what
= mfree(s
->what
);
145 s
->parameters_fragment
.what
= mfree(s
->parameters_fragment
.what
);
146 s
->parameters_fragment
.options
= mfree(s
->parameters_fragment
.options
);
148 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, false);
149 exec_command_done_array(s
->exec_command
, _SWAP_EXEC_COMMAND_MAX
);
150 s
->control_command
= NULL
;
152 dynamic_creds_unref(&s
->dynamic_creds
);
154 swap_unwatch_control_pid(s
);
156 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
159 static int swap_arm_timer(Swap
*s
, usec_t usec
) {
164 if (s
->timer_event_source
) {
165 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
169 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
172 if (usec
== USEC_INFINITY
)
175 r
= sd_event_add_time(
176 UNIT(s
)->manager
->event
,
177 &s
->timer_event_source
,
180 swap_dispatch_timer
, s
);
184 (void) sd_event_source_set_description(s
->timer_event_source
, "swap-timer");
189 static int swap_add_device_dependencies(Swap
*s
) {
195 if (!s
->from_fragment
)
198 if (is_device_path(s
->what
))
199 return unit_add_node_dependency(UNIT(s
), s
->what
, MANAGER_IS_SYSTEM(UNIT(s
)->manager
), UNIT_BINDS_TO
, UNIT_DEPENDENCY_FILE
);
201 /* File based swap devices need to be ordered after
202 * systemd-remount-fs.service, since they might need a
203 * writable file system. */
204 return unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_REMOUNT_FS_SERVICE
, NULL
, true, UNIT_DEPENDENCY_FILE
);
207 static int swap_add_default_dependencies(Swap
*s
) {
212 if (!UNIT(s
)->default_dependencies
)
215 if (!MANAGER_IS_SYSTEM(UNIT(s
)->manager
))
218 if (detect_container() > 0)
221 /* swap units generated for the swap dev links are missing the
222 * ordering dep against the swap target. */
223 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SWAP_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
227 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_UMOUNT_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
230 static int swap_verify(Swap
*s
) {
231 _cleanup_free_
char *e
= NULL
;
234 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
237 r
= unit_name_from_path(s
->what
, ".swap", &e
);
239 return log_unit_error_errno(UNIT(s
), r
, "Failed to generate unit name from path: %m");
241 if (!unit_has_name(UNIT(s
), e
)) {
242 log_unit_error(UNIT(s
), "Value of What= and unit name do not match, not loading.");
246 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
247 log_unit_error(UNIT(s
), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.");
254 static int swap_load_devnode(Swap
*s
) {
255 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
261 if (stat(s
->what
, &st
) < 0 || !S_ISBLK(st
.st_mode
))
264 d
= udev_device_new_from_devnum(UNIT(s
)->manager
->udev
, 'b', st
.st_rdev
);
268 p
= udev_device_get_devnode(d
);
272 return swap_set_devnode(s
, p
);
275 static int swap_load(Unit
*u
) {
280 assert(u
->load_state
== UNIT_STUB
);
282 /* Load a .swap file */
283 if (SWAP(u
)->from_proc_swaps
)
284 r
= unit_load_fragment_and_dropin_optional(u
);
286 r
= unit_load_fragment_and_dropin(u
);
290 if (u
->load_state
== UNIT_LOADED
) {
292 if (UNIT(s
)->fragment_path
)
293 s
->from_fragment
= true;
296 if (s
->parameters_fragment
.what
)
297 s
->what
= strdup(s
->parameters_fragment
.what
);
298 else if (s
->parameters_proc_swaps
.what
)
299 s
->what
= strdup(s
->parameters_proc_swaps
.what
);
301 r
= unit_name_to_path(u
->id
, &s
->what
);
310 path_kill_slashes(s
->what
);
312 if (!UNIT(s
)->description
) {
313 r
= unit_set_description(u
, s
->what
);
318 r
= unit_require_mounts_for(UNIT(s
), s
->what
, UNIT_DEPENDENCY_IMPLICIT
);
322 r
= swap_add_device_dependencies(s
);
326 r
= swap_load_devnode(s
);
330 r
= unit_patch_contexts(u
);
334 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
338 r
= unit_set_default_slice(u
);
342 r
= swap_add_default_dependencies(s
);
347 return swap_verify(s
);
350 static int swap_setup_unit(
353 const char *what_proc_swaps
,
357 _cleanup_free_
char *e
= NULL
;
365 assert(what_proc_swaps
);
367 r
= unit_name_from_path(what
, ".swap", &e
);
369 return log_unit_error_errno(u
, r
, "Failed to generate unit name from path: %m");
371 u
= manager_get_unit(m
, e
);
374 SWAP(u
)->from_proc_swaps
&&
375 !path_equal(SWAP(u
)->parameters_proc_swaps
.what
, what_proc_swaps
)) {
376 log_error("Swap %s appeared twice with different device paths %s and %s", e
, SWAP(u
)->parameters_proc_swaps
.what
, what_proc_swaps
);
383 r
= unit_new_for_name(m
, sizeof(Swap
), e
, &u
);
387 SWAP(u
)->what
= strdup(what
);
388 if (!SWAP(u
)->what
) {
393 unit_add_to_load_queue(u
);
397 p
= &SWAP(u
)->parameters_proc_swaps
;
400 p
->what
= strdup(what_proc_swaps
);
408 SWAP(u
)->is_active
= true;
409 SWAP(u
)->just_activated
= !SWAP(u
)->from_proc_swaps
;
412 SWAP(u
)->from_proc_swaps
= true;
414 p
->priority
= priority
;
416 unit_add_to_dbus_queue(u
);
420 log_unit_warning_errno(u
, r
, "Failed to load swap unit: %m");
428 static int swap_process_new(Manager
*m
, const char *device
, int prio
, bool set_flags
) {
429 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
430 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
437 r
= swap_setup_unit(m
, device
, device
, prio
, set_flags
);
441 /* If this is a block device, then let's add duplicates for
442 * all other names of this block device */
443 if (stat(device
, &st
) < 0 || !S_ISBLK(st
.st_mode
))
446 d
= udev_device_new_from_devnum(m
->udev
, 'b', st
.st_rdev
);
450 /* Add the main device node */
451 dn
= udev_device_get_devnode(d
);
452 if (dn
&& !streq(dn
, device
))
453 swap_setup_unit(m
, dn
, device
, prio
, set_flags
);
455 /* Add additional units for all symlinks */
456 first
= udev_device_get_devlinks_list_entry(d
);
457 udev_list_entry_foreach(item
, first
) {
460 /* Don't bother with the /dev/block links */
461 p
= udev_list_entry_get_name(item
);
463 if (streq(p
, device
))
466 if (path_startswith(p
, "/dev/block/"))
469 if (stat(p
, &st
) >= 0)
470 if (!S_ISBLK(st
.st_mode
) ||
471 st
.st_rdev
!= udev_device_get_devnum(d
))
474 swap_setup_unit(m
, p
, device
, prio
, set_flags
);
480 static void swap_set_state(Swap
*s
, SwapState state
) {
486 old_state
= s
->state
;
489 if (!SWAP_STATE_WITH_PROCESS(state
)) {
490 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
491 swap_unwatch_control_pid(s
);
492 s
->control_command
= NULL
;
493 s
->control_command_id
= _SWAP_EXEC_COMMAND_INVALID
;
496 if (state
!= old_state
)
497 log_unit_debug(UNIT(s
), "Changed %s -> %s", swap_state_to_string(old_state
), swap_state_to_string(state
));
499 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
501 /* If there other units for the same device node have a job
502 queued it might be worth checking again if it is runnable
503 now. This is necessary, since swap_start() refuses
504 operation with EAGAIN if there's already another job for
505 the same device node queued. */
506 LIST_FOREACH_OTHERS(same_devnode
, other
, s
)
507 if (UNIT(other
)->job
)
508 job_add_to_run_queue(UNIT(other
)->job
);
511 static int swap_coldplug(Unit
*u
) {
513 SwapState new_state
= SWAP_DEAD
;
517 assert(s
->state
== SWAP_DEAD
);
519 if (s
->deserialized_state
!= s
->state
)
520 new_state
= s
->deserialized_state
;
521 else if (s
->from_proc_swaps
)
522 new_state
= SWAP_ACTIVE
;
524 if (new_state
== s
->state
)
527 if (s
->control_pid
> 0 &&
528 pid_is_unwaited(s
->control_pid
) &&
529 SWAP_STATE_WITH_PROCESS(new_state
)) {
531 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
535 r
= swap_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
540 if (!IN_SET(new_state
, SWAP_DEAD
, SWAP_FAILED
)) {
541 (void) unit_setup_dynamic_creds(u
);
542 (void) unit_setup_exec_runtime(u
);
545 swap_set_state(s
, new_state
);
549 static void swap_dump(Unit
*u
, FILE *f
, const char *prefix
) {
550 char buf
[FORMAT_TIMESPAN_MAX
];
557 if (s
->from_proc_swaps
)
558 p
= &s
->parameters_proc_swaps
;
559 else if (s
->from_fragment
)
560 p
= &s
->parameters_fragment
;
568 "%sFrom /proc/swaps: %s\n"
569 "%sFrom fragment: %s\n",
570 prefix
, swap_state_to_string(s
->state
),
571 prefix
, swap_result_to_string(s
->result
),
573 prefix
, yes_no(s
->from_proc_swaps
),
574 prefix
, yes_no(s
->from_fragment
));
577 fprintf(f
, "%sDevice Node: %s\n", prefix
, s
->devnode
);
584 prefix
, strempty(p
->options
));
587 "%sTimeoutSec: %s\n",
588 prefix
, format_timespan(buf
, sizeof(buf
), s
->timeout_usec
, USEC_PER_SEC
));
590 if (s
->control_pid
> 0)
592 "%sControl PID: "PID_FMT
"\n",
593 prefix
, s
->control_pid
);
595 exec_context_dump(&s
->exec_context
, f
, prefix
);
596 kill_context_dump(&s
->kill_context
, f
, prefix
);
597 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
600 static int swap_spawn(Swap
*s
, ExecCommand
*c
, pid_t
*_pid
) {
602 ExecParameters exec_params
= {
603 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
615 r
= unit_prepare_exec(UNIT(s
));
619 r
= swap_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
623 unit_set_exec_params(UNIT(s
), &exec_params
);
625 r
= exec_spawn(UNIT(s
),
635 r
= unit_watch_pid(UNIT(s
), pid
);
637 /* FIXME: we need to do something here */
645 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
650 static void swap_enter_dead(Swap
*s
, SwapResult f
) {
653 if (s
->result
== SWAP_SUCCESS
)
656 if (s
->result
!= SWAP_SUCCESS
)
657 log_unit_warning(UNIT(s
), "Failed with result '%s'.", swap_result_to_string(s
->result
));
659 swap_set_state(s
, s
->result
!= SWAP_SUCCESS
? SWAP_FAILED
: SWAP_DEAD
);
661 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, true);
663 exec_context_destroy_runtime_directory(&s
->exec_context
, UNIT(s
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
665 unit_unref_uid_gid(UNIT(s
), true);
667 dynamic_creds_destroy(&s
->dynamic_creds
);
670 static void swap_enter_active(Swap
*s
, SwapResult f
) {
673 if (s
->result
== SWAP_SUCCESS
)
676 swap_set_state(s
, SWAP_ACTIVE
);
679 static void swap_enter_dead_or_active(Swap
*s
, SwapResult f
) {
682 if (s
->from_proc_swaps
)
683 swap_enter_active(s
, f
);
685 swap_enter_dead(s
, f
);
688 static void swap_enter_signal(Swap
*s
, SwapState state
, SwapResult f
) {
694 if (s
->result
== SWAP_SUCCESS
)
697 if (state
== SWAP_DEACTIVATING_SIGTERM
)
698 kop
= KILL_TERMINATE
;
702 r
= unit_kill_context(UNIT(s
), &s
->kill_context
, kop
, -1, s
->control_pid
, false);
707 r
= swap_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
711 swap_set_state(s
, state
);
712 } else if (state
== SWAP_DEACTIVATING_SIGTERM
&& s
->kill_context
.send_sigkill
)
713 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGKILL
, SWAP_SUCCESS
);
715 swap_enter_dead_or_active(s
, SWAP_SUCCESS
);
720 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
721 swap_enter_dead_or_active(s
, SWAP_FAILURE_RESOURCES
);
724 static void swap_enter_activating(Swap
*s
) {
725 _cleanup_free_
char *opts
= NULL
;
730 unit_warn_leftover_processes(UNIT(s
));
732 s
->control_command_id
= SWAP_EXEC_ACTIVATE
;
733 s
->control_command
= s
->exec_command
+ SWAP_EXEC_ACTIVATE
;
735 if (s
->from_fragment
) {
738 r
= fstab_find_pri(s
->parameters_fragment
.options
, &priority
);
740 log_warning_errno(r
, "Failed to parse swap priority \"%s\", ignoring: %m", s
->parameters_fragment
.options
);
741 else if (r
== 1 && s
->parameters_fragment
.priority
>= 0)
742 log_warning("Duplicate swap priority configuration by Priority and Options fields.");
744 if (r
<= 0 && s
->parameters_fragment
.priority
>= 0) {
745 if (s
->parameters_fragment
.options
)
746 r
= asprintf(&opts
, "%s,pri=%i", s
->parameters_fragment
.options
, s
->parameters_fragment
.priority
);
748 r
= asprintf(&opts
, "pri=%i", s
->parameters_fragment
.priority
);
754 r
= exec_command_set(s
->control_command
, "/sbin/swapon", NULL
);
758 if (s
->parameters_fragment
.options
|| opts
) {
759 r
= exec_command_append(s
->control_command
, "-o",
760 opts
? : s
->parameters_fragment
.options
, NULL
);
765 r
= exec_command_append(s
->control_command
, s
->what
, NULL
);
769 swap_unwatch_control_pid(s
);
771 r
= swap_spawn(s
, s
->control_command
, &s
->control_pid
);
775 swap_set_state(s
, SWAP_ACTIVATING
);
780 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'swapon' task: %m");
781 swap_enter_dead_or_active(s
, SWAP_FAILURE_RESOURCES
);
784 static void swap_enter_deactivating(Swap
*s
) {
789 s
->control_command_id
= SWAP_EXEC_DEACTIVATE
;
790 s
->control_command
= s
->exec_command
+ SWAP_EXEC_DEACTIVATE
;
792 r
= exec_command_set(s
->control_command
,
799 swap_unwatch_control_pid(s
);
801 r
= swap_spawn(s
, s
->control_command
, &s
->control_pid
);
805 swap_set_state(s
, SWAP_DEACTIVATING
);
810 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'swapoff' task: %m");
811 swap_enter_dead_or_active(s
, SWAP_FAILURE_RESOURCES
);
814 static int swap_start(Unit
*u
) {
815 Swap
*s
= SWAP(u
), *other
;
820 /* We cannot fulfill this request right now, try again later please! */
823 SWAP_DEACTIVATING_SIGTERM
,
824 SWAP_DEACTIVATING_SIGKILL
))
828 if (s
->state
== SWAP_ACTIVATING
)
831 assert(IN_SET(s
->state
, SWAP_DEAD
, SWAP_FAILED
));
833 if (detect_container() > 0)
836 /* If there's a job for another swap unit for the same node
837 * running, then let's not dispatch this one for now, and wait
838 * until that other job has finished. */
839 LIST_FOREACH_OTHERS(same_devnode
, other
, s
)
840 if (UNIT(other
)->job
&& UNIT(other
)->job
->state
== JOB_RUNNING
)
843 r
= unit_start_limit_test(u
);
845 swap_enter_dead(s
, SWAP_FAILURE_START_LIMIT_HIT
);
849 r
= unit_acquire_invocation_id(u
);
853 s
->result
= SWAP_SUCCESS
;
855 u
->reset_accounting
= true;
857 swap_enter_activating(s
);
861 static int swap_stop(Unit
*u
) {
868 case SWAP_DEACTIVATING
:
869 case SWAP_DEACTIVATING_SIGTERM
:
870 case SWAP_DEACTIVATING_SIGKILL
:
874 case SWAP_ACTIVATING
:
875 case SWAP_ACTIVATING_DONE
:
876 /* There's a control process pending, directly enter kill mode */
877 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGTERM
, SWAP_SUCCESS
);
881 if (detect_container() > 0)
884 swap_enter_deactivating(s
);
888 assert_not_reached("Unexpected state.");
892 static int swap_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
899 unit_serialize_item(u
, f
, "state", swap_state_to_string(s
->state
));
900 unit_serialize_item(u
, f
, "result", swap_result_to_string(s
->result
));
902 if (s
->control_pid
> 0)
903 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
905 if (s
->control_command_id
>= 0)
906 unit_serialize_item(u
, f
, "control-command", swap_exec_command_to_string(s
->control_command_id
));
911 static int swap_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
917 if (streq(key
, "state")) {
920 state
= swap_state_from_string(value
);
922 log_unit_debug(u
, "Failed to parse state value: %s", value
);
924 s
->deserialized_state
= state
;
925 } else if (streq(key
, "result")) {
928 f
= swap_result_from_string(value
);
930 log_unit_debug(u
, "Failed to parse result value: %s", value
);
931 else if (f
!= SWAP_SUCCESS
)
933 } else if (streq(key
, "control-pid")) {
936 if (parse_pid(value
, &pid
) < 0)
937 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
939 s
->control_pid
= pid
;
941 } else if (streq(key
, "control-command")) {
944 id
= swap_exec_command_from_string(value
);
946 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
948 s
->control_command_id
= id
;
949 s
->control_command
= s
->exec_command
+ id
;
952 log_unit_debug(u
, "Unknown serialization key: %s", key
);
957 _pure_
static UnitActiveState
swap_active_state(Unit
*u
) {
960 return state_translation_table
[SWAP(u
)->state
];
963 _pure_
static const char *swap_sub_state_to_string(Unit
*u
) {
966 return swap_state_to_string(SWAP(u
)->state
);
969 _pure_
static bool swap_may_gc(Unit
*u
) {
974 if (s
->from_proc_swaps
)
980 static void swap_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
987 if (pid
!= s
->control_pid
)
992 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
994 else if (code
== CLD_EXITED
)
995 f
= SWAP_FAILURE_EXIT_CODE
;
996 else if (code
== CLD_KILLED
)
997 f
= SWAP_FAILURE_SIGNAL
;
998 else if (code
== CLD_DUMPED
)
999 f
= SWAP_FAILURE_CORE_DUMP
;
1001 assert_not_reached("Unknown code");
1003 if (s
->result
== SWAP_SUCCESS
)
1006 if (s
->control_command
) {
1007 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
1009 s
->control_command
= NULL
;
1010 s
->control_command_id
= _SWAP_EXEC_COMMAND_INVALID
;
1013 log_unit_full(u
, f
== SWAP_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
1014 "Swap process exited, code=%s status=%i", sigchld_code_to_string(code
), status
);
1018 case SWAP_ACTIVATING
:
1019 case SWAP_ACTIVATING_DONE
:
1021 if (f
== SWAP_SUCCESS
|| s
->from_proc_swaps
)
1022 swap_enter_active(s
, f
);
1024 swap_enter_dead(s
, f
);
1027 case SWAP_DEACTIVATING
:
1028 case SWAP_DEACTIVATING_SIGKILL
:
1029 case SWAP_DEACTIVATING_SIGTERM
:
1031 swap_enter_dead_or_active(s
, f
);
1035 assert_not_reached("Uh, control process died at wrong time.");
1038 /* Notify clients about changed exit status */
1039 unit_add_to_dbus_queue(u
);
1042 static int swap_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
1043 Swap
*s
= SWAP(userdata
);
1046 assert(s
->timer_event_source
== source
);
1050 case SWAP_ACTIVATING
:
1051 case SWAP_ACTIVATING_DONE
:
1052 log_unit_warning(UNIT(s
), "Activation timed out. Stopping.");
1053 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGTERM
, SWAP_FAILURE_TIMEOUT
);
1056 case SWAP_DEACTIVATING
:
1057 log_unit_warning(UNIT(s
), "Deactivation timed out. Stopping.");
1058 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGTERM
, SWAP_FAILURE_TIMEOUT
);
1061 case SWAP_DEACTIVATING_SIGTERM
:
1062 if (s
->kill_context
.send_sigkill
) {
1063 log_unit_warning(UNIT(s
), "Swap process timed out. Killing.");
1064 swap_enter_signal(s
, SWAP_DEACTIVATING_SIGKILL
, SWAP_FAILURE_TIMEOUT
);
1066 log_unit_warning(UNIT(s
), "Swap process timed out. Skipping SIGKILL. Ignoring.");
1067 swap_enter_dead_or_active(s
, SWAP_FAILURE_TIMEOUT
);
1071 case SWAP_DEACTIVATING_SIGKILL
:
1072 log_unit_warning(UNIT(s
), "Swap process still around after SIGKILL. Ignoring.");
1073 swap_enter_dead_or_active(s
, SWAP_FAILURE_TIMEOUT
);
1077 assert_not_reached("Timeout at wrong time.");
1083 static int swap_load_proc_swaps(Manager
*m
, bool set_flags
) {
1089 rewind(m
->proc_swaps
);
1091 (void) fscanf(m
->proc_swaps
, "%*s %*s %*s %*s %*s\n");
1094 _cleanup_free_
char *dev
= NULL
, *d
= NULL
;
1097 k
= fscanf(m
->proc_swaps
,
1098 "%ms " /* device/file */
1099 "%*s " /* type of swap */
1100 "%*s " /* swap size */
1102 "%i\n", /* priority */
1108 log_warning("Failed to parse /proc/swaps:%u.", i
);
1112 if (cunescape(dev
, UNESCAPE_RELAX
, &d
) < 0)
1115 device_found_node(m
, d
, true, DEVICE_FOUND_SWAP
, set_flags
);
1117 k
= swap_process_new(m
, d
, prio
, set_flags
);
1125 static int swap_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
1126 Manager
*m
= userdata
;
1131 assert(revents
& EPOLLPRI
);
1133 r
= swap_load_proc_swaps(m
, true);
1135 log_error_errno(r
, "Failed to reread /proc/swaps: %m");
1137 /* Reset flags, just in case, for late calls */
1138 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_SWAP
]) {
1139 Swap
*swap
= SWAP(u
);
1141 swap
->is_active
= swap
->just_activated
= false;
1147 manager_dispatch_load_queue(m
);
1149 LIST_FOREACH(units_by_type
, u
, m
->units_by_type
[UNIT_SWAP
]) {
1150 Swap
*swap
= SWAP(u
);
1152 if (!swap
->is_active
) {
1153 /* This has just been deactivated */
1155 swap_unset_proc_swaps(swap
);
1157 switch (swap
->state
) {
1160 swap_enter_dead(swap
, SWAP_SUCCESS
);
1165 swap_set_state(swap
, swap
->state
);
1170 device_found_node(m
, swap
->what
, false, DEVICE_FOUND_SWAP
, true);
1172 } else if (swap
->just_activated
) {
1174 /* New swap entry */
1176 switch (swap
->state
) {
1180 (void) unit_acquire_invocation_id(UNIT(swap
));
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_OTHERS(same_devnode
, other
, s
)
1218 if (other
->from_fragment
)
1221 /* Otherwise, make everybody follow the unit that's named after
1222 * the swap device in the kernel */
1224 if (streq_ptr(s
->what
, s
->devnode
))
1227 LIST_FOREACH_AFTER(same_devnode
, other
, s
)
1228 if (streq_ptr(other
->what
, other
->devnode
))
1231 LIST_FOREACH_BEFORE(same_devnode
, other
, s
) {
1232 if (streq_ptr(other
->what
, other
->devnode
))
1238 /* Fall back to the first on the list */
1242 static int swap_following_set(Unit
*u
, Set
**_set
) {
1243 Swap
*s
= SWAP(u
), *other
;
1244 _cleanup_set_free_ Set
*set
= NULL
;
1250 if (LIST_JUST_US(same_devnode
, s
)) {
1255 set
= set_new(NULL
);
1259 LIST_FOREACH_OTHERS(same_devnode
, other
, s
) {
1260 r
= set_put(set
, other
);
1265 *_set
= TAKE_PTR(set
);
1269 static void swap_shutdown(Manager
*m
) {
1272 m
->swap_event_source
= sd_event_source_unref(m
->swap_event_source
);
1274 m
->proc_swaps
= safe_fclose(m
->proc_swaps
);
1276 m
->swaps_by_devnode
= hashmap_free(m
->swaps_by_devnode
);
1279 static void swap_enumerate(Manager
*m
) {
1284 if (!m
->proc_swaps
) {
1285 m
->proc_swaps
= fopen("/proc/swaps", "re");
1286 if (!m
->proc_swaps
) {
1287 if (errno
== ENOENT
)
1288 log_debug("Not swap enabled, skipping enumeration");
1290 log_error_errno(errno
, "Failed to open /proc/swaps: %m");
1295 r
= sd_event_add_io(m
->event
, &m
->swap_event_source
, fileno(m
->proc_swaps
), EPOLLPRI
, swap_dispatch_io
, m
);
1297 log_error_errno(r
, "Failed to watch /proc/swaps: %m");
1301 /* Dispatch this before we dispatch SIGCHLD, so that
1302 * we always get the events from /proc/swaps before
1303 * the SIGCHLD of /sbin/swapon. */
1304 r
= sd_event_source_set_priority(m
->swap_event_source
, SD_EVENT_PRIORITY_NORMAL
-10);
1306 log_error_errno(r
, "Failed to change /proc/swaps priority: %m");
1310 (void) sd_event_source_set_description(m
->swap_event_source
, "swap-proc");
1313 r
= swap_load_proc_swaps(m
, false);
1323 int swap_process_device_new(Manager
*m
, struct udev_device
*dev
) {
1324 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
1325 _cleanup_free_
char *e
= NULL
;
1333 dn
= udev_device_get_devnode(dev
);
1337 r
= unit_name_from_path(dn
, ".swap", &e
);
1341 u
= manager_get_unit(m
, e
);
1343 r
= swap_set_devnode(SWAP(u
), dn
);
1345 first
= udev_device_get_devlinks_list_entry(dev
);
1346 udev_list_entry_foreach(item
, first
) {
1347 _cleanup_free_
char *n
= NULL
;
1350 q
= unit_name_from_path(udev_list_entry_get_name(item
), ".swap", &n
);
1354 u
= manager_get_unit(m
, n
);
1356 q
= swap_set_devnode(SWAP(u
), dn
);
1365 int swap_process_device_remove(Manager
*m
, struct udev_device
*dev
) {
1370 dn
= udev_device_get_devnode(dev
);
1374 while ((s
= hashmap_get(m
->swaps_by_devnode
, dn
))) {
1377 q
= swap_set_devnode(s
, NULL
);
1385 static void swap_reset_failed(Unit
*u
) {
1390 if (s
->state
== SWAP_FAILED
)
1391 swap_set_state(s
, SWAP_DEAD
);
1393 s
->result
= SWAP_SUCCESS
;
1396 static int swap_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
1397 return unit_kill_common(u
, who
, signo
, -1, SWAP(u
)->control_pid
, error
);
1400 static int swap_get_timeout(Unit
*u
, usec_t
*timeout
) {
1405 if (!s
->timer_event_source
)
1408 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
1411 if (t
== USEC_INFINITY
)
1418 static bool swap_supported(void) {
1419 static int supported
= -1;
1421 /* If swap support is not available in the kernel, or we are
1422 * running in a container we don't support swap units, and any
1423 * attempts to starting one should fail immediately. */
1427 access("/proc/swaps", F_OK
) >= 0 &&
1428 detect_container() <= 0;
1433 static int swap_control_pid(Unit
*u
) {
1438 return s
->control_pid
;
1441 static const char* const swap_exec_command_table
[_SWAP_EXEC_COMMAND_MAX
] = {
1442 [SWAP_EXEC_ACTIVATE
] = "ExecActivate",
1443 [SWAP_EXEC_DEACTIVATE
] = "ExecDeactivate",
1446 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command
, SwapExecCommand
);
1448 static const char* const swap_result_table
[_SWAP_RESULT_MAX
] = {
1449 [SWAP_SUCCESS
] = "success",
1450 [SWAP_FAILURE_RESOURCES
] = "resources",
1451 [SWAP_FAILURE_TIMEOUT
] = "timeout",
1452 [SWAP_FAILURE_EXIT_CODE
] = "exit-code",
1453 [SWAP_FAILURE_SIGNAL
] = "signal",
1454 [SWAP_FAILURE_CORE_DUMP
] = "core-dump",
1455 [SWAP_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
1458 DEFINE_STRING_TABLE_LOOKUP(swap_result
, SwapResult
);
1460 const UnitVTable swap_vtable
= {
1461 .object_size
= sizeof(Swap
),
1462 .exec_context_offset
= offsetof(Swap
, exec_context
),
1463 .cgroup_context_offset
= offsetof(Swap
, cgroup_context
),
1464 .kill_context_offset
= offsetof(Swap
, kill_context
),
1465 .exec_runtime_offset
= offsetof(Swap
, exec_runtime
),
1466 .dynamic_creds_offset
= offsetof(Swap
, dynamic_creds
),
1472 .private_section
= "Swap",
1478 .coldplug
= swap_coldplug
,
1482 .start
= swap_start
,
1487 .get_timeout
= swap_get_timeout
,
1489 .serialize
= swap_serialize
,
1490 .deserialize_item
= swap_deserialize_item
,
1492 .active_state
= swap_active_state
,
1493 .sub_state_to_string
= swap_sub_state_to_string
,
1495 .may_gc
= swap_may_gc
,
1497 .sigchld_event
= swap_sigchld_event
,
1499 .reset_failed
= swap_reset_failed
,
1501 .control_pid
= swap_control_pid
,
1503 .bus_vtable
= bus_swap_vtable
,
1504 .bus_set_property
= bus_swap_set_property
,
1505 .bus_commit_properties
= bus_swap_commit_properties
,
1507 .following
= swap_following
,
1508 .following_set
= swap_following_set
,
1510 .enumerate
= swap_enumerate
,
1511 .shutdown
= swap_shutdown
,
1512 .supported
= swap_supported
,
1514 .status_message_formats
= {
1515 .starting_stopping
= {
1516 [0] = "Activating swap %s...",
1517 [1] = "Deactivating swap %s...",
1519 .finished_start_job
= {
1520 [JOB_DONE
] = "Activated swap %s.",
1521 [JOB_FAILED
] = "Failed to activate swap %s.",
1522 [JOB_TIMEOUT
] = "Timed out activating swap %s.",
1524 .finished_stop_job
= {
1525 [JOB_DONE
] = "Deactivated swap %s.",
1526 [JOB_FAILED
] = "Failed deactivating swap %s.",
1527 [JOB_TIMEOUT
] = "Timed out deactivating swap %s.",