debuggers.hg

changeset 21191:be6e58cc2475

xl: Remove some duplicated boilerplate. (Improves logging slightly.)

We remove six lines of boilerplate from the top of each function, and
instead have a single struct libxl_ctx which is initialised once at
the top of main.

Likewise we wrap domain_qualifier_to_domid in a new function
find_domain, which does the error handling, and stores the domid and
the specified name (if applicable).

This reduces the size of xl.c by 7% (!)

As a beneficial side effect, the earlier call to libxl_ctx_set_log in
main makes some lost messages appear.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 12 17:44:47 2010 +0100 (2010-04-12)
parents 5d96ac36d97e
children 117c79b7066d
files tools/libxl/libxl_utils.c tools/libxl/libxl_utils.h tools/libxl/xl.c
line diff
     1.1 --- a/tools/libxl/libxl_utils.c	Mon Apr 12 17:44:01 2010 +0100
     1.2 +++ b/tools/libxl/libxl_utils.c	Mon Apr 12 17:44:47 2010 +0100
     1.3 @@ -55,7 +55,8 @@ char *libxl_domid_to_name(struct libxl_c
     1.4      return s;
     1.5  }
     1.6  
     1.7 -int libxl_name_to_domid(struct libxl_ctx *ctx, char *name, uint32_t *domid)
     1.8 +int libxl_name_to_domid(struct libxl_ctx *ctx, const char *name,
     1.9 +                        uint32_t *domid)
    1.10  {
    1.11      int i, nb_domains;
    1.12      char *domname;
     2.1 --- a/tools/libxl/libxl_utils.h	Mon Apr 12 17:44:01 2010 +0100
     2.2 +++ b/tools/libxl/libxl_utils.h	Mon Apr 12 17:44:47 2010 +0100
     2.3 @@ -19,7 +19,7 @@
     2.4  #include "libxl.h"
     2.5  
     2.6  unsigned long libxl_get_required_shadow_memory(unsigned long maxmem_kb, unsigned int smp_cpus);
     2.7 -int libxl_name_to_domid(struct libxl_ctx *ctx, char *name, uint32_t *domid);
     2.8 +int libxl_name_to_domid(struct libxl_ctx *ctx, const char *name, uint32_t *domid);
     2.9  char *libxl_domid_to_name(struct libxl_ctx *ctx, uint32_t domid);
    2.10  int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid);
    2.11  int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_domid);
     3.1 --- a/tools/libxl/xl.c	Mon Apr 12 17:44:01 2010 +0100
     3.2 +++ b/tools/libxl/xl.c	Mon Apr 12 17:44:47 2010 +0100
     3.3 @@ -37,7 +37,14 @@
     3.4  
     3.5  #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
     3.6  
     3.7 -int logfile = 2;
     3.8 +static int logfile = 2;
     3.9 +
    3.10 +/* every libxl action in xl uses this same libxl context */
    3.11 +static struct libxl_ctx ctx;
    3.12 +
    3.13 +/* when we operate on a domain, it is this one: */
    3.14 +static uint32_t domid;
    3.15 +static const char *common_domname;
    3.16  
    3.17  void log_callback(void *userdata, int loglevel, const char *file, int line, const char *func, char *s)
    3.18  {
    3.19 @@ -47,7 +54,8 @@ void log_callback(void *userdata, int lo
    3.20      write(logfile, str, strlen(str));
    3.21  }
    3.22  
    3.23 -static int domain_qualifier_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid)
    3.24 +static int domain_qualifier_to_domid(const char *p, uint32_t *domid_r,
    3.25 +                                     int *was_name_r)
    3.26  {
    3.27      int i, alldigit;
    3.28  
    3.29 @@ -60,12 +68,26 @@ static int domain_qualifier_to_domid(str
    3.30      }
    3.31  
    3.32      if (i > 0 && alldigit) {
    3.33 -        *domid = strtoul(p, NULL, 10);
    3.34 +        *domid_r = strtoul(p, NULL, 10);
    3.35 +        if (was_name_r) *was_name_r = 0;
    3.36          return 0;
    3.37      } else {
    3.38          /* check here if it's a uuid and do proper conversion */
    3.39      }
    3.40 -    return libxl_name_to_domid(ctx, p, domid);
    3.41 +    if (was_name_r) *was_name_r = 1;
    3.42 +    return libxl_name_to_domid(&ctx, p, domid_r);
    3.43 +}
    3.44 +
    3.45 +static void find_domain(const char *p)
    3.46 +{
    3.47 +    int rc, was_name;
    3.48 +
    3.49 +    rc = domain_qualifier_to_domid(p, &domid, &was_name);
    3.50 +    if (rc) {
    3.51 +        fprintf(stderr, "%s is an invalid domain identifier (rc=%d)\n", p, rc);
    3.52 +        exit(2);
    3.53 +    }
    3.54 +    common_domname = was_name ? p : 0;
    3.55  }
    3.56  
    3.57  #define LOG(_f, _a...)   dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a)
    3.58 @@ -681,8 +703,6 @@ skip_pci:
    3.59  
    3.60  static void create_domain(int debug, int daemonize, const char *config_file, const char *restore_file, int paused)
    3.61  {
    3.62 -    struct libxl_ctx ctx;
    3.63 -    uint32_t domid;
    3.64      libxl_domain_create_info info1;
    3.65      libxl_domain_build_info info2;
    3.66      libxl_domain_build_state state;
    3.67 @@ -709,13 +729,6 @@ static void create_domain(int debug, int
    3.68  start:
    3.69      domid = 0;
    3.70  
    3.71 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
    3.72 -        fprintf(stderr, "cannot init xl context\n");
    3.73 -        return;
    3.74 -    }
    3.75 -
    3.76 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
    3.77 -
    3.78      ret = libxl_domain_make(&ctx, &info1, &domid);
    3.79      if (ret) {
    3.80          fprintf(stderr, "cannot make domain: %d\n", ret);
    3.81 @@ -834,7 +847,6 @@ start:
    3.82                              libxl_free_waiter(w2);
    3.83                              free(w1);
    3.84                              free(w2);
    3.85 -                            libxl_ctx_free(&ctx);
    3.86                              LOG("Done. Rebooting now");
    3.87                              goto start;
    3.88                          }
    3.89 @@ -954,21 +966,11 @@ static void help(char *command)
    3.90  
    3.91  void set_memory_target(char *p, char *mem)
    3.92  {
    3.93 -    struct libxl_ctx ctx;
    3.94 -    uint32_t domid;
    3.95 +    char *endptr;
    3.96      uint32_t memorykb;
    3.97 -    char *endptr;
    3.98  
    3.99 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.100 -        fprintf(stderr, "cannot init xl context\n");
   3.101 -        return;
   3.102 -    }
   3.103 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.104 +    find_domain(p);
   3.105  
   3.106 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.107 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.108 -        exit(2);
   3.109 -    }
   3.110      memorykb = strtoul(mem, &endptr, 10);
   3.111      if (*endptr != '\0') {
   3.112          fprintf(stderr, "invalid memory size: %s\n", mem);
   3.113 @@ -1007,39 +1009,16 @@ int main_memset(int argc, char **argv)
   3.114  
   3.115  void console(char *p, int cons_num)
   3.116  {
   3.117 -    struct libxl_ctx ctx;
   3.118 -    uint32_t domid;
   3.119 -
   3.120 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.121 -        fprintf(stderr, "cannot init xl context\n");
   3.122 -        return;
   3.123 -    }
   3.124 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.125 -
   3.126 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.127 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.128 -        exit(2);
   3.129 -    }
   3.130 +    find_domain(p);
   3.131      libxl_console_attach(&ctx, domid, cons_num);
   3.132  }
   3.133  
   3.134  void cd_insert(char *dom, char *virtdev, char *phys)
   3.135  {
   3.136 -    struct libxl_ctx ctx;
   3.137 -    uint32_t domid;
   3.138      libxl_device_disk disk;
   3.139      char *p;
   3.140  
   3.141 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.142 -        fprintf(stderr, "cannot init xl context\n");
   3.143 -        return;
   3.144 -    }
   3.145 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.146 -
   3.147 -    if (domain_qualifier_to_domid(&ctx, dom, &domid) < 0) {
   3.148 -        fprintf(stderr, "%s is an invalid domain identifier\n", dom);
   3.149 -        exit(2);
   3.150 -    }
   3.151 +    find_domain(dom);
   3.152  
   3.153      disk.backend_domid = 0;
   3.154      disk.domid = domid;
   3.155 @@ -1161,21 +1140,11 @@ int main_console(int argc, char **argv)
   3.156  
   3.157  void pcilist(char *dom)
   3.158  {
   3.159 -    struct libxl_ctx ctx;
   3.160 -    uint32_t domid;
   3.161      libxl_device_pci *pcidevs;
   3.162      int num, i;
   3.163  
   3.164 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.165 -        fprintf(stderr, "cannot init xl context\n");
   3.166 -        return;
   3.167 -    }
   3.168 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.169 +    find_domain(dom);
   3.170  
   3.171 -    if (domain_qualifier_to_domid(&ctx, dom, &domid) < 0) {
   3.172 -        fprintf(stderr, "%s is an invalid domain identifier\n", dom);
   3.173 -        exit(2);
   3.174 -    }
   3.175      pcidevs = libxl_device_pci_list(&ctx, domid, &num);
   3.176      if (!num)
   3.177          return;
   3.178 @@ -1214,21 +1183,11 @@ int main_pcilist(int argc, char **argv)
   3.179  
   3.180  void pcidetach(char *dom, char *bdf)
   3.181  {
   3.182 -    struct libxl_ctx ctx;
   3.183 -    uint32_t domid;
   3.184      libxl_device_pci pcidev;
   3.185      unsigned int domain, bus, dev, func;
   3.186  
   3.187 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.188 -        fprintf(stderr, "cannot init xl context\n");
   3.189 -        return;
   3.190 -    }
   3.191 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.192 +    find_domain(dom);
   3.193  
   3.194 -    if (domain_qualifier_to_domid(&ctx, dom, &domid) < 0) {
   3.195 -        fprintf(stderr, "%s is an invalid domain identifier\n", dom);
   3.196 -        exit(2);
   3.197 -    }
   3.198      memset(&pcidev, 0x00, sizeof(pcidev));
   3.199      sscanf(bdf, PCI_BDF, &domain, &bus, &dev, &func);
   3.200      libxl_device_pci_init(&pcidev, domain, bus, dev, func, 0);
   3.201 @@ -1263,21 +1222,11 @@ int main_pcidetach(int argc, char **argv
   3.202  }
   3.203  void pciattach(char *dom, char *bdf, char *vs)
   3.204  {
   3.205 -    struct libxl_ctx ctx;
   3.206 -    uint32_t domid;
   3.207      libxl_device_pci pcidev;
   3.208      unsigned int domain, bus, dev, func;
   3.209  
   3.210 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.211 -        fprintf(stderr, "cannot init xl context\n");
   3.212 -        return;
   3.213 -    }
   3.214 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.215 +    find_domain(dom);
   3.216  
   3.217 -    if (domain_qualifier_to_domid(&ctx, dom, &domid) < 0) {
   3.218 -        fprintf(stderr, "%s is an invalid domain identifier\n", dom);
   3.219 -        exit(2);
   3.220 -    }
   3.221      memset(&pcidev, 0x00, sizeof(pcidev));
   3.222      sscanf(bdf, PCI_BDF, &domain, &bus, &dev, &func);
   3.223      libxl_device_pci_init(&pcidev, domain, bus, dev, func, 0);
   3.224 @@ -1316,70 +1265,27 @@ int main_pciattach(int argc, char **argv
   3.225  
   3.226  void pause_domain(char *p)
   3.227  {
   3.228 -    struct libxl_ctx ctx;
   3.229 -    uint32_t domid;
   3.230 -
   3.231 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.232 -        fprintf(stderr, "cannot init xl context\n");
   3.233 -        return;
   3.234 -    }
   3.235 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.236 -
   3.237 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.238 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.239 -        exit(2);
   3.240 -    }
   3.241 +    find_domain(p);
   3.242      libxl_domain_pause(&ctx, domid);
   3.243  }
   3.244  
   3.245  void unpause_domain(char *p)
   3.246  {
   3.247 -    struct libxl_ctx ctx;
   3.248 -    uint32_t domid;
   3.249 -
   3.250 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.251 -        fprintf(stderr, "cannot init xl context\n");
   3.252 -        return;
   3.253 -    }
   3.254 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.255 -
   3.256 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.257 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.258 -        exit(2);
   3.259 -    }
   3.260 +    find_domain(p);
   3.261      libxl_domain_unpause(&ctx, domid);
   3.262  }
   3.263  
   3.264  void destroy_domain(char *p)
   3.265  {
   3.266 -    struct libxl_ctx ctx;
   3.267 -    uint32_t domid;
   3.268 -
   3.269 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.270 -        fprintf(stderr, "cannot init xl context\n");
   3.271 -        return;
   3.272 -    }
   3.273 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.274 -
   3.275 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.276 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.277 -        exit(2);
   3.278 -    }
   3.279 +    find_domain(p);
   3.280      libxl_domain_destroy(&ctx, domid, 0);
   3.281  }
   3.282  
   3.283  void list_domains(int verbose)
   3.284  {
   3.285 -    struct libxl_ctx ctx;
   3.286      struct libxl_dominfo *info;
   3.287      int nb_domain, i;
   3.288  
   3.289 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.290 -        fprintf(stderr, "cannot init xl context\n");
   3.291 -        return;
   3.292 -    }
   3.293 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.294 -
   3.295      info = libxl_list_domain(&ctx, &nb_domain);
   3.296  
   3.297      if (info < 0) {
   3.298 @@ -1408,16 +1314,9 @@ void list_domains(int verbose)
   3.299  
   3.300  void list_vm(void)
   3.301  {
   3.302 -    struct libxl_ctx ctx;
   3.303      struct libxl_vminfo *info;
   3.304      int nb_vm, i;
   3.305  
   3.306 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.307 -        fprintf(stderr, "cannot init xl context\n");
   3.308 -        return;
   3.309 -    }
   3.310 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.311 -
   3.312      info = libxl_list_vm(&ctx, &nb_vm);
   3.313  
   3.314      if (info < 0) {
   3.315 @@ -1438,20 +1337,9 @@ void list_vm(void)
   3.316  
   3.317  int save_domain(char *p, char *filename, int checkpoint)
   3.318  {
   3.319 -    struct libxl_ctx ctx;
   3.320 -    uint32_t domid;
   3.321      int fd;
   3.322  
   3.323 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.324 -        fprintf(stderr, "cannot init xl context\n");
   3.325 -        exit(2);
   3.326 -    }
   3.327 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.328 -
   3.329 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.330 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.331 -        exit(2);
   3.332 -    }
   3.333 +    find_domain(p);
   3.334      fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
   3.335      if (fd < 0) {
   3.336          fprintf(stderr, "Failed to open temp file %s for writing\n", filename);
   3.337 @@ -1693,17 +1581,9 @@ int main_create(int argc, char **argv)
   3.338  
   3.339  void button_press(char *p, char *b)
   3.340  {
   3.341 -    struct libxl_ctx ctx;
   3.342 -    uint32_t domid;
   3.343      libxl_button button;
   3.344  
   3.345 -    libxl_ctx_init(&ctx, LIBXL_VERSION);
   3.346 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.347 -
   3.348 -    if (domain_qualifier_to_domid(&ctx, p, &domid) < 0) {
   3.349 -        fprintf(stderr, "%s is an invalid domain identifier\n", p);
   3.350 -        exit(2);
   3.351 -    }
   3.352 +    find_domain(p);
   3.353  
   3.354      if (!strcmp(b, "power")) {
   3.355          button = POWER_BUTTON;
   3.356 @@ -1745,7 +1625,7 @@ int main_button_press(int argc, char **a
   3.357      exit(0);
   3.358  }
   3.359  
   3.360 -static void print_vcpuinfo(struct libxl_ctx *ctx, uint32_t domid,
   3.361 +static void print_vcpuinfo(uint32_t tdomid,
   3.362                             const struct libxl_vcpuinfo *vcpuinfo,
   3.363                             uint32_t nr_cpus)
   3.364  {
   3.365 @@ -1755,7 +1635,7 @@ static void print_vcpuinfo(struct libxl_
   3.366  
   3.367      /*      NAME  ID  VCPU */
   3.368      printf("%-32s %5u %5u",
   3.369 -           libxl_domid_to_name(ctx, domid), domid, vcpuinfo->vcpuid);
   3.370 +           libxl_domid_to_name(&ctx, tdomid), tdomid, vcpuinfo->vcpuid);
   3.371      if (!vcpuinfo->online) {
   3.372          /*      CPU STA */
   3.373          printf("%5c %3c%cp ", '-', '-', '-');
   3.374 @@ -1813,19 +1693,11 @@ static void print_vcpuinfo(struct libxl_
   3.375  
   3.376  void vcpulist(int argc, char **argv)
   3.377  {
   3.378 -    struct libxl_ctx ctx;
   3.379      struct libxl_dominfo *dominfo;
   3.380 -    uint32_t domid;
   3.381      struct libxl_vcpuinfo *vcpuinfo;
   3.382      struct libxl_physinfo physinfo;
   3.383      int nb_vcpu, nb_domain, cpusize;
   3.384  
   3.385 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.386 -        fprintf(stderr, "cannot init xl context\n");
   3.387 -        return;
   3.388 -    }
   3.389 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.390 -
   3.391      if (libxl_get_physinfo(&ctx, &physinfo) != 0) {
   3.392          fprintf(stderr, "libxl_physinfo failed.\n");
   3.393          goto vcpulist_out;
   3.394 @@ -1843,12 +1715,12 @@ void vcpulist(int argc, char **argv)
   3.395                  goto vcpulist_out;
   3.396              }
   3.397              for (; nb_vcpu > 0; --nb_vcpu, ++vcpuinfo) {
   3.398 -                print_vcpuinfo(&ctx, dominfo->domid, vcpuinfo, physinfo.nr_cpus);
   3.399 +                print_vcpuinfo(dominfo->domid, vcpuinfo, physinfo.nr_cpus);
   3.400              }
   3.401          }
   3.402      } else {
   3.403          for (; argc > 0; ++argv, --argc) {
   3.404 -            if (domain_qualifier_to_domid(&ctx, *argv, &domid) < 0) {
   3.405 +            if (domain_qualifier_to_domid(*argv, &domid, 0) < 0) {
   3.406                  fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
   3.407              }
   3.408              if (!(vcpuinfo = libxl_list_vcpu(&ctx, domid, &nb_vcpu, &cpusize))) {
   3.409 @@ -1856,12 +1728,12 @@ void vcpulist(int argc, char **argv)
   3.410                  goto vcpulist_out;
   3.411              }
   3.412              for (; nb_vcpu > 0; --nb_vcpu, ++vcpuinfo) {
   3.413 -                print_vcpuinfo(&ctx, domid, vcpuinfo, physinfo.nr_cpus);
   3.414 +                print_vcpuinfo(domid, vcpuinfo, physinfo.nr_cpus);
   3.415              }
   3.416          }
   3.417      }
   3.418    vcpulist_out:
   3.419 -    libxl_ctx_free(&ctx);
   3.420 +    ;
   3.421  }
   3.422  
   3.423  void main_vcpulist(int argc, char **argv)
   3.424 @@ -1885,12 +1757,11 @@ void main_vcpulist(int argc, char **argv
   3.425  
   3.426  void vcpupin(char *d, const char *vcpu, char *cpu)
   3.427  {
   3.428 -    struct libxl_ctx ctx;
   3.429      struct libxl_vcpuinfo *vcpuinfo;
   3.430      struct libxl_physinfo physinfo;
   3.431      uint64_t *cpumap = NULL;
   3.432  
   3.433 -    uint32_t domid, vcpuid, cpuida, cpuidb;
   3.434 +    uint32_t vcpuid, cpuida, cpuidb;
   3.435      char *endptr, *toka, *tokb;
   3.436      int i, nb_vcpu, cpusize;
   3.437  
   3.438 @@ -1903,16 +1774,8 @@ void vcpupin(char *d, const char *vcpu, 
   3.439          vcpuid = -1;
   3.440      }
   3.441  
   3.442 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.443 -        fprintf(stderr, "cannot init xl context\n");
   3.444 -        return;
   3.445 -    }
   3.446 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.447 +    find_domain(d);
   3.448  
   3.449 -    if (domain_qualifier_to_domid(&ctx, d, &domid) < 0) {
   3.450 -        fprintf(stderr, "%s is an invalid domain identifier\n", d);
   3.451 -        goto vcpupin_out1;
   3.452 -    }
   3.453      if (libxl_get_physinfo(&ctx, &physinfo) != 0) {
   3.454          fprintf(stderr, "libxl_get_physinfo failed.\n");
   3.455          goto vcpupin_out1;
   3.456 @@ -1970,7 +1833,7 @@ void vcpupin(char *d, const char *vcpu, 
   3.457    vcpupin_out1:
   3.458      free(cpumap);
   3.459    vcpupin_out:
   3.460 -    libxl_ctx_free(&ctx);
   3.461 +    ;
   3.462  }
   3.463  
   3.464  int main_vcpupin(int argc, char **argv)
   3.465 @@ -1998,9 +1861,7 @@ int main_vcpupin(int argc, char **argv)
   3.466  
   3.467  void vcpuset(char *d, char* nr_vcpus)
   3.468  {
   3.469 -    struct libxl_ctx ctx;
   3.470      char *endptr;
   3.471 -    uint32_t domid;
   3.472      unsigned int max_vcpus;
   3.473  
   3.474      max_vcpus = strtoul(nr_vcpus, &endptr, 10);
   3.475 @@ -2009,22 +1870,11 @@ void vcpuset(char *d, char* nr_vcpus)
   3.476          return;
   3.477      }
   3.478  
   3.479 -    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.480 -        fprintf(stderr, "cannot init xl context\n");
   3.481 -        return;
   3.482 -    }
   3.483 -    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.484 +    find_domain(d);
   3.485  
   3.486 -    if (domain_qualifier_to_domid(&ctx, d, &domid) < 0) {
   3.487 -        fprintf(stderr, "%s is an invalid domain identifier\n", d);
   3.488 -        goto vcpuset_out;
   3.489 -    }
   3.490      if (libxl_set_vcpucount(&ctx, domid, max_vcpus) == ERROR_INVAL) {
   3.491          fprintf(stderr, "Error: Cannot set vcpus greater than max vcpus on running domain or lesser than 1.\n");
   3.492      }
   3.493 -
   3.494 -  vcpuset_out:
   3.495 -    libxl_ctx_free(&ctx);
   3.496  }
   3.497  
   3.498  int main_vcpuset(int argc, char **argv)
   3.499 @@ -2057,6 +1907,15 @@ int main(int argc, char **argv)
   3.500          exit(1);
   3.501      }
   3.502  
   3.503 +    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.504 +        fprintf(stderr, "cannot init xl context\n");
   3.505 +        exit(1);
   3.506 +    }
   3.507 +    if (libxl_ctx_set_log(&ctx, log_callback, NULL)) {
   3.508 +        fprintf(stderr, "cannot set xl log callback\n");
   3.509 +        exit(-ERROR_FAIL);
   3.510 +    }
   3.511 +
   3.512      srand(time(0));
   3.513  
   3.514      if (!strcmp(argv[1], "create")) {