util: add a method for checking if swtpm is available

The QEMU domain capabilities code wants to quietly know whether swtpm is
available on the host.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2021-11-24 13:36:21 +00:00
parent 45f83e3a0b
commit 193436c6a1
3 changed files with 36 additions and 12 deletions

View File

@ -3451,6 +3451,7 @@ virTPMCreateCancelPath;
virTPMGetSwtpm;
virTPMGetSwtpmIoctl;
virTPMGetSwtpmSetup;
virTPMHasSwtpm;
virTPMSwtpmCapsGet;
virTPMSwtpmFeatureTypeFromString;
virTPMSwtpmSetupCapsGet;

View File

@ -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,18 +311,21 @@ virTPMEmulatorInit(void)
path = virFindFileInPath(virTPMBinaryTypeToString(i));
if (!path) {
if (!quiet)
virReportSystemError(ENOENT,
_("Unable to find '%s' binary in $PATH"),
virTPMBinaryTypeToString(i));
return -1;
}
if (!virFileIsExecutable(path)) {
if (!quiet)
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s is not an executable"),
path);
return -1;
}
if (stat(path, &swtpmBinaries[i].stat) < 0) {
if (!quiet)
virReportSystemError(errno,
_("Could not stat %s"), path);
return -1;
@ -326,7 +347,7 @@ virTPMBinaryGetCaps(virTPMBinary binary,
virMutexLock(&swtpm_tools_lock);
if (virTPMEmulatorInit() < 0)
if (virTPMEmulatorInit(false) < 0)
goto cleanup;
if (!swtpmBinaries[binary].caps &&

View File

@ -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);