diff mbox series

[4/4] python: split the two sides of send_fd_scm

Message ID 20210928135309.199796-5-pbonzini@redhat.com (mailing list archive)
State New, archived
Headers show
Series python: remove socket_scm_helper | expand

Commit Message

Paolo Bonzini Sept. 28, 2021, 1:53 p.m. UTC
send_fd_scm can be used as a simple wrapper for self._qmp.send_fd, or it
can be given a file that will be opened for the duration of the sendmsg
system call.  Split the two cases to separate functions.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 python/qemu/machine/machine.py | 34 ++++++++++++++++------------------
 tests/qemu-iotests/045         |  2 +-
 tests/qemu-iotests/147         |  2 +-
 3 files changed, 18 insertions(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py
index 813ccb17c2..8ad3604049 100644
--- a/python/qemu/machine/machine.py
+++ b/python/qemu/machine/machine.py
@@ -207,30 +207,28 @@  def add_fd(self: _T, fd: int, fdset: int,
         self._args.append(','.join(options))
         return self
 
-    def send_fd_scm(self, fd: Optional[int] = None,
-                    file_path: Optional[str] = None) -> None:
+    def send_file_scm(self, file_path: str) -> None:
         """
-        Send an fd or file_path via QMP.
-
-        Exactly one of fd and file_path must be given.
-        If it is file_path, the function will open that file and pass
-        its own fd.
+        Open a file and pass it to QEMU as a file descriptor.
         """
         # In iotest.py, the qmp should always use unix socket.
         assert self._qmp.is_scm_available()
 
-        if file_path is not None:
-            assert fd is None
-            fd = -1
-            try:
-                fd = os.open(file_path, os.O_RDONLY)
-                self._qmp.send_fd(fd)
-            finally:
-                if fd != -1:
-                    os.close(fd)
-        else:
-            assert fd is not None
+        fd = -1
+        try:
+            fd = os.open(file_path, os.O_RDONLY)
             self._qmp.send_fd(fd)
+        finally:
+            if fd != -1:
+                os.close(fd)
+
+    def send_fd_scm(self, fd: int) -> None:
+        """
+        Send a file descriptor via QMP.
+        """
+        # In iotest.py, the qmp should always use unix socket.
+        assert self._qmp.is_scm_available()
+        self._qmp.send_fd(fd)
 
     @staticmethod
     def _remove_if_exists(path: str) -> None:
diff --git a/tests/qemu-iotests/045 b/tests/qemu-iotests/045
index 3e6d42010e..1d1fe4a19a 100755
--- a/tests/qemu-iotests/045
+++ b/tests/qemu-iotests/045
@@ -141,7 +141,7 @@  class TestSCMFd(iotests.QMPTestCase):
         os.remove(image0)
 
     def _send_fd_by_SCM(self):
-        self.vm.send_fd_scm(file_path=image0)
+        self.vm.send_file_scm(image0)
 
     def test_add_fd(self):
         self._send_fd_by_SCM()
diff --git a/tests/qemu-iotests/147 b/tests/qemu-iotests/147
index 58de6db52e..e493ff4d0d 100755
--- a/tests/qemu-iotests/147
+++ b/tests/qemu-iotests/147
@@ -269,7 +269,7 @@  class BuiltinNBD(NBDBlockdevAddBase):
         sockfd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
         sockfd.connect(unix_socket)
 
-        self.vm.send_fd_scm(fd=sockfd.fileno())
+        self.vm.send_fd_scm(sockfd.fileno())
 
         result = self.vm.qmp('getfd', fdname='nbd-fifo')
         self.assert_qmp(result, 'return', {})