From patchwork Wed Mar 27 19:29:39 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 231812 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 404352C00D1 for ; Thu, 28 Mar 2013 06:27:02 +1100 (EST) Received: from localhost ([::1]:35879 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKvzt-0004wh-JU for incoming@patchwork.ozlabs.org; Wed, 27 Mar 2013 15:26:57 -0400 Received: from eggs.gnu.org ([208.118.235.92]:60727) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKvzI-0004q3-4C for qemu-devel@nongnu.org; Wed, 27 Mar 2013 15:26:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UKvzF-0002KC-OG for qemu-devel@nongnu.org; Wed, 27 Mar 2013 15:26:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52311) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKvzF-0002K0-Gr for qemu-devel@nongnu.org; Wed, 27 Mar 2013 15:26:17 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r2RJQG9Z025024 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 27 Mar 2013 15:26:16 -0400 Received: from shalem.localdomain.com (vpn1-6-32.ams2.redhat.com [10.36.6.32]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r2RJQDDC010123; Wed, 27 Mar 2013 15:26:15 -0400 From: Hans de Goede To: qemu-devel@nongnu.org Date: Wed, 27 Mar 2013 20:29:39 +0100 Message-Id: <1364412581-3672-2-git-send-email-hdegoede@redhat.com> In-Reply-To: <1364412581-3672-1-git-send-email-hdegoede@redhat.com> References: <1364412581-3672-1-git-send-email-hdegoede@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Paolo Bonzini , Hans de Goede Subject: [Qemu-devel] [PATCH 1/3] qemu-char: Add qemu_chr_fe_claim / _release helper functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add qemu_chr_fe_claim / _release helper functions for properly dealing with avail_connections. Signed-off-by: Hans de Goede --- hw/qdev-properties-system.c | 5 ++--- include/char/char.h | 29 +++++++++++++++++++++++++++++ qemu-char.c | 23 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/hw/qdev-properties-system.c b/hw/qdev-properties-system.c index 12a87d5..e27d708 100644 --- a/hw/qdev-properties-system.c +++ b/hw/qdev-properties-system.c @@ -123,11 +123,10 @@ static int parse_chr(DeviceState *dev, const char *str, void **ptr) if (chr == NULL) { return -ENOENT; } - if (chr->avail_connections < 1) { + if (qemu_chr_fe_claim(chr) != 0) { return -EEXIST; } *ptr = chr; - --chr->avail_connections; return 0; } @@ -140,7 +139,7 @@ static void release_chr(Object *obj, const char *name, void *opaque) if (chr) { qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); - ++chr->avail_connections; + qemu_chr_fe_release(chr); } } diff --git a/include/char/char.h b/include/char/char.h index 1457e80..920a5a0 100644 --- a/include/char/char.h +++ b/include/char/char.h @@ -188,6 +188,35 @@ int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg); int qemu_chr_fe_get_msgfd(CharDriverState *s); /** + * @qemu_chr_fe_claim: + * + * Claim a backend before using it, should be called before calling + * qemu_chr_add_handlers(). + * + * Returns: -1 if the backend is already in use by another frontend, 0 on + * success. + */ +int qemu_chr_fe_claim(CharDriverState *s); + +/** + * @qemu_chr_fe_claim_no_fail: + * + * Like qemu_chr_fe_claim, but will exit qemu with an error when the + * backend is already in use. + */ +void qemu_chr_fe_claim_no_fail(CharDriverState *s); + +/** + * @qemu_chr_fe_claim: + * + * Release a backend for use by another frontend. + * + * Returns: -1 if the backend is already in use by another frontend, 0 on + * success. + */ +void qemu_chr_fe_release(CharDriverState *s); + +/** * @qemu_chr_be_can_write: * * Determine how much data the front end can currently accept. This function diff --git a/qemu-char.c b/qemu-char.c index edf3779..da6cbe2 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -3418,6 +3418,29 @@ int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond, return tag; } +int qemu_chr_fe_claim(CharDriverState *s) +{ + if (s->avail_connections < 1) { + return -1; + } + s->avail_connections--; + return 0; +} + +void qemu_chr_fe_claim_no_fail(CharDriverState *s) +{ + if (qemu_chr_fe_claim(s) != 0) { + fprintf(stderr, "%s: error chardev \"%s\" already used\n", + __func__, s->label); + exit(1); + } +} + +void qemu_chr_fe_release(CharDriverState *s) +{ + s->avail_connections++; +} + void qemu_chr_delete(CharDriverState *chr) { QTAILQ_REMOVE(&chardevs, chr, next);