Skip to content

Commit

Permalink
Replace fcntl(O_NONBLOCK) with g_unix_set_fd_nonblocking()
Browse files Browse the repository at this point in the history
Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
  • Loading branch information
elmarco committed May 3, 2022
1 parent d640b59 commit 22e135f
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 21 deletions.
4 changes: 2 additions & 2 deletions net/tap-bsd.c
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion net/tap-linux.c
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion net/tap-solaris.c
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/qtest/fuzz/virtio_net_fuzz.c
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test-iov.c
Expand Up @@ -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));

Expand Down Expand Up @@ -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);
Expand Down
16 changes: 2 additions & 14 deletions util/oslib-posix.c
Expand Up @@ -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 : -errno;
}

void qemu_set_nonblock(int fd)
Expand Down

0 comments on commit 22e135f

Please sign in to comment.