Mailing List Archive

[PATCH 04 of 11 v3] libxl: introduce libxl__wait_for_device_state
# HG changeset patch
# User Roger Pau Monne <roger.pau@entel.upc.edu>
# Date 1322751691 -3600
# Node ID 3b5b14fcd208e7c5cb1840ea76eb11a63958a030
# Parent 0abd9540788822b58585c8f2d29571e63fb68865
libxl: introduce libxl__wait_for_device_state

This is a generic function, that waits for xs watches to reach a
certain state and then executes the passed helper function. Removed
wait_for_dev_destroy and used this new function instead. This
function will also be used by future patches that need to wait for
the initialization of devices before executing hotplug scripts.

Signed-off-by: Roger Pau Monne <roger.pau@entel.upc.edu>

diff -r 0abd95407888 -r 3b5b14fcd208 tools/libxl/libxl_device.c
--- a/tools/libxl/libxl_device.c Thu Dec 01 16:01:17 2011 +0100
+++ b/tools/libxl/libxl_device.c Thu Dec 01 16:01:31 2011 +0100
@@ -370,7 +370,9 @@ int libxl__device_disk_dev_number(const
* Returns 0 if a device is removed, ERROR_* if an error
* or timeout occurred.
*/
-static int wait_for_dev_destroy(libxl__gc *gc, struct timeval *tv)
+int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv,
+ XenbusState state,
+ libxl__device_state_handler handler)
{
libxl_ctx *ctx = libxl__gc_owner(gc);
int nfds, rc;
@@ -395,17 +397,14 @@ start:
default:
l1 = xs_read_watch(ctx->xsh, &n);
if (l1 != NULL) {
- char *state = libxl__xs_read(gc, XBT_NULL,
+ char *sstate = libxl__xs_read(gc, XBT_NULL,
l1[XS_WATCH_PATH]);
- if (!state || atoi(state) == 6) {
- xs_unwatch(ctx->xsh, l1[0], l1[1]);
- xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]);
- LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
- "Destroyed device backend at %s",
- l1[XS_WATCH_TOKEN]);
- rc = 0;
+ if (!sstate || atoi(sstate) == state) {
+ /* Call handler function if present */
+ if (handler)
+ rc = handler(gc, l1, sstate);
} else {
- /* State is not "disconnected", continue waiting... */
+ /* State is different than expected, continue waiting... */
goto start;
}
free(l1);
@@ -418,6 +417,23 @@ start:
}

/*
+ * Handler function for device destruction to be passed to
+ * libxl__wait_for_device_state
+ */
+static int destroy_device(libxl__gc *gc, char **l1, char *state)
+{
+ libxl_ctx *ctx = libxl__gc_owner(gc);
+
+ xs_unwatch(ctx->xsh, l1[0], l1[1]);
+ xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]);
+ LIBXL__LOG(ctx, LIBXL__LOG_DEBUG,
+ "Destroyed device backend at %s",
+ l1[XS_WATCH_TOKEN]);
+
+ return 0;
+}
+
+/*
* Returns 0 (device already destroyed) or 1 (caller must
* wait_for_dev_destroy) on success, ERROR_* on fail.
*/
@@ -458,7 +474,8 @@ retry_transaction:
struct timeval tv;
tv.tv_sec = LIBXL_DESTROY_TIMEOUT;
tv.tv_usec = 0;
- rc = wait_for_dev_destroy(gc, &tv);
+ rc = libxl__wait_for_device_state(gc, &tv, XenbusStateClosed,
+ destroy_device);
if (rc < 0) /* an error or timeout occurred, clear watches */
xs_unwatch(ctx->xsh, state_path, be_path);
xs_rm(ctx->xsh, XBT_NULL, libxl__device_frontend_path(gc, dev));
@@ -566,7 +583,8 @@ int libxl__devices_destroy(libxl__gc *gc
tv.tv_sec = LIBXL_DESTROY_TIMEOUT;
tv.tv_usec = 0;
while (n_watches > 0) {
- if (wait_for_dev_destroy(gc, &tv) < 0) {
+ if (libxl__wait_for_device_state(gc, &tv, XenbusStateClosed,
+ destroy_device) < 0) {
/* function returned ERROR_* */
break;
} else {
diff -r 0abd95407888 -r 3b5b14fcd208 tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h Thu Dec 01 16:01:17 2011 +0100
+++ b/tools/libxl/libxl_internal.h Thu Dec 01 16:01:31 2011 +0100
@@ -20,11 +20,14 @@
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
+#include <sys/time.h>

#include <xs.h>
#include <xenctrl.h>
#include "xentoollog.h"

+#include <xen/io/xenbus.h>
+
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
#define _hidden __attribute__((visibility("hidden")))
#define _protected __attribute__((visibility("protected")))
@@ -252,6 +255,31 @@ _hidden int libxl__device_destroy(libxl_
_hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force);
_hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);

+/* Handler for the libxl__wait_for_device_state callback */
+/*
+ * libxl__device_state_handler - Handler for the libxl__wait_for_device_state
+ * gc: allocation pool
+ * l1: array containing the path and token
+ * state: string that contains the state of the device
+ *
+ * Returns 0 on success, and < 0 on error.
+ */
+typedef int libxl__device_state_handler(libxl__gc *gc, char **l1, char *state);
+
+/*
+ * libxl__wait_for_device_state - waits a given time for a device to
+ * reach a given state
+ * gc: allocation pool
+ * tv: timeval struct containing the maximum time to wait
+ * state: state to wait for (check xen/io/xenbus.h)
+ * handler: callback function to execute when state is reached
+ *
+ * Returns 0 on success, and < 0 on error.
+ */
+_hidden int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv,
+ XenbusState state,
+ libxl__device_state_handler handler);
+
/*
* libxl__try_phy_backend - Check if there's support for the passed
* type of file using the PHY backend

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel