conf: Use virDomainObjListFindBy*Locked for virDomainObjListAdd

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>
This commit is contained in:
John Ferlan 2018-03-09 07:37:14 -05:00
parent cf5184d1e1
commit 7ae289203a

View File

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