debuggers.hg

changeset 22887:c5a7a40cc4f4

libxl: fix error handling (xenstore transaction leak) in libxl__domain_make

libxl__domain_make could under some circumstances leak the xenstore
transaction (stored in the variable t). Also, failures to commit the
xenstore transaction for reasons other than EAGAIN would be ignored (!)

Fix this as follows:

* Initialise t to 0 (not a valid transaction id), and when the
transaction is successfully committed or rolled back, reset it.

* Change all the instances of: libxl__free_all(&gc); return error;
to instead do: rc=error; goto out;

* Use the out stanza for exiting, setting rc=0 on success first.

* Explicitly abort the transaction in the out stanza.

Also add a note by the calls manipulating the gc, to note that as this
is an internal function, the gc should really be set up and destroyed
by its caller. But let's not do that at this stage of the 4.1 release
cycle.

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:34:52 2011 +0000 (2011-01-28)
parents 3e928084336f
children ccfa0527893e
files tools/libxl/libxl_create.c
line diff
     1.1 --- a/tools/libxl/libxl_create.c	Fri Jan 28 17:56:00 2011 +0000
     1.2 +++ b/tools/libxl/libxl_create.c	Fri Jan 28 18:34:52 2011 +0000
     1.3 @@ -284,7 +284,7 @@ out:
     1.4  int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
     1.5                         uint32_t *domid)
     1.6  {
     1.7 -    libxl__gc gc = LIBXL_INIT_GC(ctx);
     1.8 +    libxl__gc gc = LIBXL_INIT_GC(ctx); /* fixme: should be done by caller */
     1.9      int flags, ret, i, rc;
    1.10      char *uuid_string;
    1.11      char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
    1.12 @@ -293,13 +293,13 @@ int libxl__domain_make(libxl_ctx *ctx, l
    1.13      char *dom_path, *vm_path;
    1.14      struct xs_permissions roperm[2];
    1.15      struct xs_permissions rwperm[1];
    1.16 -    xs_transaction_t t;
    1.17 +    xs_transaction_t t = 0;
    1.18      xen_domain_handle_t handle;
    1.19  
    1.20      uuid_string = libxl__uuid2string(&gc, info->uuid);
    1.21      if (!uuid_string) {
    1.22 -        libxl__free_all(&gc);
    1.23 -        return ERROR_NOMEM;
    1.24 +        rc = ERROR_NOMEM;
    1.25 +        goto out;
    1.26      }
    1.27  
    1.28      flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
    1.29 @@ -313,28 +313,28 @@ int libxl__domain_make(libxl_ctx *ctx, l
    1.30      ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
    1.31      if (ret < 0) {
    1.32          LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "domain creation fail");
    1.33 -        libxl__free_all(&gc);
    1.34 -        return ERROR_FAIL;
    1.35 +        rc = ERROR_FAIL;
    1.36 +        goto out;
    1.37      }
    1.38  
    1.39      ret = xc_cpupool_movedomain(ctx->xch, info->poolid, *domid);
    1.40      if (ret < 0) {
    1.41          LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "domain move fail");
    1.42 -        libxl__free_all(&gc);
    1.43 -        return ERROR_FAIL;
    1.44 +        rc = ERROR_FAIL;
    1.45 +        goto out;
    1.46      }
    1.47  
    1.48      dom_path = libxl__xs_get_dompath(&gc, *domid);
    1.49      if (!dom_path) {
    1.50 -        libxl__free_all(&gc);
    1.51 -        return ERROR_FAIL;
    1.52 +        rc = ERROR_FAIL;
    1.53 +        goto out;
    1.54      }
    1.55  
    1.56      vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
    1.57      if (!vm_path) {
    1.58          LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot allocate create paths");
    1.59 -        libxl__free_all(&gc);
    1.60 -        return ERROR_FAIL;
    1.61 +        rc = ERROR_FAIL;
    1.62 +        goto out;
    1.63      }
    1.64  
    1.65      roperm[0].id = 0;
    1.66 @@ -356,10 +356,8 @@ retry_transaction:
    1.67  
    1.68      xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, strlen(vm_path));
    1.69      rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
    1.70 -    if (rc) {
    1.71 -        libxl__free_all(&gc);
    1.72 -        return rc;
    1.73 -    }
    1.74 +    if (rc)
    1.75 +        goto out;
    1.76  
    1.77      for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
    1.78          char *path = libxl__sprintf(&gc, "%s/%s", dom_path, rw_paths[i]);
    1.79 @@ -381,12 +379,23 @@ retry_transaction:
    1.80      libxl__xs_writev(&gc, t, libxl__sprintf(&gc, "%s/platform", dom_path), info->platformdata);
    1.81  
    1.82      xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
    1.83 -    if (!xs_transaction_end(ctx->xsh, t, 0))
    1.84 -        if (errno == EAGAIN)
    1.85 +    if (!xs_transaction_end(ctx->xsh, t, 0)) {
    1.86 +        if (errno == EAGAIN) {
    1.87 +            t = 0;
    1.88              goto retry_transaction;
    1.89 +        }
    1.90 +        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "domain creation "
    1.91 +                         "xenstore transaction commit failed");
    1.92 +        rc = ERROR_FAIL;
    1.93 +        goto out;
    1.94 +    }
    1.95 +    t = 0;
    1.96  
    1.97 -    libxl__free_all(&gc);
    1.98 -    return 0;
    1.99 +    rc = 0;
   1.100 + out:
   1.101 +    if (t) xs_transaction_end(ctx->xsh, t, 1);
   1.102 +    libxl__free_all(&gc); /* fixme: should be done by caller */
   1.103 +    return rc;
   1.104  }
   1.105  
   1.106  static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,