All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com,
	coiby.xu@gmail.com, mreitz@redhat.com, stefanha@redhat.com
Subject: [PATCH v2 16/20] monitor: Create QAPIfied monitor_init()
Date: Mon, 24 Feb 2020 15:30:04 +0100	[thread overview]
Message-ID: <20200224143008.13362-17-kwolf@redhat.com> (raw)
In-Reply-To: <20200224143008.13362-1-kwolf@redhat.com>

This adds a new QAPI-based monitor_init() function. The existing
monitor_init_opts() is rewritten to simply put its QemuOpts parameter
into a visitor and pass the resulting QAPI object to monitor_init().

This will cause some change in those error messages for the monitor
options in the system emulator that are now generated by the visitor
rather than explicitly checked in monitor_init_opts().

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qapi/control.json         | 36 ++++++++++++++++++
 include/monitor/monitor.h |  2 +
 monitor/monitor.c         | 77 +++++++++++++++++++++------------------
 3 files changed, 80 insertions(+), 35 deletions(-)

diff --git a/qapi/control.json b/qapi/control.json
index 759c20e76f..3ee086aec7 100644
--- a/qapi/control.json
+++ b/qapi/control.json
@@ -216,3 +216,39 @@
 # <- { "return": {} }
 ##
 { 'command': 'quit' }
+
+##
+# @MonitorMode:
+#
+# An enumeration of monitor modes.
+#
+# @readline: HMP monitor (human-oriented command line interface)
+#
+# @control: QMP monitor (JSON-based machine interface)
+#
+# Since: 5.0
+##
+{ 'enum': 'MonitorMode', 'data': [ 'readline', 'control' ] }
+
+##
+# @MonitorOptions:
+#
+# Options to be used for adding a new monitor.
+#
+# @id:          Name of the monitor
+#
+# @mode:        Selects the monitor mode (default: readline)
+#
+# @pretty:      Enables pretty printing (QMP only)
+#
+# @chardev:     Name of a character device to expose the monitor on
+#
+# Since: 5.0
+##
+{ 'struct': 'MonitorOptions',
+  'data': {
+      '*id': 'str',
+      '*mode': 'MonitorMode',
+      '*pretty': 'bool',
+      'chardev': 'str'
+  } }
diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
index b7bdd2bb2a..db1112552c 100644
--- a/include/monitor/monitor.h
+++ b/include/monitor/monitor.h
@@ -7,6 +7,7 @@
 
 extern __thread Monitor *cur_mon;
 typedef struct MonitorHMP MonitorHMP;
+typedef struct MonitorOptions MonitorOptions;
 
 #define QMP_REQ_QUEUE_LEN_MAX 8
 
@@ -18,6 +19,7 @@ void monitor_init_globals(void);
 void monitor_init_globals_core(void);
 void monitor_init_qmp(Chardev *chr, bool pretty);
 void monitor_init_hmp(Chardev *chr, bool use_readline);
+int monitor_init(MonitorOptions *opts, Error **errp);
 int monitor_init_opts(QemuOpts *opts, Error **errp);
 void monitor_cleanup(void);
 
diff --git a/monitor/monitor.c b/monitor/monitor.c
index c1a6c4460f..cc7ee812eb 100644
--- a/monitor/monitor.c
+++ b/monitor/monitor.c
@@ -25,7 +25,9 @@
 #include "qemu/osdep.h"
 #include "monitor-internal.h"
 #include "qapi/error.h"
+#include "qapi/opts-visitor.h"
 #include "qapi/qapi-emit-events.h"
+#include "qapi/qapi-visit-control.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qstring.h"
 #include "qemu/error-report.h"
@@ -609,50 +611,55 @@ void monitor_init_globals_core(void)
                                    NULL);
 }
 
