diff mbox series

[v2,3/6] python/console_socket: accept existing FD in initializer

Message ID 20230725180337.2937292-4-jsnow@redhat.com
State New
Headers show
Series python/machine: use socketpair() for console socket | expand

Commit Message

John Snow July 25, 2023, 6:03 p.m. UTC
Useful if we want to use ConsoleSocket() for a socket created by
socketpair().

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/qemu/machine/console_socket.py | 29 +++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

Comments

Daniel P. Berrangé July 25, 2023, 6:19 p.m. UTC | #1
On Tue, Jul 25, 2023 at 02:03:34PM -0400, John Snow wrote:
> Useful if we want to use ConsoleSocket() for a socket created by
> socketpair().
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  python/qemu/machine/console_socket.py | 29 +++++++++++++++++++--------
>  1 file changed, 21 insertions(+), 8 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

With regards,
Daniel
Ani Sinha July 27, 2023, 5:41 a.m. UTC | #2
> On 25-Jul-2023, at 11:33 PM, John Snow <jsnow@redhat.com> wrote:
> 
> Useful if we want to use ConsoleSocket() for a socket created by
> socketpair().
> 
> Signed-off-by: John Snow <jsnow@redhat.com>

Reviewed-by: Ani Sinha <anisinha@redhat.com>

> ---
> python/qemu/machine/console_socket.py | 29 +++++++++++++++++++--------
> 1 file changed, 21 insertions(+), 8 deletions(-)
> 
> diff --git a/python/qemu/machine/console_socket.py b/python/qemu/machine/console_socket.py
> index 4e28ba9bb2..0a4e09ffc7 100644
> --- a/python/qemu/machine/console_socket.py
> +++ b/python/qemu/machine/console_socket.py
> @@ -24,19 +24,32 @@ class ConsoleSocket(socket.socket):
>     """
>     ConsoleSocket represents a socket attached to a char device.
> 
> -    Optionally (if drain==True), drains the socket and places the bytes
> -    into an in memory buffer for later processing.
> -
> -    Optionally a file path can be passed in and we will also
> -    dump the characters to this file for debugging purposes.
> +    :param address: An AF_UNIX path or address.
> +    :param sock_fd: Optionally, an existing socket file descriptor.
> +                    One of address or sock_fd must be specified.
> +    :param file: Optionally, a filename to log to.
> +    :param drain: Optionally, drains the socket and places the bytes
> +                  into an in memory buffer for later processing.
>     """
> -    def __init__(self, address: str, file: Optional[str] = None,
> +    def __init__(self,
> +                 address: Optional[str] = None,
> +                 sock_fd: Optional[int] = None,
> +                 file: Optional[str] = None,
>                  drain: bool = False):
> +        if address is None and sock_fd is None:
> +            raise ValueError("one of 'address' or 'sock_fd' must be specified")
> +        if address is not None and sock_fd is not None:
> +            raise ValueError("can't specify both 'address' and 'sock_fd'")
> +
>         self._recv_timeout_sec = 300.0
>         self._sleep_time = 0.5
>         self._buffer: Deque[int] = deque()
> -        socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM)
> -        self.connect(address)
> +        if address is not None:
> +            socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM)
> +            self.connect(address)
> +        else:
> +            assert sock_fd is not None
> +            socket.socket.__init__(self, fileno=sock_fd)
>         self._logfile = None
>         if file:
>             # pylint: disable=consider-using-with
> -- 
> 2.41.0
>
diff mbox series

Patch

diff --git a/python/qemu/machine/console_socket.py b/python/qemu/machine/console_socket.py
index 4e28ba9bb2..0a4e09ffc7 100644
--- a/python/qemu/machine/console_socket.py
+++ b/python/qemu/machine/console_socket.py
@@ -24,19 +24,32 @@  class ConsoleSocket(socket.socket):
     """
     ConsoleSocket represents a socket attached to a char device.
 
-    Optionally (if drain==True), drains the socket and places the bytes
-    into an in memory buffer for later processing.
-
-    Optionally a file path can be passed in and we will also
-    dump the characters to this file for debugging purposes.
+    :param address: An AF_UNIX path or address.
+    :param sock_fd: Optionally, an existing socket file descriptor.
+                    One of address or sock_fd must be specified.
+    :param file: Optionally, a filename to log to.
+    :param drain: Optionally, drains the socket and places the bytes
+                  into an in memory buffer for later processing.
     """
-    def __init__(self, address: str, file: Optional[str] = None,
+    def __init__(self,
+                 address: Optional[str] = None,
+                 sock_fd: Optional[int] = None,
+                 file: Optional[str] = None,
                  drain: bool = False):
+        if address is None and sock_fd is None:
+            raise ValueError("one of 'address' or 'sock_fd' must be specified")
+        if address is not None and sock_fd is not None:
+            raise ValueError("can't specify both 'address' and 'sock_fd'")
+
         self._recv_timeout_sec = 300.0
         self._sleep_time = 0.5
         self._buffer: Deque[int] = deque()
-        socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM)
-        self.connect(address)
+        if address is not None:
+            socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM)
+            self.connect(address)
+        else:
+            assert sock_fd is not None
+            socket.socket.__init__(self, fileno=sock_fd)
         self._logfile = None
         if file:
             # pylint: disable=consider-using-with