]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: use faccessat()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 18 Sep 2022 22:23:30 +0000 (07:23 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 19 Sep 2022 09:04:13 +0000 (18:04 +0900)
No functional changes, just refactoring.

src/libsystemd/sd-device/sd-device.c

index f5f36694028c651028ae1a5ef4e9d89dc818ba02..96a8ebf6b8e95d4939ca9e7f1b62cdb34f29fd63 100644 (file)
@@ -1877,28 +1877,29 @@ static int device_sysattrs_read_all_internal(sd_device *device, const char *subd
                 return r;
 
         if (subdir) {
-                _cleanup_free_ char *p = NULL;
-
-                p = path_join(syspath, subdir, "uevent");
-                if (!p)
-                        return -ENOMEM;
-
-                if (access(p, F_OK) >= 0)
-                        /* this is a child device, skipping */
-                        return 0;
-                if (errno != ENOENT) {
-                        log_device_debug_errno(device, errno, "sd-device: Failed to stat %s, ignoring subdir: %m", p);
-                        return 0;
-                }
-
                 path_dir = path_join(syspath, subdir);
                 if (!path_dir)
                         return -ENOMEM;
         }
 
         dir = opendir(path_dir ?: syspath);
-        if (!dir)
+        if (!dir) {
+                if (errno == ENOENT && subdir)
+                        return 0; /* Maybe, this is a child device, and is already removed. */
+
                 return -errno;
+        }
+
+        if (subdir) {
+                if (faccessat(dirfd(dir), "uevent", F_OK, 0) >= 0)
+                        return 0; /* this is a child device, skipping */
+                if (errno != ENOENT) {
+                        log_device_debug_errno(device, errno,
+                                               "sd-device: Failed to access %s/uevent, ignoring sub-directory %s: %m",
+                                               subdir, subdir);
+                        return 0;
+                }
+        }
 
         FOREACH_DIRENT_ALL(de, dir, return -errno) {
                 _cleanup_free_ char *p = NULL;