7a3408
From 7186535d4cb2661b6cc841440b46a154e6598d8e Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <7186535d4cb2661b6cc841440b46a154e6598d8e@dist-git>
7a3408
From: Andrea Bolognani <abologna@redhat.com>
7a3408
Date: Wed, 5 Aug 2015 18:18:17 +0200
7a3408
Subject: [PATCH] nodeinfo: Make sysfs_prefix usage more consistent
7a3408
7a3408
Make sure sysfs_prefix, when present, is always the first argument
7a3408
to a function; don't use a different name to refer to it; check
7a3408
whether it is NULL, and hence SYSFS_SYSTEM_PATH should be used, only
7a3408
when using it directly and not just passing it down to another
7a3408
function; always pass down the same value we've been passed when
7a3408
calling another function.
7a3408
7a3408
(cherry picked from commit 75f6f5454602d54bdb3218db2ec4931df133b8d0)
7a3408
7a3408
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1213713
7a3408
7a3408
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
---
7a3408
 src/nodeinfo.c       | 41 +++++++++++++++++++----------------------
7a3408
 src/nodeinfopriv.h   |  4 ++--
7a3408
 tests/nodeinfotest.c | 14 +++++++-------
7a3408
 3 files changed, 28 insertions(+), 31 deletions(-)
7a3408
7a3408
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
7a3408
index 5158680..60dfc8b 100644
7a3408
--- a/src/nodeinfo.c
7a3408
+++ b/src/nodeinfo.c
7a3408
@@ -415,7 +415,6 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
                  int *threads,
7a3408
                  int *offline)
7a3408
 {
7a3408
-    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SYSTEM_PATH;
7a3408
     int ret = -1;
7a3408
     int processors = 0;
7a3408
     DIR *cpudir = NULL;
7a3408
@@ -441,7 +440,7 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
         goto cleanup;
7a3408
     }
7a3408
 
7a3408
-    present_cpumap = nodeGetPresentCPUBitmap(prefix);
7a3408
+    present_cpumap = nodeGetPresentCPUBitmap(sysfs_prefix);
7a3408
 
7a3408
     /* enumerate sockets in the node */
7a3408
     CPU_ZERO(&sock_map);
7a3408
@@ -553,11 +552,12 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
     return ret;
7a3408
 }
7a3408
 
7a3408
-int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
7a3408
-                             const char *sysfs_dir,
7a3408
+int linuxNodeInfoCPUPopulate(const char *sysfs_prefix,
7a3408
+                             FILE *cpuinfo,
7a3408
                              virArch arch,
7a3408
                              virNodeInfoPtr nodeinfo)
7a3408
 {
7a3408
+    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SYSTEM_PATH;
7a3408
     char line[1024];
7a3408
     DIR *nodedir = NULL;
7a3408
     struct dirent *nodedirent = NULL;
7a3408
@@ -652,7 +652,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
7a3408
     /* OK, we've parsed clock speed out of /proc/cpuinfo. Get the
7a3408
      * core, node, socket, thread and topology information from /sys
7a3408
      */
7a3408
-    if (virAsprintf(&sysfs_nodedir, "%s/node", sysfs_dir) < 0)
7a3408
+    if (virAsprintf(&sysfs_nodedir, "%s/node", prefix) < 0)
7a3408
         goto cleanup;
7a3408
 
7a3408
     if (!(nodedir = opendir(sysfs_nodedir))) {
7a3408
@@ -667,10 +667,10 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
7a3408
         nodeinfo->nodes++;
7a3408
 
7a3408
         if (virAsprintf(&sysfs_cpudir, "%s/node/%s",
7a3408
-                        sysfs_dir, nodedirent->d_name) < 0)
7a3408
+                        prefix, nodedirent->d_name) < 0)
7a3408
             goto cleanup;
7a3408
 
