summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-09-10 02:52:54 +0900
committerGitHub <noreply@github.com>2022-09-10 02:52:54 +0900
commit9a18458834ed6fe560879b2ddd6909d26b755fa3 (patch)
tree561f048167e70a46dc13d716f362a5b9ea8a3a17
parent329984037b5c78ca9b5d72171efefcdfe064757e (diff)
parent86f9b69a6a394bd752be01d997d47849de67006f (diff)
downloadsystemd-9a18458834ed6fe560879b2ddd6909d26b755fa3.tar.gz
Merge pull request #24618 from yuwata/udev-split-synthesizing
udev: shorten synthesize_change() a bit
-rw-r--r--src/shared/blockdev-util.c23
-rw-r--r--src/shared/blockdev-util.h2
-rw-r--r--src/udev/udevd.c57
3 files changed, 33 insertions, 49 deletions
diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c
index 7ffb619ac6..e5abb524d5 100644
--- a/src/shared/blockdev-util.c
+++ b/src/shared/blockdev-util.c
@@ -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"
@@ -509,7 +510,7 @@ int block_device_resize_partition(
return RET_NERRNO(ioctl(fd, BLKPG, &ba));
}
-static int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
+int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
const char *s;
int r;
@@ -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;
+}
diff --git a/src/shared/blockdev-util.h b/src/shared/blockdev-util.h
index fb041bf619..550b2786d2 100644
--- a/src/shared/blockdev-util.h
+++ b/src/shared/blockdev-util.h
@@ -36,5 +36,7 @@ int path_get_whole_disk(const char *path, bool backing, dev_t *ret);
int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size);
int block_device_remove_partition(int fd, const char *name, int nr);
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);
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index b179dfac37..c2a4a8a7bd 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -28,6 +28,7 @@
#include "sd-event.h"
#include "alloc-util.h"
+#include "blockdev-util.h"
#include "cgroup-setup.h"
#include "cgroup-util.h"
#include "cpu-set-util.h"
@@ -1399,69 +1400,29 @@ 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, has_partitions = 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;
- }
-
- /* search for partitions */
- r = sd_device_enumerator_new(&e);
- if (r < 0)
- return r;
- r = sd_device_enumerator_allow_uninitialized(e);
+ r = blockdev_reread_partition_table(dev);
if (r < 0)
- return r;
+ log_device_debug_errno(dev, r, "Failed to re-read partition table, ignoring: %m");
+ part_table_read = r >= 0;
- r = sd_device_enumerator_add_match_parent(e, dev);
- if (r < 0)
- return r;
-
- r = sd_device_enumerator_add_match_subsystem(e, "block", true);
+ /* search for partitions */
+ r = partition_enumerator_new(dev, &e);
if (r < 0)
return r;
- FOREACH_DEVICE(e, d) {
- const char *t;
-
- if (sd_device_get_devtype(d, &t) < 0 || !streq(t, "partition"))
- continue;
-
- has_partitions = true;
- break;
- }
-
/* We have partitions and re-read the table, the kernel already sent out a "change"
* event for the disk, and "remove/add" for all partitions. */
- if (part_table_read && has_partitions)
+ if (part_table_read && sd_device_enumerator_get_device_first(e))
return 0;
/* We have partitions but re-reading the partition table did not work, synthesize
* "change" for the disk and all partitions. */
(void) synthesize_change_one(dev, dev);
-
- FOREACH_DEVICE(e, d) {
- const char *t;
-
- if (sd_device_get_devtype(d, &t) < 0 || !streq(t, "partition"))
- continue;
-
+ FOREACH_DEVICE(e, d)
(void) synthesize_change_one(dev, d);
- }
-
} else
(void) synthesize_change_one(dev, dev);