debuggers.hg

changeset 22888:ccfa0527893e

libxl, xl: fixes to domain creation cleanup logic (domid values)

libxl__domain_make makes some assumptions about the way its caller
treats its uint32_t *domid parameter: specifically, if it fails it may
have partially created the domain and it does not every destroy it.
But it does not initialise it. Document this assumption in a comment,
and assert on entry that domid not a guest domain id, to ensure that
the caller has properly initialised it.

Introduce a function libxl_domid_valid_guest to help with this.

This is not intended to produce any practical functional change in
current code.

Secondly, libxl_create_stubdom calls libxl__domain_make and has no
code to tear down the domain again on error. This is complicated to
fix (since it may even be possible for the the domain to be left in a
state where it's not possible to tell that it was going to be a
stubdom for some other domain). So for now simply leave a fixme
comment.

Finally, in 22739:d839631b6048 we introduced "-1" as a sentinel "no
such domain" value for domid. However, domid is a uint32 so testing
it with "if (domid > 0)" as we do in 22740:ce208811f540 is wrong
because it always triggers. Instead use libxl_domid_valid_guest.
This fix means that that if "xl create" fails, it will not try to
destroy the domain "-1". Previously you'd see this message:
libxl: error: libxl.c:697:libxl_domain_destroy non-existant domain -1
whose "-1" many readers may have thought was an error code, but which
is actually supposedly a domain id.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Ian Jackson <Ian.Jackson@eu.citrix.com>
date Fri Jan 28 18:36:54 2011 +0000 (2011-01-28)
parents c5a7a40cc4f4
children 5eaf9405ed51
files tools/libxl/libxl.h tools/libxl/libxl_create.c tools/libxl/libxl_dm.c tools/libxl/xl_cmdimpl.c
line diff
     1.1 --- a/tools/libxl/libxl.h	Fri Jan 28 18:34:52 2011 +0000
     1.2 +++ b/tools/libxl/libxl.h	Fri Jan 28 18:36:54 2011 +0000
     1.3 @@ -554,6 +554,13 @@ int libxl_cpupool_cpuremove(libxl_ctx *c
     1.4  int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus);
     1.5  int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid);
     1.6  
     1.7 +static inline int libxl_domid_valid_guest(uint32_t domid)
     1.8 +{
     1.9 +    /* returns 1 if the value _could_ be a valid guest domid, 0 otherwise
    1.10 +     * does not check whether the domain actually exists */
    1.11 +    return domid > 0 && domid < DOMID_FIRST_RESERVED;
    1.12 +}
    1.13 +
    1.14  /* common paths */
    1.15  const char *libxl_sbindir_path(void);
    1.16  const char *libxl_bindir_path(void);
     2.1 --- a/tools/libxl/libxl_create.c	Fri Jan 28 18:34:52 2011 +0000
     2.2 +++ b/tools/libxl/libxl_create.c	Fri Jan 28 18:36:54 2011 +0000
     2.3 @@ -25,6 +25,7 @@
     2.4  #include <xenctrl.h>
     2.5  #include <xc_dom.h>
     2.6  #include <xenguest.h>
     2.7 +#include <assert.h>
     2.8  #include "libxl.h"
     2.9  #include "libxl_utils.h"
    2.10  #include "libxl_internal.h"
    2.11 @@ -283,6 +284,8 @@ out:
    2.12  
    2.13  int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
    2.14                         uint32_t *domid)
    2.15 + /* on entry, libxl_domid_valid_guest(domid) must be false;
    2.16 +  * on exit (even error exit), domid may be valid and refer to a domain */
    2.17  {
    2.18      libxl__gc gc = LIBXL_INIT_GC(ctx); /* fixme: should be done by caller */
    2.19      int flags, ret, i, rc;
    2.20 @@ -296,6 +299,8 @@ int libxl__domain_make(libxl_ctx *ctx, l
    2.21      xs_transaction_t t = 0;
    2.22      xen_domain_handle_t handle;
    2.23  
    2.24 +    assert(!libxl_domid_valid_guest(*domid));
    2.25 +
    2.26      uuid_string = libxl__uuid2string(&gc, info->uuid);
    2.27      if (!uuid_string) {
    2.28          rc = ERROR_NOMEM;
     3.1 --- a/tools/libxl/libxl_dm.c	Fri Jan 28 18:34:52 2011 +0000
     3.2 +++ b/tools/libxl/libxl_dm.c	Fri Jan 28 18:36:54 2011 +0000
     3.3 @@ -473,6 +473,8 @@ static int libxl_create_stubdom(libxl_ct
     3.4      b_info.u.pv.features = "";
     3.5      b_info.hvm = 0;
     3.6  
     3.7 +    /* fixme: this function can leak the stubdom if it fails */
     3.8 +
     3.9      ret = libxl__domain_make(ctx, &c_info, &domid);
    3.10      if (ret)
    3.11          goto out_free;
     4.1 --- a/tools/libxl/xl_cmdimpl.c	Fri Jan 28 18:34:52 2011 +0000
     4.2 +++ b/tools/libxl/xl_cmdimpl.c	Fri Jan 28 18:36:54 2011 +0000
     4.3 @@ -1649,7 +1649,7 @@ start:
     4.4  
     4.5  error_out:
     4.6      release_lock();
     4.7 -    if (domid > 0)
     4.8 +    if (libxl_domid_valid_guest(domid))
     4.9          libxl_domain_destroy(&ctx, domid, 0);
    4.10  
    4.11  out: