[PATCH v2 17/26] Replace fcntl(0_NONBLOCK) with g_unix_set_fd_nonblocking()

marcandre.lureau@redhat.com posted 26 patches 3 years, 5 months ago
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Jason Wang <jasowang@redhat.com>, Eric Blake <eblake@redhat.com>, Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>, Michael Roth <michael.roth@amd.com>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Thomas Huth <thuth@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Laurent Vivier <lvivier@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <ani@anisinha.ca>, John Snow <jsnow@redhat.com>, Fam Zheng <fam@euphon.net>, Peter Maydell <peter.maydell@linaro.org>, "Cédric Le Goater" <clg@kaod.org>, Andrew Jeffery <andrew@aj.id.au>, Joel Stanley <joel@jms.id.au>, Dmitry Fleytman <dmitry.fleytman@gmail.com>, Hannes Reinecke <hare@suse.com>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Bin Meng <bin.meng@windriver.com>, Su Hang <suhang16@mails.ucas.ac.cn>, Daniel Henrique Barboza <danielhb413@gmail.com>, David Gibson <david@gibson.dropbear.id.au>, Greg Kurz <groug@kaod.org>, Coiby Xu <Coiby.Xu@gmail.com>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Havard Skinnemoen <hskinnemoen@google.com>, Tyrone Ting <kfting@nuvoton.com>, Keith Busch <kbusch@kernel.org>, Klaus Jensen <its@irrelevant.dk>, Markus Armbruster <armbru@redhat.com>, Zhang Chen <chen.zhang@intel.com>, Li Zhijian <lizhijian@fujitsu.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Amit Shah <amit@kernel.org>, Vikram Garhwal <fnu.vikram@xilinx.com>, Francisco Iglesias <francisco.iglesias@xilinx.com>, Stefan Weil <sw@weilnetz.de>
There is a newer version of this series
[PATCH v2 17/26] Replace fcntl(0_NONBLOCK) with g_unix_set_fd_nonblocking()
Posted by marcandre.lureau@redhat.com 3 years, 5 months ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 net/tap-bsd.c                      |  4 ++--
 net/tap-linux.c                    |  2 +-
 net/tap-solaris.c                  |  2 +-
 tests/qtest/fuzz/virtio_net_fuzz.c |  2 +-
 tests/unit/test-iov.c              |  4 ++--
 util/oslib-posix.c                 | 16 ++--------------
 6 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/net/tap-bsd.c b/net/tap-bsd.c
index 7e65bd391f0b..005ce05c6e07 100644
--- a/net/tap-bsd.c
+++ b/net/tap-bsd.c
@@ -98,7 +98,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
             return -1;
         }
     }
-    fcntl(fd, F_SETFL, O_NONBLOCK);
+    g_unix_set_fd_nonblocking(fd, true, NULL);
     return fd;
 }
 
@@ -189,7 +189,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
         goto error;
     }
 
-    fcntl(fd, F_SETFL, O_NONBLOCK);
+    g_unix_set_fd_nonblocking(fd, true, NULL);
     return fd;
 
 error:
diff --git a/net/tap-linux.c b/net/tap-linux.c
index 3e24d232e782..304ff45071dd 100644
--- a/net/tap-linux.c
+++ b/net/tap-linux.c
@@ -113,7 +113,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
         return -1;
     }
     pstrcpy(ifname, ifname_size, ifr.ifr_name);
-    fcntl(fd, F_SETFL, O_NONBLOCK);
+    g_unix_set_fd_nonblocking(fd, true, NULL);
     return fd;
 }
 
diff --git a/net/tap-solaris.c b/net/tap-solaris.c
index 79919785c9f6..a44f8805c23e 100644
--- a/net/tap-solaris.c
+++ b/net/tap-solaris.c
@@ -198,7 +198,7 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
             return -1;
         }
     }
-    fcntl(fd, F_SETFL, O_NONBLOCK);
+    g_unix_set_fd_nonblocking(fd, true, NULL);
     return fd;
 }
 
diff --git a/tests/qtest/fuzz/virtio_net_fuzz.c b/tests/qtest/fuzz/virtio_net_fuzz.c
index 3df78d9c1c2a..c2c15f07f062 100644
--- a/tests/qtest/fuzz/virtio_net_fuzz.c
+++ b/tests/qtest/fuzz/virtio_net_fuzz.c
@@ -151,7 +151,7 @@ static void *virtio_net_test_setup_socket(GString *cmd_line, void *arg)
 {
     int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sockfds);
     g_assert_cmpint(ret, !=, -1);
