debuggers.hg

changeset 22082:ed717f785082

libxl: implement destroy for libxl_file_reference builtin type

As well as freeing data any file mappings need to be torn down so
implement an explicit destroy function.

Also the map and unmap function are internal to libxl so make that so.

[PATCH 09 of 16 of
libxl: autogenerate type definitions and destructor functions]

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Ian Campbell <ian.campbell@citrix.com>
date Thu Aug 19 15:25:15 2010 +0100 (2010-08-19)
parents 1ba7cbf9683b
children f7d818754afb
files tools/libxl/libxl.c tools/libxl/libxl.h tools/libxl/libxl.idl tools/libxl/libxl_bootloader.c tools/libxl/libxl_internal.c tools/libxl/libxl_internal.h
line diff
     1.1 --- a/tools/libxl/libxl.c	Thu Aug 19 15:24:41 2010 +0100
     1.2 +++ b/tools/libxl/libxl.c	Thu Aug 19 15:25:15 2010 +0100
     1.3 @@ -23,7 +23,6 @@
     1.4  #include <sys/stat.h>
     1.5  #include <fcntl.h>
     1.6  #include <sys/select.h>
     1.7 -#include <sys/mman.h>
     1.8  #include <sys/wait.h>
     1.9  #include <sys/time.h>
    1.10  #include <signal.h>
    1.11 @@ -340,9 +339,9 @@ int libxl_domain_build(libxl_ctx *ctx, l
    1.12      }
    1.13      ret = build_post(ctx, domid, info, state, vments, localents);
    1.14  out:
    1.15 -    libxl_file_reference_unmap(ctx, &info->kernel);
    1.16 +    libxl__file_reference_unmap(&info->kernel);
    1.17      if (!info->hvm)
    1.18 -	    libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
    1.19 +	    libxl__file_reference_unmap(&info->u.pv.ramdisk);
    1.20  
    1.21      libxl_free_all(&gc);
    1.22      return ret;
    1.23 @@ -405,9 +404,9 @@ int libxl_domain_restore(libxl_ctx *ctx,
    1.24      }
    1.25  
    1.26  out:
    1.27 -    libxl_file_reference_unmap(ctx, &info->kernel);
    1.28 +    libxl__file_reference_unmap(&info->kernel);
    1.29      if (!info->hvm)
    1.30 -	    libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
    1.31 +	    libxl__file_reference_unmap(&info->u.pv.ramdisk);
    1.32  
    1.33      esave = errno;
    1.34  
    1.35 @@ -3355,47 +3354,8 @@ int libxl_tmem_freeable(libxl_ctx *ctx)
    1.36      return rc;
    1.37  }
    1.38  
    1.39 -int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f)
    1.40 +void libxl_file_reference_destroy(libxl_file_reference *f)
    1.41  {
    1.42 -	struct stat st_buf;
    1.43 -	int ret, fd;
    1.44 -	void *data;
    1.45 -
    1.46 -	if (f->mapped)
    1.47 -		return 0;
    1.48 -
    1.49 -	fd = open(f->path, O_RDONLY);
    1.50 -	if (f < 0)
    1.51 -		return ERROR_FAIL;
    1.52 -
    1.53 -	ret = fstat(fd, &st_buf);
    1.54 -	if (ret < 0)
    1.55 -		goto out;
    1.56 -
    1.57 -	ret = -1;
    1.58 -	data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    1.59 -	if (data == NULL)
    1.60 -		goto out;
    1.61 -
    1.62 -	f->mapped = 1;
    1.63 -	f->data = data;
    1.64 -	f->size = st_buf.st_size;
    1.65 -
    1.66 -	ret = 0;
    1.67 -out:
    1.68 -	close(fd);
    1.69 -
    1.70 -	return ret == 0 ? 0 : ERROR_FAIL;
    1.71 +    libxl__file_reference_unmap(f);
    1.72 +    free(f->path);
    1.73  }
    1.74 -
    1.75 -int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f)
    1.76 -{
    1.77 -	int ret;
    1.78 -
    1.79 -	if (!f->mapped)
    1.80 -		return 0;
    1.81 -
    1.82 -	ret = munmap(f->data, f->size);
    1.83 -
    1.84 -	return ret == 0 ? 0 : ERROR_FAIL;
    1.85 -}
     2.1 --- a/tools/libxl/libxl.h	Thu Aug 19 15:24:41 2010 +0100
     2.2 +++ b/tools/libxl/libxl.h	Thu Aug 19 15:25:15 2010 +0100
     2.3 @@ -142,10 +142,8 @@ typedef uint8_t libxl_uuid[16];
     2.4  typedef uint8_t libxl_mac[6];
     2.5  
     2.6  typedef char **libxl_string_list;
     2.7 -void libxl_string_list_destroy(libxl_string_list sl);
     2.8  
     2.9  typedef char **libxl_key_value_list;
    2.10 -void libxl_key_value_list_destroy(libxl_key_value_list kvl);
    2.11  
    2.12  typedef uint64_t *libxl_cpumap;
    2.13  
    2.14 @@ -235,8 +233,10 @@ int libxl_domain_shutdown(libxl_ctx *ctx
    2.15  int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force);
    2.16  int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid);
    2.17  
    2.18 -int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f);
    2.19 -int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f);
    2.20 +/* destructors for builtin data types */
    2.21 +void libxl_string_list_destroy(libxl_string_list sl);
    2.22 +void libxl_key_value_list_destroy(libxl_key_value_list kvl);
    2.23 +void libxl_file_reference_destroy(libxl_file_reference *f);
    2.24  
    2.25  /*
    2.26   * Run the configured bootloader for a PV domain and update
     3.1 --- a/tools/libxl/libxl.idl	Thu Aug 19 15:24:41 2010 +0100
     3.2 +++ b/tools/libxl/libxl.idl	Thu Aug 19 15:25:15 2010 +0100
     3.3 @@ -85,7 +85,7 @@ libxl_file_reference = Struct("file_refe
     3.4  mapped is true then the actual file may already be unlinked."""),
     3.5      ("mapped", integer),
     3.6      ("data", void),
     3.7 -    ("size", size_t)])
     3.8 +    ("size", size_t)], autogenerate_destructor=False)
     3.9  
    3.10  libxl_domain_build_info = Struct("domain_build_info",[
    3.11      ("max_vcpus",       integer),
     4.1 --- a/tools/libxl/libxl_bootloader.c	Thu Aug 19 15:24:41 2010 +0100
     4.2 +++ b/tools/libxl/libxl_bootloader.c	Thu Aug 19 15:25:15 2010 +0100
     4.3 @@ -279,12 +279,12 @@ static void parse_bootloader_result(libx
     4.4          if (strncmp("kernel ", o, strlen("kernel ")) == 0) {
     4.5              free(info->kernel.path);
     4.6              info->kernel.path = strdup(o + strlen("kernel "));
     4.7 -            libxl_file_reference_map(ctx, &info->kernel);
     4.8 +            libxl__file_reference_map(&info->kernel);
     4.9              unlink(info->kernel.path);
    4.10          } else if (strncmp("ramdisk ", o, strlen("ramdisk ")) == 0) {
    4.11              free(info->u.pv.ramdisk.path);
    4.12              info->u.pv.ramdisk.path = strdup(o + strlen("ramdisk "));
    4.13 -            libxl_file_reference_map(ctx, &info->u.pv.ramdisk);
    4.14 +            libxl__file_reference_map(&info->u.pv.ramdisk);
    4.15              unlink(info->u.pv.ramdisk.path);
    4.16          } else if (strncmp("args ", o, strlen("args ")) == 0) {
    4.17              free(info->u.pv.cmdline);
     5.1 --- a/tools/libxl/libxl_internal.c	Thu Aug 19 15:24:41 2010 +0100
     5.2 +++ b/tools/libxl/libxl_internal.c	Thu Aug 19 15:25:15 2010 +0100
     5.3 @@ -19,6 +19,12 @@
     5.4  #include <stdarg.h>
     5.5  #include <string.h>
     5.6  
     5.7 +#include <sys/types.h>
     5.8 +#include <sys/stat.h>
     5.9 +#include <fcntl.h>
    5.10 +#include <sys/mman.h>
    5.11 +#include <unistd.h>
    5.12 +
    5.13  #include "libxl.h"
    5.14  #include "libxl_internal.h"
    5.15  #include "libxl_utils.h"
    5.16 @@ -185,3 +191,48 @@ char *libxl_abs_path(libxl_gc *gc, char 
    5.17      return libxl_sprintf(gc, "%s/%s", path, s);
    5.18  }
    5.19  
    5.20 +
    5.21 +int libxl__file_reference_map(libxl_file_reference *f)
    5.22 +{
    5.23 +	struct stat st_buf;
    5.24 +	int ret, fd;
    5.25 +	void *data;
    5.26 +
    5.27 +	if (f->mapped)
    5.28 +		return 0;
    5.29 +
    5.30 +	fd = open(f->path, O_RDONLY);
    5.31 +	if (f < 0)
    5.32 +		return ERROR_FAIL;
    5.33 +
    5.34 +	ret = fstat(fd, &st_buf);
    5.35 +	if (ret < 0)
    5.36 +		goto out;
    5.37 +
    5.38 +	ret = -1;
    5.39 +	data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    5.40 +	if (data == NULL)
    5.41 +		goto out;
    5.42 +
    5.43 +	f->mapped = 1;
    5.44 +	f->data = data;
    5.45 +	f->size = st_buf.st_size;
    5.46 +
    5.47 +	ret = 0;
    5.48 +out:
    5.49 +	close(fd);
    5.50 +
    5.51 +	return ret == 0 ? 0 : ERROR_FAIL;
    5.52 +}
    5.53 +
    5.54 +int libxl__file_reference_unmap(libxl_file_reference *f)
    5.55 +{
    5.56 +	int ret;
    5.57 +
    5.58 +	if (!f->mapped)
    5.59 +		return 0;
    5.60 +
    5.61 +	ret = munmap(f->data, f->size);
    5.62 +
    5.63 +	return ret == 0 ? 0 : ERROR_FAIL;
    5.64 +}
     6.1 --- a/tools/libxl/libxl_internal.h	Thu Aug 19 15:24:41 2010 +0100
     6.2 +++ b/tools/libxl/libxl_internal.h	Thu Aug 19 15:25:15 2010 +0100
     6.3 @@ -274,4 +274,7 @@ struct libxl__xen_console_reader {
     6.4  
     6.5  _hidden int libxl_error_set(libxl_ctx *ctx, int code);
     6.6  
     6.7 +_hidden int libxl__file_reference_map(libxl_file_reference *f);
     6.8 +_hidden int libxl__file_reference_unmap(libxl_file_reference *f);
     6.9 +
    6.10  #endif