char: allow specifying a GMainContext at opening time

This will be needed by vhost-user-test, when each test switches to
its own GMainLoop and GMainContext.  Otherwise, for a reconnecting
socket the initial connection will happen on the default GMainContext,
and no one will be listening on it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20190202110834.24880-1-pbonzini@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Paolo Bonzini 2019-02-13 14:18:13 +01:00 committed by Marc-André Lureau
parent 211ef6c4b6
commit 4ad6f6cb14
17 changed files with 71 additions and 56 deletions

View File

@ -636,7 +636,8 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
return backend;
}
Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
Error **errp)
{
const ChardevClass *cc;
Chardev *chr = NULL;
@ -676,7 +677,7 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
chr = qemu_chardev_new(bid ? bid : id,
object_class_get_name(OBJECT_CLASS(cc)),
backend, errp);
backend, context, errp);
if (chr == NULL) {
goto out;
@ -689,7 +690,7 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
backend->type = CHARDEV_BACKEND_KIND_MUX;
backend->u.mux.data = g_new0(ChardevMux, 1);
backend->u.mux.data->chardev = g_strdup(bid);
mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp);
mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
if (mux == NULL) {
object_unparent(OBJECT(chr));
chr = NULL;
@ -705,7 +706,7 @@ out:
}
Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
bool permit_mux_mon)
bool permit_mux_mon, GMainContext *context)
{
const char *p;
Chardev *chr;
@ -720,7 +721,7 @@ Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
if (!opts)
return NULL;
chr = qemu_chr_new_from_opts(opts, &err);
chr = qemu_chr_new_from_opts(opts, context, &err);
if (!chr) {
error_report_err(err);
goto out;
@ -738,10 +739,11 @@ out:
static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
const char *filename,
bool permit_mux_mon)
bool permit_mux_mon,
GMainContext *context)
{
Chardev *chr;
chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon);
chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon, context);
if (chr) {
if (replay_mode != REPLAY_MODE_NONE) {
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
@ -755,14 +757,16 @@ static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
return chr;
}
Chardev *qemu_chr_new(const char *label, const char *filename)
Chardev *qemu_chr_new(const char *label, const char *filename,
GMainContext *context)
{
return qemu_chr_new_permit_mux_mon(label, filename, false);
return qemu_chr_new_permit_mux_mon(label, filename, false, context);
}
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename)
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
GMainContext *context)
{
return qemu_chr_new_permit_mux_mon(label, filename, true);
return qemu_chr_new_permit_mux_mon(label, filename, true, context);
}
static int qmp_query_chardev_foreach(Object *obj, void *data)
@ -937,6 +941,7 @@ void qemu_chr_set_feature(Chardev *chr,
Chardev *qemu_chardev_new(const char *id, const char *typename,
ChardevBackend *backend,
GMainContext *gcontext,
Error **errp)
{
Object *obj;
@ -949,6 +954,7 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
obj = object_new(typename);
chr = CHARDEV(obj);
chr->label = g_strdup(id);
chr->gcontext = gcontext;
qemu_char_open(chr, backend, &be_opened, &local_err);
if (local_err) {
@ -993,7 +999,7 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
}
chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
backend, errp);
backend, NULL, errp);
if (!chr) {
return NULL;
}
@ -1051,7 +1057,7 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
}
chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)),
backend, errp);
backend, chr->gcontext, errp);
if (!chr_new) {
return NULL;
}

View File

