[PATCH 1/6] parallels: return a new list of domains from parallelsLoadDomains

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

 



Make parallelsLoadDomains  more useful, so that it will not touch list
in global _parallelsConn structure, but allocate and return a new one
instead. So it can be used later, when you need the up-to-date list
of VMs instead of the one stored in _parallelsConn structure.

Also rename parallelsLoadDomains to parallelsGetDomains and add
new function parallelsGetDomain, which is used in
parallelsDomainDefineXML.

Signed-off-by: Dmitry Guryanov <dguryanov@xxxxxxxxxxxxx>
---
 src/parallels/parallels_driver.c |  134 +++++++++++++++++++++++++-------------
 src/parallels/parallels_utils.h  |    2 +-
 2 files changed, 90 insertions(+), 46 deletions(-)

diff --git a/src/parallels/parallels_driver.c b/src/parallels/parallels_driver.c
index c14f616..794d61d 100644
--- a/src/parallels/parallels_driver.c
+++ b/src/parallels/parallels_driver.c
@@ -401,11 +401,8 @@ parallelsAddVNCInfo(virDomainDefPtr def, virJSONValuePtr jobj_root)
     return ret;
 }
 
-/*
- * Must be called with privconn->lock held
- */
 static virDomainObjPtr
-parallelsLoadDomain(parallelsConnPtr privconn, virJSONValuePtr jobj)
+parallelsParseDomainInfo(parallelsConnPtr privconn, virJSONValuePtr jobj)
 {
     virDomainObjPtr dom = NULL;
     virDomainDefPtr def = NULL;
@@ -528,11 +525,12 @@ parallelsLoadDomain(parallelsConnPtr privconn, virJSONValuePtr jobj)
     if (parallelsAddVNCInfo(def, jobj) < 0)
         goto cleanup;
 
-    if (!(dom = virDomainAssignDef(privconn->caps,
-                                   &privconn->domains, def, false)))
+    if (!(dom = virDomainObjNew(privconn->caps)))
         goto cleanup;
     /* dom is locked here */
 
+    dom->def = def;
+
     dom->privateDataFreeFunc = parallelsDomObjFreePrivate;
     dom->privateData = pdom;
     dom->persistent = 1;
@@ -562,22 +560,21 @@ parallelsLoadDomain(parallelsConnPtr privconn, virJSONValuePtr jobj)
 }
 
 /*
- * Must be called with privconn->lock held
- *
  * if domain_name is NULL - load information about all
  * registered domains.
  */
-static int
-parallelsLoadDomains(parallelsConnPtr privconn, const char *domain_name)
+static virDomainObjListPtr
+parallelsGetDomains(parallelsConnPtr privconn)
 {
     int count, i;
     virJSONValuePtr jobj;
     virJSONValuePtr jobj2;
     virDomainObjPtr dom = NULL;
-    int ret = -1;
+    virDomainObjListPtr domains = NULL;
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
 
     jobj = parallelsParseOutput(PRLCTL, "list", "-j", "-a", "-i", "-H",
-                                "--vmtype", "vm", domain_name, NULL);
+                                "--vmtype", "vm", NULL);
     if (!jobj) {
         parallelsParseError();
         goto cleanup;
@@ -589,6 +586,11 @@ parallelsLoadDomains(parallelsConnPtr privconn, const char *domain_name)
         goto cleanup;
     }
 
+    if (VIR_ALLOC(domains) < 0)
+        goto error;
+    if (virDomainObjListInit(domains) < 0)
+        goto error;
+
     for (i = 0; i < count; i++) {
         jobj2 = virJSONValueArrayGet(jobj, i);
         if (!jobj2) {
@@ -596,16 +598,59 @@ parallelsLoadDomains(parallelsConnPtr privconn, const char *domain_name)
             goto cleanup;
         }
 
-        dom = parallelsLoadDomain(privconn, jobj2);
+        dom = parallelsParseDomainInfo(privconn, jobj2);
         if (!dom)
             goto cleanup;
+
+        virUUIDFormat(dom->def->uuid, uuidstr);
+        if (virHashAddEntry(domains->objs, uuidstr, dom) < 0) {
+            VIR_FREE(dom);
+            goto error;
+        }
     }
 
