qmp: Fail gracefully if chardev is already in use

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>
Message-Id: <20200224143008.13362-18-kwolf@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Kevin Wolf 2020-02-24 15:30:05 +01:00
parent f2098725aa
commit f27a9bb3e9
5 changed files with 16 additions and 8 deletions

View File

@ -17,7 +17,7 @@ bool monitor_cur_is_qmp(void);
void monitor_init_globals(void); void monitor_init_globals(void);
void monitor_init_globals_core(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); void monitor_init_hmp(Chardev *chr, bool use_readline);
int monitor_init(MonitorOptions *opts, Error **errp); int monitor_init(MonitorOptions *opts, Error **errp);
int monitor_init_opts(QemuOpts *opts, Error **errp); int monitor_init_opts(QemuOpts *opts, Error **errp);

View File

@ -614,6 +614,7 @@ void monitor_init_globals_core(void)
int monitor_init(MonitorOptions *opts, Error **errp) int monitor_init(MonitorOptions *opts, Error **errp)
{ {
Chardev *chr; Chardev *chr;
Error *local_err = NULL;
chr = qemu_chr_find(opts->chardev); chr = qemu_chr_find(opts->chardev);
if (chr == NULL) { if (chr == NULL) {
@ -623,7 +624,7 @@ int monitor_init(MonitorOptions *opts, Error **errp)
switch (opts->mode) { switch (opts->mode) {
case MONITOR_MODE_CONTROL: case MONITOR_MODE_CONTROL:
monitor_init_qmp(chr, opts->pretty); monitor_init_qmp(chr, opts->pretty, &local_err);
break; break;
case MONITOR_MODE_READLINE: case MONITOR_MODE_READLINE:
if (opts->pretty) { if (opts->pretty) {
@ -636,6 +637,10 @@ int monitor_init(MonitorOptions *opts, Error **errp)
g_assert_not_reached(); g_assert_not_reached();
} }
if (local_err) {
error_propagate(errp, local_err);
return -1;
}
return 0; return 0;
} }

View File

@ -395,10 +395,16 @@ static void monitor_qmp_setup_handlers_bh(void *opaque)
monitor_list_append(&mon->common); 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); 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 */ /* Note: we run QMP monitor in I/O thread when @chr supports that */
monitor_data_init(&mon->common, true, false, monitor_data_init(&mon->common, true, false,
qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT)); 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); qemu_mutex_init(&mon->qmp_queue_lock);
mon->qmp_requests = g_queue_new(); 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); json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);
if (mon->common.use_io_thread) { if (mon->common.use_io_thread) {
/* /*

View File

@ -5,7 +5,7 @@
__thread Monitor *cur_mon; __thread Monitor *cur_mon;
void monitor_init_qmp(Chardev *chr, bool pretty) void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
{ {
} }

View File

@ -71,7 +71,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
*/ */
__thread Monitor *cur_mon; __thread Monitor *cur_mon;
int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); } 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) {} void monitor_init_hmp(Chardev *chr, bool use_readline) {}