提交 287e5fe8 编写于 作者: D Daniel P. Berrange

Ensure connection object gets associated with errors

上级 d762148a
Tue Nov 7 15:58:43 EDT 2006 Daniel P. Berrange <berrange@redhat.com>
* src/xend_internal.c: Ensure that virConnectPtr object is passed
around to all functions which can throw errors, so that errors get
correctly associated with the connection, rather than global error
variables.
Tue Nov 7 16:33:43 CET 2006 Daniel Veillard <veillard@redhat.com> Tue Nov 7 16:33:43 CET 2006 Daniel Veillard <veillard@redhat.com>
* libvirt.spec.in: libvirt-devel depends on pkgconfig * libvirt.spec.in: libvirt-devel depends on pkgconfig
......
...@@ -224,6 +224,7 @@ do_connect(virConnectPtr xend) ...@@ -224,6 +224,7 @@ do_connect(virConnectPtr xend)
/** /**
* wr_sync: * wr_sync:
* @xend: the xend connection object
* @fd: the file descriptor * @fd: the file descriptor
* @buffer: the I/O buffer * @buffer: the I/O buffer
* @size: the size of the I/O * @size: the size of the I/O
...@@ -234,7 +235,7 @@ do_connect(virConnectPtr xend) ...@@ -234,7 +235,7 @@ do_connect(virConnectPtr xend)
* Returns the number of bytes exchanged, or -1 in case of error * Returns the number of bytes exchanged, or -1 in case of error
*/ */
static size_t static size_t
wr_sync(int fd, void *buffer, size_t size, int do_read) wr_sync(virConnectPtr xend, int fd, void *buffer, size_t size, int do_read)
{ {
size_t offset = 0; size_t offset = 0;
...@@ -260,10 +261,10 @@ wr_sync(int fd, void *buffer, size_t size, int do_read) ...@@ -260,10 +261,10 @@ wr_sync(int fd, void *buffer, size_t size, int do_read)
/* unrecoverable error */ /* unrecoverable error */
if (len == -1) { if (len == -1) {
if (do_read) if (do_read)
virXendError(NULL, VIR_ERR_INTERNAL_ERROR, virXendError(xend, VIR_ERR_INTERNAL_ERROR,
_("failed to read from Xen Daemon")); _("failed to read from Xen Daemon"));
else else
virXendError(NULL, VIR_ERR_INTERNAL_ERROR, virXendError(xend, VIR_ERR_INTERNAL_ERROR,
_("failed to read from Xen Daemon")); _("failed to read from Xen Daemon"));
return (-1); return (-1);
...@@ -277,6 +278,7 @@ wr_sync(int fd, void *buffer, size_t size, int do_read) ...@@ -277,6 +278,7 @@ wr_sync(int fd, void *buffer, size_t size, int do_read)
/** /**
* sread: * sread:
* @xend: the xend connection object
* @fd: the file descriptor * @fd: the file descriptor
* @buffer: the I/O buffer * @buffer: the I/O buffer
* @size: the size of the I/O * @size: the size of the I/O
...@@ -286,13 +288,14 @@ wr_sync(int fd, void *buffer, size_t size, int do_read) ...@@ -286,13 +288,14 @@ wr_sync(int fd, void *buffer, size_t size, int do_read)
* Returns the number of bytes read, or -1 in case of error * Returns the number of bytes read, or -1 in case of error
*/ */
static ssize_t static ssize_t
sread(int fd, void *buffer, size_t size) sread(virConnectPtr xend, int fd, void *buffer, size_t size)
{ {
return wr_sync(fd, buffer, size, 1); return wr_sync(xend, fd, buffer, size, 1);
} }
/** /**
* swrite: * swrite:
* @xend: the xend connection object
* @fd: the file descriptor * @fd: the file descriptor
* @buffer: the I/O buffer * @buffer: the I/O buffer
* @size: the size of the I/O * @size: the size of the I/O
...@@ -302,13 +305,14 @@ sread(int fd, void *buffer, size_t size) ...@@ -302,13 +305,14 @@ sread(int fd, void *buffer, size_t size)
* Returns the number of bytes written, or -1 in case of error * Returns the number of bytes written, or -1 in case of error
*/ */
static ssize_t static ssize_t
swrite(int fd, const void *buffer, size_t size) swrite(virConnectPtr xend, int fd, const void *buffer, size_t size)
{ {
return wr_sync(fd, (void *) buffer, size, 0); return wr_sync(xend, fd, (void *) buffer, size, 0);
} }
/** /**
* swrites: * swrites:
* @xend: the xend connection object
* @fd: the file descriptor * @fd: the file descriptor
* @string: the string to write * @string: the string to write
* *
...@@ -317,13 +321,14 @@ swrite(int fd, const void *buffer, size_t size) ...@@ -317,13 +321,14 @@ swrite(int fd, const void *buffer, size_t size)
* Returns the number of bytes written, or -1 in case of error * Returns the number of bytes written, or -1 in case of error
*/ */
static ssize_t static ssize_t
swrites(int fd, const char *string) swrites(virConnectPtr xend, int fd, const char *string)
{ {
return swrite(fd, string, strlen(string)); return swrite(xend, fd, string, strlen(string));
} }
/** /**
* sreads: * sreads:
* @xend: the xend connection object
* @fd: the file descriptor * @fd: the file descriptor
* @buffer: the I/O buffer * @buffer: the I/O buffer
* @n_buffer: the size of the I/O buffer * @n_buffer: the size of the I/O buffer
...@@ -333,7 +338,7 @@ swrites(int fd, const char *string) ...@@ -333,7 +338,7 @@ swrites(int fd, const char *string)
* Returns the number of bytes read, or -1 in case of error * Returns the number of bytes read, or -1 in case of error
*/ */
static ssize_t static ssize_t
sreads(int fd, char *buffer, size_t n_buffer) sreads(virConnectPtr xend, int fd, char *buffer, size_t n_buffer)
{ {
size_t offset; size_t offset;
...@@ -343,7 +348,7 @@ sreads(int fd, char *buffer, size_t n_buffer) ...@@ -343,7 +348,7 @@ sreads(int fd, char *buffer, size_t n_buffer)
for (offset = 0; offset < (n_buffer - 1); offset++) { for (offset = 0; offset < (n_buffer - 1); offset++) {
ssize_t ret; ssize_t ret;
ret = sread(fd, buffer + offset, 1); ret = sread(xend, fd, buffer + offset, 1);
if (ret == 0) if (ret == 0)
break; break;
else if (ret == -1) else if (ret == -1)
...@@ -368,6 +373,7 @@ istartswith(const char *haystack, const char *needle) ...@@ -368,6 +373,7 @@ istartswith(const char *haystack, const char *needle)
/** /**
* xend_req: * xend_req:
* @xend: the xend connection object
* @fd: the file descriptor * @fd: the file descriptor
* @content: the buffer to store the content * @content: the buffer to store the content
* @n_content: the size of the buffer * @n_content: the size of the buffer
...@@ -377,13 +383,13 @@ istartswith(const char *haystack, const char *needle) ...@@ -377,13 +383,13 @@ istartswith(const char *haystack, const char *needle)
* Returns the HTTP return code. * Returns the HTTP return code.
*/ */
static int static int
xend_req(int fd, char *content, size_t n_content) xend_req(virConnectPtr xend, int fd, char *content, size_t n_content)
{ {
char buffer[4096]; char buffer[4096];
int content_length = -1; int content_length = -1;
int retcode = 0; int retcode = 0;
while (sreads(fd, buffer, sizeof(buffer)) > 0) { while (sreads(xend, fd, buffer, sizeof(buffer)) > 0) {
if (strcmp(buffer, "\r\n") == 0) if (strcmp(buffer, "\r\n") == 0)
break; break;
...@@ -399,7 +405,7 @@ xend_req(int fd, char *content, size_t n_content) ...@@ -399,7 +405,7 @@ xend_req(int fd, char *content, size_t n_content)
if ((unsigned int) content_length > (n_content + 1)) if ((unsigned int) content_length > (n_content + 1))
content_length = n_content - 1; content_length = n_content - 1;
ret = sread(fd, content, content_length); ret = sread(xend, fd, content, content_length);
if (ret < 0) if (ret < 0)
return -1; return -1;
...@@ -432,21 +438,21 @@ xend_get(virConnectPtr xend, const char *path, ...@@ -432,21 +438,21 @@ xend_get(virConnectPtr xend, const char *path,
if (s == -1) if (s == -1)
return s; return s;
swrites(s, "GET "); swrites(xend, s, "GET ");
swrites(s, path); swrites(xend, s, path);
swrites(s, " HTTP/1.1\r\n"); swrites(xend, s, " HTTP/1.1\r\n");
swrites(s, swrites(xend, s,
"Host: localhost:8000\r\n" "Host: localhost:8000\r\n"
"Accept-Encoding: identity\r\n" "Accept-Encoding: identity\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n" "\r\n"); "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n");
ret = xend_req(s, content, n_content); ret = xend_req(xend, s, content, n_content);
close(s); close(s);
if (((ret < 0) || (ret >= 300)) && if (((ret < 0) || (ret >= 300)) &&
((ret != 404) || (strncmp(path, "/xend/domain/", 13)))) { ((ret != 404) || (strncmp(path, "/xend/domain/", 13)))) {
virXendError(NULL, VIR_ERR_GET_FAILED, content); virXendError(xend, VIR_ERR_GET_FAILED, content);
} }
return ret; return ret;
...@@ -477,27 +483,27 @@ xend_post(virConnectPtr xend, const char *path, const char *ops, ...@@ -477,27 +483,27 @@ xend_post(virConnectPtr xend, const char *path, const char *ops,
if (s == -1) if (s == -1)
return s; return s;
swrites(s, "POST "); swrites(xend, s, "POST ");
swrites(s, path); swrites(xend, s, path);
swrites(s, " HTTP/1.1\r\n"); swrites(xend, s, " HTTP/1.1\r\n");
swrites(s, swrites(xend, s,
"Host: localhost:8000\r\n" "Host: localhost:8000\r\n"
"Accept-Encoding: identity\r\n" "Accept-Encoding: identity\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n" "Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: "); "Content-Length: ");
snprintf(buffer, sizeof(buffer), "%d", (int) strlen(ops)); snprintf(buffer, sizeof(buffer), "%d", (int) strlen(ops));
swrites(s, buffer); swrites(xend ,s, buffer);
swrites(s, "\r\n\r\n"); swrites(xend, s, "\r\n\r\n");
swrites(s, ops); swrites(xend, s, ops);
ret = xend_req(s, content, n_content); ret = xend_req(xend, s, content, n_content);
close(s); close(s);
if ((ret < 0) || (ret >= 300)) { if ((ret < 0) || (ret >= 300)) {
virXendError(NULL, VIR_ERR_POST_FAILED, content); virXendError(xend, VIR_ERR_POST_FAILED, content);
} else if ((ret = 202) && (strstr(content, "failed") != NULL)) { } else if ((ret = 202) && (strstr(content, "failed") != NULL)) {
virXendError(NULL, VIR_ERR_POST_FAILED, content); virXendError(xend, VIR_ERR_POST_FAILED, content);
ret = -1; ret = -1;
} }
...@@ -508,6 +514,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops, ...@@ -508,6 +514,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops,
/** /**
* http2unix: * http2unix:
* @xend: the xend connection object
* @ret: the http return code * @ret: the http return code
* *
* Convert the HTTP return code to 0/-1 and set errno if needed * Convert the HTTP return code to 0/-1 and set errno if needed
...@@ -515,7 +522,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops, ...@@ -515,7 +522,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops,
* Return -1 in case of error code 0 otherwise * Return -1 in case of error code 0 otherwise
*/ */
static int static int
http2unix(int ret) http2unix(virConnectPtr xend, int ret)
{ {
switch (ret) { switch (ret) {
case -1: case -1:
...@@ -531,7 +538,7 @@ http2unix(int ret) ...@@ -531,7 +538,7 @@ http2unix(int ret)
errno = EIO; errno = EIO;
break; break;
default: default:
virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret); virXendErrorInt(xend, VIR_ERR_HTTP_ERROR, ret);
errno = EINVAL; errno = EINVAL;
break; break;
} }
...@@ -573,7 +580,7 @@ xend_op_ext2(virConnectPtr xend, const char *path, char *error, ...@@ -573,7 +580,7 @@ xend_op_ext2(virConnectPtr xend, const char *path, char *error,
sizeof(ops) - offset, "%s", "&"); sizeof(ops) - offset, "%s", "&");
} }
return http2unix(xend_post(xend, path, ops, error, n_error)); return http2unix(xend, xend_post(xend, path, ops, error, n_error));
} }
...@@ -661,7 +668,7 @@ sexpr_get(virConnectPtr xend, const char *fmt, ...) ...@@ -661,7 +668,7 @@ sexpr_get(virConnectPtr xend, const char *fmt, ...)
va_end(ap); va_end(ap);
ret = xend_get(xend, path, buffer, sizeof(buffer)); ret = xend_get(xend, path, buffer, sizeof(buffer));
ret = http2unix(ret); ret = http2unix(xend ,ret);
if (ret == -1) if (ret == -1)
return NULL; return NULL;
...@@ -1333,7 +1340,7 @@ xend_node_restart(virConnectPtr xend) ...@@ -1333,7 +1340,7 @@ xend_node_restart(virConnectPtr xend)
int int
xend_dmesg(virConnectPtr xend, char *buffer, size_t n_buffer) xend_dmesg(virConnectPtr xend, char *buffer, size_t n_buffer)
{ {
return http2unix(xend_get(xend, "/xend/node/dmesg", buffer, n_buffer)); return http2unix(xend, xend_get(xend, "/xend/node/dmesg", buffer, n_buffer));
} }
/** /**
...@@ -1365,7 +1372,7 @@ xend_dmesg_clear(virConnectPtr xend) ...@@ -1365,7 +1372,7 @@ xend_dmesg_clear(virConnectPtr xend)
int int
xend_log(virConnectPtr xend, char *buffer, size_t n_buffer) xend_log(virConnectPtr xend, char *buffer, size_t n_buffer)
{ {
return http2unix(xend_get(xend, "/xend/node/log", buffer, n_buffer)); return http2unix(xend, xend_get(xend, "/xend/node/log", buffer, n_buffer));
} }
#endif /* PROXY */ #endif /* PROXY */
...@@ -1383,6 +1390,7 @@ xend_log(virConnectPtr xend, char *buffer, size_t n_buffer) ...@@ -1383,6 +1390,7 @@ xend_log(virConnectPtr xend, char *buffer, size_t n_buffer)
/** /**
* xend_parse_sexp_desc_os: * xend_parse_sexp_desc_os:
* @xend: the xend connection object
* @node: the root of the parsed S-Expression * @node: the root of the parsed S-Expression
* @buf: output buffer object * @buf: output buffer object
* @hvm: true or 1 if no contains HVM S-Expression * @hvm: true or 1 if no contains HVM S-Expression
...@@ -1392,7 +1400,7 @@ xend_log(virConnectPtr xend, char *buffer, size_t n_buffer) ...@@ -1392,7 +1400,7 @@ xend_log(virConnectPtr xend, char *buffer, size_t n_buffer)
* Returns 0 in case of success and -1 in case of error * Returns 0 in case of success and -1 in case of error
*/ */
static int static int
xend_parse_sexp_desc_os(struct sexpr *node, virBufferPtr buf, int hvm) xend_parse_sexp_desc_os(virConnectPtr xend, struct sexpr *node, virBufferPtr buf, int hvm)
{ {
const char *tmp; const char *tmp;
...@@ -1405,7 +1413,7 @@ xend_parse_sexp_desc_os(struct sexpr *node, virBufferPtr buf, int hvm) ...@@ -1405,7 +1413,7 @@ xend_parse_sexp_desc_os(struct sexpr *node, virBufferPtr buf, int hvm)
virBufferVSprintf(buf, " <type>hvm</type>\n"); virBufferVSprintf(buf, " <type>hvm</type>\n");
tmp = sexpr_node(node, "domain/image/hvm/kernel"); tmp = sexpr_node(node, "domain/image/hvm/kernel");
if (tmp == NULL) { if (tmp == NULL) {
virXendError(NULL, VIR_ERR_INTERNAL_ERROR, virXendError(xend, VIR_ERR_INTERNAL_ERROR,
_("domain information incomplete, missing kernel")); _("domain information incomplete, missing kernel"));
return(-1); return(-1);
} }
...@@ -1430,7 +1438,7 @@ xend_parse_sexp_desc_os(struct sexpr *node, virBufferPtr buf, int hvm) ...@@ -1430,7 +1438,7 @@ xend_parse_sexp_desc_os(struct sexpr *node, virBufferPtr buf, int hvm)
virBufferVSprintf(buf, " <type>linux</type>\n"); virBufferVSprintf(buf, " <type>linux</type>\n");
tmp = sexpr_node(node, "domain/image/linux/kernel"); tmp = sexpr_node(node, "domain/image/linux/kernel");
if (tmp == NULL) { if (tmp == NULL) {
virXendError(NULL, VIR_ERR_INTERNAL_ERROR, virXendError(xend, VIR_ERR_INTERNAL_ERROR,
_("domain information incomplete, missing kernel")); _("domain information incomplete, missing kernel"));
return(-1); return(-1);
} }
...@@ -1515,7 +1523,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, int xendConfigVersi ...@@ -1515,7 +1523,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, int xendConfigVersi
if (sexpr_lookup(root, "domain/image")) { if (sexpr_lookup(root, "domain/image")) {
hvm = sexpr_lookup(root, "domain/image/hvm") ? 1 : 0; hvm = sexpr_lookup(root, "domain/image/hvm") ? 1 : 0;
xend_parse_sexp_desc_os(root, &buf, hvm); xend_parse_sexp_desc_os(conn, root, &buf, hvm);
} }
virBufferVSprintf(&buf, " <memory>%d</memory>\n", virBufferVSprintf(&buf, " <memory>%d</memory>\n",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册