char: allow specifying a GMainContext at opening time
PATCH 1/1 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.
Based-on: <[email protected]>
--- chardev/char.c | 30 +++++++++++++++------------- gdbstub.c | 4 ++-- hmp.c | 2 +- hw/arm/omap2.c | 2 +- hw/bt/hci-csr.c | 2 +- hw/char/omap_uart.c | 4 ++-- hw/char/xen_console.c | 3 ++- hw/isa/isa-superio.c | 4 ++-- hw/mips/boston.c | 2 +- hw/mips/mips_malta.c | 2 +- hw/usb/dev-serial.c | 2 +- include/chardev/char.h | 16 +++++++++++---- net/slirp.c | 2 +- qtest.c | 2 +- tests/test-char.c | 43 +++++++++++++++++++++-------------------- tests/vhost-user-test.c | 2 +- vl.c | 8 ++++---- 17 files changed, 72 insertions(+), 58 deletions(-)
chardev/char.c +17 -13
@@ -634,7 +634,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 ;
@@ -674,7 +675,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;
@@ -687,7 +688,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 ;
@@ -703,7 +704,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;
@@ -718,7 +719,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;
@@ -736,10 +737,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 );
@@ -753,14 +755,14 @@ 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)
@@ -935,6 +937,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;
@@ -947,6 +950,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) {
@@ -991,7 +995,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 ; }
@@ -1049,7 +1053,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 ; }
gdbstub.c +2 -2
@@ -2527,7 +2527,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 ; }
@@ -2541,7 +2541,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);
hmp.c +1 -1
@@ -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);
hw/arm/omap2.c +1 -1
@@ -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",
hw/bt/hci-csr.c +1 -1
@@ -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 )
hw/char/omap_uart.c +2 -2
@@ -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 ); }
hw/char/xen_console.c +2 -1
@@ -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,
hw/isa/isa-superio.c +2 -2
@@ -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); }
hw/mips/boston.c +1 -1
@@ -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);
hw/mips/mips_malta.c +1 -1
@@ -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);
hw/usb/dev-serial.c +1 -1
@@ -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 ;
include/chardev/char.h +12 -4
@@ -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;
net/slirp.c +1 -1
@@ -780,7 +780,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'",
qtest.c +1 -1
@@ -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\"",
tests/test-char.c +22 -21
169 lines not on this page · raw message
tests/vhost-user-test.c +1 -1
@@ -543,7 +543,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);
vl.c +4 -4
@@ -2314,7 +2314,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 ;
@@ -2452,7 +2452,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);
@@ -2488,7 +2488,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);
@@ -2502,7 +2502,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 ); }
Signature · 1 line
2.20.1
Quoted 7 lines · Paolo Bonzini
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 <[email protected]>Based-on: <[email protected]>
I'm wondering if you had looked at my series related to the chardev reconnect feature, as this feature has many flaws as implemented right now:
https://lists.gnu.org/archive/html/qemu-devel/2019-01/msg05947.html
Our patches will conflict, but I think yours is needed regardless.
Incidentally I'm increasingly of the opinion that the qemu_chr_new() design right now is flawed.
IMHO we should really decouple the parsing & creation of the Chardev object, from the opening of the backend. eg I would like to see a qemu_chr_open() as a separate explicitly callable step from qemu_chr_new(). If we had that, then we would not need to pass GMainContext into qemu_chr_new() as we would be able to call the API to set the GMainContext before calling qemu_chr_open
I fear that would be a complex refactoring job though, so no objection to your current patch for solving the immediate need.
Quoted 11 lines
--- chardev/char.c | 30 +++++++++++++++------------- gdbstub.c | 4 ++-- hmp.c | 2 +- hw/arm/omap2.c | 2 +- hw/bt/hci-csr.c | 2 +- hw/char/omap_uart.c | 4 ++-- hw/char/xen_console.c | 3 ++- hw/isa/isa-superio.c | 4 ++-- hw/mips/boston.c | 2 +- hw/mips/mips_malta.c | 2 +-
Quoted 8 lines
hw/usb/dev-serial.c | 2 +- include/chardev/char.h | 16 +++++++++++---- net/slirp.c | 2 +- qtest.c | 2 +- tests/test-char.c | 43 +++++++++++++++++++++-------------------- tests/vhost-user-test.c | 2 +- vl.c | 8 ++++---- 17 files changed, 72 insertions(+), 58 deletions(-)
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|