]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: split worker_lock_block_device() into two
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 24 Mar 2022 17:55:25 +0000 (02:55 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 25 Mar 2022 01:28:07 +0000 (10:28 +0900)
This also makes return value initialized when these function return 0 to
follow our coding style.

Just a preparation for later commits.

src/udev/udevd.c

index 973727375b67fc692127e700024a692bc5178db9..0b620cb7dcac8dea23b8638b40d9d1688ac14dcc 100644 (file)
@@ -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) {