Mailing List Archive

[PATCH] libxl: improve error handling when saving device model state
# HG changeset patch
# User Ian Campbell <ian.campbell@citrix.com>
# Date 1323795995 0
# Node ID 64e48ae2ef6760db221fe1896f7571260b0fd6ba
# Parent 738c34b1b0450724c4b91b739d1e9cae09f26035
libxl: improve error handling when saving device model state.

Do not leak a file descriptor (fd2 when used with upstream qemu) or a file (the
save file which is leaked on failure).

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

diff -r 738c34b1b045 -r 64e48ae2ef67 tools/libxl/libxl_dom.c
--- a/tools/libxl/libxl_dom.c Tue Dec 13 17:06:35 2011 +0000
+++ b/tools/libxl/libxl_dom.c Tue Dec 13 17:06:35 2011 +0000
@@ -605,7 +605,7 @@ out:
int libxl__domain_save_device_model(libxl__gc *gc, uint32_t domid, int fd)
{
libxl_ctx *ctx = libxl__gc_owner(gc);
- int fd2, c;
+ int ret, fd2, c;
char buf[1024];
const char *filename = libxl__device_model_savefile(gc, domid);
struct stat st;
@@ -630,8 +630,10 @@ int libxl__domain_save_device_model(libx
return ERROR_FAIL;
}
/* Save DM state into fd2 */
- if (libxl__qmp_migrate(gc, domid, fd2))
- return ERROR_FAIL;
+ ret = libxl__qmp_migrate(gc, domid, fd2);
+ close(fd2);
+ if (ret)
+ goto out_unlink;
break;
default:
return ERROR_INVAL;
@@ -646,31 +648,35 @@ int libxl__domain_save_device_model(libx
qemu_state_len = st.st_size;
LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Qemu state is %d bytes\n", qemu_state_len);

- c = libxl_write_exactly(ctx, fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE),
- "saved-state file", "qemu signature");
- if (c)
- return c;
+ ret = libxl_write_exactly(ctx, fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE),
+ "saved-state file", "qemu signature");
+ if (ret)
+ goto out_unlink;

- c = libxl_write_exactly(ctx, fd, &qemu_state_len, sizeof(qemu_state_len),
+ ret = libxl_write_exactly(ctx, fd, &qemu_state_len, sizeof(qemu_state_len),
"saved-state file", "saved-state length");
- if (c)
- return c;
+ if (ret)
+ goto out_unlink;

fd2 = open(filename, O_RDONLY);
while ((c = read(fd2, buf, sizeof(buf))) != 0) {
if (c < 0) {
if (errno == EINTR)
continue;
- return errno;
+ ret = errno;
+ goto out_close_fd2;
}
- c = libxl_write_exactly(
+ ret = libxl_write_exactly(
ctx, fd, buf, c, "saved-state file", "qemu state");
- if (c)
- return c;
+ if (ret)
+ goto out_close_fd2;
}
+ ret = 0;
+out_close_fd2:
close(fd2);
+out_unlink:
unlink(filename);
- return 0;
+ return ret;
}

char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid)

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