From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111084; cv=none; d=zohomail.com; s=zohoarc; b=EwW+K40S80l2GC9WX0EX1muFrJfNIt0aQ+6DP7myGUqPqFevI/lij4t2aW3CclcFkBbD7PjuU0BQi8Xio4N4VM/DFafGQk6pIWTZ/vwXw1lvCoN3NuR3AzqYz2O0IzhbBePrd/4+Dv7GnoOR6ha0LHH2VLUmijXERLV9xAY/IOc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111084; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=tZrqi637sS4Wn4OrWWtKnNMH6bxJrJXSOQbGKDRZJag=; b=lcCqtWQu4tyEKBgNdv67QVv7RLlVUCM2JMIYzYQfEO9dM+5mor9vrLD+jJgKTVUNmhjaZaP+QE84oaQilJ8xE27m0Z+nJ9xQrfDLYlROQOJD3bdUAvEUu1uH3pOfp/+vpyY6aofAySyKcbfV9xIpKqDgHdTADni0OmGoHxAlH6I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1749111084928138.96226737555935; Thu, 5 Jun 2025 01:11:24 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id D3115E75; Thu, 5 Jun 2025 04:11:23 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 1A352BF7; Thu, 5 Jun 2025 04:10:58 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id F2731939; Thu, 5 Jun 2025 04:10:54 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 56D7810A for ; Thu, 5 Jun 2025 04:10:54 -0400 (EDT) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-388-nCrNdoBOPvK38cf0SpIyXQ-1; Thu, 05 Jun 2025 04:10:52 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3737318002BB for ; Thu, 5 Jun 2025 08:10:51 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 65DDF18002A5 for ; Thu, 5 Jun 2025 08:10:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111053; h=from:from: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; bh=6MMsGLjq/37OYu7uvT9mz+1uv1mrt5E2FKgdwC3Lfqk=; b=DQhxoH4lXVMSXwu2psoITRECCL+9tgAAyVfkOw+HTQVXvGgbWvIKXtOoOGe80pP8xLN4gm QIdelLqFr4gRYVt1u9j/X/wbvdjnfe3WcUZfEnz59dXHFTJs28hR1d0SsHXkbPuty0rbA9 bqZ1E2d+K+bPIV11jaKIIzTtxgCWyz8= X-MC-Unique: nCrNdoBOPvK38cf0SpIyXQ-1 X-Mimecast-MFC-AGG-ID: nCrNdoBOPvK38cf0SpIyXQ_1749111051 To: devel@lists.libvirt.org Subject: [PATCH 1/7] virthread: Register auto cleanup function for virMutex Date: Thu, 5 Jun 2025 10:10:41 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Rn6FKOOuZyNbmRHvx9ltk5SQe4iCsqq38N0FiaV1WOI_1749111051 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: SF7V6E4YKTJTMCEIZFLTI5Q43W5B7ISS X-Message-ID-Hash: SF7V6E4YKTJTMCEIZFLTI5Q43W5B7ISS X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111087585116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa This makes it very convenient to declare a mutex on stack along with the VIR_MUTEX_INITIALIZE macro without the need to have complex cleanup. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/util/virthread.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util/virthread.h b/src/util/virthread.h index 23abe0b6c9..a7ec6bf32c 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -121,6 +121,7 @@ int virOnce(virOnceControl *once, virOnceFunc init) int virMutexInit(virMutex *m) G_GNUC_WARN_UNUSED_RESULT; int virMutexInitRecursive(virMutex *m) G_GNUC_WARN_UNUSED_RESULT; void virMutexDestroy(virMutex *m); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virMutex, virMutexDestroy); void virMutexLock(virMutex *m); void virMutexUnlock(virMutex *m); --=20 2.49.0 From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111131; cv=none; d=zohomail.com; s=zohoarc; b=E8Sz+91RObJJkHPWRan2wS7vFJZbieRaKsRtsAcEZY/ooex7VOiC70x2HEV8BvgxUM3mFdUFqf4wzlzn7SRHmP09tQOkSs8Mab+EJEXJ73Yf019sb7LGymZmJgNyr+0BGu5Dlb7IOB1ITdJGFMcoV2C6Y/0M2Ak/VMsvV3PKqZs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111131; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=nq5OdzKhBFOIm+/LXXZhZ804/dHTLBGbCwh5+zPwOd8=; b=Cu9EJAord8Tf2bjuEWxRtBww5k5egekYM2+43EE7KK+5rUK+n/oHW3h4j6CBkr+iBJEDpP5y0xuMzoB1qD1GWyob+suGX/ukL+2vfyJcxHaiPmkBXS328OWgkxI6iMk+YIRSxUQpFlmLvI3vIikLPOdX5dToVmE5ygo7kRJIX4Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 174911113136078.91279274358908; Thu, 5 Jun 2025 01:12:11 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 6BD2813FF; Thu, 5 Jun 2025 04:12:10 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id EDF5813EA; Thu, 5 Jun 2025 04:11:03 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 80D63608; Thu, 5 Jun 2025 04:10:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 1FF98448 for ; Thu, 5 Jun 2025 04:10:55 -0400 (EDT) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-465-vUxkNtl3PcCO4h8aThDmHw-1; Thu, 05 Jun 2025 04:10:53 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 924E418002B4 for ; Thu, 5 Jun 2025 08:10:52 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C234C18002A5 for ; Thu, 5 Jun 2025 08:10:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111054; h=from:from: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; bh=VpRl85hkGFQct+4mBRanfYXnsyEK/yPWrHX5iYRrScg=; b=WNNrQjS8yYBsbV+1fDtqZmz38DmXemEyAgJN+Tj2OVVpCmTF3QSxOCTgRSjtDaY3fH3RAb AvJhC5Oa8la+H3y2RASRSnHp5Z0paEqmdeJ+RRqbfefVa2LHyZ3d2XRyOfd3y7CXXnB/1b vjwlYn5bmwC8qYLN+rUruUe20NM8+VM= X-MC-Unique: vUxkNtl3PcCO4h8aThDmHw-1 X-Mimecast-MFC-AGG-ID: vUxkNtl3PcCO4h8aThDmHw_1749111052 To: devel@lists.libvirt.org Subject: [PATCH 2/7] virthread: Add infrastructure for static virCond definitions Date: Thu, 5 Jun 2025 10:10:42 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: W-llF79cdJOlqvjoBAl2KjYqrRQV4K_SR9vI69fb0Sc_1749111052 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: XGSHTFYV3Q5QBDCBHBPQRKSSYGCK7MZI X-Message-ID-Hash: XGSHTFYV3Q5QBDCBHBPQRKSSYGCK7MZI X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111133285116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa Add the automatic cleanup handler as well as static initializer to allow simple stack allocated conditions. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/util/virthread.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/util/virthread.h b/src/util/virthread.h index a7ec6bf32c..9e4e8cdafe 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -73,6 +73,11 @@ struct virOnceControl { .once =3D PTHREAD_ONCE_INIT \ } +#define VIR_COND_INITIALIZER \ + { \ + .cond =3D PTHREAD_COND_INITIALIZER \ + } + typedef void (*virOnceFunc)(void); typedef void (*virThreadFunc)(void *opaque); @@ -142,6 +147,7 @@ void virRWLockUnlock(virRWLock *m); int virCondInit(virCond *c) G_GNUC_WARN_UNUSED_RESULT; int virCondDestroy(virCond *c); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virCond, virCondDestroy); /* virCondWait, virCondWaitUntil: * These functions can return without the associated predicate --=20 2.49.0 From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111168; cv=none; d=zohomail.com; s=zohoarc; b=Es8uRFo8vPUtYZUUkGqUqR24dzMAOu2Ck4XqH9m11TWgmuHeDmi0XrSYGNC0ZqcNxBE2RJeYL/4o8O/yVZBEWrMXrRwo0CnGnaKVu8+4M4ULcWpuhm2ym5cqawzU/Rl5Wo7z3vacytfGxrmHYgY4XpPL3TXivj6FqDvyda7X7v8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111168; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=hRtHb5xfmOMbW36LH4lOLbnEQBRuVTWkApLbtl2GonE=; b=aqNWQP+LU/SH8zR+F0Ai7xhUabrDPE7XMKPqdBqOXhQnJQQoeZMSaSjEkb1RvsueftmZKgCEnR3zwZ1gCfWdrAcP0GHjbllVavJjeWGVftZTixGha8NQsxbEaQf55Q2LxvYEmvuZfV7yQxTaeebIuCC266ohXYsud9L9u7yxPls= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1749111168540376.16305212809493; Thu, 5 Jun 2025 01:12:48 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 49F6811E1; Thu, 5 Jun 2025 04:12:47 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 1204B1435; Thu, 5 Jun 2025 04:11:06 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id D1127A11; Thu, 5 Jun 2025 04:10:56 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 4B5CEA02 for ; Thu, 5 Jun 2025 04:10:56 -0400 (EDT) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-587-CBstnv0yPfW14xsfteEDwA-1; Thu, 05 Jun 2025 04:10:54 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E2A1318001D6 for ; Thu, 5 Jun 2025 08:10:53 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2875018003FC for ; Thu, 5 Jun 2025 08:10:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111056; h=from:from: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; bh=iRs2YrEDYAB9+AHzxq1lVyOKkDmew+MvMKDwyp3//pk=; b=QsYib9gLQT3sVg1E6CsGA2B2Uj+B9x4lDnN4w6+GsU7dkjuZSha1MLoorMFWi5itqYWEBT /yyGrvzmPdb35FQIusFyAgIitC87CBVv91wtCGdOtA0M2PTa5Z0Rllm5aIvbjWCtnMnBzt qEe0xLFunxOhckDcNXD8HPDz8ipdIwM= X-MC-Unique: CBstnv0yPfW14xsfteEDwA-1 X-Mimecast-MFC-AGG-ID: CBstnv0yPfW14xsfteEDwA_1749111054 To: devel@lists.libvirt.org Subject: [PATCH 3/7] virsh: cmdEvent: Ensure that event callbacks are unregistered before returning Date: Thu, 5 Jun 2025 10:10:43 +0200 Message-ID: <9589d6d2a674173fcd53dec0aa0f6ab5decd55fc.1749110902.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: AC8eJbAHySAfWxWbGgE9IxZEhP9Xf1QclCgYQ3mRyvQ_1749111054 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JXBCOF72NT545QMUJREOYDTWFLCC3LEK X-Message-ID-Hash: JXBCOF72NT545QMUJREOYDTWFLCC3LEK X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111169927116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa Successful return from 'virConnectDomainEventDeregisterAny' does not guarantee that there aren't still in-progress events being handled by the callbacks. Since 'cmdEvent' passes in a slice from an array as the private data of the callbacks, we must ensure that the array stays in scope (it's auto-freed) for the whole time there are possible callbacks being executed. While in practice this doesn't happen as the callbacks are usually quick enough to finish while unregistering stuff, placing a 'sleep(1)' into e.g. 'virshEventLifecyclePrint' and starting a domain results in crash of virsh with the following backtrace: Program terminated with signal SIGSEGV, Segmentation fault. #0 0x00005557b5cfd343 in virshEventPrintf (data=3Ddata@entry=3D0x5557db96= 19b0, fmt=3Dfmt@entry=3D0x5557b5d5e527 "%s") at ../../../libvirt/tools/virs= h-domain-event.c:252 Thread 2 (Thread 0x7f59a54b7d00 (LWP 2097121)): #0 0x00007f59a6cadbf9 in __futex_abstimed_wait_common () at /lib64/libc.s= o.6 #1 0x00007f59a6cb2cf3 in __pthread_clockjoin_ex () at /lib64/libc.so.6 #2 0x00005557b5cd57f6 in virshDeinit (ctl=3D0x7ffc7b615140) at ../../../l= ibvirt/tools/virsh.c:408 #3 0x00005557b5cd5391 in main (argc=3D, argv=3D) at ../../../libvirt/tools/virsh.c:932 Thread 1 (Thread 0x7f59a51a66c0 (LWP 2097122)): #0 0x00005557b5cfd343 in virshEventPrintf (data=3Ddata@entry=3D0x5557db96= 19b0, fmt=3Dfmt@entry=3D0x5557b5d5e527 "%s") at ../../../libvirt/tools/virs= h-domain-event.c:252 #1 0x00005557b5cffa10 in virshEventPrint (data=3D0x5557db9619b0, buf=3D0x= 7f59a51a55c0) at ../../../libvirt/tools/virsh-domain-event.c:290 #2 virshEventLifecyclePrint (conn=3D, dom=3D, event=3D, detail=3D, opaque=3D0x5557db9619= b0) at ../../../libvirt/ [snipped] From the backtrace you can see that the 'main()' thread is already shutting down virsh, which means that 'cmdEvent' terminated and the private data was freed. The event loop thread is still execing the callback which accesses the data. To fix this add a condition and wait on all of the callbacks to be unregistered first (their private data freeing function will be called). This bug was observed when I've copied the event code for a new virsh command which had a bit more involved callbacks. Fixes: 99fa96c3907 Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain-event.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tools/virsh-domain-event.c b/tools/virsh-domain-event.c index 69a68d857d..9aa21b2e78 100644 --- a/tools/virsh-domain-event.c +++ b/tools/virsh-domain-event.c @@ -237,10 +237,23 @@ struct virshDomEventData { bool timestamp; virshDomainEventCallback *cb; int id; + + virMutex *m; /* needed to signal that handler was unregistered for cle= an shutdown */ + virCond *c; }; typedef struct virshDomEventData virshDomEventData; +static void +virshDomEventDataUnregistered(virshDomEventData *d) +{ + g_auto(virLockGuard) name =3D virLockGuardLock(d->m); + /* signal that the handler was unregistered */ + d->id =3D -1; + virCondSignal(d->c); +} + + static void G_GNUC_PRINTF(2, 3) virshEventPrintf(virshDomEventData *data, const char *fmt, @@ -936,6 +949,8 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) bool timestamp =3D vshCommandOptBool(cmd, "timestamp"); int count =3D 0; virshControl *priv =3D ctl->privData; + g_auto(virMutex) m =3D VIR_MUTEX_INITIALIZER; + g_auto(virCond) c =3D VIR_COND_INITIALIZER; VSH_EXCLUSIVE_OPTIONS("all", "event"); VSH_EXCLUSIVE_OPTIONS("list", "all"); @@ -969,6 +984,8 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) data[ndata].count =3D &count; data[ndata].timestamp =3D timestamp; data[ndata].cb =3D &virshDomainEventCallbacks[i]; + data[ndata].m =3D &m; + data[ndata].c =3D &c; data[ndata].id =3D -1; ndata++; } @@ -994,7 +1011,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) data[i].event, data[i].cb->cb, &data[i], - NULL)) < 0) { + (virFreeCallbac= k) virshDomEventDataUnregistered)) < 0) { /* When registering for all events: if the first * registration succeeds, silently ignore failures on all * later registrations on the assumption that the server @@ -1022,14 +1039,27 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) ret =3D true; cleanup: - vshEventCleanup(ctl); if (data) { for (i =3D 0; i < ndata; i++) { if (data[i].id >=3D 0 && virConnectDomainEventDeregisterAny(priv->conn, data[i].id)= < 0) ret =3D false; } + + virMutexLock(&m); + while (true) { + for (i =3D 0; i < ndata; i++) { + if (data[i].id >=3D 0) + break; + } + + if (i =3D=3D ndata || + virCondWait(&c, &m) < 0) + break; + } + virMutexUnlock(&m); } + vshEventCleanup(ctl); return ret; } --=20 2.49.0 From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111193; cv=none; d=zohomail.com; s=zohoarc; b=UWjvF2y1XtS/kdWa/pY9ejSK2FvvEqDei4O4BlXJdKRr1WM+qnlsrHv1U9kLBR0qceWElfvqRJXz3o3s2jIvGZtN4qTcCg4nG48BANS0qU92NlRuomNX8qltNt8QPMftAfqRR39DKdI9ZXJd8I0ggMd+XYhKu8TD3QBLktw/gzw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111193; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=debTQf++9iJ8yRlmHyggxORbGG3Slh5/N/nTYmpND+A=; b=OvNATHDFPL+OTsDOrjhIfKV67XWpNWl+crBH5ELx0IO05FUJthw7aIe/IvnCFUau4cPZBIJskkz5tTD18WZkQ8I7H5BncA24gv4fJJtqXJx77oXY2EdyxTuxqDvEgRE3SeaZNYmdRgNpz7NrIazFSn8/68T2JuV7dgUlMMqZSZ4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1749111193942811.2155622854247; Thu, 5 Jun 2025 01:13:13 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 025B214F8; Thu, 5 Jun 2025 04:13:12 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id C3FA11452; Thu, 5 Jun 2025 04:11:07 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 45DD4E90; Thu, 5 Jun 2025 04:10:58 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id B0DE1A3E for ; Thu, 5 Jun 2025 04:10:57 -0400 (EDT) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-356-lVE7cXgoOgCbZbtoBLrFuQ-1; Thu, 05 Jun 2025 04:10:56 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 355D61800773 for ; Thu, 5 Jun 2025 08:10:55 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6F99B18002A5 for ; Thu, 5 Jun 2025 08:10:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111057; h=from:from: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; bh=LHiRSv4qusla+e+L5tFhS5qvz3XbmXkNGJYQ42wXYsA=; b=YfJVNUTZmb5gr5nrby1xhogY2ygj/h1wjOuER1QwAI6Tix/g9GAcVJpsLnn+wFtFMaUXVG a76Aj3NbSUAfWpMGS9qdnTIcn9i1HLqNBzy/xikjmctYRl0UyUcvpuKMwRVB8k3Ku0fgOb FGg08rjIaiw46/OKPwP2XSruKmYAy+E= X-MC-Unique: lVE7cXgoOgCbZbtoBLrFuQ-1 X-Mimecast-MFC-AGG-ID: lVE7cXgoOgCbZbtoBLrFuQ_1749111055 To: devel@lists.libvirt.org Subject: [PATCH 4/7] vshCommandRun: Convert to directly return the exit code Date: Thu, 5 Jun 2025 10:10:44 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: uxeoHtb50OTRmeq_iA3XU4eyG7zjawxr4bpv3fZopB0_1749111055 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: TWNI6D3TIRXD4EZYYRB4PPKTEWKRZQOK X-Message-ID-Hash: TWNI6D3TIRXD4EZYYRB4PPKTEWKRZQOK X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111196415116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa Currently the handler functions in the virt shells return only a boolean signalling if the command was successful or not. In preparation for a command which will want to return another value (timeout) convert vshCommand run to actually return the requested exit code instead and document the conversion from boolean. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh.c | 4 ++-- tools/virt-admin.c | 4 ++-- tools/vsh.c | 29 ++++++++++++++++++++--------- tools/vsh.h | 2 +- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 244ca655ee..643ef6b453 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -836,7 +836,7 @@ main(int argc, char **argv) vshControl _ctl =3D { 0 }; vshControl *ctl =3D &_ctl; virshControl virshCtl =3D { 0 }; - bool ret =3D true; + int ret =3D EXIT_SUCCESS; ctl->name =3D "virsh"; /* hardcoded name of the binary */ ctl->env_prefix =3D "VIRSH"; @@ -930,5 +930,5 @@ main(int argc, char **argv) } virshDeinit(ctl); - exit(ret ? EXIT_SUCCESS : EXIT_FAILURE); + exit(ret); } diff --git a/tools/virt-admin.c b/tools/virt-admin.c index b701ed1fe4..ac8f3202b6 100644 --- a/tools/virt-admin.c +++ b/tools/virt-admin.c @@ -1530,7 +1530,7 @@ main(int argc, char **argv) vshControl _ctl =3D { 0 }; vshControl *ctl =3D &_ctl; vshAdmControl virtAdminCtl =3D { 0 }; - bool ret =3D true; + int ret =3D EXIT_SUCCESS; ctl->name =3D "virt-admin"; /* hardcoded name of the binary */ ctl->env_prefix =3D "VIRT_ADMIN"; @@ -1612,5 +1612,5 @@ main(int argc, char **argv) } vshAdmDeinit(ctl); - exit(ret ? EXIT_SUCCESS : EXIT_FAILURE); + exit(ret); } diff --git a/tools/vsh.c b/tools/vsh.c index e892cbca22..497b7ec631 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -1341,14 +1341,22 @@ vshBlockJobOptionBandwidth(vshControl *ctl, } -/* - * Executes command(s) and returns return code from last command +/** + * vshCommandRun: + * @ctl: virt shell data + * @cmd: command to execute + * + * Returns return code from last command. Return values from command handl= ers + * which return boolean are converted as: + * true -> EXIT_SUCCESS + * false -> EXIT_FAILURE */ -bool -vshCommandRun(vshControl *ctl, const vshCmd *cmd) +int +vshCommandRun(vshControl *ctl, + const vshCmd *cmd) { const vshClientHooks *hooks =3D ctl->hooks; - bool ret =3D true; + int ret =3D EXIT_SUCCESS; while (cmd) { gint64 before, after; @@ -1358,16 +1366,19 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd) if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) || (hooks && hooks->connHandler && hooks->connHandler(ctl))) { - ret =3D cmd->def->handler(ctl, cmd); + if (cmd->def->handler(ctl, cmd)) + ret =3D EXIT_SUCCESS; + else + ret =3D EXIT_FAILURE; } else { /* connection is not usable, return error */ - ret =3D false; + ret =3D EXIT_FAILURE; } after =3D g_get_real_time(); /* try to automatically catch disconnections */ - if (!ret && + if (ret !=3D EXIT_SUCCESS && ((last_error !=3D NULL) && (((last_error->code =3D=3D VIR_ERR_SYSTEM_ERROR) && (last_error->domain =3D=3D VIR_FROM_REMOTE)) || @@ -1376,7 +1387,7 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd) (last_error->code =3D=3D VIR_ERR_INVALID_CONN)))) disconnected++; - if (!ret) + if (ret !=3D EXIT_SUCCESS) vshReportError(ctl); if (STREQ(cmd->def->name, "quit") || diff --git a/tools/vsh.h b/tools/vsh.h index 3b75216e11..284da36e32 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -287,7 +287,7 @@ int vshBlockJobOptionBandwidth(vshControl *ctl, bool bytes, unsigned long *bandwidth); bool vshCommandOptBool(const vshCmd *cmd, const char *name); -bool vshCommandRun(vshControl *ctl, const vshCmd *cmd); +int vshCommandRun(vshControl *ctl, const vshCmd *cmd); bool vshCommandStringParse(vshControl *ctl, char *cmdstr, vshCmd **partial); --=20 2.49.0 From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111255; cv=none; d=zohomail.com; s=zohoarc; b=VsCpeaPbbOXH6OdNO7+Ek+TvnSobcw8ycmzSACu+qkRwAXIXr8yjIRJfWNn2V7cBYKJnRViFK41zkEYpqrEVDJNUSagcRBXrmvZTk2pZ7qqDlZJUmm8vd8pzfth5NFbHdRQ4jkD4vHkpDgG5+EqhnhV/gNFVCJJuIUqkr8mvd5w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111255; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=ZgcSEYXtdByDw4tC/tDsZ/uSsRpoEHFEf3t2sVmBKhM=; b=ciHvwVCfqz2sj3OZO6yxxLTQbpm21bbiQ/JU6mKjNNOpyUz3cFrnp/S60p6WXi9wbrFH9W3rhiQ62ZsMymBMCUmUQaHPeo6NiskZf0ONnlacdmr45eiv8kRvCNr6+dhFXqcsyfCSJgnDT6BanUb2rolE7HG0RNDN3pKQ+4gOZgA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1749111255882484.8234827424808; Thu, 5 Jun 2025 01:14:15 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id CD56411CB; Thu, 5 Jun 2025 04:14:14 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 5FF27C04; Thu, 5 Jun 2025 04:11:22 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id CAD1911CF; Thu, 5 Jun 2025 04:11:17 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 333AF137C for ; Thu, 5 Jun 2025 04:11:01 -0400 (EDT) Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-659-sKfJ3e5YNSet4uJ_S4v_4w-1; Thu, 05 Jun 2025 04:10:57 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7179B1955F54 for ; Thu, 5 Jun 2025 08:10:56 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AB29A18003FC for ; Thu, 5 Jun 2025 08:10:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111060; h=from:from: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; bh=pmNxS81lEbO4Wr/tcVebLI0fKR3Z+8TIPYoJkzaI/CY=; b=IMcQwiwDhOWWBiD0uAeeI5RsybAqnQ1Gn3ECrw/ml1B6MVVnuQyqDf3IllJSYqf6xsACLp zOFslc2/ZQuMtPPT0za4RY1rEHMJtAzUdGdCSfydcG2DO50+/CI6T8N9W1idwi3ZEUvSSu TbqCC1nCDN+nS1BJaLi31o5XdQYlg0Y= X-MC-Unique: sKfJ3e5YNSet4uJ_S4v_4w-1 X-Mimecast-MFC-AGG-ID: sKfJ3e5YNSet4uJ_S4v_4w_1749111056 To: devel@lists.libvirt.org Subject: [PATCH 5/7] vsh: Add support for commands with more return values Date: Thu, 5 Jun 2025 10:10:45 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Z7cdS95LGyQpEt-VQ5dVcjKMUJ7ZO7utuSwxiUTjtq4_1749111056 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: YWKHHYASC4SYZHWMXPZHLSDQ7ZTXGM6L X-Message-ID-Hash: YWKHHYASC4SYZHWMXPZHLSDQ7ZTXGM6L X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111257081116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa Add a new handler callback for command handlers which will want to return more than just EXIT_SUCCESS/EXIT_FAILURE. The new handler allows returning integers. Any negative values are converted to EXIT_FAILURE, other values are returned as reported in cases where we forward the command state (non-interactive usage) as return value of the virt shell program. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/vsh.c | 25 ++++++++++++++++++++----- tools/vsh.h | 1 + 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index 497b7ec631..4aacc5feac 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -293,7 +293,7 @@ vshCmddefCheckInternals(vshControl *ctl, return -1; } - if (cmd->handler) { + if (cmd->handler || cmd->handler_rv) { vshError(ctl, _("command '%1$s' has handler set"), cmd->name); return -1; } @@ -323,6 +323,11 @@ vshCmddefCheckInternals(vshControl *ctl, return -1; } + if (!!cmd->handler + !!cmd->handler_rv !=3D 1) { + vshError(ctl, _("command '%1$s' must have exactly one of the handl= er callbacks set"), cmd->name); + return -1; + } + if (!cmd->opts) return 0; @@ -1350,6 +1355,9 @@ vshBlockJobOptionBandwidth(vshControl *ctl, * which return boolean are converted as: * true -> EXIT_SUCCESS * false -> EXIT_FAILURE + * Return values from command handlers returning integers are converted as: + * '< 0' -> EXIT_FAILURE + * others -> use value returned by handler callback. */ int vshCommandRun(vshControl *ctl, @@ -1366,10 +1374,17 @@ vshCommandRun(vshControl *ctl, if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) || (hooks && hooks->connHandler && hooks->connHandler(ctl))) { - if (cmd->def->handler(ctl, cmd)) - ret =3D EXIT_SUCCESS; - else - ret =3D EXIT_FAILURE; + if (cmd->def->handler_rv) { + ret =3D cmd->def->handler_rv(ctl, cmd); + + if (ret < 0) + ret =3D EXIT_FAILURE; + } else { + if (cmd->def->handler(ctl, cmd)) + ret =3D EXIT_SUCCESS; + else + ret =3D EXIT_FAILURE; + } } else { /* connection is not usable, return error */ ret =3D EXIT_FAILURE; diff --git a/tools/vsh.h b/tools/vsh.h index 284da36e32..bd2494e899 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -167,6 +167,7 @@ enum { struct _vshCmdDef { const char *name; /* name of command, or NULL for list end */ bool (*handler) (vshControl *, const vshCmd *); /* command handler = */ + int (*handler_rv) (vshControl *, const vshCmd *); /* command handler -= allows return codes */ const vshCmdOptDef *opts; /* definition of command options */ const vshCmdInfo *info; /* details about command */ unsigned int flags; /* bitwise OR of VSH_CMD_FLAG */ --=20 2.49.0 From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111226; cv=none; d=zohomail.com; s=zohoarc; b=BraFSUCY0FRzf4GlWdthza3tTM3/4dIGKDdBzFZ+MBdWSepEjZcfZ/oRnCA59h8BEGk7ijMEneCGMnO2SXLP3B+nfakqCQBswjk2nnX8ktMXi5TQGFyEQmIurkvO1C6HOgwftkAapInrD/XC6MthQOWyfoAkg6bvAIy3xhBbZoo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111226; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=zBemTTqQjGWc2FWT0+KRKgYhsY9BI9N9SBmKH0tVZO4=; b=I/TETTuSy9sIXr/+0AJuwj/C9Z3b7bmXxexP2eCK4uG63/lEXaiD0DQx/INQAy9YKjqt/GuNWMyZLOVIHRJtHuswaQ0f5UZo70FTB0sGjPtJ8Y68DBa1/3LnmhNbM5R5Zazc+2fwi+oQaeEokvmk9hBI2wsbNnRWjBTTE5JsY/Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1749111226302261.0653551351635; Thu, 5 Jun 2025 01:13:46 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 4AC751325; Thu, 5 Jun 2025 04:13:45 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 05810149D; Thu, 5 Jun 2025 04:11:17 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id DB3AE1322; Thu, 5 Jun 2025 04:11:12 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 38EFC127F for ; Thu, 5 Jun 2025 04:11:00 -0400 (EDT) Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-8he8fv-CMiKNWo4JE7CRSg-1; Thu, 05 Jun 2025 04:10:58 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B84C91955F41 for ; Thu, 5 Jun 2025 08:10:57 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F199118003FC for ; Thu, 5 Jun 2025 08:10:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111059; h=from:from: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; bh=oZvHT1nFKRm/eDcDdb/sNekrd7QgNolTtwuHiPDaaQY=; b=MQErnFPp4XKRpCiCVXn00Dvc20j4fL/1rSh3UkvXFY+KKBjtgUCXsjlG4kyw9MHlugXJ9z lvKN7/dWE3RqMoDdaD84GwmGCgBZ+V4fGyCqi3tG01E0yByBftQ61l4L016we63lvj5o7u Bet30pQTHWYTMrWw4FU/+qnfhs3hts4= X-MC-Unique: 8he8fv-CMiKNWo4JE7CRSg-1 X-Mimecast-MFC-AGG-ID: 8he8fv-CMiKNWo4JE7CRSg_1749111057 To: devel@lists.libvirt.org Subject: [PATCH 6/7] virsh: Introduce 'await' command for waiting until target domain state is reached Date: Thu, 5 Jun 2025 10:10:46 +0200 Message-ID: <0b3da6419b31d8a297c7849b39808b387d7b1fd4.1749110902.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ddZqpzTzXCXEfrke1K-QEOXnpTpj90PB83aHIvW86QY_1749111057 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: DPGU74H5RJRGTFHCDY6BTIJG4KRTRIEA X-Message-ID-Hash: DPGU74H5RJRGTFHCDY6BTIJG4KRTRIEA X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111226903116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa The new command is meant as syntax sugar for event handling which blocks virsh until the requested state condition is reached. The initial implementation adds a condition 'domain-inactive' returning if the domain is/becomes inactive for whatever reason. This command is useful for simple scripts e.g. for debugging libvirt when it allows responding to target state in shell without the need to fuss too much with polling or writing handlers around 'virsh event'. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- docs/manpages/virsh.rst | 25 ++++ tools/virsh-domain-event.c | 245 +++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 895a905b08..1515f84063 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -3062,6 +3062,31 @@ When *--timestamp* is used, a human-readable timesta= mp will be printed before the event. +await +----- + +**Syntax:** + +:: + + await --condition [--timeout seconds] + +Wait until the *--condition* for is satisfied. Uses events for +efficient state updates. + +Supported conditions: + + *domain-inactive* + + domain is or becomes inactive + +If *--timeout* is specified, the command gives up waiting for the conditio= n to +satisfy after *seconds* have elapsed. If SIGINT is delivered to virsh +(usually via ``Ctrl-C``) the wait is given up immediately. In non-interact= ive +mode virsh will return '2' if either of those cases instead of '1' which m= eans +an error happened. + + get-user-sshkeys ---------------- diff --git a/tools/virsh-domain-event.c b/tools/virsh-domain-event.c index 9aa21b2e78..2cd52933c7 100644 --- a/tools/virsh-domain-event.c +++ b/tools/virsh-domain-event.c @@ -1064,6 +1064,245 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) } +/* virsh event-await */ + +struct virshDomEventAwaitConditionData; + +struct virshDomainEventAwaitCondition { + const char *name; + int event; + int (*handler)(struct virshDomEventAwaitConditionData *data); +}; + +struct virshDomEventAwaitConditionData { + vshControl *ctl; + virshDomain *dom; + const struct virshDomainEventAwaitCondition *cond; + + virMutex *m; /* synchronization to ensure clean shutdown */ + virCond *c; + bool done; +}; + + +static void +virshDomEventAwaitConditionDataUnregistered(struct virshDomEventAwaitCondi= tionData *data) +{ + g_auto(virLockGuard) name =3D virLockGuardLock(data->m); + /* signal that the handler was unregistered */ + data->done =3D true; + virCondSignal(data->c); +} + + +static void +virshDomainEventAwaitCallbackLifecycle(virConnectPtr conn G_GNUC_UNUSED, + virDomainPtr dom G_GNUC_UNUSED, + int event G_GNUC_UNUSED, + int detail G_GNUC_UNUSED, + void *opaque) +{ + struct virshDomEventAwaitConditionData *data =3D opaque; + + if (data->cond->handler(data) < 1) + vshEventDone(data->ctl); +} + + +struct virshDomainEventAwaitCallbackTuple { + int event; + virConnectDomainEventGenericCallback eventCB; +}; + + +static const struct virshDomainEventAwaitCallbackTuple callbacks[] =3D +{ + { .event =3D VIR_DOMAIN_EVENT_ID_LIFECYCLE, + .eventCB =3D VIR_DOMAIN_EVENT_CALLBACK(virshDomainEventAwaitCallback= Lifecycle), + }, +}; + + +static int +virshDomainEventAwaitConditionDomainInactive(struct virshDomEventAwaitCond= itionData *data) +{ + int state =3D -1; + + if (virDomainGetState(data->dom, &state, NULL, 0) < 0) { + vshError(data->ctl, "%s", _("failed to update domain state")); + return -1; + } + + switch ((virDomainState) state) { + case VIR_DOMAIN_SHUTOFF: + case VIR_DOMAIN_CRASHED: + return 0; + + case VIR_DOMAIN_NOSTATE: + case VIR_DOMAIN_RUNNING: + case VIR_DOMAIN_BLOCKED: + case VIR_DOMAIN_PAUSED: + case VIR_DOMAIN_SHUTDOWN: + case VIR_DOMAIN_PMSUSPENDED: + case VIR_DOMAIN_LAST: + break; + } + + return 1; +} + + +static const struct virshDomainEventAwaitCondition conditions[] =3D { + { .name =3D "domain-inactive", + .event =3D VIR_DOMAIN_EVENT_ID_LIFECYCLE, + .handler =3D virshDomainEventAwaitConditionDomainInactive, + }, +}; + + +static char ** +virshDomainAwaitConditionNameCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + size_t i =3D 0; + GStrv ret =3D NULL; + + virCheckFlags(0, NULL); + + ret =3D g_new0(char *, G_N_ELEMENTS(conditions) + 1); + + for (i =3D 0; i < G_N_ELEMENTS(conditions); i++) + ret[i] =3D g_strdup(conditions[i].name); + + return ret; +} + + +static const vshCmdInfo info_await =3D { + .help =3D N_("await a domain event"), + .desc =3D N_("waits for a certain domain event to happen and then ter= minates"), +}; + +static const vshCmdOptDef opts_await[] =3D { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name =3D "condition", + .type =3D VSH_OT_STRING, + .required =3D true, + .completer =3D virshDomainAwaitConditionNameCompleter, + .help =3D N_("which condition to wait until") + }, + {.name =3D "timeout", + .type =3D VSH_OT_INT, + .help =3D N_("timeout seconds") + }, + {.name =3D NULL} +}; + + +static int +cmdAwait(vshControl *ctl, + const vshCmd *cmd) +{ + g_autoptr(virshDomain) dom =3D NULL; + int timeout =3D 0; + size_t i; + const char *conditionName =3D NULL; + virshControl *priv =3D ctl->privData; + const struct virshDomainEventAwaitCallbackTuple *callback =3D NULL; + int evid =3D -1; + g_auto(virMutex) m =3D VIR_MUTEX_INITIALIZER; + g_auto(virCond) c =3D VIR_COND_INITIALIZER; + struct virshDomEventAwaitConditionData data =3D { + .ctl =3D ctl, + .m =3D &m, + .c =3D &c, + }; + int ret =3D -1; + + if (vshCommandOptString(ctl, cmd, "condition", &conditionName) < 0) + return -1; + + for (i =3D 0; i < G_N_ELEMENTS(conditions); i++) { + if (STREQ(conditionName, conditions[i].name)) { + data.cond =3D conditions + i; + break; + } + } + + if (!data.cond) { + vshError(ctl, _("Unsupported await condition name '%1$s'"), condit= ionName); + return -1; + } + + for (i =3D 0; i < G_N_ELEMENTS(callbacks); i++) { + if (data.cond->event =3D=3D callbacks[i].event) { + callback =3D callbacks + i; + break; + } + } + + if (!callback) { + vshError(ctl, _("Missing callback definition for event type '%1$d'= "), data.cond->event); + return -1; + } + + if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0) + return -1; + + if (!(data.dom =3D dom =3D virshCommandOptDomain(ctl, cmd, NULL))) + return -1; + + if (vshEventStart(ctl, timeout) < 0) + goto cleanup; + + if ((evid =3D virConnectDomainEventRegisterAny(priv->conn, dom, + callback->event, + callback->eventCB, + &data, + (virFreeCallback) virshDo= mEventAwaitConditionDataUnregistered)) < 0) + goto cleanup; + + /* invoke the handler to ensure initial state update */ + if ((ret =3D data.cond->handler(&data)) <=3D 0) + goto cleanup; + + switch (vshEventWait(ctl)) { + case VSH_EVENT_INTERRUPT: + vshPrintExtra(ctl, "%s", _("event loop interrupted\n")); + ret =3D 2; + break; + + case VSH_EVENT_TIMEOUT: + vshPrintExtra(ctl, "%s", _("event loop timed out\n")); + ret =3D 2; + break; + + case VSH_EVENT_DONE: + ret =3D 0; + break; + + default: + ret =3D -1; + goto cleanup; + } + + cleanup: + if (evid >=3D 0) { + virConnectDomainEventDeregisterAny(priv->conn, evid); + + virMutexLock(&m); + while (!data.done) { + if (virCondWait(&c, &m) < 0) + break; + } + virMutexUnlock(&m); + } + vshEventCleanup(ctl); + + return ret; +} + const vshCmdDef domEventCmds[] =3D { {.name =3D "event", .handler =3D cmdEvent, @@ -1071,5 +1310,11 @@ const vshCmdDef domEventCmds[] =3D { .info =3D &info_event, .flags =3D 0 }, + {.name =3D "await", + .handler_rv =3D cmdAwait, + .opts =3D opts_await, + .info =3D &info_await, + .flags =3D 0 + }, {.name =3D NULL} }; --=20 2.49.0 From nobody Mon Sep 8 21:39:50 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1749111275; cv=none; d=zohomail.com; s=zohoarc; b=FwalNPFyAksVsG+6xQiXGcSgh7CfF4OkdZYQhhBc5InB2MhwlZ+Ev8i2rV0ePTfe7d22O0IESDCUETYWRDZ+rKFkFAQu6DTyj8IB4//SDPRUk9+EujauKQycW/YUtsUedYQXQRZBJncC8T85xx5K1Po2iAw9BWtQOtx9PE/xqf8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749111275; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=NikEUfKV4XmFxAmW+/khSrLLNeCXMXFJ2Pr2us0vgSI=; b=IBtRHcnFt8g/99I4aVIdKiVoXdEnUFeqhSGNHbnR30yAS6iPs6dHphxxgibxlPf3gUvlxIL3/rvEgLriY4R+YpIMsAKAi/z0oFbIQcv73VbrzXLuHrm8cEgs0yWSOLvUQ/hpFlSRnwqaUr221/toegcGcv7xF9novEig3NULImo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1749111275540274.9842077757494; Thu, 5 Jun 2025 01:14:35 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 7E81611BF; Thu, 5 Jun 2025 04:14:34 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 598D114C1; Thu, 5 Jun 2025 04:11:24 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 6739E11A8; Thu, 5 Jun 2025 04:11:18 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id A8C7E13A0 for ; Thu, 5 Jun 2025 04:11:01 -0400 (EDT) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-256-8A_p7kboPSyRVL6ZQbGZiQ-1; Thu, 05 Jun 2025 04:10:59 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1144018003FC for ; Thu, 5 Jun 2025 08:10:59 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.44.22.3]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3D4E218003FC for ; Thu, 5 Jun 2025 08:10:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749111061; h=from:from: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; bh=4P9OIWswAIrNNT9ah0xMlKx7cp2YAd1P0ZeHaVi+Coo=; b=O5tKupG4/E6Dv82k49GxHPzTCzpfRiaANCUjGj8tJB9j6S1/3YJFutAGOKYlYUoC7q65Tw NzN7hChjPQE+1bjUI+Rq/9YrE6Uk0Qwxs/wK70JX0+ibrrhpm2sNrYwF1MdXHiKHrSYxyj 6ZcgtP0FuekW59iKNFCBUiEFLLlk2Kw= X-MC-Unique: 8A_p7kboPSyRVL6ZQbGZiQ-1 X-Mimecast-MFC-AGG-ID: 8A_p7kboPSyRVL6ZQbGZiQ_1749111059 To: devel@lists.libvirt.org Subject: [PATCH 7/7] virsh: await: Introduce 'guest-agent-available' condition Date: Thu, 5 Jun 2025 10:10:47 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: s25JJTtczqOKSAZMZC-JQLumIA5FR36T-kJvYJfqZIw_1749111059 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: OZEHSVE2GOJY7CJRUNFI7D7SJBBJX4DN X-Message-ID-Hash: OZEHSVE2GOJY7CJRUNFI7D7SJBBJX4DN X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Peter Krempa via Devel Reply-To: Peter Krempa X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1749111277365116600 Content-Type: text/plain; charset="utf-8" From: Peter Krempa The new condition allows waiting for the guest agent to show up, which usually means that the guest has booted enough to respond to external stimuli. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- docs/manpages/virsh.rst | 5 +++++ tools/virsh-domain-event.c | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 1515f84063..0259c21065 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -3080,6 +3080,11 @@ Supported conditions: domain is or becomes inactive + *guest-agent-available* + + the guest agent inside the guest connects and becomes available for co= mmands + (usually means that the guest has booted) + If *--timeout* is specified, the command gives up waiting for the conditio= n to satisfy after *seconds* have elapsed. If SIGINT is delivered to virsh (usually via ``Ctrl-C``) the wait is given up immediately. In non-interact= ive diff --git a/tools/virsh-domain-event.c b/tools/virsh-domain-event.c index 2cd52933c7..de33ed9d74 100644 --- a/tools/virsh-domain-event.c +++ b/tools/virsh-domain-event.c @@ -24,6 +24,7 @@ #include "virenum.h" #include "virtime.h" #include "virtypedparam.h" +#include "virxml.h" /* * "event" command @@ -1109,6 +1110,20 @@ virshDomainEventAwaitCallbackLifecycle(virConnectPtr= conn G_GNUC_UNUSED, } +static void +virshDomainEventAwaitAgentLifecycle(virConnectPtr conn G_GNUC_UNUSED, + virDomainPtr dom G_GNUC_UNUSED, + int state G_GNUC_UNUSED, + int reason G_GNUC_UNUSED, + void *opaque G_GNUC_UNUSED) +{ + struct virshDomEventAwaitConditionData *data =3D opaque; + + if (data->cond->handler(data) < 1) + vshEventDone(data->ctl); +} + + struct virshDomainEventAwaitCallbackTuple { int event; virConnectDomainEventGenericCallback eventCB; @@ -1120,6 +1135,9 @@ static const struct virshDomainEventAwaitCallbackTupl= e callbacks[] =3D { .event =3D VIR_DOMAIN_EVENT_ID_LIFECYCLE, .eventCB =3D VIR_DOMAIN_EVENT_CALLBACK(virshDomainEventAwaitCallback= Lifecycle), }, + { .event =3D VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE, + .eventCB =3D VIR_DOMAIN_EVENT_CALLBACK(virshDomainEventAwaitAgentLif= ecycle), + }, }; @@ -1152,11 +1170,35 @@ virshDomainEventAwaitConditionDomainInactive(struct= virshDomEventAwaitConditionD } +static int +virshDomainEventAwaitConditionGuestAgentAvailable(struct virshDomEventAwai= tConditionData *data) +{ + g_autoptr(xmlDoc) xml =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + g_autofree char *state =3D NULL; + + if (virshDomainGetXMLFromDom(data->ctl, data->dom, 0, &xml, &ctxt) < 0) + return -1; + + if ((state =3D virXPathString("string(//devices/channel/target[@name = =3D 'org.qemu.guest_agent.0']/@state)", + ctxt))) { + if (STREQ(state, "connected")) + return 0; + } + + return 1; +} + + static const struct virshDomainEventAwaitCondition conditions[] =3D { { .name =3D "domain-inactive", .event =3D VIR_DOMAIN_EVENT_ID_LIFECYCLE, .handler =3D virshDomainEventAwaitConditionDomainInactive, }, + { .name =3D "guest-agent-available", + .event =3D VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE, + .handler =3D virshDomainEventAwaitConditionGuestAgentAvailable, + }, }; --=20 2.49.0