From patchwork Sat Oct 22 09:52:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 685381 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3t1JpZ4xGJz9srZ for ; Sat, 22 Oct 2016 21:35:17 +1100 (AEDT) Received: from localhost ([::1]:36503 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxtdy-0003PA-IH for incoming@patchwork.ozlabs.org; Sat, 22 Oct 2016 06:35:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37278) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxt06-0002EW-3f for qemu-devel@nongnu.org; Sat, 22 Oct 2016 05:54:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bxt01-0004GY-7Y for qemu-devel@nongnu.org; Sat, 22 Oct 2016 05:54:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56214) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bxt00-0004G8-WA for qemu-devel@nongnu.org; Sat, 22 Oct 2016 05:53:57 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 31A143D94B for ; Sat, 22 Oct 2016 09:53:56 +0000 (UTC) Received: from localhost (ovpn-116-7.phx2.redhat.com [10.3.116.7]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u9M9rr9o024620; Sat, 22 Oct 2016 05:53:55 -0400 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Sat, 22 Oct 2016 12:52:49 +0300 Message-Id: <20161022095318.17775-10-marcandre.lureau@redhat.com> In-Reply-To: <20161022095318.17775-1-marcandre.lureau@redhat.com> References: <20161022095318.17775-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Sat, 22 Oct 2016 09:53:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 09/38] char: introduce CharBackend X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This new structure is meant to keep the details associated with a char driver usage. On initialization, it gets a tag from the mux backend. It can change its handlers thanks to qemu_chr_fe_set_handlers(). This structure is introduced so that all frontend will be moved to hold and use a CharBackend. This will allow to better track char usage and allocation, and help prevent some memory leaks or corruption. Signed-off-by: Marc-André Lureau --- qemu-char.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/sysemu/char.h | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/qemu-char.c b/qemu-char.c index 29b339f..9722fb6 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -910,6 +910,63 @@ static CharDriverState *qemu_chr_open_mux(const char *id, return chr; } +CharDriverState *qemu_chr_fe_get_driver(CharBackend *be) +{ + assert(be); + + return be->chr; +} + +bool qemu_chr_fe_init(CharBackend *b, CharDriverState *s, Error **errp) +{ + int tag = 0; + + assert(b); + assert(s); + + if (s->is_mux) { + tag = mux_chr_new_handler_tag(s, errp); + if (tag < 0) { + return false; + } + } + + b->tag = tag; + b->chr = s; + + return true; +} + +void qemu_chr_fe_set_handlers(CharBackend *b, + IOCanReadHandler *fd_can_read, + IOReadHandler *fd_read, + IOEventHandler *fd_event, + void *opaque, + GMainContext *context) +{ + assert(b); + + if (!b->chr) { + return; + } + + qemu_chr_set_handlers(b->chr, fd_can_read, fd_read, + fd_event, opaque, context, b->tag); + + if (b->chr->is_mux) { + mux_chr_set_handlers(b->chr, context); + } +} + +void qemu_chr_fe_take_focus(CharBackend *b) +{ + assert(b); + assert(b->chr); + + if (b->chr->is_mux) { + mux_set_focus(b->chr->opaque, b->tag); + } +} typedef struct IOWatchPoll { @@ -4184,6 +4241,9 @@ void qemu_chr_disconnect(CharDriverState *chr) static void qemu_chr_free_common(CharDriverState *chr) { + if (chr->be) { + chr->be->chr = NULL; + } g_free(chr->filename); g_free(chr->label); if (chr->logfd != -1) { diff --git a/include/sysemu/char.h b/include/sysemu/char.h index f0abbac..01cf0cc 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -72,6 +72,12 @@ typedef enum { QEMU_CHAR_FEATURE_LAST, } CharDriverFeature; +/* This is the backend as seen by frontend, the actual backend is + * CharDriverState */ +typedef struct CharBackend { + CharDriverState *chr; + int tag; +} CharBackend; struct CharDriverState { QemuMutex chr_write_lock; @@ -156,6 +162,8 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend); * Returns: a new character backend */ CharDriverState *qemu_chr_new(const char *label, const char *filename); + + /** * @qemu_chr_disconnect: * @@ -425,6 +433,48 @@ void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len); */ void qemu_chr_be_event(CharDriverState *s, int event); +/** + * @qemu_chr_fe_init: + * + * Initializes a front end for the given CharBackend and CharDriver. + * + * Returns: false on error. + */ +bool qemu_chr_fe_init(CharBackend *b, CharDriverState *s, Error **errp); + +/** + * @qemu_chr_fe_get_driver: + * + * Returns the driver associated with a CharBackend or NULL. + */ +CharDriverState *qemu_chr_fe_get_driver(CharBackend *be); + +/** + * @qemu_chr_fe_set_handlers: + * @b: a CharBackend + * @fd_can_read: callback to get the amount of data the frontend may + * receive + * @fd_read: callback to receive data from char + * @fd_event: event callback + * @opaque: an opaque pointer for the callbacks + * @context: a main loop context or NULL for the default + * + * Set the front end char handlers. + */ +void qemu_chr_fe_set_handlers(CharBackend *b, + IOCanReadHandler *fd_can_read, + IOReadHandler *fd_read, + IOEventHandler *fd_event, + void *opaque, + GMainContext *context); + +/** + * @qemu_chr_fe_take_focus: + * + * Take the focus (if the front end is muxed) + */ +void qemu_chr_fe_take_focus(CharBackend *b); + void qemu_chr_add_handlers(CharDriverState *s, IOCanReadHandler *fd_can_read, IOReadHandler *fd_read,