mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
remote: refactor the code for choosing the UNIX socket path
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@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
8b21674932
commit
746955690a
@ -747,34 +747,35 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr conn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifndef WIN32
|
static char *
|
||||||
static char *remoteGetUNIXSocketNonRoot(void)
|
remoteGetUNIXSocket(remoteDriverTransport transport,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
char *sockname = NULL;
|
char *sockname = NULL;
|
||||||
char *userdir = virGetUserRuntimeDirectory();
|
VIR_AUTOFREE(char *) userdir = NULL;
|
||||||
|
|
||||||
if (!userdir)
|
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
|
||||||
return NULL;
|
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) {
|
if (virAsprintf(&sockname,
|
||||||
VIR_FREE(userdir);
|
"%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0)
|
||||||
return NULL;
|
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);
|
VIR_DEBUG("Chosen UNIX sockname %s", sockname);
|
||||||
return sockname;
|
return sockname;
|
||||||
@ -961,6 +962,30 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
VIR_DEBUG("Connecting with transport %d", transport);
|
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;
|
||||||
|
break;
|
||||||
|
|
||||||
|
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. */
|
/* Connect to the remote service. */
|
||||||
switch ((remoteDriverTransport)transport) {
|
switch ((remoteDriverTransport)transport) {
|
||||||
case REMOTE_DRIVER_TRANSPORT_TLS:
|
case REMOTE_DRIVER_TRANSPORT_TLS:
|
||||||
@ -1002,20 +1027,6 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
|
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");
|
VIR_DEBUG("Starting LibSSH2 session");
|
||||||
|
|
||||||
priv->client = virNetClientNewLibSSH2(priv->hostname,
|
priv->client = virNetClientNewLibSSH2(priv->hostname,
|
||||||
@ -1037,20 +1048,6 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case REMOTE_DRIVER_TRANSPORT_LIBSSH:
|
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");
|
VIR_DEBUG("Starting libssh session");
|
||||||
|
|
||||||
priv->client = virNetClientNewLibssh(priv->hostname,
|
priv->client = virNetClientNewLibssh(priv->hostname,
|
||||||
@ -1073,15 +1070,6 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
case REMOTE_DRIVER_TRANSPORT_UNIX:
|
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) &&
|
if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
|
||||||
!(daemonPath = virFileFindResourceFull("libvirtd",
|
!(daemonPath = virFileFindResourceFull("libvirtd",
|
||||||
NULL, NULL,
|
NULL, NULL,
|
||||||
@ -1102,20 +1090,6 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
if (!command && VIR_STRDUP(command, "ssh") < 0)
|
if (!command && VIR_STRDUP(command, "ssh") < 0)
|
||||||
goto failed;
|
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,
|
if (!(priv->client = virNetClientNewSSH(priv->hostname,
|
||||||
port,
|
port,
|
||||||
command,
|
command,
|
||||||
|
Loading…
Reference in New Issue
Block a user