WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH] libxenlight: Clean up logging arrangements

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH] libxenlight: Clean up logging arrangements
From: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
Date: Tue, 17 Nov 2009 17:55:31 +0000
Delivery-date: Tue, 17 Nov 2009 09:56:09 -0800
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
libxenlight: Clean up logging arrangements

* Introduce new variants of the logging functions which include
  errno values (converted using strerror) in the messages passed to the
  application's logging callback.

* While we're at it, introduce xl_logv.

* Use the new errno-including logging functions everywhere where
  appropriate.  In general, xc_... functions return errno values or 0;
  xs_... functions return 0 or -1 (or some such) setting errno.

* When libxl_xs_get_dompath fails, do not treat it as an allocation
  error.  It isn't: it usually means xenstored failed.

* Remove many spurious \n's from log messages.  (The applications log
  callback is expected to add a \n if it wants to do that, so libxl's
  logging functions should be passes strings without \n.)

Signed-off-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 69690e4..5e2f23b 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -106,14 +106,17 @@ int libxl_domain_make(struct libxl_ctx *ctx, 
libxl_domain_create_info *info,
 
     ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
     if (ret < 0) {
-        XL_LOG(ctx, XL_LOG_ERROR, "domain creation fail: %d", ret);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
         return ERROR_FAIL;
     }
 
     dom_path = libxl_xs_get_dompath(ctx, *domid);
+    if (!dom_path)
+        return ERROR_FAIL;
+
     vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
     vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
-    if (!dom_path || !vm_path || !vss_path) {
+    if (!vm_path || !vss_path) {
         XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
         return ERROR_FAIL;
     }
@@ -326,6 +329,9 @@ int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t 
domid, int req)
         return ERROR_INVAL;
 
     dom_path = libxl_xs_get_dompath(ctx, domid);
+    if (!dom_path)
+        return ERROR_FAIL;
+
     shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
 
     xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], 
strlen(req_table[req]));
@@ -347,20 +353,21 @@ static int libxl_destroy_device_model(struct libxl_ctx 
*ctx, uint32_t domid)
 
     pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, 
"/local/domain/%d/image/device-model-pid", domid));
     if (!pid) {
-        XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find device model's pid\n");
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
         return -1;
     }
     xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, 
"/local/domain/0/device-model/%d", domid));
 
     ret = kill(atoi(pid), SIGHUP);
     if (ret < 0 && errno == ESRCH) {
-        XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited\n");
+        XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
         ret = 0;
     } else if (ret == 0) {
-        XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled\n");
+        XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
         ret = 0;
     } else {
-        XL_LOG(ctx, XL_LOG_ERROR, "kill %d returned %d errno=%d\n", atoi(pid), 
ret, errno);
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
+                     atoi(pid));
     }
     return ret;
 }
@@ -369,38 +376,40 @@ int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t 
domid, int force)
 {
     char *dom_path, vm_path[41];
     xen_uuid_t *uuid;
+    int rc;
 
     dom_path = libxl_xs_get_dompath(ctx, domid);
-    if (!dom_path) {
-        XL_LOG(ctx, XL_LOG_ERROR, "dompath doesn't exist for %d\n", domid);
+    if (!dom_path)
         return -1;
-    }
+
     if (libxl_domid_to_uuid(ctx, &uuid, domid) < 0) {
-        XL_LOG(ctx, XL_LOG_ERROR, "failed ot get uuid for %d\n", domid);
+        XL_LOG(ctx, XL_LOG_ERROR, "failed ot get uuid for %d", domid);
         return -1;
     }
     if (libxl_device_pci_shutdown(ctx, domid) < 0)
-        XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d\n", domid);
+        XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
     xs_write(ctx->xsh, XBT_NULL,
              libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", 
domid),
              "shutdown", strlen("shutdown"));
