From nobody Tue Feb 10 02:44:03 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=1640202348; cv=none; d=zohomail.com; s=zohoarc; b=nWE2wYjCBRkuZFeZzF0z21MHcEpo9cIbYUqY6ZxJd4ZfluhQYhisrk1eQA7wtLLMEZd02Ch7gLIJDg86oscz2Ne4JKqEZ+dOMRdz50fzL1j54cKlAOgv3Pi+Iw6OoBN8VVjfjw7+MVtrTvDqy7e1ZdKCyXhtCe1+pdpdZjB0KZo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1640202348; h=Content-Type:Content-Transfer-Encoding:Cc: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=vVJTeG1mk9rbvCLR9O5mHej+FtUaCbTBa/35SDrKgys=; b=iGIhKNQm0epaGGicK9mscsqY2v2Vx1w1ilzHC/xBiQjo+FufnUjzFZMSwtwdsTXKdnpBFLnDWG2Ag3a14Xyva97nitOLNuaebV/oCrl9PKiPqXioV2i4CpXT2T+C1p07p8IMVt1cuR4l5GVaP8JF9Bt7Zo0oJO/UXy1iKzpeSg0= 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 1640202348872830.1739619501759; Wed, 22 Dec 2021 11:45:48 -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-328-5hwlTmlkOmaCTOPfs6FHPA-1; Wed, 22 Dec 2021 14:45:43 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 10CBC801B0C; Wed, 22 Dec 2021 19:45:39 +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 E78E76BC9A; Wed, 22 Dec 2021 19:45:38 +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 BB7421806D1D; Wed, 22 Dec 2021 19:45:38 +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 1BMJjbsg019061 for ; Wed, 22 Dec 2021 14:45:37 -0500 Received: by smtp.corp.redhat.com (Postfix) id 5A6E474E8A; Wed, 22 Dec 2021 19:45:37 +0000 (UTC) Received: from localhost (unknown [10.39.208.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F99C747B5; Wed, 22 Dec 2021 19:45:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1640202347; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=vVJTeG1mk9rbvCLR9O5mHej+FtUaCbTBa/35SDrKgys=; b=dTiaGIfabUlC3Zo8i7z+AskuIURdKGYgDDHOYwzhU27C3MsMzQgGr+MWoPMFsdDivgCKso hRpM9xzZEkaSXwdxWGZ5sN2y6M4onv74HnFvRNhmvv/Z0ZIXtatwCU5lvz48cMzUXuSaIl vwicJgvwjCMMscRqINVKbOws85cz9IE= X-MC-Unique: 5hwlTmlkOmaCTOPfs6FHPA-1 From: marcandre.lureau@redhat.com To: libvir-list@redhat.com Subject: [libvirt PATCH v3 07/16] virsh: refactor/split cmdDomDisplay() Date: Wed, 22 Dec 2021 23:43:36 +0400 Message-Id: <20211222194345.766352-8-marcandre.lureau@redhat.com> In-Reply-To: <20211222194345.766352-1-marcandre.lureau@redhat.com> References: <20211222194345.766352-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= 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.12 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1640202349960100001 From: Marc-Andr=C3=A9 Lureau The function is large and quite complex. Move the inner loop for each scheme in a separate function cmdDomDisplayScheme(). Signed-off-by: Marc-Andr=C3=A9 Lureau --- tools/virsh-domain.c | 341 +++++++++++++++++++++---------------------- 1 file changed, 169 insertions(+), 172 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index f086c2dd4b58..002cfc4be6af 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -11507,209 +11507,213 @@ static const vshCmdOptDef opts_domdisplay[] =3D= { }; =20 static bool -cmdDomDisplay(vshControl *ctl, const vshCmd *cmd) +cmdDomDisplayScheme(vshControl *ctl, const char *scheme, + xmlXPathContext *ctxt, virBuffer *buf) { - g_autoptr(xmlDoc) xml =3D NULL; - g_autoptr(xmlXPathContext) ctxt =3D NULL; - g_autoptr(virshDomain) dom =3D NULL; - 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; - 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"); + g_autofree char *xpath =3D NULL; + g_autofree char *listen_addr =3D NULL; + g_autofree char *type_conn =3D NULL; + g_autofree char *sockpath =3D NULL; + g_autofree char *passwd =3D NULL; const char *xpath_fmt =3D "string(/domain/devices/graphics[@type=3D'%s= ']/%s)"; virSocketAddr addr; + int port, tls_port =3D 0; + bool params =3D false; + int tmp; =20 - VSH_EXCLUSIVE_OPTIONS("all", "type"); + /* Create our XPATH lookup for the current display's port */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "@port"); =20 - 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); =20 - 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; =20 - 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"); =20 - 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; =20 - 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"); =20 - /* 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 */ + listen_addr =3D virXPathString(xpath, ctxt); + VIR_FREE(xpath); =20 - /* 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"); =20 - /* 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 */ + type_conn =3D virXPathString(xpath, ctxt); + VIR_FREE(xpath); =20 - /* 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"); =20 - /* 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); =20 - /* 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); + } + } =20 - /* Create our XPATH lookup for the current display's address */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "@listen"); + if (!port && !tls_port && !sockpath) + return false; + + 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"); =20 - /* 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); + } =20 - /* 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); =20 - /* 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); + } + } + } =20 - 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 */ =20 - sockpath =3D virXPathString(xpath, ctxt); + /* Create our XPATH lookup for the password */ + xpath =3D g_strdup_printf(xpath_fmt, scheme, "@passwd"); =20 - VIR_FREE(xpath); - } + /* Attempt to get the password */ + 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); + + /* Add the port */ + if (port) { + if (STREQ(scheme, "vnc")) { + /* VNC protocol handlers take their port number as + * 'port' - 5900 */ + port -=3D 5900; } =20 - if (!port && !tls_port && !sockpath) - continue; + virBufferAsprintf(buf, ":%d", port); + } =20 - 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; + } =20 - /* 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; + } =20 - if (uri) { - listen_addr =3D g_strdup(uri->server); - virURIFree(uri); - } - } - } + return true; +} =20 - /* 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 */ +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; + g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + g_autofree char *output =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"); =20 - /* Create our XPATH lookup for the password */ - xpath =3D g_strdup_printf(xpath_fmt, scheme[iter], "@passwd"); + VSH_EXCLUSIVE_OPTIONS("all", "type"); =20 - /* Attempt to get the password */ - VIR_FREE(passwd); - passwd =3D virXPathString(xpath, ctxt); - VIR_FREE(xpath); + if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) + return false; =20 - /* 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); + if (!virDomainIsActive(dom)) { + vshError(ctl, _("Domain is not running")); + return false; + } =20 - /* Free socket to prepare the pointer for the next iteration */ - VIR_FREE(sockpath); + if (vshCommandOptBool(cmd, "include-password")) + flags |=3D VIR_DOMAIN_XML_SECURE; =20 - /* Add the port */ - if (port) { - if (STREQ(scheme[iter], "vnc")) { - /* VNC protocol handlers take their port number as - * 'port' - 5900 */ - port -=3D 5900; - } + if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0) + return false; =20 - virBufferAsprintf(&buf, ":%d", port); - } + if (virshDomainGetXMLFromDom(ctl, dom, flags, &xml, &ctxt) < 0) + return false; =20 - /* TLS Port */ - if (tls_port) { - virBufferAsprintf(&buf, - "?tls-port=3D%d", - tls_port); - params =3D true; - } + /* 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; =20 - if (STREQ(scheme[iter], "spice") && passwd) { - virBufferAsprintf(&buf, - "%spassword=3D%s", - params ? "&" : "?", - passwd); - params =3D true; - } + if (!cmdDomDisplayScheme(ctl, scheme[iter], ctxt, &buf)) + continue; =20 /* Print out our full URI */ VIR_FREE(output); @@ -11730,13 +11734,6 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd) vshError(ctl, _("No graphical display found")); } =20 - cleanup: - VIR_FREE(xpath); - VIR_FREE(type_conn); - VIR_FREE(sockpath); - VIR_FREE(passwd); - VIR_FREE(listen_addr); - VIR_FREE(output); return ret; } =20 --=20 2.34.1.8.g35151cf07204