From nobody Sat Apr 20 12:59:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1534324763733955.6287194471755; Wed, 15 Aug 2018 02:19:23 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A36923086263; Wed, 15 Aug 2018 09:19:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5F2722010CC5; Wed, 15 Aug 2018 09:19:21 +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 151EF3FCC3; Wed, 15 Aug 2018 09:19:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7F9IwL4020038 for ; Wed, 15 Aug 2018 05:18:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 69DED1134CA5; Wed, 15 Aug 2018 09:18:58 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 083E210073AC for ; Wed, 15 Aug 2018 09:18:57 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 15 Aug 2018 11:18:44 +0200 Message-Id: <9ed2e7ccf0dee3ff681039561e20cf4bc41755ce.1534324576.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 07/10] qemu: command: Extract opening of TPM backend FDs for mocking purposes 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Wed, 15 Aug 2018 09:19:22 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Allow mocking of the file descriptor numbers used for the TPM passthrough mode by extracting the relevant code into an exported function. Signed-off-by: Peter Krempa --- src/qemu/qemu_command.c | 41 +++++++++++++++++++++++++++-------------- src/qemu/qemu_command.h | 7 +++++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index ddb90895e0..fa66b8affb 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -9586,6 +9586,31 @@ qemuBuildTPMDevStr(const virDomainDef *def, } +/* this function is exported so that tests can mock the FDs */ +int +qemuBuildTPMOpenBackendFDs(const char *tpmdev, + const char *cancel_path, + int *tpmfd, + int *cancelfd) +{ + if ((*tpmfd =3D open(tpmdev, O_RDWR)) < 0) { + virReportSystemError(errno, _("Could not open TPM device %s"), + tpmdev); + return -1; + } + + if ((*cancelfd =3D open(cancel_path, O_WRONLY)) < 0) { + virReportSystemError(errno, + _("Could not open TPM device's cancel " + "path %s"), cancel_path); + VIR_FORCE_CLOSE(*tpmfd); + return -1; + } + + return 0; +} + + static char * qemuBuildTPMBackendStr(const virDomainDef *def, virCommandPtr cmd, @@ -9624,12 +9649,8 @@ qemuBuildTPMBackendStr(const virDomainDef *def, goto error; if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_ADD_FD)) { - *tpmfd =3D open(tpmdev, O_RDWR); - if (*tpmfd < 0) { - virReportSystemError(errno, _("Could not open TPM device %= s"), - tpmdev); + if (qemuBuildTPMOpenBackendFDs(tpmdev, cancel_path, tpmfd, can= celfd) < 0) goto error; - } virCommandPassFD(cmd, *tpmfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT); @@ -9637,17 +9658,9 @@ qemuBuildTPMBackendStr(const virDomainDef *def, if (devset =3D=3D NULL) goto error; - *cancelfd =3D open(cancel_path, O_WRONLY); - if (*cancelfd < 0) { - virReportSystemError(errno, - _("Could not open TPM device's cancel= " - "path %s"), cancel_path); - goto error; - } - VIR_FREE(cancel_path); - virCommandPassFD(cmd, *cancelfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT); + VIR_FREE(cancel_path); cancel_path =3D qemuVirCommandGetDevSet(cmd, *cancelfd); if (cancel_path =3D=3D NULL) goto error; diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h index 283bf3120d..7f84f904ce 100644 --- a/src/qemu/qemu_command.h +++ b/src/qemu/qemu_command.h @@ -216,4 +216,11 @@ qemuBuildVsockDevStr(virDomainDefPtr def, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4); +/* this function is exported so that tests can mock the FDs */ +int +qemuBuildTPMOpenBackendFDs(const char *tpmdev, + const char *cancel_path, + int *tpmfd, + int *cancelfd); + #endif /* __QEMU_COMMAND_H__*/ --=20 2.16.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list