-    if (xc_domain_pause(ctx->xch, domid) < 0) {
-        XL_LOG(ctx, XL_LOG_ERROR, "xc_domain_pause failed for %d\n", domid);
+    rc = xc_domain_pause(ctx->xch, domid);
+    if (rc < 0) {
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for 
%d", domid);
         return -1;
     }
-    if (xc_domain_destroy(ctx->xch, domid) < 0) {
-        XL_LOG(ctx, XL_LOG_ERROR, "xc_domain_destroy failed for %d\n", domid);
+    rc = xc_domain_destroy(ctx->xch, domid);
+    if (rc < 0) {
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for 
%d", domid);
         return -1;
     }
     if (libxl_devices_destroy(ctx, domid, force) < 0)
-        XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d\n", 
domid);
+        XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", 
domid);
     if (libxl_destroy_device_model(ctx, domid) < 0)
-        XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for 
%d\n", domid);
+        XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", 
domid);
     if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
-        XL_LOG(ctx, XL_LOG_ERROR, "xs_rm failed for %s\n", dom_path);
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
     snprintf(vm_path, sizeof(vm_path), "/vm/%s", libxl_uuid_to_string(ctx, 
uuid));
     if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
-        XL_LOG(ctx, XL_LOG_ERROR, "xs_rm failed for %s\n", vm_path);
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
     return 0;
 }
 
@@ -524,6 +533,8 @@ int libxl_create_device_model(struct libxl_ctx *ctx,
         return ERROR_FAIL;
 
     dom_path = libxl_xs_get_dompath(ctx, info->domid);
+    if (!dom_path)
+        return ERROR_FAIL;
 
     path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
     xs_mkdir(ctx->xsh, XBT_NULL, path);
@@ -1017,7 +1028,7 @@ static int libxl_create_pci_backend(struct libxl_ctx 
*ctx, uint32_t domid, libxl
     if (!back)
         return ERROR_NOMEM;
 
-    XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend\n");
+    XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
 
     /* add pci device */
     device.backend_devid = 0;
@@ -1088,7 +1099,7 @@ static int libxl_device_pci_add_xenstore(struct libxl_ctx 
*ctx, uint32_t domid,
     if (!back)
         return ERROR_NOMEM;
 
-    XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore\n");
+    XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
     num = atoi(num_devs);
     flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
     flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, 
pcidev->bus, pcidev->dev, pcidev->func));
@@ -1140,7 +1151,7 @@ static int libxl_device_pci_remove_xenstore(struct 
libxl_ctx *ctx, uint32_t domi
 
     if (!is_hvm(ctx, domid)) {
         if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
-            XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready\n");
+            XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready");
             return -1;
         }
     }
@@ -1154,7 +1165,7 @@ static int libxl_device_pci_remove_xenstore(struct 
libxl_ctx *ctx, uint32_t domi
         }
     }
     if (i == num) {
-        XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore\n");
+        XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
         return -1;
     }
 
@@ -1196,7 +1207,7 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t 
domid, libxl_device_pci
         snprintf(path, sizeof(path), 
"/local/domain/0/device-model/%d/command", domid);
         xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
         if (libxl_wait_for_device_model(ctx, domid, "pci-inserted") < 0)
-            XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time\n");
+            XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
         snprintf(path, sizeof(path), 
"/local/domain/0/device-model/%d/parameter", domid);
         vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
         sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
@@ -1211,22 +1222,22 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, 
uint32_t domid, libxl_device_pci
         int i;
 
         if (f == NULL) {
-            XL_LOG(ctx, XL_LOG_ERROR, "Couldn't open %s\n", sysfs_path);
+            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
             return -1;
         }
         for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
