aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl
diff options
context:
space:
mode:
authorKeir Fraser <keir.fraser@citrix.com>2010-05-13 09:35:05 +0100
committerKeir Fraser <keir.fraser@citrix.com>2010-05-13 09:35:05 +0100
commiteac0f64d8003db0008b8bace4f6c94289a16b3c1 (patch)
tree4967e188fd97bcdbb63e1f0f611d273b6e07ad94 /tools/libxl
parentd09e6e47f0a3fce0c88f8e8334d989c341326ed3 (diff)
downloadxen-eac0f64d8003db0008b8bace4f6c94289a16b3c1.tar.gz
xen-eac0f64d8003db0008b8bace4f6c94289a16b3c1.tar.bz2
xen-eac0f64d8003db0008b8bace4f6c94289a16b3c1.zip
libxl: Check return codes of write/asprintf/daemon consistently.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
Diffstat (limited to 'tools/libxl')
-rw-r--r--tools/libxl/libxl.c27
-rw-r--r--tools/libxl/libxl_dom.c15
-rw-r--r--tools/libxl/libxl_utils.c2
-rw-r--r--tools/libxl/libxl_xshelp.c3
-rw-r--r--tools/libxl/xl.c10
-rw-r--r--tools/libxl/xl_cmdimpl.c33
6 files changed, 64 insertions, 26 deletions
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index c47a50424b..967f804037 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -340,10 +340,13 @@ int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
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_ctx *ctx, libxl_domain_suspend_info *info,
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_get_wait_fd(struct libxl_ctx *ctx, int *fd)
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 libxl_ctx *ctx, uint32_t guest_domid, libx
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_spawn, pid_t innerchild)
/* 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 --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index a4f2087786..dcace7a02d 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -349,10 +349,21 @@ int save_device_model(struct libxl_ctx *ctx, uint32_t domid, int fd)
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 --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index 620c254daf..c6ef6cb870 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -298,11 +298,13 @@ int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
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 --git a/tools/libxl/libxl_xshelp.c b/tools/libxl/libxl_xshelp.c
index 66de589c36..16e8c842ee 100644
--- a/tools/libxl/libxl_xshelp.c
+++ b/tools/libxl/libxl_xshelp.c
@@ -32,7 +32,8 @@ int xs_writev(struct xs_handle *xsh, xs_transaction_t t, char *dir, char *kvs[])
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 --git a/tools/libxl/xl.c b/tools/libxl/xl.c
index 248e5e4bab..a01bf38658 100644
--- a/tools/libxl/xl.c
+++ b/tools/libxl/xl.c
@@ -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 --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 4efd114aed..aa07996510 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -152,7 +152,7 @@ void dolog(const char *file, int line, const char *func, char *fmt, ...)
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 *configfile_filename_report,
} 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_create *dom_info)
* 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, int checkpoint,
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_spec, const char *rune,
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);