From patchwork Mon Jun 22 14:31:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 191354 Delivered-To: patch@linaro.org Received: by 2002:a92:1f07:0:0:0:0:0 with SMTP id i7csp1103905ile; Mon, 22 Jun 2020 07:41:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWlgfVhGpfahQ4U8iNMI9/uzutuywWxcgLchVxrUQ/k9XmtqmHOvDkeB0gilPjgu53FqAk X-Received: by 2002:a25:c683:: with SMTP id k125mr29621825ybf.305.1592836872241; Mon, 22 Jun 2020 07:41:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592836872; cv=none; d=google.com; s=arc-20160816; b=bzjN32OBNa1X6+F3MDm2YGSlVCEGoVthPo1rb8WnBm7JSQdJNvSE/DL+Pbmg6GyOcu fFTi94pLGE3IiuxNaMyrwdAsz4DVek6lugW9hPloGZMYeeSh/ymwsPMCRVmZNCNl1R71 R24CXqkoPNC0icYoqGgp5btzBcCxuKpOJLjrUQWW67nK46C9N6qm8Wx9t1tEie0okIKL Aw6GXErf8xTw3buQ0ul/QmweG/gVLGkvfTj8xV490qYa1wr+YG/fPsV8BYyA9QgovpcG LvnAwDzP4zEI1H8lzKu3N/Ok4esaJtg1tBkyPrPj5qTL4qx6su1quMlz0Xnkq6eYEJpZ u83A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=SfCKKjXRpSzktD2zHuCdVYnzBzWod3odSIiX8dUA3RA=; b=Kr37YgXIMjgqBnrHXVic49lOmz3LIlHKrgrxrS5mdJwP+2YC0qX3JJDRzBQqb8W7ie Z31EqJGYWKmLi0nN/SsMEudhaNYCKNBkR3EWxgxHTWLOgURBubIS+AHAA+ha1qQFKonD 8AFgi53aS7ON1+YfYeK7KdH/wGX45LWIYYF6JvrwxxbDYZ3qGGRKet9VqiT63u6cSEud UO5HUxgn5KTfOlyR8JWtbP9HL6GOX2duVv4zxqYwv2X6eVGhaYH3Dqt+hiL2l/DCHK5g pC/SAoqAxhBxUHlvIbnoF+3f1BjPFdjHLfMgbcqPp5QHBu/E7lrTaaNz8z7kaVGo1H+3 cTUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="Xar42K/1"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d205si12862737ybh.435.2020.06.22.07.41.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 Jun 2020 07:41:12 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="Xar42K/1"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:49074 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jnNdH-0003y1-Cr for patch@linaro.org; Mon, 22 Jun 2020 10:41:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40034) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jnNUm-00073f-KJ for qemu-devel@nongnu.org; Mon, 22 Jun 2020 10:32:24 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]:35651) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jnNUk-0005g7-6e for qemu-devel@nongnu.org; Mon, 22 Jun 2020 10:32:24 -0400 Received: by mail-wr1-x42c.google.com with SMTP id g18so7858384wrm.2 for ; Mon, 22 Jun 2020 07:32:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SfCKKjXRpSzktD2zHuCdVYnzBzWod3odSIiX8dUA3RA=; b=Xar42K/1wjtXTgnbv0KVk7mQFYnxiGeu4JK4TeU0aAiM6kwk4XSzTnkRSCFiNHCWe7 siWnL8D9HevP5Wv+vXMoZlwEy2thSP40n/u1gVFC1ABdURO3y9q9YH3VQYHW1HZBMIpj 3EGy8AEfHXQRZSN9GbdMg4baZASUDbSa6cd8BAcH3Z7ndGfYCDYQ5lQFhM/pE+V+m6jr XTt42PKolpQP9oIzRkQHIMtfS73F7xEeAVP5w4nFZ3gELPd0DQBuqkTFdZ/NxWMsY2y7 wKHuJyO+au0b11EEgGFEO6P1+Y+Ux7flXPHJwX9lDNPTYSoQVEDVcVDGscLcGhcJFCWy iDPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SfCKKjXRpSzktD2zHuCdVYnzBzWod3odSIiX8dUA3RA=; b=Laa/xzEnPcUz6y/nBIEbNO5dJuQFr0MKxPROyifB5+tYR711vk5uxQ+tAegMd8XAxx xIGKfHplMHad1drytKtR2gmU0HGHVNyHQAiYLJdm5JMSDp3+WlJ8PamJHfq+xEu+GPdN L9kr3/OuMVlyRrOajcKiUuDs2jG01isAYZ7mp6BycR/njHGSDkJRndVA3YTspdSN3ei3 aP4ZJ82A+BixUobqWY8lw+CQCJu8wcoDV/nMHqjqTT0iUQE5ji4CVxIvXUUIMtnxG9eA UWLDZQxjiyv/Y+t9CJOQriygpB8w174Dc2j1zkz/Q7VZOkt714L8rGJXjCaeuS0gba4C 9dQw== X-Gm-Message-State: AOAM533r3qPe2XLLUM2C+vCugdAkjumoVh/fAN0JP+eOk5XiobQpgIkf 7h9eNOnWMATwiEj7Dd5dUitBHw== X-Received: by 2002:adf:edc8:: with SMTP id v8mr17920774wro.125.1592836340554; Mon, 22 Jun 2020 07:32:20 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id n16sm12311571wrq.39.2020.06.22.07.32.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 07:32:12 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id A7DED1FF98; Mon, 22 Jun 2020 15:32:05 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v1 09/18] python/qemu: Add ConsoleSocket for optional use in QEMUMachine Date: Mon, 22 Jun 2020 15:31:55 +0100 Message-Id: <20200622143204.12921-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200622143204.12921-1-alex.bennee@linaro.org> References: <20200622143204.12921-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42c; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42c.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, berrange@redhat.com, Robert Foley , =?utf-8?q?Alex_Benn=C3=A9e?= , richard.henderson@linaro.org, f4bug@amsat.org, =?utf-8?q?Philippe_Mat?= =?utf-8?q?hieu-Daud=C3=A9?= , cota@braap.org, Cleber Rosa , Peter Puhov , aurelien@aurel32.net, Eduardo Habkost Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Robert Foley We add the ConsoleSocket object, which has a socket interface and which will consume all arriving characters on the socket, placing them into an in memory buffer. This will also provide those chars via recv() as would a regular socket. ConsoleSocket also has the option of dumping the console bytes to a log file. We also give QEMUMachine the option of using ConsoleSocket to drain and to use for logging console to a file. By default QEMUMachine does not use ConsoleSocket. This is added in preparation for use by basevm.py in a later commit. This is a workaround we found was needed for basevm.py since there is a known issue where QEMU will hang waiting for console characters to be consumed. Cc: Eduardo Habkost Cc: Cleber Rosa Signed-off-by: Robert Foley Reviewed-by: Peter Puhov Acked-by: Alex Bennée Tested-by: Philippe Mathieu-Daudé Signed-off-by: Alex Bennée Message-Id: <20200601211421.1277-9-robert.foley@linaro.org> --- python/qemu/console_socket.py | 110 ++++++++++++++++++++++++++++++++++ python/qemu/machine.py | 23 +++++-- 2 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 python/qemu/console_socket.py -- 2.20.1 diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py new file mode 100644 index 00000000000..830cb7c6282 --- /dev/null +++ b/python/qemu/console_socket.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +# +# This python module implements a ConsoleSocket object which is +# designed always drain the socket itself, and place +# the bytes into a 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 debug. +# +# Copyright 2020 Linaro +# +# Authors: +# Robert Foley +# +# This code is licensed under the GPL version 2 or later. See +# the COPYING file in the top-level directory. +# +import asyncore +import socket +import threading +import io +import os +import sys +from collections import deque +import time +import traceback + +class ConsoleSocket(asyncore.dispatcher): + + def __init__(self, address, file=None): + self._recv_timeout_sec = 300 + self._buffer = deque() + self._asyncore_thread = None + self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self._sock.connect(address) + self._logfile = None + if file: + self._logfile = open(file, "w") + asyncore.dispatcher.__init__(self, sock=self._sock) + self._open = True + self._thread_start() + + def _thread_start(self): + """Kick off a thread to wait on the asyncore.loop""" + if self._asyncore_thread is not None: + return + self._asyncore_thread = threading.Thread(target=asyncore.loop, + kwargs={'timeout':1}) + self._asyncore_thread.daemon = True + self._asyncore_thread.start() + + def handle_close(self): + """redirect close to base class""" + # Call the base class close, but not self.close() since + # handle_close() occurs in the context of the thread which + # self.close() attempts to join. + asyncore.dispatcher.close(self) + + def close(self): + """Close the base object and wait for the thread to terminate""" + if self._open: + self._open = False + asyncore.dispatcher.close(self) + if self._asyncore_thread is not None: + thread, self._asyncore_thread = self._asyncore_thread, None + thread.join() + if self._logfile: + self._logfile.close() + self._logfile = None + + def handle_read(self): + """process arriving characters into in memory _buffer""" + try: + data = asyncore.dispatcher.recv(self, 1) + # latin1 is needed since there are some chars + # we are receiving that cannot be encoded to utf-8 + # such as 0xe2, 0x80, 0xA6. + string = data.decode("latin1") + except: + print("Exception seen.") + traceback.print_exc() + return + if self._logfile: + self._logfile.write("{}".format(string)) + self._logfile.flush() + for c in string: + self._buffer.extend(c) + + def recv(self, n=1, sleep_delay_s=0.1): + """Return chars from in memory buffer""" + start_time = time.time() + while len(self._buffer) < n: + time.sleep(sleep_delay_s) + elapsed_sec = time.time() - start_time + if elapsed_sec > self._recv_timeout_sec: + raise socket.timeout + chars = ''.join([self._buffer.popleft() for i in range(n)]) + # We choose to use latin1 to remain consistent with + # handle_read() and give back the same data as the user would + # receive if they were reading directly from the + # socket w/o our intervention. + return chars.encode("latin1") + + def set_blocking(self): + """Maintain compatibility with socket API""" + pass + + def settimeout(self, seconds): + """Set current timeout on recv""" + self._recv_timeout_sec = seconds diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 041c615052e..c25f0b42cf6 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -26,6 +26,7 @@ import socket import tempfile from typing import Optional, Type from types import TracebackType +from qemu.console_socket import ConsoleSocket from . import qmp @@ -75,7 +76,8 @@ class QEMUMachine: def __init__(self, binary, args=None, wrapper=None, name=None, test_dir="/var/tmp", monitor_address=None, - socket_scm_helper=None, sock_dir=None): + socket_scm_helper=None, sock_dir=None, + drain_console=False, console_log=None): ''' Initialize a QEMUMachine @@ -86,6 +88,9 @@ class QEMUMachine: @param test_dir: where to create socket and log file @param monitor_address: address for QMP monitor @param socket_scm_helper: helper program, required for send_fd_scm() + @param sock_dir: where to create socket (overrides test_dir for sock) + @param console_log: (optional) path to console log file + @param drain_console: (optional) True to drain console socket to buffer @note: Qemu process is not started until launch() is used. ''' if args is None: @@ -122,6 +127,12 @@ class QEMUMachine: self._console_address = None self._console_socket = None self._remove_files = [] + self._console_log_path = console_log + if self._console_log_path: + # In order to log the console, buffering needs to be enabled. + self._drain_console = True + else: + self._drain_console = drain_console def __enter__(self): return self @@ -580,7 +591,11 @@ class QEMUMachine: Returns a socket connected to the console """ if self._console_socket is None: - self._console_socket = socket.socket(socket.AF_UNIX, - socket.SOCK_STREAM) - self._console_socket.connect(self._console_address) + if self._drain_console: + self._console_socket = ConsoleSocket(self._console_address, + file=self._console_log_path) + else: + self._console_socket = socket.socket(socket.AF_UNIX, + socket.SOCK_STREAM) + self._console_socket.connect(self._console_address) return self._console_socket