]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tpm: Refactor virTPMEmulatorInit to use loop
authorStefan Berger <stefanb@linux.vnet.ibm.com>
Thu, 25 Jul 2019 18:22:03 +0000 (14:22 -0400)
committerDaniel P. Berrangé <berrange@redhat.com>
Fri, 26 Jul 2019 09:30:08 +0000 (10:30 +0100)
Refactor virTPMEmulatorInit to use a loop with parameters. This allows
for easier extension later on.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/virtpm.c

index d35848d2f236b6bb22cba398805b7e8a6ee12755..6df225f4e4fa0f173428ab824b48baf49ef50de0 100644 (file)
@@ -136,54 +136,46 @@ int
 virTPMEmulatorInit(void)
 {
     int ret = -1;
-
-    virMutexLock(&swtpm_tools_lock);
-
-    if (!swtpm_path) {
-        swtpm_path = virFindFileInPath("swtpm");
-        if (!swtpm_path) {
-            virReportSystemError(ENOENT, "%s",
-                                 _("Unable to find 'swtpm' binary in $PATH"));
-            goto cleanup;
-        }
-        if (!virFileIsExecutable(swtpm_path)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("TPM emulator %s is not an executable"),
-                           swtpm_path);
-            VIR_FREE(swtpm_path);
-            goto cleanup;
+    static const struct {
+        const char *name;
+        char **path;
+    } prgs[] = {
+        {
+            .name = "swtpm",
+            .path = &swtpm_path,
+        },
+        {
+            .name = "swtpm_setup",
+            .path = &swtpm_setup,
+        },
+        {
+            .name = "swtpm_ioctl",
+            .path = &swtpm_ioctl,
         }
-    }
+    };
+    size_t i;
 
-    if (!swtpm_setup) {
-        swtpm_setup = virFindFileInPath("swtpm_setup");
-        if (!swtpm_setup) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("Could not find 'swtpm_setup' in PATH"));
-            goto cleanup;
-        }
-        if (!virFileIsExecutable(swtpm_setup)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("'%s' is not an executable"),
-                           swtpm_setup);
-            VIR_FREE(swtpm_setup);
-            goto cleanup;
-        }
-    }
+    virMutexLock(&swtpm_tools_lock);
 
-    if (!swtpm_ioctl) {
-        swtpm_ioctl = virFindFileInPath("swtpm_ioctl");
-        if (!swtpm_ioctl) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("Could not find swtpm_ioctl in PATH"));
-            goto cleanup;
-        }
-        if (!virFileIsExecutable(swtpm_ioctl)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("swtpm_ioctl program %s is not an executable"),
-                           swtpm_ioctl);
-            VIR_FREE(swtpm_ioctl);
-            goto cleanup;
+    for (i = 0; i < ARRAY_CARDINALITY(prgs); i++) {
+        VIR_AUTOFREE(char *) path = NULL;
+        bool findit = *prgs[i].path == NULL;
+
+        if (findit) {
+            path = virFindFileInPath(prgs[i].name);
+            if (!path) {
+                virReportSystemError(ENOENT,
+                                _("Unable to find '%s' binary in $PATH"),
+                                prgs[i].name);
+                goto cleanup;
+            }
+            if (!virFileIsExecutable(path)) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("%s is not an executable"),
+                               path);
+                goto cleanup;
+            }
+            *prgs[i].path = path;
         }
     }