qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 06/10] chardev: add file chardev support to chardev-add (qmp)
Date: Wed, 16 Jan 2013 11:21:09 +0100	[thread overview]
Message-ID: <1358331673-6159-7-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1358331673-6159-1-git-send-email-kraxel@redhat.com>

Add support for file chardevs.  Output file is mandatory,
input file is optional.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 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
 
     {
-- 
1.7.9.7

  parent reply	other threads:[~2013-01-16 10:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-16 10:21 [Qemu-devel] [PULL 00/10] chardev hotplug support Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 01/10] chardev: add error reporting for qemu_chr_new_from_opts Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 02/10] chardev: fix QemuOpts lifecycle Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 03/10] chardev: reduce chardev ifdef mess a bit Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 04/10] chardev: add qmp hotplug commands, with null chardev support Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 05/10] chardev: add hmp hotplug commands Gerd Hoffmann
2013-01-16 10:21 ` Gerd Hoffmann [this message]
2013-01-16 10:21 ` [Qemu-devel] [PATCH 07/10] chardev: add serial chardev support to chardev-add (qmp) Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 08/10] chardev: add parallel " Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 09/10] chardev: add socket " Gerd Hoffmann
2013-01-16 10:21 ` [Qemu-devel] [PATCH 10/10] chardev: add pty " Gerd Hoffmann
2013-01-16 13:23 ` [Qemu-devel] [PULL 00/10] chardev hotplug support Eric Blake
2013-01-17  1:22 ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1358331673-6159-7-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).