Skip to contentSkip to conversation

libxl last fixups

PATCH New Vincent Hanquez 1 participant 7 messages Jan 11, 2010

lore
Share & tools
lore
#0 lore raw

cover libxl last fixups

Last batch of important fixups intertwined with various cleanups and stability fixes:

Lots of more values checking, and abort as soon as there are errors. Contains a critical usability fix regarding waitpid (typo in option) and without this batch it's very easy to trigger a SIGBUS in xenstored.

The library is now robust to:
- missing xenstored
- missing xenctrl privcmd
- not enough memory to build a domain
- missing vif devices
- and some usually unexpected situation

not addressed yet: fixup stubdomains building to have the same robustness.

Vincent Hanquez (6):
tests a lots more of xl return value inside the library and in xl. do not try to set memory target with a number we haven't verified in set-mem. misc cleanup
typo in old patch, lead to waitpid forever instead of waitpid with WNOHANG remove ctx argument to exec
remove ctx dangerously passed to children.

 tools/libxl/libxl.c          |   54 +++++++++++++++++++++++++----------------
 tools/libxl/libxl_exec.c     |   22 +++++++---------
 tools/libxl/libxl_internal.h |    7 +----
 tools/libxl/xl.c             |   39 +++++++++++++++++++++++++----
 4 files changed, 78 insertions(+), 44 deletions(-)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

PATCH 1/6 tests a lots more of xl return value inside the library and in xl.

introducing a domain where the xenguest build function has fail, lead to having xenstored receiving SIGBUS, since it's trying to access some of the domain's memory, which haven't been properly allocated. (it doesn't seems to be a way to make xenstored more robust to this though since xc_map_foreign_range just succeed).

make xl a lot more robust regarding all those random errors possible.

---
 tools/libxl/libxl.c |   37 +++++++++++++++++++++++++------------
 tools/libxl/xl.c    |   29 ++++++++++++++++++++++++-----
 2 files changed, 49 insertions(+), 17 deletions(-)
tools/libxl/libxl.c +25 -12
index c2dcbcc..15299ea 100644
@@ -176,18 +176,24 @@ retry_transaction:
 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
 {
     char **vments = NULL, **localents = NULL;
-    int i;
+    int i, ret;
+
+    ret = build_pre(ctx, domid, info, state);
+    if (ret) goto out;
 
-    build_pre(ctx, domid, info, state);
     if (info->hvm) {
-        build_hvm(ctx, domid, info, state);
+        ret = build_hvm(ctx, domid, info, state);
+        if (ret) goto out;
+
         vments = libxl_calloc(ctx, 5, sizeof(char *));
         vments[0] = "rtc/timeoffset";
         vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
         vments[2] = "image/ostype";
         vments[3] = "hvm";
     } else {
-        build_pv(ctx, domid, info, state);
+        ret = build_pv(ctx, domid, info, state);
+        if (ret) goto out;
+
         vments = libxl_calloc(ctx, 9, sizeof(char *));
         i = 0;
         vments[i++] = "image/ostype";
@@ -203,8 +209,9 @@ int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uin
             vments[i++] = (char*) info->u.pv.cmdline;
         }
     }
-    build_post(ctx, domid, info, state, vments, localents);
-    return 0;
+    ret = build_post(ctx, domid, info, state, vments, localents);
+out:
+    return ret;
 }
 
 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
@@ -212,10 +219,14 @@ int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
                          libxl_device_model_info *dm_info)
 {
     char **vments = NULL, **localents = NULL;
-    int i;
+    int i, ret;
+
+    ret = build_pre(ctx, domid, info, state);
+    if (ret) goto out;
+
+    ret = restore_common(ctx, domid, info, state, fd);
+    if (ret) goto out;
 
-    build_pre(ctx, domid, info, state);
-    restore_common(ctx, domid, info, state, fd);
     if (info->hvm) {
         vments = libxl_calloc(ctx, 5, sizeof(char *));
         vments[0] = "rtc/timeoffset";
@@ -238,13 +249,15 @@ int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
             vments[i++] = (char*) info->u.pv.cmdline;
         }
     }
-    build_post(ctx, domid, info, state, vments, localents);
+    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;
-
-    return 0;
+out:
+    return ret;
 }
 
 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
