]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: use readlinkat_malloc()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 10 Apr 2022 18:29:07 +0000 (03:29 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Sep 2022 20:01:52 +0000 (05:01 +0900)
And try to read it only when the file is symlink.

src/udev/udev-node.c

index 6ead839a5f5aaccabad87ecd83465013c7815c8b..0320f2d041b3057e1d38246c02c7e0b0b4416599 100644 (file)
@@ -147,7 +147,6 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir,
                 return r;
 
         FOREACH_DIRENT_ALL(de, dir, break) {
-                _cleanup_free_ char *path = NULL, *buf = NULL;
                 int tmp_prio;
 
                 if (de->d_name[0] == '.')
@@ -157,15 +156,18 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir,
                 if (streq(de->d_name, id))
                         continue;
 
-                path = path_join(stackdir, de->d_name);
-                if (!path)
-                        return -ENOMEM;
-
-                if (readlink_malloc(path, &buf) >= 0) {
+                if (de->d_type == DT_LNK) {
+                        _cleanup_free_ char *buf = NULL;
                         char *devnode;
 
                         /* New format. The devnode and priority can be obtained from symlink. */
 
+                        r = readlinkat_malloc(dirfd(dir), de->d_name, &buf);
+                        if (r < 0) {
+                                log_device_debug_errno(dev, r, "Failed to read symlink %s, ignoring: %m", de->d_name);
+                                continue;
+                        }
+
                         devnode = strchr(buf, ':');
                         if (!devnode || devnode == buf)
                                 continue;
@@ -183,7 +185,8 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir,
                         r = free_and_strdup(&target, devnode);
                         if (r < 0)
                                 return r;
-                } else {
+
+                } else if (de->d_type == DT_REG) {
                         _cleanup_(sd_device_unrefp) sd_device *tmp_dev = NULL;
                         const char *devnode;
 
@@ -205,7 +208,8 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir,
                         r = free_and_strdup(&target, devnode);
                         if (r < 0)
                                 return r;
-                }
+                } else
+                        continue;
 
                 priority = tmp_prio;
         }