1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

src/driver.c: remove duplicated code in virGetConnect* functions

All the 6 virGetConnect* functions in driver.c shares the
same code base. This patch creates a new static function
virGetConnectGeneric() that contains the common code to
be used with all other virGetConnect*.

Reviewed-by: Cole Robinson <crobinso@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Daniel Henrique Barboza 2019-10-08 18:02:00 -03:00 committed by Cole Robinson
parent fd3b8fe7ad
commit 37b565c000

View File

@ -135,112 +135,61 @@ virConnectCacheOnceInit(void)
VIR_ONCE_GLOBAL_INIT(virConnectCache); VIR_ONCE_GLOBAL_INIT(virConnectCache);
virConnectPtr virGetConnectInterface(void) static virConnectPtr
virGetConnectGeneric(virThreadLocalPtr threadPtr, const char *name)
{ {
virConnectPtr conn; virConnectPtr conn;
if (virConnectCacheInitialize() < 0) if (virConnectCacheInitialize() < 0)
return NULL; return NULL;
conn = virThreadLocalGet(&connectInterface); conn = virThreadLocalGet(threadPtr);
if (conn) { if (conn) {
VIR_DEBUG("Return cached interface connection %p", conn); VIR_DEBUG("Return cached %s connection %p", name, conn);
virObjectRef(conn); virObjectRef(conn);
} else { } else {
conn = virConnectOpen(geteuid() == 0 ? "interface:///system" : "interface:///session"); VIR_AUTOFREE(char *) uri = NULL;
VIR_DEBUG("Opened new interface connection %p", conn); const char *uriPath = geteuid() == 0 ? "/system" : "/session";
if (virAsprintf(&uri, "%s://%s", name, uriPath) < 0)
return NULL;
conn = virConnectOpen(uri);
VIR_DEBUG("Opened new %s connection %p", name, conn);
} }
return conn; return conn;
} }
virConnectPtr virGetConnectInterface(void)
{
return virGetConnectGeneric(&connectInterface, "interface");
}
virConnectPtr virGetConnectNetwork(void) virConnectPtr virGetConnectNetwork(void)
{ {
virConnectPtr conn; return virGetConnectGeneric(&connectNetwork, "network");
if (virConnectCacheInitialize() < 0)
return NULL;
conn = virThreadLocalGet(&connectNetwork);
if (conn) {
VIR_DEBUG("Return cached network connection %p", conn);
virObjectRef(conn);
} else {
conn = virConnectOpen(geteuid() == 0 ? "network:///system" : "network:///session");
VIR_DEBUG("Opened new network connection %p", conn);
}
return conn;
} }
virConnectPtr virGetConnectNWFilter(void) virConnectPtr virGetConnectNWFilter(void)
{ {
virConnectPtr conn; return virGetConnectGeneric(&connectNWFilter, "nwfilter");
if (virConnectCacheInitialize() < 0)
return NULL;
conn = virThreadLocalGet(&connectNWFilter);
if (conn) {
VIR_DEBUG("Return cached nwfilter connection %p", conn);
virObjectRef(conn);
} else {
conn = virConnectOpen(geteuid() == 0 ? "nwfilter:///system" : "nwfilter:///session");
VIR_DEBUG("Opened new nwfilter connection %p", conn);
}
return conn;
} }
virConnectPtr virGetConnectNodeDev(void) virConnectPtr virGetConnectNodeDev(void)
{ {
virConnectPtr conn; return virGetConnectGeneric(&connectNodeDev, "nodedev");
if (virConnectCacheInitialize() < 0)
return NULL;
conn = virThreadLocalGet(&connectNodeDev);
if (conn) {
VIR_DEBUG("Return cached nodedev connection %p", conn);
virObjectRef(conn);
} else {
conn = virConnectOpen(geteuid() == 0 ? "nodedev:///system" : "nodedev:///session");
VIR_DEBUG("Opened new nodedev connection %p", conn);
}
return conn;
} }
virConnectPtr virGetConnectSecret(void) virConnectPtr virGetConnectSecret(void)
{ {
virConnectPtr conn; return virGetConnectGeneric(&connectSecret, "secret");
if (virConnectCacheInitialize() < 0)
return NULL;
conn = virThreadLocalGet(&connectSecret);
if (conn) {
VIR_DEBUG("Return cached secret connection %p", conn);
virObjectRef(conn);
} else {
conn = virConnectOpen(geteuid() == 0 ? "secret:///system" : "secret:///session");
VIR_DEBUG("Opened new secret connection %p", conn);
}
return conn;
} }
virConnectPtr virGetConnectStorage(void) virConnectPtr virGetConnectStorage(void)
{ {
virConnectPtr conn; return virGetConnectGeneric(&connectStorage, "storage");
if (virConnectCacheInitialize() < 0)
return NULL;
conn = virThreadLocalGet(&connectStorage);
if (conn) {
VIR_DEBUG("Return cached storage connection %p", conn);
virObjectRef(conn);
} else {
conn = virConnectOpen(geteuid() == 0 ? "storage:///system" : "storage:///session");
VIR_DEBUG("Opened new storage connection %p", conn);
}
return conn;
} }