tools/libxl/xl.c +24 -5
index 996965b..851519f 100644
@@ -750,6 +750,7 @@ static void create_domain(int debug, int daemonize, const char *config_file, con
     int num_disks = 0, num_vifs = 0, num_pcidevs = 0, num_vfbs = 0, num_vkbs = 0;
     int i, fd;
     int need_daemon = 1;
+    int ret;
     libxl_device_model_starting *dm_starting = 0;
     libxl_waiter *w1 = NULL, *w2 = NULL;
     memset(&dm_info, 0x00, sizeof(dm_info));
@@ -768,29 +769,47 @@ start:
     }
 
     libxl_ctx_set_log(&ctx, log_callback, NULL);
-    libxl_domain_make(&ctx, &info1, &domid);
+
+    ret = libxl_domain_make(&ctx, &info1, &domid);
+    if (ret) {
+        fprintf(stderr, "cannot make domain: %d\n", ret);
+        return;
+    }
 
     if (!restore_file || !need_daemon) {
         if (dm_info.saved_state) {
             free(dm_info.saved_state);
             dm_info.saved_state = NULL;
         }
-        libxl_domain_build(&ctx, &info2, domid, &state);
+        ret = libxl_domain_build(&ctx, &info2, domid, &state);
     } else {
         int restore_fd;
 
         restore_fd = open(restore_file, O_RDONLY);
-        libxl_domain_restore(&ctx, &info2, domid, restore_fd, &state, &dm_info);
+        ret = libxl_domain_restore(&ctx, &info2, domid, restore_fd, &state, &dm_info);
         close(restore_fd);
     }
 
+    if (ret) {
+        fprintf(stderr, "cannot (re-)build domain: %d\n", ret);
+        return;
+    }
+
     for (i = 0; i < num_disks; i++) {
         disks[i].domid = domid;
-        libxl_device_disk_add(&ctx, domid, &disks[i]);
+        ret = libxl_device_disk_add(&ctx, domid, &disks[i]);
+        if (ret) {
+            fprintf(stderr, "cannot add disk %d to domain: %d\n", i, ret);
+            return;
+        }
     }
     for (i = 0; i < num_vifs; i++) {
         vifs[i].domid = domid;
-        libxl_device_nic_add(&ctx, domid, &vifs[i]);
+        ret = libxl_device_nic_add(&ctx, domid, &vifs[i]);
+        if (ret) {
+            fprintf(stderr, "cannot add nic %d to domain: %d\n", i, ret);
+            return;
+        }
     }
     if (info1.hvm) {
         dm_info.domid = domid;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw

PATCH 2/6 do not try to set memory target with a number we haven't verified in set-mem.

In reply to Vincent Hanquez

checking that memory string conversion what done properly instead of sending a request to balloon a domain to 0 memory.

---
 tools/libxl/xl.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
tools/libxl/xl.c +9 -1
index 851519f..2cf08cd 100644
@@ -993,6 +993,8 @@ void set_memory_target(char *p, char *mem)
 {
     struct libxl_ctx ctx;
     uint32_t domid;
+    uint32_t memorykb;
+    char *endptr;
 
     if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
         fprintf(stderr, "cannot init xl context\n");
@@ -1004,7 +1006,13 @@ void set_memory_target(char *p, char *mem)
         fprintf(stderr, "%s is an invalid domain identifier\n", p);
         exit(2);
     }
-    libxl_set_memory_target(&ctx, domid, atoi(mem));
+    memorykb = strtoul(mem, &endptr, 10);
+    if (*endptr != '\0') {
+        fprintf(stderr, "invalid memory size: %s\n", mem);
+        exit(3);
+    }
+    printf("setting domid %d memory to : %d\n", domid, memorykb);
+    libxl_set_memory_target(&ctx, domid, memorykb);
 }
 
 int main_memset(int argc, char **argv)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw

PATCH 3/6 misc cleanup

In reply to Vincent Hanquez

wait_for_device_model expect two pointer as the end not 2 integers. remove debugging message in libxl_list

---
 tools/libxl/libxl.c |    9 ++++-----
 1 files changed, 4 insertions(+), 5 deletions(-)
