qemu-devel.nongnu.org archive mirror
 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 17/20] qmp: Fail gracefully if chardev is already in use
Date: Mon, 24 Feb 2020 15:30:05 +0100	[thread overview]
Message-ID: <20200224143008.13362-18-kwolf@redhat.com> (raw)
In-Reply-To: <20200224143008.13362-1-kwolf@redhat.com>

Trying to attach a QMP monitor to a chardev that is already in use
results in a crash because monitor_init_qmp() passes &error_abort to
qemu_chr_fe_init():

$ ./x86_64-softmmu/qemu-system-x86_64 --chardev stdio,id=foo --mon foo,mode=control --mon foo,mode=control
Unexpected error in qemu_chr_fe_init() at chardev/char-fe.c:220:
qemu-system-x86_64: --mon foo,mode=control: Device 'foo' is in use
Abgebrochen (Speicherabzug geschrieben)

Fix this by allowing monitor_init_qmp() to return an error and passing
any error in qemu_chr_fe_init() to its caller instead of aborting.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 include/monitor/monitor.h |  2 +-
 monitor/monitor.c         |  7 ++++++-
 monitor/qmp.c             | 11 +++++++----
 stubs/monitor-core.c      |  2 +-
 tests/test-util-sockets.c |  2 +-
 5 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h
index db1112552c..e55a3b57e0 100644
--- a/include/monitor/monitor.h
+++ b/include/monitor/monitor.h
@@ -17,7 +17,7 @@ bool monitor_cur_is_qmp(void);
 
 void monitor_init_globals(void);
 void monitor_init_globals_core(void);
-void monitor_init_qmp(Chardev *chr, bool pretty);
+void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp);
 void monitor_init_hmp(Chardev *chr, bool use_readline);
 int monitor_init(MonitorOptions *opts, Error **errp);
 int monitor_init_opts(QemuOpts *opts, Error **errp);
diff --git a/monitor/monitor.c b/monitor/monitor.c
index cc7ee812eb..af84f53dcd 100644
--- a/monitor/monitor.c
+++ b/monitor/monitor.c
@@ -614,6 +614,7 @@ void monitor_init_globals_core(void)
 int monitor_init(MonitorOptions *opts, Error **errp)
 {
     Chardev *chr;
+    Error *local_err = NULL;
 
     chr = qemu_chr_find(opts->chardev);
     if (chr == NULL) {
@@ -623,7 +624,7 @@ int monitor_init(MonitorOptions *opts, Error **errp)
 
     switch(opts->mode) {
     case MONITOR_MODE_CONTROL:
-        monitor_init_qmp(chr, opts->pretty);
+        monitor_init_qmp(chr, opts->pretty, &local_err);
         break;
     case MONITOR_MODE_READLINE:
         if (opts->pretty) {
@@ -636,6 +637,10 @@ int monitor_init(MonitorOptions *opts, Error **errp)
         g_assert_not_reached();
     }
 
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return -1;
+    }
     return 0;
 }
 
diff --git a/monitor/qmp.c b/monitor/qmp.c
index 8379c8f96e..f89e7daf27 100644
--- a/monitor/qmp.c
+++ b/monitor/qmp.c
@@ -395,10 +395,16 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
     monitor_list_append(&mon->common);
 }
 
-void monitor_init_qmp(Chardev *chr, bool pretty)
+void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
 {
     MonitorQMP *mon = g_new0(MonitorQMP, 1);
 
+    if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
+        g_free(mon);
+        return;
+    }
+    qemu_chr_fe_set_echo(&mon->common.chr, true);
+
     /* Note: we run QMP monitor in I/O thread when @chr supports that */
     monitor_data_init(&mon->common, true, false,
                       qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT));
@@ -408,9 +414,6 @@ void monitor_init_qmp(Chardev *chr, bool pretty)
     qemu_mutex_init(&mon->qmp_queue_lock);
     mon->qmp_requests = g_queue_new();
 
-    qemu_chr_fe_init(&mon->common.chr, chr, &error_abort);
-    qemu_chr_fe_set_echo(&mon->common.chr, true);
-
     json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);
     if (mon->common.use_io_thread) {
         /*
diff --git a/stubs/monitor-core.c b/stubs/monitor-core.c
index 403c00a6d0..6cff1c4e1d 100644
--- a/stubs/monitor-core.c
+++ b/stubs/monitor-core.c
@@ -5,7 +5,7 @@
 
 __thread Monitor *cur_mon;
 
-void monitor_init_qmp(Chardev *chr, bool pretty)
+void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
 {
 }
 
diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index 8ce55efe70..2edb4c539d 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -71,7 +71,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
  */
 __thread Monitor *cur_mon;
 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); }
-void monitor_init_qmp(Chardev *chr, bool pretty) {}
+void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) {}
 void monitor_init_hmp(Chardev *chr, bool use_readline) {}
 
 
-- 
2.20.1



  parent reply	other threads:[~2020-02-24 14:42 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 ` [PATCH v2 16/20] monitor: Create QAPIfied monitor_init() Kevin Wolf
2020-02-24 14:30 ` Kevin Wolf [this message]
2020-02-24 14:30 ` [PATCH v2 18/20] hmp: Fail gracefully if chardev is already in use 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-18-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 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).