[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 03/25] python/console_socket: accept existing FD in initializer
From: |
John Snow |
Subject: |
[PULL 03/25] python/console_socket: accept existing FD in initializer |
Date: |
Fri, 13 Oct 2023 15:09:18 -0400 |
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>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-id: 20230928044943.849073-4-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@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
- [PULL 00/25] Python patches, John Snow, 2023/10/13
- [PULL 08/25] python/qmp: remove Server.wait_closed() call for Python 3.12, John Snow, 2023/10/13
- [PULL 03/25] python/console_socket: accept existing FD in initializer,
John Snow <=
- [PULL 02/25] python/machine: close sock_pair in cleanup path, John Snow, 2023/10/13
- [PULL 04/25] python/machine: use socketpair() for console connections, John Snow, 2023/10/13
- [PULL 09/25] configure: fix error message to say Python 3.8, John Snow, 2023/10/13
- [PULL 11/25] python/qemu/qmp/legacy: cmd(): drop cmd_id unused argument, John Snow, 2023/10/13
- [PULL 05/25] python/machine: use socketpair() for qtest connection, John Snow, 2023/10/13
- [PULL 07/25] Python/iotests: Add type hint for nbd module, John Snow, 2023/10/13
- [PULL 06/25] python/machine: remove unused sock_dir argument, John Snow, 2023/10/13
- [PULL 10/25] Python: Enable python3.12 support, John Snow, 2023/10/13
- [PULL 01/25] python/machine: move socket setup out of _base_args property, John Snow, 2023/10/13
- [PULL 12/25] qmp_shell.py: _fill_completion() use .command() instead of .cmd(), John Snow, 2023/10/13