1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-08-30 05:50:08 +03:00

qemuBuildChrChardevStr: Directly generate command line

'qemuBuildChrChardevStr' used a hybrid approach where some arguments
were directly added to '@cmd' while the commandline itself was returned
as a string.

This patch renames qemuBuildChrChardevStr to qemuBuildChardevCommand
and adds the argument directly to @cmd inside the function.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa
2021-11-04 13:59:47 +01:00
parent dc3f025617
commit cf2d64cc27

View File

@ -5036,14 +5036,12 @@ qemuOpenChrChardevUNIXSocket(const virDomainChrSourceDef *dev)
} }
/* This function outputs a -chardev command line option which describes only the static int
* host side of the character device */ qemuBuildChardevCommand(virCommand *cmd,
static char * virQEMUDriverConfig *cfg,
qemuBuildChrChardevStr(virCommand *cmd, const virDomainChrSourceDef *dev,
virQEMUDriverConfig *cfg, const char *alias,
const virDomainChrSourceDef *dev, virQEMUCaps *qemuCaps)
const char *alias,
virQEMUCaps *qemuCaps)
{ {
qemuDomainChrSourcePrivate *chrSourcePriv = QEMU_DOMAIN_CHR_SOURCE_PRIVATE(dev); qemuDomainChrSourcePrivate *chrSourcePriv = QEMU_DOMAIN_CHR_SOURCE_PRIVATE(dev);
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
@ -5051,7 +5049,7 @@ qemuBuildChrChardevStr(virCommand *cmd,
g_autofree char *charAlias = NULL; g_autofree char *charAlias = NULL;
if (!(charAlias = qemuAliasChardevFromDevAlias(alias))) if (!(charAlias = qemuAliasChardevFromDevAlias(alias)))
return NULL; return -1;
switch ((virDomainChrType) dev->type) { switch ((virDomainChrType) dev->type) {
case VIR_DOMAIN_CHR_TYPE_NULL: case VIR_DOMAIN_CHR_TYPE_NULL:
@ -5158,20 +5156,20 @@ qemuBuildChrChardevStr(virCommand *cmd,
if (qemuBuildObjectSecretCommandLine(cmd, if (qemuBuildObjectSecretCommandLine(cmd,
chrSourcePriv->secinfo, chrSourcePriv->secinfo,
qemuCaps) < 0) qemuCaps) < 0)
return NULL; return -1;
tlsCertEncSecAlias = chrSourcePriv->secinfo->alias; tlsCertEncSecAlias = chrSourcePriv->secinfo->alias;
} }
if (!(objalias = qemuAliasTLSObjFromSrcAlias(charAlias))) if (!(objalias = qemuAliasTLSObjFromSrcAlias(charAlias)))
return NULL; return -1;
if (qemuBuildTLSx509CommandLine(cmd, cfg->chardevTLSx509certdir, if (qemuBuildTLSx509CommandLine(cmd, cfg->chardevTLSx509certdir,
dev->data.tcp.listen, dev->data.tcp.listen,
cfg->chardevTLSx509verify, cfg->chardevTLSx509verify,
tlsCertEncSecAlias, tlsCertEncSecAlias,
objalias, qemuCaps) < 0) { objalias, qemuCaps) < 0) {
return NULL; return -1;
} }
virBufferAsprintf(&buf, ",tls-creds=%s", objalias); virBufferAsprintf(&buf, ",tls-creds=%s", objalias);
@ -5214,7 +5212,7 @@ qemuBuildChrChardevStr(virCommand *cmd,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unsupported chardev '%s'"), _("unsupported chardev '%s'"),
virDomainChrTypeToString(dev->type)); virDomainChrTypeToString(dev->type));
return NULL; return -1;
} }
if (dev->logfile) { if (dev->logfile) {
@ -5241,7 +5239,9 @@ qemuBuildChrChardevStr(virCommand *cmd,
} }
} }
return virBufferContentAndReset(&buf); virCommandAddArgList(cmd, "-chardev", virBufferCurrentContent(&buf), NULL);
return 0;
} }
@ -5513,19 +5513,15 @@ qemuBuildMonitorCommandLine(virCommand *cmd,
virQEMUDriverConfig *cfg, virQEMUDriverConfig *cfg,
qemuDomainObjPrivate *priv) qemuDomainObjPrivate *priv)
{ {
g_autofree char *chrdev = NULL;
if (!priv->monConfig) if (!priv->monConfig)
return 0; return 0;
if (!(chrdev = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
priv->monConfig, priv->monConfig,
"monitor", "monitor",
priv->qemuCaps))) priv->qemuCaps) < 0)
return -1; return -1;
virCommandAddArg(cmd, "-chardev");
virCommandAddArg(cmd, chrdev);
virCommandAddArg(cmd, "-mon"); virCommandAddArg(cmd, "-mon");
virCommandAddArg(cmd, "chardev=charmonitor,id=monitor,mode=control"); virCommandAddArg(cmd, "chardev=charmonitor,id=monitor,mode=control");
@ -5641,14 +5637,11 @@ qemuBuildSclpDevProps(virDomainChrDef *dev)
static int static int
qemuBuildRNGBackendChrdevStr(virCommand *cmd, qemuBuildRNGBackendChrdev(virCommand *cmd,
virQEMUDriverConfig *cfg, virQEMUDriverConfig *cfg,
virDomainRNGDef *rng, virDomainRNGDef *rng,
virQEMUCaps *qemuCaps, virQEMUCaps *qemuCaps)
char **chr)
{ {
*chr = NULL;
switch ((virDomainRNGBackend) rng->backend) { switch ((virDomainRNGBackend) rng->backend) {
case VIR_DOMAIN_RNG_BACKEND_RANDOM: case VIR_DOMAIN_RNG_BACKEND_RANDOM:
case VIR_DOMAIN_RNG_BACKEND_BUILTIN: case VIR_DOMAIN_RNG_BACKEND_BUILTIN:
@ -5657,11 +5650,11 @@ qemuBuildRNGBackendChrdevStr(virCommand *cmd,
return 0; return 0;
case VIR_DOMAIN_RNG_BACKEND_EGD: case VIR_DOMAIN_RNG_BACKEND_EGD:
if (!(*chr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
rng->source.chardev, rng->source.chardev,
rng->info.alias, rng->info.alias,
qemuCaps))) qemuCaps) < 0)
return -1; return -1;
break; break;
} }
@ -5759,7 +5752,6 @@ qemuBuildRNGCommandLine(virCommand *cmd,
for (i = 0; i < def->nrngs; i++) { for (i = 0; i < def->nrngs; i++) {
g_autoptr(virJSONValue) props = NULL; g_autoptr(virJSONValue) props = NULL;
virDomainRNGDef *rng = def->rngs[i]; virDomainRNGDef *rng = def->rngs[i];
g_autofree char *chardev = NULL;
g_autoptr(virJSONValue) devprops = NULL; g_autoptr(virJSONValue) devprops = NULL;
if (!rng->info.alias) { if (!rng->info.alias) {
@ -5769,12 +5761,9 @@ qemuBuildRNGCommandLine(virCommand *cmd,
} }
/* possibly add character device for backend */ /* possibly add character device for backend */
if (qemuBuildRNGBackendChrdevStr(cmd, cfg, rng, qemuCaps, &chardev) < 0) if (qemuBuildRNGBackendChrdev(cmd, cfg, rng, qemuCaps) < 0)
return -1; return -1;
if (chardev)
virCommandAddArgList(cmd, "-chardev", chardev, NULL);
if (qemuBuildRNGBackendProps(rng, &props) < 0) if (qemuBuildRNGBackendProps(rng, &props) < 0)
return -1; return -1;
@ -8597,18 +8586,17 @@ static int
qemuInterfaceVhostuserConnect(virQEMUDriver *driver, qemuInterfaceVhostuserConnect(virQEMUDriver *driver,
virCommand *cmd, virCommand *cmd,
virDomainNetDef *net, virDomainNetDef *net,
virQEMUCaps *qemuCaps, virQEMUCaps *qemuCaps)
char **chardev)
{ {
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
switch ((virDomainChrType)net->data.vhostuser->type) { switch ((virDomainChrType)net->data.vhostuser->type) {
case VIR_DOMAIN_CHR_TYPE_UNIX: case VIR_DOMAIN_CHR_TYPE_UNIX:
if (!(*chardev = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
net->data.vhostuser, net->data.vhostuser,
net->info.alias, net->info.alias,
qemuCaps))) qemuCaps) < 0)
return -1; return -1;
break; break;
@ -8649,7 +8637,6 @@ qemuBuildInterfaceCommandLine(virQEMUDriver *driver,
int ret = -1; int ret = -1;
g_autoptr(virJSONValue) nicprops = NULL; g_autoptr(virJSONValue) nicprops = NULL;
g_autofree char *nic = NULL; g_autofree char *nic = NULL;
g_autofree char *chardev = NULL;
int *tapfd = NULL; int *tapfd = NULL;
size_t tapfdSize = 0; size_t tapfdSize = 0;
int *vhostfd = NULL; int *vhostfd = NULL;
@ -8727,7 +8714,7 @@ qemuBuildInterfaceCommandLine(virQEMUDriver *driver,
case VIR_DOMAIN_NET_TYPE_VHOSTUSER: case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
requireNicdev = true; requireNicdev = true;
if (qemuInterfaceVhostuserConnect(driver, cmd, net, qemuCaps, &chardev) < 0) if (qemuInterfaceVhostuserConnect(driver, cmd, net, qemuCaps) < 0)
goto cleanup; goto cleanup;
if (virNetDevOpenvswitchGetVhostuserIfname(net->data.vhostuser->data.nix.path, if (virNetDevOpenvswitchGetVhostuserIfname(net->data.vhostuser->data.nix.path,
@ -8888,9 +8875,6 @@ qemuBuildInterfaceCommandLine(virQEMUDriver *driver,
vdpafd = -1; vdpafd = -1;
} }
if (chardev)
virCommandAddArgList(cmd, "-chardev", chardev, NULL);
if (!(hostnetprops = qemuBuildHostNetProps(net, if (!(hostnetprops = qemuBuildHostNetProps(net,
tapfdName, tapfdSize, tapfdName, tapfdSize,
vhostfdName, vhostfdSize, vhostfdName, vhostfdSize,
@ -9045,18 +9029,14 @@ qemuBuildSmartcardCommandLine(virCommand *cmd,
break; break;
case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH: { case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH: {
g_autofree char *chardevstr = NULL;
g_autofree char *chardevalias = g_strdup_printf("char%s", smartcard->info.alias); g_autofree char *chardevalias = g_strdup_printf("char%s", smartcard->info.alias);
if (!(chardevstr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
smartcard->data.passthru, smartcard->data.passthru,
smartcard->info.alias, smartcard->info.alias,
qemuCaps))) { qemuCaps) < 0)
return -1; return -1;
}
virCommandAddArgList(cmd, "-chardev", chardevstr, NULL);
if (virJSONValueObjectAdd(&props, if (virJSONValueObjectAdd(&props,
"s:driver", "ccid-card-passthru", "s:driver", "ccid-card-passthru",
@ -9205,7 +9185,6 @@ qemuBuildShmemCommandLine(virCommand *cmd,
{ {
g_autoptr(virJSONValue) memProps = NULL; g_autoptr(virJSONValue) memProps = NULL;
g_autoptr(virJSONValue) devProps = NULL; g_autoptr(virJSONValue) devProps = NULL;
g_autofree char *chardev = NULL;
if (shmem->size) { if (shmem->size) {
/* /*
@ -9264,15 +9243,12 @@ qemuBuildShmemCommandLine(virCommand *cmd,
return -1; return -1;
if (shmem->server.enabled) { if (shmem->server.enabled) {
chardev = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
shmem->server.chr, shmem->server.chr,
shmem->info.alias, shmem->info.alias,
qemuCaps); qemuCaps) < 0)
if (!chardev)
return -1; return -1;
virCommandAddArgList(cmd, "-chardev", chardev, NULL);
} }
return 0; return 0;
@ -9381,19 +9357,16 @@ qemuBuildSerialCommandLine(virCommand *cmd,
for (i = 0; i < def->nserials; i++) { for (i = 0; i < def->nserials; i++) {
virDomainChrDef *serial = def->serials[i]; virDomainChrDef *serial = def->serials[i];
g_autofree char *devstr = NULL;
if (serial->source->type == VIR_DOMAIN_CHR_TYPE_SPICEPORT && !havespice) if (serial->source->type == VIR_DOMAIN_CHR_TYPE_SPICEPORT && !havespice)
continue; continue;
if (!(devstr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
serial->source, serial->source,
serial->info.alias, serial->info.alias,
qemuCaps))) qemuCaps) < 0)
return -1; return -1;
virCommandAddArg(cmd, "-chardev");
virCommandAddArg(cmd, devstr);
/* If the device is not a platform device, build the devstr */ /* If the device is not a platform device, build the devstr */
if (!qemuChrIsPlatformDevice(def, serial)) { if (!qemuChrIsPlatformDevice(def, serial)) {
@ -9430,16 +9403,13 @@ qemuBuildParallelsCommandLine(virCommand *cmd,
for (i = 0; i < def->nparallels; i++) { for (i = 0; i < def->nparallels; i++) {
virDomainChrDef *parallel = def->parallels[i]; virDomainChrDef *parallel = def->parallels[i];
g_autofree char *devstr = NULL;
if (!(devstr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
parallel->source, parallel->source,
parallel->info.alias, parallel->info.alias,
qemuCaps))) qemuCaps) < 0)
return -1; return -1;
virCommandAddArg(cmd, "-chardev");
virCommandAddArg(cmd, devstr);
if (qemuBuildChrDeviceCommandLine(cmd, def, parallel, if (qemuBuildChrDeviceCommandLine(cmd, def, parallel,
qemuCaps) < 0) qemuCaps) < 0)
@ -9460,19 +9430,15 @@ qemuBuildChannelsCommandLine(virCommand *cmd,
for (i = 0; i < def->nchannels; i++) { for (i = 0; i < def->nchannels; i++) {
virDomainChrDef *channel = def->channels[i]; virDomainChrDef *channel = def->channels[i];
g_autofree char *chardevstr = NULL;
g_autoptr(virJSONValue) netdevprops = NULL; g_autoptr(virJSONValue) netdevprops = NULL;
if (!(chardevstr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
channel->source, channel->source,
channel->info.alias, channel->info.alias,
qemuCaps))) qemuCaps) < 0)
return -1; return -1;
virCommandAddArg(cmd, "-chardev");
virCommandAddArg(cmd, chardevstr);
switch ((virDomainChrChannelTargetType) channel->targetType) { switch ((virDomainChrChannelTargetType) channel->targetType) {
case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_GUESTFWD: case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_GUESTFWD:
if (!(netdevprops = qemuBuildChannelGuestfwdNetdevProps(channel))) if (!(netdevprops = qemuBuildChannelGuestfwdNetdevProps(channel)))
@ -9509,21 +9475,17 @@ qemuBuildConsoleCommandLine(virCommand *cmd,
/* Explicit console devices */ /* Explicit console devices */
for (i = 0; i < def->nconsoles; i++) { for (i = 0; i < def->nconsoles; i++) {
virDomainChrDef *console = def->consoles[i]; virDomainChrDef *console = def->consoles[i];
char *devstr;
switch (console->targetType) { switch (console->targetType) {
case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLP: case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLP:
case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLPLM: case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLPLM:
case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_VIRTIO: case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_VIRTIO:
if (!(devstr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
console->source, console->source,
console->info.alias, console->info.alias,
qemuCaps))) qemuCaps) < 0)
return -1; return -1;
virCommandAddArg(cmd, "-chardev");
virCommandAddArg(cmd, devstr);
VIR_FREE(devstr);
if (qemuBuildChrDeviceCommandLine(cmd, def, console, qemuCaps) < 0) if (qemuBuildChrDeviceCommandLine(cmd, def, console, qemuCaps) < 0)
return -1; return -1;
@ -9610,19 +9572,13 @@ qemuBuildRedirdevCommandLine(virCommand *cmd,
for (i = 0; i < def->nredirdevs; i++) { for (i = 0; i < def->nredirdevs; i++) {
virDomainRedirdevDef *redirdev = def->redirdevs[i]; virDomainRedirdevDef *redirdev = def->redirdevs[i];
g_autoptr(virJSONValue) devprops = NULL; g_autoptr(virJSONValue) devprops = NULL;
char *devstr;
if (!(devstr = qemuBuildChrChardevStr(cmd, if (qemuBuildChardevCommand(cmd,
cfg, cfg,
redirdev->source, redirdev->source,
redirdev->info.alias, redirdev->info.alias,
qemuCaps))) { qemuCaps) < 0)
return -1; return -1;
}
virCommandAddArg(cmd, "-chardev");
virCommandAddArg(cmd, devstr);
VIR_FREE(devstr);
if (!(devprops = qemuBuildRedirdevDevProps(def, redirdev))) if (!(devprops = qemuBuildRedirdevDevProps(def, redirdev)))
return -1; return -1;