aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorIan Campbell <ian.campbell@citrix.com>2010-08-19 15:25:15 +0100
committerIan Campbell <ian.campbell@citrix.com>2010-08-19 15:25:15 +0100
commit10cf11b43638a854e372e39275edb7863614a7b9 (patch)
tree9c96bae68871baf1b0df633c6382b9c98b6f04be /tools
parent60ce81bc56b4b8a997a7d8791390d3da1a389a71 (diff)
downloadxen-10cf11b43638a854e372e39275edb7863614a7b9.tar.gz
xen-10cf11b43638a854e372e39275edb7863614a7b9.tar.bz2
xen-10cf11b43638a854e372e39275edb7863614a7b9.zip
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>
Diffstat (limited to 'tools')
-rw-r--r--tools/libxl/libxl.c54
-rw-r--r--tools/libxl/libxl.h8
-rw-r--r--tools/libxl/libxl.idl2
-rw-r--r--tools/libxl/libxl_bootloader.c4
-rw-r--r--tools/libxl/libxl_internal.c51
-rw-r--r--tools/libxl/libxl_internal.h3
6 files changed, 68 insertions, 54 deletions
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 531590ce7a..7a02a24db7 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -23,7 +23,6 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
-#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
@@ -340,9 +339,9 @@ int libxl_domain_build(libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t d
}
ret = build_post(ctx, domid, info, state, vments, localents);
out:
- libxl_file_reference_unmap(ctx, &info->kernel);
+ libxl__file_reference_unmap(&info->kernel);
if (!info->hvm)
- libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
+ libxl__file_reference_unmap(&info->u.pv.ramdisk);
libxl_free_all(&gc);
return ret;
@@ -405,9 +404,9 @@ int libxl_domain_restore(libxl_ctx *ctx, libxl_domain_build_info *info,
}
out:
- libxl_file_reference_unmap(ctx, &info->kernel);
+ libxl__file_reference_unmap(&info->kernel);
if (!info->hvm)
- libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
+ libxl__file_reference_unmap(&info->u.pv.ramdisk);
esave = errno;
@@ -3355,47 +3354,8 @@ int libxl_tmem_freeable(libxl_ctx *ctx)
return rc;
}
-int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f)
+void libxl_file_reference_destroy(libxl_file_reference *f)
{
- struct stat st_buf;
- int ret, fd;
- void *data;
-
- if (f->mapped)
- return 0;
-
- fd = open(f->path, O_RDONLY);
- if (f < 0)
- return ERROR_FAIL;
-
- ret = fstat(fd, &st_buf);
- if (ret < 0)
- goto out;
-
- ret = -1;
- data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- if (data == NULL)
- goto out;
-
- f->mapped = 1;
- f->data = data;
- f->size = st_buf.st_size;
-
- ret = 0;
-out:
- close(fd);
-
- return ret == 0 ? 0 : ERROR_FAIL;
-}
-
-int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f)
-{
- int ret;
-
- if (!f->mapped)
- return 0;
-
- ret = munmap(f->data, f->size);
-
- return ret == 0 ? 0 : ERROR_FAIL;
+ libxl__file_reference_unmap(f);
+ free(f->path);
}
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 566da1455a..ca1d71b8c6 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -142,10 +142,8 @@ typedef uint8_t libxl_uuid[16];
typedef uint8_t libxl_mac[6];
typedef char **libxl_string_list;
-void libxl_string_list_destroy(libxl_string_list sl);
typedef char **libxl_key_value_list;
-void libxl_key_value_list_destroy(libxl_key_value_list kvl);
typedef uint64_t *libxl_cpumap;
@@ -235,8 +233,10 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req);
int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force);
int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid);
-int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f);
-int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f);
+/* destructors for builtin data types */
+void libxl_string_list_destroy(libxl_string_list sl);
+void libxl_key_value_list_destroy(libxl_key_value_list kvl);
+void libxl_file_reference_destroy(libxl_file_reference *f);
/*
* Run the configured bootloader for a PV domain and update
diff --git a/tools/libxl/libxl.idl b/tools/libxl/libxl.idl
index fcbeddeb3a..f59950d3e8 100644
--- a/tools/libxl/libxl.idl
+++ b/tools/libxl/libxl.idl
@@ -85,7 +85,7 @@ libxl_file_reference = Struct("file_reference",[
mapped is true then the actual file may already be unlinked."""),
("mapped", integer),
("data", void),
- ("size", size_t)])
+ ("size", size_t)], autogenerate_destructor=False)
libxl_domain_build_info = Struct("domain_build_info",[
("max_vcpus", integer),
diff --git a/tools/libxl/libxl_bootloader.c b/tools/libxl/libxl_bootloader.c
index e7e88ce3d1..08d6a22274 100644
--- a/tools/libxl/libxl_bootloader.c
+++ b/tools/libxl/libxl_bootloader.c
@@ -279,12 +279,12 @@ static void parse_bootloader_result(libxl_ctx *ctx,
if (strncmp("kernel ", o, strlen("kernel ")) == 0) {
free(info->kernel.path);
info->kernel.path = strdup(o + strlen("kernel "));
- libxl_file_reference_map(ctx, &info->kernel);
+ libxl__file_reference_map(&info->kernel);
unlink(info->kernel.path);
} else if (strncmp("ramdisk ", o, strlen("ramdisk ")) == 0) {
free(info->u.pv.ramdisk.path);
info->u.pv.ramdisk.path = strdup(o + strlen("ramdisk "));
- libxl_file_reference_map(ctx, &info->u.pv.ramdisk);
+ libxl__file_reference_map(&info->u.pv.ramdisk);
unlink(info->u.pv.ramdisk.path);
} else if (strncmp("args ", o, strlen("args ")) == 0) {
free(info->u.pv.cmdline);
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 5eb27a6f4d..05ef3e3130 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -19,6 +19,12 @@
#include <stdarg.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
#include "libxl.h"
#include "libxl_internal.h"
#include "libxl_utils.h"
@@ -185,3 +191,48 @@ char *libxl_abs_path(libxl_gc *gc, char *s, const char *path)
return libxl_sprintf(gc, "%s/%s", path, s);
}
+
+int libxl__file_reference_map(libxl_file_reference *f)
+{
+ struct stat st_buf;
+ int ret, fd;
+ void *data;
+
+ if (f->mapped)
+ return 0;
+
+ fd = open(f->path, O_RDONLY);
+ if (f < 0)
+ return ERROR_FAIL;
+
+ ret = fstat(fd, &st_buf);
+ if (ret < 0)
+ goto out;
+
+ ret = -1;
+ data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (data == NULL)
+ goto out;
+
+ f->mapped = 1;
+ f->data = data;
+ f->size = st_buf.st_size;
+
+ ret = 0;
+out:
+ close(fd);
+
+ return ret == 0 ? 0 : ERROR_FAIL;
+}
+
+int libxl__file_reference_unmap(libxl_file_reference *f)
+{
+ int ret;
+
+ if (!f->mapped)
+ return 0;
+
+ ret = munmap(f->data, f->size);
+
+ return ret == 0 ? 0 : ERROR_FAIL;
+}
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 2f81acce92..face02136c 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -274,4 +274,7 @@ struct libxl__xen_console_reader {
_hidden int libxl_error_set(libxl_ctx *ctx, int code);
+_hidden int libxl__file_reference_map(libxl_file_reference *f);
+_hidden int libxl__file_reference_unmap(libxl_file_reference *f);
+
#endif