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*. Signed-off-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx> --- CC'ing Cole Robinson since he reviewed similar patches a few days ago. src/driver.c | 100 +++++++++++++-------------------------------------- 1 file changed, 25 insertions(+), 75 deletions(-) diff --git a/src/driver.c b/src/driver.c index ed2d943ddf..8a4bc8ff66 100644 --- a/src/driver.c +++ b/src/driver.c @@ -29,6 +29,7 @@ #include "virfile.h" #include "virlog.h" #include "virmodule.h" +#include "virstring.h" #include "virthread.h" #include "configmake.h" @@ -96,112 +97,61 @@ virConnectCacheOnceInit(void) VIR_ONCE_GLOBAL_INIT(virConnectCache); -virConnectPtr virGetConnectInterface(void) +static virConnectPtr +virGetConnectGeneric(virThreadLocal thread, const char *name) { virConnectPtr conn; if (virConnectCacheInitialize() < 0) return NULL; - conn = virThreadLocalGet(&connectInterface); + conn = virThreadLocalGet(&thread); + if (conn) { - VIR_DEBUG("Return cached interface connection %p", conn); + VIR_DEBUG("Return cached %s connection %p", name, conn); virObjectRef(conn); } else { - conn = virConnectOpen(geteuid() == 0 ? "interface:///system" : "interface:///session"); - VIR_DEBUG("Opened new interface connection %p", conn); + VIR_AUTOFREE(char *) uri = NULL; + 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; } -virConnectPtr virGetConnectNetwork(void) -{ - virConnectPtr conn; - if (virConnectCacheInitialize() < 0) - return NULL; +virConnectPtr virGetConnectInterface(void) +{ + return virGetConnectGeneric(connectInterface, "interface"); +} - 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 virGetConnectNetwork(void) +{ + return virGetConnectGeneric(connectNetwork, "network"); } virConnectPtr virGetConnectNWFilter(void) { - virConnectPtr conn; - - 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; + return virGetConnectGeneric(connectNWFilter, "nwfilter"); } virConnectPtr virGetConnectNodeDev(void) { - virConnectPtr conn; - - 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; + return virGetConnectGeneric(connectNodeDev, "nodedev"); } virConnectPtr virGetConnectSecret(void) { - virConnectPtr conn; - - 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; + return virGetConnectGeneric(connectSecret, "secret"); } virConnectPtr virGetConnectStorage(void) { - virConnectPtr conn; - - 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; + return virGetConnectGeneric(connectStorage, "storage"); } -- 2.21.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list