From 193436c6a125f536d4e3a79e471a64e94e1000e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Wed, 24 Nov 2021 13:36:21 +0000 Subject: [PATCH] util: add a method for checking if swtpm is available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The QEMU domain capabilities code wants to quietly know whether swtpm is available on the host. Reviewed-by: Ján Tomko Signed-off-by: Daniel P. Berrangé --- src/libvirt_private.syms | 1 + src/util/virtpm.c | 45 +++++++++++++++++++++++++++++----------- src/util/virtpm.h | 2 ++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 48b1f1568f..76fed136cd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3451,6 +3451,7 @@ virTPMCreateCancelPath; virTPMGetSwtpm; virTPMGetSwtpmIoctl; virTPMGetSwtpmSetup; +virTPMHasSwtpm; virTPMSwtpmCapsGet; virTPMSwtpmFeatureTypeFromString; virTPMSwtpmSetupCapsGet; diff --git a/src/util/virtpm.c b/src/util/virtpm.c index ec51c0efb3..63579b8e69 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -132,7 +132,7 @@ static virTPMBinaryInfo swtpmBinaries[VIR_TPM_BINARY_LAST] = { }, }; -static int virTPMEmulatorInit(void); +static int virTPMEmulatorInit(bool quiet); static char * virTPMBinaryGetPath(virTPMBinary binary) @@ -141,7 +141,7 @@ virTPMBinaryGetPath(virTPMBinary binary) virMutexLock(&swtpm_tools_lock); - if (virTPMEmulatorInit() < 0) + if (virTPMEmulatorInit(false) < 0) goto cleanup; s = g_strdup(swtpmBinaries[binary].path); @@ -169,6 +169,24 @@ virTPMGetSwtpmIoctl(void) return virTPMBinaryGetPath(VIR_TPM_BINARY_SWTPM_IOCTL); } +bool virTPMHasSwtpm(void) +{ + bool ret = false; + + virMutexLock(&swtpm_tools_lock); + + if (virTPMEmulatorInit(true) < 0) + goto cleanup; + + ret = swtpmBinaries[VIR_TPM_BINARY_SWTPM].path != NULL && + swtpmBinaries[VIR_TPM_BINARY_SWTPM_SETUP].path != NULL && + swtpmBinaries[VIR_TPM_BINARY_SWTPM_IOCTL].path != NULL; + + cleanup: + virMutexUnlock(&swtpm_tools_lock); + return ret; +} + /* virTPMExecGetCaps * * Execute the prepared command and parse the returned JSON object @@ -269,7 +287,7 @@ virTPMGetCaps(virTPMBinaryCapsParse capsParse, * executables that we will use to start and setup the swtpm */ static int -virTPMEmulatorInit(void) +virTPMEmulatorInit(bool quiet) { size_t i; @@ -293,20 +311,23 @@ virTPMEmulatorInit(void) path = virFindFileInPath(virTPMBinaryTypeToString(i)); if (!path) { - virReportSystemError(ENOENT, - _("Unable to find '%s' binary in $PATH"), - virTPMBinaryTypeToString(i)); + if (!quiet) + virReportSystemError(ENOENT, + _("Unable to find '%s' binary in $PATH"), + virTPMBinaryTypeToString(i)); return -1; } if (!virFileIsExecutable(path)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("%s is not an executable"), - path); + if (!quiet) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("%s is not an executable"), + path); return -1; } if (stat(path, &swtpmBinaries[i].stat) < 0) { - virReportSystemError(errno, - _("Could not stat %s"), path); + if (!quiet) + virReportSystemError(errno, + _("Could not stat %s"), path); return -1; } swtpmBinaries[i].path = g_steal_pointer(&path); @@ -326,7 +347,7 @@ virTPMBinaryGetCaps(virTPMBinary binary, virMutexLock(&swtpm_tools_lock); - if (virTPMEmulatorInit() < 0) + if (virTPMEmulatorInit(false) < 0) goto cleanup; if (!swtpmBinaries[binary].caps && diff --git a/src/util/virtpm.h b/src/util/virtpm.h index 4c16332f9b..0a82a03b69 100644 --- a/src/util/virtpm.h +++ b/src/util/virtpm.h @@ -26,6 +26,8 @@ char *virTPMGetSwtpm(void); char *virTPMGetSwtpmSetup(void); char *virTPMGetSwtpmIoctl(void); +bool virTPMHasSwtpm(void); + bool virTPMSwtpmCapsGet(unsigned int cap); bool virTPMSwtpmSetupCapsGet(unsigned int cap);