From patchwork Wed Jan 16 10:21:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 212490 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 A776D2C0096 for ; Wed, 16 Jan 2013 22:18:28 +1100 (EST) Received: from localhost ([::1]:47588 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvQ8P-0008LC-Kx for incoming@patchwork.ozlabs.org; Wed, 16 Jan 2013 05:22:17 -0500 Received: from eggs.gnu.org ([208.118.235.92]:58448) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvQ7a-0007CO-DH for qemu-devel@nongnu.org; Wed, 16 Jan 2013 05:21:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TvQ7S-0002eT-Si for qemu-devel@nongnu.org; Wed, 16 Jan 2013 05:21:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:21532) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvQ7S-0002eO-K8 for qemu-devel@nongnu.org; Wed, 16 Jan 2013 05:21:18 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r0GALHi3029679 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 16 Jan 2013 05:21:18 -0500 Received: from rincewind.home.kraxel.org (ovpn-116-103.ams2.redhat.com [10.36.116.103]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r0GALF8J024250; Wed, 16 Jan 2013 05:21:16 -0500 Received: by rincewind.home.kraxel.org (Postfix, from userid 500) id A62A240C83; Wed, 16 Jan 2013 11:21:13 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Wed, 16 Jan 2013 11:21:09 +0100 Message-Id: <1358331673-6159-7-git-send-email-kraxel@redhat.com> In-Reply-To: <1358331673-6159-1-git-send-email-kraxel@redhat.com> References: <1358331673-6159-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH 06/10] chardev: add file chardev support to chardev-add (qmp) 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 support for file chardevs. Output file is mandatory, input file is optional. Signed-off-by: Gerd Hoffmann --- qapi-schema.json | 16 +++++++++++++- qemu-char.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qmp-commands.hx | 8 ++++++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/qapi-schema.json b/qapi-schema.json index 462511e..c70c118 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -3019,6 +3019,19 @@ { 'command': 'nbd-server-stop' } ## +# @ChardevFile: +# +# Configuration info for file chardevs. +# +# @in: #optional The name of the input file +# @out: The name of the output file +# +# Since: 1.4 +## +{ 'type': 'ChardevFile', 'data': { '*in' : 'str', + 'out' : 'str' } } + +## # @ChardevBackend: # # Configuration info for the new chardev backend. @@ -3027,7 +3040,8 @@ ## { 'type': 'ChardevDummy', 'data': { } } -{ 'union': 'ChardevBackend', 'data': { 'null' : 'ChardevDummy' } } +{ 'union': 'ChardevBackend', 'data': { 'file' : 'ChardevFile', + 'null' : 'ChardevDummy' } } ## # @ChardevReturn: diff --git a/qemu-char.c b/qemu-char.c index 73a5e37..d447d96 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -3011,6 +3011,64 @@ QemuOptsList qemu_chardev_opts = { }, }; +#ifdef _WIN32 + +static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) +{ + HANDLE out; + + if (file->in) { + error_setg(errp, "input file not supported"); + return NULL; + } + + out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (out == INVALID_HANDLE_VALUE) { + error_setg(errp, "open %s failed", file->out); + return NULL; + } + return qemu_chr_open_win_file(out); +} + +#else /* WIN32 */ + +static int qmp_chardev_open_file_source(char *src, int flags, + Error **errp) +{ + int fd = -1; + + TFR(fd = qemu_open(src, flags, 0666)); + if (fd == -1) { + error_setg(errp, "open %s: %s", src, strerror(errno)); + } + return fd; +} + +static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) +{ + int flags, in = -1, out = -1; + + flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY; + out = qmp_chardev_open_file_source(file->out, flags, errp); + if (error_is_set(errp)) { + return NULL; + } + + if (file->in) { + flags = O_RDONLY; + in = qmp_chardev_open_file_source(file->in, flags, errp); + if (error_is_set(errp)) { + qemu_close(out); + return NULL; + } + } + + return qemu_chr_open_fd(in, out); +} + +#endif /* WIN32 */ + ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, Error **errp) { @@ -3025,6 +3083,9 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, } switch (backend->kind) { + case CHARDEV_BACKEND_KIND_FILE: + chr = qmp_chardev_open_file(backend->file, errp); + break; case CHARDEV_BACKEND_KIND_NULL: chr = qemu_chr_open_null(NULL); break; diff --git a/qmp-commands.hx b/qmp-commands.hx index c9ab37c..4d382f4 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -2672,13 +2672,19 @@ Arguments: - "id": the chardev's ID, must be unique (json-string) - "backend": chardev backend type + parameters -Example: +Examples: -> { "execute" : "chardev-add", "arguments" : { "id" : "foo", "backend" : { "type" : "null", "data" : {} } } } <- { "return": {} } +-> { "execute" : "chardev-add", + "arguments" : { "id" : "bar", + "backend" : { "type" : "file", + "data" : { "out" : "/tmp/bar.log" } } } } +<- { "return": {} } + EQMP {