-    fcntl(sockfds[0], F_SETFL, O_NONBLOCK);
+    g_unix_set_fd_nonblocking(sockfds[0], true, NULL);
     sockfds_initialized = true;
     g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ",
                            sockfds[1]);
diff --git a/tests/unit/test-iov.c b/tests/unit/test-iov.c
index 0d2ba9ba87d9..93bda00f0e10 100644
--- a/tests/unit/test-iov.c
+++ b/tests/unit/test-iov.c
@@ -186,7 +186,7 @@ static void test_io(void)
 
        close(sv[0]);
        FD_SET(sv[1], &fds);
-       fcntl(sv[1], F_SETFL, O_RDWR|O_NONBLOCK);
+       g_unix_set_fd_nonblocking(sv[1], true, NULL);
        r = g_test_rand_int_range(sz / 2, sz);
        setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &r, sizeof(r));
 
@@ -220,7 +220,7 @@ static void test_io(void)
 
        close(sv[1]);
        FD_SET(sv[0], &fds);
-       fcntl(sv[0], F_SETFL, O_RDWR|O_NONBLOCK);
+       g_unix_set_fd_nonblocking(sv[0], true, NULL);
        r = g_test_rand_int_range(sz / 2, sz);
        setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &r, sizeof(r));
        usleep(500000);
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 1c231087408f..d75dfad6b625 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -226,24 +226,12 @@ void qemu_anon_ram_free(void *ptr, size_t size)
 
 void qemu_set_block(int fd)
 {
-    int f;
-    f = fcntl(fd, F_GETFL);
-    assert(f != -1);
-    f = fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
-    assert(f != -1);
+    g_unix_set_fd_nonblocking(fd, false, NULL);
 }
 
 int qemu_try_set_nonblock(int fd)
 {
-    int f;
-    f = fcntl(fd, F_GETFL);
-    if (f == -1) {
-        return -errno;
-    }
-    if (fcntl(fd, F_SETFL, f | O_NONBLOCK) == -1) {
-        return -errno;
-    }
-    return 0;
+    return g_unix_set_fd_nonblocking(fd, true, NULL) ? 0 : -1;
 }
 
 void qemu_set_nonblock(int fd)
-- 
2.36.0


Re: [PATCH v2 17/26] Replace fcntl(0_NONBLOCK) with g_unix_set_fd_nonblocking()
Posted by Richard Henderson 3 years, 5 months ago
On 4/26/22 02:27, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau<marcandre.lureau@redhat.com>
> 
> Suggested-by: Daniel P. Berrangé<berrange@redhat.com>
> Signed-off-by: Marc-André Lureau<marcandre.lureau@redhat.com>
> ---
>   net/tap-bsd.c                      |  4 ++--
>   net/tap-linux.c                    |  2 +-
>   net/tap-solaris.c                  |  2 +-
>   tests/qtest/fuzz/virtio_net_fuzz.c |  2 +-
>   tests/unit/test-iov.c              |  4 ++--
>   util/oslib-posix.c                 | 16 ++--------------
>   6 files changed, 9 insertions(+), 21 deletions(-)

Typo in subject: s/0/O/.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~

Re: [PATCH v2 17/26] Replace fcntl(0_NONBLOCK) with g_unix_set_fd_nonblocking()
Posted by Marc-André Lureau 3 years, 5 months ago
Hi

On Wed, Apr 27, 2022 at 5:15 AM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 4/26/22 02:27, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau<marcandre.lureau@redhat.com>
> >
> > Suggested-by: Daniel P. Berrangé<berrange@redhat.com>
> > Signed-off-by: Marc-André Lureau<marcandre.lureau@redhat.com>
> > ---
> >   net/tap-bsd.c                      |  4 ++--
> >   net/tap-linux.c                    |  2 +-
> >   net/tap-solaris.c                  |  2 +-
> >   tests/qtest/fuzz/virtio_net_fuzz.c |  2 +-
> >   tests/unit/test-iov.c              |  4 ++--
> >   util/oslib-posix.c                 | 16 ++--------------
> >   6 files changed, 9 insertions(+), 21 deletions(-)
>
> Typo in subject: s/0/O/.
>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>
> r~
>
>
Thanks, additionally I fixed the return value to be -errno on
qemu_try_set_nonblock() error.


-- 
Marc-André Lureau