qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 1/3] python/qemu/machine: Adding functions _send_fds and _recv


From: Oksana Vohchana
Subject: [PATCH v3 1/3] python/qemu/machine: Adding functions _send_fds and _recv_fds
Date: Wed, 26 Feb 2020 13:57:33 +0200

To pass the fd via SCM_RIGHT we should use socket_scm_helper file. And the
path to it file should be provided on starting the virtual machine.
For acceptance tests, this is not convenient, but sometimes not possible to
get this binary file during the testing.
This patch provides new possibilities to send or receive data through the
Unix domain socket file descriptor.
This is useful for obtaining a socket that belongs to a different
network namespace.

Signed-off-by: Oksana Vohchana <address@hidden>
---
 python/qemu/machine.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/python/qemu/machine.py b/python/qemu/machine.py
index 183d8f3d38..976316e5f5 100644
--- a/python/qemu/machine.py
+++ b/python/qemu/machine.py
@@ -24,6 +24,7 @@ import subprocess
 import shutil
 import socket
 import tempfile
+import array
 
 from . import qmp
 
@@ -155,6 +156,29 @@ class QEMUMachine(object):
         self._args.append(','.join(options))
         return self
 
+    def _recv_fds(self, sock, msglen=8192, maxfds=4096):
+        """
+        Function from:
+        https://docs.python.org/3/library/socket.html#socket.socket.recvmsg
+        """
+        fds = array.array("i")
+        msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(
+            maxfds * fds.itemsize))
+        for cmsg_level, cmsg_type, cmsg_data in ancdata:
+            if (cmsg_level == socket.SOL_SOCKET and
+                    cmsg_type == socket.SCM_RIGHTS):
+                fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data)
+                    % fds.itemsize)])
+        return msg, list(fds)
+
+    def _send_fds(self, sock, msg, fds):
+        """
+        Function from:
+        https://docs.python.org/3/library/socket.html#socket.socket.sendmsg
+        """
+        return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS,
+                                      array.array("i", fds))])
+
     def send_fd_scm(self, fd=None, file_path=None):
         """
         Send an fd or file_path to socket_scm_helper.
-- 
2.21.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]