-    ret = 0;
+  cleanup:
+    virJSONValueFree(jobj);
+    return domains;
+  error:
+    virJSONValueFree(jobj);
+    virDomainObjListDeinit(domains);
+    VIR_FREE(domains);
+    return NULL;
+}
+
+static virDomainObjPtr
+parallelsGetDomain(parallelsConnPtr privconn, const char *domain_name)
+{
+    int count;
+    virJSONValuePtr jobj;
+    virJSONValuePtr jobj2;
+    virDomainObjPtr dom = NULL;
+
+    jobj = parallelsParseOutput(PRLCTL, "list", "-j", "-a", "-i", "-H",
+                                "--vmtype", "vm", domain_name, NULL);
+    if (!jobj) {
+        parallelsParseError();
+        goto cleanup;
+    }
+
+    count = virJSONValueArraySize(jobj);
+    if (count != 1) {
+        parallelsParseError();
+        goto cleanup;
+    }
+
+    jobj2 = virJSONValueArrayGet(jobj, 0);
+    if (!jobj2) {
+        parallelsParseError();
+        goto cleanup;
+    }
+
+    dom = parallelsParseDomainInfo(privconn, jobj2);
 
   cleanup:
     virJSONValueFree(jobj);
-    return ret;
+    return dom;
 }
 
 static int
@@ -626,18 +671,14 @@ parallelsOpenDefault(virConnectPtr conn)
     if (!(privconn->caps = parallelsBuildCapabilities()))
         goto error;
 
-    if (virDomainObjListInit(&privconn->domains) < 0)
+    if (!(privconn->domains = parallelsGetDomains(privconn)))
         goto error;
 
     conn->privateData = privconn;
 
-    if (parallelsLoadDomains(privconn, NULL))
-        goto error;
-
     return VIR_DRV_OPEN_SUCCESS;
 
   error:
-    virDomainObjListDeinit(&privconn->domains);
     virCapabilitiesFree(privconn->caps);
     virStoragePoolObjListFree(&privconn->pools);
     VIR_FREE(privconn);
@@ -684,7 +725,8 @@ parallelsClose(virConnectPtr conn)
 
     parallelsDriverLock(privconn);
     virCapabilitiesFree(privconn->caps);
-    virDomainObjListDeinit(&privconn->domains);
+    virDomainObjListDeinit(privconn->domains);
+    VIR_FREE(privconn->domains);
     conn->privateData = NULL;
 
     parallelsDriverUnlock(privconn);
@@ -747,7 +789,7 @@ parallelsListDomains(virConnectPtr conn, int *ids, int maxids)
     int n;
 
     parallelsDriverLock(privconn);
-    n = virDomainObjListGetActiveIDs(&privconn->domains, ids, maxids);
+    n = virDomainObjListGetActiveIDs(privconn->domains, ids, maxids);
     parallelsDriverUnlock(privconn);
 
     return n;
@@ -760,7 +802,7 @@ parallelsNumOfDomains(virConnectPtr conn)
     int count;
 
     parallelsDriverLock(privconn);
-    count = virDomainObjListNumOfDomains(&privconn->domains, 1);
+    count = virDomainObjListNumOfDomains(privconn->domains, 1);
     parallelsDriverUnlock(privconn);
 
     return count;
