1
0

remote: Clean up coding style and refactor remote connection opening

Remove spaces before function calls and some other coding nits in some
parts of the remote driver and refactor getting of URI argument
components into variables used by libvirt later on.
This commit is contained in:
Peter Krempa 2012-07-17 14:25:34 +02:00
parent 16d3ab8662
commit 6758ad4f9e

View File

@ -346,6 +346,29 @@ static void remoteClientCloseFunc(virNetClientPtr client ATTRIBUTE_UNUSED,
virMutexUnlock(&conn->lock);
}
/* helper macro to ease extraction of arguments from the URI */
#define EXTRACT_URI_ARG_STR(ARG_NAME, ARG_VAR) \
if (STRCASEEQ(var->name, ARG_NAME)) { \
VIR_FREE(ARG_VAR); \
if (!(ARG_VAR = strdup(var->value))) \
goto no_memory; \
var->ignore = 1; \
continue; \
}
#define EXTRACT_URI_ARG_BOOL(ARG_NAME, ARG_VAR) \
if (STRCASEEQ(var->name, ARG_NAME)) { \
int tmp; \
if (virStrToLong_i(var->value, NULL, 10, &tmp) < 0) { \
virReportError(VIR_ERR_INVALID_ARG, \
_("Failed to parse value of URI component %s"), \
var->name); \
goto failed; \
} \
ARG_VAR = tmp == 0; \
var->ignore = 1; \
continue; \
}
/*
* URIs that this driver needs to handle:
*
@ -446,26 +469,27 @@ doRemoteOpen (virConnectPtr conn,
/* Remote server defaults to "localhost" if not specified. */
if (conn->uri && conn->uri->port != 0) {
if (virAsprintf(&port, "%d", conn->uri->port) == -1) goto out_of_memory;
if (virAsprintf(&port, "%d", conn->uri->port) < 0)
goto no_memory;
} else if (transport == trans_tls) {
port = strdup (LIBVIRTD_TLS_PORT);
if (!port) goto out_of_memory;
if (!(port = strdup(LIBVIRTD_TLS_PORT)))
goto no_memory;
} else if (transport == trans_tcp) {
port = strdup (LIBVIRTD_TCP_PORT);
if (!port) goto out_of_memory;
} else
port = NULL; /* Port not used for unix, ext., default for ssh */
if (!(port = strdup(LIBVIRTD_TCP_PORT)))
goto no_memory;
} /* Port not used for unix, ext., default for ssh */
if (conn->uri && conn->uri->server)
priv->hostname = strdup(conn->uri->server);
else
priv->hostname = strdup("localhost");
priv->hostname = strdup (conn->uri && conn->uri->server ?
conn->uri->server : "localhost");
if (!priv->hostname)
goto out_of_memory;
if (conn->uri && conn->uri->user) {
username = strdup (conn->uri->user);
if (!username)
goto out_of_memory;
}
goto no_memory;
if (conn->uri && conn->uri->user &&
!(username = strdup (conn->uri->user)))
goto no_memory;
/* Get the variables from the query string.
* Then we need to reconstruct the query string (because
@ -477,57 +501,27 @@ doRemoteOpen (virConnectPtr conn,
if (conn->uri) {
for (i = 0; i < conn->uri->paramsCount ; i++) {
virURIParamPtr var = &conn->uri->params[i];
if (STRCASEEQ (var->name, "name")) {
VIR_FREE(name);
name = strdup (var->value);
if (!name) goto out_of_memory;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "command")) {
VIR_FREE(command);
command = strdup (var->value);
if (!command) goto out_of_memory;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "socket")) {
VIR_FREE(sockname);
sockname = strdup (var->value);
if (!sockname) goto out_of_memory;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "auth")) {
VIR_FREE(authtype);
authtype = strdup (var->value);
if (!authtype) goto out_of_memory;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "netcat")) {
VIR_FREE(netcat);
netcat = strdup (var->value);
if (!netcat) goto out_of_memory;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "keyfile")) {
VIR_FREE(keyfile);
keyfile = strdup (var->value);
if (!keyfile) goto out_of_memory;
} else if (STRCASEEQ (var->name, "no_sanity")) {
sanity = atoi(var->value) == 0;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "no_verify")) {
verify = atoi (var->value) == 0;
var->ignore = 1;
} else if (STRCASEEQ (var->name, "no_tty")) {
tty = atoi (var->value) == 0;
var->ignore = 1;
} else if (STRCASEEQ(var->name, "pkipath")) {
VIR_FREE(pkipath);
pkipath = strdup(var->value);
if (!pkipath) goto out_of_memory;
var->ignore = 1;
} else if (STRCASEEQ(var->name, "authfile")) {
EXTRACT_URI_ARG_STR("name", name);
EXTRACT_URI_ARG_STR("command", command);
EXTRACT_URI_ARG_STR("socket", sockname);
EXTRACT_URI_ARG_STR("auth", authtype);
EXTRACT_URI_ARG_STR("netcat", netcat);
EXTRACT_URI_ARG_STR("keyfile", keyfile);
EXTRACT_URI_ARG_STR("pkipath", pkipath);
EXTRACT_URI_ARG_BOOL("no_sanity", sanity);
EXTRACT_URI_ARG_BOOL("no_verify", verify);
EXTRACT_URI_ARG_BOOL("no_tty", tty);
if (STRCASEEQ(var->name, "authfile")) {
/* Strip this param, used by virauth.c */
var->ignore = 1;
} else {
continue;
}
VIR_DEBUG("passing through variable '%s' ('%s') to remote end",
var->name, var->value);
}
}
/* Construct the original name. */
if (!name) {
@ -536,7 +530,7 @@ doRemoteOpen (virConnectPtr conn,
STRPREFIX(conn->uri->scheme, "remote+"))) {
/* Allow remote serve to probe */
if (!(name = strdup("")))
goto out_of_memory;
goto no_memory;
} else {
virURI tmpuri = {
.scheme = conn->uri->scheme,
@ -566,7 +560,7 @@ doRemoteOpen (virConnectPtr conn,
} else {
/* Probe URI server side */
if (!(name = strdup("")))
goto out_of_memory;
goto no_memory;
}
VIR_DEBUG("proceeding with name = %s", name);
@ -578,7 +572,6 @@ doRemoteOpen (virConnectPtr conn,
goto failed;
}
VIR_DEBUG("Connecting with transport %d", transport);
/* Connect to the remote service. */
switch (transport) {
@ -615,7 +608,7 @@ doRemoteOpen (virConnectPtr conn,
if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
VIR_FREE(userdir);
goto out_of_memory;
goto no_memory;
}
VIR_FREE(userdir);
} else {
@ -624,7 +617,7 @@ doRemoteOpen (virConnectPtr conn,
else
sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET);
if (sockname == NULL)
goto out_of_memory;
goto no_memory;
}
VIR_DEBUG("Proceeding with sockname %s", sockname);
}
@ -643,17 +636,16 @@ doRemoteOpen (virConnectPtr conn,
break;
case trans_ssh:
command = command ? command : strdup ("ssh");
if (command == NULL)
goto out_of_memory;
if (!command && !(command = strdup("ssh")))
goto no_memory;
if (!sockname) {
if (flags & VIR_DRV_OPEN_REMOTE_RO)
sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET_RO);
else
sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET);
if (sockname == NULL)
goto out_of_memory;
if (!sockname)
goto no_memory;
}
if (!(priv->client = virNetClientNewSSH(priv->hostname,
@ -788,7 +780,7 @@ doRemoteOpen (virConnectPtr conn,
return retcode;
out_of_memory:
no_memory:
virReportOOMError();
failed:
@ -801,6 +793,8 @@ doRemoteOpen (virConnectPtr conn,
VIR_FREE(priv->hostname);
goto cleanup;
}
#undef EXTRACT_URI_ARG_STR
#undef EXTRACT_URI_ARG_BOOL
static struct private_data *
remoteAllocPrivateData(void)
@ -927,7 +921,7 @@ static char *
get_transport_from_scheme(char *scheme)
{
char *p = strchr(scheme, '+');
return p ? p+1 : 0;
return p ? p + 1 : NULL;
}
/*----------------------------------------------------------------------*/
@ -1142,8 +1136,7 @@ remoteNodeGetCPUStats (virConnectPtr conn,
rv = 0;
cleanup:
xdr_free ((xdrproc_t) xdr_remote_node_get_cpu_stats_ret,
(char *) &ret);
xdr_free((xdrproc_t) xdr_remote_node_get_cpu_stats_ret, (char *) &ret);
done:
remoteDriverUnlock(priv);
return rv;
@ -1152,7 +1145,8 @@ done:
static int
remoteNodeGetMemoryStats(virConnectPtr conn,
int cellNum,
virNodeMemoryStatsPtr params, int *nparams,
virNodeMemoryStatsPtr params,
int *nparams,
unsigned int flags)
{
int rv = -1;
@ -1206,8 +1200,7 @@ remoteNodeGetMemoryStats (virConnectPtr conn,
rv = 0;
cleanup:
xdr_free ((xdrproc_t) xdr_remote_node_get_memory_stats_ret,
(char *) &ret);
xdr_free((xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret);
done:
remoteDriverUnlock(priv);
return rv;