-            fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
+            fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
             size = end - start + 1;
             if (start) {
                 if (flags & PCI_BAR_IO) {
                     rc = xc_domain_ioport_permission(ctx->xch, domid, start, 
size, 1);
                     if (rc < 0)
-                        XL_LOG(ctx, XL_LOG_ERROR, "Error: 
xc_domain_ioport_permission error 0x%x/0x%x:  %d\n", start, size, rc);
+                        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: 
xc_domain_ioport_permission error 0x%x/0x%x", start, size);
                 } else {
                     rc = xc_domain_iomem_permission(ctx->xch, domid, 
start>>XC_PAGE_SHIFT,
                                                     
(size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
                     if (rc < 0)
-                        XL_LOG(ctx, XL_LOG_ERROR, "Error: 
xc_domain_iomem_permission error 0x%x/0x%x:  %d\n", start, size, rc);
+                        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: 
xc_domain_iomem_permission error 0x%x/0x%x", start, size);
                 }
             }
         }
@@ -1235,25 +1246,25 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, 
uint32_t domid, libxl_device_pci
                                    pcidev->bus, pcidev->dev, pcidev->func);
         f = fopen(sysfs_path, "r");
         if (f == NULL) {
-            XL_LOG(ctx, XL_LOG_ERROR, "Couldn't open %s\n", sysfs_path);
+            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
             goto out;
         }
         fscanf(f, "%u", &irq);
         if (irq) {
             rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
             if (rc < 0) {
-                XL_LOG(ctx, XL_LOG_ERROR, "Error: xc_physdev_map_pirq irq=%d: 
%d\n", irq, rc);
+                XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: 
xc_physdev_map_pirq irq=%d", irq);
             }
             rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
             if (rc < 0) {
-                XL_LOG(ctx, XL_LOG_ERROR, "Error: xc_domain_irq_permission 
irq=%d: %d\n", irq, rc);
+                XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: 
xc_domain_irq_permission irq=%d", irq);
             }
         }
         fclose(f);
     }
 out:
     if ((rc = xc_assign_device(ctx->xch, domid, pcidev->value)) < 0)
-        XL_LOG(ctx, XL_LOG_ERROR, "Error: xc_assign_device error %d\n", rc);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
 
     libxl_device_pci_add_xenstore(ctx, domid, pcidev);
     return 0;
@@ -1280,7 +1291,7 @@ int libxl_device_pci_remove(struct libxl_ctx *ctx, 
uint32_t domid, libxl_device_
         snprintf(path, sizeof(path), 
"/local/domain/0/device-model/%d/command", domid);
         xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
         if (libxl_wait_for_device_model(ctx, domid, "pci-removed") < 0) {
-            XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time\n");
+            XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
             return -1;
         }
         snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", 
domid);
@@ -1294,7 +1305,7 @@ int libxl_device_pci_remove(struct libxl_ctx *ctx, 
uint32_t domid, libxl_device_
         int i;
 
         if (f == NULL) {
-            XL_LOG(ctx, XL_LOG_ERROR, "Couldn't open %s\n", sysfs_path);
+            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
             goto skip1;
         }
         for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
@@ -1304,12 +1315,12 @@ int libxl_device_pci_remove(struct libxl_ctx *ctx, 
uint32_t domid, libxl_device_
                 if (flags & PCI_BAR_IO) {
                     rc = xc_domain_ioport_permission(ctx->xch, domid, start, 
size, 0);
                     if (rc < 0)
-                        XL_LOG(ctx, XL_LOG_ERROR, "Error: 
xc_domain_ioport_permission error 0x%x/0x%x:  %d\n", start, size, rc);
+                        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, 
"xc_domain_ioport_permission error 0x%x/0x%x", start, size);
                 } else {
                     rc = xc_domain_iomem_permission(ctx->xch, domid, 
start>>XC_PAGE_SHIFT,
                                                     
(size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
                     if (rc < 0)
-                        XL_LOG(ctx, XL_LOG_ERROR, "Error: 
xc_domain_iomem_permission error 0x%x/0x%x:  %d\n", start, size, rc);
+                        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, 
"xc_domain_iomem_permission error 0x%x/0x%x", start, size);
                 }
             }
         }
@@ -1319,18 +1330,18 @@ skip1:
                                    pcidev->bus, pcidev->dev, pcidev->func);
         f = fopen(sysfs_path, "r");
         if (f == NULL) {
-            XL_LOG(ctx, XL_LOG_ERROR, "Couldn't open %s\n", sysfs_path);
+            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
             goto out;
         }
         fscanf(f, "%u", &irq);
         if (irq) {
             rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
             if (rc < 0) {
-                XL_LOG(ctx, XL_LOG_ERROR, "Error: xc_physdev_map_pirq irq=%d: 
%d\n", irq, rc);
+                XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq 
irq=%d", irq);
             }
             rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
             if (rc < 0) {
-                XL_LOG(ctx, XL_LOG_ERROR, "Error: xc_domain_irq_permission 
irq=%d: %d\n", irq, rc);
+                XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, 
"xc_domain_irq_permission irq=%d", irq);
             }
         }
         fclose(f);
@@ -1341,7 +1352,7 @@ out:
     libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, 
pcidev->func);
 
     if ((rc = xc_deassign_device(ctx->xch, domid, pcidev->value)) < 0)
-        XL_LOG(ctx, XL_LOG_ERROR, "Error: xc_deassign_device error %d\n", rc);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
     return 0;
 }
 
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index eee0a40..82a2a30 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -92,7 +92,7 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
         else
-            XL_LOG(ctx, XL_LOG_ERROR, "xs transaction failed errno=%d\n", 
errno);
+            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs transaction failed");
     }
     return 0;
 }
@@ -213,7 +213,7 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t 
domid, int force)
     path = libxl_sprintf(ctx, "/local/domain/%d/device", domid);
     l1 = libxl_xs_directory(ctx, XBT_NULL, path, &num1);
     if (!l1) {
-        XL_LOG(ctx, XL_LOG_ERROR, "%s is empty\n", path);
+        XL_LOG(ctx, XL_LOG_ERROR, "%s is empty", path);
         return -1;
     }
     for (i = 0; i < num1; i++) {
@@ -248,7 +248,7 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t 
domid, int force)
                     if (!state || atoi(state) == 6) {
                         xs_unwatch(ctx->xsh, l1[0], l1[1]);
                         xs_rm(ctx->xsh, XBT_NULL, l1[1]);
-                        XL_LOG(ctx, XL_LOG_DEBUG, "Destroyed device backend at 
%s\n", l1[1]);
+                        XL_LOG(ctx, XL_LOG_DEBUG, "Destroyed device backend at 
%s", l1[1]);
                         n_watches--;
                     }
                 }
@@ -267,15 +267,20 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t 
domid, int force)
 int libxl_device_pci_flr(struct libxl_ctx *ctx, unsigned int domain, unsigned 
int bus,
                          unsigned int dev, unsigned int func)
 {
+    char *do_flr= "/sys/bus/pci/drivers/pciback/do_flr";
     FILE *fd;
 
-    fd = fopen("/sys/bus/pci/drivers/pciback/do_flr", "w");
+    fd = fopen(do_flr, "w");
     if (fd != NULL) {
         fprintf(fd, PCI_BDF, domain, bus, dev, func);
         fclose(fd);
         return 0;
     }
-    XL_LOG(ctx, XL_LOG_ERROR, "Pciback doesn't support do_flr, cannot flr the 
device\n");
+    if (errno == ENOENT) {
+        XL_LOG(ctx, XL_LOG_ERROR, "Pciback doesn't support do_flr, cannot flr 
the device");
+    } else {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Failed to access pciback path %s", 
do_flr);
+    }
     return -1;
 }
 
@@ -303,7 +308,7 @@ int libxl_wait_for_device_model(struct libxl_ctx *ctx, 
uint32_t domid, char *sta
             }
         }
     }
