The ssh, libssh, libssh2 & unix transports all need to use a UNIX socket
path, and duplicate some of the same logic for error checking. Pull this
out into a separate method to increase code sharing.
Reviewed-by: Andrea Bolognani <abologna(a)redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/remote/remote_driver.c | 123 +++++++++++++++----------------------
1 file changed, 48 insertions(+), 75 deletions(-)
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 706d9e6e14..e647a77f41 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -747,34 +747,35 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr conn,
}
-#ifndef WIN32
-static char *remoteGetUNIXSocketNonRoot(void)
+static char *
+remoteGetUNIXSocket(remoteDriverTransport transport,
+ unsigned int flags)
{
char *sockname = NULL;
- char *userdir = virGetUserRuntimeDirectory();
-
- if (!userdir)
- return NULL;
+ VIR_AUTOFREE(char *) userdir = NULL;
+
+ if (flags & VIR_DRV_OPEN_REMOTE_USER) {
+ if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
+ virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
+ _("Connecting to session instance without "
+ "socket path is not supported by the %s "
+ "transport"),
+ remoteDriverTransportTypeToString(transport));
+ return NULL;
+ }
+ if (!(userdir = virGetUserRuntimeDirectory()))
+ return NULL;
- if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir)
< 0) {
- VIR_FREE(userdir);
- return NULL;
+ if (virAsprintf(&sockname,
+ "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0)
+ return NULL;
+ } else {
+ if (VIR_STRDUP(sockname,
+ flags & VIR_DRV_OPEN_REMOTE_RO ?
+ LIBVIRTD_PRIV_UNIX_SOCKET_RO :
+ LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+ return NULL;
}
- VIR_FREE(userdir);
-
- VIR_DEBUG("Chosen UNIX sockname %s", sockname);
- return sockname;
-}
-#endif /* WIN32 */
-
-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;
@@ -976,6 +977,29 @@ doRemoteOpen(virConnectPtr conn,
}
VIR_DEBUG("Connecting with transport %d", transport);
+
+ switch ((remoteDriverTransport)transport) {
+ case REMOTE_DRIVER_TRANSPORT_UNIX:
+ case REMOTE_DRIVER_TRANSPORT_SSH:
+ case REMOTE_DRIVER_TRANSPORT_LIBSSH:
+ case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
+ if (!sockname &&
+ !(sockname = remoteGetUNIXSocket(transport, flags)))
+ goto failed;
+
+ case REMOTE_DRIVER_TRANSPORT_TCP:
+ case REMOTE_DRIVER_TRANSPORT_TLS:
+ case REMOTE_DRIVER_TRANSPORT_EXT:
+ break;
+
+ case REMOTE_DRIVER_TRANSPORT_LAST:
+ default:
+ virReportEnumRangeError(remoteDriverTransport, transport);
+ goto failed;
+ }
+
+ VIR_DEBUG("Chosen UNIX socket %s", NULLSTR(sockname));
+
/* Connect to the remote service. */
switch ((remoteDriverTransport)transport) {
case REMOTE_DRIVER_TRANSPORT_TLS:
@@ -1017,20 +1041,6 @@ doRemoteOpen(virConnectPtr conn,
break;
case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
- if (!sockname) {
- /* Right now we don't support default session connections */
- if (flags & VIR_DRV_OPEN_REMOTE_USER) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("Connecting to session instance without "
- "socket path is not supported by the libssh2
"
- "connection driver"));
- goto failed;
- }
-
- if (!(sockname = remoteGetUNIXSocketRoot(flags)))
- goto failed;
- }
-
VIR_DEBUG("Starting LibSSH2 session");
priv->client = virNetClientNewLibSSH2(priv->hostname,
@@ -1052,20 +1062,6 @@ doRemoteOpen(virConnectPtr conn,
break;
case REMOTE_DRIVER_TRANSPORT_LIBSSH:
- if (!sockname) {
- /* Right now we don't support default session connections */
- if (flags & VIR_DRV_OPEN_REMOTE_USER) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("Connecting to session instance without "
- "socket path is not supported by the libssh "
- "connection driver"));
- goto failed;
- }
-
- if (!(sockname = remoteGetUNIXSocketRoot(flags)))
- goto failed;
- }
-
VIR_DEBUG("Starting libssh session");
priv->client = virNetClientNewLibssh(priv->hostname,
@@ -1088,15 +1084,6 @@ doRemoteOpen(virConnectPtr conn,
#ifndef WIN32
case REMOTE_DRIVER_TRANSPORT_UNIX:
- if (!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) &&
!(daemonPath = virFileFindResourceFull("libvirtd",
NULL, NULL,
@@ -1117,20 +1104,6 @@ doRemoteOpen(virConnectPtr conn,
if (!command && VIR_STRDUP(command, "ssh") < 0)
goto failed;
- if (!sockname) {
- /* Right now we don't support default session connections */
- if (flags & VIR_DRV_OPEN_REMOTE_USER) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("Connecting to session instance without "
- "socket path is not supported by the ssh "
- "connection driver"));
- goto failed;
- }
-
- if (!(sockname = remoteGetUNIXSocketRoot(flags)))
- goto failed;
- }
-
if (!(priv->client = virNetClientNewSSH(priv->hostname,
port,
command,
--
2.21.0