* include/virterror.h src/virterror.c src/xend_internal.c: more work

plugging in the error system in the code.
Daniel
This commit is contained in:
Daniel Veillard 2006-02-27 19:56:23 +00:00
parent 596b601021
commit 739dfb531d
5 changed files with 122 additions and 21 deletions

View File

@ -1,3 +1,8 @@
Mon Feb 27 14:56:57 EST 2006 Daniel Veillard <veillard@redhat.com>
* include/virterror.h src/virterror.c src/xend_internal.c: more work
plugging in the error system in the code.
Mon Feb 27 17:25:48 CET 2006 Daniel Veillard <veillard@redhat.com> Mon Feb 27 17:25:48 CET 2006 Daniel Veillard <veillard@redhat.com>
* TODO: updated * TODO: updated

View File

@ -40,7 +40,8 @@ typedef enum {
VIR_FROM_NONE = 0, VIR_FROM_NONE = 0,
VIR_FROM_XEN, /* Error at Xen hypervisor layer */ VIR_FROM_XEN, /* Error at Xen hypervisor layer */
VIR_FROM_XEND, /* Error at connection with xend daemon */ VIR_FROM_XEND, /* Error at connection with xend daemon */
VIR_FROM_DOM /* Error when operating on a domain */ VIR_FROM_SEXPR, /* Error in the S-Epression code */
VIR_FROM_DOM, /* Error when operating on a domain */
} virErrorDomain; } virErrorDomain;
@ -76,11 +77,15 @@ typedef enum {
VIR_ERR_INTERNAL_ERROR, /* internal error */ VIR_ERR_INTERNAL_ERROR, /* internal error */
VIR_ERR_NO_MEMORY, /* memory allocation failure */ VIR_ERR_NO_MEMORY, /* memory allocation failure */
VIR_ERR_NO_SUPPORT, /* no support for this connection */ VIR_ERR_NO_SUPPORT, /* no support for this connection */
VIR_ERR_UNKNOWN_HOST,/* could not resolve hostname */
VIR_ERR_NO_CONNECT, /* can't connect to hypervisor */ VIR_ERR_NO_CONNECT, /* can't connect to hypervisor */
VIR_ERR_INVALID_CONN,/* invalid connection object */ VIR_ERR_INVALID_CONN,/* invalid connection object */
VIR_ERR_INVALID_DOMAIN,/* invalid domain object */ VIR_ERR_INVALID_DOMAIN,/* invalid domain object */
VIR_ERR_INVALID_ARG,/* invalid function argument */ VIR_ERR_INVALID_ARG,/* invalid function argument */
VIR_ERR_OPERATION_FAILED,/* a command to hypervisor failed */ VIR_ERR_OPERATION_FAILED,/* a command to hypervisor failed */
VIR_ERR_GET_FAILED,/* a HTTP GET command to failed */
VIR_ERR_POST_FAILED,/* a HTTP POST command to failed */
VIR_ERR_HTTP_ERROR,/* unexpected HTTP error code */
} virErrorNumber; } virErrorNumber;
/** /**

View File

@ -40,7 +40,8 @@ typedef enum {
VIR_FROM_NONE = 0, VIR_FROM_NONE = 0,
VIR_FROM_XEN, /* Error at Xen hypervisor layer */ VIR_FROM_XEN, /* Error at Xen hypervisor layer */
VIR_FROM_XEND, /* Error at connection with xend daemon */ VIR_FROM_XEND, /* Error at connection with xend daemon */
VIR_FROM_DOM /* Error when operating on a domain */ VIR_FROM_SEXPR, /* Error in the S-Epression code */
VIR_FROM_DOM, /* Error when operating on a domain */
} virErrorDomain; } virErrorDomain;
@ -76,11 +77,15 @@ typedef enum {
VIR_ERR_INTERNAL_ERROR, /* internal error */ VIR_ERR_INTERNAL_ERROR, /* internal error */
VIR_ERR_NO_MEMORY, /* memory allocation failure */ VIR_ERR_NO_MEMORY, /* memory allocation failure */
VIR_ERR_NO_SUPPORT, /* no support for this connection */ VIR_ERR_NO_SUPPORT, /* no support for this connection */
VIR_ERR_UNKNOWN_HOST,/* could not resolve hostname */
VIR_ERR_NO_CONNECT, /* can't connect to hypervisor */ VIR_ERR_NO_CONNECT, /* can't connect to hypervisor */
VIR_ERR_INVALID_CONN,/* invalid connection object */ VIR_ERR_INVALID_CONN,/* invalid connection object */
VIR_ERR_INVALID_DOMAIN,/* invalid domain object */ VIR_ERR_INVALID_DOMAIN,/* invalid domain object */
VIR_ERR_INVALID_ARG,/* invalid function argument */ VIR_ERR_INVALID_ARG,/* invalid function argument */
VIR_ERR_OPERATION_FAILED,/* a command to hypervisor failed */ VIR_ERR_OPERATION_FAILED,/* a command to hypervisor failed */
VIR_ERR_GET_FAILED,/* a HTTP GET command to failed */
VIR_ERR_POST_FAILED,/* a HTTP POST command to failed */
VIR_ERR_HTTP_ERROR,/* unexpected HTTP error code */
} virErrorNumber; } virErrorNumber;
/** /**

View File

@ -385,6 +385,30 @@ __virErrorMsg(virErrorNumber error, const char *info) {
case VIR_ERR_INVALID_ARG: case VIR_ERR_INVALID_ARG:
errmsg = "invalid domain pointer in"; errmsg = "invalid domain pointer in";
break; break;
case VIR_ERR_OPERATION_FAILED:
if (info != NULL)
errmsg = "operation failed: %s";
else
errmsg = "operation failed";
break;
case VIR_ERR_GET_FAILED:
if (info != NULL)
errmsg = "GET operation failed: %s";
else
errmsg = "GET operation failed";
break;
case VIR_ERR_POST_FAILED:
if (info != NULL)
errmsg = "POST operation failed: %s";
else
errmsg = "POST operation failed";
break;
case VIR_ERR_HTTP_ERROR:
errmsg = "got unknown HTTP error code %d";
break;
case VIR_ERR_UNKNOWN_HOST:
errmsg = "Unknown host %s";
break;
} }
return(errmsg); return(errmsg);
} }

View File

@ -78,6 +78,27 @@ virXendError(virConnectPtr conn, virErrorNumber error, const char *info) {
errmsg, info, NULL, 0, 0, errmsg, info); errmsg, info, NULL, 0, 0, errmsg, info);
} }
/**
* virXendErrorInt:
* @conn: the connection if available
* @error: the error noumber
* @val: extra integer information
*
* Handle an error at the xend daemon interface
*/
static void
virXendErrorInt(virConnectPtr conn, virErrorNumber error,
int val) {
const char *errmsg;
if (error == VIR_ERR_OK)
return;
errmsg = __virErrorMsg(error, NULL);
__virRaiseError(conn, NULL, VIR_FROM_XEND, error, VIR_ERR_ERROR,
errmsg, NULL, NULL, val, 0, errmsg, val);
}
#define foreach(iterator, start) \ #define foreach(iterator, start) \
for (_for_i = (start), *iterator = (start)->car; \ for (_for_i = (start), *iterator = (start)->car; \
@ -343,8 +364,7 @@ xend_get(virConnectPtr xend, const char *path,
close(s); close(s);
if ((ret < 0) || (ret >= 300)) { if ((ret < 0) || (ret >= 300)) {
virXendError(NULL, VIR_ERR_OPERATION_FAILED, virXendError(NULL, VIR_ERR_GET_FAILED, content);
content);
} }
return ret; return ret;
@ -392,8 +412,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops,
close(s); close(s);
if ((ret < 0) || (ret >= 300)) { if ((ret < 0) || (ret >= 300)) {
virXendError(NULL, VIR_ERR_OPERATION_FAILED, virXendError(NULL, VIR_ERR_POST_FAILED, content);
content);
} }
return ret; return ret;
@ -421,7 +440,7 @@ http2unix(int ret)
errno = ESRCH; errno = ESRCH;
break; break;
default: default:
printf("unknown error code %d\n", ret); virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret);
errno = EINVAL; errno = EINVAL;
break; break;
} }
@ -1048,6 +1067,7 @@ xend_setup_tcp(virConnectPtr xend, const char *host, int port)
pent = gethostbyname(host); pent = gethostbyname(host);
if (pent == NULL) { if (pent == NULL) {
if (inet_aton(host, &ip) == 0) { if (inet_aton(host, &ip) == 0) {
virXendError(xend, VIR_ERR_UNKNOWN_HOST, host);
errno = ESRCH; errno = ESRCH;
return(-1); return(-1);
} }
@ -1152,8 +1172,11 @@ xend_unpause(virConnectPtr xend, const char *name)
int int
xend_rename(virConnectPtr xend, const char *old, const char *new) xend_rename(virConnectPtr xend, const char *old, const char *new)
{ {
if ((xend == NULL) || (old == NULL) || (new == NULL)) if ((xend == NULL) || (old == NULL) || (new == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, old, "op", "rename", "name", new, NULL); return xend_op(xend, old, "op", "rename", "name", new, NULL);
} }
@ -1169,8 +1192,11 @@ xend_rename(virConnectPtr xend, const char *old, const char *new)
int int
xend_reboot(virConnectPtr xend, const char *name) xend_reboot(virConnectPtr xend, const char *name)
{ {
if ((xend == NULL) || (name == NULL)) if ((xend == NULL) || (name == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, name, "op", "shutdown", "reason", "reboot", NULL); return xend_op(xend, name, "op", "shutdown", "reason", "reboot", NULL);
} }
@ -1187,8 +1213,11 @@ xend_reboot(virConnectPtr xend, const char *name)
int int
xend_shutdown(virConnectPtr xend, const char *name) xend_shutdown(virConnectPtr xend, const char *name)
{ {
if ((xend == NULL) || (name == NULL)) if ((xend == NULL) || (name == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, name, return xend_op(xend, name,
"op", "shutdown", "reason", "halt", NULL); "op", "shutdown", "reason", "halt", NULL);
} }
@ -1206,8 +1235,11 @@ xend_shutdown(virConnectPtr xend, const char *name)
int int
xend_sysrq(virConnectPtr xend, const char *name, const char *key) xend_sysrq(virConnectPtr xend, const char *name, const char *key)
{ {
if ((xend == NULL) || (name == NULL) || (key == NULL)) if ((xend == NULL) || (name == NULL) || (key == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, name, "op", "sysrq", "key", key, NULL); return xend_op(xend, name, "op", "sysrq", "key", key, NULL);
} }
@ -1225,8 +1257,11 @@ xend_sysrq(virConnectPtr xend, const char *name, const char *key)
int int
xend_destroy(virConnectPtr xend, const char *name) xend_destroy(virConnectPtr xend, const char *name)
{ {
if ((xend == NULL) || (name == NULL)) if ((xend == NULL) || (name == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, name, "op", "destroy", NULL); return xend_op(xend, name, "op", "destroy", NULL);
} }
@ -1247,8 +1282,11 @@ xend_destroy(virConnectPtr xend, const char *name)
int int
xend_save(virConnectPtr xend, const char *name, const char *filename) xend_save(virConnectPtr xend, const char *name, const char *filename)
{ {
if ((xend == NULL) || (filename == NULL)) if ((xend == NULL) || (filename == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, name, "op", "save", "file", filename, NULL); return xend_op(xend, name, "op", "save", "file", filename, NULL);
} }
@ -1266,8 +1304,11 @@ xend_save(virConnectPtr xend, const char *name, const char *filename)
int int
xend_restore(virConnectPtr xend, const char *filename) xend_restore(virConnectPtr xend, const char *filename)
{ {
if ((xend == NULL) || (filename == NULL)) if ((xend == NULL) || (filename == NULL)) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(-1); return(-1);
}
return xend_op(xend, "", "op", "restore", "file", filename, NULL); return xend_op(xend, "", "op", "restore", "file", filename, NULL);
} }
@ -1463,6 +1504,12 @@ xend_create_sexpr(virConnectPtr xend, const char *sexpr)
char *ptr; char *ptr;
ptr = urlencode(sexpr); ptr = urlencode(sexpr);
if (ptr == NULL) {
/* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
"Failed to urlencode the create S-Expr");
return(-1);
}
ret = xend_op(xend, "", "op", "create", "config", ptr, NULL); ret = xend_op(xend, "", "op", "create", "config", ptr, NULL);
@ -1977,14 +2024,22 @@ xend_get_domain_ids(virConnectPtr xend, const char *domname,
goto error; goto error;
value = sexpr_node(root, "domain/domid"); value = sexpr_node(root, "domain/domid");
if (value == NULL) if (value == NULL) {
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
"domain informations incomplete, missing domid");
goto error; goto error;
}
ret = strtol(value, NULL, 0); ret = strtol(value, NULL, 0);
if ((ret == 0) && (value[0] != '0')) { if ((ret == 0) && (value[0] != '0')) {
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
"domain informations incorrect domid not numberic");
ret = -1; ret = -1;
} else if (uuid != NULL) { } else if (uuid != NULL) {
char **ptr = (char **) &uuid; char **ptr = (char **) &uuid;
sexpr_uuid(ptr, root, "domain/uuid"); if (sexpr_uuid(ptr, root, "domain/uuid") == NULL) {
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
"domain informations incomplete, missing uuid");
}
} }
error: error:
@ -2188,7 +2243,8 @@ xend_parse_sexp_desc(struct sexpr *root) {
sexpr_int(root, "domain/domid")); sexpr_int(root, "domain/domid"));
tmp = sexpr_node(root, "domain/name"); tmp = sexpr_node(root, "domain/name");
if (tmp == NULL) { if (tmp == NULL) {
/* VIR_ERR_NO_NAME */ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
"domain informations incomplete, missing name");
goto error; goto error;
} }
virBufferVSprintf(&buf, " <name>%s</name>\n", tmp); virBufferVSprintf(&buf, " <name>%s</name>\n", tmp);
@ -2197,7 +2253,8 @@ xend_parse_sexp_desc(struct sexpr *root) {
/* /*
* TODO: we will need some fallback here for other guest OSes * TODO: we will need some fallback here for other guest OSes
*/ */
/* VIR_ERR_NO_KERNEL */ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
"domain informations incomplete, missing kernel");
goto error; goto error;
} }
virBufferAdd(&buf, " <os>\n", 7); virBufferAdd(&buf, " <os>\n", 7);
@ -2230,7 +2287,8 @@ xend_parse_sexp_desc(struct sexpr *root) {
virBufferVSprintf(&buf, " <source file='%s'/>\n", tmp); virBufferVSprintf(&buf, " <source file='%s'/>\n", tmp);
tmp = sexpr_node(node, "device/vbd/dev"); tmp = sexpr_node(node, "device/vbd/dev");
if (tmp == NULL) { if (tmp == NULL) {
/* VIR_ERR_NO_DEV */ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
"domain informations incomplete, vbd has no dev");
goto error; goto error;
} }
virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp); virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
@ -2244,7 +2302,8 @@ xend_parse_sexp_desc(struct sexpr *root) {
virBufferVSprintf(&buf, " <source dev='%s'/>\n", tmp); virBufferVSprintf(&buf, " <source dev='%s'/>\n", tmp);
tmp = sexpr_node(node, "device/vbd/dev"); tmp = sexpr_node(node, "device/vbd/dev");
if (tmp == NULL) { if (tmp == NULL) {
/* VIR_ERR_NO_DEV */ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
"domain informations incomplete, vbd has no dev");
goto error; goto error;
} }
virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp); virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
@ -2316,8 +2375,11 @@ xend_get_domain_xml(virDomainPtr domain) {
char *ret = NULL; char *ret = NULL;
struct sexpr *root; struct sexpr *root;
if (!VIR_IS_DOMAIN(domain)) if (!VIR_IS_DOMAIN(domain)) {
/* this should be caught at the interface but ... */
virXendError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
return(NULL); return(NULL);
}
root = sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name); root = sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name);
if (root == NULL) if (root == NULL)