@@ -774,7 +816,7 @@ parallelsListDefinedDomains(virConnectPtr conn, char **const names, int maxnames
 
     parallelsDriverLock(privconn);
     memset(names, 0, sizeof(*names) * maxnames);
-    n = virDomainObjListGetInactiveNames(&privconn->domains, names,
+    n = virDomainObjListGetInactiveNames(privconn->domains, names,
                                          maxnames);
     parallelsDriverUnlock(privconn);
 
@@ -788,7 +830,7 @@ parallelsNumOfDefinedDomains(virConnectPtr conn)
     int count;
 
     parallelsDriverLock(privconn);
-    count = virDomainObjListNumOfDomains(&privconn->domains, 0);
+    count = virDomainObjListNumOfDomains(privconn->domains, 0);
     parallelsDriverUnlock(privconn);
 
     return count;
@@ -804,7 +846,7 @@ parallelsListAllDomains(virConnectPtr conn,
 
     virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1);
     parallelsDriverLock(privconn);
-    ret = virDomainList(conn, privconn->domains.objs, domains, flags);
+    ret = virDomainList(conn, privconn->domains->objs, domains, flags);
     parallelsDriverUnlock(privconn);
 
     return ret;
@@ -818,7 +860,7 @@ parallelsLookupDomainByID(virConnectPtr conn, int id)
     virDomainObjPtr dom;
 
     parallelsDriverLock(privconn);
-    dom = virDomainFindByID(&privconn->domains, id);
+    dom = virDomainFindByID(privconn->domains, id);
     parallelsDriverUnlock(privconn);
 
     if (dom == NULL) {
@@ -844,7 +886,7 @@ parallelsLookupDomainByUUID(virConnectPtr conn, const unsigned char *uuid)
     virDomainObjPtr dom;
 
     parallelsDriverLock(privconn);
-    dom = virDomainFindByUUID(&privconn->domains, uuid);
+    dom = virDomainFindByUUID(privconn->domains, uuid);
     parallelsDriverUnlock(privconn);
 
     if (dom == NULL) {
@@ -873,7 +915,7 @@ parallelsLookupDomainByName(virConnectPtr conn, const char *name)
     virDomainObjPtr dom;
 
     parallelsDriverLock(privconn);
-    dom = virDomainFindByName(&privconn->domains, name);
+    dom = virDomainFindByName(privconn->domains, name);
     parallelsDriverUnlock(privconn);
 
     if (dom == NULL) {
@@ -900,7 +942,7 @@ parallelsGetDomainInfo(virDomainPtr domain, virDomainInfoPtr info)
     int ret = -1;
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     parallelsDriverUnlock(privconn);
 
     if (privdom == NULL) {
@@ -930,7 +972,7 @@ parallelsGetOSType(virDomainPtr domain)
     char *ret = NULL;
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     if (privdom == NULL) {
         parallelsDomNotFoundError(domain);
         goto cleanup;
@@ -954,7 +996,7 @@ parallelsDomainIsPersistent(virDomainPtr domain)
     int ret = -1;
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     if (privdom == NULL) {
         parallelsDomNotFoundError(domain);
         goto cleanup;
@@ -979,7 +1021,7 @@ parallelsDomainGetState(virDomainPtr domain,
     virCheckFlags(0, -1);
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     parallelsDriverUnlock(privconn);
 
     if (privdom == NULL) {
@@ -1007,7 +1049,7 @@ parallelsDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
     /* Flags checked by virDomainDefFormat */
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     parallelsDriverUnlock(privconn);
 
     if (privdom == NULL) {
@@ -1034,7 +1076,7 @@ parallelsDomainGetAutostart(virDomainPtr domain, int *autostart)
     int ret = -1;
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     parallelsDriverUnlock(privconn);
 
     if (privdom == NULL) {
@@ -1066,7 +1108,7 @@ parallelsDomainChangeState(virDomainPtr domain,
     int ret = -1;
 
     parallelsDriverLock(privconn);
-    privdom = virDomainFindByUUID(&privconn->domains, domain->uuid);
+    privdom = virDomainFindByUUID(privconn->domains, domain->uuid);
     parallelsDriverUnlock(privconn);
 
     if (privdom == NULL) {
@@ -1621,13 +1663,13 @@ parallelsDomainDefineXML(virConnectPtr conn, const char *xml)
         goto cleanup;
     }
 
-    if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0) {
+    if ((dupVM = virDomainObjIsDuplicate(privconn->domains, def, 0)) < 0) {
         virReportError(VIR_ERR_INVALID_ARG, "%s", _("Already exists"));
         goto cleanup;
     }
 
     if (dupVM == 1) {
-        olddom = virDomainFindByUUID(&privconn->domains, def->uuid);
+        olddom = virDomainFindByUUID(privconn->domains, def->uuid);
         if (parallelsApplyChanges(olddom, def) < 0) {
             virDomainObjUnlock(olddom);
             goto cleanup;
@@ -1635,7 +1677,7 @@ parallelsDomainDefineXML(virConnectPtr conn, const char *xml)
         virDomainObjUnlock(olddom);
 
         if (!(dom = virDomainAssignDef(privconn->caps,
-                                       &privconn->domains, def, false))) {
+                                       privconn->domains, def, false))) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Can't allocate domobj"));
             goto cleanup;
@@ -1643,15 +1685,17 @@ parallelsDomainDefineXML(virConnectPtr conn, const char *xml)
 
         def = NULL;
     } else {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+
         if (parallelsCreateVm(conn, def))
             goto cleanup;
-        if (parallelsLoadDomains(privconn, def->name))
+
+        if (!(dom = parallelsGetDomain(privconn, def->name)))
             goto cleanup;
-        dom = virDomainFindByName(&privconn->domains, def->name);
-        if (!dom) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Domain for '%s' is not defined after creation"),
-                           def->name ? def->name : _("(unnamed)"));
+
+        virUUIDFormat(dom->def->uuid, uuidstr);
+        if (virHashAddEntry(privconn->domains->objs, uuidstr, dom) < 0) {
+            VIR_FREE(dom);
             goto cleanup;
         }
     }
diff --git a/src/parallels/parallels_utils.h b/src/parallels/parallels_utils.h
index 6a27003..3468c1d 100644
--- a/src/parallels/parallels_utils.h
+++ b/src/parallels/parallels_utils.h
@@ -32,7 +32,7 @@
 
 struct _parallelsConn {
     virMutex lock;
-    virDomainObjList domains;
+    virDomainObjListPtr domains;
     virStoragePoolObjList pools;
     virCapsPtr caps;
     virDomainEventStatePtr domainEventState;
-- 
1.7.1

--
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]