libxl: Make an internal function explicitly check existence of expected paths
PATCH 1/1 libxl: Make an internal function explicitly check existence of expected paths
# HG changeset patch
# User George Dunlap <[email protected]>
# Date 1354294821 0
# Node ID bc2a7645dc2be4a01f2b5ee30d7453cc3d7339aa
# Parent bd041b7426fe10a730994edd98708ff98ae1cb74
libxl: Make an internal function explicitly check existence of expected paths
libxl__device_disk_from_xs_be() was failing without error for some missing xenstore nodes in a backend, while assuming (without checking) that other nodes were valid, causing a crash when another internal error wrote these nodes in the wrong place.
Make this function consistent by:
* Checking the existence of all nodes before using
* Choosing a default only when the node is not written in device_disk_add()
* Failing with log msg if any node written by device_disk_add() is not present
* Returning an error on failure
Also make the callers of the function pay attention to the error and behave appropriately.
v2:
* Remove "Internal error", as the failure will most likely look internal
* Use LOG(ERROR...) macros for incrased prettiness
tools/libxl/libxl.c +38 -16
@@ -2168,9 +2168,9 @@ void libxl__device_disk_add(libxl__egc *device_disk_add (egc, domid, disk, aodev,NULL ,NULL ); } -static void libxl__device_disk_from_xs_be (libxl__gc *gc, -const char *be_path, -libxl_device_disk *disk) +static int libxl__device_disk_from_xs_be (libxl__gc *gc, +const char *be_path, +libxl_device_disk *disk) {libxl_ctx *ctx =libxl__gc_owner (gc);unsigned int len;
@@ -2178,6 +2178,7 @@ static void libxl__device_disk_from_xs_blibxl_device_disk_init (disk); +/* "params" may not be present; but everything else must be. */ tmp =xs_read (ctx->xsh,XBT_NULL ,libxl__sprintf (gc,"%s/params" , be_path), &len);if (tmp &&strchr (tmp,':' )) {
@@ -2186,21 +2187,36 @@ static void libxl__device_disk_from_xs_b }else { disk->pdev_path = tmp; } -libxl_string_to_backend (ctx, -libxl__xs_read (gc,XBT_NULL , -libxl__sprintf (gc,"%s/type" , be_path)), - &(disk->backend)); + + + tmp =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/type" , be_path)); +if (!tmp) { +LOG (ERROR ,"Missing xenstore node %s/type" , be_path); +return ERROR_FAIL ; + } +libxl_string_to_backend (ctx, tmp, &(disk->backend)); + disk->vdev =xs_read (ctx->xsh,XBT_NULL ,libxl__sprintf (gc,"%s/dev" , be_path), &len); +if (!disk->vdev) { +LOG (ERROR ,"Missing xenstore node %s/dev" , be_path); +return ERROR_FAIL ; + } + tmp =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/removable" , be_path)); - -if (tmp) - disk->removable =atoi (tmp); -else - disk->removable =0 ; +if (!tmp) { +LOG (ERROR ,"Missing xenstore node %s/removable" , be_path); +return ERROR_FAIL ; + } + disk->removable =atoi (tmp); tmp =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/mode" , be_path)); +if (!tmp) { +LOG (ERROR ,"Missing xenstore node %s/mode" , be_path); +return ERROR_FAIL ; + }if (!strcmp (tmp,"w" )) disk->readwrite =1 ;else
@@ -2208,9 +2224,15 @@ static void libxl__device_disk_from_xs_b tmp =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/device-type" , be_path)); +if (!tmp) { +LOG (ERROR ,"Missing xenstore node %s/device-type" , be_path); +return ERROR_FAIL ; + } disk->is_cdrom = !strcmp (tmp,"cdrom" ); disk->format =LIBXL_DISK_FORMAT_UNKNOWN ; + +return 0 ; } int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid,
@@ -2236,9 +2258,7 @@ int libxl_vdev_to_device_disk(libxl_ctxif (!path) goto out; -libxl__device_disk_from_xs_be (gc, path, disk); - - rc =0 ; + rc =libxl__device_disk_from_xs_be (gc, path, disk); out:GC_FREE ;return rc;
@@ -2255,6 +2275,7 @@ static int libxl__append_disk_list_of_tychar **dir =NULL ;unsigned int n =0 ;libxl_device_disk *pdisk =NULL , *pdisk_end =NULL ; +int rc=0 ; be_path =libxl__sprintf (gc,"%s/backend/%s/%d" ,libxl__xs_get_dompath (gc,0 ), type, domid);
@@ -2271,7 +2292,8 @@ static int libxl__append_disk_list_of_tyfor (; pdisk < pdisk_end; pdisk++, dir++) {const char *p; p =libxl__sprintf (gc,"%s/%s" , be_path, *dir); -libxl__device_disk_from_xs_be (gc, p, pdisk); +if ((rc=libxl__device_disk_from_xs_be (gc, p, pdisk))) +return rc; pdisk->backend_domid =0 ; } }
Quoted 12 lines · George Dunlap
# HG changeset patch
# User George Dunlap <[email protected]>
# Date 1354294821 0
# Node IDbc2a7645dc2be4a01f2b5ee30d7453cc3d7339aa
# Parentbd041b7426fe10a730994edd98708ff98ae1cb74
libxl: Make an internal function explicitly check existence of expected paths
libxl__device_disk_from_xs_be()was failing without error for some missing xenstore nodes in a backend, while assuming (without checking) that other nodes were valid, causing a crash when another internal error wrote these nodes in the wrong place.
Quoted 8 lines
Make this function consistent by:
* Checking the existence of all nodes before using
* Choosing a default only when the node is not written indevice_disk_add()
* Failing with log msg if any node written bydevice_disk_add()is not present
* Returning an error on failureAlso make the callers of the function pay attention to the error and behave appropriately.
If libxl__device_disk_from_xs_be returns an error then someone needs to cleanup the partial allocations in the disk (pdev_path) probably by calling libxl_device_disk_dispose.
It's probably easiest to do this in libxl__device_disk_from_xs_be on error rather than modifying all the callers?
Also libxl__append_disk_list_of_type updates *ndisks early, so if you abort half way through initialising the elements of the disks array using libxl__device_disk_from_xs_be then the caller will try and free some stuff which hasn't been initialised. I think the code needs to remember ndisks-in-array separately from ndisks-which-I-have-initialised, with the latter becoming the returned *ndisks.
Quoted 3 lines
v2:
* Remove "Internal error", as the failure will most likely look internal
* Use LOG(ERROR...) macros for incrased prettiness
More crass?
Quoted 6 lines
tools/libxl/libxl.c +0
-2@@ -2186,21 +2187,36 @@ static void libxl__device_disk_from_xs_b }else { disk->pdev_path = tmp; } - libxl_string_to_backend(ctx, - libxl__xs_read(gc, XBT_NULL,
tools/libxl/libxl.c static void libxl__device_disk_from_xs_b · line 2197
Quoted 10 lines
tools/libxl/libxl.c +8
-2- libxl__sprintf(gc, "%s/type", be_path)), - &(disk->backend)); + + + tmp =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/type" , be_path)); +if (!tmp) { +LOG (ERROR ,"Missing xenstore node %s/type" , be_path); +return ERROR_FAIL ; + }
I've just remembered about libxl__xs_read_checked which effectively implements the error reporting for you.
Oh, but it accepts ENOENT, so not quite what you need -- nevermind!
Ian.
Quoted 19 lines · Ian Campbell
Quoted 18 lines · George Dunlap
# HG changeset patch
# User George Dunlap <[email protected]>
# Date 1354294821 0
# Node IDbc2a7645dc2be4a01f2b5ee30d7453cc3d7339aa
# Parentbd041b7426fe10a730994edd98708ff98ae1cb74
libxl: Make an internal function explicitly check existence of expected paths
libxl__device_disk_from_xs_be()was failing without error for some missing xenstore nodes in a backend, while assuming (without checking) that other nodes were valid, causing a crash when another internal error wrote these nodes in the wrong place.Make this function consistent by:
* Checking the existence of all nodes before using
* Choosing a default only when the node is not written indevice_disk_add()
* Failing with log msg if any node written bydevice_disk_add()is not present
* Returning an error on failure
Quoted 8 lines
Quoted 2 lines
Also make the callers of the function pay attention to the error and behave appropriately.
If
libxl__device_disk_from_xs_bereturns an error then someone needs to cleanup the partial allocations in the disk (pdev_path) probably by callinglibxl_device_disk_dispose.It's probably easiest to do this in
libxl__device_disk_from_xs_beon error rather than modifying all the callers?
Well, there are only two callers, only one of which (it looks like) needs a clean-up. It seems like better design to make each caller do its own clean-up. Let me take a look at that.
-George
Quoted 38 lines
Also
libxl__append_disk_list_of_typeupdates *ndisks early, so if you abort half way through initialising the elements of the disks array usinglibxl__device_disk_from_xs_bethen the caller will try and free some stuff which hasn't been initialised. I think the code needs to remember ndisks-in-array separately from ndisks-which-I-have-initialised, with the latter becoming the returned *ndisks.Quoted 3 lines
v2:
* Remove "Internal error", as the failure will most likely look internal
* Use LOG(ERROR...) macros for incrased prettinessMore crass?
Quoted 6 lines
tools/libxl/libxl.c +0
-2@@ -2186,21 +2187,36 @@ static void libxl__device_disk_from_xs_b }else { disk->pdev_path = tmp; } - libxl_string_to_backend(ctx, - libxl__xs_read(gc, XBT_NULL,tools/libxl/libxl.c static void libxl__device_disk_from_xs_b · line 2197
Quoted 10 lines
tools/libxl/libxl.c +8
-2- libxl__sprintf(gc, "%s/type", be_path)), - &(disk->backend)); + + + tmp =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%s/type" , be_path)); +if (!tmp) { +LOG (ERROR ,"Missing xenstore node %s/type" , be_path); +return ERROR_FAIL ; + }I've just remembered about
libxl__xs_read_checkedwhich effectively implements the error reporting for you.Oh, but it accepts ENOENT, so not quite what you need -- nevermind!
Ian.