]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Use virDomainObjListFindBy*Locked for virDomainObjListAdd
authorJohn Ferlan <jferlan@redhat.com>
Fri, 9 Mar 2018 12:37:14 +0000 (07:37 -0500)
committerJohn Ferlan <jferlan@redhat.com>
Thu, 3 May 2018 23:09:03 +0000 (19:09 -0400)
Use the FindBy{UUID|Name}Locked helpers which will return a locked
and ref counted object rather than the direct virHashLookup and
virObjectLock of the returned object. We'll need to temporarily
virObjectUnref when we assign a new domain @def, but that will
change shortly when virDomainObjListAddObjLocked returns the
correct reference counted object.

Use the virDomainObjEndAPI in the error path to Unref/Unlock for
the corresponding Unref/Unlock of either the FindBy* return or
the virDomainObjNew since both return a reffed/locked object.

Signed-off-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/conf/virdomainobjlist.c

index 2f1702059361280a7e31c9312ab3819035dcf02e..6711d67954b9ea15538db3e116fb170598e7bfd8 100644 (file)
@@ -280,11 +280,8 @@ virDomainObjListAddLocked(virDomainObjListPtr doms,
     if (oldDef)
         *oldDef = NULL;
 
-    virUUIDFormat(def->uuid, uuidstr);
-
     /* See if a VM with matching UUID already exists */
-    if ((vm = virHashLookup(doms->objs, uuidstr))) {
-        virObjectLock(vm);
+    if ((vm = virDomainObjListFindByUUIDLocked(doms, def->uuid))) {
         /* UUID matches, but if names don't match, refuse it */
         if (STRNEQ(vm->def->name, def->name)) {
             virUUIDFormat(vm->def->uuid, uuidstr);
@@ -314,10 +311,12 @@ virDomainObjListAddLocked(virDomainObjListPtr doms,
                               def,
                               !!(flags & VIR_DOMAIN_OBJ_LIST_ADD_LIVE),
                               oldDef);
+        /* XXX: Temporary until this API is fixed to return a locked and
+         *      refcnt'd object */
+        virObjectUnref(vm);
     } else {
         /* UUID does not match, but if a name matches, refuse it */
-        if ((vm = virHashLookup(doms->objsName, def->name))) {
-            virObjectLock(vm);
+        if ((vm = virDomainObjListFindByNameLocked(doms, def->name))) {
             virUUIDFormat(vm->def->uuid, uuidstr);
             virReportError(VIR_ERR_OPERATION_FAILED,
                            _("domain '%s' already exists with uuid %s"),
@@ -329,18 +328,15 @@ virDomainObjListAddLocked(virDomainObjListPtr doms,
             goto cleanup;
         vm->def = def;
 
-        if (virDomainObjListAddObjLocked(doms, vm) < 0) {
-            virDomainObjEndAPI(&vm);
-            return NULL;
-        }
+        if (virDomainObjListAddObjLocked(doms, vm) < 0)
+            goto error;
     }
  cleanup:
     return vm;
 
  error:
-    virObjectUnlock(vm);
-    vm = NULL;
-    goto cleanup;
+    virDomainObjEndAPI(&vm);
+    return NULL;
 }