solidy and cleanup libxl
cover solidy and cleanup libxl
this patchset fix lots of segfault (mostly in unexpected scenario), making libxl more fault resiliant. it also fix a couple of bugs that were introduced with previous patchset.
Vincent Hanquez (8):
don't ignore ctx_init errors if there is one.
don't free ifname, can lead to double free
returns errors if xc and/or xs has not been initialized properly. don't use the cloning logic in dm_xenstore_record_pid. remove all cloning logic.
simplify the logic of domain_list to just return 1024 domains maximum don't try to delete path when they doesn't exists. initialize domid to -1 in domain_create
tools/libxl/libxl.c | 83 ++++++++++++++++++------------------------ tools/libxl/libxl_device.c | 20 ++++------ tools/libxl/libxl_internal.c | 25 ++----------- tools/libxl/libxl_internal.h | 19 +--------- tools/libxl/libxl_xshelp.c | 19 ++++++++++ tools/libxl/xl.c | 64 +++++++++++++++++++++++++-------- 6 files changed, 116 insertions(+), 114 deletions(-)
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 1/8 don't ignore ctx_init errors if there is one.
ctx_init can return error, and ignoring the errors, would lead to segfault later. fix all call sites to check return values.
--- tools/libxl/xl.c | 61 +++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 49 insertions(+), 12 deletions(-)
tools/libxl/xl.c +49 -12
@@ -762,7 +762,11 @@ static void create_domain(int debug, int daemonize, const char *config_file, con start: domid =0 ; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + } +libxl_ctx_set_log (&ctx, log_callback,NULL );libxl_domain_make (&ctx, &info1, &domid);
@@ -974,7 +978,10 @@ void set_memory_target(char *p, char *mem)struct libxl_ctx ctx;uint32_t domid; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, p, &domid) <0 ) {
@@ -1016,7 +1023,10 @@ void console(char *p, int cons_num)struct libxl_ctx ctx;uint32_t domid; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, p, &domid) <0 ) {
@@ -1033,7 +1043,10 @@ void cd_insert(char *dom, char *virtdev, char *phys)libxl_device_disk disk;char *p; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, dom, &domid) <0 ) {
@@ -1166,7 +1179,10 @@ void pcilist(char *dom)libxl_device_pci *pcidevs;int num, i; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, dom, &domid) <0 ) {
@@ -1216,7 +1232,10 @@ void pcidetach(char *dom, char *bdf)libxl_device_pci pcidev;unsigned int domain, bus, dev, func; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, dom, &domid) <0 ) {
@@ -1262,7 +1281,10 @@ void pciattach(char *dom, char *bdf, char *vs)libxl_device_pci pcidev;unsigned int domain, bus, dev, func; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, dom, &domid) <0 ) {
@@ -1310,7 +1332,10 @@ void pause_domain(char *p)struct libxl_ctx ctx;uint32_t domid; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, p, &domid) <0 ) {
@@ -1325,7 +1350,10 @@ void unpause_domain(char *p)struct libxl_ctx ctx;uint32_t domid; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, p, &domid) <0 ) {
@@ -1340,7 +1368,10 @@ void destroy_domain(char *p)struct libxl_ctx ctx;uint32_t domid; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, p, &domid) <0 ) {
@@ -1356,7 +1387,10 @@ void list_domains(void)struct libxl_dominfo *info;int nb_domain, i; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +return ; + }libxl_ctx_set_log (&ctx, log_callback,NULL ); info =libxl_domain_list (&ctx, &nb_domain);
@@ -1383,7 +1417,10 @@ int save_domain(char *p, char *filename, int checkpoint)uint32_t domid;int fd; -libxl_ctx_init (&ctx,LIBXL_VERSION ); +if (libxl_ctx_init (&ctx,LIBXL_VERSION )) { +fprintf (stderr,"cannot init xl context\n" ); +exit (2 ); + }libxl_ctx_set_log (&ctx, log_callback,NULL );if (domain_qualifier_to_domid (&ctx, p, &domid) <0 ) {
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 2/8 don't free ifname, can lead to double free
In reply to Vincent Hanquezifname is mostly handled directly in xl now, do not attempts to free this.
--- tools/libxl/xl.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
tools/libxl/xl.c +0 -3
@@ -888,9 +888,6 @@ start: }close (logfile); -for (i =0 ; i < num_vifs; i++) { -free (vifs[i].ifname); - }free (disks);free (vifs);free (vfbs);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 3/8 returns errors if xc and/or xs has not been initialized properly.
In reply to Vincent Hanquezfixed segfault when xenstore or xc are not available.
--- tools/libxl/libxl.c | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-)
tools/libxl/libxl.c +10 -0
@@ -45,7 +45,17 @@ int libxl_ctx_init(struct libxl_ctx *ctx, int version)return ERROR_NOMEM ; ctx->xch =xc_interface_open (); +if (ctx->xch ==-1 ) { +free (ctx->alloc_ptrs); +return ERROR_FAIL ; + } + ctx->xsh =xs_daemon_open (); +if (!ctx->xsh) { +xc_interface_close (ctx->xch); +free (ctx->alloc_ptrs); +return ERROR_FAIL ; + }return 0 ; }
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 4/8 don't use the cloning logic in dm_xenstore_record_pid.
In reply to Vincent Hanquezuse call to lowlevel functions to do the same things.
--- tools/libxl/libxl.c | 32 ++++++++++---------------------- tools/libxl/libxl_internal.h | 2 ++ tools/libxl/libxl_xshelp.c | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 22 deletions(-)
tools/libxl/libxl.c +10 -22
@@ -728,33 +728,21 @@ static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,void dm_xenstore_record_pid (struct libxl_ctx *ctx,void *for_spawn,pid_t innerchild) {struct libxl_device_model_starting *starting = for_spawn; -struct libxl_ctx clone;char *kvs[3 ]; -int rc, cloned; - -if (libxl_clone_context_xs (ctx, &clone)) { -XL_LOG (ctx,XL_LOG_ERROR ,"Out of memory when cloning context" ); -/* Throw a prayer fallback */ - clone = *ctx; - clone.xsh =xs_daemon_open (); - cloned =0 ; - }else { - cloned =1 ; - } +int rc; +struct xs_handle *xsh; + + xsh =xs_daemon_open ();/* we mustn't use the parent's handle in the child */ kvs[0 ] ="image/device-model-pid" ; - kvs[1 ] =libxl_sprintf (&clone,"%d" , innerchild); +asprintf (&kvs[1 ],"%d" , innerchild); kvs[2 ] =NULL ; - rc =libxl_xs_writev (&clone,XBT_NULL , starting->dom_path, kvs); -if (rc)XL_LOG_ERRNO (&clone,XL_LOG_ERROR , -"Couldn't record device model pid %ld at %s/%s" , - (unsigned long )innerchild, starting->dom_path, kvs); -if (cloned) { -libxl_discard_cloned_context_xs (&clone); - }else { -xs_daemon_close (clone.xsh); - } + + rc =xs_writev (xsh,XBT_NULL , starting->dom_path, kvs); +if (rc) +return ; +xs_daemon_close (xsh); } static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
tools/libxl/libxl_internal.h +2 -0
@@ -93,6 +93,8 @@ typedef struct { (u)[0], (u)[1], (u)[2], (u)[3], (u)[4], (u)[5], (u)[6], (u)[7], \ (u)[8], (u)[9], (u)[10], (u)[11], (u)[12], (u)[13], (u)[14], (u)[15]) +int xs_writev (struct xs_handle *xsh,xs_transaction_t t,char *dir,char *kvs[]); +/* memory allocation tracking/helpers */ int libxl_clone_context (struct libxl_ctx *from,struct libxl_ctx *to); static inline int libxl_clone_context_xs(
tools/libxl/libxl_xshelp.c +19 -0
@@ -23,6 +23,25 @@#include "libxl.h" #include "libxl_internal.h" +int xs_writev (struct xs_handle *xsh,xs_transaction_t t,char *dir,char *kvs[]) +{ +char *path; +int i; + +if (!kvs) +return 0 ; + +for (i =0 ; kvs[i] !=NULL ; i +=2 ) { +asprintf (&path,"%s/%s" , dir, kvs[i]); +if (path) { +int length =strlen (kvs[i +1 ]); +xs_write (xsh, t, path, kvs[i +1 ], length); +free (path); + } + } +return 0 ; +} +char **libxl_xs_kvs_of_flexarray (struct libxl_ctx *ctx,flexarray_t *array,int length) {char **kvs;
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 5/8 remove all cloning logic.
In reply to Vincent Hanquezreplace by simpler xl ctx init/free calls. also don't segfault if there's no callback for log.
--- tools/libxl/libxl_device.c | 20 ++++++++------------ tools/libxl/libxl_internal.c | 25 +++---------------------- tools/libxl/libxl_internal.h | 17 ----------------- 3 files changed, 11 insertions(+), 51 deletions(-)
tools/libxl/libxl_device.c +8 -12
@@ -240,9 +240,8 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)flexarray_t *toremove;struct libxl_ctx clone; -if (libxl_clone_context_xs (ctx, &clone)) { -XL_LOG (ctx,XL_LOG_ERROR ,"Out of memory when cloning context" ); -return ERROR_NOMEM ; +if (libxl_ctx_init (&clone,LIBXL_VERSION )) { +return -1 ; } toremove =flexarray_make (16 ,1 );
@@ -250,7 +249,7 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t domid, int force) l1 =libxl_xs_directory (&clone,XBT_NULL , path, &num1);if (!l1) {XL_LOG (&clone,XL_LOG_ERROR ,"%s is empty" , path); -libxl_discard_cloned_context_xs (&clone); +libxl_ctx_free (&clone);return -1 ; }for (i =0 ; i < num1; i++) {
@@ -294,7 +293,7 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)xs_rm (clone.xsh,XBT_NULL , path); }flexarray_free (toremove); -libxl_discard_cloned_context_xs (&clone); +libxl_ctx_free (&clone);return 0 ; }
@@ -304,9 +303,8 @@ int libxl_device_del(struct libxl_ctx *ctx, libxl_device *dev, int wait)int rc;struct libxl_ctx clone; -if (libxl_clone_context_xs (ctx, &clone)) { -XL_LOG (ctx,XL_LOG_ERROR ,"Out of memory when cloning context" ); -return ERROR_NOMEM ; +if (libxl_ctx_init (&clone,LIBXL_VERSION )) { +return -1 ; }/* Create strings */
@@ -323,7 +321,7 @@ int libxl_device_del(struct libxl_ctx *ctx, libxl_device *dev, int wait) rc =libxl_device_destroy (&clone, backend_path, !wait);if (rc ==-1 ) { -libxl_discard_cloned_context_xs (&clone); +libxl_ctx_free (&clone);return ERROR_FAIL ; }
@@ -335,9 +333,7 @@ int libxl_device_del(struct libxl_ctx *ctx, libxl_device *dev, int wait) }xs_rm (clone.xsh,XBT_NULL , hotplug_path); -libxl_free (&clone, hotplug_path); -libxl_free (&clone, backend_path); -libxl_discard_cloned_context_xs (&clone); +libxl_ctx_free (&clone);return 0 ; }
tools/libxl/libxl_internal.c +3 -22
@@ -28,28 +28,6 @@ int libxl_error_set(struct libxl_ctx *ctx, int code)return 0 ; } -int libxl_clone_context (struct libxl_ctx *from,struct libxl_ctx *to) -{ -/* We could just copy the structs, but since -* maxsize is not a pointer we need to take care -* of our own GC. */ - *to = *from; - to->alloc_ptrs =NULL ; - to->alloc_maxsize =256 ; - to->alloc_ptrs =calloc (to->alloc_maxsize,sizeof (void *)); -if (!to->alloc_ptrs) -return ERROR_NOMEM ; -return 0 ; -} - -void libxl_discard_cloned_context (struct libxl_ctx *ctx) -{ -/* We only need to worry about GC-related fields */ - (void )libxl_ctx_free (ctx); -if (ctx->alloc_ptrs) -free (ctx->alloc_ptrs); -} -int libxl_ptr_add (struct libxl_ctx *ctx,void *ptr) {int i;
@@ -180,6 +158,9 @@ void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval,char *s;int rc; +if (!ctx->log_callback) +return ; + rc =vasprintf (&s, fmt, ap);if (rc<0 ) { s = enomem; goto x; }
tools/libxl/libxl_internal.h +0 -17
@@ -96,23 +96,6 @@ typedef struct {int xs_writev (struct xs_handle *xsh,xs_transaction_t t,char *dir,char *kvs[]);/* memory allocation tracking/helpers */ -int libxl_clone_context (struct libxl_ctx *from,struct libxl_ctx *to); -static inline int libxl_clone_context_xs ( -struct libxl_ctx *from,struct libxl_ctx *to) -{ -int rc; - rc =libxl_clone_context (from, to); -if (rc)return rc; - to->xsh =xs_daemon_open (); -return 0 ; -} -void libxl_discard_cloned_context (struct libxl_ctx *ctx); -static inline void libxl_discard_cloned_context_xs ( -struct libxl_ctx *ctx) -{ -libxl_discard_cloned_context (ctx); -xs_daemon_close (ctx->xsh); -}int libxl_ptr_add (struct libxl_ctx *ctx,void *ptr);int libxl_free (struct libxl_ctx *ctx,void *ptr);int libxl_free_all (struct libxl_ctx *ctx);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 6/8 simplify the logic of domain_list to just return 1024 domains maximum
In reply to Vincent Hanquez--- tools/libxl/libxl.c | 24 +++++------------------- 1 files changed, 5 insertions(+), 19 deletions(-)
tools/libxl/libxl.c +5 -19
@@ -273,32 +273,20 @@ struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain) {struct libxl_dominfo *ptr;int index, i, ret, first_domain; -xc_domaininfo_t info[16 ]; -int size =16 ; +xc_domaininfo_t info[1024 ]; +int size =1024 ; first_domain =1 ; index =0 ; ptr =calloc (size,sizeof (struct libxl_dominfo ));if (!ptr)return NULL ; -redo: - ret =xc_domain_getinfolist (ctx->xch, first_domain,16 , info); -for (i =0 ; i < ret; i++) { -if (index == size) { -struct libxl_dominfo *ptr2; - ptr2 =calloc (size *2 ,sizeof (struct libxl_dominfo )); -if (!ptr2) { -free (ptr); -return NULL ; - } -memcpy (ptr2, ptr,sizeof (struct libxl_dominfo ) * size); -free (ptr); - ptr = ptr2; - size *=2 ; - } + ret =xc_domain_getinfolist (ctx->xch, first_domain,1024 , info); +for (i =0 ; i < ret; i++) {memcpy (&(ptr[index].uuid), info[i].handle,sizeof (xen_domain_handle_t)); ptr[index].domid = info[i].domain; +printf ("domain %d [ret %d]\n" , ptr[index].domid, ret);if (info[i].flags & XEN_DOMINF_dying) ptr[index].dying =1 ;
@@ -310,8 +298,6 @@ redo: first_domain = info[i].domain +1 ; index++; } -if (ret ==16 ) - goto redo; *nb_domain = index;return ptr; }
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 7/8 don't try to delete path when they doesn't exists.
In reply to Vincent Hanquezfix segfault in destroy when creation hasn't been done properly.
--- tools/libxl/libxl.c | 15 +++++++++------ 1 files changed, 9 insertions(+), 6 deletions(-)
tools/libxl/libxl.c +9 -6
@@ -564,16 +564,19 @@ int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , dom_path); vm_path =libxl_xs_read (ctx,XBT_NULL ,libxl_sprintf (ctx,"/local/domain/%d/vm" , domid)); -if (!xs_rm (ctx->xsh,XBT_NULL , vm_path)) -XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , vm_path); +if (vm_path) +if (!xs_rm (ctx->xsh,XBT_NULL , vm_path)) +XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , vm_path); vss_path =libxl_xs_read (ctx,XBT_NULL ,libxl_sprintf (ctx,"/local/domain/%d/vss" , domid)); -if (!xs_rm (ctx->xsh,XBT_NULL , vss_path)) -XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , vss_path); +if (vss_path) +if (!xs_rm (ctx->xsh,XBT_NULL , vss_path)) +XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , vss_path); xapi_path =libxl_sprintf (ctx,"/xapi/%u" , domid); -if (!xs_rm (ctx->xsh,XBT_NULL , xapi_path)) -XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , xapi_path); +if (xapi_path) +if (!xs_rm (ctx->xsh,XBT_NULL , xapi_path)) +XL_LOG_ERRNO (ctx,XL_LOG_ERROR ,"xs_rm failed for %s" , xapi_path); rc =xc_domain_destroy (ctx->xch, domid);if (rc <0 ) {
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 8/8 initialize domid to -1 in domain_create
In reply to Vincent Hanquezprevent call site that doesn't check return values to try to do operation on domain 0. instead they use domid -1, which is unlikely to exist.
--- tools/libxl/libxl.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
tools/libxl/libxl.c +1 -1
@@ -99,7 +99,7 @@ int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info, flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest :0 ; flags |= info->hap ? XEN_DOMCTL_CDF_hap :0 ; - *domid =0 ; + *domid =-1 ;/* Ultimately, handle is an array of 16 uint8_t, same as uuid */ memcpy (handle, info->uuid,sizeof (xen_domain_handle_t));
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel