qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Oksana Vohchana <ovoshcha@redhat.com>
To: qemu-devel@nongnu.org
Cc: philmd@redhat.com, ehabkost@redhat.com, wainersm@redhat.com,
	crosa@redhat.com
Subject: [PATCH 2/3] Updates send_fd_scm function
Date: Wed, 19 Feb 2020 18:33:43 +0200	[thread overview]
Message-ID: <20200219163344.27651-3-ovoshcha@redhat.com> (raw)
In-Reply-To: <20200219163344.27651-1-ovoshcha@redhat.com>

A qemu-iotest uses for FD-migration test a helper program "socket_scm_helper".
And it makes some problems if you didn't build it with a QEMU. And now we can
use new methods for the socket that allow us to send a file/socket descriptor
(with access and permissions) from one process to another.

Signed-off-by: Oksana Vohchana <ovoshcha@redhat.com>
---
 python/qemu/machine.py | 56 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 22 deletions(-)

diff --git a/python/qemu/machine.py b/python/qemu/machine.py
index 8c5bd64795..0936b71856 100644
--- a/python/qemu/machine.py
+++ b/python/qemu/machine.py
@@ -173,19 +173,24 @@ class QEMUMachine(object):
         """
         return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))])
 
-    def send_fd_scm(self, fd=None, file_path=None):
+    def send_fd_scm(self, fd=None, file_path=None, data=None):
         """
-        Send an fd or file_path to socket_scm_helper.
+        Can be used in two different cases.
+        Send an fd or file_path to socket_scm_helper or
+        provide data and fd to send it to the socket.
 
-        Exactly one of fd and file_path must be given.
+        Exactly one of fd and file_path must be given to the case of socket_scm_helper
         If it is file_path, the helper will open that file and pass its own fd.
+
+        To second case need adds data that include a QMP request and fd
         """
         # In iotest.py, the qmp should always use unix socket.
         assert self._qmp.is_scm_available()
-        if self._socket_scm_helper is None:
-            raise QEMUMachineError("No path to socket_scm_helper set")
-        if not os.path.exists(self._socket_scm_helper):
-            raise QEMUMachineError("%s does not exist" %
+        if data is None:
+            if self._socket_scm_helper is None:
+                raise QEMUMachineError("No path to socket_scm_helper set or data not provided")
+            if not os.path.exists(self._socket_scm_helper):
+                raise QEMUMachineError("%s does not exist" %
                                    self._socket_scm_helper)
 
         # This did not exist before 3.4, but since then it is
@@ -195,24 +200,31 @@ class QEMUMachine(object):
             if fd is not None:
                 os.set_inheritable(fd, True)
 
-        fd_param = ["%s" % self._socket_scm_helper,
-                    "%d" % self._qmp.get_sock_fd()]
+        if data is None:
+            fd_param = ["%s" % self._socket_scm_helper,
+                        "%d" % self._qmp.get_sock_fd()]
+            if file_path is not None:
+                assert fd is None
+                fd_param.append(file_path)
+            else:
+                assert fd is not None
+                fd_param.append(str(fd))
 
-        if file_path is not None:
-            assert fd is None
-            fd_param.append(file_path)
-        else:
-            assert fd is not None
-            fd_param.append(str(fd))
+            devnull = open(os.path.devnull, 'rb')
+            proc = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
+                                    stderr=subprocess.STDOUT, close_fds=False)
+            output = proc.communicate()[0]
+            if output:
+                LOG.debug(output)
 
-        devnull = open(os.path.devnull, 'rb')
-        proc = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
-                                stderr=subprocess.STDOUT, close_fds=False)
-        output = proc.communicate()[0]
-        if output:
-            LOG.debug(output)
+            return proc.returncode
 
-        return proc.returncode
+        else:
+            sock_fd = socket.fromfd(self._qmp.get_sock_fd(), socket.AF_UNIX, socket.SOCK_STREAM)
+            fds_param = [fd, self._qmp.get_sock_fd()]
+            self._send_fds(sock_fd, data, fds_param)
+            self._recv_fds(sock_fd)
+            return self
 
     @staticmethod
     def _remove_if_exists(path):
-- 
2.21.1



  parent reply	other threads:[~2020-02-19 17:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-19 16:33 [PATCH 0/3] Migration mechanism with FD Oksana Vohchana
2020-02-19 16:33 ` [PATCH 1/3] Adding functions _send_fds and _recv_fds Oksana Vohchana
2020-02-19 16:33 ` Oksana Vohchana [this message]
2020-02-19 16:33 ` [PATCH 3/3] Acceptance test: FD migration Oksana Vohchana
2020-02-19 18:02 ` [PATCH 0/3] Migration mechanism with FD no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200219163344.27651-3-ovoshcha@redhat.com \
    --to=ovoshcha@redhat.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=wainersm@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).