detach-loopback: also decouple from umount.h

Let's introduce LoopbackDevice as replacement for MountPoint, with just
the fields we actually need.
This commit is contained in:
Lennart Poettering
2023-06-02 11:02:16 +02:00
parent 6514a6fd82
commit f5fcacdf2c
4 changed files with 37 additions and 16 deletions

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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);

View File

@@ -5,6 +5,8 @@
Copyright © 2010 ProFUSION embedded systems
***/
#include <stdbool.h>
#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);