libxl: make is_hvm log on errors, and make callers check its error returns
PATCH 1/1 libxl: make is_hvm log on errors, and make callers check its error returns
--- tools/libxl/libxl.c | 31 +++++++++++++++++++++---------- tools/libxl/libxl_dom.c | 14 ++++++++++---- tools/libxl/libxl_pci.c | 14 ++++++++------ 3 files changed, 39 insertions(+), 20 deletions(-)
tools/libxl/libxl.c +21 -10
@@ -427,9 +427,10 @@ out:int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid) {libxl_gc gc =LIBXL_INIT_GC (ctx); -int rc =0 ; +int hvm, rc =0 ; -if (is_hvm (ctx, domid)) { + hvm =is_hvm (ctx, domid);if (hvm<0 ) { rc = hvm; goto out; } +if (hvm) {XL_LOG (ctx,XL_LOG_DEBUG ,"Called domain_resume on " "non-cooperative hvm domain %u" , domid); rc =ERROR_NI ;
@@ -645,11 +646,13 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)int libxl_domain_suspend (libxl_ctx *ctx,libxl_domain_suspend_info *info,uint32_t domid,int fd) { -int hvm =is_hvm (ctx, domid); +int hvm;int live = info !=NULL && info->flags &XL_SUSPEND_LIVE ;int debug = info !=NULL && info->flags &XL_SUSPEND_DEBUG ;int rc =0 ; + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm; +core_suspend (ctx, domid, fd, hvm, live, debug);if (hvm) rc =save_device_model (ctx, domid, fd);
@@ -685,9 +688,10 @@ int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)libxl_gc gc =LIBXL_INIT_GC (ctx);char *path;char *state; -int ret, rc =0 ; +int hvm, ret, rc =0 ; -if (is_hvm (ctx, domid)) { + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm; +if (hvm) { path =libxl_sprintf (&gc,"/local/domain/0/device-model/%d/state" , domid); state =libxl_xs_read (&gc,XBT_NULL , path);if (state !=NULL && !strcmp (state,"paused" )) {
@@ -717,6 +721,7 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)libxl_gc gc =LIBXL_INIT_GC (ctx);char *shutdown_path;char *dom_path; +int hvm, rc =0 ;if (req >ARRAY_SIZE (req_table)) {libxl_free_all (&gc);
@@ -732,7 +737,8 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req) shutdown_path =libxl_sprintf (&gc,"%s/control/shutdown" , dom_path);xs_write (ctx->xsh,XBT_NULL , shutdown_path, req_table[req],strlen (req_table[req])); -if (is_hvm (ctx,domid)) { + hvm =is_hvm (ctx,domid);if (hvm<0 ) { rc = hvm; goto out; } +if (hvm) {unsigned long acpi_s_state =0 ;unsigned long pvdriver =0 ;int ret;
@@ -754,8 +760,9 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req) } } } + out:libxl_free_all (&gc); -return 0 ; +return rc; }int libxl_get_wait_fd (libxl_ctx *ctx,int *fd)
@@ -920,9 +927,10 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)libxl_gc gc =LIBXL_INIT_GC (ctx);char *dom_path;char *vm_path; -int rc, dm_present; +int rc, hvm, dm_present; -if (is_hvm (ctx, domid)) { + hvm =is_hvm (ctx, domid);if (hvm<0 ) { rc = hvm; goto out; } +if (hvm) { dm_present =1 ; }else {char *pid;
@@ -1004,11 +1012,14 @@ out:int libxl_primary_console_exec (libxl_ctx *ctx,uint32_t domid_vm) { +int hvm;uint32_t stubdomid =libxl_get_stubdom_id (ctx, domid_vm); +if (stubdomid)return libxl_console_exec (ctx, stubdomid,1 ,LIBXL_CONSTYPE_PV );else { -if (is_hvm (ctx, domid_vm)) + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm; +if (hvm)return libxl_console_exec (ctx, domid_vm,0 ,LIBXL_CONSTYPE_SERIAL );else return libxl_console_exec (ctx, domid_vm,0 ,LIBXL_CONSTYPE_PV );
tools/libxl/libxl_dom.c +10 -4
@@ -37,10 +37,16 @@ int is_hvm(libxl_ctx *ctx, uint32_t domid)int ret; ret =xc_domain_getinfolist (ctx->xch, domid,1 , &info); -if (ret !=1 ) -return -1 ; -if (info.domain != domid) -return -1 ; +if (ret !=1 ) { +XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"is_hvm: xc_domain_getinfolist failed" ); +return ERROR_FAIL ; + } +if (info.domain != domid) { +XL_LOG (ctx,XL_LOG_ERROR ,"is_hvm: xc_domain_getinfolist gave info " +"for %" PRIu32"d when we asked for %" PRIu32"d" , + info.domain, domid); +return ERROR_FAIL ; + }return !!(info.flags & XEN_DOMINF_hvm_guest); }
tools/libxl/libxl_pci.c +8 -6
@@ -266,7 +266,7 @@ static int libxl_device_pci_add_xenstore(libxl_gc *gc, uint32_t domid, libxl_devlibxl_ctx *ctx =libxl_gc_owner (gc);flexarray_t *back;char *num_devs, *be_path; -int num =0 ; +int hvm, num =0 ;unsigned int boffset =0 ;xs_transaction_t t;
@@ -275,7 +275,8 @@ static int libxl_device_pci_add_xenstore(libxl_gc *gc, uint32_t domid, libxl_devif (!num_devs)return libxl_create_pci_backend (gc, domid, pcidev,1 ); -if (!is_hvm (ctx, domid)) { + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm; +if (!hvm) {if (libxl_wait_for_backend (ctx, be_path,"4" ) <0 )return ERROR_FAIL ; }
@@ -330,7 +331,8 @@ static int libxl_device_pci_remove_xenstore(libxl_gc *gc, uint32_t domid, libxl_return ERROR_INVAL ; num =atoi (num_devs); -if (!is_hvm (ctx, domid)) { + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm; +if (!hvm) {if (libxl_wait_for_backend (ctx, be_path,"4" ) <0 ) {XL_LOG (ctx,XL_LOG_DEBUG ,"pci backend at %s is not ready" , be_path);return ERROR_FAIL ;
@@ -358,7 +360,7 @@ retry_transaction:if (errno ==EAGAIN ) goto retry_transaction; -if (!is_hvm (ctx, domid)) { +if (!hvm) {if (libxl_wait_for_backend (ctx, be_path,"4" ) <0 ) {XL_LOG (ctx,XL_LOG_DEBUG ,"pci backend at %s is not ready" , be_path);return ERROR_FAIL ;
@@ -610,7 +612,7 @@ static int do_pci_add(libxl_gc *gc, uint32_t domid, libxl_device_pci *pcidev)char *state, *vdevfn;int rc, hvm; - hvm =is_hvm (ctx, domid); + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm;if (hvm) {if (libxl_wait_for_device_model (ctx, domid,"running" ,NULL ,NULL ) <0 ) {return ERROR_FAIL ;
@@ -834,7 +836,7 @@ static int do_pci_remove(libxl_gc *gc, uint32_t domid, libxl_device_pci *pcidev)libxl_device_pci_remove_xenstore (gc, domid, pcidev); - hvm =is_hvm (ctx, domid); + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm;if (hvm) {if (libxl_wait_for_device_model (ctx, domid,"running" ,NULL ,NULL ) <0 ) {return ERROR_FAIL ;
Signature · 1 line
1.5.6.5
Quoted 26 lines · Ian Jackson
Signed-off-by Ian Jackson <[email protected]>--- tools/libxl/libxl.c | 31 +++++++++++++++++++++---------- tools/libxl/libxl_dom.c | 14 ++++++++++---- tools/libxl/libxl_pci.c | 14 ++++++++------ 3 files changed, 39 insertions(+), 20 deletions(-)tools/libxl/libxl.c +3
-2@@ -427,9 +427,10 @@ out:int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid) {libxl_gc gc =LIBXL_INIT_GC (ctx); -int rc =0 ; +int hvm, rc =0 ; -if (is_hvm (ctx, domid)) { + hvm =is_hvm (ctx, domid);if (hvm<0 ) { rc = hvm; goto out; } +if (hvm) {XL_LOG (ctx,XL_LOG_DEBUG ,"Called domain_resume on " "non-cooperative hvm domain %u" , domid); rc =ERROR_NI ;@@ -645,11 +646,13 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm) int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
tools/libxl/libxl.c libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm) · line 655
Quoted 10 lines
tools/libxl/libxl.c +3
-1uint32_t domid, int fd) { - int hvm = is_hvm(ctx, domid); + int hvm;int live = info !=NULL && info->flags &XL_SUSPEND_LIVE ;int debug = info !=NULL && info->flags &XL_SUSPEND_DEBUG ;int rc =0 ; + hvm =is_hvm (ctx, domid);if (hvm<0 )return hvm; +
I hate this coding style, it's really easy to miss whats going on when scanning code and two things are done on one line. I think either lines ought to be added or change declaration to:
int get_vm_mode(libxl_ctx, int domid, int *hvm);
so at least compiler can tell you if you're using it the way it was intended.
Gianni
Gianni Tedesco (3P) writes ("Re: [Xen-devel] [PATCH] libxl: make is_hvm log on errors, and make callers check its error returns"):
Quoted 8 lines
I hate this coding style, it's really easy to miss whats going on when scanning code and two things are done on one line. I think either lines ought to be added or change declaration to:
int
get_vm_mode(libxl_ctx, int domid, int *hvm);so at least compiler can tell you if you're using it the way it was intended.
I'd be happy with the latter proposal. I don't like anything that increases the code size. Shall I resend my patch ?
Ian.
Quoted 5 lines · Ian Jackson
Gianni Tedesco (3P) writes ("Re: [Xen-devel] [PATCH] libxl: make
is_hvmlog on errors, and make callers check its error returns"):Quoted 4 lines
I hate this coding style, it's really easy to miss whats going on when scanning code and two things are done on one line. I think either lines ought to be added or change declaration to:
Quoted 7 lines
Quoted 4 lines
int
get_vm_mode(libxl_ctx, int domid, int *hvm);so at least compiler can tell you if you're using it the way it was intended.
I'd be happy with the latter proposal. I don't like anything that increases the code size. Shall I resend my patch ?
Fair enough, yes please re-send.
PATCH 1/1 libxl: properly check errors from libxl__get_ishvm (v3)
Make is_hvm log on errors; make callers check its error returns; and rename the function according to the libxl__ name scheme for internal functions.
--- tools/libxl/libxl.c | 31 +++++++++++++++++++++---------- tools/libxl/libxl_dom.c | 19 +++++++++++++------ tools/libxl/libxl_internal.h | 2 +- tools/libxl/libxl_pci.c | 16 +++++++++------- 4 files changed, 44 insertions(+), 24 deletions(-)
tools/libxl/libxl.c +21 -10
@@ -427,9 +427,10 @@ out:int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid) {libxl_gc gc =LIBXL_INIT_GC (ctx); -int rc =0 ; +int hvm, rc =0 ; -if (is_hvm (ctx, domid)) { + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc) goto out; +if (hvm) {XL_LOG (ctx,XL_LOG_DEBUG ,"Called domain_resume on " "non-cooperative hvm domain %u" , domid); rc =ERROR_NI ;
@@ -645,11 +646,13 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)int libxl_domain_suspend (libxl_ctx *ctx,libxl_domain_suspend_info *info,uint32_t domid,int fd) { -int hvm =is_hvm (ctx, domid); +int hvm;int live = info !=NULL && info->flags &XL_SUSPEND_LIVE ;int debug = info !=NULL && info->flags &XL_SUSPEND_DEBUG ;int rc =0 ; + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc)return rc; +core_suspend (ctx, domid, fd, hvm, live, debug);if (hvm) rc =save_device_model (ctx, domid, fd);
@@ -685,9 +688,10 @@ int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)libxl_gc gc =LIBXL_INIT_GC (ctx);char *path;char *state; -int ret, rc =0 ; +int hvm, ret, rc =0 ; -if (is_hvm (ctx, domid)) { + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc)return rc; +if (hvm) { path =libxl_sprintf (&gc,"/local/domain/0/device-model/%d/state" , domid); state =libxl_xs_read (&gc,XBT_NULL , path);if (state !=NULL && !strcmp (state,"paused" )) {
@@ -717,6 +721,7 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)libxl_gc gc =LIBXL_INIT_GC (ctx);char *shutdown_path;char *dom_path; +int hvm, rc =0 ;if (req >ARRAY_SIZE (req_table)) {libxl_free_all (&gc);
@@ -732,7 +737,8 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req) shutdown_path =libxl_sprintf (&gc,"%s/control/shutdown" , dom_path);xs_write (ctx->xsh,XBT_NULL , shutdown_path, req_table[req],strlen (req_table[req])); -if (is_hvm (ctx,domid)) { + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc) goto out; +if (hvm) {unsigned long acpi_s_state =0 ;unsigned long pvdriver =0 ;int ret;
@@ -754,8 +760,9 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req) } } } + out:libxl_free_all (&gc); -return 0 ; +return rc; }int libxl_get_wait_fd (libxl_ctx *ctx,int *fd)
@@ -920,9 +927,10 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)libxl_gc gc =LIBXL_INIT_GC (ctx);char *dom_path;char *vm_path; -int rc, dm_present; +int rc, hvm, dm_present; -if (is_hvm (ctx, domid)) { + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc) goto out; +if (hvm) { dm_present =1 ; }else {char *pid;
@@ -1004,11 +1012,14 @@ out:int libxl_primary_console_exec (libxl_ctx *ctx,uint32_t domid_vm) { +int rc, hvm;uint32_t stubdomid =libxl_get_stubdom_id (ctx, domid_vm); +if (stubdomid)return libxl_console_exec (ctx, stubdomid,1 ,LIBXL_CONSTYPE_PV );else { -if (is_hvm (ctx, domid_vm)) + rc =libxl__get_ishvm (ctx, domid_vm, &hvm);if (rc)return rc; +if (hvm)return libxl_console_exec (ctx, domid_vm,0 ,LIBXL_CONSTYPE_SERIAL );else return libxl_console_exec (ctx, domid_vm,0 ,LIBXL_CONSTYPE_PV );
tools/libxl/libxl_dom.c +13 -6
@@ -31,17 +31,24 @@#include "libxl.h" #include "libxl_internal.h" -int is_hvm (libxl_ctx *ctx,uint32_t domid) +int libxl__get_ishvm (libxl_ctx *ctx,uint32_t domid,int *is_hvm_r) {xc_domaininfo_t info;int ret; ret =xc_domain_getinfolist (ctx->xch, domid,1 , &info); -if (ret !=1 ) -return -1 ; -if (info.domain != domid) -return -1 ; -return !!(info.flags & XEN_DOMINF_hvm_guest); +if (ret !=1 ) { +XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"is_hvm: xc_domain_getinfolist failed" ); +return ERROR_FAIL ; + } +if (info.domain != domid) { +XL_LOG (ctx,XL_LOG_ERROR ,"is_hvm: xc_domain_getinfolist gave info " +"for %" PRIu32"d when we asked for %" PRIu32"d" , + info.domain, domid); +return ERROR_FAIL ; + } + *is_hvm_r = !!(info.flags & XEN_DOMINF_hvm_guest); +return 0 ; }int get_shutdown_reason (libxl_ctx *ctx,uint32_t domid)
tools/libxl/libxl_internal.h +1 -1
@@ -140,7 +140,7 @@ _hidden char *libxl_xs_read(libxl_gc *gc, xs_transaction_t t, char *path); _hidden char **libxl_xs_directory(libxl_gc *gc, xs_transaction_t t, char *path, unsigned int *nb);/* from xl_dom */ -_hidden int is_hvm(libxl_ctx *ctx, uint32_t domid); +_hidden int libxl__get_ishvm(libxl_ctx *ctx, uint32_t domid, int *is_hvm_r); _hidden int get_shutdown_reason(libxl_ctx *ctx, uint32_t domid);#define dominfo_get_shutdown_reason (info) (((info)->flags >> XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask)
tools/libxl/libxl_pci.c +9 -7
@@ -266,7 +266,7 @@ static int libxl_device_pci_add_xenstore(libxl_gc *gc, uint32_t domid, libxl_devlibxl_ctx *ctx =libxl_gc_owner (gc);flexarray_t *back;char *num_devs, *be_path; -int num =0 ; +int rc, hvm, num =0 ;unsigned int boffset =0 ;xs_transaction_t t;
@@ -275,7 +275,8 @@ static int libxl_device_pci_add_xenstore(libxl_gc *gc, uint32_t domid, libxl_devif (!num_devs)return libxl_create_pci_backend (gc, domid, pcidev,1 ); -if (!is_hvm (ctx, domid)) { + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc)return rc; +if (!hvm) {if (libxl_wait_for_backend (ctx, be_path,"4" ) <0 )return ERROR_FAIL ; }
@@ -319,7 +320,7 @@ static int libxl_device_pci_remove_xenstore(libxl_gc *gc, uint32_t domid, libxl_ {libxl_ctx *ctx =libxl_gc_owner (gc);char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath; -int num, i, j; +int num, i, j, rc, hvm;xs_transaction_t t;unsigned int domain =0 , bus =0 , dev =0 , func =0 ;
@@ -330,7 +331,8 @@ static int libxl_device_pci_remove_xenstore(libxl_gc *gc, uint32_t domid, libxl_return ERROR_INVAL ; num =atoi (num_devs); -if (!is_hvm (ctx, domid)) { + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc)return rc; +if (!hvm) {if (libxl_wait_for_backend (ctx, be_path,"4" ) <0 ) {XL_LOG (ctx,XL_LOG_DEBUG ,"pci backend at %s is not ready" , be_path);return ERROR_FAIL ;
@@ -358,7 +360,7 @@ retry_transaction:if (errno ==EAGAIN ) goto retry_transaction; -if (!is_hvm (ctx, domid)) { +if (!hvm) {if (libxl_wait_for_backend (ctx, be_path,"4" ) <0 ) {XL_LOG (ctx,XL_LOG_DEBUG ,"pci backend at %s is not ready" , be_path);return ERROR_FAIL ;
@@ -610,7 +612,7 @@ static int do_pci_add(libxl_gc *gc, uint32_t domid, libxl_device_pci *pcidev)char *state, *vdevfn;int rc, hvm; - hvm =is_hvm (ctx, domid); + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc)return rc;if (hvm) {if (libxl_wait_for_device_model (ctx, domid,"running" ,NULL ,NULL ) <0 ) {return ERROR_FAIL ;
@@ -834,7 +836,7 @@ static int do_pci_remove(libxl_gc *gc, uint32_t domid, libxl_device_pci *pcidev)libxl_device_pci_remove_xenstore (gc, domid, pcidev); - hvm =is_hvm (ctx, domid); + rc =libxl__get_ishvm (ctx, domid, &hvm);if (rc)return rc;if (hvm) {if (libxl_wait_for_device_model (ctx, domid,"running" ,NULL ,NULL ) <0 ) {return ERROR_FAIL ;
Signature · 1 line
1.5.6.5