From: Daniel P. Berrangé Date: Fri, 23 Mar 2018 10:41:23 +0000 (+0000) Subject: remote: refactor code for building UNIX socket paths X-Git-Tag: v4.3.0-rc1~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5cca0cb6a0be68dcbb7c2c8930863ff09fa898f8;p=thirdparty%2Flibvirt.git remote: refactor code for building UNIX socket paths The code for building UNIX socket paths will be getting more complex to cope with accessing various different daemons. Refactor it to eliminate the code duplication and isolation the logic for constructing paths. Signed-off-by: Daniel P. Berrangé --- diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 87398df575..3973b37d5f 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -710,6 +710,39 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr conn, var->ignore = 1; \ continue; \ } + + +static char *remoteGetUNIXSocketNonRoot(void) +{ + char *sockname = NULL; + char *userdir = virGetUserRuntimeDirectory(); + + if (!userdir) + return NULL; + + if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) { + VIR_FREE(userdir); + return NULL; + } + VIR_FREE(userdir); + + VIR_DEBUG("Chosen UNIX sockname %s", sockname); + return sockname; +} + +static char *remoteGetUNIXSocketRoot(unsigned int flags) +{ + char *sockname = NULL; + + if (VIR_STRDUP(sockname, + flags & VIR_DRV_OPEN_REMOTE_RO ? + LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0) + return NULL; + + VIR_DEBUG("Chosen UNIX sockname %s", sockname); + return sockname; +} + /* * URIs that this driver needs to handle: * @@ -971,9 +1004,7 @@ doRemoteOpen(virConnectPtr conn, goto failed; } - if (VIR_STRDUP(sockname, - flags & VIR_DRV_OPEN_REMOTE_RO ? - LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0) + if (!(sockname = remoteGetUNIXSocketRoot(flags))) goto failed; } @@ -1008,9 +1039,7 @@ doRemoteOpen(virConnectPtr conn, goto failed; } - if (VIR_STRDUP(sockname, - flags & VIR_DRV_OPEN_REMOTE_RO ? - LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0) + if (!(sockname = remoteGetUNIXSocketRoot(flags))) goto failed; } @@ -1037,24 +1066,12 @@ doRemoteOpen(virConnectPtr conn, #ifndef WIN32 case trans_unix: if (!sockname) { - if (flags & VIR_DRV_OPEN_REMOTE_USER) { - char *userdir = virGetUserRuntimeDirectory(); - - if (!userdir) - goto failed; - - if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) { - VIR_FREE(userdir); - goto failed; - } - VIR_FREE(userdir); - } else { - if (VIR_STRDUP(sockname, - flags & VIR_DRV_OPEN_REMOTE_RO ? - LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0) - goto failed; - } - VIR_DEBUG("Proceeding with sockname %s", sockname); + if (flags & VIR_DRV_OPEN_REMOTE_USER) + sockname = remoteGetUNIXSocketNonRoot(); + else + sockname = remoteGetUNIXSocketRoot(flags); + if (!sockname) + goto failed; } if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) && @@ -1087,9 +1104,7 @@ doRemoteOpen(virConnectPtr conn, goto failed; } - if (VIR_STRDUP(sockname, - flags & VIR_DRV_OPEN_REMOTE_RO ? - LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0) + if (!(sockname = remoteGetUNIXSocketRoot(flags))) goto failed; }