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 v2 03/23] switch chardev to QemuOpts: infrastructure, null device
Date: Thu, 10 Sep 2009 10:58:35 +0200	[thread overview]
Message-ID: <1252573135-27688-4-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1252573135-27688-1-git-send-email-kraxel@redhat.com>

start switching chardevs to QemuOpts.  This patch adds the
infrastructure and converts the null device.

The patch brings two new functions:

qemu_chr_open_opts()
	same as qemu_chr_open(), but uses QemuOpts instead of a
	option char string.

qemu_chr_parse_compat()
	accepts a traditional chardev option string, returns the
	corresponding QemuOpts instance, to handle backward
	compatibility.

The patch also adds a new -chardev switch which can be used to create
named+unconnected chardevs, like this:

	-chardev null,id=test

This uses the new qemu_chr_open_opts.  Thus with this patch alone only
the null device works.  The other devices will follow ...

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 qemu-char.c     |   71 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 qemu-char.h     |    4 +++
 qemu-config.c   |    9 +++++++
 qemu-config.h   |    1 +
 qemu-options.hx |    2 +
 vl.c            |   10 +++++++
 6 files changed, 93 insertions(+), 4 deletions(-)

diff --git a/qemu-char.c b/qemu-char.c
index c25ed1c..bd2eca8 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -214,7 +214,7 @@ static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     return len;
 }
 
-static CharDriverState *qemu_chr_open_null(void)
+static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
 {
     CharDriverState *chr;
 
@@ -2216,10 +2216,76 @@ static CharDriverState *qemu_chr_open_tcp(const char *host_str,
     return NULL;
 }
 
+static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
+{
+    QemuOpts *opts;
+
+    opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
+    if (NULL == opts)
+        return NULL;
+
+    if (strcmp(filename, "null") == 0) {
+        qemu_opt_set(opts, "backend", "null");
+        return opts;
+    }
+
+    qemu_opts_del(opts);
+    return NULL;
+}
+
+static const struct {
+    const char *name;
+    CharDriverState *(*open)(QemuOpts *opts);
+} backend_table[] = {
+    { .name = "null",      .open = qemu_chr_open_null },
+};
+
+CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
+                                    void (*init)(struct CharDriverState *s))
+{
+    CharDriverState *chr;
+    int i;
+
+    if (qemu_opts_id(opts) == NULL) {
+        fprintf(stderr, "chardev: no id specified\n");
+        return NULL;
+    }
+
+    for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
+        if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
+            break;
+    }
+    if (i == ARRAY_SIZE(backend_table)) {
+        fprintf(stderr, "chardev: backend \"%s\" not found\n",
+                qemu_opt_get(opts, "backend"));
+        return NULL;
+    }
+
+    chr = backend_table[i].open(opts);
+    if (!chr) {
+        fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
+                qemu_opt_get(opts, "backend"));
+        return NULL;
+    }
+
+    if (!chr->filename)
+        chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
+    chr->init = init;
+    chr->label = qemu_strdup(qemu_opts_id(opts));
+    TAILQ_INSERT_TAIL(&chardevs, chr, next);
+    return chr;
+}
+
 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
 {
     const char *p;
     CharDriverState *chr;
+    QemuOpts *opts;
+
+    opts = qemu_chr_parse_compat(label, filename);
+    if (opts) {
+        return qemu_chr_open_opts(opts, init);
+    }
 
     if (!strcmp(filename, "vc")) {
         chr = text_console_init(NULL);
@@ -2227,9 +2293,6 @@ CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*i
     if (strstart(filename, "vc:", &p)) {
         chr = text_console_init(p);
     } else
-    if (!strcmp(filename, "null")) {
-        chr = qemu_chr_open_null();
-    } else
     if (strstart(filename, "tcp:", &p)) {
         chr = qemu_chr_open_tcp(p, 0, 0);
     } else
diff --git a/qemu-char.h b/qemu-char.h
index df620bc..9bff0c7 100644
--- a/qemu-char.h
+++ b/qemu-char.h
@@ -3,6 +3,8 @@
 
 #include "qemu-common.h"
 #include "sys-queue.h"
+#include "qemu-option.h"
+#include "qemu-config.h"
 
 /* character device */
 
@@ -68,6 +70,8 @@ struct CharDriverState {
     TAILQ_ENTRY(CharDriverState) next;
 };
 
+CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
+                                    void (*init)(struct CharDriverState *s));
 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s));
 void qemu_chr_close(CharDriverState *chr);
 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...);
