Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/libvirt-admin.c | 66 ++++++++++++++++++++++++++++----------------------
src/libvirt.c | 70 +++++++++++++++++++++++++++++------------------------
2 files changed, 76 insertions(+), 60 deletions(-)
diff --git a/src/libvirt-admin.c b/src/libvirt-admin.c
index f07cb10..9d1a219 100644
--- a/src/libvirt-admin.c
+++ b/src/libvirt-admin.c
@@ -158,35 +158,32 @@ getSocketPath(virURIPtr uri)
goto cleanup;
}
-static const char *
-virAdmGetDefaultURI(virConfPtr conf)
+static int
+virAdmGetDefaultURI(virConfPtr conf, char **uristr)
{
- virConfValuePtr value = NULL;
- const char *uristr = NULL;
-
- uristr = virGetEnvAllowSUID("LIBVIRT_ADMIN_DEFAULT_URI");
- if (uristr && *uristr) {
- VIR_DEBUG("Using LIBVIRT_ADMIN_DEFAULT_URI '%s'", uristr);
- } else if ((value = virConfGetValue(conf, "admin_uri_default"))) {
- if (value->type != VIR_CONF_STRING) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("Expected a string for 'admin_uri_default'
config "
- "parameter"));
+ const char *defname = virGetEnvAllowSUID("LIBVIRT_ADMIN_DEFAULT_URI");
+ if (defname && *defname) {
+ if (VIR_STRDUP(*uristr, defname) < 0)
return NULL;
- }
-
- VIR_DEBUG("Using config file uri '%s'", value->str);
- uristr = value->str;
+ VIR_DEBUG("Using LIBVIRT_ADMIN_DEFAULT_URI '%s'", *uristr);
} else {
- /* Since we can't probe connecting via any hypervisor driver as libvirt
- * does, if no explicit URI was given and neither the environment
- * variable, nor the configuration parameter had previously been set,
- * we set the default admin server URI to 'libvirtd://system'.
- */
- uristr = "libvirtd:///system";
+ if (virConfGetValueString(conf, "admin_uri_default", uristr) < 0)
+ return -1;
+
+ if (*uristr) {
+ VIR_DEBUG("Using config file uri '%s'", *uristr);
+ } else {
+ /* Since we can't probe connecting via any hypervisor driver as libvirt
+ * does, if no explicit URI was given and neither the environment
+ * variable, nor the configuration parameter had previously been set,
+ * we set the default admin server URI to 'libvirtd://system'.
+ */
+ if (VIR_STRDUP(*uristr, "libvirtd:///system") < 0)
+ return -1;
+ }
}
- return uristr;
+ return 0;
}
/**
@@ -206,6 +203,7 @@ virAdmConnectOpen(const char *name, unsigned int flags)
char *alias = NULL;
virAdmConnectPtr conn = NULL;
virConfPtr conf = NULL;
+ char *uristr = NULL;
if (virAdmInitialize() < 0)
goto error;
@@ -219,14 +217,24 @@ virAdmConnectOpen(const char *name, unsigned int flags)
if (virConfLoadConfig(&conf, "libvirt-admin.conf") < 0)
goto error;
- if (!name && !(name = virAdmGetDefaultURI(conf)))
- goto error;
+ if (name) {
+ if (VIR_STRDUP(uristr, name) < 0)
+ goto error;
+ } else {
+ if (virAdmGetDefaultURI(conf, &uristr) < 0)
+ goto error;
+ }
if ((!(flags & VIR_CONNECT_NO_ALIASES) &&
- virURIResolveAlias(conf, name, &alias) < 0))
+ virURIResolveAlias(conf, uristr, &alias) < 0))
goto error;
- if (!(conn->uri = virURIParse(alias ? alias : name)))
+ if (alias) {
+ VIR_FREE(uristr);
+ uristr = alias;
+ }
+
+ if (!(conn->uri = virURIParse(uristr)))
goto error;
if (!(sock_path = getSocketPath(conn->uri)))
@@ -242,7 +250,7 @@ virAdmConnectOpen(const char *name, unsigned int flags)
cleanup:
VIR_FREE(sock_path);
- VIR_FREE(alias);
+ VIR_FREE(uristr);
virConfFree(conf);
return conn;
diff --git a/src/libvirt.c b/src/libvirt.c
index a5e0e41..68c8317 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -903,22 +903,20 @@ virGetVersion(unsigned long *libVer, const char *type
ATTRIBUTE_UNUSED,
static int
virConnectGetDefaultURI(virConfPtr conf,
- const char **name)
+ char **name)
{
int ret = -1;
- virConfValuePtr value = NULL;
const char *defname = virGetEnvBlockSUID("LIBVIRT_DEFAULT_URI");
if (defname && *defname) {
VIR_DEBUG("Using LIBVIRT_DEFAULT_URI '%s'", defname);
- *name = defname;
- } else if ((value = virConfGetValue(conf, "uri_default"))) {
- if (value->type != VIR_CONF_STRING) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("Expected a string for 'uri_default' config
parameter"));
+ if (VIR_STRDUP(*name, defname) < 0)
goto cleanup;
- }
- VIR_DEBUG("Using config file uri '%s'", value->str);
- *name = value->str;
+ } else {
+ if (virConfGetValueString(conf, "uri_default", name) < 0)
+ goto cleanup;
+
+ if (*name)
+ VIR_DEBUG("Using config file uri '%s'", *name);
}
ret = 0;
@@ -965,6 +963,7 @@ virConnectOpenInternal(const char *name,
int res;
virConnectPtr ret;
virConfPtr conf = NULL;
+ char *uristr = NULL;
ret = virGetConnect();
if (ret == NULL)
@@ -982,54 +981,61 @@ virConnectOpenInternal(const char *name,
goto failed;
}
+ /* Convert xen -> xen:/// for back compat */
+ if (name && STRCASEEQ(name, "xen"))
+ name = "xen:///";
+
+ /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
+ * former. This allows URIs such as xen://localhost to work.
+ */
+ if (name && STREQ(name, "xen://"))
+ name = "xen:///";
+
/*
* If no URI is passed, then check for an environment string if not
* available probe the compiled in drivers to find a default hypervisor
* if detectable.
*/
- if (!name &&
- virConnectGetDefaultURI(conf, &name) < 0)
- goto failed;
-
if (name) {
- char *alias = NULL;
- /* Convert xen -> xen:/// for back compat */
- if (STRCASEEQ(name, "xen"))
- name = "xen:///";
+ if (VIR_STRDUP(uristr, name) < 0)
+ goto failed;
+ } else {
+ if (virConnectGetDefaultURI(conf, &uristr) < 0)
+ goto failed;
+ }
- /* Convert xen:// -> xen:/// because xmlParseURI cannot parse the
- * former. This allows URIs such as xen://localhost to work.
- */
- if (STREQ(name, "xen://"))
- name = "xen:///";
+ if (uristr) {
+ char *alias = NULL;
if (!(flags & VIR_CONNECT_NO_ALIASES) &&
- virURIResolveAlias(conf, name, &alias) < 0)
+ virURIResolveAlias(conf, uristr, &alias) < 0)
goto failed;
- if (!(ret->uri = virURIParse(alias ? alias : name))) {
+ if (alias) {
+ VIR_FREE(uristr);
+ uristr = alias;
+ }
+
+ if (!(ret->uri = virURIParse(uristr))) {
VIR_FREE(alias);
goto failed;
}
- VIR_DEBUG("name \"%s\" to URI components:\n"
+ VIR_DEBUG("Split \"%s\" to URI components:\n"
" scheme %s\n"
" server %s\n"
" user %s\n"
" port %d\n"
" path %s",
- alias ? alias : name,
+ uristr,
NULLSTR(ret->uri->scheme), NULLSTR(ret->uri->server),
NULLSTR(ret->uri->user), ret->uri->port,
NULLSTR(ret->uri->path));
- if (virConnectCheckURIMissingSlash(alias ? alias : name,
+ if (virConnectCheckURIMissingSlash(uristr,
ret->uri) < 0) {
- VIR_FREE(alias);
goto failed;
}
-
- VIR_FREE(alias);
} else {
VIR_DEBUG("no name, allowing driver auto-select");
}
@@ -1114,10 +1120,12 @@ virConnectOpenInternal(const char *name,
}
virConfFree(conf);
+ VIR_FREE(uristr);
return ret;
failed:
+ VIR_FREE(uristr);
virConfFree(conf);
virObjectUnref(ret);
--
2.7.4