From 78a09aa0338e6a26c7409dd6b99336857822a5ba Mon Sep 17 00:00:00 2001 From: Pavel Hrdina Date: Wed, 18 May 2016 10:52:22 +0200 Subject: [PATCH] graphics: resolve address for listen type network in qemu_process Both VNC and SPICE requires the same code to resolve address for listen type network. Remove code duplication and create a new function that will be used in qemuProcessSetupGraphics(). Signed-off-by: Pavel Hrdina --- src/qemu/qemu_command.c | 103 +++++----------------------------------- src/qemu/qemu_process.c | 48 ++++++++++++++++++- 2 files changed, 58 insertions(+), 93 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 27e8c76909..dd12f86460 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -7358,10 +7358,7 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, { virBuffer opt = VIR_BUFFER_INITIALIZER; virDomainGraphicsListenDefPtr glisten = NULL; - const char *listenAddr = NULL; - char *netAddr = NULL; bool escapeAddr; - int ret; if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNC)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", @@ -7369,6 +7366,8 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, goto error; } + glisten = virDomainGraphicsGetListen(graphics, 0); + if (graphics->data.vnc.socket || cfg->vncAutoUnixSocket) { if (!graphics->data.vnc.socket) { if (virAsprintf(&graphics->data.vnc.socket, @@ -7390,52 +7389,15 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, goto error; } - if ((glisten = virDomainGraphicsGetListen(graphics, 0))) { - - switch (glisten->type) { - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS: - listenAddr = glisten->address; - break; - - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK: - if (!glisten->network) - break; - - ret = networkGetNetworkAddress(glisten->network, &netAddr); - if (ret <= -2) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - "%s", _("network-based listen not possible, " - "network driver not present")); - goto error; - } - if (ret < 0) - goto error; - - listenAddr = netAddr; - /* store the address we found in the element so it - * will show up in status. */ - if (VIR_STRDUP(glisten->address, netAddr) < 0) - goto error; - break; - - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NONE: - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_LAST: - break; - } + if (glisten && glisten->address) { + escapeAddr = strchr(glisten->address, ':') != NULL; + if (escapeAddr) + virBufferAsprintf(&opt, "[%s]", glisten->address); + else + virBufferAdd(&opt, glisten->address, -1); } - - if (!listenAddr) - listenAddr = cfg->vncListen; - - escapeAddr = strchr(listenAddr, ':') != NULL; - if (escapeAddr) - virBufferAsprintf(&opt, "[%s]", listenAddr); - else - virBufferAdd(&opt, listenAddr, -1); virBufferAsprintf(&opt, ":%d", graphics->data.vnc.port - 5900); - - VIR_FREE(netAddr); } if (!graphics->data.vnc.socket && @@ -7499,7 +7461,6 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, return 0; error: - VIR_FREE(netAddr); virBufferFreeAndReset(&opt); return -1; } @@ -7513,9 +7474,6 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, { virBuffer opt = VIR_BUFFER_INITIALIZER; virDomainGraphicsListenDefPtr glisten = NULL; - const char *listenAddr = NULL; - char *netAddr = NULL; - int ret; int defaultMode = graphics->data.spice.defaultMode; int port = graphics->data.spice.port; int tlsPort = graphics->data.spice.tlsPort; @@ -7527,6 +7485,8 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, goto error; } + glisten = virDomainGraphicsGetListen(graphics, 0); + if (port > 0) virBufferAsprintf(&opt, "port=%u,", port); @@ -7541,46 +7501,8 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, } if (port > 0 || tlsPort > 0) { - if ((glisten = virDomainGraphicsGetListen(graphics, 0))) { - - switch (glisten->type) { - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS: - listenAddr = glisten->address; - break; - - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK: - if (!glisten->network) - break; - - ret = networkGetNetworkAddress(glisten->network, &netAddr); - if (ret <= -2) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - "%s", _("network-based listen not possible, " - "network driver not present")); - goto error; - } - if (ret < 0) - goto error; - - listenAddr = netAddr; - /* store the address we found in the element so it will - * show up in status. */ - if (VIR_STRDUP(glisten->address, listenAddr) < 0) - goto error; - break; - - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NONE: - case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_LAST: - break; - } - } - - if (!listenAddr) - listenAddr = cfg->spiceListen; - if (listenAddr) - virBufferAsprintf(&opt, "addr=%s,", listenAddr); - - VIR_FREE(netAddr); + if (glisten && glisten->address) + virBufferAsprintf(&opt, "addr=%s,", glisten->address); } if (cfg->spiceSASL) { @@ -7749,7 +7671,6 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, return 0; error: - VIR_FREE(netAddr); virBufferFreeAndReset(&opt); return -1; } diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index fbc778bdfd..fb8df6aeef 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3990,6 +3990,33 @@ qemuProcessGraphicsReservePorts(virQEMUDriverPtr driver, } +static int +qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr glisten, + const char *listenAddr) +{ + int rc; + + /* TODO: reject configuration without network specified for network listen */ + if (!glisten->network) { + if (VIR_STRDUP(glisten->address, listenAddr) < 0) + return -1; + return 0; + } + + rc = networkGetNetworkAddress(glisten->network, &glisten->address); + if (rc <= -2) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("network-based listen isn't possible, " + "network driver isn't present")); + return -1; + } + if (rc < 0) + return -1; + + return 0; +} + + static int qemuProcessSetupGraphics(virQEMUDriverPtr driver, virDomainObjPtr vm, @@ -4032,12 +4059,29 @@ qemuProcessSetupGraphics(virQEMUDriverPtr driver, for (j = 0; j < graphics->nListens; j++) { virDomainGraphicsListenDefPtr glisten = &graphics->listens[j]; - if (glisten->type == VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS && - !glisten->address && listenAddr) { + switch (glisten->type) { + case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS: + if (glisten->address || !listenAddr) + continue; + if (VIR_STRDUP(glisten->address, listenAddr) < 0) goto cleanup; glisten->fromConfig = true; + break; + + case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK: + if (glisten->address || !listenAddr) + continue; + + if (qemuProcessGraphicsSetupNetworkAddress(glisten, + listenAddr) < 0) + goto cleanup; + break; + + case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NONE: + case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_LAST: + break; } } }