libxenlight: Clean up logging arrangements
PATCH 1/1 libxenlight: Clean up logging arrangements
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.)
tools/libxl/libxl.c +51 -40
@@ -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, libxlif (!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 domiif (!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_pcisnprintf (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_pciint 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 ; }
tools/libxl/libxl_device.c +19 -8
@@ -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 ; }
tools/libxl/libxl_dom.c +7 -4
@@ -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);
tools/libxl/libxl_exec.c +1 -1
@@ -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 ) {
tools/libxl/libxl_internal.c +33 -6
@@ -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); }
tools/libxl/libxl_internal.h +8 -3
@@ -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);
tools/libxl/libxl_xshelp.c +5 -0
@@ -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; }
Re: [PATCH] libxenlight: correct broken osdeps.[ch]
Quoted 1 line · I wrote:
libxenlight: Clean up logging arrangements
Here is a follow-on patch, which may not apply cleanly without the logging arrangements patch applied first.
libxenlight: correct broken osdeps.[ch] and make #includes consistent
osdep.[hc] previously mistakenly declared and defined [v]asprintf. These functions are available in the libc on most platforms.
So now, instead:
* _GNU_SOURCE is #defined in osdep.h so that we get the system [v]asprintf (and various other functions)
* osdep.h is included first in every libxl*.c file (it needs to be before any system headers so that _GNU_SOURCE) takes effect.
* osdep.[hc] only provide their own reimplementation of [v]asprintf if NEED_OWN_ASPRINTF is defined. Currently it is not ever defined but this is provided for any platform which needs it.
* While I was editing the #includes in each .c file, I put them all into the same order: "osdep.h", then system headers, then local headers.
tools/libxl/Makefile +1 -2
@@ -23,8 +23,7 @@ LIBCONFIG_URL ?= http://www.hyperrealm.com/libconfig LIBCONFIG_SOURCE = libconfig-1.3.2 LIBCONFIG_OUTPUT = $(LIBCONFIG_SOURCE)/.libs -LIBXL_OBJS-y = -LIBXL_OBJS-$(CONFIG_Linux) += osdeps.o +LIBXL_OBJS-y = osdeps.o LIBXL_OBJS = flexarray.o libxl.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o xenguest.o libxl_utils.o $(LIBXL_OBJS-y) CLIENTS = xl
tools/libxl/libxl.c +3 -0
@@ -14,6 +14,8 @@* GNU Lesser General Public License for more details. */ +#include "osdep.h" +#include <stdio.h> #include <string.h> #include <stdlib.h>
@@ -25,6 +27,7 @@#include <unistd.h> /* for write, unlink and close */ #include <stdint.h> #include <inttypes.h> +#include "libxl.h" #include "libxl_utils.h" #include "libxl_internal.h"
tools/libxl/libxl_device.c +5 -2
@@ -14,15 +14,18 @@* GNU Lesser General Public License for more details. */ +#include "osdep.h" +#include <string.h> #include <stdio.h> -#include "libxl.h" -#include "libxl_internal.h" #include <sys/time.h> /* for struct timeval */ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> +#include "libxl.h" +#include "libxl_internal.h" +char *string_of_kinds[] = { [DEVICE_VIF ] ="vif" , [DEVICE_VBD ] ="vbd" ,
tools/libxl/libxl_dom.c +5 -2
@@ -13,8 +13,8 @@* GNU Lesser General Public License for more details. */ -#include "libxl.h" -#include "libxl_internal.h" +#include "osdep.h" +#include <stdio.h> #include <inttypes.h> #include <xenguest.h>
@@ -24,6 +24,9 @@#include <sys/time.h> /* for struct timeval */ #include <unistd.h> /* for sleep(2) */ +#include "libxl.h" +#include "libxl_internal.h" +int is_hvm (struct libxl_ctx *ctx,uint32_t domid) {xc_domaininfo_t info;
tools/libxl/libxl_exec.c +3 -0
@@ -15,9 +15,12 @@* GNU Lesser General Public License for more details. */ +#include "osdep.h" +#include <stdio.h> #include <unistd.h> #include <stdlib.h> +#include "libxl.h" #include "libxl_internal.h"
tools/libxl/libxl_internal.c +7 -3
@@ -12,13 +12,17 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ -#include "libxl.h" -#include "libxl_internal.h" -#include "libxl_utils.h" + +#include "osdep.h" +#include <stdio.h> #include <stdarg.h> #include <string.h> +#include "libxl.h" +#include "libxl_internal.h" +#include "libxl_utils.h" +int libxl_error_set (struct libxl_ctx *ctx,int code) {return 0 ;
tools/libxl/libxl_utils.c +4 -2
@@ -13,8 +13,8 @@* GNU Lesser General Public License for more details. */ -#include "libxl_utils.h" -#include "libxl_internal.h" +#include "osdep.h" +#include <stdio.h> #include <stdlib.h> #include <stdint.h>
@@ -24,6 +24,8 @@#include <ctype.h> #include <errno.h> +#include "libxl_utils.h" +#include "libxl_internal.h" unsigned long libxl_get_required_shadow_memory (unsigned long maxmem_kb,unsigned int smp_cpus)
tools/libxl/libxl_xshelp.c +5 -2
@@ -13,13 +13,16 @@* GNU Lesser General Public License for more details. */ +#include "osdep.h" +#include <string.h> #include <stddef.h> -#include "libxl.h" -#include "libxl_internal.h" #include <stdio.h> #include <stdarg.h> +#include "libxl.h" +#include "libxl_internal.h" +char **libxl_xs_kvs_of_flexarray (struct libxl_ctx *ctx,flexarray_t *array,int length) {char **kvs;
tools/libxl/osdeps.c +4 -0
@@ -19,6 +19,8 @@#include <sys/time.h> #include <stdlib.h> +#ifdef NEED_OWN_ASPRINTF +int vasprintf (char **buffer,const char *fmt,va_list ap) {int size =0 ;
@@ -60,3 +62,5 @@ int asprintf(char **buffer, char *fmt, ...)va_end (ap); return status; } + +#endif
tools/libxl/osdeps.h +8 -1
@@ -13,12 +13,19 @@* GNU Lesser General Public License for more details. */ +/* +* This header must be included first, before any system headers, +* so that _GNU_SOURCE takes effect properly. +*/ +#ifndef LIBXL_OSDEP #define LIBXL_OSDEP +#define _GNU_SOURCE + +#ifdef NEED_OWN_ASPRINTF #include <stdarg.h> -#if defined(__linux__)int asprintf (char **buffer,char *fmt, ...);int vasprintf (char **buffer,const char *fmt,va_list ap);#endif
tools/libxl/xl.c +3 -2
@@ -14,8 +14,6 @@* GNU Lesser General Public License for more details. */ -#include "libxl.h" -#include "libxl_utils.h" #include <stdio.h> #include <stdlib.h> #include <string.h>
@@ -29,6 +27,9 @@#include <arpa/inet.h> #include <xenctrl.h> +#include "libxl.h" +#include "libxl_utils.h" +void log_callback (void *userdata,int loglevel,const char *file,int line,const char *func,char *s) {fprintf (stderr,"[%d] %s:%d:%s: %s\n" , loglevel, file, line, func, s);
Re: [PATCH] libxenlight: correct broken osdeps.[ch]
Quoted 11 lines · I wrote:
Quoted 1 line · I wrote:
libxenlight: Clean up logging arrangements
Here is a follow-on patch, which may not apply cleanly without the logging arrangements patch applied first.
libxenlight: correct broken osdeps.[ch] and make #includes consistent
osdep.[hc] previously mistakenly declared and defined [v]asprintf. These functions are available in the libc on most platforms.
Please don't apply this patch. I've just discovered that my arrangements for test-build and testing are broken (they don't detect FTBFS) and that this patch doesn't build.
Ian.
Quoted 12 lines · Ian Jackson
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.
Quoted 8 lines
* When
libxl_xs_get_dompathfails, 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 <[email protected]>
both patches look good.
Thanks,
Signature · 1 line
Vincent
Vincent Hanquez writes ("Re: [Xen-devel] [PATCH] libxenlight: Clean up logging arrangements"):
Quoted 2 lines
Quoted 1 line · Ian Jackson
libxenlight: Clean up logging arrangements
...
Quoted 5 lines
Quoted 1 line
Signed-off-by Ian Jackson <[email protected]>both patches look good.
Acked-by Vincent Hanquez <[email protected]>
Thanks. Here is v2 of the osdep.[ch] patch, which actually builds this time (!).
libxenlight: correct broken osdeps.[ch] and make #includes consistent
osdeps.[hc] previously mistakenly declared and defined [v]asprintf. These functions are available in the libc on most platforms. Also, osdeps.h is used by xc.c but xc.c is not part of the library, so osdeps.h is part of the public interface and should have a better name.
So now, instead:
* osdeps.h is libxl_osdeps.h.
* _GNU_SOURCE is #defined in libxl_osdeps.h so that we get the system [v]asprintf (and various other functions)
* libxl_osdeps.h is included first in every libxl*.c file (it needs to be before any system headers so that _GNU_SOURCE) takes effect.
* osdeps.[hc] only provide their own reimplementation of [v]asprintf if NEED_OWN_ASPRINTF is defined. Currently it is not ever defined but this is provided for any platform which needs it.
* While I was editing the #includes in each .c file, I put them all into the same order: "libxl_osdeps.h", then system headers, then local headers.
* xs.h is included in libxl.h. This is needed for "bool"; it has to not be typedefed in libxl.h because otherwise we get a duplicate definition when including xs.h.
tools/libxl/Makefile +1 -2
@@ -23,8 +23,7 @@ LIBCONFIG_URL ?= http://www.hyperrealm.com/libconfig LIBCONFIG_SOURCE = libconfig-1.3.2 LIBCONFIG_OUTPUT = $(LIBCONFIG_SOURCE)/.libs -LIBXL_OBJS-y = -LIBXL_OBJS-$(CONFIG_Linux) += osdeps.o +LIBXL_OBJS-y = osdeps.o LIBXL_OBJS = flexarray.o libxl.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o xenguest.o libxl_utils.o $(LIBXL_OBJS-y) CLIENTS = xl
tools/libxl/libxl.c +3 -0
@@ -14,6 +14,8 @@* GNU Lesser General Public License for more details. */ +#include "libxl_osdeps.h" +#include <stdio.h> #include <string.h> #include <stdlib.h>
@@ -25,6 +27,7 @@#include <unistd.h> /* for write, unlink and close */ #include <stdint.h> #include <inttypes.h> +#include "libxl.h" #include "libxl_utils.h" #include "libxl_internal.h"
tools/libxl/libxl.h +2 -3
@@ -15,14 +15,13 @@#ifndef LIBXL_H #define LIBXL_H -#include "osdeps.h" #include <stdint.h> #include <stdarg.h> #include <netinet/in.h> #include <xenctrl.h> -#include "xen_uuid.h" +#include <xs.h> -typedef int bool ; +#include "xen_uuid.h" typedef void (*libxl_log_callback )(void *userdata,int loglevel,const char *file,int line,const char *func,char *s);
tools/libxl/libxl_device.c +5 -2
@@ -14,15 +14,18 @@* GNU Lesser General Public License for more details. */ +#include "libxl_osdeps.h" +#include <string.h> #include <stdio.h> -#include "libxl.h" -#include "libxl_internal.h" #include <sys/time.h> /* for struct timeval */ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> +#include "libxl.h" +#include "libxl_internal.h" +char *string_of_kinds[] = { [DEVICE_VIF ] ="vif" , [DEVICE_VBD ] ="vbd" ,
tools/libxl/libxl_dom.c +10 -6
@@ -13,17 +13,21 @@* GNU Lesser General Public License for more details. */ -#include "libxl.h" -#include "libxl_internal.h" +#include "libxl_osdeps.h" +#include <stdio.h> #include <inttypes.h> -#include <xenguest.h> -#include <xenctrl.h> -#include <xc_dom.h> #include <string.h> #include <sys/time.h> /* for struct timeval */ #include <unistd.h> /* for sleep(2) */ +#include <xenctrl.h> +#include <xc_dom.h> +#include <xenguest.h> + +#include "libxl.h" +#include "libxl_internal.h" +int is_hvm (struct libxl_ctx *ctx,uint32_t domid) {xc_domaininfo_t info;
@@ -110,7 +114,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_ERRNOVAL (ctx,XL_LOG_ERROR , dom,"xc_dom_allocate failed" ); +XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xc_dom_allocate failed" );return -1 ; } if ((ret = xc_dom_linux_build(ctx->xch, dom, domid, info->max_memkb / 1024,
tools/libxl/libxl_exec.c +3 -0
@@ -15,9 +15,12 @@* GNU Lesser General Public License for more details. */ +#include "libxl_osdeps.h" +#include <stdio.h> #include <unistd.h> #include <stdlib.h> +#include "libxl.h" #include "libxl_internal.h"
tools/libxl/libxl_internal.c +7 -3
@@ -12,13 +12,17 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ -#include "libxl.h" -#include "libxl_internal.h" -#include "libxl_utils.h" + +#include "libxl_osdeps.h" +#include <stdio.h> #include <stdarg.h> #include <string.h> +#include "libxl.h" +#include "libxl_internal.h" +#include "libxl_utils.h" +int libxl_error_set (struct libxl_ctx *ctx,int code) {return 0 ;
tools/libxl/libxl_osdeps.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* Copyright (C) 2009 Citrix Ltd. +* Author Stefano Stabellini <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published +* by the Free Software Foundation; version 2.1 only. with the special +* exception on linking described in file LICENSE. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +*/ + +/* +* This header must be included first, before any system headers, +* so that _GNU_SOURCE takes effect properly. +*/ + +#ifndef LIBXL_OSDEP +#define LIBXL_OSDEP + +#define _GNU_SOURCE + +#ifdef NEED_OWN_ASPRINTF +#include <stdarg.h> + +int asprintf (char **buffer,char *fmt, ...); +int vasprintf (char **buffer,const char *fmt,va_list ap); +#endif /*NEED_OWN_ASPRINTF*/ + +#endif
tools/libxl/libxl_utils.c +4 -2
@@ -13,8 +13,8 @@* GNU Lesser General Public License for more details. */ -#include "libxl_utils.h" -#include "libxl_internal.h" +#include "libxl_osdeps.h" +#include <stdio.h> #include <stdlib.h> #include <stdint.h>
@@ -24,6 +24,8 @@#include <ctype.h> #include <errno.h> +#include "libxl_utils.h" +#include "libxl_internal.h" unsigned long libxl_get_required_shadow_memory (unsigned long maxmem_kb,unsigned int smp_cpus)
tools/libxl/libxl_xshelp.c +5 -2
@@ -13,13 +13,16 @@* GNU Lesser General Public License for more details. */ +#include "libxl_osdeps.h" +#include <string.h> #include <stddef.h> -#include "libxl.h" -#include "libxl_internal.h" #include <stdio.h> #include <stdarg.h> +#include "libxl.h" +#include "libxl_internal.h" +char **libxl_xs_kvs_of_flexarray (struct libxl_ctx *ctx,flexarray_t *array,int length) {char **kvs;
tools/libxl/osdeps.c +4 -0
@@ -19,6 +19,8 @@#include <sys/time.h> #include <stdlib.h> +#ifdef NEED_OWN_ASPRINTF +int vasprintf (char **buffer,const char *fmt,va_list ap) {int size =0 ;
@@ -60,3 +62,5 @@ int asprintf(char **buffer, char *fmt, ...)va_end (ap); return status; } + +#endif
tools/libxl/osdeps.h deleted +0 -26
@@ -1,26 +0,0 @@ -/* -* Copyright (C) 2009 Citrix Ltd. -* Author Stefano Stabellini <[email protected]> -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU Lesser General Public License as published -* by the Free Software Foundation; version 2.1 only. with the special -* exception on linking described in file LICENSE. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Lesser General Public License for more details. -*/ - -#ifndef LIBXL_OSDEP -#define LIBXL_OSDEP - -#include <stdarg.h> - -#if defined(__linux__) -int asprintf (char **buffer,char *fmt, ...); -int vasprintf (char **buffer,const char *fmt,va_list ap); -#endif - -#endif
tools/libxl/xl.c +5 -2
@@ -14,8 +14,8 @@* GNU Lesser General Public License for more details. */ -#include "libxl.h" -#include "libxl_utils.h" +#include "libxl_osdeps.h" +#include <stdio.h> #include <stdlib.h> #include <string.h>
@@ -29,6 +29,9 @@#include <arpa/inet.h> #include <xenctrl.h> +#include "libxl.h" +#include "libxl_utils.h" +void log_callback (void *userdata,int loglevel,const char *file,int line,const char *func,char *s) {fprintf (stderr,"[%d] %s:%d:%s: %s\n" , loglevel, file, line, func, s);