From nobody Sun Feb 8 14:53:12 2026 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 ARC-Seal: i=1; a=rsa-sha256; t=1564065058; cv=none; d=zoho.com; s=zohoarc; b=J5cXo+JCskG/87KtuEQX/Zg+9AJv50ku9LJhdjdOHIvgyzz+U7RSvwTFj4cVq8y0w7iy70pR+LvHvNzfqrsnffIU1sSYXxFNL+VRMMMw++Ye/zsJeHBsTb5awq27tD3OnXLvOc6fYJEtwUGgmhWr97i7VXyQxLvbcjswJ5CO12s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564065058; 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:ARC-Authentication-Results; bh=3DHZsLPNQ73y1jYYdCnClJzXOoAhbgnUPkf/Np1Xqus=; b=duArim2mHdbmcRZqmMvH0o0YkJi/DRCvDKKhvztQ9j+uaYwOGNquapS+CE5aIlsL9OvZoPW4J13Q5ycLte533WMu+wvdO8kvsL4bBAxVpx+DvlcKOPut76CdZkHOO0Vzyk3kw1gvGrewj7GOl+quPsLNedzhWJQKRDrc9LiBwIc= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 156406505865781.97771555360168; Thu, 25 Jul 2019 07:30:58 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 003A481DF6; Thu, 25 Jul 2019 14:30:57 +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 B6D4C665FA; Thu, 25 Jul 2019 14:30:56 +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 67930E166; Thu, 25 Jul 2019 14:30:56 +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 x6PEUrxw012773 for ; Thu, 25 Jul 2019 10:30:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id EFC7866A10; Thu, 25 Jul 2019 14:30:52 +0000 (UTC) Received: from mx1.redhat.com (ext-mx17.extmail.prod.ext.phx2.redhat.com [10.5.110.46]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E8DC179401 for ; Thu, 25 Jul 2019 14:30:50 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 45CB130805C4 for ; Thu, 25 Jul 2019 14:30:49 +0000 (UTC) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x6PEUAVT053261 for ; Thu, 25 Jul 2019 10:30:48 -0400 Received: from e33.co.us.ibm.com (e33.co.us.ibm.com [32.97.110.151]) by mx0a-001b2d01.pphosted.com with ESMTP id 2tydjgt6k8-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 25 Jul 2019 10:30:48 -0400 Received: from localhost by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 25 Jul 2019 15:30:47 +0100 Received: from b03cxnp07029.gho.boulder.ibm.com (9.17.130.16) by e33.co.us.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Thu, 25 Jul 2019 15:30:44 +0100 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x6PEUhOR56623428 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 Jul 2019 14:30:43 GMT Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8C036BE059; Thu, 25 Jul 2019 14:30:43 +0000 (GMT) Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 25315BE04F; Thu, 25 Jul 2019 14:30:43 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP; Thu, 25 Jul 2019 14:30:43 +0000 (GMT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 25 Jul 2019 10:30:20 -0400 In-Reply-To: <20190725143033.1176271-1-stefanb@linux.vnet.ibm.com> References: <20190725143033.1176271-1-stefanb@linux.vnet.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19072514-0036-0000-0000-00000ADE51AA X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00011491; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000287; SDB=6.01237266; UDB=6.00652184; IPR=6.01018639; MB=3.00027885; MTD=3.00000008; XFM=3.00000015; UTC=2019-07-25 14:30:46 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19072514-0037-0000-0000-00004CBE14F5 Message-Id: <20190725143033.1176271-7-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-07-25_06:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1906280000 definitions=main-1907250169 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 238 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Thu, 25 Jul 2019 14:30:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Thu, 25 Jul 2019 14:30:49 +0000 (UTC) for IP:'148.163.156.1' DOMAIN:'mx0a-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -0.698 (RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE) 148.163.156.1 mx0a-001b2d01.pphosted.com 148.163.156.1 mx0a-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.46 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: marcandre.lureau@redhat.com, Stefan Berger Subject: [libvirt] [PATCH v6 06/19] tpm: Move qemuTPMEmulatorInit to virTPMEmulatorInit in virtpm.c 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: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 25 Jul 2019 14:30:57 +0000 (UTC) Move qemuTPMEmulatorInit to virTPMEmulatorInit in virtpm.c and introduce a few functions to query the executables needed for virCommands. Add locking to protect the tool paths and return a copy of the tool paths to callers wanting to access them so that we can run the initialization function multiples time later on and detect when the executable gets update= d. Signed-off-by: Stefan Berger Reviewed-by: Marc-Andr=C3=A9 Lureau --- src/libvirt_private.syms | 4 ++ src/qemu/qemu_tpm.c | 90 ++++++----------------------- src/util/virtpm.c | 122 +++++++++++++++++++++++++++++++++++++++ src/util/virtpm.h | 5 ++ 4 files changed, 149 insertions(+), 72 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ff5a77b0e2..4cdbb80596 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3177,6 +3177,10 @@ virTimeStringThenRaw; =20 # util/virtpm.h virTPMCreateCancelPath; +virTPMEmulatorInit; +virTPMGetSwtpm; +virTPMGetSwtpmIoctl; +virTPMGetSwtpmSetup; =20 =20 # util/virtypedparam.h diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index cc8c69433b..7282b01bfe 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -41,79 +41,12 @@ #include "configmake.h" #include "dirname.h" #include "qemu_tpm.h" +#include "virtpm.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 VIR_LOG_INIT("qemu.tpm"); =20 -/* - * executables for the swtpm; to be found on the host - */ -static char *swtpm_path; -static char *swtpm_setup; -static char *swtpm_ioctl; - -/* - * qemuTPMEmulatorInit - * - * Initialize the Emulator functions by searching for necessary - * executables that we will use to start and setup the swtpm - */ -static int -qemuTPMEmulatorInit(void) -{ - if (!swtpm_path) { - swtpm_path =3D virFindFileInPath("swtpm"); - if (!swtpm_path) { - virReportSystemError(ENOENT, "%s", - _("Unable to find 'swtpm' binary in $PATH= ")); - return -1; - } - if (!virFileIsExecutable(swtpm_path)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("TPM emulator %s is not an executable"), - swtpm_path); - VIR_FREE(swtpm_path); - return -1; - } - } - - if (!swtpm_setup) { - swtpm_setup =3D virFindFileInPath("swtpm_setup"); - if (!swtpm_setup) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Could not find 'swtpm_setup' in PATH")); - return -1; - } - if (!virFileIsExecutable(swtpm_setup)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("'%s' is not an executable"), - swtpm_setup); - VIR_FREE(swtpm_setup); - return -1; - } - } - - if (!swtpm_ioctl) { - swtpm_ioctl =3D virFindFileInPath("swtpm_ioctl"); - if (!swtpm_ioctl) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Could not find swtpm_ioctl in PATH")); - return -1; - } - if (!virFileIsExecutable(swtpm_ioctl)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("swtpm_ioctl program %s is not an executable"= ), - swtpm_ioctl); - VIR_FREE(swtpm_ioctl); - return -1; - } - } - - return 0; -} - - /* * qemuTPMCreateEmulatorStoragePath * @@ -345,12 +278,13 @@ qemuTPMEmulatorGetPid(const char *swtpmStateDir, pid_t *pid) { int ret; + VIR_AUTOFREE(char *) swtpm =3D virTPMGetSwtpm(); char *pidfile =3D qemuTPMEmulatorCreatePidFilename(swtpmStateDir, shortName); if (!pidfile) return -ENOMEM; =20 - ret =3D virPidFileReadPathIfAlive(pidfile, pid, swtpm_path); + ret =3D virPidFileReadPathIfAlive(pidfile, pid, swtpm); =20 VIR_FREE(pidfile); =20 @@ -386,7 +320,7 @@ qemuTPMEmulatorPrepareHost(virDomainTPMDefPtr tpm, { int ret =3D -1; =20 - if (qemuTPMEmulatorInit() < 0) + if (virTPMEmulatorInit() < 0) return -1; =20 /* create log dir ... allow 'tss' user to cd into it */ @@ -471,6 +405,10 @@ qemuTPMEmulatorRunSetup(const char *storagepath, int ret =3D -1; char uuid[VIR_UUID_STRING_BUFLEN]; char *vmid =3D NULL; + VIR_AUTOFREE(char *)swtpm_setup =3D virTPMGetSwtpmSetup(); + + if (!swtpm_setup) + return -1; =20 if (!privileged && tpmversion =3D=3D VIR_DOMAIN_TPM_VERSION_1_2) return virFileWriteStr(logfile, @@ -562,6 +500,10 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, virCommandPtr cmd =3D NULL; bool created =3D false; char *pidfile; + VIR_AUTOFREE(char *) swtpm =3D virTPMGetSwtpm(); + + if (!swtpm) + return NULL; =20 if (qemuTPMCreateEmulatorStorage(tpm->data.emulator.storagepath, &created, swtpm_user, swtpm_group) < = 0) @@ -575,7 +517,7 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, =20 unlink(tpm->data.emulator.source.data.nix.path); =20 - cmd =3D virCommandNew(swtpm_path); + cmd =3D virCommandNew(swtpm); if (!cmd) goto error; =20 @@ -639,8 +581,12 @@ qemuTPMEmulatorStop(const char *swtpmStateDir, virCommandPtr cmd; char *pathname; char *errbuf =3D NULL; + VIR_AUTOFREE(char *) swtpm_ioctl =3D virTPMGetSwtpmIoctl(); + + if (!swtpm_ioctl) + return; =20 - if (qemuTPMEmulatorInit() < 0) + if (virTPMEmulatorInit() < 0) return; =20 if (!(pathname =3D qemuTPMCreateEmulatorSocket(swtpmStateDir, shortNam= e))) diff --git a/src/util/virtpm.c b/src/util/virtpm.c index 583b9a64a4..d35848d2f2 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -72,3 +72,125 @@ virTPMCreateCancelPath(const char *devpath) cleanup: return path; } + +/* + * executables for the swtpm; to be found on the host + */ +static virMutex swtpm_tools_lock =3D VIR_MUTEX_INITIALIZER; +static char *swtpm_path; +static char *swtpm_setup; +static char *swtpm_ioctl; + +char * +virTPMGetSwtpm(void) +{ + char *s; + + if (!swtpm_path && virTPMEmulatorInit() < 0) + return NULL; + + virMutexLock(&swtpm_tools_lock); + ignore_value(VIR_STRDUP(s, swtpm_path)); + virMutexUnlock(&swtpm_tools_lock); + + return s; +} + +char * +virTPMGetSwtpmSetup(void) +{ + char *s; + + if (!swtpm_setup && virTPMEmulatorInit() < 0) + return NULL; + + virMutexLock(&swtpm_tools_lock); + ignore_value(VIR_STRDUP(s, swtpm_setup)); + virMutexUnlock(&swtpm_tools_lock); + + return s; +} + +char * +virTPMGetSwtpmIoctl(void) +{ + char *s; + + if (!swtpm_ioctl && virTPMEmulatorInit() < 0) + return NULL; + + virMutexLock(&swtpm_tools_lock); + ignore_value(VIR_STRDUP(s, swtpm_ioctl)); + virMutexUnlock(&swtpm_tools_lock); + + return s; +} + +/* + * virTPMEmulatorInit + * + * Initialize the Emulator functions by searching for necessary + * executables that we will use to start and setup the swtpm + */ +int +virTPMEmulatorInit(void) +{ + int ret =3D -1; + + virMutexLock(&swtpm_tools_lock); + + if (!swtpm_path) { + swtpm_path =3D virFindFileInPath("swtpm"); + if (!swtpm_path) { + virReportSystemError(ENOENT, "%s", + _("Unable to find 'swtpm' binary in $PATH= ")); + goto cleanup; + } + if (!virFileIsExecutable(swtpm_path)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("TPM emulator %s is not an executable"), + swtpm_path); + VIR_FREE(swtpm_path); + goto cleanup; + } + } + + if (!swtpm_setup) { + swtpm_setup =3D virFindFileInPath("swtpm_setup"); + if (!swtpm_setup) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find 'swtpm_setup' in PATH")); + goto cleanup; + } + if (!virFileIsExecutable(swtpm_setup)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("'%s' is not an executable"), + swtpm_setup); + VIR_FREE(swtpm_setup); + goto cleanup; + } + } + + if (!swtpm_ioctl) { + swtpm_ioctl =3D virFindFileInPath("swtpm_ioctl"); + if (!swtpm_ioctl) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find swtpm_ioctl in PATH")); + goto cleanup; + } + if (!virFileIsExecutable(swtpm_ioctl)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("swtpm_ioctl program %s is not an executable"= ), + swtpm_ioctl); + VIR_FREE(swtpm_ioctl); + goto cleanup; + } + } + + ret =3D 0; + + cleanup: + virMutexUnlock(&swtpm_tools_lock); + + return ret; +} diff --git a/src/util/virtpm.h b/src/util/virtpm.h index 4408bdb217..2311f04ae5 100644 --- a/src/util/virtpm.h +++ b/src/util/virtpm.h @@ -21,3 +21,8 @@ #pragma once =20 char *virTPMCreateCancelPath(const char *devpath) ATTRIBUTE_NOINLINE; + +char *virTPMGetSwtpm(void); +char *virTPMGetSwtpmSetup(void); +char *virTPMGetSwtpmIoctl(void); +int virTPMEmulatorInit(void); --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list