From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xen.org
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 07/15] libxl: provide libxl__xs_*_checked and libxl__xs_transaction_*
Date: Wed, 30 May 2012 17:16:38 +0100 [thread overview]
Message-ID: <1338394606-22693-8-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1338394606-22693-1-git-send-email-ian.jackson@eu.citrix.com>
These useful utility functions make dealing with xenstore a little
less painful.
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
tools/libxl/libxl_internal.h | 38 +++++++++++++++++++++
tools/libxl/libxl_xshelp.c | 76 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 0 deletions(-)
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index e4c3f34..ee7f66a 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -498,6 +498,44 @@ _hidden bool libxl__xs_mkdir(libxl__gc *gc, xs_transaction_t t,
_hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid);
+
+/*----- "checked" xenstore access functions -----*/
+/* Each of these functions will check that it succeeded; if it
+ * fails it logs and returns ERROR_FAIL.
+ */
+
+/* On success, *result_out came from the gc.
+ * On error, *result_out is undefined.
+ * ENOENT counts as success but sets *result_out=0
+ */
+int libxl__xs_read_checked(libxl__gc *gc, xs_transaction_t t,
+ const char *path, const char **result_out);
+
+/* Does not include a trailing null.
+ * May usefully be combined with GCSPRINTF if the format string
+ * behaviour of libxl__xs_write is desirable. */
+int libxl__xs_write_checked(libxl__gc *gc, xs_transaction_t t,
+ const char *path, const char *string);
+
+/* ENOENT is not an error (even if the parent directories don't exist) */
+int libxl__xs_rm_checked(libxl__gc *gc, xs_transaction_t t, const char *path);
+
+/* Transaction functions, best used together.
+ * The caller should initialise *t to 0 (XBT_NULL) before calling start.
+ * Each function leaves *t!=0 iff the transaction needs cleaning up.
+ *
+ * libxl__xs_transaction_commit returns:
+ * <0 failure - a libxl error code
+ * +1 commit conflict; transaction has been destroyed and caller
+ * must go round again (call _start again and retry)
+ * 0 commited successfully
+ */
+int libxl__xs_transaction_start(libxl__gc *gc, xs_transaction_t *t);
+int libxl__xs_transaction_commit(libxl__gc *gc, xs_transaction_t *t);
+void libxl__xs_transaction_abort(libxl__gc *gc, xs_transaction_t *t);
+
+
+
/*
* This is a recursive delete, from top to bottom. What this function does
* is remove empty folders that contained the deleted entry.
diff --git a/tools/libxl/libxl_xshelp.c b/tools/libxl/libxl_xshelp.c
index c5b5364..5f359bc 100644
--- a/tools/libxl/libxl_xshelp.c
+++ b/tools/libxl/libxl_xshelp.c
@@ -135,6 +135,82 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
return s;
}
+int libxl__xs_read_checked(libxl__gc *gc, xs_transaction_t t,
+ const char *path, const char **result_out)
+{
+ char *result = libxl__xs_read(gc, t, path);
+ if (!result) {
+ if (errno != ENOENT) {
+ LOGE(ERROR, "xenstore read failed: `%s'", path);
+ return ERROR_FAIL;
+ }
+ }
+ *result_out = result;
+ return 0;
+}
+
+int libxl__xs_write_checked(libxl__gc *gc, xs_transaction_t t,
+ const char *path, const char *string)
+{
+ size_t length = strlen(string);
+ if (!xs_write(CTX->xsh, t, path, string, length)) {
+ LOGE(ERROR, "xenstore write failed: `%s' = `%s'", path, string);
+ return ERROR_FAIL;
+ }
+ return 0;
+}
+
+int libxl__xs_rm_checked(libxl__gc *gc, xs_transaction_t t, const char *path)
+{
+ if (!xs_rm(CTX->xsh, t, path)) {
+ if (errno == ENOENT)
+ return 0;
+
+ LOGE(ERROR, "xenstore rm failed: `%s'", path);
+ return ERROR_FAIL;
+ }
+ return 0;
+}
+
+int libxl__xs_transaction_start(libxl__gc *gc, xs_transaction_t *t)
+{
+ assert(!*t);
+ *t = xs_transaction_start(CTX->xsh);
+ if (!*t) {
+ LOGE(ERROR, "could not create xenstore transaction");
+ return ERROR_FAIL;
+ }
+ return 0;
+}
+
+int libxl__xs_transaction_commit(libxl__gc *gc, xs_transaction_t *t)
+{
+ assert(*t);
+
+ if (!xs_transaction_end(CTX->xsh, *t, 0)) {
+ if (errno == EAGAIN)
+ return +1;
+
+ *t = 0;
+ LOGE(ERROR, "could not commit xenstore transacton");
+ return ERROR_FAIL;
+ }
+
+ *t = 0;
+ return 0;
+}
+
+void libxl__xs_transaction_abort(libxl__gc *gc, xs_transaction_t *t)
+{
+ if (!*t)
+ return;
+
+ if (!xs_transaction_end(CTX->xsh, *t, 1))
+ LOGE(ERROR, "could not abort xenstore transacton");
+
+ *t = 0;
+}
+
int libxl__xs_path_cleanup(libxl__gc *gc, xs_transaction_t t, char *user_path)
{
unsigned int nb = 0;
--
1.7.2.5
next prev parent reply other threads:[~2012-05-30 16:16 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-30 16:16 [PATCH v2 00/15] libxl: domain save/restore: run in a separate process Ian Jackson
2012-05-30 16:16 ` [PATCH 01/15] libxc: xc_domain_restore, make toolstack_restore const-correct Ian Jackson
2012-05-30 17:26 ` Ian Campbell
2012-05-31 9:55 ` Ian Jackson
2012-05-31 11:16 ` Ian Campbell
2012-05-31 14:00 ` Ian Jackson
2012-05-30 16:16 ` [PATCH 02/15] libxl: domain save: rename variables etc Ian Jackson
2012-05-30 17:32 ` Ian Campbell
2012-05-31 10:02 ` Ian Jackson
2012-05-30 16:16 ` [PATCH 03/15] libxl: domain restore: reshuffle, preparing for ao Ian Jackson
2012-05-30 16:16 ` [PATCH 04/15] libxl: domain save: API changes for asynchrony Ian Jackson
2012-05-30 16:16 ` [PATCH 05/15] libxl: domain save/restore: run in a separate process Ian Jackson
2012-05-30 16:16 ` [PATCH 06/15] libxl: rename libxl_dom:save_helper to physmap_path Ian Jackson
2012-05-30 17:44 ` Ian Campbell
2012-05-30 16:16 ` Ian Jackson [this message]
2012-05-30 17:48 ` [PATCH 07/15] libxl: provide libxl__xs_*_checked and libxl__xs_transaction_* Ian Campbell
2012-05-31 10:22 ` Ian Jackson
2012-05-31 11:17 ` Ian Campbell
2012-05-30 16:16 ` [PATCH 08/15] libxl: wait for qemu to acknowledge logdirty command Ian Jackson
2012-05-30 16:16 ` [PATCH 09/15] libxl: datacopier: provide "prefix data" facilit Ian Jackson
2012-05-31 7:52 ` Ian Campbell
2012-05-31 10:32 ` Ian Jackson
2012-05-31 11:20 ` Ian Campbell
2012-05-31 14:07 ` Ian Jackson
2012-05-30 16:16 ` [PATCH 10/15] libxl: prepare for asynchronous writing of qemu save file Ian Jackson
2012-05-30 16:16 ` [PATCH 11/15] libxl: Make libxl__domain_save_device_model asynchronous Ian Jackson
2012-05-30 16:16 ` [PATCH 12/15] xl: Handle return value from libxl_domain_suspend correctly Ian Jackson
2012-05-31 7:53 ` Ian Campbell
2012-05-30 16:16 ` [PATCH 13/15] libxl: do not leak dms->saved_state Ian Jackson
2012-05-31 7:54 ` Ian Campbell
2012-05-30 16:16 ` [PATCH 14/15] libxl: do not leak spawned middle children Ian Jackson
2012-05-30 16:16 ` [PATCH 15/15] libxl: do not leak an event struct on ignored ao progress Ian Jackson
2012-05-31 7:55 ` Ian Campbell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1338394606-22693-8-git-send-email-ian.jackson@eu.citrix.com \
--to=ian.jackson@eu.citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).