]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: command: Extract opening of TPM backend FDs for mocking purposes
authorPeter Krempa <pkrempa@redhat.com>
Tue, 14 Aug 2018 12:17:52 +0000 (14:17 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 24 Aug 2018 13:58:34 +0000 (15:58 +0200)
Allow mocking of the file descriptor numbers used for the TPM
passthrough mode by extracting the relevant code into an exported
function.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
src/qemu/qemu_command.c
src/qemu/qemu_command.h

index 88d698e2622e8abba7ca71befa5e5bbf92bdfaac..5bbc43be4367c99b62a6a5e644021196610c68d0 100644 (file)
@@ -9688,6 +9688,31 @@ qemuBuildTPMDevStr(const virDomainDef *def,
 }
 
 
+/* this function is exported so that tests can mock the FDs */
+int
+qemuBuildTPMOpenBackendFDs(const char *tpmdev,
+                           const char *cancel_path,
+                           int *tpmfd,
+                           int *cancelfd)
+{
+    if ((*tpmfd = open(tpmdev, O_RDWR)) < 0) {
+        virReportSystemError(errno, _("Could not open TPM device %s"),
+                             tpmdev);
+        return -1;
+    }
+
+    if ((*cancelfd = open(cancel_path, O_WRONLY)) < 0) {
+        virReportSystemError(errno,
+                             _("Could not open TPM device's cancel "
+                               "path %s"), cancel_path);
+        VIR_FORCE_CLOSE(*tpmfd);
+        return -1;
+    }
+
+    return 0;
+}
+
+
 static char *
 qemuBuildTPMBackendStr(const virDomainDef *def,
                        virCommandPtr cmd,
@@ -9726,30 +9751,16 @@ qemuBuildTPMBackendStr(const virDomainDef *def,
             goto error;
 
         if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_ADD_FD)) {
-            *tpmfd = open(tpmdev, O_RDWR);
-            if (*tpmfd < 0) {
-                virReportSystemError(errno, _("Could not open TPM device %s"),
-                                     tpmdev);
+            if (qemuBuildTPMOpenBackendFDs(tpmdev, cancel_path, tpmfd, cancelfd) < 0)
                 goto error;
-            }
 
-            virCommandPassFD(cmd, *tpmfd,
-                             VIR_COMMAND_PASS_FD_CLOSE_PARENT);
+            virCommandPassFD(cmd, *tpmfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
+            virCommandPassFD(cmd, *cancelfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
             devset = qemuVirCommandGetDevSet(cmd, *tpmfd);
             if (devset == NULL)
                 goto error;
 
-            *cancelfd = open(cancel_path, O_WRONLY);
-            if (*cancelfd < 0) {
-                virReportSystemError(errno,
-                                     _("Could not open TPM device's cancel "
-                                       "path %s"), cancel_path);
-                goto error;
-            }
             VIR_FREE(cancel_path);
-
-            virCommandPassFD(cmd, *cancelfd,
-                             VIR_COMMAND_PASS_FD_CLOSE_PARENT);
             cancel_path = qemuVirCommandGetDevSet(cmd, *cancelfd);
             if (cancel_path == NULL)
                 goto error;
index 13c5508ae859ccd92b6e47e2360e1cf4e63fc069..98d4ac90b53dc38b10d73cda03bb7525bf7729cd 100644 (file)
@@ -219,4 +219,13 @@ qemuBuildVsockDevStr(virDomainDefPtr def,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
     ATTRIBUTE_NONNULL(4);
 
+/* this function is exported so that tests can mock the FDs */
+int
+qemuBuildTPMOpenBackendFDs(const char *tpmdev,
+                           const char *cancel_path,
+                           int *tpmfd,
+                           int *cancelfd)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
+    ATTRIBUTE_NONNULL(4);
+
 #endif /* __QEMU_COMMAND_H__*/