Skip to contentSkip to conversation

chardev related fixes

PATCH In review [email protected] 3 participants 21 messages Aug 4, 2021 – Aug 5, 2021 Networking

lore
Share & tools
lore
#0 lore raw

v2 cover chardev related fixes

From: Marc-André Lureau <[email protected]>

Hi,

Those patches have been sent earlier. I compiled them here, as I intend to send a pull request for 6.1. We may want to drop the last 2/3 patches since we are in freeze though.

Marc-André Lureau (11):
util: fix abstract socket path copy chardev/socket: print a more correct command-line address
chardev: remove needless class method
chardev: add some comments about the class methods
chardev: mark explicitly first argument as poisoned
chardev: fix fd_chr_add_watch() when in != out
chardev: fix qemu_chr_open_fd() being called with fd=-1
chardev: fix qemu_chr_open_fd() with fd_in==fd_out
chardev: give some context on chardev-add error
chardev: report a simpler error about duplicated id
chardev: reuse qmp_chardev_new()

 include/chardev/char-fe.h |   8 ++-
 include/chardev/char.h    |  34 ++++++++++-
 chardev/char-fd.c         | 119 ++++++++++++++++++++++++++++++++++----
 chardev/char-fe.c         |   2 +-
 chardev/char-mux.c        |   6 +-
 chardev/char-socket.c     |   4 +-
 chardev/char.c            |  33 ++++++-----
 hw/char/cadence_uart.c    |   2 +-
 hw/char/cmsdk-apb-uart.c  |   2 +-
 hw/char/ibex_uart.c       |   2 +-
 hw/char/nrf51_uart.c      |   2 +-
 hw/char/serial.c          |   2 +-
 hw/char/virtio-console.c  |   2 +-
 hw/usb/redirect.c         |   2 +-
 hw/virtio/vhost-user.c    |   2 +-
 monitor/monitor.c         |   2 +-
 net/vhost-user.c          |   4 +-
 util/qemu-sockets.c       |   5 +-
 18 files changed, 184 insertions(+), 49 deletions(-)
Signature · 1 line
2.32.0.264.g75ae10bc75
#1 lore raw

PATCH v2 1/11 util: fix abstract socket path copy

From: Marc-André Lureau <[email protected]>

Commit 776b97d360 "qemu-sockets: add abstract UNIX domain socket support" neglected to update socket_sockaddr_to_address_unix() and copied the whole sun_path without taking "salen" into account.

Later, commit 3b14b4ec49 "sockets: Fix socket_sockaddr_to_address_unix() for abstract sockets" handled the abstract UNIX path, by stripping the leading \0 character and fixing address details, but didn't use salen either.

Not taking "salen" into account may result in incorrect "path" being returned in monitors commands, as we read past the address which is not necessarily \0-terminated.

Fixes 776b97d3605ed0fc94443048fdf988c7725e38a9
Fixes 3b14b4ec49a801067da19d6b8469eb1c1911c020
---
 util/qemu-sockets.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
util/qemu-sockets.c +4 -1
index 080a240b74..f2f3676d1f 100644
@@ -1345,13 +1345,16 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,
     SocketAddress *addr;
     struct sockaddr_un *su = (struct sockaddr_un *)sa;
 
+    assert(salen >= sizeof(su->sun_family) + 1 &&
+           salen <= sizeof(struct sockaddr_un));
+
     addr = g_new0(SocketAddress, 1);
     addr->type = SOCKET_ADDRESS_TYPE_UNIX;
 #ifdef CONFIG_LINUX
     if (!su->sun_path[0]) {
         /* Linux abstract socket */
         addr->u.q_unix.path = g_strndup(su->sun_path + 1,
-                                        sizeof(su->sun_path) - 1);
+                                        salen - sizeof(su->sun_family) - 1);
         addr->u.q_unix.has_abstract = true;
         addr->u.q_unix.abstract = true;
         addr->u.q_unix.has_tight = true;
Signature · 1 line
2.32.0.264.g75ae10bc75
#2 lore raw

PATCH v2 2/11 chardev/socket: print a more correct command-line address

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Better reflect the command line version of the socket address arguments, following the now recommended long-form opt=on syntax.

Complement/fixes commit 9d902d51 "chardev: do not use short form boolean options in non-QemuOpts character device descriptions".

---
 chardev/char-socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
chardev/char-socket.c +2 -2
index d0fb545963..c43668cc15 100644
@@ -468,9 +468,9 @@ static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)
 
 #ifdef CONFIG_LINUX
         if (sa->has_abstract && sa->abstract) {
-            abstract = ",abstract";
+            abstract = ",abstract=on";
             if (sa->has_tight && sa->tight) {
-                tight = ",tight";
+                tight = ",tight=on";
             }
         }
 #endif
Signature · 1 line
2.32.0.264.g75ae10bc75
Reviewed-by on v2 2/11
#3 lore raw

Re: [PATCH v2 02/11] chardev/socket: print a more correct command-line address

Quoted 5 lines · On Wed, Aug 04, 2021 at 07:48:39PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Better reflect the command line version of the socket address arguments, following the now recommended long-form opt=on syntax.

Quoted 8 lines

Complement/fixes commit 9d902d51 "chardev: do not use short form boolean options in non-QemuOpts character device descriptions".

---
 chardev/char-socket.c | 4 ++--
 1 file changed, 2 insertions(+), 2 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 :|
#4 lore raw

PATCH v2 3/11 chardev: remove needless class method

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

"chr_option_parsed" is only implemented by the "mux" chardev, we can specialize the code there to avoid the needless generic class method.

---
 include/chardev/char.h | 1 -
 chardev/char-mux.c     | 6 ++----
 2 files changed, 2 insertions(+), 5 deletions(-)
