From nobody Mon Feb 9 16:44:59 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=1646229362; cv=none; d=zohomail.com; s=zohoarc; b=Ow82pgQznzW5U2jDS7X/Lp9XuQ4XQ00lHABxjp8zK7df3WModojCf02/Sse/761mQq0zZUOh+fUeGFuRfgEM5e/u/0Qs+lSiPprRKJa4GFfbZDAXWUrsDYD9j1kR0ZWxe5t2K2lmsTLdsHXDBkhzmckfs7U+KmOWpbB/byq4v0U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646229362; 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=udRnoCzaKGXqpPOzec3c76egN7oqSzP3Y3XQyxX6W+A=; b=kzXnuDkH8XPXe7xgzoMecwJ3zgjSPsyerpu45ixoY1ic88/f6/zvJGDGR0kH5t96YIrBFTYr5HZU8MDabo3e2yXJADE4JqQKK62tZ4WBSfA9MpQOKKrf7eT1S188Bwo5FumeqKmWqueyvFrqf8tuIH5U2j5NNJXAGFJMHcnJWfU= 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 1646229362142272.22411407145546; Wed, 2 Mar 2022 05:56:02 -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-320-4YAMuehEP1eZVSJC6aewdA-1; Wed, 02 Mar 2022 08:55:58 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3A9F0521C; Wed, 2 Mar 2022 13:55:53 +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 1BE4583782; Wed, 2 Mar 2022 13:55:53 +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 E2CAA1808800; Wed, 2 Mar 2022 13:55:52 +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 222Dtkqf017486 for ; Wed, 2 Mar 2022 08:55:46 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6B41D17B7D; Wed, 2 Mar 2022 13:55:46 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id D2C2783780 for ; Wed, 2 Mar 2022 13:55:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646229361; 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=udRnoCzaKGXqpPOzec3c76egN7oqSzP3Y3XQyxX6W+A=; b=CAuJBtwpF4xL3VSp8qHQUCLddUMpmvjyKditQfJICAtToQs3lRct097U0phpDv67VTiLm/ 0nmbDzc/w1KbELJ6jRQLIJEJr22eqJPH0rfI5bv8cpKb8sIWPf+gXJbJskZ3wHt+Pzw3qJ Scfl9MI9DDLZHE3jaOqbk/ydWBnGI24= X-MC-Unique: 4YAMuehEP1eZVSJC6aewdA-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 10/23] virsh: cmdEvent: Rewrite questionable event registration Date: Wed, 2 Mar 2022 14:55:09 +0100 Message-Id: <22e85eb216bb78599f1436e8e7ecc7471f877b03.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.15 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: 1646229364519100008 Content-Type: text/plain; charset="utf-8" The code registering the event handlers in 'cmdEvent' had too many blocks of code conditional on whether just one event is being listened to or all events. The code can be greatly simplified by uniting the code paths and having only one branch when filling the list of events we want to listen for. Signed-off-by: Peter Krempa --- tools/virsh-domain-event.c | 70 +++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/tools/virsh-domain-event.c b/tools/virsh-domain-event.c index 51571dffad..1a2f1cb6e0 100644 --- a/tools/virsh-domain-event.c +++ b/tools/virsh-domain-event.c @@ -274,6 +274,7 @@ typedef struct virshDomainEventCallback virshDomainEven= tCallback; struct virshDomEventData { vshControl *ctl; + int event; bool loop; int *count; bool timestamp; @@ -885,10 +886,10 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) g_autoptr(virshDomain) dom =3D NULL; bool ret =3D false; int timeout =3D 0; - virshDomEventData *data =3D NULL; + g_autofree virshDomEventData *data =3D NULL; + size_t ndata =3D 0; size_t i; const char *eventName =3D NULL; - int event =3D -1; bool all =3D vshCommandOptBool(cmd, "all"); bool loop =3D vshCommandOptBool(cmd, "loop"); bool timestamp =3D vshCommandOptBool(cmd, "timestamp"); @@ -900,59 +901,59 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) VSH_EXCLUSIVE_OPTIONS("list", "event"); if (vshCommandOptBool(cmd, "list")) { - for (event =3D 0; event < VIR_DOMAIN_EVENT_ID_LAST; event++) - vshPrint(ctl, "%s\n", virshDomainEventCallbacks[event].name); + for (i =3D 0; i < G_N_ELEMENTS(virshDomainEventCallbacks); i++) + vshPrint(ctl, "%s\n", virshDomainEventCallbacks[i].name); return true; } if (vshCommandOptStringReq(ctl, cmd, "event", &eventName) < 0) return false; - if (eventName) { - for (event =3D 0; event < VIR_DOMAIN_EVENT_ID_LAST; event++) - if (STREQ(eventName, virshDomainEventCallbacks[event].name)) - break; - if (event =3D=3D VIR_DOMAIN_EVENT_ID_LAST) { - vshError(ctl, _("unknown event type %s"), eventName); - return false; - } - } else if (!all) { + + if (!eventName && !all) { vshError(ctl, "%s", _("one of --list, --all, or --event is required")); return false; } - if (all) { - data =3D g_new0(virshDomEventData, VIR_DOMAIN_EVENT_ID_LAST); - for (i =3D 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) { - data[i].ctl =3D ctl; - data[i].loop =3D loop; - data[i].count =3D &count; - data[i].timestamp =3D timestamp; - data[i].cb =3D &virshDomainEventCallbacks[i]; - data[i].id =3D -1; - } - } else { + if (eventName) data =3D g_new0(virshDomEventData, 1); - data[0].ctl =3D ctl; - data[0].loop =3D vshCommandOptBool(cmd, "loop"); - data[0].count =3D &count; - data[0].timestamp =3D timestamp; - data[0].cb =3D &virshDomainEventCallbacks[event]; - data[0].id =3D -1; + else + data =3D g_new0(virshDomEventData, G_N_ELEMENTS(virshDomainEventCa= llbacks)); + + for (i =3D 0; i < G_N_ELEMENTS(virshDomainEventCallbacks); i++) { + if (eventName && + STRNEQ(eventName, virshDomainEventCallbacks[i].name)) + continue; + + data[i].event =3D i; + data[i].ctl =3D ctl; + data[i].loop =3D loop; + data[i].count =3D &count; + data[i].timestamp =3D timestamp; + data[i].cb =3D &virshDomainEventCallbacks[i]; + data[i].id =3D -1; + ndata++; } + + if (ndata =3D=3D 0) { + vshError(ctl, _("unknown event type %s"), eventName); + return false; + } + if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0) goto cleanup; - if (vshCommandOptBool(cmd, "domain")) + if (vshCommandOptBool(cmd, "domain")) { if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) goto cleanup; + } if (vshEventStart(ctl, timeout) < 0) goto cleanup; - for (i =3D 0; i < (all ? VIR_DOMAIN_EVENT_ID_LAST : 1); i++) { + for (i =3D 0; i < ndata; i++) { if ((data[i].id =3D virConnectDomainEventRegisterAny(priv->conn, d= om, - all ? i : event, + data[i].event, data[i].cb->cb, &data[i], NULL)) < 0) { @@ -985,12 +986,11 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) cleanup: vshEventCleanup(ctl); if (data) { - for (i =3D 0; i < (all ? VIR_DOMAIN_EVENT_ID_LAST : 1); i++) { + for (i =3D 0; i < ndata; i++) { if (data[i].id >=3D 0 && virConnectDomainEventDeregisterAny(priv->conn, data[i].id)= < 0) ret =3D false; } - VIR_FREE(data); } return ret; } --=20 2.35.1