From nobody Sun Feb 8 21:29:11 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1646229382; cv=none; d=zohomail.com; s=zohoarc; b=TOfMwmGQBzortApdPIy4BeqPfLbYwaS3hXjMnCCmqM0kVp2kpMEgzuuLMQ9EQP8Ar6+WXzmzWb+gq6xdZQq22DN/HnlupDZioa9gLHZ4t6PCI/OzhTW8eMTd/DTeUicjG0nY/I/f4vwAu2n8HnLT9e8H3jNzOZ7PNI4OfHrVwGo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646229382; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=J1bYDDQM7Il0DkL2CZshm8D3MMqF45gp5Ens5IyDIaQ=; b=FHVbGLVNpCHm9CFpD/Dm5iMgpi1myQjstBlqOn0ZkIWn/MqLQqwidbqQ/6MxYebBGkLlf797NXO9g+VqAS0t7TXOw5xr8qbVpHmWj7k1em+FF9aO7TI0qxid46efVlKrO006HHNEuFz/qXGtJ0moKHKs/lBTSqk/HUr3nyvLQ5s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1646229382676470.06124954752977; Wed, 2 Mar 2022 05:56:22 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-672-v7LduyUJPRyPg1D3bnSRpw-1; Wed, 02 Mar 2022 08:56:18 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D57BE824FAC; Wed, 2 Mar 2022 13:56:13 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B2CF6838DA; Wed, 2 Mar 2022 13:56:13 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 81F221809CB4; Wed, 2 Mar 2022 13:56:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 222DuCob017733 for ; Wed, 2 Mar 2022 08:56:12 -0500 Received: by smtp.corp.redhat.com (Postfix) id 5657383780; Wed, 2 Mar 2022 13:56:12 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99E4B83783 for ; Wed, 2 Mar 2022 13:55:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646229382; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=J1bYDDQM7Il0DkL2CZshm8D3MMqF45gp5Ens5IyDIaQ=; b=VBQZFnotNWvclz22lDtqG74APClDsdjkTJNUUXNiwFDmSPNtNqr5Htr5YbOFczSUSeS/P7 F3pefxqeVHyss4YjFJUskfqCKRmB4CH8/H8xBml5SirTMU2wtqvm+G1c/JOjuTdc1kXwLo 1xiue68+laUKRmys6VckxPChIYbYRNk= X-MC-Unique: v7LduyUJPRyPg1D3bnSRpw-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 11/23] virsh: cmdDomDisplay: Extract loop body fetching display URIs into 'virshGetOneDisplay' Date: Wed, 2 Mar 2022 14:55:10 +0100 Message-Id: <7fd02940e2e7cfd9d98d2a780cc4b4f71f0d88d0.1646229194.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1646229391586100003 Content-Type: text/plain; charset="utf-8" Separate the code so that the function is not as massive. Note that this is a minimal extraction which does not clean up the code meant for looping. Signed-off-by: Peter Krempa --- tools/virsh-domain.c | 352 ++++++++++++++++++++++--------------------- 1 file changed, 183 insertions(+), 169 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index dc6e3b5020..f82aa49745 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -11659,215 +11659,235 @@ static const vshCmdOptDef opts_domdisplay[] =3D= { {.name =3D NULL} }; -static bool -cmdDomDisplay(vshControl *ctl, const vshCmd *cmd) + +static char * +virshGetOneDisplay(vshControl *ctl, + const char *scheme, + xmlXPathContext *ctxt) { - g_autoptr(xmlDoc) xml =3D NULL; - g_autoptr(xmlXPathContext) ctxt =3D NULL; - g_autoptr(virshDomain) dom =3D NULL; + const char *xpath_fmt =3D "string(/domain/devices/graphics[@type=3D'%s= ']/%s)"; g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; - bool ret =3D false; char *xpath =3D NULL; char *listen_addr =3D NULL; - int port, tls_port =3D 0; + int port =3D 0; + int tls_port =3D 0; char *type_conn =3D NULL; char *sockpath =3D NULL; char *passwd =3D NULL; - char *output =3D NULL; - const char *scheme[] =3D { "vnc", "spice", "rdp", NULL }; - const char *type =3D NULL; - int iter =3D 0; int tmp; - int flags =3D 0; bool params =3D false; - bool all =3D vshCommandOptBool(cmd, "all"); - const char *xpath_fmt =3D "string(/domain/devices/graphics[@type=3D'%s= ']/%s)"; virSocketAddr addr; - VSH_EXCLUSIVE_OPTIONS("all", "type"); + /* Create our XPATH lookup for the current display's port */ + VIR_FREE(xpath); + xpath =3D g_strdup_printf(xpath_fmt, scheme, "@port"); - if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) - return false; + /* Attempt to get the port number for the current graphics scheme */ + tmp =3D virXPathInt(xpath, ctxt, &port); + VIR_FREE(xpath); - if (!virDomainIsActive(dom)) { - vshError(ctl, _("Domain is not running")); - goto cleanup; - } + /* If there is no port number for this type, then jump to the next + * scheme */ + if (tmp) + port =3D 0; - if (vshCommandOptBool(cmd, "include-password")) - flags |=3D VIR_DOMAIN_XML_SECURE; + /* Create our XPATH lookup for TLS Port (automatically skipped + * for unsupported schemes */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "@tlsPort"); - if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0) - goto cleanup; + /* Attempt to get the TLS port number */ + tmp =3D virXPathInt(xpath, ctxt, &tls_port); + VIR_FREE(xpath); + if (tmp) + tls_port =3D 0; - if (virshDomainGetXMLFromDom(ctl, dom, flags, &xml, &ctxt) < 0) - goto cleanup; + /* Create our XPATH lookup for the current display's address */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "@listen"); - /* Attempt to grab our display info */ - for (iter =3D 0; scheme[iter] !=3D NULL; iter++) { - /* Particular scheme requested */ - if (!all && type && STRNEQ(type, scheme[iter])) - continue; + /* Attempt to get the listening addr if set for the current + * graphics scheme */ + VIR_FREE(listen_addr); + listen_addr =3D virXPathString(xpath, ctxt); + VIR_FREE(xpath); - /* Create our XPATH lookup for the current display's port */ - VIR_FREE(xpath); - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "@port"); + /* Create our XPATH lookup for the current spice type. */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "listen/@type"); - /* Attempt to get the port number for the current graphics scheme = */ - tmp =3D virXPathInt(xpath, ctxt, &port); - VIR_FREE(xpath); + /* Attempt to get the type of spice connection */ + VIR_FREE(type_conn); + type_conn =3D virXPathString(xpath, ctxt); + VIR_FREE(xpath); - /* If there is no port number for this type, then jump to the next - * scheme */ - if (tmp) - port =3D 0; + if (STREQ_NULLABLE(type_conn, "socket")) { + if (!sockpath) { + xpath =3D g_strdup_printf(xpath_fmt, scheme, "listen/@socket"); - /* Create our XPATH lookup for TLS Port (automatically skipped - * for unsupported schemes */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "@tlsPort"); + sockpath =3D virXPathString(xpath, ctxt); - /* Attempt to get the TLS port number */ - tmp =3D virXPathInt(xpath, ctxt, &tls_port); - VIR_FREE(xpath); - if (tmp) - tls_port =3D 0; + VIR_FREE(xpath); + } + } + + if (!port && !tls_port && !sockpath) + goto cleanup; - /* Create our XPATH lookup for the current display's address */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "@listen"); + if (!listen_addr) { + /* The subelement address - - + * *should* have been automatically backfilled into its + * parent (which we just tried to + * retrieve into listen_addr above) but in some cases it + * isn't, so we also do an explicit check for the + * subelement (which, by the way, doesn't exist on libvirt + * < 0.9.4, so we really do need to check both places) + */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "listen/@address"); - /* Attempt to get the listening addr if set for the current - * graphics scheme */ - VIR_FREE(listen_addr); listen_addr =3D virXPathString(xpath, ctxt); VIR_FREE(xpath); + } else { + /* If listen_addr is 0.0.0.0 or [::] we should try to parse URI an= d set + * listen_addr based on current URI. */ + if (virSocketAddrParse(&addr, listen_addr, AF_UNSPEC) > 0 && + virSocketAddrIsWildcard(&addr)) { + + virConnectPtr conn =3D ((virshControl *)(ctl->privData))->conn; + char *uriStr =3D virConnectGetURI(conn); + virURI *uri =3D NULL; + + if (uriStr) { + uri =3D virURIParse(uriStr); + VIR_FREE(uriStr); + } - /* Create our XPATH lookup for the current spice type. */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "listen/@type"); + /* It's safe to free the listen_addr even if parsing of URI + * fails, if there is no listen_addr we will print "localhost"= . */ + VIR_FREE(listen_addr); - /* Attempt to get the type of spice connection */ - VIR_FREE(type_conn); - type_conn =3D virXPathString(xpath, ctxt); - VIR_FREE(xpath); + if (uri) { + listen_addr =3D g_strdup(uri->server); + virURIFree(uri); + } + } + } - if (STREQ_NULLABLE(type_conn, "socket")) { - if (!sockpath) { - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "listen= /@socket"); + /* We can query this info for all the graphics types since we'll + * get nothing for the unsupported ones (just rdp for now). + * Also the parameter '--include-password' was already taken + * care of when getting the XML */ - sockpath =3D virXPathString(xpath, ctxt); + /* Create our XPATH lookup for the password */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "@passwd"); - VIR_FREE(xpath); - } + /* Attempt to get the password */ + VIR_FREE(passwd); + passwd =3D virXPathString(xpath, ctxt); + VIR_FREE(xpath); + + /* Build up the full URI, starting with the scheme */ + if (sockpath) + virBufferAsprintf(&buf, "%s+unix://", scheme); + else + virBufferAsprintf(&buf, "%s://", scheme); + + /* There is no user, so just append password if there's any */ + if (STREQ(scheme, "vnc") && passwd) + virBufferAsprintf(&buf, ":%s@", passwd); + + /* Then host name or IP */ + if (!listen_addr && !sockpath) + virBufferAddLit(&buf, "localhost"); + else if (!sockpath && strchr(listen_addr, ':')) + virBufferAsprintf(&buf, "[%s]", listen_addr); + else if (sockpath) + virBufferAsprintf(&buf, "%s", sockpath); + else + virBufferAsprintf(&buf, "%s", listen_addr); + + /* Free socket to prepare the pointer for the next iteration */ + VIR_FREE(sockpath); + + /* Add the port */ + if (port) { + if (STREQ(scheme, "vnc")) { + /* VNC protocol handlers take their port number as + * 'port' - 5900 */ + port -=3D 5900; } - if (!port && !tls_port && !sockpath) - continue; + virBufferAsprintf(&buf, ":%d", port); + } - if (!listen_addr) { - /* The subelement address - - - * *should* have been automatically backfilled into its - * parent (which we just tried to - * retrieve into listen_addr above) but in some cases it - * isn't, so we also do an explicit check for the - * subelement (which, by the way, doesn't exist on libvirt - * < 0.9.4, so we really do need to check both places) - */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "listen/@ad= dress"); - - listen_addr =3D virXPathString(xpath, ctxt); - VIR_FREE(xpath); - } else { - /* If listen_addr is 0.0.0.0 or [::] we should try to parse UR= I and set - * listen_addr based on current URI. */ - if (virSocketAddrParse(&addr, listen_addr, AF_UNSPEC) > 0 && - virSocketAddrIsWildcard(&addr)) { - - virConnectPtr conn =3D ((virshControl *)(ctl->privData))->= conn; - char *uriStr =3D virConnectGetURI(conn); - virURI *uri =3D NULL; - - if (uriStr) { - uri =3D virURIParse(uriStr); - VIR_FREE(uriStr); - } + /* TLS Port */ + if (tls_port) { + virBufferAsprintf(&buf, + "?tls-port=3D%d", + tls_port); + params =3D true; + } - /* It's safe to free the listen_addr even if parsing of URI - * fails, if there is no listen_addr we will print "localh= ost". */ - VIR_FREE(listen_addr); + if (STREQ(scheme, "spice") && passwd) { + virBufferAsprintf(&buf, + "%spassword=3D%s", + params ? "&" : "?", + passwd); + params =3D true; + } - if (uri) { - listen_addr =3D g_strdup(uri->server); - virURIFree(uri); - } - } - } + cleanup: + VIR_FREE(xpath); + VIR_FREE(type_conn); + VIR_FREE(sockpath); + VIR_FREE(passwd); + VIR_FREE(listen_addr); - /* We can query this info for all the graphics types since we'll - * get nothing for the unsupported ones (just rdp for now). - * Also the parameter '--include-password' was already taken - * care of when getting the XML */ + return virBufferContentAndReset(&buf); +} - /* Create our XPATH lookup for the password */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "@passwd"); - /* Attempt to get the password */ - VIR_FREE(passwd); - passwd =3D virXPathString(xpath, ctxt); - VIR_FREE(xpath); +static bool +cmdDomDisplay(vshControl *ctl, const vshCmd *cmd) +{ + g_autoptr(xmlDoc) xml =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + g_autoptr(virshDomain) dom =3D NULL; + bool ret =3D false; + const char *scheme[] =3D { "vnc", "spice", "rdp", NULL }; + const char *type =3D NULL; + int iter =3D 0; + int flags =3D 0; + bool all =3D vshCommandOptBool(cmd, "all"); - /* Build up the full URI, starting with the scheme */ - if (sockpath) - virBufferAsprintf(&buf, "%s+unix://", scheme[iter]); - else - virBufferAsprintf(&buf, "%s://", scheme[iter]); - - /* There is no user, so just append password if there's any */ - if (STREQ(scheme[iter], "vnc") && passwd) - virBufferAsprintf(&buf, ":%s@", passwd); - - /* Then host name or IP */ - if (!listen_addr && !sockpath) - virBufferAddLit(&buf, "localhost"); - else if (!sockpath && strchr(listen_addr, ':')) - virBufferAsprintf(&buf, "[%s]", listen_addr); - else if (sockpath) - virBufferAsprintf(&buf, "%s", sockpath); - else - virBufferAsprintf(&buf, "%s", listen_addr); + VSH_EXCLUSIVE_OPTIONS("all", "type"); - /* Free socket to prepare the pointer for the next iteration */ - VIR_FREE(sockpath); + if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) + return false; - /* Add the port */ - if (port) { - if (STREQ(scheme[iter], "vnc")) { - /* VNC protocol handlers take their port number as - * 'port' - 5900 */ - port -=3D 5900; - } + if (!virDomainIsActive(dom)) { + vshError(ctl, _("Domain is not running")); + goto cleanup; + } - virBufferAsprintf(&buf, ":%d", port); - } + if (vshCommandOptBool(cmd, "include-password")) + flags |=3D VIR_DOMAIN_XML_SECURE; - /* TLS Port */ - if (tls_port) { - virBufferAsprintf(&buf, - "?tls-port=3D%d", - tls_port); - params =3D true; - } + if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0) + goto cleanup; - if (STREQ(scheme[iter], "spice") && passwd) { - virBufferAsprintf(&buf, - "%spassword=3D%s", - params ? "&" : "?", - passwd); - params =3D true; - } + if (virshDomainGetXMLFromDom(ctl, dom, flags, &xml, &ctxt) < 0) + goto cleanup; - /* Print out our full URI */ - VIR_FREE(output); - output =3D virBufferContentAndReset(&buf); - vshPrint(ctl, "%s", output); + /* Attempt to grab our display info */ + for (iter =3D 0; scheme[iter] !=3D NULL; iter++) { + g_autofree char *display =3D NULL; + + /* Particular scheme requested */ + if (!all && type && STRNEQ(type, scheme[iter])) + continue; + + if (!(display =3D virshGetOneDisplay(ctl, scheme[iter], ctxt))) + continue; + + vshPrint(ctl, "%s", display); /* We got what we came for so return successfully */ ret =3D true; @@ -11884,12 +11904,6 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd) } cleanup: - VIR_FREE(xpath); - VIR_FREE(type_conn); - VIR_FREE(sockpath); - VIR_FREE(passwd); - VIR_FREE(listen_addr); - VIR_FREE(output); return ret; } --=20 2.35.1