From: Daniel P. Berrangé Date: Wed, 24 Nov 2021 13:13:15 +0000 (+0000) Subject: util: refactor TPM helper methods to reduce duplicationm X-Git-Tag: v8.0.0-rc1~448 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e715648e8ba193fc377fcf52d641f3c9175b44d6;p=thirdparty%2Flibvirt.git util: refactor TPM helper methods to reduce duplicationm 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 Signed-off-by: Daniel P. Berrangé --- 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); }