mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 11:35:19 +00:00
qemuMonitorJSONAttachCharDevGetProps: Modernize construction of JSON objects
Use 'virJSONValueObjectAdd' instead of the step-by-step manual JSON object building. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
03cab99ca5
commit
d5643e73da
@ -6596,151 +6596,136 @@ int qemuMonitorJSONGetTPMTypes(qemuMonitor *mon,
|
|||||||
return qemuMonitorJSONGetStringArray(mon, "query-tpm-types", tpmtypes);
|
return qemuMonitorJSONGetStringArray(mon, "query-tpm-types", tpmtypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
qemuMonitorJSONBuildChrChardevReconnect(virJSONValue *object,
|
|
||||||
const virDomainChrSourceReconnectDef *def)
|
|
||||||
{
|
|
||||||
int timeout = 0;
|
|
||||||
|
|
||||||
switch (def->enabled) {
|
|
||||||
case VIR_TRISTATE_BOOL_ABSENT:
|
|
||||||
case VIR_TRISTATE_BOOL_LAST:
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
case VIR_TRISTATE_BOOL_YES:
|
|
||||||
timeout = def->timeout;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VIR_TRISTATE_BOOL_NO:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return virJSONValueObjectAppendNumberUint(object, "reconnect", timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
static virJSONValue *
|
static virJSONValue *
|
||||||
qemuMonitorJSONAttachCharDevGetProps(const char *chrID,
|
qemuMonitorJSONAttachCharDevGetProps(const char *chrID,
|
||||||
const virDomainChrSourceDef *chr)
|
const virDomainChrSourceDef *chr)
|
||||||
{
|
{
|
||||||
g_autoptr(virJSONValue) props = NULL;
|
g_autoptr(virJSONValue) props = NULL;
|
||||||
g_autoptr(virJSONValue) backend = virJSONValueNewObject();
|
g_autoptr(virJSONValue) backend = NULL;
|
||||||
g_autoptr(virJSONValue) backendData = virJSONValueNewObject();
|
g_autoptr(virJSONValue) backendData = virJSONValueNewObject();
|
||||||
g_autoptr(virJSONValue) addr = NULL;
|
|
||||||
const char *backendType = NULL;
|
const char *backendType = NULL;
|
||||||
const char *host;
|
|
||||||
const char *port;
|
|
||||||
g_autofree char *tlsalias = NULL;
|
|
||||||
bool telnet;
|
|
||||||
|
|
||||||
switch ((virDomainChrType)chr->type) {
|
switch ((virDomainChrType)chr->type) {
|
||||||
case VIR_DOMAIN_CHR_TYPE_NULL:
|
case VIR_DOMAIN_CHR_TYPE_NULL:
|
||||||
backendType = "null";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_VC:
|
case VIR_DOMAIN_CHR_TYPE_VC:
|
||||||
backendType = "vc";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_PTY:
|
case VIR_DOMAIN_CHR_TYPE_PTY:
|
||||||
backendType = "pty";
|
backendType = virDomainChrTypeToString(chr->type);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_FILE:
|
case VIR_DOMAIN_CHR_TYPE_FILE:
|
||||||
backendType = "file";
|
backendType = "file";
|
||||||
if (virJSONValueObjectAppendString(backendData, "out", chr->data.file.path) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (virJSONValueObjectAdd(&backendData,
|
if (virJSONValueObjectAdd(&backendData,
|
||||||
|
"s:out", chr->data.file.path,
|
||||||
"T:append", chr->data.file.append,
|
"T:append", chr->data.file.append,
|
||||||
NULL) < 0)
|
NULL) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_DEV:
|
case VIR_DOMAIN_CHR_TYPE_DEV:
|
||||||
backendType = STRPREFIX(chrID, "parallel") ? "parallel" : "serial";
|
if (STRPREFIX(chrID, "parallel"))
|
||||||
if (virJSONValueObjectAppendString(backendData, "device",
|
backendType = "parallel";
|
||||||
chr->data.file.path) < 0)
|
else
|
||||||
return NULL;
|
backendType = "serial";
|
||||||
break;
|
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_TCP:
|
if (virJSONValueObjectAdd(&backendData,
|
||||||
backendType = "socket";
|
"s:device", chr->data.file.path,
|
||||||
addr = qemuMonitorJSONBuildInetSocketAddress(chr->data.tcp.host,
|
NULL) < 0)
|
||||||
chr->data.tcp.service);
|
|
||||||
if (!addr ||
|
|
||||||
virJSONValueObjectAppend(backendData, "addr", &addr) < 0)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
telnet = chr->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
|
|
||||||
|
|
||||||
if (chr->data.tcp.listen &&
|
|
||||||
virJSONValueObjectAppendBoolean(backendData, "wait", false) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (virJSONValueObjectAppendBoolean(backendData, "telnet", telnet) < 0 ||
|
|
||||||
virJSONValueObjectAppendBoolean(backendData, "server", chr->data.tcp.listen) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (chr->data.tcp.tlscreds) {
|
|
||||||
if (!(tlsalias = qemuAliasTLSObjFromSrcAlias(chrID)))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (virJSONValueObjectAppendString(backendData, "tls-creds", tlsalias) < 0)
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (qemuMonitorJSONBuildChrChardevReconnect(backendData, &chr->data.tcp.reconnect) < 0)
|
|
||||||
return NULL;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_UDP:
|
|
||||||
backendType = "udp";
|
|
||||||
host = chr->data.udp.connectHost;
|
|
||||||
if (!host)
|
|
||||||
host = "";
|
|
||||||
addr = qemuMonitorJSONBuildInetSocketAddress(host,
|
|
||||||
chr->data.udp.connectService);
|
|
||||||
if (!addr ||
|
|
||||||
virJSONValueObjectAppend(backendData, "remote", &addr) < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
host = chr->data.udp.bindHost;
|
|
||||||
port = chr->data.udp.bindService;
|
|
||||||
if (host || port) {
|
|
||||||
if (!host)
|
|
||||||
host = "";
|
|
||||||
if (!port)
|
|
||||||
port = "";
|
|
||||||
addr = qemuMonitorJSONBuildInetSocketAddress(host, port);
|
|
||||||
if (!addr ||
|
|
||||||
virJSONValueObjectAppend(backendData, "local", &addr) < 0)
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_UNIX:
|
case VIR_DOMAIN_CHR_TYPE_UNIX:
|
||||||
|
case VIR_DOMAIN_CHR_TYPE_TCP: {
|
||||||
|
g_autofree char *tlsalias = NULL;
|
||||||
|
g_autoptr(virJSONValue) addr = NULL;
|
||||||
|
virTristateBool waitval = VIR_TRISTATE_BOOL_ABSENT;
|
||||||
|
virTristateBool telnet = VIR_TRISTATE_BOOL_ABSENT;
|
||||||
|
bool server = false;
|
||||||
|
int reconnect = -1;
|
||||||
|
|
||||||
backendType = "socket";
|
backendType = "socket";
|
||||||
addr = qemuMonitorJSONBuildUnixSocketAddress(chr->data.nix.path);
|
|
||||||
|
|
||||||
if (!addr ||
|
if (chr->type == VIR_DOMAIN_CHR_TYPE_TCP) {
|
||||||
virJSONValueObjectAppend(backendData, "addr", &addr) < 0)
|
telnet = virTristateBoolFromBool(chr->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET);
|
||||||
|
|
||||||
|
if (chr->data.tcp.listen) {
|
||||||
|
server = true;
|
||||||
|
waitval = VIR_TRISTATE_BOOL_NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chr->data.tcp.tlscreds &&
|
||||||
|
!(tlsalias = qemuAliasTLSObjFromSrcAlias(chrID)))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!(addr = qemuMonitorJSONBuildInetSocketAddress(chr->data.tcp.host,
|
||||||
|
chr->data.tcp.service)))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (chr->data.tcp.reconnect.enabled == VIR_TRISTATE_BOOL_YES)
|
||||||
|
reconnect = chr->data.tcp.reconnect.timeout;
|
||||||
|
else if (chr->data.tcp.reconnect.enabled == VIR_TRISTATE_BOOL_NO)
|
||||||
|
reconnect = 0;
|
||||||
|
} else {
|
||||||
|
if (chr->data.nix.listen) {
|
||||||
|
server = true;
|
||||||
|
waitval = VIR_TRISTATE_BOOL_NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(addr = qemuMonitorJSONBuildUnixSocketAddress(chr->data.nix.path)))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (chr->data.nix.reconnect.enabled == VIR_TRISTATE_BOOL_YES)
|
||||||
|
reconnect = chr->data.tcp.reconnect.timeout;
|
||||||
|
else if (chr->data.nix.reconnect.enabled == VIR_TRISTATE_BOOL_NO)
|
||||||
|
reconnect = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (virJSONValueObjectAdd(&backendData,
|
||||||
|
"a:addr", &addr,
|
||||||
|
"T:wait", waitval,
|
||||||
|
"T:telnet", telnet,
|
||||||
|
"b:server", server,
|
||||||
|
"S:tls-creds", tlsalias,
|
||||||
|
"k:reconnect", reconnect,
|
||||||
|
NULL) < 0)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case VIR_DOMAIN_CHR_TYPE_UDP: {
|
||||||
|
g_autoptr(virJSONValue) local = NULL;
|
||||||
|
g_autoptr(virJSONValue) remote = NULL;
|
||||||
|
|
||||||
|
backendType = "udp";
|
||||||
|
|
||||||
|
if (!(remote = qemuMonitorJSONBuildInetSocketAddress(NULLSTR_EMPTY(chr->data.udp.connectHost),
|
||||||
|
chr->data.udp.connectService)))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (chr->data.nix.listen &&
|
if (chr->data.udp.bindHost || chr->data.udp.bindService) {
|
||||||
virJSONValueObjectAppendBoolean(backendData, "wait", false) < 0)
|
if (!(local = qemuMonitorJSONBuildInetSocketAddress(NULLSTR_EMPTY(chr->data.udp.bindHost),
|
||||||
return NULL;
|
NULLSTR_EMPTY(chr->data.udp.bindService))))
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (virJSONValueObjectAppendBoolean(backendData, "server", chr->data.nix.listen) < 0)
|
if (virJSONValueObjectAdd(&backendData,
|
||||||
return NULL;
|
"a:remote", &remote,
|
||||||
|
"A:local", &local,
|
||||||
if (qemuMonitorJSONBuildChrChardevReconnect(backendData, &chr->data.nix.reconnect) < 0)
|
NULL) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
|
case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
|
||||||
backendType = "spicevmc";
|
backendType = "spicevmc";
|
||||||
|
|
||||||
if (virJSONValueObjectAppendString(backendData, "type",
|
if (virJSONValueObjectAdd(&backendData,
|
||||||
virDomainChrSpicevmcTypeToString(chr->data.spicevmc)) < 0)
|
"s:type", virDomainChrSpicevmcTypeToString(chr->data.spicevmc),
|
||||||
|
NULL) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
|
case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
|
||||||
@ -6758,15 +6743,18 @@ qemuMonitorJSONAttachCharDevGetProps(const char *chrID,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chr->logfile &&
|
if (chr->logfile) {
|
||||||
virJSONValueObjectAdd(&backendData,
|
if (virJSONValueObjectAdd(&backendData,
|
||||||
"s:logfile", chr->logfile,
|
"s:logfile", chr->logfile,
|
||||||
"T:logappend", chr->logappend,
|
"T:logappend", chr->logappend,
|
||||||
NULL) < 0)
|
NULL) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (virJSONValueObjectAppendString(backend, "type", backendType) < 0 ||
|
if (virJSONValueObjectAdd(&backend,
|
||||||
virJSONValueObjectAppend(backend, "data", &backendData) < 0)
|
"s:type", backendType,
|
||||||
|
"A:data", &backendData,
|
||||||
|
NULL) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (virJSONValueObjectAdd(&props,
|
if (virJSONValueObjectAdd(&props,
|
||||||
|
Loading…
Reference in New Issue
Block a user