-    XL_LOG(ctx, XL_LOG_ERROR, "Device Model not ready\n");
+    XL_LOG(ctx, XL_LOG_ERROR, "Device Model not ready");
     return -1;
 }
 
@@ -317,7 +322,13 @@ int libxl_wait_for_backend(struct libxl_ctx *ctx, char 
*be_path, char *state)
     while (watchdog > 0) {
         p = xs_read(ctx->xsh, XBT_NULL, path, &len);
         if (p == NULL) {
-            XL_LOG(ctx, XL_LOG_ERROR, "Backend %s does not exist\n", be_path);
+            if (errno == ENOENT) {
+                XL_LOG(ctx, XL_LOG_ERROR, "Backend %s does not exist",
+                       be_path);
+            } else {
+                XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Failed to access backend %s",
+                       be_path);
+            }
             return -1;
         } else {
             if (!strcmp(p, state)) {
@@ -328,7 +339,7 @@ int libxl_wait_for_backend(struct libxl_ctx *ctx, char 
*be_path, char *state)
             }
         }
     }
-    XL_LOG(ctx, XL_LOG_ERROR, "Backend %s not ready\n", be_path);
+    XL_LOG(ctx, XL_LOG_ERROR, "Backend %s not ready", be_path);
     return -1;
 }
 
diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index a98daee..eaea6f3 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -83,6 +83,9 @@ int build_post(struct libxl_ctx *ctx, uint32_t domid,
     ents[9] = libxl_sprintf(ctx, "%lu", state->store_mfn);
 
     dom_path = libxl_xs_get_dompath(ctx, domid);
+    if (!dom_path)
+        return ERROR_FAIL;
+    
     vm_path = xs_read(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "%s/vm", 
dom_path), NULL);
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
@@ -107,7 +110,7 @@ int build_pv(struct libxl_ctx *ctx, uint32_t domid,
 
     dom = xc_dom_allocate(info->u.pv.cmdline, info->u.pv.features);
     if (!dom) {
-        XL_LOG(ctx, XL_LOG_ERROR, "xc_dom_allocate failed: %d", dom);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, dom, "xc_dom_allocate failed");
         return -1;
     }
     if ((ret = xc_dom_linux_build(ctx->xch, dom, domid, info->max_memkb / 1024,
@@ -115,7 +118,7 @@ int build_pv(struct libxl_ctx *ctx, uint32_t domid,
                                   state->store_port, &state->store_mfn,
                                   state->console_port, &state->console_mfn)) 
!= 0) {
         xc_dom_release(dom);
-        XL_LOG(ctx, XL_LOG_ERROR, "xc_dom_linux_build failed: %d", ret);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "xc_dom_linux_build failed");
         return -2;
     }
     xc_dom_release(dom);
@@ -129,7 +132,7 @@ int build_hvm(struct libxl_ctx *ctx, uint32_t domid,
 
     ret = xc_hvm_build(ctx->xch, domid, info->max_memkb / 1024, info->kernel);
     if (ret) {
-        XL_LOG(ctx, XL_LOG_ERROR, "hvm building failed: %d", ret);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "hvm building failed");
         return ERROR_FAIL;
     }
     ret = hvm_build_set_params(ctx->xch, domid, info->u.hvm.apic, 
info->u.hvm.acpi,
@@ -137,7 +140,7 @@ int build_hvm(struct libxl_ctx *ctx, uint32_t domid,
                                info->max_vcpus,
                                state->store_port, &state->store_mfn);
     if (ret) {
-        XL_LOG(ctx, XL_LOG_ERROR, "hvm build set params failed: %d", ret);
+        XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "hvm build set params failed");
         return ERROR_FAIL;
     }
     xc_cpuid_apply_policy(ctx->xch, domid);
