qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Jagannathan Raman" <jag.raman@oracle.com>,
	"Zhao Liu" <zhao1.liu@intel.com>,
	"Eric Blake" <eblake@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Hanna Reitz" <hreitz@redhat.com>,
	"Gustavo Romero" <gustavo.romero@linaro.org>,
	"Thanos Makatos" <thanos.makatos@nutanix.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"Darren Kenny" <darren.kenny@oracle.com>,
	"Stefano Garzarella" <sgarzare@redhat.com>,
	"Kevin Wolf" <kwolf@redhat.com>,
	"Fabiano Rosas" <farosas@suse.de>,
	qemu-block@nongnu.org, "Peter Xu" <peterx@redhat.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Elena Ufimtseva" <elena.ufimtseva@oracle.com>,
	"John Levon" <john.levon@nutanix.com>,
	"Fam Zheng" <fam@euphon.net>, "Alexander Bulekov" <alxndr@bu.edu>,
	"Stefan Weil" <sw@weilnetz.de>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Coiby Xu" <Coiby.Xu@gmail.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Qiuhao Li" <Qiuhao.Li@outlook.com>,
	"Michael Roth" <michael.roth@amd.com>,
	"Vladimir Sementsov-Ogievskiy" <vsementsov@yandex-team.ru>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Bandan Das" <bsd@redhat.com>,
	"Kostiantyn Kostiuk" <kkostiuk@redhat.com>,
	"Hailiang Zhang" <zhanghailiang@xfusion.com>
Subject: [PULL 11/16] io/channel-socket: rework qio_channel_socket_copy_fds()
Date: Fri, 19 Sep 2025 12:50:12 +0100	[thread overview]
Message-ID: <20250919115017.1536203-12-berrange@redhat.com> (raw)
In-Reply-To: <20250919115017.1536203-1-berrange@redhat.com>

From: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

We want to switch from qemu_socket_set_block() to newer
qemu_set_blocking(), which provides return status of operation,
to handle errors.

Still, we want to keep qio_channel_socket_readv() interface clean,
as currently it allocate @fds only on success.

So, in case of error, we should close all incoming fds and keep
user's @fds untouched or zero.

Let's make separate functions qio_channel_handle_fds() and
qio_channel_cleanup_fds(), to achieve what we want.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 include/io/channel.h |  8 +++---
 io/channel-socket.c  | 67 +++++++++++++++++++++++++++++++++-----------
 2 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/include/io/channel.h b/include/io/channel.h
index 999a8f5f23..0f25ae0069 100644
--- a/include/io/channel.h
+++ b/include/io/channel.h
@@ -124,8 +124,8 @@ struct QIOChannelClass {
      * incoming fds are set BLOCKING (unless
      * QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING flag is set) and
      * CLOEXEC (if available).
-     * @fds and @nfds are set only on success path, and untouched
-     * in case of errors.
+     * @fds and @nfds are set only on success path. Still, setting
+     * @fds and @nfds to zero is acceptable on failure path.
      */
     ssize_t (*io_readv)(QIOChannel *ioc,
                         const struct iovec *iov,
@@ -246,8 +246,8 @@ void qio_channel_set_name(QIOChannel *ioc,
  * to call close() on each file descriptor and to
  * call g_free() on the array pointer in @fds.
  * @fds allocated and set (and @nfds is set too)
- * _only_ on success path. These parameters are
- * untouched in case of errors.
+ * _only_ on success path. Still, @fds and @nfds
+ * may be set to zero on failure path.
  * qio_channel_readv_full() guarantees that all
  * incoming fds are set BLOCKING (unless
  * QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING flag
diff --git a/io/channel-socket.c b/io/channel-socket.c
index f7e3cb9742..e53d9ac76f 100644
--- a/io/channel-socket.c
+++ b/io/channel-socket.c
@@ -464,8 +464,7 @@ static void qio_channel_socket_finalize(Object *obj)
 
 #ifndef WIN32
 static void qio_channel_socket_copy_fds(struct msghdr *msg,
-                                        int **fds, size_t *nfds,
-                                        bool preserve_blocking)
+                                        int **fds, size_t *nfds)
 {
     struct cmsghdr *cmsg;
 
@@ -473,7 +472,7 @@ static void qio_channel_socket_copy_fds(struct msghdr *msg,
     *fds = NULL;
 
     for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
-        int fd_size, i;
+        int fd_size;
         int gotfds;
 
         if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
@@ -491,24 +490,53 @@ static void qio_channel_socket_copy_fds(struct msghdr *msg,
         gotfds = fd_size / sizeof(int);
         *fds = g_renew(int, *fds, *nfds + gotfds);
         memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
+        *nfds += gotfds;
+    }
+}
 
-        for (i = 0; i < gotfds; i++) {
-            int fd = (*fds)[*nfds + i];
-            if (fd < 0) {
-                continue;
-            }
+static bool qio_channel_handle_fds(int *fds, size_t nfds,
+                                   bool preserve_blocking, Error **errp)
+{
+    int *end = fds + nfds, *fd;
+
+#ifdef MSG_CMSG_CLOEXEC
+    if (preserve_blocking) {
+        /* Nothing to do */
+        return true;
+    }
+#endif
 
-            if (!preserve_blocking) {
-                /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
-                qemu_socket_set_block(fd);
+    for (fd = fds; fd != end; fd++) {
+        if (*fd < 0) {
+            continue;
+        }
+
+        if (!preserve_blocking) {
+            /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
+            if (!qemu_set_blocking(*fd, true, errp)) {
+                return false;
             }
+        }
 
 #ifndef MSG_CMSG_CLOEXEC
-            qemu_set_cloexec(fd);
+        qemu_set_cloexec(*fd);
 #endif
+    }
+
+    return true;
+}
+
+static void qio_channel_cleanup_fds(int **fds, size_t *nfds)
+{
+    for (size_t i = 0; i < *nfds; i++) {
+        if ((*fds)[i] < 0) {
+            continue;
         }
-        *nfds += gotfds;
+        close((*fds)[i]);
     }
+
+    g_clear_pointer(fds, g_free);
+    *nfds = 0;
 }
 
 
@@ -559,9 +587,16 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
     }
 
     if (fds && nfds) {
-        qio_channel_socket_copy_fds(
-            &msg, fds, nfds,
-            flags & QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING);
+        bool preserve_blocking =
+            flags & QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING;
+
+        qio_channel_socket_copy_fds(&msg, fds, nfds);
+
+        if (!qio_channel_handle_fds(*fds, *nfds,
+                                    preserve_blocking, errp)) {
+            qio_channel_cleanup_fds(fds, nfds);
+            return -1;
+        }
     }
 
     return ret;
-- 
2.50.1



  parent reply	other threads:[~2025-09-19 11:57 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-19 11:50 [PULL 00/16] Treewide I/O handle cleanup Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 01/16] MAINTAINERS: list qemu-security@nongnu.org as security contact Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 02/16] migration/qemu-file: don't make incoming fds blocking again Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 03/16] io/channel: document how qio_channel_readv_full() handles fds Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 04/16] char-socket: tcp_chr_recv(): drop extra _set_(block, cloexec) Daniel P. Berrangé via
2025-09-19 11:50 ` [PULL 05/16] char-socket: tcp_chr_recv(): add comment Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 06/16] util: add qemu_set_blocking() function Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 07/16] treewide: handle result of qio_channel_set_blocking() Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 08/16] migration: qemu_file_set_blocking(): add errp parameter Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 09/16] util: drop qemu_socket_set_nonblock() Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 10/16] util: drop qemu_socket_try_set_nonblock() Daniel P. Berrangé
2025-09-19 11:50 ` Daniel P. Berrangé [this message]
2025-09-19 11:50 ` [PULL 12/16] util: drop qemu_socket_set_block() Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 13/16] treewide: use qemu_set_blocking instead of g_unix_set_fd_nonblocking Daniel P. Berrangé
2025-09-22 13:40   ` Peter Maydell
2025-09-22 14:19     ` Vladimir Sementsov-Ogievskiy
2025-09-19 11:50 ` [PULL 14/16] chardev: qemu_chr_open_fd(): add errp Daniel P. Berrangé
2025-09-22 13:45   ` Peter Maydell
2025-09-22 14:27     ` Vladimir Sementsov-Ogievskiy
2025-10-14 12:52       ` Peter Maydell
2025-10-14 13:20         ` Vladimir Sementsov-Ogievskiy
2025-09-22 14:29     ` Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 15/16] chardev: close an fd on failure path Daniel P. Berrangé
2025-09-19 11:50 ` [PULL 16/16] util/vhost-user-server: vu_message_read(): improve error handling Daniel P. Berrangé
2025-09-19 20:47 ` [PULL 00/16] Treewide I/O handle cleanup Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250919115017.1536203-12-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=Coiby.Xu@gmail.com \
    --cc=Qiuhao.Li@outlook.com \
    --cc=alxndr@bu.edu \
    --cc=bsd@redhat.com \
    --cc=clg@redhat.com \
    --cc=darren.kenny@oracle.com \
    --cc=eblake@redhat.com \
    --cc=elena.ufimtseva@oracle.com \
    --cc=fam@euphon.net \
    --cc=farosas@suse.de \
    --cc=gustavo.romero@linaro.org \
    --cc=hreitz@redhat.com \
    --cc=jag.raman@oracle.com \
    --cc=jasowang@redhat.com \
    --cc=john.levon@nutanix.com \
    --cc=kkostiuk@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=michael.roth@amd.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sgarzare@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=sw@weilnetz.de \
    --cc=thanos.makatos@nutanix.com \
    --cc=vsementsov@yandex-team.ru \
    --cc=zhanghailiang@xfusion.com \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).