[libvirt PATCH v2 03/12] util: refactor TPM helper methods to reduce duplicationm

Daniel P. Berrangé berrange at redhat.com
Wed Nov 24 15:44:07 UTC 2021


The TPM helper methods for querying the binary path and capabilities
have the same patterns across all swtpm binaries. This code duplication
can be reduced by introducing helper methods.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/util/virtpm.c | 53 ++++++++++++++++++++---------------------------
 1 file changed, 23 insertions(+), 30 deletions(-)

diff --git a/src/util/virtpm.c b/src/util/virtpm.c
index 1a34f8ce56..cc9173cecd 100644
--- a/src/util/virtpm.c
+++ b/src/util/virtpm.c
@@ -109,49 +109,37 @@ static struct stat swtpm_ioctl_stat;
 
 typedef int (*virTPMBinaryCapsParse)(const char *);
 
-char *
-virTPMGetSwtpm(void)
+static char *
+virTPMBinaryGetPath(char **path_var)
 {
     char *s;
 
-    if (!swtpm_path && virTPMEmulatorInit() < 0)
+    if (!*path_var && virTPMEmulatorInit() < 0)
         return NULL;
 
     virMutexLock(&swtpm_tools_lock);
-    s = g_strdup(swtpm_path);
+    s = g_strdup(*path_var);
     virMutexUnlock(&swtpm_tools_lock);
 
     return s;
 }
 
 char *
-virTPMGetSwtpmSetup(void)
+virTPMGetSwtpm(void)
 {
-    char *s;
-
-    if (!swtpm_setup_path && virTPMEmulatorInit() < 0)
-        return NULL;
-
-    virMutexLock(&swtpm_tools_lock);
-    s = g_strdup(swtpm_setup_path);
-    virMutexUnlock(&swtpm_tools_lock);
+    return virTPMBinaryGetPath(&swtpm_path);
+}
 
-    return s;
+char *
+virTPMGetSwtpmSetup(void)
+{
+    return virTPMBinaryGetPath(&swtpm_setup_path);
 }
 
 char *
 virTPMGetSwtpmIoctl(void)
 {
-    char *s;
-
-    if (!swtpm_ioctl_path && virTPMEmulatorInit() < 0)
-        return NULL;
-
-    virMutexLock(&swtpm_tools_lock);
-    s = g_strdup(swtpm_ioctl_path);
-    virMutexUnlock(&swtpm_tools_lock);
-
-    return s;
+    return virTPMBinaryGetPath(&swtpm_ioctl_path);
 }
 
 /* virTPMExecGetCaps
@@ -345,18 +333,23 @@ virTPMEmulatorInit(void)
     return ret;
 }
 
-bool
-virTPMSwtpmCapsGet(unsigned int cap)
+static bool
+virTPMBinaryGetCaps(virBitmap **caps_var,
+                    unsigned int cap)
 {
     if (virTPMEmulatorInit() < 0)
         return false;
-    return virBitmapIsBitSet(swtpm_caps, cap);
+    return virBitmapIsBitSet(*caps_var, cap);
+}
+
+bool
+virTPMSwtpmCapsGet(unsigned int cap)
+{
+    return virTPMBinaryGetCaps(&swtpm_caps, cap);
 }
 
 bool
 virTPMSwtpmSetupCapsGet(unsigned int cap)
 {
-    if (virTPMEmulatorInit() < 0)
-        return false;
-    return virBitmapIsBitSet(swtpm_setup_caps, cap);
+    return virTPMBinaryGetCaps(&swtpm_setup_caps, cap);
 }
-- 
2.33.1




More information about the libvir-list mailing list