7a3408
-        if ((cpus = virNodeParseNode(sysfs_dir, sysfs_cpudir, arch,
7a3408
+        if ((cpus = virNodeParseNode(sysfs_prefix, sysfs_cpudir, arch,
7a3408
                                      &socks, &cores,
7a3408
                                      &threads, &offline)) < 0)
7a3408
             goto cleanup;
7a3408
@@ -698,10 +698,10 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
7a3408
  fallback:
7a3408
     VIR_FREE(sysfs_cpudir);
7a3408
 
7a3408
-    if (virAsprintf(&sysfs_cpudir, "%s/cpu", sysfs_dir) < 0)
7a3408
+    if (virAsprintf(&sysfs_cpudir, "%s/cpu", prefix) < 0)
7a3408
         goto cleanup;
7a3408
 
7a3408
-    if ((cpus = virNodeParseNode(sysfs_dir, sysfs_cpudir, arch,
7a3408
+    if ((cpus = virNodeParseNode(sysfs_prefix, sysfs_cpudir, arch,
7a3408
                                  &socks, &cores,
7a3408
                                  &threads, &offline)) < 0)
7a3408
         goto cleanup;
7a3408
@@ -1059,7 +1059,6 @@ int nodeGetInfo(const char *sysfs_prefix ATTRIBUTE_UNUSED,
7a3408
 #ifdef __linux__
7a3408
     {
7a3408
     int ret = -1;
7a3408
-    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SYSTEM_PATH;
7a3408
     FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
7a3408
 
7a3408
     if (!cpuinfo) {
7a3408
@@ -1068,7 +1067,7 @@ int nodeGetInfo(const char *sysfs_prefix ATTRIBUTE_UNUSED,
7a3408
         return -1;
7a3408
     }
7a3408
 
7a3408
-    ret = linuxNodeInfoCPUPopulate(cpuinfo, prefix,
7a3408
+    ret = linuxNodeInfoCPUPopulate(sysfs_prefix, cpuinfo,
7a3408
                                    hostarch, nodeinfo);
7a3408
     if (ret < 0)
7a3408
         goto cleanup;
7a3408
@@ -1225,7 +1224,7 @@ nodeGetCPUCount(const char *sysfs_prefix ATTRIBUTE_UNUSED)
7a3408
     char *cpupath = NULL;
7a3408
     int ncpu = -1;
7a3408
 
7a3408
-    if (!(present_path = linuxGetCPUPresentPath(prefix)))
7a3408
+    if (!(present_path = linuxGetCPUPresentPath(sysfs_prefix)))
7a3408
         return -1;
7a3408
 
7a3408
     if (virFileExists(present_path)) {
7a3408
@@ -1273,13 +1272,12 @@ nodeGetPresentCPUBitmap(const char *sysfs_prefix)
7a3408
     char *present_path = NULL;
7a3408
     virBitmapPtr bitmap = NULL;
7a3408
 #endif
7a3408
-    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SYSTEM_PATH;
7a3408
 
7a3408
-    if ((max_present = nodeGetCPUCount(prefix)) < 0)
7a3408
+    if ((max_present = nodeGetCPUCount(sysfs_prefix)) < 0)
7a3408
         return NULL;
7a3408
 
7a3408
 #ifdef __linux__
7a3408
-    if (!(present_path = linuxGetCPUPresentPath(prefix)))
7a3408
+    if (!(present_path = linuxGetCPUPresentPath(sysfs_prefix)))
7a3408
         return NULL;
7a3408
     if (virFileExists(present_path))
7a3408
         bitmap = linuxParseCPUmap(max_present, present_path);
7a3408
@@ -1301,7 +1299,7 @@ nodeGetCPUBitmap(const char *sysfs_prefix ATTRIBUTE_UNUSED,
7a3408
     virBitmapPtr cpumap;
7a3408
     int present;
7a3408
 
7a3408
-    present = nodeGetCPUCount(prefix);
7a3408
+    present = nodeGetCPUCount(sysfs_prefix);
7a3408
     if (present < 0)
7a3408
         return NULL;
7a3408
 
7a3408
@@ -1646,7 +1644,6 @@ nodeGetCPUMap(const char *sysfs_prefix,
7a3408
               unsigned int *online,
7a3408
               unsigned int flags)
7a3408
 {
7a3408
-    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SYSTEM_PATH;
7a3408
     virBitmapPtr cpus = NULL;
7a3408
     int maxpresent;
7a3408
     int ret = -1;
7a3408
@@ -1655,9 +1652,9 @@ nodeGetCPUMap(const char *sysfs_prefix,
7a3408
     virCheckFlags(0, -1);
7a3408
 
7a3408
     if (!cpumap && !online)
7a3408
-        return nodeGetCPUCount(prefix);
7a3408
+        return nodeGetCPUCount(sysfs_prefix);
7a3408
 
7a3408
-    if (!(cpus = nodeGetCPUBitmap(prefix, &maxpresent)))
7a3408
+    if (!(cpus = nodeGetCPUBitmap(sysfs_prefix, &maxpresent)))
7a3408
         goto cleanup;
7a3408
 
7a3408
     if (cpumap && virBitmapToData(cpus, cpumap, &dummy) < 0)
7a3408
@@ -1674,7 +1671,7 @@ nodeGetCPUMap(const char *sysfs_prefix,
7a3408
 }
7a3408
 
7a3408
 static int
7a3408
-nodeCapsInitNUMAFake(const char *prefix,
7a3408
+nodeCapsInitNUMAFake(const char *sysfs_prefix,
7a3408
                      const char *cpupath ATTRIBUTE_UNUSED,
7a3408
                      virCapsPtr caps ATTRIBUTE_UNUSED)
7a3408
 {
7a3408
@@ -1685,7 +1682,7 @@ nodeCapsInitNUMAFake(const char *prefix,
7a3408
     int id, cid;
7a3408
     int onlinecpus ATTRIBUTE_UNUSED;
7a3408
 
7a3408
-    if (nodeGetInfo(prefix, &nodeinfo) < 0)
7a3408
+    if (nodeGetInfo(sysfs_prefix, &nodeinfo) < 0)
7a3408
         return -1;
7a3408
 
7a3408
     ncpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
7a3408
@@ -1957,7 +1954,7 @@ nodeCapsInitNUMA(const char *sysfs_prefix,
7a3408
         return -1;
7a3408
 
7a3408
     if (!virNumaIsAvailable()) {
7a3408
-        ret = nodeCapsInitNUMAFake(prefix, cpupath, caps);
7a3408
+        ret = nodeCapsInitNUMAFake(sysfs_prefix, cpupath, caps);
7a3408
         goto cleanup;
7a3408
     }
7a3408
 
7a3408
diff --git a/src/nodeinfopriv.h b/src/nodeinfopriv.h
7a3408
index 8bfbe1e..1aab4ad 100644
7a3408
--- a/src/nodeinfopriv.h
7a3408
+++ b/src/nodeinfopriv.h
7a3408
@@ -25,8 +25,8 @@
7a3408
 # include "nodeinfo.h"
7a3408
 
7a3408
 # ifdef __linux__
7a3408
-int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
7a3408
-                             const char *sysfs_dir,
7a3408
+int linuxNodeInfoCPUPopulate(const char *sysfs_prefix,
7a3408
+                             FILE *cpuinfo,
7a3408
                              virArch arch,
7a3408
                              virNodeInfoPtr nodeinfo);
7a3408
 
7a3408
diff --git a/tests/nodeinfotest.c b/tests/nodeinfotest.c
7a3408
index be099f0..60467bc 100644
7a3408
--- a/tests/nodeinfotest.c
7a3408
+++ b/tests/nodeinfotest.c
7a3408
@@ -24,8 +24,8 @@ main(void)
7a3408
 #else
7a3408
 
7a3408
 static int
7a3408
-linuxTestCompareFiles(const char *cpuinfofile,
7a3408
-                      char *sysfs_dir,
7a3408
+linuxTestCompareFiles(char *sysfs_prefix,
7a3408
+                      const char *cpuinfofile,
7a3408
                       virArch arch,
7a3408
                       const char *outputfile)
7a3408
 {
7a3408
@@ -42,7 +42,7 @@ linuxTestCompareFiles(const char *cpuinfofile,
7a3408
     }
7a3408
 
7a3408
     memset(&nodeinfo, 0, sizeof(nodeinfo));
7a3408
-    if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, arch, &nodeinfo) < 0) {
7a3408
+    if (linuxNodeInfoCPUPopulate(sysfs_prefix, cpuinfo, arch, &nodeinfo) < 0) {
7a3408
         if (virTestGetDebug()) {
7a3408
             virErrorPtr error = virSaveLastError();
7a3408
             if (error && error->code != VIR_ERR_OK)
7a3408
@@ -163,12 +163,12 @@ linuxTestNodeInfo(const void *opaque)
7a3408
 {
7a3408
     int result = -1;
7a3408
     char *cpuinfo = NULL;
7a3408
-    char *sysfs_dir = NULL;
7a3408
+    char *sysfs_prefix = NULL;
7a3408
     char *output = NULL;
7a3408
     struct linuxTestNodeInfoData *data = (struct linuxTestNodeInfoData *) opaque;
7a3408
     const char *archStr = virArchToString(data->arch);
7a3408
 
7a3408
-    if (virAsprintf(&sysfs_dir, "%s/nodeinfodata/linux-%s",
7a3408
+    if (virAsprintf(&sysfs_prefix, "%s/nodeinfodata/linux-%s",
7a3408
                     abs_srcdir, data->testName) < 0 ||
7a3408
         virAsprintf(&cpuinfo, "%s/nodeinfodata/linux-%s-%s.cpuinfo",
7a3408
                     abs_srcdir, archStr, data->testName) < 0 ||
7a3408
@@ -177,12 +177,12 @@ linuxTestNodeInfo(const void *opaque)
7a3408
         goto cleanup;
7a3408
     }
7a3408
 
7a3408
-    result = linuxTestCompareFiles(cpuinfo, sysfs_dir, data->arch, output);
7a3408
+    result = linuxTestCompareFiles(sysfs_prefix, cpuinfo, data->arch, output);
7a3408
 
7a3408
  cleanup:
7a3408
     VIR_FREE(cpuinfo);
7a3408
     VIR_FREE(output);
7a3408
-    VIR_FREE(sysfs_dir);
7a3408
+    VIR_FREE(sysfs_prefix);
7a3408
 
7a3408
     return result;
7a3408
 }
7a3408
-- 
7a3408
2.5.0
7a3408