diff --git a/qemu-config.c b/qemu-config.c
index 4808db0..b156c24 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -75,6 +75,14 @@ QemuOptsList qemu_drive_opts = {
     },
 };
 
+QemuOptsList qemu_chardev_opts = {
+    .name = "chardev",
+    .head = TAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
+    .desc = {
+        { /* end if list */ }
+    },
+};
+
 QemuOptsList qemu_device_opts = {
     .name = "device",
     .head = TAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
@@ -90,6 +98,7 @@ QemuOptsList qemu_device_opts = {
 
 static QemuOptsList *lists[] = {
     &qemu_drive_opts,
+    &qemu_chardev_opts,
     &qemu_device_opts,
     NULL,
 };
diff --git a/qemu-config.h b/qemu-config.h
index e49d715..13b0f19 100644
--- a/qemu-config.h
+++ b/qemu-config.h
@@ -2,6 +2,7 @@
 #define QEMU_CONFIG_H
 
 extern QemuOptsList qemu_drive_opts;
+extern QemuOptsList qemu_chardev_opts;
 extern QemuOptsList qemu_device_opts;
 
 int qemu_set_option(const char *str);
diff --git a/qemu-options.hx b/qemu-options.hx
index ce38a3b..d3aa55b 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1195,6 +1195,8 @@ STEXI
 @table @option
 ETEXI
 
+DEF("chardev", HAS_ARG, QEMU_OPTION_chardev, \
+    "-chardev spec   create unconnected chardev\n")
 DEF("serial", HAS_ARG, QEMU_OPTION_serial, \
     "-serial dev     redirect the serial port to char device 'dev'\n")
 STEXI
diff --git a/vl.c b/vl.c
index c6c6a6b..eb7a9a1 100644
--- a/vl.c
+++ b/vl.c
@@ -5286,6 +5286,16 @@ int main(int argc, char **argv, char **envp)
                 monitor_devices[monitor_device_index] = optarg;
                 monitor_device_index++;
                 break;
+            case QEMU_OPTION_chardev:
+                opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
+                if (!opts) {
+                    fprintf(stderr, "parse error: %s\n", optarg);
+                    exit(1);
+                }
+                if (NULL == qemu_chr_open_opts(opts, NULL)) {
+                    exit(1);
+                }
+                break;
             case QEMU_OPTION_serial:
                 if (serial_device_index >= MAX_SERIAL_PORTS) {
                     fprintf(stderr, "qemu: too many serial ports\n");
-- 
1.6.2.5

  parent reply	other threads:[~2009-09-10  8:59 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-10  8:58 [Qemu-devel] [PATCH v2 00/23] switch sockets+chardev to QemuOpts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 01/23] QemuOpts: split option parser into two functions Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 02/23] qemu-option.h include protectors Gerd Hoffmann
2009-09-10  8:58 ` Gerd Hoffmann [this message]
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 04/23] convert file+pipe chardevs to QemuOpts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 05/23] sockets: add unix_connect_opts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 06/23] sockets: add unix_listen_opts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 07/23] sockets: add unix_*_opts for windows Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 08/23] sockets: add inet_connect_opts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 09/23] sockets: add inet_listen_opts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 10/23] convert unix+tcp chardevs to QemuOpts Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 11/23] convert pty chardev " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 12/23] convert stdio " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 13/23] convert msmouse " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 14/23] convert braille " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 15/23] convert windows console " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 16/23] convert tty + parport chardevs " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 17/23] convert vc chardev " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 18/23] convert mux " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 19/23] convert udp " Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 20/23] Allow -serial chardev:<name> Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 21/23] qdev: add parser for chardev properties Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 22/23] monitor: fix muxing Gerd Hoffmann
2009-09-10  8:58 ` [Qemu-devel] [PATCH v2 23/23] move mux focus field from CharDriverState to MuxDriver Gerd Hoffmann

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=1252573135-27688-4-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).