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(a)redhat.com>
---
src/conf/network_conf.c | 11 +++++++----
src/parallels/parallels_network.c | 9 +++++----
src/test/test_driver.c | 4 ++--
3 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 5b0f36f..c620201 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -137,6 +137,7 @@ virNetworkObjEndAPI(virNetworkObjPtr *net)
return;
virObjectUnlock(*net);
+ virObjectUnref(*net);
*net = NULL;
}
@@ -164,6 +165,7 @@ virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets,
virObjectLock(nets->objs[i]);
if (!memcmp(nets->objs[i]->def->uuid, uuid, VIR_UUID_BUFLEN)) {
ret = nets->objs[i];
+ virObjectRef(ret);
break;
}
virObjectUnlock(nets->objs[i]);
@@ -184,6 +186,7 @@ virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets,
virObjectLock(nets->objs[i]);
if (STREQ(nets->objs[i]->def->name, name)) {
ret = nets->objs[i];
+ virObjectRef(ret);
break;
}
virObjectUnlock(nets->objs[i]);
@@ -469,13 +472,13 @@ virNetworkAssignDef(virNetworkObjListPtr nets,
network->def = def;
network->persistent = !live;
+ virObjectRef(network);
virObjectUnlock(nets);
return network;
error:
virObjectUnlock(nets);
- virObjectUnlock(network);
- virObjectUnref(network);
+ virNetworkObjEndAPI(&network);
return NULL;
}
@@ -3057,7 +3060,7 @@ virNetworkLoadAllState(virNetworkObjListPtr nets,
continue;
if ((net = virNetworkLoadState(nets, stateDir, entry->d_name)))
- virObjectUnlock(net);
+ virNetworkObjEndAPI(&net);
}
closedir(dir);
@@ -3098,7 +3101,7 @@ int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
autostartDir,
entry->d_name);
if (net)
- virObjectUnlock(net);
+ virNetworkObjEndAPI(&net);
}
closedir(dir);
diff --git a/src/parallels/parallels_network.c b/src/parallels/parallels_network.c
index 7618890..da29060 100644
--- a/src/parallels/parallels_network.c
+++ b/src/parallels/parallels_network.c
@@ -230,7 +230,6 @@ parallelsLoadNetwork(parallelsConnPtr privconn, virJSONValuePtr jobj)
goto cleanup;
net->active = 1;
net->autostart = 1;
- virObjectUnlock(net);
return net;
cleanup:
@@ -265,7 +264,6 @@ parallelsAddRoutedNetwork(parallelsConnPtr privconn)
}
net->active = 1;
net->autostart = 1;
- virObjectUnlock(net);
return net;
@@ -277,7 +275,7 @@ parallelsAddRoutedNetwork(parallelsConnPtr privconn)
static int parallelsLoadNetworks(parallelsConnPtr privconn)
{
virJSONValuePtr jobj, jobj2;
- virNetworkObjPtr net;
+ virNetworkObjPtr net = NULL;
int ret = -1;
int count;
size_t i;
@@ -305,16 +303,19 @@ static int parallelsLoadNetworks(parallelsConnPtr privconn)
net = parallelsLoadNetwork(privconn, jobj2);
if (!net)
goto cleanup;
+ else
+ virNetworkObjEndAPI(&net);
}
- if (!parallelsAddRoutedNetwork(privconn))
+ if (!(net = parallelsAddRoutedNetwork(privconn)))
goto cleanup;
ret = 0;
cleanup:
virJSONValueFree(jobj);
+ virNetworkObjEndAPI(&net);
return ret;
}
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index a7a3848..bfa0e91 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -787,7 +787,7 @@ testOpenDefault(virConnectPtr conn)
goto error;
}
netobj->active = 1;
- virObjectUnlock(netobj);
+ virNetworkObjEndAPI(&netobj);
if (!(interfacedef = virInterfaceDefParseString(defaultInterfaceXML)))
goto error;
@@ -1155,7 +1155,7 @@ testParseNetworks(testConnPtr privconn,
}
obj->active = 1;
- virObjectUnlock(obj);
+ virNetworkObjEndAPI(&obj);
}
ret = 0;
--
2.0.5