[Libvir] Ensure errors are reported against virConnect object

Daniel P. Berrange berrange at redhat.com
Wed Oct 25 18:20:30 UTC 2006


In xend_internal.c there are a large number of places where errors are being
reported without supplying an associated virConnectPtr object. So for example
when calling virDomainCreateLinux, some errors get associated with the
connection object, while others get attached to the global error object.
The attached patch fixes up all places in xend_internal.c which have an
virConnectPtr object available to always pass this object into the the
error reporting funcs.

Regards,
Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
-------------- next part --------------
Index: src/xend_internal.c
===================================================================
RCS file: /data/cvs/libvirt/src/xend_internal.c,v
retrieving revision 1.68
diff -c -r1.68 xend_internal.c
*** src/xend_internal.c	9 Oct 2006 14:32:07 -0000	1.68
--- src/xend_internal.c	25 Oct 2006 18:56:47 -0000
***************
*** 224,229 ****
--- 224,230 ----
  
  /**
   * wr_sync:
+  * @xend: the xend connection object
   * @fd:  the file descriptor
   * @buffer: the I/O buffer
   * @size: the size of the I/O
***************
*** 234,240 ****
   * Returns the number of bytes exchanged, or -1 in case of error
   */
  static size_t
! wr_sync(int fd, void *buffer, size_t size, int do_read)
  {
      size_t offset = 0;
  
--- 235,241 ----
   * Returns the number of bytes exchanged, or -1 in case of error
   */
  static size_t
! wr_sync(virConnectPtr xend, int fd, void *buffer, size_t size, int do_read)
  {
      size_t offset = 0;
  
***************
*** 260,269 ****
          /* unrecoverable error */
          if (len == -1) {
              if (do_read)
!                 virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
                               _("failed to read from Xen Daemon"));
              else
!                 virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
                               _("failed to read from Xen Daemon"));
  
              return (-1);
--- 261,270 ----
          /* unrecoverable error */
          if (len == -1) {
              if (do_read)
!                 virXendError(xend, VIR_ERR_INTERNAL_ERROR,
                               _("failed to read from Xen Daemon"));
              else
!                 virXendError(xend, VIR_ERR_INTERNAL_ERROR,
                               _("failed to read from Xen Daemon"));
  
              return (-1);
***************
*** 277,282 ****
--- 278,284 ----
  
  /**
   * sread:
+  * @xend: the xend connection object
   * @fd:  the file descriptor
   * @buffer: the I/O buffer
   * @size: the size of the I/O
***************
*** 286,298 ****
   * Returns the number of bytes read, or -1 in case of error
   */
  static ssize_t
! sread(int fd, void *buffer, size_t size)
  {
!     return wr_sync(fd, buffer, size, 1);
  }
  
  /**
   * swrite:
   * @fd:  the file descriptor
   * @buffer: the I/O buffer
   * @size: the size of the I/O
--- 288,301 ----
   * Returns the number of bytes read, or -1 in case of error
   */
  static ssize_t
! sread(virConnectPtr xend, int fd, void *buffer, size_t size)
  {
!     return wr_sync(xend, fd, buffer, size, 1);
  }
  
  /**
   * swrite:
+  * @xend: the xend connection object
   * @fd:  the file descriptor
   * @buffer: the I/O buffer
   * @size: the size of the I/O
***************
*** 302,314 ****
   * Returns the number of bytes written, or -1 in case of error
   */
  static ssize_t
! swrite(int fd, const void *buffer, size_t size)
  {
!     return wr_sync(fd, (void *) buffer, size, 0);
  }
  
  /**
   * swrites:
   * @fd:  the file descriptor
   * @string: the string to write
   *
--- 305,318 ----
   * Returns the number of bytes written, or -1 in case of error
   */
  static ssize_t
! swrite(virConnectPtr xend, int fd, const void *buffer, size_t size)
  {
!     return wr_sync(xend, fd, (void *) buffer, size, 0);
  }
  
  /**
   * swrites:
+  * @xend: the xend connection object
   * @fd:  the file descriptor
   * @string: the string to write
   *
***************
*** 317,329 ****
   * Returns the number of bytes written, or -1 in case of error
   */
  static ssize_t
! swrites(int fd, const char *string)
  {
!     return swrite(fd, string, strlen(string));
  }
  
  /**
   * sreads:
   * @fd:  the file descriptor
   * @buffer: the I/O buffer
   * @n_buffer: the size of the I/O buffer
--- 321,334 ----
   * Returns the number of bytes written, or -1 in case of error
   */
  static ssize_t
! swrites(virConnectPtr xend, int fd, const char *string)
  {
!     return swrite(xend, fd, string, strlen(string));
  }
  
  /**
   * sreads:
+  * @xend: the xend connection object
   * @fd:  the file descriptor
   * @buffer: the I/O buffer
   * @n_buffer: the size of the I/O buffer
***************
*** 333,339 ****
   * Returns the number of bytes read, or -1 in case of error
   */
  static ssize_t
! sreads(int fd, char *buffer, size_t n_buffer)
  {
      size_t offset;
  
--- 338,344 ----
   * Returns the number of bytes read, or -1 in case of error
   */
  static ssize_t
! sreads(virConnectPtr xend, int fd, char *buffer, size_t n_buffer)
  {
      size_t offset;
  
***************
*** 343,349 ****
      for (offset = 0; offset < (n_buffer - 1); offset++) {
          ssize_t ret;
  
!         ret = sread(fd, buffer + offset, 1);
          if (ret == 0)
              break;
          else if (ret == -1)
--- 348,354 ----
      for (offset = 0; offset < (n_buffer - 1); offset++) {
          ssize_t ret;
  
!         ret = sread(xend, fd, buffer + offset, 1);
          if (ret == 0)
              break;
          else if (ret == -1)
***************
*** 368,373 ****
--- 373,379 ----
  
  /**
   * xend_req:
+  * @xend: the xend connection object
   * @fd: the file descriptor
   * @content: the buffer to store the content
   * @n_content: the size of the buffer
***************
*** 377,389 ****
   * Returns the HTTP return code.
   */
  static int
! xend_req(int fd, char *content, size_t n_content)
  {
      char buffer[4096];
      int content_length = -1;
      int retcode = 0;
  
!     while (sreads(fd, buffer, sizeof(buffer)) > 0) {
          if (strcmp(buffer, "\r\n") == 0)
              break;
  
--- 383,395 ----
   * Returns the HTTP return code.
   */
  static int
! xend_req(virConnectPtr xend, int fd, char *content, size_t n_content)
  {
      char buffer[4096];
      int content_length = -1;
      int retcode = 0;
  
!     while (sreads(xend, fd, buffer, sizeof(buffer)) > 0) {
          if (strcmp(buffer, "\r\n") == 0)
              break;
  
***************
*** 399,405 ****
          if ((unsigned int) content_length > (n_content + 1))
              content_length = n_content - 1;
  
!         ret = sread(fd, content, content_length);
          if (ret < 0)
              return -1;
  
--- 405,411 ----
          if ((unsigned int) content_length > (n_content + 1))
              content_length = n_content - 1;
  
!         ret = sread(xend, fd, content, content_length);
          if (ret < 0)
              return -1;
  
***************
*** 432,451 ****
      if (s == -1)
          return s;
  
!     swrites(s, "GET ");
!     swrites(s, path);
!     swrites(s, " HTTP/1.1\r\n");
  
!     swrites(s,
              "Host: localhost:8000\r\n"
              "Accept-Encoding: identity\r\n"
              "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n");
  
!     ret = xend_req(s, content, n_content);
      close(s);
  
      if ((ret < 0) || (ret >= 300)) {
!         virXendError(NULL, VIR_ERR_GET_FAILED, content);
      }
  
      return ret;
--- 438,457 ----
      if (s == -1)
          return s;
  
!     swrites(xend, s, "GET ");
!     swrites(xend, s, path);
!     swrites(xend, s, " HTTP/1.1\r\n");
  
!     swrites(xend, s,
              "Host: localhost:8000\r\n"
              "Accept-Encoding: identity\r\n"
              "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n");
  
!     ret = xend_req(xend, s, content, n_content);
      close(s);
  
      if ((ret < 0) || (ret >= 300)) {
!         virXendError(xend, VIR_ERR_GET_FAILED, content);
      }
  
      return ret;
***************
*** 476,502 ****
      if (s == -1)
          return s;
  
!     swrites(s, "POST ");
!     swrites(s, path);
!     swrites(s, " HTTP/1.1\r\n");
  
!     swrites(s,
              "Host: localhost:8000\r\n"
              "Accept-Encoding: identity\r\n"
              "Content-Type: application/x-www-form-urlencoded\r\n"
              "Content-Length: ");
      snprintf(buffer, sizeof(buffer), "%d", (int) strlen(ops));
!     swrites(s, buffer);
!     swrites(s, "\r\n\r\n");
!     swrites(s, ops);
  
!     ret = xend_req(s, content, n_content);
      close(s);
  
      if ((ret < 0) || (ret >= 300)) {
!         virXendError(NULL, VIR_ERR_POST_FAILED, content);
      } else if ((ret = 202) && (strstr(content, "failed") != NULL)) {
!         virXendError(NULL, VIR_ERR_POST_FAILED, content);
          ret = -1;
      }
  
--- 482,508 ----
      if (s == -1)
          return s;
  
!     swrites(xend, s, "POST ");
!     swrites(xend, s, path);
!     swrites(xend, s, " HTTP/1.1\r\n");
  
!     swrites(xend, s,
              "Host: localhost:8000\r\n"
              "Accept-Encoding: identity\r\n"
              "Content-Type: application/x-www-form-urlencoded\r\n"
              "Content-Length: ");
      snprintf(buffer, sizeof(buffer), "%d", (int) strlen(ops));
!     swrites(xend ,s, buffer);
!     swrites(xend, s, "\r\n\r\n");
!     swrites(xend, s, ops);
  
!     ret = xend_req(xend, s, content, n_content);
      close(s);
  
      if ((ret < 0) || (ret >= 300)) {
!         virXendError(xend, VIR_ERR_POST_FAILED, content);
      } else if ((ret = 202) && (strstr(content, "failed") != NULL)) {
!         virXendError(xend, VIR_ERR_POST_FAILED, content);
          ret = -1;
      }
  
***************
*** 507,512 ****
--- 513,519 ----
  
  /**
   * http2unix:
+  * @xend: the xend connection object
   * @ret: the http return code
   *
   * Convert the HTTP return code to 0/-1 and set errno if needed
***************
*** 514,520 ****
   * Return -1 in case of error code 0 otherwise
   */
  static int
! http2unix(int ret)
  {
      switch (ret) {
          case -1:
--- 521,527 ----
   * Return -1 in case of error code 0 otherwise
   */
  static int
! http2unix(virConnectPtr xend, int ret)
  {
      switch (ret) {
          case -1:
***************
*** 530,536 ****
              errno = EIO;
              break;
          default:
!             virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret);
              errno = EINVAL;
              break;
      }
--- 537,543 ----
              errno = EIO;
              break;
          default:
!             virXendErrorInt(xend, VIR_ERR_HTTP_ERROR, ret);
              errno = EINVAL;
              break;
      }
***************
*** 572,578 ****
                                 sizeof(ops) - offset, "%s", "&");
      }
  
!     return http2unix(xend_post(xend, path, ops, error, n_error));
  }
  
  
--- 579,585 ----
                                 sizeof(ops) - offset, "%s", "&");
      }
  
!     return http2unix(xend, xend_post(xend, path, ops, error, n_error));
  }
  
  
***************
*** 660,666 ****
      va_end(ap);
  
      ret = xend_get(xend, path, buffer, sizeof(buffer));
!     ret = http2unix(ret);
      if (ret == -1)
          return NULL;
  
--- 667,673 ----
      va_end(ap);
  
      ret = xend_get(xend, path, buffer, sizeof(buffer));
!     ret = http2unix(xend ,ret);
      if (ret == -1)
          return NULL;
  
***************
*** 1332,1338 ****
  int
  xend_dmesg(virConnectPtr xend, char *buffer, size_t n_buffer)
  {
!     return http2unix(xend_get(xend, "/xend/node/dmesg", buffer, n_buffer));
  }
  
  /**
--- 1339,1345 ----
  int
  xend_dmesg(virConnectPtr xend, char *buffer, size_t n_buffer)
  {
!     return http2unix(xend, xend_get(xend, "/xend/node/dmesg", buffer, n_buffer));
  }
  
  /**
***************
*** 1364,1370 ****
  int
  xend_log(virConnectPtr xend, char *buffer, size_t n_buffer)
  {
!     return http2unix(xend_get(xend, "/xend/node/log", buffer, n_buffer));
  }
  #endif /* PROXY */
  
--- 1371,1377 ----
  int
  xend_log(virConnectPtr xend, char *buffer, size_t n_buffer)
  {
!     return http2unix(xend, xend_get(xend, "/xend/node/log", buffer, n_buffer));
  }
  #endif /* PROXY */
  
***************
*** 1382,1387 ****
--- 1389,1395 ----
  
  /**
   * xend_parse_sexp_desc_os:
+  * @xend: the xend connection object
   * @node: the root of the parsed S-Expression
   * @buf: output buffer object
   * @hvm: true or 1 if no contains HVM S-Expression 
***************
*** 1391,1397 ****
   * Returns 0 in case of success and -1 in case of error
   */
  static int
! xend_parse_sexp_desc_os(struct sexpr *node, virBufferPtr buf, int hvm)
  {
      const char *tmp;
  
--- 1399,1405 ----
   * Returns 0 in case of success and -1 in case of error
   */
  static int
! xend_parse_sexp_desc_os(virConnectPtr xend, struct sexpr *node, virBufferPtr buf, int hvm)
  {
      const char *tmp;
  
***************
*** 1404,1410 ****
          virBufferVSprintf(buf, "    <type>hvm</type>\n");
          tmp = sexpr_node(node, "domain/image/hvm/kernel");
          if (tmp == NULL) {
!             virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
                           _("domain information incomplete, missing kernel"));
              return(-1);
  	}
--- 1412,1418 ----
          virBufferVSprintf(buf, "    <type>hvm</type>\n");
          tmp = sexpr_node(node, "domain/image/hvm/kernel");
          if (tmp == NULL) {
!             virXendError(xend, VIR_ERR_INTERNAL_ERROR,
                           _("domain information incomplete, missing kernel"));
              return(-1);
  	}
***************
*** 1429,1435 ****
          virBufferVSprintf(buf, "    <type>linux</type>\n");
          tmp = sexpr_node(node, "domain/image/linux/kernel");
          if (tmp == NULL) {
!             virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
                           _("domain information incomplete, missing kernel"));
              return(-1);
  	}
--- 1437,1443 ----
          virBufferVSprintf(buf, "    <type>linux</type>\n");
          tmp = sexpr_node(node, "domain/image/linux/kernel");
          if (tmp == NULL) {
!             virXendError(xend, VIR_ERR_INTERNAL_ERROR,
                           _("domain information incomplete, missing kernel"));
              return(-1);
  	}
***************
*** 1514,1520 ****
  
      if (sexpr_lookup(root, "domain/image")) {
          hvm = sexpr_lookup(root, "domain/image/hvm") ? 1 : 0;
!         xend_parse_sexp_desc_os(root, &buf, hvm);
      }
  
      virBufferVSprintf(&buf, "  <memory>%d</memory>\n",
--- 1522,1528 ----
  
      if (sexpr_lookup(root, "domain/image")) {
          hvm = sexpr_lookup(root, "domain/image/hvm") ? 1 : 0;
!         xend_parse_sexp_desc_os(conn, root, &buf, hvm);
      }
  
      virBufferVSprintf(&buf, "  <memory>%d</memory>\n",


More information about the libvir-list mailing list