debuggers.hg

diff tools/libxl/libxl.c @ 20779:9417aa2c7380

libxl: coding styles cleanup

simplify some lines, and keep the xl style consistant with itself.
use libxl_sprintf instead of snprintf/sprintf

Signed-off-by: Vincent Hanquez <vincent.hanquez@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 30 12:41:22 2009 +0000 (2009-12-30)
parents 042f50e7353f
children 1604ad5c529f
line diff
     1.1 --- a/tools/libxl/libxl.c	Wed Dec 30 12:40:44 2009 +0000
     1.2 +++ b/tools/libxl/libxl.c	Wed Dec 30 12:41:22 2009 +0000
     1.3 @@ -45,7 +45,7 @@ int libxl_ctx_init(struct libxl_ctx *ctx
     1.4      ctx->xch = xc_interface_open();
     1.5      ctx->xsh = xs_daemon_open();
     1.6  
     1.7 -    ctx->waitpid_instead= libxl_waitpid_instead_default;
     1.8 +    ctx->waitpid_instead = libxl_waitpid_instead_default;
     1.9      return 0;
    1.10  }
    1.11  
    1.12 @@ -332,26 +332,6 @@ xc_dominfo_t * libxl_domain_infolist(str
    1.13      return info;
    1.14  }
    1.15  
    1.16 -static int libxl_save_device_model(struct libxl_ctx *ctx, uint32_t domid, int fd)
    1.17 -{
    1.18 -    int fd2, c;
    1.19 -    char buf[1024];
    1.20 -    char *filename = libxl_sprintf(ctx, "/var/lib/xen/qemu-save.%d", domid);
    1.21 -
    1.22 -    XL_LOG(ctx, XL_LOG_DEBUG, "Saving device model state to %s", filename);
    1.23 -    libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "save", strlen("save"));
    1.24 -    libxl_wait_for_device_model(ctx, domid, "paused", NULL, NULL);
    1.25 -
    1.26 -    write(fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE));
    1.27 -    fd2 = open(filename, O_RDONLY);
    1.28 -    while ((c = read(fd2, buf, sizeof(buf))) != 0) {
    1.29 -        write(fd, buf, c);
    1.30 -    }
    1.31 -    close(fd2);
    1.32 -    unlink(filename);
    1.33 -    return 0;
    1.34 -}
    1.35 -
    1.36  xc_dominfo_t *libxl_domain_info(struct libxl_ctx *ctx, uint32_t domid)
    1.37  {
    1.38      xc_dominfo_t *info;
    1.39 @@ -377,6 +357,7 @@ xc_dominfo_t *libxl_domain_info(struct l
    1.40  
    1.41      return info;
    1.42  }
    1.43 +
    1.44  int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
    1.45                           uint32_t domid, int fd)
    1.46  {
    1.47 @@ -384,11 +365,9 @@ int libxl_domain_suspend(struct libxl_ct
    1.48      int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
    1.49      int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
    1.50  
    1.51 -
    1.52      core_suspend(ctx, domid, fd, hvm, live, debug);
    1.53      if (hvm)
    1.54 -        libxl_save_device_model(ctx, domid, fd);
    1.55 -
    1.56 +        save_device_model(ctx, domid, fd);
    1.57      return 0;
    1.58  }
    1.59  
    1.60 @@ -400,11 +379,11 @@ int libxl_domain_pause(struct libxl_ctx 
    1.61  
    1.62  int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
    1.63  {
    1.64 -    char path[50];
    1.65 +    char *path;
    1.66      char *state;
    1.67  
    1.68      if (is_hvm(ctx, domid)) {
    1.69 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
    1.70 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
    1.71          state = libxl_xs_read(ctx, XBT_NULL, path);
    1.72          if (state != NULL && !strcmp(state, "paused")) {
    1.73              libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
    1.74 @@ -607,7 +586,7 @@ static int libxl_destroy_device_model(st
    1.75  int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
    1.76  {
    1.77      char *dom_path, *uuid_str;
    1.78 -    char vm_path[UUID_LEN_STR + 5], vss_path[UUID_LEN_STR + 6], xapi_path[20];
    1.79 +    char *vm_path, *vss_path, *xapi_path;
    1.80      xen_uuid_t *uuid;
    1.81      int rc, dm_present;
    1.82  
    1.83 @@ -648,17 +627,21 @@ int libxl_domain_destroy(struct libxl_ct
    1.84          XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
    1.85      if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
    1.86          XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
    1.87 +
    1.88      uuid_str = libxl_uuid_to_string(ctx, uuid);
    1.89 -    snprintf(vm_path, sizeof(vm_path), "/vm/%s", uuid_str);
    1.90 +
    1.91 +    vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_str);
    1.92      if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
    1.93          XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
    1.94 -    snprintf(vss_path, sizeof(vss_path), "/vss/%s", uuid_str);
    1.95 +
    1.96 +    vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_str);
    1.97      if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
    1.98          XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
    1.99 -    libxl_free(ctx, uuid_str);
   1.100 -    snprintf(xapi_path, sizeof(xapi_path), "/xapi/%u", domid);
   1.101 +
   1.102 +    xapi_path = libxl_sprintf(ctx, "/xapi/%u", domid);
   1.103      if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
   1.104          XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
   1.105 +
   1.106      rc = xc_domain_destroy(ctx->xch, domid);
   1.107      if (rc < 0) {
   1.108          XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
   1.109 @@ -1068,7 +1051,8 @@ int libxl_create_device_model(struct lib
   1.110  }
   1.111  
   1.112  int libxl_detach_device_model(struct libxl_ctx *ctx,
   1.113 -                              libxl_device_model_starting *starting) {
   1.114 +                              libxl_device_model_starting *starting)
   1.115 +{
   1.116      int rc;
   1.117      rc = libxl_spawn_detach(ctx, starting->for_spawn);
   1.118      if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
   1.119 @@ -1078,7 +1062,8 @@ int libxl_detach_device_model(struct lib
   1.120  
   1.121  
   1.122  int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
   1.123 -                                       libxl_device_model_starting *starting) {
   1.124 +                                       libxl_device_model_starting *starting)
   1.125 +{
   1.126      int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
   1.127                                                libxl_spawn_check,
   1.128                                                starting->for_spawn);
   1.129 @@ -1176,7 +1161,8 @@ int libxl_device_disk_add(struct libxl_c
   1.130              if (is_blktap2_supported()) {
   1.131                  int rc, c, p[2], tot;
   1.132                  char buf[1024], *dev;
   1.133 -                if ((dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype))) == NULL) {
   1.134 +                dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
   1.135 +                if (dev == NULL) {
   1.136                      if (pipe(p) < 0) {
   1.137                          XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
   1.138                          return -1;
   1.139 @@ -1959,7 +1945,7 @@ retry_transaction2:
   1.140  
   1.141  int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
   1.142  {
   1.143 -    char path[50];
   1.144 +    char *path;
   1.145      char *state, *vdevfn;
   1.146      int rc, hvm;
   1.147      int stubdomid = 0;
   1.148 @@ -1968,7 +1954,8 @@ int libxl_device_pci_add(struct libxl_ct
   1.149  
   1.150      libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
   1.151  
   1.152 -    if ((stubdomid = libxl_get_stubdom_id(ctx, domid)) != 0) {
   1.153 +    stubdomid = libxl_get_stubdom_id(ctx, domid);
   1.154 +    if (stubdomid != 0) {
   1.155          libxl_device_pci pcidev_s = *pcidev;
   1.156          libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
   1.157      }
   1.158 @@ -1978,23 +1965,23 @@ int libxl_device_pci_add(struct libxl_ct
   1.159          if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
   1.160              return -1;
   1.161          }
   1.162 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
   1.163 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
   1.164          state = libxl_xs_read(ctx, XBT_NULL, path);
   1.165 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
   1.166 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
   1.167          if (pcidev->vdevfn)
   1.168              libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
   1.169                             pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
   1.170          else
   1.171              libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
   1.172                             pcidev->bus, pcidev->dev, pcidev->func);
   1.173 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
   1.174 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
   1.175          xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
   1.176          if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
   1.177              XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
   1.178 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
   1.179 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
   1.180          vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
   1.181          sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
   1.182 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
   1.183 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
   1.184          xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
   1.185      } else {
   1.186          char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
   1.187 @@ -2047,7 +2034,8 @@ int libxl_device_pci_add(struct libxl_ct
   1.188      }
   1.189  out:
   1.190      if (!libxl_is_stubdom(ctx, domid)) {
   1.191 -        if ((rc = xc_assign_device(ctx->xch, domid, pcidev->value)) < 0)
   1.192 +        rc = xc_assign_device(ctx->xch, domid, pcidev->value);
   1.193 +        if (rc < 0)
   1.194              XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
   1.195      }
   1.196  
   1.197 @@ -2057,7 +2045,7 @@ out:
   1.198  
   1.199  int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
   1.200  {
   1.201 -    char path[50];
   1.202 +    char *path;
   1.203      char *state;
   1.204      int hvm, rc;
   1.205      int stubdomid = 0;
   1.206 @@ -2070,18 +2058,18 @@ int libxl_device_pci_remove(struct libxl
   1.207          if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
   1.208              return -1;
   1.209          }
   1.210 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
   1.211 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
   1.212          state = libxl_xs_read(ctx, XBT_NULL, path);
   1.213 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
   1.214 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
   1.215          libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
   1.216                         pcidev->bus, pcidev->dev, pcidev->func);
   1.217 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
   1.218 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
   1.219          xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
   1.220          if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
   1.221              XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
   1.222              return -1;
   1.223          }
   1.224 -        snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
   1.225 +        path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
   1.226          xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
   1.227      } else {
   1.228          char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
   1.229 @@ -2137,11 +2125,13 @@ out:
   1.230      libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
   1.231  
   1.232      if (!libxl_is_stubdom(ctx, domid)) {
   1.233 -        if ((rc = xc_deassign_device(ctx->xch, domid, pcidev->value)) < 0)
   1.234 +        rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
   1.235 +        if (rc < 0)
   1.236              XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
   1.237      }
   1.238  
   1.239 -    if ((stubdomid = libxl_get_stubdom_id(ctx, domid)) != 0) {
   1.240 +    stubdomid = libxl_get_stubdom_id(ctx, domid);
   1.241 +    if (stubdomid != 0) {
   1.242          libxl_device_pci pcidev_s = *pcidev;
   1.243          libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
   1.244      }
   1.245 @@ -2163,7 +2153,7 @@ libxl_device_pci *libxl_device_pci_list(
   1.246          return NULL;
   1.247      }
   1.248      n = atoi(num_devs);
   1.249 -    pcidevs = (libxl_device_pci *) calloc(n, sizeof(libxl_device_pci));
   1.250 +    pcidevs = calloc(n, sizeof(libxl_device_pci));
   1.251      *num = n;
   1.252  
   1.253      for (i = 0; i < n; i++) {
   1.254 @@ -2361,7 +2351,9 @@ int libxl_set_memory_target(struct libxl
   1.255      videoram = atoi(videoram_s);
   1.256  
   1.257      libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
   1.258 -    if ((rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT)) != 0) return rc;
   1.259 +    rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT);
   1.260 +    if (rc != 0)
   1.261 +        return rc;
   1.262      rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
   1.263      return rc;
   1.264  }