All callers basically end up dumping the buffer into a string and then adding '-object' 'props' arguments to virCommand. Simplify all callers by doing this in the function itself. Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx> --- src/qemu/qemu_command.c | 119 +++++++++++----------------------------- 1 file changed, 32 insertions(+), 87 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index a4d439a0ec..ae8234f88f 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -156,10 +156,11 @@ VIR_ENUM_IMPL(qemuAudioDriver, static int -qemuBuildObjectCommandlineFromJSON(virBuffer *buf, +qemuBuildObjectCommandlineFromJSON(virCommand *cmd, virJSONValue *props, virQEMUCaps *qemuCaps) { + g_autofree char *arg = NULL; const char *type = virJSONValueObjectGetString(props, "qom-type"); const char *alias = virJSONValueObjectGetString(props, "id"); @@ -171,13 +172,22 @@ qemuBuildObjectCommandlineFromJSON(virBuffer *buf, } if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_QAPIFIED)) { - return virJSONValueToBuffer(props, buf, false); + if (!(arg = virJSONValueToString(props, false))) + return -1; } else { - virBufferAsprintf(buf, "%s,", type); + g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; - return virQEMUBuildCommandLineJSON(props, buf, "qom-type", - virQEMUBuildCommandLineJSONArrayBitmap); + virBufferAsprintf(&buf, "%s,", type); + + if (virQEMUBuildCommandLineJSON(props, &buf, "qom-type", + virQEMUBuildCommandLineJSONArrayBitmap) < 0) + return -1; + + arg = virBufferContentAndReset(&buf); } + + virCommandAddArgList(cmd, "-object", arg, NULL); + return 0; } @@ -197,7 +207,6 @@ qemuBuildMasterKeyCommandLine(virCommand *cmd, { g_autofree char *alias = NULL; g_autofree char *path = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autoptr(virJSONValue) props = NULL; if (!(alias = qemuDomainGetMasterKeyAlias())) @@ -217,12 +226,9 @@ qemuBuildMasterKeyCommandLine(virCommand *cmd, NULL) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - return 0; } @@ -732,18 +738,14 @@ qemuBuildObjectSecretCommandLine(virCommand *cmd, qemuDomainSecretInfo *secinfo, virQEMUCaps *qemuCaps) { - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autoptr(virJSONValue) props = NULL; if (qemuBuildSecretInfoProps(secinfo, &props) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - return 0; } @@ -803,19 +805,15 @@ qemuBuildTLSx509CommandLine(virCommand *cmd, const char *alias, virQEMUCaps *qemuCaps) { - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autoptr(virJSONValue) props = NULL; if (qemuBuildTLSx509BackendProps(tlspath, isListen, verifypeer, alias, certEncSecretAlias, &props) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - return 0; } @@ -1852,17 +1850,12 @@ qemuBuildObjectCommandline(virCommand *cmd, virJSONValue *objProps, virQEMUCaps *qemuCaps) { - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; - if (!objProps) return 0; - if (qemuBuildObjectCommandlineFromJSON(&buf, objProps, qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, objProps, qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - return 0; } @@ -3210,7 +3203,7 @@ qemuBuildMemoryCellBackendProps(virDomainDef *def, static int -qemuBuildMemoryDimmBackendStr(virBuffer *buf, +qemuBuildMemoryDimmBackendStr(virCommand *cmd, virDomainMemoryDef *mem, virDomainDef *def, virQEMUDriverConfig *cfg, @@ -3231,7 +3224,7 @@ qemuBuildMemoryDimmBackendStr(virBuffer *buf, priv, def, mem, true, false) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; return 0; @@ -3946,16 +3939,12 @@ qemuBuildInputCommandLine(virCommand *cmd, if (input->type == VIR_DOMAIN_INPUT_TYPE_EVDEV) { g_autoptr(virJSONValue) props = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; if (!(props = qemuBuildInputEvdevProps(input))) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, qemuCaps) < 0) return -1; - - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); } else { g_autofree char *devstr = NULL; @@ -5523,11 +5512,9 @@ qemuBuildRNGCommandLine(virLogManager *logManager, for (i = 0; i < def->nrngs; i++) { g_autoptr(virJSONValue) props = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; virDomainRNGDef *rng = def->rngs[i]; g_autofree char *chardev = NULL; g_autofree char *devstr = NULL; - int rc; if (!rng->info.alias) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -5547,14 +5534,9 @@ qemuBuildRNGCommandLine(virLogManager *logManager, if (qemuBuildRNGBackendProps(rng, &props) < 0) return -1; - rc = qemuBuildObjectCommandlineFromJSON(&buf, props, qemuCaps); - - if (rc < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - /* add the device */ if (qemuCommandAddExtDevice(cmd, &rng->info) < 0) return -1; @@ -7109,7 +7091,6 @@ qemuBuildMemCommandLineMemoryDefaultBackend(virCommand *cmd, { g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(priv->driver); g_autoptr(virJSONValue) props = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; virDomainMemoryDef mem = { 0 }; mem.size = virDomainDefGetMemoryInitial(def); @@ -7120,11 +7101,9 @@ qemuBuildMemCommandLineMemoryDefaultBackend(virCommand *cmd, priv, def, &mem, false, true) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); return 0; } @@ -7203,17 +7182,13 @@ qemuBuildIOThreadCommandLine(virCommand *cmd, for (i = 0; i < def->niothreadids; i++) { g_autoptr(virJSONValue) props = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autofree char *alias = g_strdup_printf("iothread%u", def->iothreadids[i]->iothread_id); if (qemuMonitorCreateObjectProps(&props, "iothread", alias, NULL) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, qemuCaps) < 0) return -1; - - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); } return 0; @@ -7442,14 +7417,9 @@ qemuBuildNumaCommandLine(virQEMUDriverConfig *cfg, ssize_t initiator = virDomainNumaGetNodeInitiator(def->numa, i); if (needBackend) { - g_auto(virBuffer) objbuf = VIR_BUFFER_INITIALIZER; - - if (qemuBuildObjectCommandlineFromJSON(&objbuf, nodeBackends[i], + if (qemuBuildObjectCommandlineFromJSON(cmd, nodeBackends[i], priv->qemuCaps) < 0) goto cleanup; - - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &objbuf); } virCommandAddArg(cmd, "-numa"); @@ -7527,15 +7497,11 @@ qemuBuildMemoryDeviceCommandLine(virCommand *cmd, /* memory hotplug requires NUMA to be enabled - we already checked * that memory devices are present only when NUMA is */ for (i = 0; i < def->nmems; i++) { - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; char *dimmStr; - if (qemuBuildMemoryDimmBackendStr(&buf, def->mems[i], def, cfg, priv) < 0) + if (qemuBuildMemoryDimmBackendStr(cmd, def->mems[i], def, cfg, priv) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - if (!(dimmStr = qemuBuildMemoryDeviceStr(def, def->mems[i], priv->qemuCaps))) return -1; @@ -9044,10 +9010,8 @@ qemuBuildShmemCommandLine(virLogManager *logManager, bool chardevStdioLogd) { g_autoptr(virJSONValue) memProps = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autofree char *devstr = NULL; g_autofree char *chardev = NULL; - int rc; unsigned int cdevflags = QEMU_BUILD_CHARDEV_TCP_NOWAIT | QEMU_BUILD_CHARDEV_UNIX_FD_PASS; if (chardevStdioLogd) @@ -9088,14 +9052,9 @@ qemuBuildShmemCommandLine(virLogManager *logManager, if (!(memProps = qemuBuildShmemBackendMemProps(shmem))) return -1; - rc = qemuBuildObjectCommandlineFromJSON(&buf, memProps, qemuCaps); - - if (rc < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, memProps, qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - G_GNUC_FALLTHROUGH; case VIR_DOMAIN_SHMEM_MODEL_IVSHMEM_DOORBELL: devstr = qemuBuildShmemDevStr(def, shmem, qemuCaps); @@ -9816,7 +9775,6 @@ qemuBuildSEVCommandLine(virDomainObj *vm, virCommand *cmd, virDomainSEVDef *sev) { g_autoptr(virJSONValue) props = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; qemuDomainObjPrivate *priv = vm->privateData; g_autofree char *dhpath = NULL; g_autofree char *sessionpath = NULL; @@ -9839,11 +9797,9 @@ qemuBuildSEVCommandLine(virDomainObj *vm, virCommand *cmd, NULL) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); return 0; } @@ -9852,18 +9808,15 @@ static int qemuBuildPVCommandLine(virDomainObj *vm, virCommand *cmd) { g_autoptr(virJSONValue) props = NULL; - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; qemuDomainObjPrivate *priv = vm->privateData; if (qemuMonitorCreateObjectProps(&props, "s390-pv-guest", "lsec0", NULL) < 0) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); return 0; } @@ -9993,7 +9946,6 @@ qemuBuildManagedPRCommandLine(virCommand *cmd, const virDomainDef *def, qemuDomainObjPrivate *priv) { - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autoptr(virJSONValue) props = NULL; if (!virDomainDefHasManagedPR(def)) @@ -10002,12 +9954,9 @@ qemuBuildManagedPRCommandLine(virCommand *cmd, if (!(props = qemuBuildPRManagedManagerInfoProps(priv))) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - return 0; } @@ -10076,7 +10025,6 @@ qemuBuildDBusVMStateCommandLine(virCommand *cmd, virQEMUDriver *driver, virDomainObj *vm) { - g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_autoptr(virJSONValue) props = NULL; qemuDomainObjPrivate *priv = QEMU_DOMAIN_PRIVATE(vm); @@ -10091,12 +10039,9 @@ qemuBuildDBusVMStateCommandLine(virCommand *cmd, if (!(props = qemuBuildDBusVMStateInfoProps(driver, vm))) return -1; - if (qemuBuildObjectCommandlineFromJSON(&buf, props, priv->qemuCaps) < 0) + if (qemuBuildObjectCommandlineFromJSON(cmd, props, priv->qemuCaps) < 0) return -1; - virCommandAddArg(cmd, "-object"); - virCommandAddArgBuffer(cmd, &buf); - priv->dbusVMState = true; return 0; -- 2.31.1