]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: refactor TPM helper methods to reduce duplicationm
authorDaniel P. Berrangé <berrange@redhat.com>
Wed, 24 Nov 2021 13:13:15 +0000 (13:13 +0000)
committerDaniel P. Berrangé <berrange@redhat.com>
Wed, 1 Dec 2021 12:14:03 +0000 (12:14 +0000)
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.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/virtpm.c

index 1a34f8ce56dbdfd6cbae98d4b130a8ac7c02a1f6..cc9173cecd233be335a8f429d713452591f93c6f 100644 (file)
@@ -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);
 }