[libvirt] [PATCH 1/1] Verify that data read from xenstore is relevant

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Make sure that the domains found in xenstore are active when reporting
the number of domains and when reporting which domains that are
active.
---
 src/xs_internal.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/src/xs_internal.c b/src/xs_internal.c
index 1f54b1f..5b0ce1e 100644
--- a/src/xs_internal.c
+++ b/src/xs_internal.c
@@ -542,9 +542,10 @@ xenStoreDomainGetMaxMemory(virDomainPtr domain)
 int
 xenStoreNumOfDomains(virConnectPtr conn)
 {
-    unsigned int num;
+    unsigned int num, realnum, len;
     char **idlist;
-    int ret = -1;
+    char *ldpath = NULL, *vmpath = NULL, *vmvalue = NULL;
+    int i, ret = -1;
     xenUnifiedPrivatePtr priv;
 
     if (conn == NULL) {
@@ -558,9 +559,29 @@ xenStoreNumOfDomains(virConnectPtr conn)
         return(-1);
     }
     idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num);
+
+    /* Check which of the found domains that are running */
+    realnum = num;
+    for (i = 0; i < num; i++) {
+        if (asprintf(&ldpath, "/local/domain/%s/vm", idlist[i]) < 0) {
+            virReportOOMError(NULL);
+            return -1;
+        }
+        vmpath = xs_read (priv->xshandle, 0, ldpath, &len);
+        if (len > 0)
+            vmvalue = xs_read (priv->xshandle, 0, vmpath, &len);
+
+        if (vmvalue == NULL)
+            realnum--;
+
+        VIR_FREE(ldpath);
+        VIR_FREE(vmpath);
+        VIR_FREE(vmvalue);
+    }
+
     if (idlist) {
         free(idlist);
-        ret = num;
+        ret = realnum;
     }
     return(ret);
 }
@@ -580,7 +601,8 @@ static int
 xenStoreDoListDomains(xenUnifiedPrivatePtr priv, int *ids, int maxids)
 {
     char **idlist = NULL, *endptr;
-    unsigned int num, i;
+    char *vmpath = NULL, *vmvalue = NULL, *ldpath = NULL;
+    unsigned int num, i, len;
     int ret = -1;
     long id;
 
@@ -592,14 +614,31 @@ xenStoreDoListDomains(xenUnifiedPrivatePtr priv, int *ids, int maxids)
         goto out;
 
     for (ret = 0, i = 0; (i < num) && (ret < maxids); i++) {
-        id = strtol(idlist[i], &endptr, 10);
-        if ((endptr == idlist[i]) || (*endptr != 0))
+        if (asprintf(&ldpath, "/local/domain/%s/vm", idlist[i]) < 0) {
+            virReportOOMError(NULL);
             goto out;
-        ids[ret++] = (int) id;
+        }
+        vmpath = xs_read (priv->xshandle, 0, ldpath, &len);
+        if (len > 0)
+            vmvalue = xs_read (priv->xshandle, 0, vmpath, &len);
+
+        if (vmvalue != NULL) {
+            id = strtol(idlist[i], &endptr, 10);
+            if ((endptr == idlist[i]) || (*endptr != 0))
+                goto out;
+            ids[ret++] = (int) id;
+        }
+
+        VIR_FREE (ldpath);
+        VIR_FREE (vmpath);
+        VIR_FREE (vmvalue);
     }
 
 out:
     VIR_FREE (idlist);
+    VIR_FREE (ldpath);
+    VIR_FREE (vmpath);
+    VIR_FREE (vmvalue);
     return ret;
 }
 
-- 
1.6.2

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

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]