tools/libxl/libxl.c +4 -5
index 15299ea..518064e 100644
@@ -299,7 +299,6 @@ struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
     for (i = 0; i < ret; i++) {
         memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
         ptr[index].domid = info[i].domain;
-	    printf("domain %d [ret %d]\n", ptr[index].domid, ret);
 
         if (info[i].flags & XEN_DOMINF_dying)
             ptr[index].dying = 1;
@@ -1900,7 +1899,7 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci
 
     hvm = is_hvm(ctx, domid);
     if (hvm) {
-        if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
+        if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
             return -1;
         }
         path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
@@ -1914,7 +1913,7 @@ int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci
                            pcidev->bus, pcidev->dev, pcidev->func);
         path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
         xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
-        if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
+        if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", NULL, NULL) < 0)
             XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
         path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
         vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
@@ -1993,7 +1992,7 @@ int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_
 
     hvm = is_hvm(ctx, domid);
     if (hvm) {
-        if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
+        if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
             return -1;
         }
         path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
@@ -2003,7 +2002,7 @@ int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_
                        pcidev->bus, pcidev->dev, pcidev->func);
         path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
         xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
-        if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
+        if (libxl_wait_for_device_model(ctx, domid, "pci-removed", NULL, NULL) < 0) {
             XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
             return -1;
         }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw

PATCH 4/6 typo in old patch, lead to waitpid forever instead of waitpid with WNOHANG

In reply to Vincent Hanquez

fix qemu starting problem

---
 tools/libxl/libxl_exec.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_exec.c +2 -2
