All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: jsnow@redhat.com
Subject: [PATCH 1/4] python: stop using socket_scm_helper
Date: Tue, 28 Sep 2021 15:53:06 +0200	[thread overview]
Message-ID: <20210928135309.199796-2-pbonzini@redhat.com> (raw)
In-Reply-To: <20210928135309.199796-1-pbonzini@redhat.com>

Python is able to call sendmsg, it does not need a helper.  Write the
code directly; for now, keep the weird calling convention.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 python/qemu/machine/machine.py | 48 ++++++++++++----------------------
 python/qemu/qmp/__init__.py    | 15 +++++++++++
 2 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py
index 34131884a5..e4356ea99e 100644
--- a/python/qemu/machine/machine.py
+++ b/python/qemu/machine/machine.py
@@ -213,48 +213,34 @@ def add_fd(self: _T, fd: int, fdset: int,
     def send_fd_scm(self, fd: Optional[int] = None,
                     file_path: Optional[str] = None) -> int:
         """
-        Send an fd or file_path to socket_scm_helper.
+        Send an fd or file_path via QMP.
 
         Exactly one of fd and file_path must be given.
-        If it is file_path, the helper will open that file and pass its own fd.
+        If it is file_path, the function will open that file and pass
+        its own 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" %
-                                   self._socket_scm_helper)
-
-        # This did not exist before 3.4, but since then it is
-        # mandatory for our purpose
-        if hasattr(os, 'set_inheritable'):
-            os.set_inheritable(self._qmp.get_sock_fd(), True)
-            if fd is not None:
-                os.set_inheritable(fd, True)
-
-        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)
+            fd = -1
+            try:
+                fd = os.open(file_path, os.O_RDONLY)
+                self._qmp.send_fd(fd)
+            except OSError:
+                return 1
+            finally:
+                if fd != -1:
+                    os.close(fd)
         else:
             assert fd is not None
-            fd_param.append(str(fd))
+            try:
+                self._qmp.send_fd(fd)
+            except OSError:
+                return 1
 
-        proc = subprocess.run(
-            fd_param,
-            stdin=subprocess.DEVNULL,
-            stdout=subprocess.PIPE,
-            stderr=subprocess.STDOUT,
-            check=False,
-            close_fds=False,
-        )
-        if proc.stdout:
-            LOG.debug(proc.stdout)
-
-        return proc.returncode
+        return 0
 
     @staticmethod
     def _remove_if_exists(path: str) -> None:
diff --git a/python/qemu/qmp/__init__.py b/python/qemu/qmp/__init__.py
index 269516a79b..27a3e8f7af 100644
--- a/python/qemu/qmp/__init__.py
+++ b/python/qemu/qmp/__init__.py
@@ -17,6 +17,7 @@
 # This work is licensed under the terms of the GNU GPL, version 2.  See
 # the COPYING file in the top-level directory.
 
+import array
 import errno
 import json
 import logging
@@ -421,3 +422,17 @@ def is_scm_available(self) -> bool:
         @return True if SCM_RIGHTS is available, otherwise False.
         """
         return self.__sock.family == socket.AF_UNIX
+
+    def send_fd(self, fd: int) -> None:
+        """
+        Send a file descriptor to QEMU via SCM_RIGHTS.
+
+        @param fd (int): file descriptor do be sent
+
+        @raise OSError: if the sendmsg system call fails.
+        """
+        # Send a single space so that QEMU looks at the ancillary data
+        self.__sock.sendmsg((b" ", ),
+                            [(socket.SOL_SOCKET,
+                              socket.SCM_RIGHTS,
+                              array.array("i", [fd]).tobytes())])
-- 
2.31.1




  reply	other threads:[~2021-09-28 14:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-28 13:53 [PATCH 0/3] python: remove socket_scm_helper Paolo Bonzini
2021-09-28 13:53 ` Paolo Bonzini [this message]
2021-09-28 13:53 ` [PATCH 2/4] socket_scm_helper: remove Paolo Bonzini
2021-09-28 13:53 ` [PATCH 3/4] python: raise OSError from send_fd_scm Paolo Bonzini
2021-09-28 13:53 ` [PATCH 4/4] python: split the two sides of send_fd_scm Paolo Bonzini
2021-09-28 17:46 ` [PATCH 0/3] python: remove socket_scm_helper John Snow

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=20210928135309.199796-2-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.