From 7b7959fba52ba4bb6b5f7001971917760df40fee Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Fri, 25 Mar 2022 02:55:25 +0900
Subject: [PATCH] udev: split worker_lock_block_device() into two
 
This also makes return value initialized when these function return 0 to
follow our coding style.
 
Just a preparation for later commits.
 
Reference:https://github.com/systemd/systemd/commit/7b7959fba52ba4bb6b5f7001971917760df40fee
Conflict:NA
 
---
 src/udev/udevd.c | 54 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 41 insertions(+), 13 deletions(-)
 
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index 973727375b67..0b620cb7dcac 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -376,35 +376,29 @@ static int worker_send_result(Manager *manager, EventResult result) {
         return loop_write(manager->worker_watch[WRITE_END], &result, sizeof(result), false);
 }
 
-static int worker_lock_block_device(sd_device *dev, int *ret_fd) {
-        _cleanup_close_ int fd = -1;
+static int device_get_block_device(sd_device *dev, const char **ret) {
         const char *val;
         int r;
 
         assert(dev);
-        assert(ret_fd);
-
-        /* Take a shared lock on the device node; this establishes a concept of device "ownership" to
-         * serialize device access. External processes holding an exclusive lock will cause udev to skip the
-         * event handling; in the case udev acquired the lock, the external process can block until udev has
-         * finished its event handling. */
+        assert(ret);
 
         if (device_for_action(dev, SD_DEVICE_REMOVE))
-                return 0;
+                goto irrelevant;
 
         r = sd_device_get_subsystem(dev, &val);
         if (r < 0)
                 return log_device_debug_errno(dev, r, "Failed to get subsystem: %m");
 
         if (!streq(val, "block"))
-                return 0;
+                goto irrelevant;
 
         r = sd_device_get_sysname(dev, &val);
         if (r < 0)
                 return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
 
         if (STARTSWITH_SET(val, "dm-", "md", "drbd"))
-                return 0;
+                goto irrelevant;
 
         r = sd_device_get_devtype(dev, &val);
         if (r < 0 && r != -ENOENT)
@@ -417,16 +411,46 @@ static int worker_lock_block_device(sd_device *dev, int *ret_fd) {
 
         r = sd_device_get_devname(dev, &val);
         if (r == -ENOENT)
-                return 0;
+                goto irrelevant;
         if (r < 0)
                 return log_device_debug_errno(dev, r, "Failed to get devname: %m");
 
+        *ret = val;
+        return 1;
+
+irrelevant:
+        *ret = NULL;
+        return 0;
+}
+
+static int worker_lock_block_device(sd_device *dev, int *ret_fd) {
+        _cleanup_close_ int fd = -1;
+        const char *val;
+        int r;
+
+        assert(dev);
+        assert(ret_fd);
+
+        /* Take a shared lock on the device node; this establishes a concept of device "ownership" to
+         * serialize device access. External processes holding an exclusive lock will cause udev to skip the
+         * event handling; in the case udev acquired the lock, the external process can block until udev has
+         * finished its event handling. */
+
+        r = device_get_block_device(dev, &val);
+        if (r < 0)
+                return r;
+        if (r == 0)
+                goto nolock;
+
         fd = open(val, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
         if (fd < 0) {
                 bool ignore = ERRNO_IS_DEVICE_ABSENT(errno);
 
                 log_device_debug_errno(dev, errno, "Failed to open '%s'%s: %m", val, ignore ? ", ignoring" : "");
-                return ignore ? 0 : -errno;
+                if (!ignore)
+                        return -errno;
+
+                goto nolock;
         }
 
         if (flock(fd, LOCK_SH|LOCK_NB) < 0)
@@ -434,6 +458,10 @@ static int worker_lock_block_device(sd_device *dev, int *ret_fd) {
 
         *ret_fd = TAKE_FD(fd);
         return 1;
+
+nolock:
+        *ret_fd = -1;
+        return 0;
 }
 
 static int worker_mark_block_device_read_only(sd_device *dev) {
 