]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
detach-swap: decouple from umount.h
authorLennart Poettering <lennart@poettering.net>
Fri, 2 Jun 2023 08:51:08 +0000 (10:51 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 Jun 2023 13:56:06 +0000 (15:56 +0200)
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.

src/shutdown/detach-swap.c
src/shutdown/detach-swap.h
src/shutdown/test-umount.c

index 83e428038e238c5a473e2a980fc5aa1f43050bd9..fd7dcdf943d15b5a6429e2d59bb037881938d77f 100644 (file)
@@ -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);
index 7c2b97f7738e02fa6e256dd13596a8b88a43e186..1ebf5eb90806d5ce32fee7e9f5b3116afdede7ee 100644 (file)
@@ -7,8 +7,15 @@
 
 #include <stdbool.h>
 
-#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);
index 36036dd3757a39b60083fa6026035cd06506f829..d74cd878c63a61b8cb462be836c8dbf1d4427c54 100644 (file)
@@ -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) {