]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: split out blockdev_reread_partition_table() 24618/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 9 Sep 2022 15:33:43 +0000 (00:33 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 9 Sep 2022 16:19:41 +0000 (01:19 +0900)
No functional changes, just refactoring.

src/shared/blockdev-util.c
src/shared/blockdev-util.h
src/udev/udevd.c

index 87c19d708695bec0341113e68a707f5b189c1506..e5abb524d5ea6c4bf3c77ee265f2fad9a07dd375 100644 (file)
@@ -3,6 +3,7 @@
 #include <linux/blkpg.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
+#include <sys/mount.h>
 #include <unistd.h>
 
 #include "sd-device.h"
@@ -649,3 +650,23 @@ int block_device_has_partitions(sd_device *dev) {
 
         return !!sd_device_enumerator_get_device_first(e);
 }
+
+int blockdev_reread_partition_table(sd_device *dev) {
+        _cleanup_close_ int fd = -1;
+
+        assert(dev);
+
+        /* Try to re-read the partition table. This only succeeds if none of the devices is busy. */
+
+        fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
+        if (fd < 0)
+                return fd;
+
+        if (flock(fd, LOCK_EX|LOCK_NB) < 0)
+                return -errno;
+
+        if (ioctl(fd, BLKRRPART, 0) < 0)
+                return -errno;
+
+        return 0;
+}
index 8df76ff30eedfc0dc55b689f45c9f8ad298f77d4..550b2786d2a32e09e81354572279c9572073f131 100644 (file)
@@ -39,3 +39,4 @@ int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size)
 int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret);
 int block_device_remove_all_partitions(sd_device *dev, int fd);
 int block_device_has_partitions(sd_device *dev);
+int blockdev_reread_partition_table(sd_device *dev);
index c034e6a3cfe7e03aca6c9030f913d30a0c8d5cb3..c2a4a8a7bd6415fa60dfd345465c1008fbcde9c9 100644 (file)
@@ -1400,23 +1400,13 @@ static int synthesize_change(sd_device *dev) {
             streq_ptr(devtype, "disk") &&
             !startswith(sysname, "dm-")) {
                 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
-                bool part_table_read = false;
+                bool part_table_read;
                 sd_device *d;
-                int fd;
-
-                /* Try to re-read the partition table. This only succeeds if none of the devices is
-                 * busy. The kernel returns 0 if no partition table is found, and we will not get an
-                 * event for the disk. */
-                fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-                if (fd >= 0) {
-                        r = flock(fd, LOCK_EX|LOCK_NB);
-                        if (r >= 0)
-                                r = ioctl(fd, BLKRRPART, 0);
-
-                        close(fd);
-                        if (r >= 0)
-                                part_table_read = true;
-                }
+
+                r = blockdev_reread_partition_table(dev);
+                if (r < 0)
+                        log_device_debug_errno(dev, r, "Failed to re-read partition table, ignoring: %m");
+                part_table_read = r >= 0;
 
                 /* search for partitions */
                 r = partition_enumerator_new(dev, &e);