Index: /trunk/src/VBox/Main/src-server/linux/PerformanceLinux.cpp
===================================================================
--- /trunk/src/VBox/Main/src-server/linux/PerformanceLinux.cpp	(revision 48014)
+++ /trunk/src/VBox/Main/src-server/linux/PerformanceLinux.cpp	(revision 48015)
@@ -32,4 +32,5 @@
 #include <iprt/system.h>
 #include <iprt/mp.h>
+#include <iprt/linux/sysfs.h>
 
 #include <map>
@@ -62,5 +63,5 @@
     virtual int _getRawHostCpuLoad();
     int getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed);
-    char *getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits);
+    void getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits);
     void addVolumeDependencies(const char *pcszVolume, DiskList& listDisks);
     void addRaidDisks(const char *pcszDevice, DiskList& listDisks);
@@ -240,26 +241,23 @@
 }
 
-int CollectorLinux::getHostDiskSize(const char *name, uint64_t *size)
-{
+int CollectorLinux::getHostDiskSize(const char *pszFile, uint64_t *size)
+{
+    char *pszPath = NULL;
+
+    RTStrAPrintf(&pszPath, "/sys/block/%s/size", pszFile);
+    Assert(pszPath);
+
     int rc = VINF_SUCCESS;
-    char *pszName = NULL;
-    long long unsigned int u64Size;
-
-    RTStrAPrintf(&pszName, "/sys/block/%s/size", name);
-    Assert(pszName);
-    FILE *f = fopen(pszName, "r");
-    RTMemFree(pszName);
-
-    if (f)
-    {
-        if (fscanf(f, "%llu", &u64Size) == 1)
-            *size = u64Size * 512;
+    if (!RTLinuxSysFsExists(pszPath))
+        rc = VERR_FILE_NOT_FOUND;
+    else
+    {
+        int64_t cSize = RTLinuxSysFsReadIntFile(0, pszPath);
+        if (cSize < 0)
+            rc = VERR_ACCESS_DENIED;
         else
-            rc = VERR_FILE_IO_ERROR;
-        fclose(f);
-    }
-    else
-        rc = VERR_ACCESS_DENIED;
-
+            *size = cSize * 512;
+    }
+    RTStrFree(pszPath);
     return rc;
 }
@@ -293,7 +291,6 @@
 
     RTStrAPrintf(&pszName, "/proc/%d/stat", process);
-    //printf("Opening %s...\n", pszName);
     FILE *f = fopen(pszName, "r");
-    RTMemFree(pszName);
+    RTStrFree(pszName);
 
     if (f)
@@ -320,36 +317,29 @@
 }
 
-int CollectorLinux::getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx)
+int CollectorLinux::getRawHostNetworkLoad(const char *pszFile, uint64_t *rx, uint64_t *tx)
 {
     int rc = VINF_SUCCESS;
     char szIfName[/*IFNAMSIZ*/ 16 + 36];
-    long long unsigned int u64Rx, u64Tx;
-
-    RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/rx_bytes", name);
-    FILE *f = fopen(szIfName, "r");
-    if (f)
-    {
-        if (fscanf(f, "%llu", &u64Rx) == 1)
-            *rx = u64Rx;
-        else
-            rc = VERR_FILE_IO_ERROR;
-        fclose(f);
-        RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/tx_bytes", name);
-        f = fopen(szIfName, "r");
-        if (f)
-        {
-            if (fscanf(f, "%llu", &u64Tx) == 1)
-                *tx = u64Tx;
-            else
-                rc = VERR_FILE_IO_ERROR;
-            fclose(f);
-        }
-        else
-            rc = VERR_ACCESS_DENIED;
-    }
-    else
-        rc = VERR_ACCESS_DENIED;
-
-    return rc;
+
+    RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/rx_bytes", pszFile);
+    if (!RTLinuxSysFsExists(szIfName))
+        return VERR_FILE_NOT_FOUND;
+
+    int64_t cSize = RTLinuxSysFsReadIntFile(0, szIfName);
+    if (cSize < 0)
+        return VERR_ACCESS_DENIED;
+
+    *rx = cSize;
+
+    RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/tx_bytes", pszFile);
+    if (!RTLinuxSysFsExists(szIfName))
+        return VERR_FILE_NOT_FOUND;
+
+    cSize = RTLinuxSysFsReadIntFile(0, szIfName);
+    if (cSize < 0)
+        return VERR_ACCESS_DENIED;
+
+    *tx = cSize;
+    return VINF_SUCCESS;
 }
 
