We provide a mechanism whereby a user of the libxl library is able to
store some information alongside the domain. The information stored
is a block of bytes. Its lifetime is that of the domain - ie the
userdata is garbage collected alongside the domain if the domain is
destroyed. (This is why the feature needs to be in libxl and cannot
be implemented in the user itself or in libxlutil.)
If a libxl caller does not need to use this feature it can ignore it.
The data is tagged with the (self-declared) name of the libxl user, so
that different users cannot accidentally trip over each others'
userdata. The data is not interpreted at all by libxl.
To assist developers and people debugging, there is a registry of the
known userdata userids, and the corresponding data format as declared
by that libxl user, in libxl.h next to these declarations:
int libxl_userdata_store(struct libxl_ctx *ctx, uint32_t domid,
const char *userdata_userid,
const uint8_t *data, int datalen);
int libxl_userdata_retrieve(struct libxl_ctx *ctx, uint32_t domid,
const char *userdata_userid,
uint8_t **data_r, int *datalen_r);
The next patch will introduce the data for the userid "xl".
Signed-off-by: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
---
tools/libxl/libxl.c | 2 +
tools/libxl/libxl.h | 31 +++++++++++
tools/libxl/libxl_dom.c | 115 ++++++++++++++++++++++++++++++++++++++++++
tools/libxl/libxl_internal.h | 1 +
4 files changed, 149 insertions(+), 0 deletions(-)
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 711f0a0..4d069cd 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -708,6 +708,8 @@ int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t
domid, int force)
if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
+ libxl__userdata_destroyall(ctx, domid);
+
rc = xc_domain_destroy(ctx->xch, domid);
if (rc < 0) {
XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for
%d", domid);
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 2d76087..d57dd10 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -359,6 +359,37 @@ int libxl_device_pci_init(libxl_device_pci *pcidev,
unsigned int domain,
unsigned int bus, unsigned int dev,
unsigned int func, unsigned int vdevfn);
+/*
+ * Functions for allowing users of libxl to store private data
+ * relating to a domain. The data is an opaque sequence of bytes and
+ * is not interpreted or used by libxl.
+ *
+ * Data is indexed by the userdata userid, which is a short printable
+ * ASCII string. The following list is a registry of userdata userids
+ * (the registry may be updated by posting a patch to xen-devel):
+ *
+ * userid Data contents
+ * "xl" domain config file in xl format, Unix line endings
+ *
+ * libxl does not enforce the registration of userdata userids or the
+ * semantics of the data. For specifications of the data formats
+ * see the code or documentation for the libxl caller in question.
+ */
+int libxl_userdata_store(struct libxl_ctx *ctx, uint32_t domid,
+ const char *userdata_userid,
+ const uint8_t *data, int datalen);
+ /* If datalen==0, data is not used and the user data for
+ * that domain and userdata_userid is deleted. */
+int libxl_userdata_retrieve(struct libxl_ctx *ctx, uint32_t domid,
+ const char *userdata_userid,
+ uint8_t **data_r, int *datalen_r);
+ /* On successful return, *data_r is from malloc.
+ * If there is no data for that domain and userdata_userid,
+ * *data_r and *datalen_r will be set to 0.
+ * data_r and datalen_r may be 0.
+ * On error return, *data_r and *datalen_r are undefined.
+ */
+
typedef enum {
POWER_BUTTON,
SLEEP_BUTTON
diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index 7196aa8..237febb 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -16,6 +16,8 @@
#include "libxl_osdeps.h"
#include <stdio.h>
+#include <assert.h>
+#include <glob.h>
#include <inttypes.h>
#include <string.h>
#include <sys/time.h> /* for struct timeval */
@@ -349,3 +351,116 @@ int save_device_model(struct libxl_ctx *ctx, uint32_t
domid, int fd)
unlink(filename);
return 0;
}
+
+static const char *userdata_path(struct libxl_ctx *ctx, uint32_t domid,
+ const char *userdata_userid,
+ const char *wh) {
+ char *path;
+ path = libxl_sprintf(ctx, "/var/lib/xen/"
+ "userdata-%s.%"PRIu32".%s",
+ wh, domid, userdata_userid);
+ if (!path)
+ XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to allocate for"
+ " userdata path");
+ return path;
+}
+
+static int userdata_delete(struct libxl_ctx *ctx, const char *path) {
+ int r;
+ r = unlink(path);
+ if (r) {
+ XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "remove failed for %s", path);
+ return errno;
+ }
+ return 0;
+}
+
+void libxl__userdata_destroyall(struct libxl_ctx *ctx, uint32_t domid) {
+ const char *pattern;
+ glob_t gl;
+ int r, i;
+
+ pattern = userdata_path(ctx, domid, "*", "?");
+ if (!pattern) return;
+
+ gl.gl_pathc = 0;
+ gl.gl_pathv = 0;
+ gl.gl_offs = 0;
+ r = glob(pattern, GLOB_ERR|GLOB_NOSORT|GLOB_MARK, 0, &gl);
+ if (r) XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "glob failed for %s", pattern);
+
+ for (i=0; i<gl.gl_pathc; i++) {
+ userdata_delete(ctx, gl.gl_pathv[i]);
+ }
+ globfree(&gl);
+}
+
+int libxl_userdata_store(struct libxl_ctx *ctx, uint32_t domid,
+ const char *userdata_userid,
+ const uint8_t *data, int datalen) {
+ const char *filename;
+ const char *newfilename;
+ int e;
+ int fd = -1;
+ FILE *f = 0;
+ size_t rs;
+
+ filename = userdata_path(ctx, domid, userdata_userid, "d");
+ if (!filename) return ENOMEM;
+
+ if (!datalen)
+ return userdata_delete(ctx, filename);
+
+ newfilename = userdata_path(ctx, domid, userdata_userid, "n");
+ if (!newfilename) return ENOMEM;
+
+ fd= open(newfilename, O_RDWR|O_CREAT|O_TRUNC, 0600);
+ if (fd<0) goto xe;
+
+ f= fdopen(fd, "wb");
+ if (!f) goto xe;
+ fd = -1;
+
+ rs = fwrite(data, 1, datalen, f);
+ if (rs != datalen) { assert(ferror(f)); goto xe; }
+
+ if (fclose(f)) goto xe;
+ f = 0;
+
+ if (rename(newfilename,filename)) goto xe;
+
+ return 0;
+
+ xe:
+ e = errno;
+ if (f) fclose(f);
+ if (fd>=0) close(fd);
+
+ XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot write %s for %s",
+ newfilename, filename);
+ return e;
+}
+
+int libxl_userdata_retrieve(struct libxl_ctx *ctx, uint32_t domid,
+ const char *userdata_userid,
+ uint8_t **data_r, int *datalen_r) {
+ const char *filename;
+ int e;
+ int datalen = 0;
+ void *data = 0;
+
+ filename = userdata_path(ctx, domid, userdata_userid, "d");
+ if (!filename) return ENOMEM;
+
+ e = libxl_read_file_contents(ctx, filename, data_r ? &data : 0, &datalen);
+
+ if (!e && !datalen) {
+ XL_LOG(ctx, XL_LOG_ERROR, "userdata file %s is empty", filename);
+ if (data_r) assert(!*data_r);
+ return EPROTO;
+ }
+
+ if (data_r) *data_r = data;
+ if (datalen_r) *datalen_r = datalen;
+ return 0;
+}
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index d1e355c..b35cf0f 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -130,6 +130,7 @@ int restore_common(struct libxl_ctx *ctx, uint32_t domid,
libxl_domain_build_info *info, libxl_domain_build_state
*state, int fd);
int core_suspend(struct libxl_ctx *ctx, uint32_t domid, int fd, int hvm, int
live, int debug);
int save_device_model(struct libxl_ctx *ctx, uint32_t domid, int fd);
+void libxl__userdata_destroyall(struct libxl_ctx *ctx, uint32_t domid);
/* from xl_device */
char *device_disk_backend_type_of_phystype(libxl_disk_phystype phystype);
--
1.5.6.5
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
|