From f5fcacdf2cb1dee456f7e5ae698caa3aaa1677b8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 2 Jun 2023 11:02:16 +0200 Subject: [PATCH] detach-loopback: also decouple from umount.h Let's introduce LoopbackDevice as replacement for MountPoint, with just the fields we actually need. --- src/shutdown/detach-loopback.c | 42 ++++++++++++++++++++++++++-------- src/shutdown/test-umount.c | 5 ++-- src/shutdown/umount.c | 2 +- src/shutdown/umount.h | 4 ++-- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/shutdown/detach-loopback.c b/src/shutdown/detach-loopback.c index 49150077d6..ceac74223c 100644 --- a/src/shutdown/detach-loopback.c +++ b/src/shutdown/detach-loopback.c @@ -18,9 +18,31 @@ #include "detach-loopback.h" #include "device-util.h" #include "fd-util.h" -#include "umount.h" -static int loopback_list_get(MountPoint **head) { +typedef struct LoopbackDevice { + char *path; + dev_t devnum; + LIST_FIELDS(struct LoopbackDevice, loopback_device); +} LoopbackDevice; + +static void loopback_device_free(LoopbackDevice **head, LoopbackDevice *m) { + assert(head); + assert(m); + + LIST_REMOVE(loopback_device, *head, m); + + free(m->path); + free(m); +} + +static void loopback_device_list_free(LoopbackDevice **head) { + assert(head); + + while (*head) + loopback_device_free(head, *head); +} + +static int loopback_list_get(LoopbackDevice **head) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; sd_device *d; int r; @@ -50,7 +72,7 @@ static int loopback_list_get(MountPoint **head) { FOREACH_DEVICE(e, d) { _cleanup_free_ char *p = NULL; const char *dn; - MountPoint *lb; + LoopbackDevice *lb; dev_t devnum; if (sd_device_get_devnum(d, &devnum) < 0 || @@ -61,16 +83,16 @@ static int loopback_list_get(MountPoint **head) { if (!p) return -ENOMEM; - lb = new(MountPoint, 1); + lb = new(LoopbackDevice, 1); if (!lb) return -ENOMEM; - *lb = (MountPoint) { + *lb = (LoopbackDevice) { .path = TAKE_PTR(p), .devnum = devnum, }; - LIST_PREPEND(mount_point, *head, lb); + LIST_PREPEND(loopback_device, *head, lb); } return 0; @@ -151,7 +173,7 @@ static int delete_loopback(const char *device) { return -EBUSY; /* Nothing changed, the device is still attached, hence it apparently is still busy */ } -static int loopback_points_list_detach(MountPoint **head, bool *changed, bool last_try) { +static int loopback_points_list_detach(LoopbackDevice **head, bool *changed, bool last_try) { int n_failed = 0, r; dev_t rootdev = 0; @@ -160,7 +182,7 @@ static int loopback_points_list_detach(MountPoint **head, bool *changed, bool la (void) get_block_device("/", &rootdev); - LIST_FOREACH(mount_point, m, *head) { + LIST_FOREACH(loopback_device, m, *head) { if (major(rootdev) != 0 && rootdev == m->devnum) { n_failed++; continue; @@ -176,14 +198,14 @@ static int loopback_points_list_detach(MountPoint **head, bool *changed, bool la if (r > 0) *changed = true; - mount_point_free(head, m); + loopback_device_free(head, m); } return n_failed; } int loopback_detach_all(bool *changed, bool last_try) { - _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, loopback_list_head); + _cleanup_(loopback_device_list_free) LIST_HEAD(LoopbackDevice, loopback_list_head); int r; assert(changed); diff --git a/src/shutdown/test-umount.c b/src/shutdown/test-umount.c index d74cd878c6..93da2e0fc3 100644 --- a/src/shutdown/test-umount.c +++ b/src/shutdown/test-umount.c @@ -24,12 +24,11 @@ static void test_mount_points_list_one(const char *fname) { assert_se(mount_points_list_get(fname, &mp_list_head) >= 0); LIST_FOREACH(mount_point, m, mp_list_head) - log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u", + log_debug("path=%s o=%s f=0x%lx try-ro=%s", m->path, strempty(m->remount_options), m->remount_flags, - yes_no(m->try_remount_ro), - major(m->devnum), minor(m->devnum)); + yes_no(m->try_remount_ro)); } TEST(mount_points_list) { diff --git a/src/shutdown/umount.c b/src/shutdown/umount.c index 05f7b5b513..6cbe2befa2 100644 --- a/src/shutdown/umount.c +++ b/src/shutdown/umount.c @@ -29,7 +29,7 @@ #include "umount.h" #include "virt.h" -void mount_point_free(MountPoint **head, MountPoint *m) { +static void mount_point_free(MountPoint **head, MountPoint *m) { assert(head); assert(m); diff --git a/src/shutdown/umount.h b/src/shutdown/umount.h index eeda7f8ad9..f8f9ae8038 100644 --- a/src/shutdown/umount.h +++ b/src/shutdown/umount.h @@ -5,6 +5,8 @@ Copyright © 2010 ProFUSION embedded systems ***/ +#include + #include "list.h" int umount_all(bool *changed, bool last_try); @@ -17,10 +19,8 @@ typedef struct MountPoint { bool try_remount_ro; bool umount_lazily; bool leaf; - dev_t devnum; LIST_FIELDS(struct MountPoint, mount_point); } MountPoint; int mount_points_list_get(const char *mountinfo, MountPoint **head); -void mount_point_free(MountPoint **head, MountPoint *m); void mount_points_list_free(MountPoint **head);