libxl: Check return codes of write/asprintf/daemon consistently.
authorKeir Fraser <keir.fraser@citrix.com>
Thu, 13 May 2010 08:35:05 +0000 (09:35 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Thu, 13 May 2010 08:35:05 +0000 (09:35 +0100)
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
tools/libxl/libxl.c
tools/libxl/libxl_dom.c
tools/libxl/libxl_utils.c
tools/libxl/libxl_xshelp.c
tools/libxl/xl.c
tools/libxl/xl_cmdimpl.c

index c47a50424b81c2c1caad5caae88e4374279bd405..967f804037b6dfdde0d078eda1f276c2e6fb584c 100644 (file)
@@ -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);
index a4f2087786028038bb9d2a951ea0bcb6b961737c..dcace7a02dcbf765aafc9f69758c74d0e896722e 100644 (file)
@@ -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);
index 620c254dafbb5f78ab010b0ecadd0a1a6071d8f5..c6ef6cb870f2dfa6285320f4211893019cb5c149 100644 (file)
@@ -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"             \
index 66de589c368ee747f5b86fb7120874afaf5f29c2..16e8c842ee293f12bf7659f5c63484ad1edab44b 100644 (file)
@@ -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);
index 248e5e4babb8450deaaddcbfb3a21f76fe9e3c03..a01bf386581474a058abedc16f98e5f673d517ba 100644 (file)
 #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)
index 4efd114aedbe595f65ac86a6d0a7f9e4b82ba673..aa079965106660aa644af19afd7d57a2d5c6a3ec 100644 (file)
@@ -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);