WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH 14/14]: libxenlight, checks before device deletion an

To: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH 14/14]: libxenlight, checks before device deletion and addition
From: Andres Lagar-Cavilla <andres@xxxxxxxxxxxxxxxx>
Date: Mon, 30 Nov 2009 14:18:18 -0500
Cc:
Delivery-date: Mon, 30 Nov 2009 11:20:35 -0800
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
User-agent: Thunderbird 2.0.0.23 (X11/20090817)
Proper existential checks before adding or deleting a
device, i.e. return error if:

- Adding a device to a non-existent domain
- Adding a device twice
- Deleting a non-existent device
- Deleting a device from a non-existent domain

Signed-off-by: Andres Lagar-Cavilla <andres@xxxxxxxxxxxxxxxx>


# HG changeset patch
# User Andres Lagar-Cavilla <andres@xxxxxxxxxxxxxxxx>
# Date 1259606866 18000
# Node ID 7a2b9d53941757142b408d09020099990b725467
# Parent  bf82205e60f45e98ab637c68d5c305dcb3e167cd
Proper existential checks before adding or deleting a 
device, i.e. return error if:

- Adding a device to a non-existent domain
- Adding a device twice
- Deleting a non-existent device
- Deleting a device from a non-existent domain

Signed-off-by: Andres Lagar-Cavilla <andres@xxxxxxxxxxxxxxxx>

diff -r bf82205e60f4 -r 7a2b9d539417 libxl.c
--- a/libxl.c
+++ b/libxl.c
@@ -1008,6 +1008,7 @@ int libxl_device_disk_add(struct libxl_c
     int devid;
     libxl_device device;
     int major, minor;
+    int rc;
 
     front = flexarray_make(16, 1);
     if (!front)
@@ -1126,12 +1127,12 @@ int libxl_device_disk_add(struct libxl_c
         flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
     }
 
-    libxl_device_generic_add(ctx, &device,
+    rc = libxl_device_generic_add(ctx, &device,
                              libxl_xs_kvs_of_flexarray(ctx, back, boffset),
                              libxl_xs_kvs_of_flexarray(ctx, front, foffset));
     flexarray_free(back);
     flexarray_free(front);
-    return 0;
+    return rc;
 }
 
 int libxl_device_disk_del(struct libxl_ctx *ctx, 
@@ -1169,6 +1170,7 @@ int libxl_device_nic_add(struct libxl_ct
     unsigned int boffset = 0;
     unsigned int foffset = 0;
     libxl_device device;
+    int rc;
 
     front = flexarray_make(16, 1);
     if (!front)
@@ -1214,14 +1216,14 @@ int libxl_device_nic_add(struct libxl_ct
         flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
     }
 
-    libxl_device_generic_add(ctx, &device,
+    rc = libxl_device_generic_add(ctx, &device,
                              libxl_xs_kvs_of_flexarray(ctx, back, boffset),
                              libxl_xs_kvs_of_flexarray(ctx, front, foffset));
 
     /* FIXME: wait for plug */
     flexarray_free(back);
     flexarray_free(front);
-    return 0;
+    return rc;
 }
 
 int libxl_device_nic_del(struct libxl_ctx *ctx, 
@@ -1257,6 +1259,7 @@ int libxl_device_console_add(struct libx
     unsigned int boffset = 0;
     unsigned int foffset = 0;
     libxl_device device;
+    int rc;
 
     if (console->build_state) {
         xs_transaction_t t;
@@ -1319,13 +1322,13 @@ retry_transaction:
     else
         flexarray_set(front, foffset++, "ioemu");
 
-    libxl_device_generic_add(ctx, &device,
+    rc = libxl_device_generic_add(ctx, &device,
                              libxl_xs_kvs_of_flexarray(ctx, back, boffset),
                              libxl_xs_kvs_of_flexarray(ctx, front, foffset));
     flexarray_free(back);
     flexarray_free(front);
 
-    return 0;
+    return rc;
 }
 
 
/******************************************************************************/
@@ -1336,6 +1339,7 @@ int libxl_device_vkb_add(struct libxl_ct
     unsigned int boffset = 0;
     unsigned int foffset = 0;
     libxl_device device;
+    int rc;
 
     front = flexarray_make(16, 1);
     if (!front)
@@ -1371,7 +1375,7 @@ int libxl_device_vkb_add(struct libxl_ct
     flexarray_free(back);
     flexarray_free(front);
 
-    return 0;
+    return rc;
 }
 
 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
diff -r bf82205e60f4 -r 7a2b9d539417 libxl_device.c
--- a/libxl_device.c
+++ b/libxl_device.c
@@ -45,6 +45,18 @@ int libxl_device_generic_add(struct libx
     struct xs_permissions backend_perms[2];
     struct xs_permissions hotplug_perms[1];
 
+    /* Check the domain exists before trying to add */
+    {
+        xc_dominfo_t *info = libxl_domain_info(ctx, device->domid);
+        if (!info) {
+            XL_LOG(ctx, XL_LOG_ERROR, "Adding %s to inexistent domain %u\n",
+                                        string_of_kinds[device->kind], 
+                                        device->domid);
+            return ERROR_FAIL;
+        }
+        free(info);
+    }
+
     dom_path_backend = libxl_xs_get_dompath(ctx, device->backend_domid);
     dom_path = libxl_xs_get_dompath(ctx, device->domid);
 
@@ -68,9 +80,29 @@ int libxl_device_generic_add(struct libx
     hotplug_perms[0].id = device->backend_domid;
     hotplug_perms[0].perms = XS_PERM_NONE;
 
+    {
+        /* Read frontend_path and check state before removing stuff */
+        char *state_path = libxl_sprintf(ctx, "%s/state", frontend_path);
+        char *state = libxl_xs_read(ctx, XBT_NULL, state_path);
+        libxl_free(ctx, state_path);
+        if (state) {
+            int state_num = atoi(state);
+            libxl_free(ctx, state);
+            if (state_num == 4) {
+                XL_LOG(ctx, XL_LOG_ERROR, "Trying to add twice device "
+                                          "with frontend path %s\n", 
frontend_path);
+                libxl_free(ctx, dom_path_backend);
+                libxl_free(ctx, dom_path);
+                libxl_free(ctx, frontend_path);
+                libxl_free(ctx, backend_path);
+                libxl_free(ctx, hotplug_path);
+                return -1;
+            }
+        }
+    }
+
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
-    /* FIXME: read frontend_path and check state before removing stuff */
 
     xs_rm(ctx->xsh, t, frontend_path);
     xs_rm(ctx->xsh, t, backend_path);
@@ -179,12 +211,20 @@ int libxl_device_destroy(struct libxl_ct
     xs_transaction_t t;
     char *state_path = libxl_sprintf(ctx, "%s/state", be_path);
     char *state = libxl_xs_read(ctx, XBT_NULL, state_path);
-    if (!state)
-        return 0;
+    if (!state) {
+        XL_LOG(ctx, XL_LOG_ERROR, "Trying to remove non-existent device "
+                                  "with backend path %s\n", be_path);
+        libxl_free(ctx, state_path);
+        libxl_free(ctx, state);
+        return -1;
+    }
     if (atoi(state) != 4) {
         xs_rm(ctx->xsh, XBT_NULL, be_path);
+        libxl_free(ctx, state_path);
+        libxl_free(ctx, state);
         return 0;
     }
+    libxl_free(ctx, state);
 
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
<Prev in Thread] Current Thread [Next in Thread>