diff --git a/tools/libxl/libxl_exec.c b/tools/libxl/libxl_exec.c
index 8d7928b..8a589b6 100644
--- a/tools/libxl/libxl_exec.c
+++ b/tools/libxl/libxl_exec.c
@@ -28,7 +28,7 @@ int libxl_exec(struct libxl_ctx *ctx, int stdinfd, int 
stdoutfd, int stderrfd,
 
     pid = fork();
     if (pid == -1) {
-        XL_LOG(ctx, XL_LOG_ERROR, "fork failed");
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
         return -1;
     }
     if (pid == 0) {
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 407cf22..7ba0c97 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -146,14 +146,41 @@ char *libxl_dirname(struct libxl_ctx *ctx, const char *s)
     return ptr;
 }
 
-void xl_log(struct libxl_ctx *ctx, int loglevel, const char *file, int line, 
const char *func, char *fmt, ...)
+void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval,
+             const char *file, int line, const char *func,
+             char *fmt, va_list ap)
 {
-    va_list ap;
+    char *enomem = "[out of memory formatting log message]";
     char *s;
-    va_start(ap, fmt);
-    vasprintf(&s, fmt, ap);
-    va_end(ap);
+    int rc;
+
+    rc = vasprintf(&s, fmt, ap);
+    if (rc<0) { s = enomem; goto x; }
+
+    if (errnoval >= 0) {
+        char *errstr, *snew;
+        errstr = strerror(errnoval);
+        if (errstr)
+            rc = asprintf(&snew, "%s: %s", s, errstr);
+        else
+            rc = asprintf(&snew, "%s: unknown error number %d", s, errnoval);
+        free(s);
+        if (rc<0) { s = enomem; goto x; }
+        s = snew;
+    }
 
+ x:
     ctx->log_callback(ctx->log_userdata, loglevel, file, line, func, s);
-    free(s);
+    if (s != enomem)
+        free(s);
+}
+
+void xl_log(struct libxl_ctx *ctx, int loglevel, int errnoval,
+            const char *file, int line,
+            const char *func, char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    xl_logv(ctx, loglevel, errnoval, file, line, func, fmt, ap);
+    va_end(ap);
 }
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index d6835be..fe6aa00 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -37,9 +37,13 @@
 #define XL_LOGGING_ENABLED
 
 #ifdef XL_LOGGING_ENABLED
-#define XL_LOG(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, __FILE__, 
__LINE__, __func__, _f, ##_a)
+#define XL_LOG(ctx, loglevel, _f, _a...)   xl_log(ctx, -1, loglevel, __FILE__, 
__LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)   xl_log(ctx, errno, loglevel, 
__FILE__, __LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...)   xl_log(ctx, 
errnoval, loglevel, __FILE__, __LINE__, __func__, _f, ##_a)
 #else
 #define XL_LOG(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
 #endif
 
 #define XL_LOG_DEBUG 3
@@ -47,7 +51,8 @@
 #define XL_LOG_WARNING 1
 #define XL_LOG_ERROR 0
 
-void xl_log(struct libxl_ctx *ctx, int loglevel, const char *file, int line, 
const char *func, char *fmt, ...);
+void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char 
*file, int line, const char *func, char *fmt, va_list al);
+void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char 
*file, int line, const char *func, char *fmt, ...);
 
 struct libxl_domain_build_state_ {
     uint32_t store_port;
@@ -97,7 +102,7 @@ int libxl_xs_writev(struct libxl_ctx *ctx, xs_transaction_t 
t,
                     char *dir, char **kvs);
 int libxl_xs_write(struct libxl_ctx *ctx, xs_transaction_t t,
                    char *path, char *fmt, ...);
-char *libxl_xs_get_dompath(struct libxl_ctx *ctx, uint32_t domid);
+char *libxl_xs_get_dompath(struct libxl_ctx *ctx, uint32_t domid); // logs errs
 char *libxl_xs_read(struct libxl_ctx *ctx, xs_transaction_t t, char *path);
 char **libxl_xs_directory(struct libxl_ctx *ctx, xs_transaction_t t, char 
*path, unsigned int *nb);
 
diff --git a/tools/libxl/libxl_xshelp.c b/tools/libxl/libxl_xshelp.c
index 6286d02..f59eee7 100644
--- a/tools/libxl/libxl_xshelp.c
+++ b/tools/libxl/libxl_xshelp.c
@@ -95,6 +95,11 @@ char * libxl_xs_read(struct libxl_ctx *ctx, xs_transaction_t 
t, char *path)
 char *libxl_xs_get_dompath(struct libxl_ctx *ctx, uint32_t domid)
 {
     char *s = xs_get_domain_path(ctx->xsh, domid);
+    if (!s) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to get dompath for %lu",
+                     domid);
+        return NULL;
+    }
     libxl_ptr_add(ctx, s);
     return s;
 }

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel