]> xenbits.xensource.com Git - libvirt.git/commitdiff
libxl: Use g_strdup_printf() instead of virAsprintf()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 22 Oct 2019 13:26:14 +0000 (15:26 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 12 Nov 2019 15:15:58 +0000 (16:15 +0100)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libxl/libxl_conf.c
src/libxl/libxl_domain.c
src/libxl/libxl_driver.c
src/libxl/libxl_logger.c
src/libxl/libxl_migration.c
src/libxl/xen_common.c
src/libxl/xen_xl.c
src/libxl/xen_xm.c

index 2583023cd535220b30c8586847a3a76b5e898e08..920f228d6af37c22c9a8a9e98a9589df0583f844 100644 (file)
@@ -212,8 +212,7 @@ libxlMakeChrdevStr(virDomainChrDefPtr def, char **buf)
 
     case VIR_DOMAIN_CHR_TYPE_FILE:
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        if (virAsprintf(buf, "%s:%s", type, srcdef->data.file.path) < 0)
-            return -1;
+        *buf = g_strdup_printf("%s:%s", type, srcdef->data.file.path);
         break;
 
     case VIR_DOMAIN_CHR_TYPE_DEV:
@@ -232,12 +231,8 @@ libxlMakeChrdevStr(virDomainChrDefPtr def, char **buf)
         if (bindService == NULL)
             bindService = "0";
 
-        if (virAsprintf(buf, "udp:%s:%s@%s:%s",
-                        connectHost,
-                        srcdef->data.udp.connectService,
-                        bindHost,
-                        bindService) < 0)
-            return -1;
+        *buf = g_strdup_printf("udp:%s:%s@%s:%s", connectHost,
+                               srcdef->data.udp.connectService, bindHost, bindService);
         break;
     }
 
@@ -249,20 +244,15 @@ libxlMakeChrdevStr(virDomainChrDefPtr def, char **buf)
         else
             prefix = "tcp";
 
-        if (virAsprintf(buf, "%s:%s:%s%s",
-                        prefix,
-                        srcdef->data.tcp.host,
-                        srcdef->data.tcp.service,
-                        srcdef->data.tcp.listen ? ",server,nowait" : "") < 0)
-            return -1;
+        *buf = g_strdup_printf("%s:%s:%s%s", prefix, srcdef->data.tcp.host,
+                               srcdef->data.tcp.service,
+                               srcdef->data.tcp.listen ? ",server,nowait" : "");
         break;
     }
 
     case VIR_DOMAIN_CHR_TYPE_UNIX:
-        if (virAsprintf(buf, "unix:%s%s",
-                        srcdef->data.nix.path,
-                        srcdef->data.nix.listen ? ",server,nowait" : "") < 0)
-            return -1;
+        *buf = g_strdup_printf("unix:%s%s", srcdef->data.nix.path,
+                               srcdef->data.nix.listen ? ",server,nowait" : "");
         break;
 
     default:
@@ -1932,11 +1922,12 @@ libxlPrepareChannel(virDomainChrDefPtr channel,
     if (channel->targetType == VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_XEN &&
         channel->source->type == VIR_DOMAIN_CHR_TYPE_UNIX &&
         !channel->source->data.nix.path) {
-        if (virAsprintf(&channel->source->data.nix.path,
-                        "%s/%s-%s", channelDir, domainName,
-                        channel->target.name ? channel->target.name
-                        : "unknown.sock") < 0)
-            return -1;
+        const char *target = channel->target.name;
+        if (!target)
+            target = "unknown.sock";
+        channel->source->data.nix.path = g_strdup_printf("%s/%s-%s", channelDir,
+                                                         domainName,
+                                                         target);
 
         channel->source->data.nix.listen = true;
     }
index d79c3c1ed7c1b0bbbcd5fcd02480b388654a6d13..514dbe21ac23687215da798281348923c004b397 100644 (file)
@@ -714,7 +714,7 @@ libxlDomainManagedSavePath(libxlDriverPrivatePtr driver, virDomainObjPtr vm)
     char *ret;
     g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver);
 
-    ignore_value(virAsprintf(&ret, "%s/%s.save", cfg->saveDir, vm->def->name));
+    ret = g_strdup_printf("%s/%s.save", cfg->saveDir, vm->def->name);
     return ret;
 }
 
@@ -905,11 +905,11 @@ libxlDomainCleanup(libxlDriverPrivatePtr driver,
         }
     }
 
-    if (virAsprintf(&file, "%s/%s.xml", cfg->stateDir, vm->def->name) > 0) {
-        if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR)
-            VIR_DEBUG("Failed to remove domain XML for %s", vm->def->name);
-        VIR_FREE(file);
-    }
+    file = g_strdup_printf("%s/%s.xml", cfg->stateDir, vm->def->name);
+
+    if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR)
+        VIR_DEBUG("Failed to remove domain XML for %s", vm->def->name);
+    VIR_FREE(file);
 
     /* The "release" hook cleans up additional resources */
     if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
@@ -940,28 +940,20 @@ libxlDomainAutoCoreDump(libxlDriverPrivatePtr driver,
     char timestr[100];
     struct tm time_info;
     char *dumpfile = NULL;
-    int ret = -1;
 
     localtime_r(&curtime, &time_info);
     strftime(timestr, sizeof(timestr), "%Y-%m-%d-%H:%M:%S", &time_info);
 
-    if (virAsprintf(&dumpfile, "%s/%s-%s",
-                    cfg->autoDumpDir,
-                    vm->def->name,
-                    timestr) < 0)
-        goto cleanup;
+    dumpfile = g_strdup_printf("%s/%s-%s", cfg->autoDumpDir, vm->def->name,
+                               timestr);
 
     /* Unlock virDomainObj while dumping core */
     virObjectUnlock(vm);
     libxl_domain_core_dump(cfg->ctx, vm->def->id, dumpfile, NULL);
     virObjectLock(vm);
 
-    ret = 0;
-
- cleanup:
     VIR_FREE(dumpfile);
-
-    return ret;
+    return 0;
 }
 
 int
@@ -1130,7 +1122,7 @@ libxlConsoleCallback(libxl_ctx *ctx, libxl_event *ev, void *for_callback)
     for (i = 0; i < vm->def->nserials; i++) {
         chr = vm->def->serials[i];
 
-        ignore_value(virAsprintf(&chr->info.alias, "serial%zd", i));
+        chr->info.alias = g_strdup_printf("serial%zd", i);
         if (chr->source->type == VIR_DOMAIN_CHR_TYPE_PTY) {
             if (chr->source->data.file.path)
                 continue;
@@ -1170,9 +1162,8 @@ libxlDomainCreateIfaceNames(virDomainDefPtr def, libxl_domain_config *d_config)
         if (net->ifname)
             continue;
 
-        ignore_value(virAsprintf(&net->ifname,
-                                 LIBXL_GENERATED_PREFIX_XEN "%d.%d%s",
-                                 def->id, x_nic->devid, suffix));
+        net->ifname = g_strdup_printf(LIBXL_GENERATED_PREFIX_XEN "%d.%d%s",
+                                      def->id, x_nic->devid, suffix);
     }
 }
 
index 31c0c8ea83995ecb22d929bd025ed793a368685f..47284a6b6cc30d46a7636b70b30a2e58eb5912e1 100644 (file)
@@ -693,8 +693,7 @@ libxlStateInitialize(bool privileged,
     if (!(cfg = libxlDriverConfigNew()))
         goto error;
 
-    if (virAsprintf(&driverConf, "%s/libxl.conf", cfg->configBaseDir) < 0)
-        goto error;
+    driverConf = g_strdup_printf("%s/libxl.conf", cfg->configBaseDir);
 
     if (libxlDriverConfigLoadFile(cfg, driverConf) < 0)
         goto error;
@@ -5380,16 +5379,13 @@ libxlDiskSectorSize(int domid, int devno)
     }
 
     path = val = NULL;
-    if (virAsprintf(&path, "/local/domain/%d/device/vbd/%d/backend",
-                    domid, devno) < 0)
-        goto cleanup;
+    path = g_strdup_printf("/local/domain/%d/device/vbd/%d/backend", domid, devno);
 
     if ((val = xs_read(handle, XBT_NULL, path, &len)) == NULL)
         goto cleanup;
 
     VIR_FREE(path);
