From faadf97500dc8e76350a409c127f3f0fed116f9a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 11 Apr 2022 03:29:07 +0900 Subject: [PATCH] udev: use readlinkat_malloc() And try to read it only when the file is symlink. --- src/udev/udev-node.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index 6ead839a5f..0320f2d041 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -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; }