include/chardev/char.h +0 -1
index 7c0444f90d..589e7fe46d 100644
@@ -273,7 +273,6 @@ struct ChardevClass {
     void (*chr_set_echo)(Chardev *chr, bool echo);
     void (*chr_set_fe_open)(Chardev *chr, int fe_open);
     void (*chr_be_event)(Chardev *s, QEMUChrEvent event);
-    void (*chr_options_parsed)(Chardev *chr);
 };
 
 Chardev *qemu_chardev_new(const char *id, const char *typename,
chardev/char-mux.c +2 -4
index 5baf419010..ada0c6866f 100644
@@ -386,10 +386,9 @@ void suspend_mux_open(void)
 static int chardev_options_parsed_cb(Object *child, void *opaque)
 {
     Chardev *chr = (Chardev *)child;
-    ChardevClass *class = CHARDEV_GET_CLASS(chr);
 
-    if (!chr->be_open && class->chr_options_parsed) {
-        class->chr_options_parsed(chr);
+    if (!chr->be_open && CHARDEV_IS_MUX(chr)) {
+        open_muxes(chr);
     }
 
     return 0;
@@ -412,7 +411,6 @@ static void char_mux_class_init(ObjectClass *oc, void *data)
     cc->chr_accept_input = mux_chr_accept_input;
     cc->chr_add_watch = mux_chr_add_watch;
     cc->chr_be_event = mux_chr_be_event;
-    cc->chr_options_parsed = open_muxes;
     cc->chr_update_read_handler = mux_chr_update_read_handlers;
 }
 
Signature · 1 line
2.32.0.264.g75ae10bc75
#5 lore raw

PATCH v2 4/11 chardev: add some comments about the class methods

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

---
 include/chardev/char.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/chardev/char.h +33 -0
index 589e7fe46d..a319b5fdff 100644
@@ -254,24 +254,57 @@ struct ChardevClass {
 
     bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
     bool supports_yank;
+
+    /* parse command line options and populate QAPI @backend */
     void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
 
+    /* called after construction, open/starts the backend */
     void (*open)(Chardev *chr, ChardevBackend *backend,
                  bool *be_opened, Error **errp);
 
+    /* write buf to the backend */
     int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
+
+    /*
+     * Read from the backend (blocking). A typical front-end will instead rely
+     * on chr_can_read/chr_read being called when polling/looping.
+     */
     int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
+
+    /* create a watch on the backend */
     GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
+
+    /* update the backend internal sources */
     void (*chr_update_read_handler)(Chardev *s);
+
+    /* send an ioctl to the backend */
     int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
+
+    /* get ancillary-received fds during last read */
     int (*get_msgfds)(Chardev *s, int* fds, int num);
+
+    /* set ancillary fds to be sent with next write */
     int (*set_msgfds)(Chardev *s, int *fds, int num);
+
+    /* accept the given fd */
     int (*chr_add_client)(Chardev *chr, int fd);
+
+    /* wait for a connection */
     int (*chr_wait_connected)(Chardev *chr, Error **errp);
+
+    /* disconnect a connection */
     void (*chr_disconnect)(Chardev *chr);
+
+    /* called by frontend when it can read */
     void (*chr_accept_input)(Chardev *chr);
+
+    /* set terminal echo */
     void (*chr_set_echo)(Chardev *chr, bool echo);
+
+    /* notify the backend of frontend open state */
     void (*chr_set_fe_open)(Chardev *chr, int fe_open);
+
+    /* handle various events */
     void (*chr_be_event)(Chardev *s, QEMUChrEvent event);
 };
 
Signature · 1 line
2.32.0.264.g75ae10bc75
#6 lore raw

PATCH v2 5/11 chardev: mark explicitly first argument as poisoned

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Since commit 9894dc0cdcc397ee5b26370bc53da6d360a363c2 "char: convert from GIOChannel to QIOChannel", the first argument to the watch callback can actually be a QIOChannel, which is not a GIOChannel (but a QEMU Object).

Even though we never used that pointer, change the callback type to warn the users. Possibly a better fix later, we may want to store the callback and call it from intermediary functions.

---
 include/chardev/char-fe.h | 8 +++++++-
 chardev/char-fe.c         | 2 +-
 hw/char/cadence_uart.c    | 2 +-
 hw/char/cmsdk-apb-uart.c  | 2 +-
 hw/char/ibex_uart.c       | 2 +-
 hw/char/nrf51_uart.c      | 2 +-
 hw/char/serial.c          | 2 +-
 hw/char/virtio-console.c  | 2 +-
 hw/usb/redirect.c         | 2 +-
 hw/virtio/vhost-user.c    | 2 +-
 monitor/monitor.c         | 2 +-
 net/vhost-user.c          | 4 ++--
 12 files changed, 19 insertions(+), 13 deletions(-)
include/chardev/char-fe.h +7 -1
index a553843364..867ef1b3b2 100644
@@ -174,6 +174,9 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open);
 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
+
+typedef gboolean (*FEWatchFunc)(void *do_not_use, GIOCondition condition, void *data);
+
 /**
  * qemu_chr_fe_add_watch:
  * @cond: the condition to poll for
@@ -188,10 +191,13 @@ void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
  * Note that you are responsible to update the front-end sources if
  * you are switching the main context with qemu_chr_fe_set_handlers().
  *
+ * Warning: DO NOT use the first callback argument (it may be either
+ * a GIOChannel or a QIOChannel, depending on the underlying chardev)
+ *
  * Returns: the source tag
  */
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data);
+                            FEWatchFunc func, void *user_data);
 
 /**
  * qemu_chr_fe_write:
chardev/char-fe.c +1 -1
index 474715c5a9..7789f7be9c 100644
@@ -354,7 +354,7 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
 }
 
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data)
+                            FEWatchFunc func, void *user_data)
 {
     Chardev *s = be->chr;
     GSource *src;
hw/char/cadence_uart.c +1 -1
index ceb677bc5a..8ee6f74b8c 100644
@@ -288,7 +288,7 @@ static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size)
     uart_update_status(s);
 }
 
-static gboolean cadence_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean cadence_uart_xmit(GIOChannel *do_not_use, GIOCondition cond,
                                   void *opaque)
 {
     CadenceUARTState *s = opaque;
hw/char/cmsdk-apb-uart.c +1 -1
index ba2cbbee3d..b07a9dee4f 100644
@@ -191,7 +191,7 @@ static uint64_t uart_read(void *opaque, hwaddr offset, unsigned size)
 /* Try to send tx data, and arrange to be called back later if
  * we can't (ie the char backend is busy/blocking).
  */
-static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void *opaque)
+static gboolean uart_transmit(GIOChannel *do_not_use, GIOCondition cond, void *opaque)
 {
     CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
     int ret;
hw/char/ibex_uart.c +1 -1
index 6b0c9330bf..e493ea08c0 100644
@@ -135,7 +135,7 @@ static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
     ibex_uart_update_irqs(s);
 }
 
