From nobody Sun May 19 20:02:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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=1689930048; cv=none; d=zohomail.com; s=zohoarc; b=Of9Me1+SO3/QKJLK0o7TvUuXnQVAw44wCufklX6oFwjN2wtQ9pWPCi1Qu6tcSS61TSqhkhwbrTctp+xiN9DpBDB/zKRG6ACJwvXLvrUYhb3a2wEKyRo71Y79hXibeYpM++tbxAI6plgz/md1Jkqfu4nDvN15I6Atg3t0zd6Pzlw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1689930048; 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=WRFdJayoUHImw5AIKcRgwDUsJa4RtRa60sQQqkjfWYk=; b=mK+Dwhw7qDyhV1SNoKbbKA7gTMhL3FqjNoIxSphL6kdIfkcT6KTEB6MRDQ3i6hQCe9pXkQi3N0RhdtnwFLa6iwUrP8kNbxU5dwR5kzxnw5wc/c4/fvrCHbfjkkS+0DdoBNtS3KUeZdWs8qXBAlcnphpplwhMmlIqEoqtNRlfsEo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.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.129.124]) by mx.zohomail.com with SMTPS id 1689930048558642.442945756129; Fri, 21 Jul 2023 02:00:48 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-553-VswcDAnKNI6SF_9W2pD7Fw-1; Fri, 21 Jul 2023 05:00:40 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7362C104B531; Fri, 21 Jul 2023 09:00:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 143C51121315; Fri, 21 Jul 2023 09:00:36 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B838E1946A49; Fri, 21 Jul 2023 09:00:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 1F58F1946A43 for ; Fri, 21 Jul 2023 09:00:35 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id E65B54CD0C1; Fri, 21 Jul 2023 09:00:34 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.2.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D04C492C13 for ; Fri, 21 Jul 2023 09:00:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689930047; 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=WRFdJayoUHImw5AIKcRgwDUsJa4RtRa60sQQqkjfWYk=; b=Bg+CbhQlhKzV7UtzQwAPV5XDs+ejdA3TsEownk6XRC3Z0u3l5yJCR5FI+cHFwzeAf/EmRo jls7axulP+zvdHqHB+ZX2LZwcpGYBDa7vipVYS64W3xvi6lgtsy2mWAPLS8gY5NmznDlVO H3DwbtBdG3IVgin++6b93SUsO2DP9pw= X-MC-Unique: VswcDAnKNI6SF_9W2pD7Fw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/2] virfile: Introduce virDirIsEmpty() Date: Fri, 21 Jul 2023 11:00:31 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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: 1689930049493100001 Content-Type: text/plain; charset="utf-8"; x-default="true" There might be cases where we want to know whether given directory is empty or not. Introduce a helper for that: virDirIsEmpty(). Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/libvirt_private.syms | 1 + src/util/virfile.c | 39 +++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 3 +++ 3 files changed, 43 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3071dec13a..da60c965dd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2271,6 +2271,7 @@ safezero; virBuildPathInternal; virDirClose; virDirCreate; +virDirIsEmpty; virDirOpen; virDirOpenIfExists; virDirOpenQuiet; diff --git a/src/util/virfile.c b/src/util/virfile.c index 6b4d4c3522..7c44a2a963 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3036,6 +3036,45 @@ void virDirClose(DIR *dirp) closedir(dirp); /* exempt from syntax-check */ } =20 +/** + * virDirIsEmpty: + * @path: path to the directory + * @hidden: whether hidden files matter + * + * Check whether given directory (@path) is empty, i.e. it + * contains just the usual entries '.' and '..'. Hidden files are + * ignored unless @hidden is true. IOW, a directory containing + * nothing but hidden files is considered empty if @hidden is + * false and not empty if @hidden is true. + * + * Returns: 1 if the directory is empty, + * 0 if the directory is not empty, + * -1 otherwise (no error reported). + */ +int virDirIsEmpty(const char *path, + bool hidden) +{ + g_autoptr(DIR) dir =3D NULL; + struct dirent *ent; + int direrr; + + if (virDirOpenQuiet(&dir, path) < 0) + return -1; + + while ((direrr =3D virDirRead(dir, &ent, NULL)) > 0) { + /* virDirRead() skips over '.' and '..' so here we have + * actual directory entry. */ + if (!hidden || + (hidden && ent->d_name[0] !=3D '.')) + return 0; + } + + if (direrr < 0) + return -1; + + return 1; +} + =20 /* * virFileChownFiles: diff --git a/src/util/virfile.h b/src/util/virfile.h index 6a14173625..b75a7cc53b 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -283,6 +283,9 @@ int virDirRead(DIR *dirp, struct dirent **ent, const ch= ar *dirname) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT; void virDirClose(DIR *dirp); G_DEFINE_AUTOPTR_CLEANUP_FUNC(DIR, virDirClose); +int virDirIsEmpty(const char *path, + bool hidden) + ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT; =20 int virFileMakeParentPath(const char *path) G_GNUC_WARN_UNUSED_RESULT; =20 --=20 2.41.0 From nobody Sun May 19 20:02:54 2024 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=1689930063; cv=none; d=zohomail.com; s=zohoarc; b=gfS3XkmEGA01GzfrEFxj/bETV2Q7gDsg+3xKDj4BtTIVoJE7pKrN+Zwua/7o4Thvv633pILndDEwe8M/ppFROXlQdtLqXHWE6x5itSSoaNXB7i6rlrhg8O0zR/C/S3adKVkk/EDU/VGwwGWizOe4nOUPQHYwGjO6a9k18YCNa4k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1689930063; 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=1PSa6TtzTNtw7zWjmc/cyDc2x2gFYLVKZAasIfLukGA=; b=gthKOVZf+yzOG+T8GUoKmW8ogHlhdwSig8dfTR2ACO74Inw9oVaaKC5d4DvRoNZCyqatYVvSqV+DydkXYtogFNQIGW+7rVn7fKuYL0mrQSo4z9mF+6G7KJDHcQtdd3ZSki2zJ97S4NhjyndWfviFLJQUWW7LHbvhc3nO3mpeHA4= 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 1689930063294265.64451534848433; Fri, 21 Jul 2023 02:01:03 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-659-s1Bpr1ThMvy3Y1STCyDcow-1; Fri, 21 Jul 2023 05:00:41 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 766573801BCC; Fri, 21 Jul 2023 09:00:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5CEEAC2C859; Fri, 21 Jul 2023 09:00:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 0BC4D1946A45; Fri, 21 Jul 2023 09:00:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9056D1946A43 for ; Fri, 21 Jul 2023 09:00:35 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 822F64CD0E6; Fri, 21 Jul 2023 09:00:35 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.2.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29505492C13 for ; Fri, 21 Jul 2023 09:00:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689930062; 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=1PSa6TtzTNtw7zWjmc/cyDc2x2gFYLVKZAasIfLukGA=; b=ZKmLqclDoqEm6/dL8JYet2HR5q9waRBFni8biFpIvyeydkPL6YrutByhQpxd7cuUgRbtZm GS3Ybf6euHqC22nNyBCMQscbKaJvkGUHlBZXcu5k9CYZ6W+ZyKtfQ1HOeIae13B8b7UfMv bASmASpkp9mpwKzkWcR1U9DvdYNybfM= X-MC-Unique: s1Bpr1ThMvy3Y1STCyDcow-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/2] qemu_tpm: Try harder to create emulator state Date: Fri, 21 Jul 2023 11:00:32 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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: 1689930065379100001 Content-Type: text/plain; charset="utf-8"; x-default="true" If a per-domain SWTPM state directory exists but is empty our code still considers it a valid state and skips running 'swtpm_setup' (handled in qemuTPMEmulatorRunSetup()). While we should not try to inspect individual files created by swtpm, we can still consider empty folder as non-existent state. Resolves: https://gitlab.com/libvirt/libvirt/-/issues/320 Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/qemu/qemu_tpm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 0b77ce2bc7..121f98174c 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -186,7 +186,8 @@ qemuTPMEmulatorCreateStorage(virDomainTPMDef *tpm, =20 *created =3D false; =20 - if (!virFileExists(storagepath)) + if (!virFileExists(storagepath) || + virDirIsEmpty(storagepath, false) > 0) *created =3D true; =20 if (virDirCreate(storagepath, 0700, swtpm_user, swtpm_group, --=20 2.41.0