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