xl: Return an error if an empty file is passed to cd-insert
PATCH 1/1 xl: Return an error if an empty file is passed to cd-insert
Two changes:
* Stat the file before calling libxl_cdrom_insert()
* Return an error if anything fails (including libxl_cdrom_insert)
This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
--- tools/libxl/xl_cmdimpl.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
tools/libxl/xl_cmdimpl.c +21 -5
@@ -2505,25 +2505,42 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;XLU_Config *config =0 ; +struct stat b; +int rc =0 ;if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; +return 1 ; }parse_disk_config (&config, buf, &disk); -libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path); +return 1 ; + } + +if (libxl_cdrom_insert (ctx, domid, &disk,NULL )) + rc=1 ;libxl_device_disk_dispose (&disk);free (buf); + +return rc; }int main_cd_eject (int argc,char **argv)
@@ -2539,8 +2556,7 @@ int main_cd_eject(int argc, char **argv) domid =find_domain (argv[optind]); virtdev = argv[optind +1 ]; -cd_insert (domid, virtdev,NULL ); -return 0 ; +return cd_insert (domid, virtdev,NULL ); }int main_cd_insert (int argc,char **argv)
Signature · 1 line
1.7.9.5
Quoted 3 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)
Hmm -- not sure how "empty file" got into that description -- it should say "non-existent file"...
-George
Quoted 81 lines
This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Signed-off-by George Dunlap <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>--- tools/libxl/xl_cmdimpl.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tools/libxl/xl_cmdimpl.c +21
-6@@ -2505,25 +2505,42 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;XLU_Config *config =0 ; +struct stat b; +int rc =0 ;if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; +return 1 ; }parse_disk_config (&config, buf, &disk); -libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path); +return 1 ; + } + +if (libxl_cdrom_insert (ctx, domid, &disk,NULL )) + rc=1 ;libxl_device_disk_dispose (&disk);free (buf); + +return rc; }int main_cd_eject (int argc,char **argv)@@ -2539,8 +2556,7 @@ int main_cd_eject(int argc, char **argv) domid =find_domain (argv[optind]); virtdev = argv[optind +1 ]; -cd_insert (domid, virtdev,NULL ); -return 0 ; +return cd_insert (domid, virtdev,NULL ); }int main_cd_insert (int argc,char **argv) --1.7.9.5
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 29 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Signed-off-by George Dunlap <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>--- tools/libxl/xl_cmdimpl.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tools/libxl/xl_cmdimpl.c +1
-1@@ -2505,25 +2505,42 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2520
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +3
-1XLU_Config *config =0 ; +struct stat b; +int rc =0 ;if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; +return 1 ;
ERROR_NOMEM
Quoted 4 lines
tools/libxl/xl_cmdimpl.c +0
-0}parse_disk_config (&config, buf, &disk);
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2533
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +9
-1-libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path);
You are leaking buf here, I know xl is just about to exit, but it might be best to to define a label below that contains:
out:
free(buf);
return rc;and go to it.
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2534
Quoted 1 line
tools/libxl/xl_cmdimpl.c +1
-0+return 1 ;
ERROR_INVAL
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2538
Quoted 4 lines
tools/libxl/xl_cmdimpl.c +4
-0+ } + +if (libxl_cdrom_insert (ctx, domid, &disk,NULL )) + rc=1 ;
You could use the return value of libxl_cdrom_insert instead of 1.
Quoted 19 lines
tools/libxl/xl_cmdimpl.c +3
-2libxl_device_disk_dispose (&disk);free (buf); + +return rc; }int main_cd_eject (int argc,char **argv)@@ -2539,8 +2556,7 @@ int main_cd_eject(int argc, char **argv) domid =find_domain (argv[optind]); virtdev = argv[optind +1 ]; -cd_insert (domid, virtdev,NULL ); -return 0 ; +return cd_insert (domid, virtdev,NULL ); }int main_cd_insert (int argc,char **argv)
Quoted 35 lines · Roger Pau Monne
Quoted 34 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Signed-off-by George Dunlap <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>--- tools/libxl/xl_cmdimpl.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tools/libxl/xl_cmdimpl.c +3
-1@@ -2505,25 +2505,42 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;XLU_Config *config =0 ; +struct stat b; +int rc =0 ;
Quoted 7 lines
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2520
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +1
-1if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; +return 1 ;
ERROR_NOMEM
xl seems pretty inconsistent in its use of libxl error codes, sometimes it uses +ERROR_FOO, other times -ERROR_FOO and other times it uses its own open coded numbers.
So I wouldn't say this is wrong as such but so long as the final xl exit code is 0 on success and non-zero otherwise I think it is up to George.
Quoted 16 lines
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +0
-0}parse_disk_config (&config, buf, &disk);tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2533
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +9
-1-libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path);
Quoted 1 line
You are leaking buf here, I know xl is just about to exit,
I prefer to have xl clear up properly whenever possible. Not because I care particularly about xl (which isn't generally long lived) but because it makes it possible to use valgrind+xl to check for leaks in libxl, and I care about that because other toolstacks which use libxl can be long lived and therefore care about leaks. Having xl not contain false positive leaks helps here.
Ian.
Quoted 44 lines · Ian Campbell
Quoted 35 lines · Roger Pau Monne
Quoted 34 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Signed-off-by George Dunlap <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>--- tools/libxl/xl_cmdimpl.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tools/libxl/xl_cmdimpl.c +3
-1@@ -2505,25 +2505,42 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;XLU_Config *config =0 ; +struct stat b; +int rc =0 ;Quoted 7 lines
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2520
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +1
-1if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; +return 1 ;
ERROR_NOMEM
Quoted 6 lines
xl seems pretty inconsistent in its use of libxl error codes, sometimes it uses +ERROR_FOO, other times -ERROR_FOO and other times it uses its own open coded numbers.
So I wouldn't say this is wrong as such but so long as the final xl exit code is 0 on success and non-zero otherwise I think it is up to George.
Yes I agree, xl is full of return 1, -1 and things like that, but anyway I think it was worth mentioning.
Quoted 27 lines
Quoted 16 lines
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +0
-0}parse_disk_config (&config, buf, &disk);tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2533
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +9
-1-libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path);Quoted 1 line
You are leaking buf here, I know xl is just about to exit,
I prefer to have xl clear up properly whenever possible. Not because I care particularly about xl (which isn't generally long lived) but because it makes it possible to use valgrind+xl to check for leaks in libxl, and I care about that because other toolstacks which use libxl can be long lived and therefore care about leaks. Having xl not contain false positive leaks helps here.
Ian.
Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Signed-off-by George Dunlap <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>--- tools/libxl/xl_cmdimpl.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)tools/libxl/xl_cmdimpl.c +3
-1@@ -2505,25 +2505,42 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;XLU_Config *config =0 ; +struct stat b; +int rc =0 ;
Quoted 6 lines
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2520
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +1
-1if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; +return 1 ;
ERROR_NOMEM
If this were a library function I would definitely return -ERROR_NOMEM; but since this is just a helper function whose value is going to be returned to the command-line, I think just returning 1 is the best thing to do.
Quoted 15 lines
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +0
-0}parse_disk_config (&config, buf, &disk);tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2533
Quoted 10 lines
tools/libxl/xl_cmdimpl.c +9
-1-libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path);
Quoted 2 lines
You are leaking buf here, I know xl is just about to exit, but it might be best to to define a label below that contains:
Oops! Sorry about that...
Quoted 8 lines
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2534
Quoted 1 line
tools/libxl/xl_cmdimpl.c +1
-0+return 1 ;
ERROR_INVALtools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2538
Quoted 4 lines
tools/libxl/xl_cmdimpl.c +4
-0+ } + +if (libxl_cdrom_insert (ctx, domid, &disk,NULL )) + rc=1 ;You could use the return value of
libxl_cdrom_insertinstead of 1.
I sort of assumed that libxl_cdrom_insert() was a negative value, and that I wanted xl to return 1.
I'll send a respin w/ the leak fixed.
-George