libxenlight: use watch and select in libxl_wait_for_device_model
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 2 Dec 2009 18:42:03 +0000 (18:42 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 2 Dec 2009 18:42:03 +0000 (18:42 +0000)
This patch reimplements libxl_wait_for_device_model using a xenstore
watch and a select loop.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
tools/libxl/libxl.c
tools/libxl/libxl_device.c
tools/libxl/libxl_internal.h

index ec75ca0af34a55b9cf235aeeb64513babca22e1d..10ed0e06fdaf7747de0ee695ed9f460365c90b93 100644 (file)
@@ -888,6 +888,7 @@ int libxl_create_device_model(struct libxl_ctx *ctx,
         *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
         if (!*starting_r) goto xit;
         p = *starting_r;
+        p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
     } else {
         p = &buf_starting;
         p->for_spawn = NULL;
index 13ef9bde48b01c8f30d8cd80b961f23d89726b5d..d9ae11291221cbddcc75cb2d2164670b8526d6a3 100644 (file)
@@ -297,31 +297,47 @@ int libxl_wait_for_device_model(struct libxl_ctx *ctx,
 {
     char path[50];
     char *p;
-    int watchdog = 100;
     unsigned int len;
     int rc;
+    struct xs_handle *xsh;
+    int nfds;
+    fd_set rfds;
+    struct timeval tv;
+    unsigned int num;
+    char **l = NULL;
 
+    xsh = xs_daemon_open();
     snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
-    while (watchdog > 0) {
-        p = xs_read(ctx->xsh, XBT_NULL, path, &len);
-        if (p == NULL) {
-            usleep(100000);
-            watchdog--;
-        } else {
-            if (state == NULL || !strcmp(state, p)) {
-                free(p);
-                return 0;
-            } else {
+    xs_watch(xsh, path, path);
+    tv.tv_sec = LIBXL_DEVICE_MODEL_START_TIMEOUT;
+    tv.tv_usec = 0;
+    nfds = xs_fileno(xsh) + 1;
+    while (tv.tv_sec > 0) {
+        FD_ZERO(&rfds);
+        FD_SET(xs_fileno(xsh), &rfds);
+        if (select(nfds, &rfds, NULL, NULL, &tv) > 0) {
+            l = xs_read_watch(xsh, &num);
+            if (l != NULL) {
+                free(l);
+                p = xs_read(xsh, XBT_NULL, path, &len);
+                if (!p)
+                    continue;
+                if (!state || !strcmp(state, p)) {
+                    free(p);
+                    xs_unwatch(xsh, path, path);
+                    xs_daemon_close(xsh);
+                    if (check_callback) {
+                        rc = check_callback(ctx, check_callback_userdata);
+                        if (rc) return rc;
+                    }
+                    return 0;
+                }
                 free(p);
-                usleep(100000);
-                watchdog--;
             }
         }
-        if (check_callback) {
-            rc = check_callback(ctx, check_callback_userdata);
-            if (rc) return rc;
-        }
     }
+    xs_unwatch(xsh, path, path);
+    xs_daemon_close(xsh);
     XL_LOG(ctx, XL_LOG_ERROR, "Device Model not ready");
     return -1;
 }
index 65dfac1150998a28a4016131ba094ba6a8c5aff1..a08821be343307dfd94712855d3757783985e67c 100644 (file)
@@ -28,6 +28,7 @@
 #include "libxl_utils.h"
 
 #define LIBXL_DESTROY_TIMEOUT 10
+#define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
 #define LIBXL_XENCONSOLE_LIMIT 1048576
 #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
 #define QEMU_SIGNATURE "QemuDeviceModelRecord"