-static gboolean ibex_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean ibex_uart_xmit(GIOChannel *do_not_use, GIOCondition cond,
                                void *opaque)
 {
     IbexUartState *s = opaque;
hw/char/nrf51_uart.c +1 -1
index 045ca5fa40..0b89b0eae4 100644
@@ -75,7 +75,7 @@ static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
     return r;
 }
 
-static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void *opaque)
+static gboolean uart_transmit(GIOChannel *do_not_use, GIOCondition cond, void *opaque)
 {
     NRF51UARTState *s = NRF51_UART(opaque);
     int r;
hw/char/serial.c +1 -1
index bc2e322970..7061aacbce 100644
@@ -220,7 +220,7 @@ static void serial_update_msl(SerialState *s)
     }
 }
 
-static gboolean serial_watch_cb(GIOChannel *chan, GIOCondition cond,
+static gboolean serial_watch_cb(void *do_not_use, GIOCondition cond,
                                 void *opaque)
 {
     SerialState *s = opaque;
hw/char/virtio-console.c +1 -1
index 6b132caa29..dd5a02e339 100644
@@ -38,7 +38,7 @@ struct VirtConsole {
  * Callback function that's called from chardevs when backend becomes
  * writable.
  */
-static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean chr_write_unblocked(void *do_not_use, GIOCondition cond,
                                     void *opaque)
 {
     VirtConsole *vcon = opaque;
hw/usb/redirect.c +1 -1
index 1ec909a63a..5f0ef9cb3b 100644
@@ -270,7 +270,7 @@ static int usbredir_read(void *priv, uint8_t *data, int count)
     return count;
 }
 
-static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean usbredir_write_unblocked(void *do_not_use, GIOCondition cond,
                                          void *opaque)
 {
     USBRedirDevice *dev = opaque;
hw/virtio/vhost-user.c +1 -1
index 29ea2b4fce..aec6cc1990 100644
@@ -303,7 +303,7 @@ struct vhost_user_read_cb_data {
     int ret;
 };
 
-static gboolean vhost_user_read_cb(GIOChannel *source, GIOCondition condition,
+static gboolean vhost_user_read_cb(void *do_not_use, GIOCondition condition,
                                    gpointer opaque)
 {
     struct vhost_user_read_cb_data *data = opaque;
monitor/monitor.c +1 -1
index b90c0f4051..46a171bca6 100644
@@ -156,7 +156,7 @@ static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
 
 static void monitor_flush_locked(Monitor *mon);
 
-static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond,
                                   void *opaque)
 {
     Monitor *mon = opaque;
net/vhost-user.c +2 -2
index ffbd94d944..6adfcd623a 100644
@@ -208,8 +208,8 @@ static NetClientInfo net_vhost_user_info = {
         .set_vnet_le = vhost_user_set_vnet_endianness,
 };
 
-static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
-                                           void *opaque)
+static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
+                                     void *opaque)
 {
     NetVhostUserState *s = opaque;
 
Signature · 1 line
2.32.0.264.g75ae10bc75
#7 lore raw

Re: [PATCH v2 05/11] chardev: mark explicitly first argument as poisoned

Quoted 74 lines · On Wed, Aug 04, 2021 at 07:48:42PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Since commit 9894dc0cdcc397ee5b26370bc53da6d360a363c2 "char: convert from GIOChannel to QIOChannel", the first argument to the watch callback can actually be a QIOChannel, which is not a GIOChannel (but a QEMU Object).

Even though we never used that pointer, change the callback type to warn the users. Possibly a better fix later, we may want to store the callback and call it from intermediary functions.

---
 include/chardev/char-fe.h | 8 +++++++-
 chardev/char-fe.c         | 2 +-
 hw/char/cadence_uart.c    | 2 +-
 hw/char/cmsdk-apb-uart.c  | 2 +-
 hw/char/ibex_uart.c       | 2 +-
 hw/char/nrf51_uart.c      | 2 +-
 hw/char/serial.c          | 2 +-
 hw/char/virtio-console.c  | 2 +-
 hw/usb/redirect.c         | 2 +-
 hw/virtio/vhost-user.c    | 2 +-
 monitor/monitor.c         | 2 +-
 net/vhost-user.c          | 4 ++--
 12 files changed, 19 insertions(+), 13 deletions(-)
include/chardev/char-fe.h +7 -1
index a553843364..867ef1b3b2 100644
@@ -174,6 +174,9 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open);
 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
+
+typedef gboolean (*FEWatchFunc)(void *do_not_use, GIOCondition condition, void *data);
+
 /**
  * qemu_chr_fe_add_watch:
  * @cond: the condition to poll for
@@ -188,10 +191,13 @@ void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
  * Note that you are responsible to update the front-end sources if
  * you are switching the main context with qemu_chr_fe_set_handlers().
  *
+ * Warning: DO NOT use the first callback argument (it may be either
+ * a GIOChannel or a QIOChannel, depending on the underlying chardev)
+ *
  * Returns: the source tag
  */
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data);
+                            FEWatchFunc func, void *user_data);
 
 /**
  * qemu_chr_fe_write:
chardev/char-fe.c +1 -1
index 474715c5a9..7789f7be9c 100644
@@ -354,7 +354,7 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
 }
 
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data)
+                            FEWatchFunc func, void *user_data)
 {
     Chardev *s = be->chr;
     GSource *src;
hw/char/cadence_uart.c +0 -0
index ceb677bc5a..8ee6f74b8c 100644

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size) · line 292

Quoted 6 lines
hw/char/cadence_uart.c +1 -1
@@ -288,7 +288,7 @@ static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size)
     uart_update_status(s);
 }
 
-static gboolean cadence_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean cadence_uart_xmit(GIOChannel *do_not_use, GIOCondition cond,
                                   void *opaque)

Why is this (and the next 3) left as GIOCondition, when you change others later on to be void ?

Quoted 124 lines
hw/char/cadence_uart.c +0 -0
 {
     CadenceUARTState *s = opaque;
hw/char/cmsdk-apb-uart.c +1 -1
index ba2cbbee3d..b07a9dee4f 100644
@@ -191,7 +191,7 @@ static uint64_t uart_read(void *opaque, hwaddr offset, unsigned size)
 /* Try to send tx data, and arrange to be called back later if
  * we can't (ie the char backend is busy/blocking).
  */
-static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void *opaque)
+static gboolean uart_transmit(GIOChannel *do_not_use, GIOCondition cond, void *opaque)
 {
     CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
     int ret;
hw/char/ibex_uart.c +1 -1
index 6b0c9330bf..e493ea08c0 100644
@@ -135,7 +135,7 @@ static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
     ibex_uart_update_irqs(s);
 }
 
-static gboolean ibex_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean ibex_uart_xmit(GIOChannel *do_not_use, GIOCondition cond,
                                void *opaque)
 {
     IbexUartState *s = opaque;
hw/char/nrf51_uart.c +1 -1
index 045ca5fa40..0b89b0eae4 100644
@@ -75,7 +75,7 @@ static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
     return r;
 }
 
-static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void *opaque)
+static gboolean uart_transmit(GIOChannel *do_not_use, GIOCondition cond, void *opaque)
 {
     NRF51UARTState *s = NRF51_UART(opaque);
     int r;
hw/char/serial.c +1 -1
index bc2e322970..7061aacbce 100644
@@ -220,7 +220,7 @@ static void serial_update_msl(SerialState *s)
     }
 }
 