-int monitor_init_opts(QemuOpts *opts, Error **errp)
+int monitor_init(MonitorOptions *opts, Error **errp)
 {
     Chardev *chr;
-    bool qmp;
-    bool pretty = false;
-    const char *chardev;
-    const char *mode;
-
-    mode = qemu_opt_get(opts, "mode");
-    if (mode == NULL) {
-        mode = "readline";
-    }
-    if (strcmp(mode, "readline") == 0) {
-        qmp = false;
-    } else if (strcmp(mode, "control") == 0) {
-        qmp = true;
-    } else {
-        error_setg(errp, "unknown monitor mode \"%s\"", mode);
+
+    chr = qemu_chr_find(opts->chardev);
+    if (chr == NULL) {
+        error_setg(errp, "chardev \"%s\" not found", opts->chardev);
         return -1;
     }
 
-    if (!qmp && qemu_opt_get(opts, "pretty")) {
-        warn_report("'pretty' is deprecated for HMP monitors, it has no effect "
-                    "and will be removed in future versions");
-    }
-    if (qemu_opt_get_bool(opts, "pretty", 0)) {
-        pretty = true;
+    switch(opts->mode) {
+    case MONITOR_MODE_CONTROL:
+        monitor_init_qmp(chr, opts->pretty);
+        break;
+    case MONITOR_MODE_READLINE:
+        if (opts->pretty) {
+            warn_report("'pretty' is deprecated for HMP monitors, it has no "
+                        "effect and will be removed in future versions");
+        }
+        monitor_init_hmp(chr, true);
+        break;
+    default:
+        g_assert_not_reached();
     }
 
-    chardev = qemu_opt_get(opts, "chardev");
-    if (!chardev) {
-        error_report("chardev is required");
-        exit(1);
-    }
-    chr = qemu_chr_find(chardev);
-    if (chr == NULL) {
-        error_setg(errp, "chardev \"%s\" not found", chardev);
-        return -1;
+    return 0;
+}
+
+int monitor_init_opts(QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    MonitorOptions *options;
+    Error *local_err = NULL;
+
+    v = opts_visitor_new(opts);
+    visit_type_MonitorOptions(v, NULL, &options, &local_err);
+    visit_free(v);
+
+    if (local_err) {
+        goto out;
     }
 
-    if (qmp) {
-        monitor_init_qmp(chr, pretty);
-    } else {
-        monitor_init_hmp(chr, true);
+    monitor_init(options, &local_err);
+    qapi_free_MonitorOptions(options);
+
+out:
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return -1;
     }
     return 0;
 }
-- 
2.20.1



  parent reply	other threads:[~2020-02-24 14:41 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-24 14:29 [PATCH v2 00/20] Add qemu-storage-daemon Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 01/20] qemu-storage-daemon: Add barebone tool Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 02/20] stubs: Add arch_type Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 03/20] block: Move system emulator QMP commands to block/qapi-sysemu.c Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 04/20] block: Move common QMP commands to block-core QAPI module Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 05/20] block: Move sysemu QMP commands to QAPI block module Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 06/20] qemu-storage-daemon: Add --blockdev option Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 07/20] qapi: Flatten object-add Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 08/20] qemu-storage-daemon: Add --object option Kevin Wolf
2020-04-16  5:20   ` Coiby Xu
2020-06-09 23:25   ` Coiby Xu
2020-02-24 14:29 ` [PATCH v2 09/20] qemu-storage-daemon: Add --nbd-server option Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 10/20] blockdev-nbd: Boxed argument type for nbd-server-add Kevin Wolf
2020-02-24 14:29 ` [PATCH v2 11/20] qemu-storage-daemon: Add --export option Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 12/20] qemu-storage-daemon: Add main loop Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 13/20] qemu-storage-daemon: Add --chardev option Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 14/20] stubs: Update monitor stubs for qemu-storage-daemon Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 15/20] qapi: Create 'pragma' module Kevin Wolf
2020-02-24 14:30 ` Kevin Wolf [this message]
2020-02-24 14:30 ` [PATCH v2 17/20] qmp: Fail gracefully if chardev is already in use Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 18/20] hmp: " Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 19/20] monitor: Add allow_hmp parameter to monitor_init() Kevin Wolf
2020-02-24 14:30 ` [PATCH v2 20/20] qemu-storage-daemon: Add --monitor option Kevin Wolf
2020-02-24 15:34 ` [PATCH v2 00/20] Add qemu-storage-daemon no-reply
2020-02-28 11:16 ` Stefan Hajnoczi
2020-03-03 17:00   ` Kevin Wolf

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=20200224143008.13362-17-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=coiby.xu@gmail.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.