提交 6758ad4f 编写于 作者: P Peter Krempa

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.
上级 16d3ab86
......@@ -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:
*
......@@ -364,7 +387,7 @@ static void remoteClientCloseFunc(virNetClientPtr client ATTRIBUTE_UNUSED,
* - xxx:/// -> UNIX domain socket
*/
static int
doRemoteOpen (virConnectPtr conn,
doRemoteOpen(virConnectPtr conn,
struct private_data *priv,
virConnectAuthPtr auth ATTRIBUTE_UNUSED,
unsigned int flags)
......@@ -394,7 +417,7 @@ doRemoteOpen (virConnectPtr conn,
transport = trans_unix;
} else {
transport_str = get_transport_from_scheme (conn->uri->scheme);
transport_str = get_transport_from_scheme(conn->uri->scheme);
if (!transport_str) {
if (conn->uri->server)
......@@ -402,9 +425,9 @@ doRemoteOpen (virConnectPtr conn,
else
transport = trans_unix;
} else {
if (STRCASEEQ (transport_str, "tls"))
if (STRCASEEQ(transport_str, "tls"))
transport = trans_tls;
else if (STRCASEEQ (transport_str, "unix")) {
else if (STRCASEEQ(transport_str, "unix")) {
if (conn->uri->server) {
virReportError(VIR_ERR_INVALID_ARG,
_("using unix socket and remote "
......@@ -414,11 +437,11 @@ doRemoteOpen (virConnectPtr conn,
} else {
transport = trans_unix;
}
} else if (STRCASEEQ (transport_str, "ssh"))
} else if (STRCASEEQ(transport_str, "ssh"))
transport = trans_ssh;
else if (STRCASEEQ (transport_str, "ext"))
else if (STRCASEEQ(transport_str, "ext"))
transport = trans_ext;
else if (STRCASEEQ (transport_str, "tcp"))
else if (STRCASEEQ(transport_str, "tcp"))
transport = trans_tcp;
else {
virReportError(VIR_ERR_INVALID_ARG, "%s",
......@@ -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,
......@@ -547,7 +541,7 @@ doRemoteOpen (virConnectPtr conn,
/* Evil, blank out transport scheme temporarily */
if (transport_str) {
assert (transport_str[-1] == '+');
assert(transport_str[-1] == '+');
transport_str[-1] = '\0';
}
......@@ -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,
......@@ -743,7 +735,7 @@ doRemoteOpen (virConnectPtr conn,
remote_open_args args = { &name, flags };
VIR_DEBUG("Trying to open URI %s", name);
if (call (conn, priv, 0, REMOTE_PROC_OPEN,
if (call(conn, priv, 0, REMOTE_PROC_OPEN,
(xdrproc_t) xdr_remote_open_args, (char *) &args,
(xdrproc_t) xdr_void, (char *) NULL) == -1)
goto failed;
......@@ -754,8 +746,8 @@ doRemoteOpen (virConnectPtr conn,
remote_get_uri_ret uriret;
VIR_DEBUG("Trying to query remote URI");
memset (&uriret, 0, sizeof(uriret));
if (call (conn, priv, 0,
memset(&uriret, 0, sizeof(uriret));
if (call(conn, priv, 0,
REMOTE_PROC_GET_URI,
(xdrproc_t) xdr_void, (char *) NULL,
(xdrproc_t) xdr_remote_get_uri_ret, (char *) &uriret) < 0)
......@@ -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)
......@@ -924,21 +918,21 @@ remoteOpen (virConnectPtr conn,
/* In a string "driver+transport" return a pointer to "transport". */
static char *
get_transport_from_scheme (char *scheme)
get_transport_from_scheme(char *scheme)
{
char *p = strchr (scheme, '+');
return p ? p+1 : 0;
char *p = strchr(scheme, '+');
return p ? p + 1 : NULL;
}
/*----------------------------------------------------------------------*/
static int
doRemoteClose (virConnectPtr conn, struct private_data *priv)
doRemoteClose(virConnectPtr conn, struct private_data *priv)
{
int ret = 0;
if (call (conn, priv, 0, REMOTE_PROC_CLOSE,
if (call(conn, priv, 0, REMOTE_PROC_CLOSE,
(xdrproc_t) xdr_void, (char *) NULL,
(xdrproc_t) xdr_void, (char *) NULL) == -1)
ret = -1;
......@@ -965,7 +959,7 @@ doRemoteClose (virConnectPtr conn, struct private_data *priv)
}
static int
remoteClose (virConnectPtr conn)
remoteClose(virConnectPtr conn)
{
int ret = 0;
struct private_data *priv = conn->privateData;
......@@ -995,7 +989,7 @@ remoteClose (virConnectPtr conn)
* http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
*/
static const char *
remoteType (virConnectPtr conn)
remoteType(virConnectPtr conn)
{
char *rv = NULL;
remote_get_type_ret ret;
......@@ -1009,8 +1003,8 @@ remoteType (virConnectPtr conn)
goto done;
}
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_GET_TYPE,
memset(&ret, 0, sizeof(ret));
if (call(conn, priv, 0, REMOTE_PROC_GET_TYPE,
(xdrproc_t) xdr_void, (char *) NULL,
(xdrproc_t) xdr_remote_get_type_ret, (char *) &ret) == -1)
goto done;
......@@ -1030,8 +1024,8 @@ static int remoteIsSecure(virConnectPtr conn)
remote_is_secure_ret ret;
remoteDriverLock(priv);
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_IS_SECURE,
memset(&ret, 0, sizeof(ret));
if (call(conn, priv, 0, REMOTE_PROC_IS_SECURE,
(xdrproc_t) xdr_void, (char *) NULL,
(xdrproc_t) xdr_remote_is_secure_ret, (char *) &ret) == -1)
goto done;
......@@ -1059,8 +1053,8 @@ static int remoteIsEncrypted(virConnectPtr conn)
remote_is_secure_ret ret;
remoteDriverLock(priv);
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_IS_SECURE,
memset(&ret, 0, sizeof(ret));
if (call(conn, priv, 0, REMOTE_PROC_IS_SECURE,
(xdrproc_t) xdr_void, (char *) NULL,
(xdrproc_t) xdr_remote_is_secure_ret, (char *) &ret) == -1)
goto done;
......@@ -1084,7 +1078,7 @@ done:
}
static int
remoteNodeGetCPUStats (virConnectPtr conn,
remoteNodeGetCPUStats(virConnectPtr conn,
int cpuNum,
virNodeCPUStatsPtr params, int *nparams,
unsigned int flags)
......@@ -1101,8 +1095,8 @@ remoteNodeGetCPUStats (virConnectPtr conn,
args.cpuNum = cpuNum;
args.flags = flags;
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_NODE_GET_CPU_STATS,
memset(&ret, 0, sizeof(ret));
if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_CPU_STATS,
(xdrproc_t) xdr_remote_node_get_cpu_stats_args,
(char *) &args,
(xdrproc_t) xdr_remote_node_get_cpu_stats_ret,
......@@ -1142,17 +1136,17 @@ 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;
}
static int
remoteNodeGetMemoryStats (virConnectPtr conn,
remoteNodeGetMemoryStats(virConnectPtr conn,
int cellNum,
virNodeMemoryStatsPtr params, int *nparams,
virNodeMemoryStatsPtr params,
int *nparams,
unsigned int flags)
{
int rv = -1;
......@@ -1168,7 +1162,7 @@ remoteNodeGetMemoryStats (virConnectPtr conn,
args.flags = flags;
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_NODE_GET_MEMORY_STATS,
if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_MEMORY_STATS,
(xdrproc_t) xdr_remote_node_get_memory_stats_args, (char *) &args,
(xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret) == -1)
goto done;
......@@ -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;
......@@ -1237,8 +1230,8 @@ remoteNodeGetCellsFreeMemory(virConnectPtr conn,
args.startCell = startCell;
args.maxcells = maxCells;
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_NODE_GET_CELLS_FREE_MEMORY,
memset(&ret, 0, sizeof(ret));
if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_CELLS_FREE_MEMORY,
(xdrproc_t) xdr_remote_node_get_cells_free_memory_args, (char *)&args,
(xdrproc_t) xdr_remote_node_get_cells_free_memory_ret, (char *)&ret) == -1)
goto done;
......@@ -1256,7 +1249,7 @@ done:
}
static int
remoteListDomains (virConnectPtr conn, int *ids, int maxids)
remoteListDomains(virConnectPtr conn, int *ids, int maxids)
{
int rv = -1;
int i;
......@@ -1274,8 +1267,8 @@ remoteListDomains (virConnectPtr conn, int *ids, int maxids)
}
args.maxids = maxids;
memset (&ret, 0, sizeof(ret));
if (call (conn, priv, 0, REMOTE_PROC_LIST_DOMAINS,
memset(&ret, 0, sizeof(ret));
if (call(conn, priv, 0, REMOTE_PROC_LIST_DOMAINS,
(xdrproc_t) xdr_remote_list_domains_args, (char *) &args,
(xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret) == -1)
goto done;
......@@ -1293,7 +1286,7 @@ remoteListDomains (virConnectPtr conn, int *ids, int maxids)
rv = ret.ids.ids_len;
cleanup:
xdr_free ((xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret);
xdr_free((xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret);
done:
remoteDriverUnlock(priv);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册