From: Lennart Poettering Date: Fri, 2 Jun 2023 08:51:08 +0000 (+0200) Subject: detach-swap: decouple from umount.h X-Git-Tag: v254-rc1~299^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a0fb20672d6bcbee6ee36b679adcbac67e33e799;p=thirdparty%2Fsystemd.git detach-swap: decouple from umount.h So far detach-swap.[ch] were still using the MountPoint structure to store swap device info in. Since it was only using a single field of it sharing the whole structure is kinda pointless. Hence, let's decouple this and only add the field we really need. --- diff --git a/src/shutdown/detach-swap.c b/src/shutdown/detach-swap.c index 83e428038e2..fd7dcdf943d 100644 --- a/src/shutdown/detach-swap.c +++ b/src/shutdown/detach-swap.c @@ -9,7 +9,24 @@ #include "detach-swap.h" #include "libmount-util.h" -int swap_list_get(const char *swaps, MountPoint **head) { +static void swap_device_free(SwapDevice **head, SwapDevice *m) { + assert(head); + assert(m); + + LIST_REMOVE(swap_device, *head, m); + + free(m->path); + free(m); +} + +void swap_devices_list_free(SwapDevice **head) { + assert(head); + + while (*head) + swap_device_free(head, *head); +} + +int swap_list_get(const char *swaps, SwapDevice **head) { _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL; _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL; int r; @@ -29,7 +46,7 @@ int swap_list_get(const char *swaps, MountPoint **head) { for (;;) { struct libmnt_fs *fs; - _cleanup_free_ MountPoint *swap = NULL; + _cleanup_free_ SwapDevice *swap = NULL; const char *source; r = mnt_table_next_fs(t, i, &fs); @@ -42,7 +59,7 @@ int swap_list_get(const char *swaps, MountPoint **head) { if (!source) continue; - swap = new0(MountPoint, 1); + swap = new0(SwapDevice, 1); if (!swap) return log_oom(); @@ -50,19 +67,19 @@ int swap_list_get(const char *swaps, MountPoint **head) { if (!swap->path) return log_oom(); - LIST_PREPEND(mount_point, *head, TAKE_PTR(swap)); + LIST_PREPEND(swap_device, *head, TAKE_PTR(swap)); } return 0; } -static int swap_points_list_off(MountPoint **head, bool *changed) { +static int swap_points_list_off(SwapDevice **head, bool *changed) { int n_failed = 0; assert(head); assert(changed); - LIST_FOREACH(mount_point, m, *head) { + LIST_FOREACH(swap_device, m, *head) { log_info("Deactivating swap %s.", m->path); if (swapoff(m->path) < 0) { log_warning_errno(errno, "Could not deactivate swap %s: %m", m->path); @@ -71,14 +88,14 @@ static int swap_points_list_off(MountPoint **head, bool *changed) { } *changed = true; - mount_point_free(head, m); + swap_device_free(head, m); } return n_failed; } int swapoff_all(bool *changed) { - _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, swap_list_head); + _cleanup_(swap_devices_list_free) LIST_HEAD(SwapDevice, swap_list_head); int r; assert(changed); diff --git a/src/shutdown/detach-swap.h b/src/shutdown/detach-swap.h index 7c2b97f7738..1ebf5eb9080 100644 --- a/src/shutdown/detach-swap.h +++ b/src/shutdown/detach-swap.h @@ -7,8 +7,15 @@ #include -#include "umount.h" +#include "list.h" int swapoff_all(bool *changed); -int swap_list_get(const char *swaps, MountPoint **head); +/* This is exported just for testing */ +typedef struct SwapDevice { + char *path; + LIST_FIELDS(struct SwapDevice, swap_device); +} SwapDevice; + +int swap_list_get(const char *swaps, SwapDevice **head); +void swap_devices_list_free(SwapDevice **head); diff --git a/src/shutdown/test-umount.c b/src/shutdown/test-umount.c index 36036dd3757..d74cd878c63 100644 --- a/src/shutdown/test-umount.c +++ b/src/shutdown/test-umount.c @@ -40,7 +40,7 @@ TEST(mount_points_list) { } static void test_swap_list_one(const char *fname) { - _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head); + _cleanup_(swap_devices_list_free) LIST_HEAD(SwapDevice, sd_list_head); _cleanup_free_ char *testdata_fname = NULL; int r; @@ -51,19 +51,14 @@ static void test_swap_list_one(const char *fname) { fname = testdata_fname; } - LIST_HEAD_INIT(mp_list_head); - r = swap_list_get(fname, &mp_list_head); + LIST_HEAD_INIT(sd_list_head); + r = swap_list_get(fname, &sd_list_head); if (ERRNO_IS_PRIVILEGE(r)) return; assert_se(r >= 0); - LIST_FOREACH(mount_point, m, mp_list_head) - log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u", - m->path, - strempty(m->remount_options), - m->remount_flags, - yes_no(m->try_remount_ro), - major(m->devnum), minor(m->devnum)); + LIST_FOREACH(swap_device, m, sd_list_head) + log_debug("path=%s", m->path); } TEST(swap_list) {