1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-02-02 13:47:13 +03:00

Centralize error reporting for URI parsing/formatting problems

Move error reporting out of the callers, into virURIParse
and virURIFormat, to get consistency.

* include/libvirt/virterror.h, src/util/virterror.c: Add VIR_FROM_URI
* src/util/viruri.c, src/util/viruri.h: Add error reporting
* src/esx/esx_driver.c, src/libvirt.c, src/libxl/libxl_driver.c,
  src/lxc/lxc_driver.c, src/openvz/openvz_driver.c,
  src/qemu/qemu_driver.c, src/qemu/qemu_migration.c,
  src/remote/remote_driver.c, src/uml/uml_driver.c,
  src/vbox/vbox_tmpl.c, src/vmx/vmx.c, src/xen/xen_driver.c,
  src/xen/xend_internal.c, tests/viruritest.c: Remove error
  reporting

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-03-20 12:16:54 +00:00
parent 94410848e1
commit 1f66c18f79
18 changed files with 63 additions and 88 deletions

View File

@ -85,6 +85,7 @@ typedef enum {
VIR_FROM_LOCKING = 42, /* Error from lock manager */ VIR_FROM_LOCKING = 42, /* Error from lock manager */
VIR_FROM_HYPERV = 43, /* Error from Hyper-V driver */ VIR_FROM_HYPERV = 43, /* Error from Hyper-V driver */
VIR_FROM_CAPABILITIES = 44, /* Error from capabilities */ VIR_FROM_CAPABILITIES = 44, /* Error from capabilities */
VIR_FROM_URI = 45, /* Error from URI handling */
} virErrorDomain; } virErrorDomain;

View File

@ -3977,12 +3977,8 @@ esxDomainMigratePerform(virDomainPtr domain,
} }
/* Parse migration URI */ /* Parse migration URI */
parsedUri = virURIParse(uri); if (!(parsedUri = virURIParse(uri)))
if (parsedUri == NULL) {
virReportOOMError();
return -1; return -1;
}
if (parsedUri->scheme == NULL || STRCASENEQ(parsedUri->scheme, "vpxmigr")) { if (parsedUri->scheme == NULL || STRCASENEQ(parsedUri->scheme, "vpxmigr")) {
ESX_ERROR(VIR_ERR_INVALID_ARG, "%s", ESX_ERROR(VIR_ERR_INVALID_ARG, "%s",

View File

@ -1165,11 +1165,7 @@ do_open (const char *name,
virConnectOpenResolveURIAlias(conf, name, &alias) < 0) virConnectOpenResolveURIAlias(conf, name, &alias) < 0)
goto failed; goto failed;
ret->uri = virURIParse (alias ? alias : name); if (!(ret->uri = virURIParse (alias ? alias : name))) {
if (!ret->uri) {
virLibConnError(VIR_ERR_INVALID_ARG,
_("could not parse connection URI %s"),
alias ? alias : name);
VIR_FREE(alias); VIR_FREE(alias);
goto failed; goto failed;
} }
@ -1770,11 +1766,9 @@ virConnectGetURI (virConnectPtr conn)
return NULL; return NULL;
} }
name = virURIFormat(conn->uri); if (!(name = virURIFormat(conn->uri)))
if (!name) {
virReportOOMError();
goto error; goto error;
}
return name; return name;
error: error:
@ -5061,9 +5055,7 @@ virDomainMigratePeer2Peer (virDomainPtr domain,
return -1; return -1;
} }
tempuri = virURIParse(dconnuri); if (!(tempuri = virURIParse(dconnuri))) {
if (!tempuri) {
virLibConnError(VIR_ERR_INVALID_ARG, __FUNCTION__);
virDispatchError(domain->conn); virDispatchError(domain->conn);
return -1; return -1;
} }

View File

