]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add a method for checking if swtpm is available
authorDaniel P. Berrangé <berrange@redhat.com>
Wed, 24 Nov 2021 13:36:21 +0000 (13:36 +0000)
committerDaniel P. Berrangé <berrange@redhat.com>
Wed, 1 Dec 2021 12:14:03 +0000 (12:14 +0000)
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>
src/libvirt_private.syms
src/util/virtpm.c
src/util/virtpm.h

index 48b1f1568fbd8fd905e3317411cecb72a12bd4f8..76fed136cd223efde4bcaeea25e2509554a83525 100644 (file)
@@ -3451,6 +3451,7 @@ virTPMCreateCancelPath;
 virTPMGetSwtpm;
 virTPMGetSwtpmIoctl;
 virTPMGetSwtpmSetup;
+virTPMHasSwtpm;
 virTPMSwtpmCapsGet;
 virTPMSwtpmFeatureTypeFromString;
 virTPMSwtpmSetupCapsGet;
index ec51c0efb331e8812ca1e1de4de28eb617938dd1..63579b8e69a03395302f769d9e0a44a952332109 100644 (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,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 &&
index 4c16332f9bbb3067812a6be310be0f1aa9f6f6b7..0a82a03b690c4f31c759de12994117dc34e66747 100644 (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);