index ba92019..1588a1f 100644
@@ -45,7 +45,7 @@ static pid_t libxl_fork(struct libxl_ctx *ctx)
 
 static int call_waitpid(pid_t (*waitpid_cb)(pid_t, int *, int), pid_t pid, int *status, int options)
 {
-    return (waitpid_cb) ? waitpid_cb(pid, status, 0) : waitpid(pid, status, 0);
+    return (waitpid_cb) ? waitpid_cb(pid, status, options) : waitpid(pid, status, options);
 }
 
 void libxl_exec(struct libxl_ctx *ctx, int stdinfd, int stdoutfd, int stderrfd,
@@ -202,7 +202,7 @@ int libxl_spawn_check(struct libxl_ctx *ctx, void *for_spawn_void)
     if (!for_spawn) return 0;
 
     assert(for_spawn->intermediate);
-    got = call_waitpid(ctx->waitpid_instead, for_spawn->intermediate, &status, 0);
+    got = call_waitpid(ctx->waitpid_instead, for_spawn->intermediate, &status, WNOHANG);
     if (!got) return 0;
 
     assert(got == for_spawn->intermediate);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#5 lore raw

PATCH 5/6 remove ctx argument to exec

In reply to Vincent Hanquez
---
 tools/libxl/libxl.c          |    4 ++--
 tools/libxl/libxl_exec.c     |    4 +---
 tools/libxl/libxl_internal.h |    3 +--
 3 files changed, 4 insertions(+), 7 deletions(-)
tools/libxl/libxl.c +2 -2
index 518064e..36e72cf 100644
@@ -974,7 +974,7 @@ int libxl_create_device_model(struct libxl_ctx *ctx,
     rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
     if (rc < 0) goto xit;
     if (!rc) { /* inner child */
-        libxl_exec(ctx, null, logfile_w, logfile_w,
+        libxl_exec(null, logfile_w, logfile_w,
                    info->device_model, args);
     }
 
@@ -1118,7 +1118,7 @@ int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_di
 
                         null_r = open("/dev/null", O_RDONLY);
                         null_w = open("/dev/null", O_WRONLY);
-                        libxl_exec(ctx, null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
+                        libxl_exec(null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
                         XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
                     }
                     close(p[1]);
tools/libxl/libxl_exec.c +1 -3
index 1588a1f..011ed84 100644
@@ -48,8 +48,7 @@ static int call_waitpid(pid_t (*waitpid_cb)(pid_t, int *, int), pid_t pid, int *
     return (waitpid_cb) ? waitpid_cb(pid, status, options) : waitpid(pid, status, options);
 }
 
-void libxl_exec(struct libxl_ctx *ctx, int stdinfd, int stdoutfd, int stderrfd,
-                char *arg0, char **args)
+void libxl_exec(int stdinfd, int stdoutfd, int stderrfd, char *arg0, char **args)
      /* call this in the child */
 {
     int i;
@@ -63,7 +62,6 @@ void libxl_exec(struct libxl_ctx *ctx, int stdinfd, int stdoutfd, int stderrfd,
     for (i = 4; i < 256; i++)
         close(i);
     execv(arg0, args);
-    XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "exec %s failed", arg0);
     _exit(-1);
 }
 
tools/libxl/libxl_internal.h +1 -2
index 44e97d1..14952ca 100644
@@ -203,8 +203,7 @@ int libxl_spawn_check(struct libxl_ctx *ctx,
 
  /* low-level stuff, for synchronous subprocesses etc. */
 
-void libxl_exec(struct libxl_ctx *ctx, int stdinfd, int stdoutfd, int stderrfd,
-                char *arg0, char **args); // logs errors, never returns
+void libxl_exec(int stdinfd, int stdoutfd, int stderrfd, char *arg0, char **args); // logs errors, never returns
 void libxl_log_child_exitstatus(struct libxl_ctx *ctx,
                                 const char *what, pid_t pid, int status);
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#6 lore raw

PATCH 6/6 remove ctx dangerously passed to children.

In reply to Vincent Hanquez

apart from ctx->waitpid, it's potentially harmful to call into logging.

---
 tools/libxl/libxl.c          |    4 ++--
 tools/libxl/libxl_exec.c     |   14 +++++++-------
 tools/libxl/libxl_internal.h |    4 +---
 3 files changed, 10 insertions(+), 12 deletions(-)
tools/libxl/libxl.c +2 -2
index 36e72cf..df3ddc0 100644
@@ -726,8 +726,8 @@ static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
     return (char **) flexarray_contents(dm_args);
 }
 
-void dm_xenstore_record_pid(struct libxl_ctx *ctx, void *for_spawn,
-                            pid_t innerchild) {
+void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
+{
     struct libxl_device_model_starting *starting = for_spawn;
     char *kvs[3];
     int rc;
tools/libxl/libxl_exec.c +7 -7
index 011ed84..e8cc74c 100644
@@ -100,8 +100,7 @@ void libxl_report_child_exitstatus(struct libxl_ctx *ctx,
 int libxl_spawn_spawn(struct libxl_ctx *ctx,
                       libxl_device_model_starting *starting,
                       const char *what,
-                      void (*intermediate_hook)(struct libxl_ctx *ctx,
-                                                void *for_spawn,
+                      void (*intermediate_hook)(void *for_spawn,
                                                 pid_t innerchild))
 {
     pid_t child, got;
@@ -127,18 +126,19 @@ int libxl_spawn_spawn(struct libxl_ctx *ctx,
 
     /* we are now the intermediate process */
 
-    child = libxl_fork(ctx);
-    if (!child) return 0; /* caller runs child code */
-    if (child < 0) exit(255);
+    child = fork();
+    if (child == -1)
+        exit(255);
+    if (!child)
+        return 0; /* caller runs child code */
 
-    intermediate_hook(ctx, starting, child);
+    intermediate_hook(starting, child);
 
     if (!for_spawn) _exit(0); /* just detach then */
 
     got = call_waitpid(ctx->waitpid_instead, child, &status, 0);
     assert(got == child);
 
-    libxl_report_child_exitstatus(ctx, what, child, status);
     _exit(WIFEXITED(status) ? WEXITSTATUS(status) :
           WIFSIGNALED(status) && WTERMSIG(status) < 127
           ? WTERMSIG(status)+128 : -1);
tools/libxl/libxl_internal.h +1 -3
index 14952ca..7664e59 100644
@@ -181,9 +181,7 @@ struct libxl_device_model_starting {
 int libxl_spawn_spawn(struct libxl_ctx *ctx,
                       libxl_device_model_starting *starting,
                       const char *what,
-                      void (*intermediate_hook)(struct libxl_ctx *ctx,
-                                                void *for_spawn,
-                                                pid_t innerchild));
+                      void (*intermediate_hook)(void *for_spawn, pid_t innerchild));
   /* Logs errors.  A copy of "what" is taken.  Return values:
    *  < 0   error, for_spawn need not be detached
    *   +1   caller is now the inner child, should probably call libxl_exec

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel