qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

hmp: Fail gracefully if chardev is already in use

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

$ ./x86_64-softmmu/qemu-system-x86_64 --chardev stdio,id=foo --mon foo --mon foo
QEMU 4.2.50 monitor - type 'help' for more information
(qemu) Unexpected error in qemu_chr_fe_init() at chardev/char-fe.c:220:
qemu-system-x86_64: --mon foo: Device 'foo' is in use
Abgebrochen (Speicherabzug geschrieben)

Fix this by allowing monitor_init_hmp() 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-19-kwolf@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>

+18 -8
+7 -1
chardev/char.c
··· 737 737 738 738 if (qemu_opt_get_bool(opts, "mux", 0)) { 739 739 assert(permit_mux_mon); 740 - monitor_init_hmp(chr, true); 740 + monitor_init_hmp(chr, true, &err); 741 + if (err) { 742 + error_report_err(err); 743 + object_unparent(OBJECT(chr)); 744 + chr = NULL; 745 + goto out; 746 + } 741 747 } 742 748 743 749 out:
+1 -1
gdbstub.c
··· 3367 3367 /* Initialize a monitor terminal for gdb */ 3368 3368 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, 3369 3369 NULL, NULL, &error_abort); 3370 - monitor_init_hmp(mon_chr, false); 3370 + monitor_init_hmp(mon_chr, false, &error_abort); 3371 3371 } else { 3372 3372 qemu_chr_fe_deinit(&s->chr, true); 3373 3373 mon_chr = s->mon_chr;
+1 -1
include/monitor/monitor.h
··· 18 18 void monitor_init_globals(void); 19 19 void monitor_init_globals_core(void); 20 20 void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp); 21 - void monitor_init_hmp(Chardev *chr, bool use_readline); 21 + void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp); 22 22 int monitor_init(MonitorOptions *opts, Error **errp); 23 23 int monitor_init_opts(QemuOpts *opts, Error **errp); 24 24 void monitor_cleanup(void);
+6 -2
monitor/hmp.c
··· 1399 1399 monitor_flush(&mon->common); 1400 1400 } 1401 1401 1402 - void monitor_init_hmp(Chardev *chr, bool use_readline) 1402 + void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) 1403 1403 { 1404 1404 MonitorHMP *mon = g_new0(MonitorHMP, 1); 1405 1405 1406 + if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) { 1407 + g_free(mon); 1408 + return; 1409 + } 1410 + 1406 1411 monitor_data_init(&mon->common, false, false, false); 1407 - qemu_chr_fe_init(&mon->common.chr, chr, &error_abort); 1408 1412 1409 1413 mon->use_readline = use_readline; 1410 1414 if (mon->use_readline) {
+1 -1
monitor/monitor.c
··· 631 631 warn_report("'pretty' is deprecated for HMP monitors, it has no " 632 632 "effect and will be removed in future versions"); 633 633 } 634 - monitor_init_hmp(chr, true); 634 + monitor_init_hmp(chr, true, &local_err); 635 635 break; 636 636 default: 637 637 g_assert_not_reached();
+1 -1
stubs/monitor.c
··· 9 9 return -1; 10 10 } 11 11 12 - void monitor_init_hmp(Chardev *chr, bool use_readline) 12 + void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) 13 13 { 14 14 } 15 15
+1 -1
tests/test-util-sockets.c
··· 72 72 __thread Monitor *cur_mon; 73 73 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); } 74 74 void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) {} 75 - void monitor_init_hmp(Chardev *chr, bool use_readline) {} 75 + void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) {} 76 76 77 77 78 78 static void test_socket_fd_pass_name_good(void)