-static gboolean serial_watch_cb(GIOChannel *chan, GIOCondition cond,
+static gboolean serial_watch_cb(void *do_not_use, GIOCondition cond,
                                 void *opaque)
 {
     SerialState *s = opaque;
hw/char/virtio-console.c +1 -1
index 6b132caa29..dd5a02e339 100644
@@ -38,7 +38,7 @@ struct VirtConsole {
  * Callback function that's called from chardevs when backend becomes
  * writable.
  */
-static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean chr_write_unblocked(void *do_not_use, GIOCondition cond,
                                     void *opaque)
 {
     VirtConsole *vcon = opaque;
hw/usb/redirect.c +1 -1
index 1ec909a63a..5f0ef9cb3b 100644
@@ -270,7 +270,7 @@ static int usbredir_read(void *priv, uint8_t *data, int count)
     return count;
 }
 
-static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean usbredir_write_unblocked(void *do_not_use, GIOCondition cond,
                                          void *opaque)
 {
     USBRedirDevice *dev = opaque;
hw/virtio/vhost-user.c +1 -1
index 29ea2b4fce..aec6cc1990 100644
@@ -303,7 +303,7 @@ struct vhost_user_read_cb_data {
     int ret;
 };
 
-static gboolean vhost_user_read_cb(GIOChannel *source, GIOCondition condition,
+static gboolean vhost_user_read_cb(void *do_not_use, GIOCondition condition,
                                    gpointer opaque)
 {
     struct vhost_user_read_cb_data *data = opaque;
monitor/monitor.c +1 -1
index b90c0f4051..46a171bca6 100644
@@ -156,7 +156,7 @@ static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
 
 static void monitor_flush_locked(Monitor *mon);
 
-static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond,
                                   void *opaque)
 {
     Monitor *mon = opaque;
net/vhost-user.c +2 -2
index ffbd94d944..6adfcd623a 100644
@@ -208,8 +208,8 @@ static NetClientInfo net_vhost_user_info = {
         .set_vnet_le = vhost_user_set_vnet_endianness,
 };
 
-static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
-                                           void *opaque)
+static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
+                                     void *opaque)
 {
     NetVhostUserState *s = opaque;
 
Signature · 1 line
2.32.0.264.g75ae10bc75

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 :|
#8 lore raw

Re: [PATCH v2 05/11] chardev: mark explicitly first argument as poisoned

Hi

Quoted 84 lines · Daniel P. Berrangé

include/chardev/char-fe.h void qemu_chr_fe_set_open(CharBackend *be, int

Quoted 32 lines · On Wed, Aug 04, 2021 at 07:48:42PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Since commit 9894dc0cdcc397ee5b26370bc53da6d360a363c2 "char: convert from GIOChannel to QIOChannel", the first argument to the watch callback can actually be a QIOChannel, which is not a GIOChannel (but a QEMU Object).

Even though we never used that pointer, change the callback type to warn the users. Possibly a better fix later, we may want to store the callback and call it from intermediary functions.

---
 include/chardev/char-fe.h | 8 +++++++-
 chardev/char-fe.c         | 2 +-
 hw/char/cadence_uart.c    | 2 +-
 hw/char/cmsdk-apb-uart.c  | 2 +-
 hw/char/ibex_uart.c       | 2 +-
 hw/char/nrf51_uart.c      | 2 +-
 hw/char/serial.c          | 2 +-
 hw/char/virtio-console.c  | 2 +-
 hw/usb/redirect.c         | 2 +-
 hw/virtio/vhost-user.c    | 2 +-
 monitor/monitor.c         | 2 +-
 net/vhost-user.c          | 4 ++--
 12 files changed, 19 insertions(+), 13 deletions(-)
include/chardev/char-fe.h +0 -0
index a553843364..867ef1b3b2 100644
@@ -174,6 +174,9 @@ void qemu_chr_fe_set_open(CharBackend *be, int

fe_open);

include/chardev/char-fe.h void qemu_chr_fe_set_open(CharBackend *be, int fe_open);

Quoted 5 lines
include/chardev/char-fe.h +2 -0
 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
+
+typedef gboolean (*FEWatchFunc)(void *do_not_use, GIOCondition

condition, void *data);

include/chardev/char-fe.h void qemu_chr_fe_printf(CharBackend *be, const

Quoted 5 lines
include/chardev/char-fe.h +1 -0
+
 /**
  * qemu_chr_fe_add_watch:
  * @cond: the condition to poll for
@@ -188,10 +191,13 @@ void qemu_chr_fe_printf(CharBackend *be, const

char *fmt, ...)

chardev/char-fe.c void qemu_chr_fe_set_open(CharBackend *be, int

Quoted 19 lines
include/chardev/char-fe.h +4 -1
  * Note that you are responsible to update the front-end sources if
  * you are switching the main context with qemu_chr_fe_set_handlers().
  *
+ * Warning: DO NOT use the first callback argument (it may be either
+ * a GIOChannel or a QIOChannel, depending on the underlying chardev)
+ *
  * Returns: the source tag
  */
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data);
+                            FEWatchFunc func, void *user_data);
 
 /**
  * qemu_chr_fe_write:
chardev/char-fe.c +0 -0
index 474715c5a9..7789f7be9c 100644
@@ -354,7 +354,7 @@ void qemu_chr_fe_set_open(CharBackend *be, int

fe_open)

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const

Quoted 13 lines
chardev/char-fe.c +1 -1
 }
 
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data)
+                            FEWatchFunc func, void *user_data)
 {
     Chardev *s = be->chr;
     GSource *src;
hw/char/cadence_uart.c +0 -0
index ceb677bc5a..8ee6f74b8c 100644
@@ -288,7 +288,7 @@ static void uart_write_rx_fifo(void *opaque, const

uint8_t *buf, int size)

Quoted 3 lines
hw/char/cadence_uart.c +0 -0
     uart_update_status(s);
 }
 
Quoted 8 lines

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const · line 291

Quoted 2 lines
hw/char/cadence_uart.c +1 -1
-static gboolean cadence_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean cadence_uart_xmit(GIOChannel *do_not_use, GIOCondition

cond,

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const · line 292

Quoted 1 line
hw/char/cadence_uart.c +0 -0
                                   void *opaque)

Why is this (and the next 3) left as GIOCondition, when you change others later on to be void ?

Good catch. It's a leftover. It is fixed in my branch
https://gitlab.com/marcandre.lureau/qemu/-/tree/chardev

thanks

Quoted 153 lines

hw/char/cmsdk-apb-uart.c static uint64_t uart_read(void *opaque, hwaddr

Quoted 7 lines
hw/char/cadence_uart.c +0 -0
 {
     CadenceUARTState *s = opaque;
hw/char/cmsdk-apb-uart.c +0 -0
index ba2cbbee3d..b07a9dee4f 100644
@@ -191,7 +191,7 @@ static uint64_t uart_read(void *opaque, hwaddr

offset, unsigned size)

hw/char/cmsdk-apb-uart.c static uint64_t uart_read(void *opaque, hwaddr offset, unsigned size)

Quoted 4 lines
hw/char/cmsdk-apb-uart.c +0 -1
 /* Try to send tx data, and arrange to be called back later if
  * we can't (ie the char backend is busy/blocking).
  */
-static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void

*opaque)

hw/char/cmsdk-apb-uart.c static uint64_t uart_read(void *opaque, hwaddr · line 194

Quoted 1 line
hw/char/cmsdk-apb-uart.c +1 -0
+static gboolean uart_transmit(GIOChannel *do_not_use, GIOCondition

cond, void *opaque)

hw/char/ibex_uart.c static void ibex_uart_receive(void *opaque, const

Quoted 8 lines
hw/char/cmsdk-apb-uart.c +0 -0
 {
     CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
     int ret;
hw/char/ibex_uart.c +0 -0
index 6b0c9330bf..e493ea08c0 100644
@@ -135,7 +135,7 @@ static void ibex_uart_receive(void *opaque, const

uint8_t *buf, int size)

hw/char/ibex_uart.c static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)

Quoted 5 lines
hw/char/ibex_uart.c +1 -1
     ibex_uart_update_irqs(s);
 }
 
-static gboolean ibex_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean ibex_uart_xmit(GIOChannel *do_not_use, GIOCondition

cond,

hw/char/nrf51_uart.c static uint64_t uart_read(void *opaque, hwaddr addr,

Quoted 8 lines
hw/char/ibex_uart.c +0 -0
                                void *opaque)
 {
     IbexUartState *s = opaque;
hw/char/nrf51_uart.c +0 -0
index 045ca5fa40..0b89b0eae4 100644
@@ -75,7 +75,7 @@ static uint64_t uart_read(void *opaque, hwaddr addr,

unsigned int size)

hw/char/nrf51_uart.c static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)

Quoted 4 lines
hw/char/nrf51_uart.c +0 -1
     return r;
 }
 
-static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void

*opaque)

hw/char/nrf51_uart.c static uint64_t uart_read(void *opaque, hwaddr addr, · line 78

Quoted 1 line
hw/char/nrf51_uart.c +1 -0
+static gboolean uart_transmit(GIOChannel *do_not_use, GIOCondition

cond, void *opaque)

hw/usb/redirect.c static int usbredir_read(void *priv, uint8_t *data,

Quoted 34 lines
hw/char/nrf51_uart.c +0 -0
 {
     NRF51UARTState *s = NRF51_UART(opaque);
     int r;
hw/char/serial.c +1 -1
index bc2e322970..7061aacbce 100644
@@ -220,7 +220,7 @@ static void serial_update_msl(SerialState *s)
     }
 }
 
-static gboolean serial_watch_cb(GIOChannel *chan, GIOCondition cond,
+static gboolean serial_watch_cb(void *do_not_use, GIOCondition cond,
                                 void *opaque)
 {
     SerialState *s = opaque;
hw/char/virtio-console.c +1 -1
index 6b132caa29..dd5a02e339 100644
@@ -38,7 +38,7 @@ struct VirtConsole {
  * Callback function that's called from chardevs when backend becomes
  * writable.
  */
-static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean chr_write_unblocked(void *do_not_use, GIOCondition cond,
                                     void *opaque)
 {
     VirtConsole *vcon = opaque;
hw/usb/redirect.c +0 -0
index 1ec909a63a..5f0ef9cb3b 100644
@@ -270,7 +270,7 @@ static int usbredir_read(void *priv, uint8_t *data,

int count)

hw/usb/redirect.c static int usbredir_read(void *priv, uint8_t *data, int count)

Quoted 4 lines
hw/usb/redirect.c +0 -1
     return count;
 }
 
-static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition

cond,

hw/usb/redirect.c static int usbredir_read(void *priv, uint8_t *data, · line 273

Quoted 1 line
hw/usb/redirect.c +1 -0
+static gboolean usbredir_write_unblocked(void *do_not_use, GIOCondition

cond,

hw/virtio/vhost-user.c struct vhost_user_read_cb_data { · line 306

Quoted 12 lines
hw/usb/redirect.c +0 -0
                                          void *opaque)
 {
     USBRedirDevice *dev = opaque;
hw/virtio/vhost-user.c +0 -1
index 29ea2b4fce..aec6cc1990 100644
@@ -303,7 +303,7 @@ struct vhost_user_read_cb_data {
     int ret;
 };
 
-static gboolean vhost_user_read_cb(GIOChannel *source, GIOCondition

condition,

hw/virtio/vhost-user.c struct vhost_user_read_cb_data { · line 306

Quoted 1 line
hw/virtio/vhost-user.c +1 -0
+static gboolean vhost_user_read_cb(void *do_not_use, GIOCondition

condition,

monitor/monitor.c static inline bool

Quoted 8 lines
hw/virtio/vhost-user.c +0 -0
                                    gpointer opaque)
 {
     struct vhost_user_read_cb_data *data = opaque;
monitor/monitor.c +0 -0
index b90c0f4051..46a171bca6 100644
@@ -156,7 +156,7 @@ static inline bool

monitor_is_hmp_non_interactive(const Monitor *mon)

Quoted 13 lines
monitor/monitor.c +1 -1
 static void monitor_flush_locked(Monitor *mon);
 
-static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
+static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond,
                                   void *opaque)
 {
     Monitor *mon = opaque;
net/vhost-user.c +0 -0
index ffbd94d944..6adfcd623a 100644

net/vhost-user.c static NetClientInfo net_vhost_user_info = { · line 211

Quoted 4 lines
net/vhost-user.c +0 -1
@@ -208,8 +208,8 @@ static NetClientInfo net_vhost_user_info = {
         .set_vnet_le = vhost_user_set_vnet_endianness,
 };
 
-static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition

cond,

net/vhost-user.c static NetClientInfo net_vhost_user_info = { · line 211

Quoted 2 lines
net/vhost-user.c +1 -1
-                                           void *opaque)
+static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition

cond,

Quoted 7 lines
net/vhost-user.c +1 -1
+                                     void *opaque)
 {
     NetVhostUserState *s = opaque;
 
--

2.32.0.264.g75ae10bc75

Regards,
Daniel
--
|: 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 :|

Signature · 1 line
Marc-André Lureau
#9 lore raw

Re: [PATCH v2 05/11] chardev: mark explicitly first argument as poisoned

Quoted 92 lines · Marc-André Lureau

Hi

Quoted 87 lines · Daniel P. Berrangé

include/chardev/char-fe.h void qemu_chr_fe_set_open(CharBackend *be, int

Quoted 32 lines · On Wed, Aug 04, 2021 at 07:48:42PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Since commit 9894dc0cdcc397ee5b26370bc53da6d360a363c2 "char: convert from GIOChannel to QIOChannel", the first argument to the watch callback can actually be a QIOChannel, which is not a GIOChannel (but a QEMU Object).

Even though we never used that pointer, change the callback type to warn the users. Possibly a better fix later, we may want to store the callback and call it from intermediary functions.

---
 include/chardev/char-fe.h | 8 +++++++-
 chardev/char-fe.c         | 2 +-
 hw/char/cadence_uart.c    | 2 +-
 hw/char/cmsdk-apb-uart.c  | 2 +-
 hw/char/ibex_uart.c       | 2 +-
 hw/char/nrf51_uart.c      | 2 +-
 hw/char/serial.c          | 2 +-
 hw/char/virtio-console.c  | 2 +-
 hw/usb/redirect.c         | 2 +-
 hw/virtio/vhost-user.c    | 2 +-
 monitor/monitor.c         | 2 +-
 net/vhost-user.c          | 4 ++--
 12 files changed, 19 insertions(+), 13 deletions(-)
include/chardev/char-fe.h +0 -0
index a553843364..867ef1b3b2 100644
@@ -174,6 +174,9 @@ void qemu_chr_fe_set_open(CharBackend *be, int

fe_open);

include/chardev/char-fe.h void qemu_chr_fe_set_open(CharBackend *be, int fe_open);

Quoted 5 lines
include/chardev/char-fe.h +2 -0
 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
+
+typedef gboolean (*FEWatchFunc)(void *do_not_use, GIOCondition

condition, void *data);

include/chardev/char-fe.h void qemu_chr_fe_printf(CharBackend *be, const

Quoted 5 lines
include/chardev/char-fe.h +1 -0
+
 /**
  * qemu_chr_fe_add_watch:
  * @cond: the condition to poll for
@@ -188,10 +191,13 @@ void qemu_chr_fe_printf(CharBackend *be, const

char *fmt, ...)

chardev/char-fe.c void qemu_chr_fe_set_open(CharBackend *be, int

Quoted 19 lines
include/chardev/char-fe.h +4 -1
  * Note that you are responsible to update the front-end sources if
  * you are switching the main context with qemu_chr_fe_set_handlers().
  *
+ * Warning: DO NOT use the first callback argument (it may be either
+ * a GIOChannel or a QIOChannel, depending on the underlying chardev)
+ *
  * Returns: the source tag
  */
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data);
+                            FEWatchFunc func, void *user_data);
 
 /**
  * qemu_chr_fe_write:
chardev/char-fe.c +0 -0
index 474715c5a9..7789f7be9c 100644
@@ -354,7 +354,7 @@ void qemu_chr_fe_set_open(CharBackend *be, int

fe_open)

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const

Quoted 13 lines
chardev/char-fe.c +1 -1
 }
 
 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
-                            GIOFunc func, void *user_data)
+                            FEWatchFunc func, void *user_data)
 {
     Chardev *s = be->chr;
     GSource *src;
hw/char/cadence_uart.c +0 -0
index ceb677bc5a..8ee6f74b8c 100644
@@ -288,7 +288,7 @@ static void uart_write_rx_fifo(void *opaque, const

uint8_t *buf, int size)

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size)

Quoted 5 lines
hw/char/cadence_uart.c +1 -1
     uart_update_status(s);
 }
 
-static gboolean cadence_uart_xmit(GIOChannel *chan, GIOCondition cond,
+static gboolean cadence_uart_xmit(GIOChannel *do_not_use, GIOCondition

cond,

Quoted 8 lines
Quoted 5 lines

hw/char/cadence_uart.c static void uart_write_rx_fifo(void *opaque, const · line 292

Quoted 1 line
hw/char/cadence_uart.c +0 -0
                                   void *opaque)

Why is this (and the next 3) left as GIOCondition, when you change others later on to be void ?

Good catch. It's a leftover. It is fixed in my branch
https://gitlab.com/marcandre.lureau/qemu/-/tree/chardev

Ok, with that

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 :|
#10 lore raw

PATCH v2 6/11 chardev: fix fd_chr_add_watch() when in != out

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Create child sources for the different streams, and dispatch on the parent source with the synthesized conditions.

---
 chardev/char-fd.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)
chardev/char-fd.c +77 -1
index 1cd62f2779..743d3989b4 100644
@@ -28,6 +28,7 @@
 #include "qemu/sockets.h"
 #include "qapi/error.h"
 #include "chardev/char.h"
+#include "chardev/char-fe.h"
 #include "io/channel-file.h"
 
 #include "chardev/char-fd.h"
@@ -80,10 +81,85 @@ static int fd_chr_read_poll(void *opaque)
     return s->max_size;
 }
 
+typedef struct FDSource {
+    GSource parent;
+
+    GIOCondition cond;
+} FDSource;
+
+static gboolean
+fd_source_prepare(GSource *source,
+                  gint *timeout_)
+{
+    FDSource *src = (FDSource *)source;
+
+    return src->cond != 0;
+}
+
+static gboolean
+fd_source_check(GSource *source)
+{
+    FDSource *src = (FDSource *)source;
+
+    return src->cond != 0;
+}
+
+static gboolean
+fd_source_dispatch(GSource *source, GSourceFunc callback,
+                   gpointer user_data)
+{
+    FDSource *src = (FDSource *)source;
+    FEWatchFunc func = (FEWatchFunc)callback;
+    gboolean ret = G_SOURCE_CONTINUE;
+
+    if (src->cond) {
+        ret = func(NULL, src->cond, user_data);
+        src->cond = 0;
+    }
+
+    return ret;
+}
+
+static GSourceFuncs fd_source_funcs = {
+  fd_source_prepare,
+  fd_source_check,
+  fd_source_dispatch,
+  NULL, NULL, NULL
+};
+
+static GSource *fd_source_new(FDChardev *chr)
+{
+    return g_source_new(&fd_source_funcs, sizeof(FDSource));
+}
+
+static gboolean child_func(GIOChannel *source,
+                           GIOCondition condition,
+                           gpointer data)
+{
+    FDSource *parent = data;
+
+    parent->cond |= condition;
+
+    return G_SOURCE_CONTINUE;
+}
+
 static GSource *fd_chr_add_watch(Chardev *chr, GIOCondition cond)
 {
     FDChardev *s = FD_CHARDEV(chr);
-    return qio_channel_create_watch(s->ioc_out, cond);
+    g_autoptr(GSource) source = fd_source_new(s);
+
+    if (s->ioc_out) {
+        g_autoptr(GSource) child = qio_channel_create_watch(s->ioc_out, cond & ~G_IO_IN);
+        g_source_set_callback(child, (GSourceFunc)child_func, source, NULL);
+        g_source_add_child_source(source, child);
+    }
+    if (s->ioc_in) {
+        g_autoptr(GSource) child = qio_channel_create_watch(s->ioc_in, cond & ~G_IO_OUT);
+        g_source_set_callback(child, (GSourceFunc)child_func, source, NULL);
+        g_source_add_child_source(source, child);
+    }
+
+    return g_steal_pointer(&source);
 }
 
 static void fd_chr_update_read_handler(Chardev *chr)
Signature · 1 line
2.32.0.264.g75ae10bc75
Reviewed-by on v2 6/11
#11 lore raw

Re: [PATCH v2 06/11] chardev: fix fd_chr_add_watch() when in != out

Quoted 9 lines · On Wed, Aug 04, 2021 at 07:48:43PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Create child sources for the different streams, and dispatch on the parent source with the synthesized conditions.

---
 chardev/char-fd.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)

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 :|
#12 lore raw

PATCH v2 7/11 chardev: fix qemu_chr_open_fd() being called with fd=-1

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

The "file" chardev may call qemu_chr_open_fd() with fd_in=-1. This may cause invalid system calls, as the QIOChannel is assumed to be properly initialized later on.

---
 chardev/char-fd.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
chardev/char-fd.c +17 -9
index 743d3989b4..c11b1037f9 100644
@@ -39,6 +39,10 @@ static int fd_chr_write(Chardev *chr, const uint8_t *buf, int len)
 {
     FDChardev *s = FD_CHARDEV(chr);
 
+    if (!s->ioc_out) {
+        return -1;
+    }
+
     return io_channel_send(s->ioc_out, buf, len);
 }
 
@@ -209,15 +213,19 @@ void qemu_chr_open_fd(Chardev *chr,
     FDChardev *s = FD_CHARDEV(chr);
     char *name;
 
-    s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
-    name = g_strdup_printf("chardev-file-in-%s", chr->label);
-    qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
-    g_free(name);
-    s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out));
-    name = g_strdup_printf("chardev-file-out-%s", chr->label);
-    qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name);
-    g_free(name);
-    qemu_set_nonblock(fd_out);
+    if (fd_in >= 0) {
+        s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
+        name = g_strdup_printf("chardev-file-in-%s", chr->label);
+        qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
+        g_free(name);
+    }
+    if (fd_out >= 0) {
+        s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out));
+        name = g_strdup_printf("chardev-file-out-%s", chr->label);
+        qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name);
+        g_free(name);
+        qemu_set_nonblock(fd_out);
+    }
 }
 
 static void char_fd_class_init(ObjectClass *oc, void *data)
Signature · 1 line
2.32.0.264.g75ae10bc75
Reviewed-by on v2 7/11
#13 lore raw

Re: [PATCH v2 07/11] chardev: fix qemu_chr_open_fd() being called with fd=-1

Quoted 10 lines · On Wed, Aug 04, 2021 at 07:48:44PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

The "file" chardev may call qemu_chr_open_fd() with fd_in=-1. This may cause invalid system calls, as the QIOChannel is assumed to be properly initialized later on.

---
 chardev/char-fd.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 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 :|
#14 lore raw

PATCH v2 8/11 chardev: fix qemu_chr_open_fd() with fd_in==fd_out

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

The "serial" chardev calls qemu_chr_open_fd() with the same fd. This may lead to double-close as each QIOChannel owns the fd.

Instead, share the reference to the same QIOChannel.

---
 chardev/char-fd.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
chardev/char-fd.c +15 -4
index c11b1037f9..93c56913b4 100644
@@ -211,20 +211,31 @@ void qemu_chr_open_fd(Chardev *chr,
                       int fd_in, int fd_out)
 {
     FDChardev *s = FD_CHARDEV(chr);
-    char *name;
+    g_autofree char *name = NULL;
+
+    if (fd_out >= 0) {
+        qemu_set_nonblock(fd_out);
+    }
+
+    if (fd_out == fd_in && fd_in >= 0) {
+        s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
+        name = g_strdup_printf("chardev-file-%s", chr->label);
+        qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
+        s->ioc_out = QIO_CHANNEL(object_ref(s->ioc_in));
+        return;
+    }
 
     if (fd_in >= 0) {
         s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
         name = g_strdup_printf("chardev-file-in-%s", chr->label);
         qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
-        g_free(name);
     }
+
     if (fd_out >= 0) {
         s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out));
+        g_free(name);
         name = g_strdup_printf("chardev-file-out-%s", chr->label);
         qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name);
-        g_free(name);
-        qemu_set_nonblock(fd_out);
     }
 }
 
Signature · 1 line
2.32.0.264.g75ae10bc75
Reviewed-by on v2 8/11
#15 lore raw

Re: [PATCH v2 08/11] chardev: fix qemu_chr_open_fd() with fd_in==fd_out

Quoted 11 lines · On Wed, Aug 04, 2021 at 07:48:45PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

The "serial" chardev calls qemu_chr_open_fd() with the same fd. This may lead to double-close as each QIOChannel owns the fd.

Instead, share the reference to the same QIOChannel.

---
 chardev/char-fd.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 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 :|
#16 lore raw

PATCH v2 9/11 chardev: give some context on chardev-add error

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Description from Daniel P. Berrangé:

Quoted 10 lines

The original code reported:

"attempt to add duplicate property 'char2' to object (type 'container')"

Since adding yank support, the current code reports

"duplicate yank instance"

With this patch applied it now reports:

Quoted 8 lines

"Failed to add chardev 'char2': duplicate yank instance"

This is marginally better, but still not great, not that the original error was great either.

It would be nice if we could report

"chardev with id 'char2' already exists"

Related to:
https://bugzilla.redhat.com/show_bug.cgi?id=1984721

---
 chardev/char.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
chardev/char.c +9 -6
index d959eec522..f59a61774b 100644
@@ -1031,27 +1031,26 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
                                Error **errp)
 {
+    ERRP_GUARD();
     const ChardevClass *cc;
     ChardevReturn *ret;
-    Chardev *chr;
+    g_autoptr(Chardev) chr = NULL;
 
     cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
     if (!cc) {
-        return NULL;
+        goto err;
     }
 
     chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
                       backend, NULL, false, errp);
     if (!chr) {
-        return NULL;
+        goto err;
     }
 
     if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
                                        errp)) {
-        object_unref(OBJECT(chr));
-        return NULL;
+        goto err;
     }
-    object_unref(OBJECT(chr));
 
     ret = g_new0(ChardevReturn, 1);
     if (CHARDEV_IS_PTY(chr)) {
@@ -1060,6 +1059,10 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     }
 
     return ret;
+
+err:
+    error_prepend(errp, "Failed to add chardev '%s': ", id);
+    return NULL;
 }
 
 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
Signature · 1 line
2.32.0.264.g75ae10bc75
#17 lore raw

PATCH v2 10/11 chardev: report a simpler error about duplicated id

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Report:
"Chardev with id 'char2' already exists"
Rather than:
"Failed to add chardev 'char2': duplicate yank instance"

---
 chardev/char.c | 5 +++++
 1 file changed, 5 insertions(+)
chardev/char.c +5 -0
index f59a61774b..4595a8d430 100644
@@ -1036,6 +1036,11 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     ChardevReturn *ret;
     g_autoptr(Chardev) chr = NULL;
 
+    if (qemu_chr_find(id)) {
+        error_setg(errp, "Chardev with id '%s' already exists", id);
+        return NULL;
+    }
+
     cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
     if (!cc) {
         goto err;
Signature · 1 line
2.32.0.264.g75ae10bc75
Reviewed-by on v2 10/11
#18 lore raw

Re: [PATCH v2 10/11] chardev: report a simpler error about duplicated id

Quoted 11 lines · On Wed, Aug 04, 2021 at 07:48:47PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Report:
"Chardev with id 'char2' already exists"
Rather than:
"Failed to add chardev 'char2': duplicate yank instance"

---
 chardev/char.c | 5 +++++
 1 file changed, 5 insertions(+)

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 :|
#19 lore raw

PATCH v2 11/11 chardev: reuse qmp_chardev_new()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

qemu_chardev_new() and qmp_chardev_add() are similar, let's factorize the code a bit.

---
 chardev/char.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)
chardev/char.c +10 -17
index 4595a8d430..3def40c914 100644
@@ -1005,7 +1005,7 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
                           GMainContext *gcontext,
                           Error **errp)
 {
-    Chardev *chr;
+    g_autoptr(Chardev) chr = NULL;
     g_autofree char *genid = NULL;
 
     if (!id) {
@@ -1013,6 +1013,11 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
         id = genid;
     }
 
+    if (qemu_chr_find(id)) {
+        error_setg(errp, "Chardev with id '%s' already exists", id);
+        return NULL;
+    }
+
     chr = chardev_new(id, typename, backend, gcontext, false, errp);
     if (!chr) {
         return NULL;
@@ -1020,12 +1025,10 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
 
     if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
                                        errp)) {
-        object_unref(OBJECT(chr));
         return NULL;
     }
-    object_unref(OBJECT(chr));
 
-    return chr;
+    return chr; /* returns a shared/unowned reference */
 }
 
 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
@@ -1034,29 +1037,19 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     ERRP_GUARD();
     const ChardevClass *cc;
     ChardevReturn *ret;
-    g_autoptr(Chardev) chr = NULL;
-
-    if (qemu_chr_find(id)) {
-        error_setg(errp, "Chardev with id '%s' already exists", id);
-        return NULL;
-    }
+    Chardev *chr = NULL;
 
     cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
     if (!cc) {
         goto err;
     }
 
-    chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
-                      backend, NULL, false, errp);
+    chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
+                           backend, NULL, errp);
     if (!chr) {
         goto err;
     }
 
-    if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
-                                       errp)) {
-        goto err;
-    }
-
     ret = g_new0(ChardevReturn, 1);
     if (CHARDEV_IS_PTY(chr)) {
         ret->pty = g_strdup(chr->filename + 4);
Signature · 1 line
2.32.0.264.g75ae10bc75
Reviewed-by on v2 11/11
#20 lore raw

Re: [PATCH v2 11/11] chardev: reuse qmp_chardev_new()

Quoted 9 lines · On Wed, Aug 04, 2021 at 07:48:48PM +0400, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

qemu_chardev_new() and qmp_chardev_add() are similar, let's factorize the code a bit.

---
 chardev/char.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 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 :|