mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-10-30 09:53:10 +00:00
virNetworkObjFindBy*: Return an reference to found object
This patch turns both virNetworkObjFindByUUID() and virNetworkObjFindByName() to return an referenced object so that even if caller unlocks it, it's for sure that object won't disappear meanwhile. Especially if the object (in general) is locked and unlocked during the caller run. Moreover, this commit is nicely small, since the object unrefing can be done in virNetworkObjEndAPI(). Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
53881c70bc
commit
68818dcdd5
@ -136,6 +136,7 @@ virNetworkObjEndAPI(virNetworkObjPtr *net)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
virObjectUnlock(*net);
|
virObjectUnlock(*net);
|
||||||
|
virObjectUnref(*net);
|
||||||
*net = NULL;
|
*net = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +158,15 @@ virNetworkObjListPtr virNetworkObjListNew(void)
|
|||||||
return nets;
|
return nets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virNetworkObjFindByUUIDLocked:
|
||||||
|
* @nets: list of network objects
|
||||||
|
* @uuid: network uuid to find
|
||||||
|
*
|
||||||
|
* This functions requires @nets to be locked already!
|
||||||
|
*
|
||||||
|
* Returns: not locked, but ref'd network object.
|
||||||
|
*/
|
||||||
virNetworkObjPtr
|
virNetworkObjPtr
|
||||||
virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets,
|
virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets,
|
||||||
const unsigned char *uuid)
|
const unsigned char *uuid)
|
||||||
@ -168,10 +178,20 @@ virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets,
|
|||||||
|
|
||||||
ret = virHashLookup(nets->objs, uuidstr);
|
ret = virHashLookup(nets->objs, uuidstr);
|
||||||
if (ret)
|
if (ret)
|
||||||
virObjectLock(ret);
|
virObjectRef(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virNetworkObjFindByUUID:
|
||||||
|
* @nets: list of network objects
|
||||||
|
* @uuid: network uuid to find
|
||||||
|
*
|
||||||
|
* This functions locks @nets and find network object which
|
||||||
|
* corresponds to @uuid.
|
||||||
|
*
|
||||||
|
* Returns: locked and ref'd network object.
|
||||||
|
*/
|
||||||
virNetworkObjPtr
|
virNetworkObjPtr
|
||||||
virNetworkObjFindByUUID(virNetworkObjListPtr nets,
|
virNetworkObjFindByUUID(virNetworkObjListPtr nets,
|
||||||
const unsigned char *uuid)
|
const unsigned char *uuid)
|
||||||
@ -181,6 +201,8 @@ virNetworkObjFindByUUID(virNetworkObjListPtr nets,
|
|||||||
virObjectLock(nets);
|
virObjectLock(nets);
|
||||||
ret = virNetworkObjFindByUUIDLocked(nets, uuid);
|
ret = virNetworkObjFindByUUIDLocked(nets, uuid);
|
||||||
virObjectUnlock(nets);
|
virObjectUnlock(nets);
|
||||||
|
if (ret)
|
||||||
|
virObjectLock(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,6 +221,15 @@ virNetworkObjSearchName(const void *payload,
|
|||||||
return want;
|
return want;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* virNetworkObjFindByNameLocked:
|
||||||
|
* @nets: list of network objects
|
||||||
|
* @name: network name to find
|
||||||
|
*
|
||||||
|
* This functions requires @nets to be locked already!
|
||||||
|
*
|
||||||
|
* Returns: not locked, but ref'd network object.
|
||||||
|
*/
|
||||||
virNetworkObjPtr
|
virNetworkObjPtr
|
||||||
virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
|
virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
|
||||||
const char *name)
|
const char *name)
|
||||||
@ -207,10 +238,20 @@ virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
|
|||||||
|
|
||||||
ret = virHashSearch(nets->objs, virNetworkObjSearchName, name);
|
ret = virHashSearch(nets->objs, virNetworkObjSearchName, name);
|
||||||
if (ret)
|
if (ret)
|
||||||
virObjectLock(ret);
|
virObjectRef(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virNetworkObjFindByName:
|
||||||
|
* @nets: list of network objects
|
||||||
|
* @name: network name to find
|
||||||
|
*
|
||||||
|
* This functions locks @nets and find network object which
|
||||||
|
* corresponds to @name.
|
||||||
|
*
|
||||||
|
* Returns: locked and ref'd network object.
|
||||||
|
*/
|
||||||
virNetworkObjPtr
|
virNetworkObjPtr
|
||||||
virNetworkObjFindByName(virNetworkObjListPtr nets,
|
virNetworkObjFindByName(virNetworkObjListPtr nets,
|
||||||
const char *name)
|
const char *name)
|
||||||
@ -220,6 +261,8 @@ virNetworkObjFindByName(virNetworkObjListPtr nets,
|
|||||||
virObjectLock(nets);
|
virObjectLock(nets);
|
||||||
ret = virNetworkObjFindByNameLocked(nets, name);
|
ret = virNetworkObjFindByNameLocked(nets, name);
|
||||||
virObjectUnlock(nets);
|
virObjectUnlock(nets);
|
||||||
|
if (ret)
|
||||||
|
virObjectLock(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,6 +527,7 @@ virNetworkAssignDef(virNetworkObjListPtr nets,
|
|||||||
virObjectLock(nets);
|
virObjectLock(nets);
|
||||||
if ((network = virNetworkObjFindByNameLocked(nets, def->name))) {
|
if ((network = virNetworkObjFindByNameLocked(nets, def->name))) {
|
||||||
virObjectUnlock(nets);
|
virObjectUnlock(nets);
|
||||||
|
virObjectLock(network);
|
||||||
virNetworkObjAssignDef(network, def, live);
|
virNetworkObjAssignDef(network, def, live);
|
||||||
return network;
|
return network;
|
||||||
}
|
}
|
||||||
@ -500,13 +544,13 @@ virNetworkAssignDef(virNetworkObjListPtr nets,
|
|||||||
|
|
||||||
network->def = def;
|
network->def = def;
|
||||||
network->persistent = !live;
|
network->persistent = !live;
|
||||||
|
virObjectRef(network);
|
||||||
virObjectUnlock(nets);
|
virObjectUnlock(nets);
|
||||||
return network;
|
return network;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
virObjectUnlock(nets);
|
virObjectUnlock(nets);
|
||||||
virObjectUnlock(network);
|
virNetworkObjEndAPI(&network);
|
||||||
virObjectUnref(network);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -680,7 +724,6 @@ void virNetworkRemoveInactive(virNetworkObjListPtr nets,
|
|||||||
virObjectLock(nets);
|
virObjectLock(nets);
|
||||||
virObjectLock(net);
|
virObjectLock(net);
|
||||||
virHashRemoveEntry(nets->objs, uuidstr);
|
virHashRemoveEntry(nets->objs, uuidstr);
|
||||||
virObjectUnlock(net);
|
|
||||||
virObjectUnlock(nets);
|
virObjectUnlock(nets);
|
||||||
virObjectUnref(net);
|
virObjectUnref(net);
|
||||||
}
|
}
|
||||||
|
@ -2939,7 +2939,6 @@ static virNetworkPtr networkCreateXML(virConnectPtr conn, const char *xml)
|
|||||||
if (networkStartNetwork(network) < 0) {
|
if (networkStartNetwork(network) < 0) {
|
||||||
virNetworkRemoveInactive(driver->networks,
|
virNetworkRemoveInactive(driver->networks,
|
||||||
network);
|
network);
|
||||||
network = NULL;
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2988,7 +2987,6 @@ static virNetworkPtr networkDefineXML(virConnectPtr conn, const char *xml)
|
|||||||
if (virNetworkSaveConfig(driver->networkConfigDir, def) < 0) {
|
if (virNetworkSaveConfig(driver->networkConfigDir, def) < 0) {
|
||||||
if (!virNetworkObjIsActive(network)) {
|
if (!virNetworkObjIsActive(network)) {
|
||||||
virNetworkRemoveInactive(driver->networks, network);
|
virNetworkRemoveInactive(driver->networks, network);
|
||||||
network = NULL;
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
/* if network was active already, just undo new persistent
|
/* if network was active already, just undo new persistent
|
||||||
@ -3052,11 +3050,8 @@ networkUndefine(virNetworkPtr net)
|
|||||||
|
|
||||||
VIR_INFO("Undefining network '%s'", network->def->name);
|
VIR_INFO("Undefining network '%s'", network->def->name);
|
||||||
if (!active) {
|
if (!active) {
|
||||||
if (networkRemoveInactive(network) < 0) {
|
if (networkRemoveInactive(network) < 0)
|
||||||
network = NULL;
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
|
||||||
network = NULL;
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* if the network still exists, it was active, and we need to make
|
/* if the network still exists, it was active, and we need to make
|
||||||
@ -3313,14 +3308,11 @@ static int networkDestroy(virNetworkPtr net)
|
|||||||
VIR_NETWORK_EVENT_STOPPED,
|
VIR_NETWORK_EVENT_STOPPED,
|
||||||
0);
|
0);
|
||||||
|
|
||||||
if (!network->persistent) {
|
if (!network->persistent &&
|
||||||
if (networkRemoveInactive(network) < 0) {
|
networkRemoveInactive(network) < 0) {
|
||||||
network = NULL;
|
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
network = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (event)
|
if (event)
|
||||||
|
@ -787,7 +787,7 @@ testOpenDefault(virConnectPtr conn)
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
netobj->active = 1;
|
netobj->active = 1;
|
||||||
virObjectUnlock(netobj);
|
virNetworkObjEndAPI(&netobj);
|
||||||
|
|
||||||
if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
|
if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
|
||||||
goto error;
|
goto error;
|
||||||
@ -1155,7 +1155,7 @@ testParseNetworks(testConnPtr privconn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
obj->active = 1;
|
obj->active = 1;
|
||||||
virObjectUnlock(obj);
|
virNetworkObjEndAPI(&obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
@ -3733,7 +3733,6 @@ static int testNetworkUndefine(virNetworkPtr network)
|
|||||||
0);
|
0);
|
||||||
|
|
||||||
virNetworkRemoveInactive(privconn->networks, privnet);
|
virNetworkRemoveInactive(privconn->networks, privnet);
|
||||||
privnet = NULL;
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
@ -3847,10 +3846,9 @@ static int testNetworkDestroy(virNetworkPtr network)
|
|||||||
event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
|
event = virNetworkEventLifecycleNew(privnet->def->name, privnet->def->uuid,
|
||||||
VIR_NETWORK_EVENT_STOPPED,
|
VIR_NETWORK_EVENT_STOPPED,
|
||||||
0);
|
0);
|
||||||
if (!privnet->persistent) {
|
if (!privnet->persistent)
|
||||||
virNetworkRemoveInactive(privconn->networks, privnet);
|
virNetworkRemoveInactive(privconn->networks, privnet);
|
||||||
privnet = NULL;
|
|
||||||
}
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
Loading…
Reference in New Issue
Block a user