@ -2531,7 +2531,7 @@ int gdbserver_start(const char *device)
* FIXME: it's a bit weird to allow using a mux chardev here
* and implicitly setup a monitor. We may want to break this.
*/
chr = qemu_chr_new_noreplay("gdb", device, true);
chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
if (!chr)
return -1;
}
@ -2545,7 +2545,7 @@ int gdbserver_start(const char *device)
/* Initialize a monitor terminal for gdb */
mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
NULL, &error_abort);
NULL, NULL, &error_abort);
monitor_init(mon_chr, 0);
} else {
qemu_chr_fe_deinit(&s->chr, true);

2
hmp.c
View File

@ -2395,7 +2395,7 @@ void hmp_chardev_add(Monitor *mon, const QDict *qdict)
if (opts == NULL) {
error_setg(&err, "Parsing chardev args failed");
} else {
qemu_chr_new_from_opts(opts, &err);
qemu_chr_new_from_opts(opts, NULL, &err);
qemu_opts_del(opts);
}
hmp_handle_error(mon, &err);

View File

@ -799,7 +799,7 @@ static struct omap_sti_s *omap_sti_init(struct omap_target_agent_s *ta,
s->irq = irq;
omap_sti_reset(s);
qemu_chr_fe_init(&s->chr, chr ?: qemu_chr_new("null", "null"),
qemu_chr_fe_init(&s->chr, chr ?: qemu_chr_new("null", "null", NULL),
&error_abort);
memory_region_init_io(&s->iomem, NULL, &omap_sti_ops, s, "omap.sti",

View File

@ -501,7 +501,7 @@ static const TypeInfo char_hci_type_info = {
Chardev *uart_hci_init(void)
{
return qemu_chardev_new(NULL, TYPE_CHARDEV_HCI,
NULL, &error_abort);
NULL, NULL, &error_abort);
}
static void register_types(void)

View File

@ -63,7 +63,7 @@ struct omap_uart_s *omap_uart_init(hwaddr base,
s->irq = irq;
s->serial = serial_mm_init(get_system_memory(), base, 2, irq,
omap_clk_getrate(fclk)/16,
chr ?: qemu_chr_new(label, "null"),
chr ?: qemu_chr_new(label, "null", NULL),
DEVICE_NATIVE_ENDIAN);
return s;
}
@ -183,6 +183,6 @@ void omap_uart_attach(struct omap_uart_s *s, Chardev *chr)
/* TODO: Should reuse or destroy current s->serial */
s->serial = serial_mm_init(get_system_memory(), s->base, 2, s->irq,
omap_clk_getrate(s->fclk) / 16,
chr ?: qemu_chr_new("null", "null"),
chr ?: qemu_chr_new("null", "null", NULL),
DEVICE_NATIVE_ENDIAN);
}

View File

@ -211,7 +211,8 @@ static int con_init(struct XenLegacyDevice *xendev)
* FIXME: sure we want to support implicit
* muxed monitors here?
*/
qemu_chr_new_mux_mon(label, output), &error_abort);
qemu_chr_new_mux_mon(label, output, NULL),
&error_abort);
}
xenstore_store_pv_console_info(con->xendev.dev,

View File

@ -44,7 +44,7 @@ static void isa_superio_realize(DeviceState *dev, Error **errp)
chr = parallel_hds[i];
if (chr == NULL) {
name = g_strdup_printf("discarding-parallel%d", i);
chr = qemu_chr_new(name, "null");
chr = qemu_chr_new(name, "null", NULL);
} else {
name = g_strdup_printf("parallel%d", i);
}
@ -84,7 +84,7 @@ static void isa_superio_realize(DeviceState *dev, Error **errp)
chr = serial_hd(i);
if (chr == NULL) {
name = g_strdup_printf("discarding-serial%d", i);
chr = qemu_chr_new(name, "null");
chr = qemu_chr_new(name, "null", NULL);
} else {
name = g_strdup_printf("serial%d", i);
}

View File

@ -512,7 +512,7 @@ static void boston_mach_init(MachineState *machine)
memory_region_init_io(lcd, NULL, &boston_lcd_ops, s, "boston-lcd", 0x8);
memory_region_add_subregion_overlap(sys_mem, 0x17fff000, lcd, 0);
chr = qemu_chr_new("lcd", "vc:320x240");
chr = qemu_chr_new("lcd", "vc:320x240", NULL);
qemu_chr_fe_init(&s->lcd_display, chr, NULL);
qemu_chr_fe_set_handlers(&s->lcd_display, NULL, NULL,
boston_lcd_event, NULL, s, NULL, true);

View File

@ -568,7 +568,7 @@ static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space,
memory_region_add_subregion(address_space, base, &s->iomem_lo);
memory_region_add_subregion(address_space, base + 0xa00, &s->iomem_hi);
chr = qemu_chr_new("fpga", "vc:320x200");
chr = qemu_chr_new("fpga", "vc:320x200", NULL);
qemu_chr_fe_init(&s->display, chr, NULL);
qemu_chr_fe_set_handlers(&s->display, NULL, NULL,
malta_fgpa_display_event, NULL, s, NULL, true);

View File

@ -514,7 +514,7 @@ static USBDevice *usb_braille_init(USBBus *bus, const char *unused)
USBDevice *dev;
Chardev *cdrv;
cdrv = qemu_chr_new("braille", "braille");
cdrv = qemu_chr_new("braille", "braille", NULL);
if (!cdrv)
return NULL;

View File

@ -73,6 +73,7 @@ struct Chardev {
/**
* qemu_chr_new_from_opts:
* @opts: see qemu-config.c for a list of valid options
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a QemuOpts list.
*
@ -81,6 +82,7 @@ struct Chardev {
* or left untouched in case of help option
*/
Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
GMainContext *context,
Error **errp);
/**
@ -106,25 +108,29 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
* qemu_chr_new:
* @label: the name of the backend
* @filename: the URI
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a URI.
* Do not implicitly initialize a monitor if the chardev is muxed.
*
* Returns: a new character backend
*/
Chardev *qemu_chr_new(const char *label, const char *filename);
Chardev *qemu_chr_new(const char *label, const char *filename,
GMainContext *context);
/**
* qemu_chr_new_mux_mon:
* @label: the name of the backend
* @filename: the URI
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a URI.
* Implicitly initialize a monitor if the chardev is muxed.
*
* Returns: a new character backend
*/
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename);
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
GMainContext *context);
/**
* qemu_chr_change:
@ -146,6 +152,7 @@ void qemu_chr_cleanup(void);
* @label: the name of the backend
* @filename: the URI
* @permit_mux_mon: if chardev is muxed, initialize a monitor
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a URI.
* Character device communications are not written
@ -154,7 +161,7 @@ void qemu_chr_cleanup(void);
* Returns: a new character backend
*/
Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
bool permit_mux_mon);
bool permit_mux_mon, GMainContext *context);
/**
* qemu_chr_be_can_write:
@ -272,7 +279,8 @@ typedef struct ChardevClass {
} ChardevClass;
Chardev *qemu_chardev_new(const char *id, const char *typename,
ChardevBackend *backend, Error **errp);
ChardevBackend *backend, GMainContext *context,
Error **errp);
extern int term_escape_char;

View File

@ -927,7 +927,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
* FIXME: sure we want to support implicit
* muxed monitors here?
*/
Chardev *chr = qemu_chr_new_mux_mon(buf, p);
Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL);
if (!chr) {
error_setg(errp, "Could not open guest forwarding device '%s'",

View File

@ -763,7 +763,7 @@ void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
{
Chardev *chr;
chr = qemu_chr_new("qtest", qtest_chrdev);
chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
if (chr == NULL) {
error_setg(errp, "Failed to initialize device for qtest: \"%s\"",

View File

@ -86,7 +86,7 @@ static void char_console_test_subprocess(void)
1, &error_abort);
qemu_opt_set(opts, "backend", "console", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
@ -108,7 +108,7 @@ static void char_stdio_test_subprocess(void)
CharBackend be;
int ret;
chr = qemu_chr_new("label", "stdio");
chr = qemu_chr_new("label", "stdio", NULL);
g_assert_nonnull(chr);
qemu_chr_fe_init(&be, chr, &error_abort);
@ -139,7 +139,7 @@ static void char_ringbuf_test(void)
qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
qemu_opt_set(opts, "size", "5", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_null(chr);
qemu_opts_del(opts);
@ -147,7 +147,7 @@ static void char_ringbuf_test(void)
1, &error_abort);
qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
qemu_opt_set(opts, "size", "2", &error_abort);
chr = qemu_chr_new_from_opts(opts, &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
g_assert_nonnull(chr);
qemu_opts_del(opts);
@ -170,7 +170,7 @@ static void char_ringbuf_test(void)
1, &error_abort);
qemu_opt_set(opts, "backend", "memory", &error_abort);
qemu_opt_set(opts, "size", "2", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
object_unparent(OBJECT(chr));
qemu_opts_del(opts);
@ -189,7 +189,7 @@ static void char_mux_test(void)
qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
qemu_opt_set(opts, "size", "128", &error_abort);
qemu_opt_set(opts, "mux", "on", &error_abort);
chr = qemu_chr_new_from_opts(opts, &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
g_assert_nonnull(chr);
qemu_opts_del(opts);
@ -412,7 +412,7 @@ static void char_websock_test(void)
CharBackend client_be;
Chardev *chr_client;
Chardev *chr = qemu_chr_new("server",
"websocket:127.0.0.1:0,server,nowait");
"websocket:127.0.0.1:0,server,nowait", NULL);
const char handshake[] = "GET / HTTP/1.1\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
@ -436,7 +436,7 @@ static void char_websock_test(void)
qemu_chr_fe_set_handlers(&be, websock_server_can_read, websock_server_read,
NULL, NULL, chr, NULL, true);
chr_client = qemu_chr_new("client", tmp);
chr_client = qemu_chr_new("client", tmp, NULL);
qemu_chr_fe_init(&client_be, chr_client, &error_abort);
qemu_chr_fe_set_handlers(&client_be, websock_client_can_read,
websock_client_read,
@ -482,7 +482,7 @@ static void char_pipe_test(void)
}
tmp = g_strdup_printf("pipe:%s", pipe);
chr = qemu_chr_new("pipe", tmp);
chr = qemu_chr_new("pipe", tmp, NULL);
g_assert_nonnull(chr);
g_free(tmp);
@ -586,7 +586,7 @@ static void char_udp_test_internal(Chardev *reuse_chr, int sock)
int port;
sock = make_udp_socket(&port);
tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
chr = qemu_chr_new("client", tmp);
chr = qemu_chr_new("client", tmp, NULL);
g_assert_nonnull(chr);
be = g_alloca(sizeof(CharBackend));
@ -764,7 +764,7 @@ static void char_socket_server_test(gconstpointer opaque)
opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
optstr, true);
g_assert_nonnull(opts);
chr = qemu_chr_new_from_opts(opts, &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
qemu_opts_del(opts);
g_assert_nonnull(chr);
g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
@ -914,7 +914,7 @@ static void char_socket_client_test(gconstpointer opaque)
opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
optstr, true);
g_assert_nonnull(opts);
chr = qemu_chr_new_from_opts(opts, &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
qemu_opts_del(opts);
g_assert_nonnull(chr);
@ -1015,14 +1015,14 @@ static void char_serial_test(void)
qemu_opt_set(opts, "backend", "serial", &error_abort);
qemu_opt_set(opts, "path", "/dev/null", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
/* TODO: add more tests with a pty */
object_unparent(OBJECT(chr));
/* test tty alias */
qemu_opt_set(opts, "backend", "tty", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
object_unparent(OBJECT(chr));
@ -1055,7 +1055,7 @@ static void char_file_fifo_test(void)
g_assert_cmpint(ret, ==, 8);
chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
&error_abort);
NULL, &error_abort);
qemu_chr_fe_init(&be, chr, &error_abort);
qemu_chr_fe_set_handlers(&be,
@ -1109,7 +1109,7 @@ static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
out = g_build_filename(tmp_path, "out", NULL);
file.out = out;
chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
&error_abort);
NULL, &error_abort);
}
ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
g_assert_cmpint(ret, ==, 6);
@ -1144,7 +1144,7 @@ static void char_null_test(void)
chr = qemu_chr_find("label-null");
g_assert_null(chr);
chr = qemu_chr_new("label-null", "null");
chr = qemu_chr_new("label-null", "null", NULL);
chr = qemu_chr_find("label-null");
g_assert_nonnull(chr);
@ -1181,7 +1181,7 @@ static void char_invalid_test(void)
{
Chardev *chr;
g_setenv("QTEST_SILENT_ERRORS", "1", 1);
chr = qemu_chr_new("label-invalid", "invalid");
chr = qemu_chr_new("label-invalid", "invalid", NULL);
g_assert_null(chr);
g_unsetenv("QTEST_SILENT_ERRORS");
}
@ -1215,7 +1215,7 @@ static void char_hotswap_test(void)
chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
chr = qemu_chr_new("chardev", chr_args);
chr = qemu_chr_new("chardev", chr_args, NULL);
qemu_chr_fe_init(&be, chr, &error_abort);
/* check that chardev operates correctly */

View File

@ -519,7 +519,7 @@ static void test_server_create_chr(TestServer *server, const gchar *opt)
Chardev *chr;
chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
chr = qemu_chr_new(server->chr_name, chr_path);
chr = qemu_chr_new(server->chr_name, chr_path, NULL);
g_free(chr_path);
g_assert_nonnull(chr);

8
vl.c
View File

@ -2302,7 +2302,7 @@ static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
{
Error *local_err = NULL;
if (!qemu_chr_new_from_opts(opts, &local_err)) {
if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
if (local_err) {
error_propagate(errp, local_err);
return -1;
@ -2440,7 +2440,7 @@ static int serial_parse(const char *devname)
snprintf(label, sizeof(label), "serial%d", index);
serial_hds = g_renew(Chardev *, serial_hds, index + 1);
serial_hds[index] = qemu_chr_new_mux_mon(label, devname);
serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
if (!serial_hds[index]) {
error_report("could not connect serial device"
" to character backend '%s'", devname);
@ -2476,7 +2476,7 @@ static int parallel_parse(const char *devname)
exit(1);
}
snprintf(label, sizeof(label), "parallel%d", index);
parallel_hds[index] = qemu_chr_new_mux_mon(label, devname);
parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
if (!parallel_hds[index]) {
error_report("could not connect parallel device"
" to character backend '%s'", devname);
@ -2490,7 +2490,7 @@ static int debugcon_parse(const char *devname)
{
QemuOpts *opts;
if (!qemu_chr_new_mux_mon("debugcon", devname)) {
if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
error_report("invalid character backend '%s'", devname);
exit(1);
}