# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1273739705 -3600
# Node ID 97da07c523ddc2565da67b6cfeec006d742bb06d
# Parent 540b2290642b11cbd7728a471fc74ba6fdca848a
libxl: Check return codes of write/asprintf/daemon consistently.
Signed-off-by: Keir Fraser <keir.fraser@xxxxxxxxxx>
---
tools/libxl/libxl.c | 27 ++++++++++++++++++---------
tools/libxl/libxl_dom.c | 15 +++++++++++++--
tools/libxl/libxl_utils.c | 2 ++
tools/libxl/libxl_xshelp.c | 3 ++-
tools/libxl/xl.c | 10 +++++++---
tools/libxl/xl_cmdimpl.c | 33 ++++++++++++++++++++++-----------
6 files changed, 64 insertions(+), 26 deletions(-)
diff -r 540b2290642b -r 97da07c523dd tools/libxl/libxl.c
--- a/tools/libxl/libxl.c Thu May 13 08:56:49 2010 +0100
+++ b/tools/libxl/libxl.c Thu May 13 09:35:05 2010 +0100
@@ -340,10 +340,13 @@ int libxl_domain_restore(struct libxl_ct
ret = build_post(ctx, domid, info, state, vments, localents);
if (ret) goto out;
- if (info->hvm)
- asprintf(&(dm_info->saved_state), "/var/lib/xen/qemu-save.%d", domid);
- else
- dm_info->saved_state = NULL;
+ dm_info->saved_state = NULL;
+ if (info->hvm) {
+ ret = asprintf(&dm_info->saved_state,
+ "/var/lib/xen/qemu-save.%d", domid);
+ ret = (ret < 0) ? ERROR_FAIL : 0;
+ }
+
out:
esave = errno;
@@ -488,7 +491,7 @@ int libxl_domain_suspend(struct libxl_ct
core_suspend(ctx, domid, fd, hvm, live, debug);
if (hvm)
- save_device_model(ctx, domid, fd);
+ return save_device_model(ctx, domid, fd);
return 0;
}
@@ -559,7 +562,8 @@ int libxl_wait_for_domain_death(struct l
int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid,
libxl_waiter *waiter)
{
waiter->path = strdup("@releaseDomain");
- asprintf(&(waiter->token), "%d", DOMAIN_DEATH);
+ if (asprintf(&(waiter->token), "%d", DOMAIN_DEATH) < 0)
+ return -1;
if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
return -1;
return 0;
@@ -574,8 +578,12 @@ int libxl_wait_for_disk_ejects(struct li
domid = guest_domid;
for (i = 0; i < num_disks; i++) {
- asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
libxl_xs_get_dompath(ctx, domid), device_disk_dev_number(disks[i].virtpath));
- asprintf(&(waiter[i].token), "%d", DISK_EJECT);
+ if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
+ libxl_xs_get_dompath(ctx, domid),
+ device_disk_dev_number(disks[i].virtpath)) < 0)
+ return -1;
+ if (asprintf(&(waiter[i].token), "%d", DISK_EJECT) < 0)
+ return -1;
xs_watch(ctx->xsh, waiter->path, waiter->token);
}
return 0;
@@ -902,7 +910,8 @@ void dm_xenstore_record_pid(void *for_sp
/* we mustn't use the parent's handle in the child */
kvs[0] = "image/device-model-pid";
- asprintf(&kvs[1], "%d", innerchild);
+ if (asprintf(&kvs[1], "%d", innerchild) < 0)
+ return;
kvs[2] = NULL;
rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
diff -r 540b2290642b -r 97da07c523dd tools/libxl/libxl_dom.c
--- a/tools/libxl/libxl_dom.c Thu May 13 08:56:49 2010 +0100
+++ b/tools/libxl/libxl_dom.c Thu May 13 09:35:05 2010 +0100
@@ -349,10 +349,21 @@ int save_device_model(struct libxl_ctx *
libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx,
"/local/domain/0/device-model/%d/command", domid), "save", strlen("save"));
libxl_wait_for_device_model(ctx, domid, "paused", NULL, NULL);
- write(fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE));
+ c = libxl_write_exactly(ctx, fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE),
+ "saved-state file", "qemu signature");
+ if (c)
+ return c;
fd2 = open(filename, O_RDONLY);
while ((c = read(fd2, buf, sizeof(buf))) != 0) {
- write(fd, buf, c);
+ if (c < 0) {
+ if (errno == EINTR)
+ continue;
+ return errno;
+ }
+ c = libxl_write_exactly(
+ ctx, fd, buf, c, "saved-state file", "qemu state");
+ if (c)
+ return c;
}
close(fd2);
unlink(filename);
diff -r 540b2290642b -r 97da07c523dd tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Thu May 13 08:56:49 2010 +0100
+++ b/tools/libxl/libxl_utils.c Thu May 13 09:35:05 2010 +0100
@@ -298,11 +298,13 @@ int libxl_read_file_contents(struct libx
got = rw(fd, data, sz); \
if (got == -1) { \
if (errno == EINTR) continue; \
+ if (!ctx) return errno; \
XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to " #rw " %s%s%s", \
what?what:"", what?" from ":"", filename); \
return errno; \
} \
if (got == 0) { \
+ if (!ctx) return EPROTO; \
XL_LOG(ctx, XL_LOG_ERROR, \
zero_is_eof \
? "file/stream truncated reading %s%s%s" \
diff -r 540b2290642b -r 97da07c523dd tools/libxl/libxl_xshelp.c
--- a/tools/libxl/libxl_xshelp.c Thu May 13 08:56:49 2010 +0100
+++ b/tools/libxl/libxl_xshelp.c Thu May 13 09:35:05 2010 +0100
@@ -32,7 +32,8 @@ int xs_writev(struct xs_handle *xsh, xs_
return 0;
for (i = 0; kvs[i] != NULL; i += 2) {
- asprintf(&path, "%s/%s", dir, kvs[i]);
+ if (asprintf(&path, "%s/%s", dir, kvs[i]) < 0)
+ return -1;
if (path && kvs[i + 1]) {
int length = strlen(kvs[i + 1]);
xs_write(xsh, t, path, kvs[i + 1], length);
diff -r 540b2290642b -r 97da07c523dd tools/libxl/xl.c
--- a/tools/libxl/xl.c Thu May 13 08:56:49 2010 +0100
+++ b/tools/libxl/xl.c Thu May 13 09:35:05 2010 +0100
@@ -28,18 +28,22 @@
#include <inttypes.h>
#include "libxl.h"
+#include "libxl_utils.h"
#include "xl_cmdimpl.h"
#include "xl_cmdtable.h"
extern struct libxl_ctx ctx;
extern int logfile;
-void log_callback(void *userdata, int loglevel, const char *file, int line,
const char *func, char *s)
+void log_callback(
+ void *userdata, int loglevel, const char *file,
+ int line, const char *func, char *s)
{
char str[1024];
- snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n", loglevel, file, line,
func, s);
- write(logfile, str, strlen(str));
+ snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n",
+ loglevel, file, line, func, s);
+ libxl_write_exactly(NULL, logfile, str, strlen(str), NULL, NULL);
}
int main(int argc, char **argv)
diff -r 540b2290642b -r 97da07c523dd tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c Thu May 13 08:56:49 2010 +0100
+++ b/tools/libxl/xl_cmdimpl.c Thu May 13 09:35:05 2010 +0100
@@ -152,7 +152,7 @@ void dolog(const char *file, int line, c
rc = vasprintf(&s, fmt, ap);
va_end(ap);
if (rc >= 0)
- write(logfile, s, rc);
+ libxl_write_exactly(NULL, logfile, s, rc, NULL, NULL);
}
static void init_create_info(libxl_domain_create_info *c_info)
@@ -500,7 +500,10 @@ static void parse_config_data(const char
} else {
char *cmdline;
if (!xlu_cfg_get_string (config, "root", &buf)) {
- asprintf(&cmdline, "root=%s", buf);
+ if (asprintf(&cmdline, "root=%s", buf) < 0) {
+ fprintf(stderr, "Failed to allocate memory in asprintf\n");
+ exit(1);
+ }
b_info->u.pv.cmdline = cmdline;
}
if (!xlu_cfg_get_string (config, "ramdisk", &buf))
@@ -956,7 +959,10 @@ static int create_domain(struct domain_c
* file; and we receive it to a temporary name */
assert(!common_domname);
common_domname = info1.name;
- asprintf(migration_domname_r, "%s--incoming", info1.name);
+ if (asprintf(migration_domname_r, "%s--incoming", info1.name) < 0)
{
+ fprintf(stderr, "Failed to allocate memory in asprintf\n");
+ exit(1);
+ }
info1.name = *migration_domname_r;
}
}
@@ -1080,7 +1086,10 @@ start:
exit(-1);
}
- asprintf(&name, "xl-%s", info1.name);
+ if (asprintf(&name, "xl-%s", info1.name) < 0) {
+ LOG("Failed to allocate memory in asprintf");
+ exit(1);
+ }
rc = libxl_create_logfile(&ctx, name, &fullname);
if (rc) {
LOG("failed to open logfile %s",fullname,strerror(errno));
@@ -1096,7 +1105,7 @@ start:
dup2(logfile, 1);
dup2(logfile, 2);
- daemon(0, 1);
+ CHK_ERRNO(daemon(0, 1) < 0);
need_daemon = 0;
}
LOG("Waiting for domain %s (domid %d) to die [pid %ld]",
@@ -1699,7 +1708,7 @@ int save_domain(char *p, char *filename,
save_domain_core_writeconfig(fd, filename, config_data, config_len);
- libxl_domain_suspend(&ctx, NULL, domid, fd);
+ CHK_ERRNO(libxl_domain_suspend(&ctx, NULL, domid, fd));
close(fd);
if (checkpoint)
@@ -1882,7 +1891,8 @@ static void migrate_domain(char *domain_
fprintf(stderr, "migration sender: Target has acknowledged transfer.\n");
if (common_domname) {
- asprintf(&away_domname, "%s--migratedaway", common_domname);
+ if (asprintf(&away_domname, "%s--migratedaway", common_domname) < 0)
+ goto failed_resume;
rc = libxl_domain_rename(&ctx, domid,
common_domname, away_domname, 0);
if (rc) goto failed_resume;
@@ -2218,10 +2228,11 @@ int main_migrate(int argc, char **argv)
if (!ssh_command[0]) {
rune= host;
} else {
- asprintf(&rune, "exec %s %s xl migrate-receive%s%s",
- ssh_command, host,
- daemonize ? "" : " -e",
- debug ? " -d" : "");
+ if (asprintf(&rune, "exec %s %s xl migrate-receive%s%s",
+ ssh_command, host,
+ daemonize ? "" : " -e",
+ debug ? " -d" : "") < 0)
+ exit(1);
}
migrate_domain(p, rune, config_filename);
_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog
|