@@ -450,5 +440,15 @@
 }
 
-char *CollectorLinux::getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits)
+/**
+ * Use the partition name to get the name of the disk. Any path component is stripped.
+ * if fTrimDigits is true, trailing digits are stripped as well, for example '/dev/sda5'
+ * is converted to 'sda'.
+ *
+ * @param   pszDiskName     Where to store the name of the disk.
+ * @param   cbDiskName      The size of the buffer pszDiskName points to.
+ * @param   pszDevName      The device name used to get the disk name.
+ * @param   fTrimDigits     Trim trailing digits (e.g. /dev/sda5)
+ */
+void CollectorLinux::getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits)
 {
     unsigned cbName = 0;
@@ -464,5 +464,4 @@
     }
     RTStrCopy(pszDiskName, RT_MIN(cbName + 1, cbDiskName), pszEnd + 1);
-    return pszDiskName;
 }
 
@@ -533,5 +532,5 @@
 
         while (fgets(szBuf, sizeof(szBuf), fp))
-            if (strncmp(szBuf, "dm-", 3))
+            if (strncmp(szBuf, RT_STR_TUPLE("dm-")))
                 listDisks.push_back(RTCString(trimTrailingDigits(szBuf)));
             else
@@ -559,21 +558,19 @@
                 char szDevName[128];
                 char szFsName[1024];
-                /* Try to resolve symbolic link if necessary */
-                ssize_t cbFsName = readlink(mntent->mnt_fsname, szFsName, sizeof(szFsName) - 1);
-                if (cbFsName != -1)
-                    szFsName[cbFsName] = '\0';
-                else
-                    strcpy(szFsName, mntent->mnt_fsname);
-                if (!strncmp(szFsName, "/dev/mapper", 11))
+                /* Try to resolve symbolic link if necessary. Yes, we access the file system here! */
+                int rc = RTPathReal(mntent->mnt_fsname, szFsName, sizeof(szFsName));
+                if (RT_FAILURE(rc))
+                    continue; /* something got wrong, just ignore this path */
+                if (!strncmp(szFsName, RT_STR_TUPLE("/dev/mapper")))
                 {
                     /* LVM */
-                    getDiskName(szDevName, sizeof(szDevName), szFsName, false);
+                    getDiskName(szDevName, sizeof(szDevName), szFsName, false /*=fTrimDigits*/);
                     addVolumeDependencies(szDevName, listUsage);
                     listLoad = listUsage;
                 }
-                else if (!strncmp(szFsName, "/dev/md", 7))
+                else if (!strncmp(szFsName, RT_STR_TUPLE("/dev/md")))
                 {
                     /* Software RAID */
-                    getDiskName(szDevName, sizeof(szDevName), szFsName, false);
+                    getDiskName(szDevName, sizeof(szDevName), szFsName, false /*=fTrimDigits*/);
                     listUsage.push_back(RTCString(szDevName));
                     addRaidDisks(szDevName, listLoad);
@@ -581,6 +578,6 @@
                 else
                 {
-                    /* Plain disk partition */
-                    getDiskName(szDevName, sizeof(szDevName), mntent->mnt_fsname, true);
+                    /* Plain disk partition. Trim the trailing digits to get the drive name */
+                    getDiskName(szDevName, sizeof(szDevName), szFsName, true /*=fTrimDigits*/);
                     listUsage.push_back(RTCString(szDevName));
                     listLoad.push_back(RTCString(szDevName));
@@ -588,5 +585,6 @@
                 if (listUsage.empty() || listLoad.empty())
                 {
-                    LogRel(("Failed to retrive disk info: getDiskName(%s) --> %s\n", mntent->mnt_fsname, szDevName));
+                    LogRel(("Failed to retrive disk info: getDiskName(%s) --> %s\n",
+                           mntent->mnt_fsname, szDevName));
                 }
                 break;