-    if (virAsprintf(&path, "%s/physical-sector-size", val) < 0)
-        goto cleanup;
+    path = g_strdup_printf("%s/physical-sector-size", val);
 
     VIR_FREE(val);
     if ((val = xs_read(handle, XBT_NULL, path, &len)) == NULL)
@@ -5427,9 +5423,8 @@ libxlDomainBlockStatsVBD(virDomainObjPtr vm,
 
     stats->backend = g_strdup("vbd");
 
-    if (virAsprintf(&path, "/sys/bus/xen-backend/devices/vbd-%d-%d/statistics",
-                    vm->def->id, devno) < 0)
-        return ret;
+    path = g_strdup_printf("/sys/bus/xen-backend/devices/vbd-%d-%d/statistics",
+                           vm->def->id, devno);
 
     if (!virFileExists(path)) {
         virReportError(VIR_ERR_OPERATION_FAILED,
@@ -5438,8 +5433,8 @@ libxlDomainBlockStatsVBD(virDomainObjPtr vm,
     }
 
 # define LIBXL_SET_VBDSTAT(FIELD, VAR, MUL) \
-    if ((virAsprintf(&name, "%s/"FIELD, path) < 0) || \
-        (virFileReadAll(name, 256, &val) < 0) || \
+    name = g_strdup_printf("%s/"FIELD, path); \
+    if ((virFileReadAll(name, 256, &val) < 0) || \
         (sscanf(val, "%llu", &status) != 1)) { \
         virReportError(VIR_ERR_OPERATION_FAILED, \
                        _("cannot read %s"), name); \
index 87ad793fb524deb0ca774c66bfd4dda1dcda5005..ffbc722f2c497c9b6097230b5adf8e4486ff17b4 100644 (file)
@@ -158,8 +158,7 @@ libxlLoggerNew(const char *logDir, virLogPriority minLevel)
     if ((logger.files = virHashCreate(3, libxlLoggerFileFree)) == NULL)
         return NULL;
 
-    if (virAsprintf(&path, "%s/libxl-driver.log", logDir) < 0)
-        goto error;
+    path = g_strdup_printf("%s/libxl-driver.log", logDir);
 
     if ((logger.defaultLogFile = fopen(path, "a")) == NULL)
         goto error;
@@ -196,9 +195,8 @@ libxlLoggerOpenFile(libxlLoggerPtr logger,
     char *domidstr = NULL;
     char ebuf[1024];
 
-    if (virAsprintf(&path, "%s/%s.log", logger->logDir, name) < 0 ||
-        virAsprintf(&domidstr, "%d", id) < 0)
-        goto cleanup;
+    path = g_strdup_printf("%s/%s.log", logger->logDir, name);
+    domidstr = g_strdup_printf("%d", id);
 
     if (!(logFile = fopen(path, "a"))) {
         VIR_WARN("Failed to open log file %s: %s",
@@ -222,8 +220,7 @@ void
 libxlLoggerCloseFile(libxlLoggerPtr logger, int id)
 {
     char *domidstr = NULL;
-    if (virAsprintf(&domidstr, "%d", id) < 0)
-        return;
+    domidstr = g_strdup_printf("%d", id);
 
     ignore_value(virHashRemoveEntry(logger->files, domidstr));
 
index 459880512fcad49732d53deec3ca61f1245c361d..b223916647707d9950221c5785ac7a1e2724ec75 100644 (file)
@@ -706,14 +706,12 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn,
             goto endjob;
 
         priv->migrationPort = port;
-        if (virAsprintf(uri_out, "tcp://%s:%d", hostname, port) < 0)
-            goto endjob;
+        *uri_out = g_strdup_printf("tcp://%s:%d", hostname, port);
     } else {
         if (!(STRPREFIX(uri_in, "tcp://"))) {
             /* not full URI, add prefix tcp:// */
             char *tmp;
-            if (virAsprintf(&tmp, "tcp://%s", uri_in) < 0)
-                goto endjob;
+            tmp = g_strdup_printf("tcp://%s", uri_in);
             uri = virURIParse(tmp);
             VIR_FREE(tmp);
         } else {
@@ -744,8 +742,7 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn,
             port = uri->port;
         }
 
-        if (virAsprintf(uri_out, "tcp://%s:%d", hostname, port) < 0)
-            goto endjob;
+        *uri_out = g_strdup_printf("tcp://%s:%d", hostname, port);
     }
 
     snprintf(portstr, sizeof(portstr), "%d", port);
index 28b1bcf7c7a3d97034601e51c24707b0042f6e23..c31a5c952e789e561eaed4d4b208aff81ecf6916 100644 (file)
@@ -1830,12 +1830,11 @@ xenFormatPCI(virConfPtr conf, virDomainDefPtr def)
             virConfValuePtr val, tmp;
             char *buf;
 
-            if (virAsprintf(&buf, "%04x:%02x:%02x.%x",
-                            def->hostdevs[i]->source.subsys.u.pci.addr.domain,
-                            def->hostdevs[i]->source.subsys.u.pci.addr.bus,
-                            def->hostdevs[i]->source.subsys.u.pci.addr.slot,
-                            def->hostdevs[i]->source.subsys.u.pci.addr.function) < 0)
-                goto error;
+            buf = g_strdup_printf("%04x:%02x:%02x.%x",
+                                  def->hostdevs[i]->source.subsys.u.pci.addr.domain,
+                                  def->hostdevs[i]->source.subsys.u.pci.addr.bus,
+                                  def->hostdevs[i]->source.subsys.u.pci.addr.slot,
+                                  def->hostdevs[i]->source.subsys.u.pci.addr.function);
 
             if (VIR_ALLOC(val) < 0) {
                 VIR_FREE(buf);
index 4afa5204659e8c668d2801bf98d0496ea0c6f458..396adf6dac140ba0064cad55569f33e0d9653f85 100644 (file)
@@ -83,11 +83,9 @@ static int xenParseCmdline(virConfPtr conf, char **r_cmdline)
             VIR_WARN("ignoring root= and extra= in favour of cmdline=");
     } else {
         if (root && extra) {
-            if (virAsprintf(&cmdline, "root=%s %s", root, extra) < 0)
-                return -1;
+            cmdline = g_strdup_printf("root=%s %s", root, extra);
         } else if (root) {
-            if (virAsprintf(&cmdline, "root=%s", root) < 0)
-                return -1;
+            cmdline = g_strdup_printf("root=%s", root);
         } else if (extra) {
             cmdline = g_strdup(extra);
         }
@@ -1396,10 +1394,7 @@ xenFormatXLCPUID(virConfPtr conf, virDomainDefPtr def)
                 policy = "0";
                 break;
         }
-        if (virAsprintf(&cpuid_pairs[j++], "%s=%s",
-                        feature_name,
-                        policy) < 0)
-            goto cleanup;
+        cpuid_pairs[j++] = g_strdup_printf("%s=%s", feature_name, policy);
     }
     cpuid_pairs[j] = NULL;
 
@@ -2064,10 +2059,9 @@ xenFormatXLUSB(virConfPtr conf,
             virConfValuePtr val, tmp;
             char *buf;
 
-            if (virAsprintf(&buf, "hostbus=%x,hostaddr=%x",
-                            def->hostdevs[i]->source.subsys.u.usb.bus,
-                            def->hostdevs[i]->source.subsys.u.usb.device) < 0)
-                goto error;
+            buf = g_strdup_printf("hostbus=%x,hostaddr=%x",
+                                  def->hostdevs[i]->source.subsys.u.usb.bus,
+                                  def->hostdevs[i]->source.subsys.u.usb.device);
 
             if (VIR_ALLOC(val) < 0) {
                 VIR_FREE(buf);
index c039614079f8feeebe27812fc2198f2e0cbe5f16..88cf9ac5b0270ad3bb0dec3213fa4012f0b2fb61 100644 (file)
@@ -90,11 +90,9 @@ xenParseXMOS(virConfPtr conf, virDomainDefPtr def)
             return -1;
 
         if (root && extra) {
-            if (virAsprintf(&def->os.cmdline, "root=%s %s", root, extra) < 0)
-                return -1;
+            def->os.cmdline = g_strdup_printf("root=%s %s", root, extra);
         } else if (root) {
-            if (virAsprintf(&def->os.cmdline, "root=%s", root) < 0)
-                return -1;
+            def->os.cmdline = g_strdup_printf("root=%s", root);
         } else if (extra) {
             def->os.cmdline = g_strdup(extra);
         }