summaryrefslogtreecommitdiff
path: root/src/mount
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-11-22 11:04:33 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2022-11-24 14:17:35 +0900
commit961d08cadb814dca9be60be55b119fc3a926e3e2 (patch)
treefdb08c4e63de54cfd8e191e0df50c923257f406f /src/mount
parent95a45a87d6425864b8fc962b45efc7d1f92bc593 (diff)
downloadsystemd-961d08cadb814dca9be60be55b119fc3a926e3e2.tar.gz
mount: split umount_by_device() into two
No functional change, just refactoring and preparation for later commits.
Diffstat (limited to 'src/mount')
-rw-r--r--src/mount/mount-tool.c67
1 files changed, 37 insertions, 30 deletions
diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c
index 72570f6fa6..b4007e58a9 100644
--- a/src/mount/mount-tool.c
+++ b/src/mount/mount-tool.c
@@ -914,49 +914,56 @@ static int stop_mounts(
return 0;
}
-static int umount_by_device(sd_bus *bus, const char *what) {
+static int umount_by_device(sd_bus *bus, sd_device *dev) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_strv_free_ char **list = NULL;
- struct stat st;
const char *v;
- char **l;
- int r, r2 = 0;
-
- assert(what);
-
- if (stat(what, &st) < 0)
- return log_error_errno(errno, "Can't stat %s: %m", what);
+ int r, ret = 0;
- if (!S_ISBLK(st.st_mode))
- return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK),
- "Not a block device: %s", what);
+ assert(bus);
+ assert(dev);
- r = sd_device_new_from_stat_rdev(&d, &st);
- if (r < 0)
- return log_error_errno(r, "Failed to get device from device number: %m");
+ if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
+ ret = stop_mounts(bus, v);
- r = sd_device_get_property_value(d, "ID_FS_USAGE", &v);
+ r = sd_device_get_devname(dev, &v);
if (r < 0)
- return log_device_error_errno(d, r, "Failed to get device property: %m");
-
- if (!streq(v, "filesystem"))
- return log_device_error_errno(d, SYNTHETIC_ERRNO(EINVAL),
- "%s does not contain a known file system.", what);
-
- if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
- r2 = stop_mounts(bus, v);
+ return r;
- r = find_mount_points(what, &list);
+ r = find_mount_points(v, &list);
if (r < 0)
return r;
- for (l = list; *l; l++) {
+ STRV_FOREACH(l, list) {
r = stop_mounts(bus, *l);
if (r < 0)
- r2 = r;
+ ret = r;
}
- return r2;
+ return ret;
+}
+
+static int umount_by_device_node(sd_bus *bus, const char *node) {
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+ const char *v;
+ int r;
+
+ assert(bus);
+ assert(node);
+
+ r = sd_device_new_from_devname(&dev, node);
+ if (r < 0)
+ return log_error_errno(r, "Failed to get device from %s: %m", node);
+
+ r = sd_device_get_property_value(dev, "ID_FS_USAGE", &v);
+ if (r < 0)
+ return log_device_error_errno(dev, r, "Failed to get \"ID_FS_USAGE\" device property: %m");
+
+ if (!streq(v, "filesystem"))
+ return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL),
+ "%s does not contain a known file system.", node);
+
+ return umount_by_device(bus, dev);
}
static int umount_loop(sd_bus *bus, const char *backing_file) {
@@ -969,7 +976,7 @@ static int umount_loop(sd_bus *bus, const char *backing_file) {
if (r < 0)
return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
- return umount_by_device(bus, loop_dev);
+ return umount_by_device_node(bus, loop_dev);
}
static int action_umount(
@@ -1014,7 +1021,7 @@ static int action_umount(
return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
if (S_ISBLK(st.st_mode))
- r = umount_by_device(bus, p);
+ r = umount_by_device_node(bus, p);
else if (S_ISREG(st.st_mode))
r = umount_loop(bus, p);
else if (S_ISDIR(st.st_mode))