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
|