@ -1044,11 +1044,8 @@ libxlOpen(virConnectPtr conn,
if (libxl_driver == NULL) if (libxl_driver == NULL)
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
conn->uri = virURIParse("xen:///"); if (!(conn->uri = virURIParse("xen:///")))
if (!conn->uri) {
virReportOOMError();
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
}
} else { } else {
/* Only xen scheme */ /* Only xen scheme */
if (conn->uri->scheme == NULL || STRNEQ(conn->uri->scheme, "xen")) if (conn->uri->scheme == NULL || STRNEQ(conn->uri->scheme, "xen"))

View File

@ -140,11 +140,8 @@ static virDrvOpenStatus lxcOpen(virConnectPtr conn,
if (lxc_driver == NULL) if (lxc_driver == NULL)
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
conn->uri = virURIParse("lxc:///"); if (!(conn->uri = virURIParse("lxc:///")))
if (!conn->uri) {
virReportOOMError();
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
}
} else { } else {
if (conn->uri->scheme == NULL || if (conn->uri->scheme == NULL ||
STRNEQ(conn->uri->scheme, "lxc")) STRNEQ(conn->uri->scheme, "lxc"))

View File

@ -1336,11 +1336,8 @@ static virDrvOpenStatus openvzOpen(virConnectPtr conn,
if (access("/proc/vz", W_OK) < 0) if (access("/proc/vz", W_OK) < 0)
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
conn->uri = virURIParse("openvz:///system"); if (!(conn->uri = virURIParse("openvz:///system")))
if (conn->uri == NULL) {
virReportOOMError();
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
}
} else { } else {
/* If scheme isn't 'openvz', then its for another driver */ /* If scheme isn't 'openvz', then its for another driver */
if (conn->uri->scheme == NULL || if (conn->uri->scheme == NULL ||

View File

@ -860,13 +860,10 @@ static virDrvOpenStatus qemudOpen(virConnectPtr conn,
if (qemu_driver == NULL) if (qemu_driver == NULL)
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
conn->uri = virURIParse(qemu_driver->privileged ? if (!(conn->uri = virURIParse(qemu_driver->privileged ?
"qemu:///system" : "qemu:///system" :
"qemu:///session"); "qemu:///session")))
if (!conn->uri) {
virReportOOMError();
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
}
} else { } else {
/* If URI isn't 'qemu' its definitely not for us */ /* If URI isn't 'qemu' its definitely not for us */
if (conn->uri->scheme == NULL || if (conn->uri->scheme == NULL ||

View File

@ -1936,11 +1936,8 @@ static int doNativeMigrate(struct qemud_driver *driver,
} else { } else {
uribits = virURIParse(uri); uribits = virURIParse(uri);
} }
if (!uribits) { if (!uribits)
qemuReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot parse URI %s"), uri);
return -1; return -1;
}
if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATE_QEMU_FD)) if (qemuCapsGet(priv->qemuCaps, QEMU_CAPS_MIGRATE_QEMU_FD))
spec.destType = MIGRATION_DEST_CONNECT_HOST; spec.destType = MIGRATION_DEST_CONNECT_HOST;

View File

@ -485,7 +485,8 @@ doRemoteOpen (virConnectPtr conn,
(STREQ(conn->uri->scheme, "remote") || (STREQ(conn->uri->scheme, "remote") ||
STRPREFIX(conn->uri->scheme, "remote+"))) { STRPREFIX(conn->uri->scheme, "remote+"))) {
/* Allow remote serve to probe */ /* Allow remote serve to probe */
name = strdup(""); if (!(name = strdup("")))
goto out_of_memory;
} else { } else {
virURI tmpuri = { virURI tmpuri = {
.scheme = conn->uri->scheme, .scheme = conn->uri->scheme,
@ -515,6 +516,9 @@ doRemoteOpen (virConnectPtr conn,
/* Restore transport scheme */ /* Restore transport scheme */
if (transport_str) if (transport_str)
transport_str[-1] = '+'; transport_str[-1] = '+';
if (!name)
goto failed;
} }
} }
@ -522,12 +526,8 @@ doRemoteOpen (virConnectPtr conn,
vars = NULL; vars = NULL;
} else { } else {
/* Probe URI server side */ /* Probe URI server side */
name = strdup(""); if (!(name = strdup("")))
} goto out_of_memory;
if (!name) {
virReportOOMError();
goto failed;
} }
VIR_DEBUG("proceeding with name = %s", name); VIR_DEBUG("proceeding with name = %s", name);
@ -720,10 +720,8 @@ doRemoteOpen (virConnectPtr conn,
VIR_DEBUG("Auto-probed URI is %s", uriret.uri); VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
conn->uri = virURIParse(uriret.uri); conn->uri = virURIParse(uriret.uri);
VIR_FREE(uriret.uri); VIR_FREE(uriret.uri);
if (!conn->uri) { if (!conn->uri)
virReportOOMError();
goto failed; goto failed;
}
} }
if (!(priv->domainEventState = virDomainEventStateNew())) if (!(priv->domainEventState = virDomainEventStateNew()))

View File

@ -1139,13 +1139,10 @@ static virDrvOpenStatus umlOpen(virConnectPtr conn,
if (uml_driver == NULL) if (uml_driver == NULL)
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
conn->uri = virURIParse(uml_driver->privileged ? if (!(conn->uri = virURIParse(uml_driver->privileged ?
"uml:///system" : "uml:///system" :
"uml:///session"); "uml:///session")))
if (!conn->uri) {
virReportOOMError();
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
}
} else { } else {
if (conn->uri->scheme == NULL || if (conn->uri->scheme == NULL ||
STRNEQ (conn->uri->scheme, "uml")) STRNEQ (conn->uri->scheme, "uml"))

View File

@ -178,6 +178,9 @@ static const char *virErrorDomainName(virErrorDomain domain) {
case VIR_FROM_CAPABILITIES: case VIR_FROM_CAPABILITIES:
dom = "Capabilities "; dom = "Capabilities ";
break; break;
case VIR_FROM_URI:
dom = "URI ";
break;
} }
return(dom); return(dom);
} }

View File

@ -12,6 +12,14 @@
#include "memory.h" #include "memory.h"
#include "util.h" #include "util.h"
#include "virterror_internal.h"
#define VIR_FROM_THIS VIR_FROM_URI
#define virURIReportError(code, ...) \
virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \
__FUNCTION__, __LINE__, __VA_ARGS__)
/** /**
* virURIParse: * virURIParse:
@ -30,9 +38,15 @@ virURIParse(const char *uri)
{ {
virURIPtr ret = xmlParseURI(uri); virURIPtr ret = xmlParseURI(uri);
if (!ret) {
/* libxml2 does not tell us what failed. Grr :-( */
virURIReportError(VIR_ERR_INTERNAL_ERROR,
"Unable to parse URI %s", uri);
return NULL;
}
/* First check: does it even make sense to jump inside */ /* First check: does it even make sense to jump inside */
if (ret != NULL && if (ret->server != NULL &&
ret->server != NULL &&
ret->server[0] == '[') { ret->server[0] == '[') {
size_t length = strlen(ret->server); size_t length = strlen(ret->server);
@ -70,8 +84,7 @@ virURIFormat(virURIPtr uri)
char *ret; char *ret;
/* First check: does it make sense to do anything */ /* First check: does it make sense to do anything */
if (uri != NULL && if (uri->server != NULL &&
uri->server != NULL &&
strchr(uri->server, ':') != NULL) { strchr(uri->server, ':') != NULL) {
backupserver = uri->server; backupserver = uri->server;
@ -82,7 +95,12 @@ virURIFormat(virURIPtr uri)
} }
ret = (char *) xmlSaveUri(uri); ret = (char *) xmlSaveUri(uri);
if (!ret) {
virReportOOMError();
goto cleanup;
}
cleanup:
/* Put the fixed version back */ /* Put the fixed version back */
if (tmpserver) { if (tmpserver) {
uri->server = backupserver; uri->server = backupserver;

View File

@ -16,8 +16,10 @@
typedef xmlURI virURI; typedef xmlURI virURI;
typedef xmlURIPtr virURIPtr; typedef xmlURIPtr virURIPtr;
virURIPtr virURIParse(const char *uri); virURIPtr virURIParse(const char *uri)
char *virURIFormat(virURIPtr uri); ATTRIBUTE_NONNULL(1);
char *virURIFormat(virURIPtr uri)
ATTRIBUTE_NONNULL(1);
void virURIFree(virURIPtr uri); void virURIFree(virURIPtr uri);

View File

@ -980,13 +980,9 @@ static virDrvOpenStatus vboxOpen(virConnectPtr conn,
virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR); virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
if (conn->uri == NULL) { if (conn->uri == NULL &&
conn->uri = virURIParse(uid ? "vbox:///session" : "vbox:///system"); !(conn->uri = virURIParse(uid ? "vbox:///session" : "vbox:///system")))
if (conn->uri == NULL) { return VIR_DRV_OPEN_ERROR;
virReportOOMError();
return VIR_DRV_OPEN_ERROR;
}
}
if (conn->uri->scheme == NULL || if (conn->uri->scheme == NULL ||
STRNEQ (conn->uri->scheme, "vbox")) STRNEQ (conn->uri->scheme, "vbox"))

View File

@ -2617,12 +2617,8 @@ virVMXParseSerial(virVMXContext *ctx, virConfPtr conf, int port,
(*def)->target.port = port; (*def)->target.port = port;
(*def)->source.type = VIR_DOMAIN_CHR_TYPE_TCP; (*def)->source.type = VIR_DOMAIN_CHR_TYPE_TCP;
parsedUri = virURIParse(fileName); if (!(parsedUri = virURIParse(fileName)))
if (parsedUri == NULL) {
virReportOOMError();
goto cleanup; goto cleanup;
}
if (parsedUri->port == 0) { if (parsedUri->port == 0) {
VMX_ERROR(VIR_ERR_INTERNAL_ERROR, VMX_ERROR(VIR_ERR_INTERNAL_ERROR,

View File

@ -270,11 +270,8 @@ xenUnifiedOpen (virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags)
if (!xenUnifiedProbe()) if (!xenUnifiedProbe())
return VIR_DRV_OPEN_DECLINED; return VIR_DRV_OPEN_DECLINED;
conn->uri = virURIParse("xen:///"); if (!(conn->uri = virURIParse("xen:///")))
if (!conn->uri) {
virReportOOMError();
return VIR_DRV_OPEN_ERROR; return VIR_DRV_OPEN_ERROR;
}
} else { } else {
if (conn->uri->scheme) { if (conn->uri->scheme) {
/* Decline any scheme which isn't "xen://" or "http://". */ /* Decline any scheme which isn't "xen://" or "http://". */

View File

@ -3224,12 +3224,10 @@ xenDaemonDomainMigratePerform (virDomainPtr domain,
* "hostname", "hostname:port" or "xenmigr://hostname[:port]/". * "hostname", "hostname:port" or "xenmigr://hostname[:port]/".
*/ */
if (strstr (uri, "//")) { /* Full URI. */ if (strstr (uri, "//")) { /* Full URI. */
virURIPtr uriptr = virURIParse (uri); virURIPtr uriptr;
if (!uriptr) { if (!(uriptr = virURIParse (uri)))
virXendError(VIR_ERR_INVALID_ARG,
"%s", _("xenDaemonDomainMigrate: invalid URI"));
return -1; return -1;
}
if (uriptr->scheme && STRCASENEQ (uriptr->scheme, "xenmigr")) { if (uriptr->scheme && STRCASENEQ (uriptr->scheme, "xenmigr")) {
virXendError(VIR_ERR_INVALID_ARG, virXendError(VIR_ERR_INVALID_ARG,
"%s", _("xenDaemonDomainMigrate: only xenmigr://" "%s", _("xenDaemonDomainMigrate: only xenmigr://"

View File

@ -50,15 +50,11 @@ static int testURIParse(const void *args)
const struct URIParseData *data = args; const struct URIParseData *data = args;
char *uristr; char *uristr;
if (!(uri = virURIParse(data->uri))) { if (!(uri = virURIParse(data->uri)))
virReportOOMError();
goto cleanup; goto cleanup;
}
if (!(uristr = virURIFormat(uri))) { if (!(uristr = virURIFormat(uri)))
virReportOOMError();
goto cleanup; goto cleanup;
}
if (!STREQ(uristr, data->uri)) { if (!STREQ(uristr, data->uri)) {
VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'", VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'",