[libvirt] [PATCH v2 4.5/4 variant 2] util: Use stat() on files under /sys/fs/resctrl/info

Martin Kletzander posted 1 patch 6 years, 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/51b20206e8bea823e0b206d3458c5d40dad1025b.1517244250.git.mkletzan@redhat.com
src/util/virresctrl.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 48 insertions(+), 1 deletion(-)
[libvirt] [PATCH v2 4.5/4 variant 2] util: Use stat() on files under /sys/fs/resctrl/info
Posted by Martin Kletzander 6 years, 1 month ago
We are skipping non-directories under /sys/fs/resctrl/(info/) since those are not
interesting for us.  However in tests it can sometimes happen that ent->d_type
is 0 instead of 4 (DT_DIR) for directories.

I've seen it fail on two machines.  Different machines, different systems, I
cannot reproduce it even using the same setup.  So one of the ways how to work
around this is call stat() on it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
---
 src/util/virresctrl.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index 684cb22fd8a2..2c1d07ff3a25 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -409,9 +409,30 @@ virResctrlGetInfo(virResctrlInfoPtr resctrl)
     }
 
     while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH "/info")) > 0) {
+        struct stat st;
+        char *path = NULL;
+
         VIR_DEBUG("Parsing info type '%s'", ent->d_name);
 
-        if (ent->d_type != DT_DIR)
+        if (virAsprintf(&path, SYSFS_RESCTRL_PATH "/info/%s", ent->d_name) < 0)
+            continue;
+
+        if (stat(path, &st) < 0) {
+            virReportSystemError(errno, _("Cannot stat '%s'"), path);
+            VIR_FREE(path);
+            continue;
+        }
+        VIR_FREE(path);
+
+        /*
+         * So this doesn't work on some machines when we're mocking syscalls in tests
+         *
+         * if (ent->d_type != DT_DIR)
+         *     continue;
+         *
+         * But the following does, for some reason.
+         */
+        if ((st.st_mode & S_IFMT) != S_IFDIR)
             continue;
 
         if (ent->d_name[0] != 'L')
@@ -1169,6 +1190,32 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
         goto error;
 
     while ((rv = virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH)) > 0) {
+        struct stat st;
+        char *path = NULL;
+
+        VIR_DEBUG("Parsing info type '%s'", ent->d_name);
+
+        if (virAsprintf(&path, SYSFS_RESCTRL_PATH "/%s", ent->d_name) < 0)
+            continue;
+
+        if (stat(path, &st) < 0) {
+            virReportSystemError(errno, _("Cannot stat '%s'"), path);
+            VIR_FREE(path);
+            continue;
+        }
+        VIR_FREE(path);
+
+        /*
+         * So this doesn't work on some machines when we're mocking syscalls in tests
+         *
+         * if (ent->d_type != DT_DIR)
+         *     continue;
+         *
+         * But the following does, for some reason.
+         */
+        if ((st.st_mode & S_IFMT) != S_IFDIR)
+            continue;
+
         if (ent->d_type != DT_DIR)
             continue;
 
-- 
2.16.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list