treewide: use qemu_set_blocking instead of g_unix_set_fd_nonblocking

Instead of open-coded g_unix_set_fd_nonblocking() calls, use
QEMU wrapper qemu_set_blocking().

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
[DB: fix missing closing ) in tap-bsd.c, remove now unused GError var]
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy
2025-09-16 16:13:59 +03:00
committed by Daniel P. Berrangé
parent 5d1d32ce9d
commit 6f607941b1
21 changed files with 67 additions and 55 deletions

View File

@@ -212,8 +212,8 @@ void qemu_chr_open_fd(Chardev *chr,
FDChardev *s = FD_CHARDEV(chr);
g_autofree char *name = NULL;
if (fd_out >= 0 && !g_unix_set_fd_nonblocking(fd_out, true, NULL)) {
assert(!"Failed to set FD nonblocking");
if (fd_out >= 0) {
qemu_set_blocking(fd_out, false, &error_abort);
}
if (fd_out == fd_in && fd_in >= 0) {

View File

@@ -349,8 +349,7 @@ static void char_pty_open(Chardev *chr,
}
close(slave_fd);
if (!g_unix_set_fd_nonblocking(master_fd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(master_fd, false, errp)) {
return;
}

View File

@@ -271,8 +271,7 @@ static void qmp_chardev_open_serial(Chardev *chr,
if (fd < 0) {
return;
}
if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(fd, false, errp)) {
return;
}
tty_serial_init(fd, 115200, 'N', 8, 1);

View File

@@ -107,8 +107,7 @@ static void qemu_chr_open_stdio(Chardev *chr,
old_fd0_flags = fcntl(0, F_GETFL);
old_fd1_flags = fcntl(1, F_GETFL);
tcgetattr(0, &oldtty);
if (!g_unix_set_fd_nonblocking(0, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(0, false, errp)) {
return;
}
atexit(term_exit);

View File

@@ -114,8 +114,7 @@ static void virtio_input_host_realize(DeviceState *dev, Error **errp)
error_setg_file_open(errp, errno, vih->evdev);
return;
}
if (!g_unix_set_fd_nonblocking(vih->fd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(vih->fd, false, errp)) {
goto err_close;
}

View File

@@ -154,7 +154,8 @@ static void ivshmem_flat_add_vector(IvshmemFTState *s, IvshmemPeer *peer,
* peer.
*/
peer->vector[peer->vector_counter].id = peer->vector_counter;
g_unix_set_fd_nonblocking(vector_fd, true, NULL);
/* WARNING: qemu_socket_set_nonblock() return code ignored */
qemu_set_blocking(vector_fd, false, &error_warn);
event_notifier_init_fd(&peer->vector[peer->vector_counter].event_notifier,
vector_fd);

View File

@@ -540,7 +540,12 @@ static void process_msg_connect(IVShmemState *s, uint16_t posn, int fd,
IVSHMEM_DPRINTF("eventfds[%d][%d] = %d\n", posn, vector, fd);
event_notifier_init_fd(&peer->eventfds[vector], fd);
g_unix_set_fd_nonblocking(fd, true, NULL); /* msix/irqfd poll non block */
/* msix/irqfd poll non block */
if (!qemu_set_blocking(fd, false, errp)) {
close(fd);
return;
}
if (posn == s->vm_id) {
setup_interrupt(s, vector, errp);

View File

@@ -147,9 +147,7 @@ static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
return;
}
if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
error_setg_errno(errp, errno,
"vhost-vsock: unable to set non-blocking mode");
if (!qemu_set_blocking(vhostfd, false, errp)) {
return;
}
} else {
@@ -160,9 +158,7 @@ static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
return;
}
if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
error_setg_errno(errp, errno,
"Failed to set FD nonblocking");
if (!qemu_set_blocking(vhostfd, false, errp)) {
return;
}
}

View File

@@ -277,9 +277,12 @@ static int qio_channel_command_set_blocking(QIOChannel *ioc,
cioc->blocking = enabled;
#else
if ((cioc->writefd >= 0 && !g_unix_set_fd_nonblocking(cioc->writefd, !enabled, NULL)) ||
(cioc->readfd >= 0 && !g_unix_set_fd_nonblocking(cioc->readfd, !enabled, NULL))) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (cioc->writefd >= 0 &&
!qemu_set_blocking(cioc->writefd, enabled, errp)) {
return -1;
}
if (cioc->readfd >= 0 &&
!qemu_set_blocking(cioc->readfd, enabled, errp)) {
return -1;
}
#endif

View File

@@ -223,8 +223,7 @@ static int qio_channel_file_set_blocking(QIOChannel *ioc,
#else
QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(fioc->fd, enabled, errp)) {
return -1;
}
return 0;

View File

@@ -98,7 +98,12 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
return -1;
}
}
g_unix_set_fd_nonblocking(fd, true, NULL);
if (!qemu_set_blocking(fd, false, errp)) {
close(fd);
return -1;
}
return fd;
}
@@ -189,7 +194,10 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
goto error;
}
g_unix_set_fd_nonblocking(fd, true, NULL);
if (!qemu_set_blocking(fd, false, errp)) {
goto error;
}
return fd;
error:

View File

@@ -124,7 +124,12 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
return -1;
}
pstrcpy(ifname, ifname_size, ifr.ifr_name);
g_unix_set_fd_nonblocking(fd, true, NULL);
if (!qemu_set_blocking(fd, false, errp)) {
close(fd);
return -1;
}
return fd;
}

View File

@@ -198,7 +198,12 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
return -1;
}
}
g_unix_set_fd_nonblocking(fd, true, NULL);
if (!qemu_set_blocking(fd, false, errp)) {
close(fd);
return -1;
}
return fd;
}

View File

@@ -627,8 +627,7 @@ int net_init_bridge(const Netdev *netdev, const char *name,
return -1;
}
if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(fd, false, errp)) {
return -1;
}
vnet_hdr = tap_probe_vnet_hdr(fd, errp);
@@ -729,9 +728,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
error_propagate(errp, err);
goto failed;
}
if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
name, fd);
if (!qemu_set_blocking(vhostfd, false, errp)) {
goto failed;
}
} else {
@@ -741,8 +738,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
"tap: open vhost char device failed");
goto failed;
}
if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(vhostfd, false, errp)) {
goto failed;
}
}
@@ -839,9 +835,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
return -1;
}
if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
name, fd);
if (!qemu_set_blocking(fd, false, errp)) {
close(fd);
return -1;
}
@@ -895,10 +889,8 @@ int net_init_tap(const Netdev *netdev, const char *name,
goto free_fail;
}
if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
if (!qemu_set_blocking(fd, false, errp)) {
ret = -1;
error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
name, fd);
goto free_fail;
}
@@ -951,8 +943,7 @@ free_fail:
return -1;
}
if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(fd, false, errp)) {
return -1;
}
vnet_hdr = tap_probe_vnet_hdr(fd, errp);

View File

@@ -503,9 +503,8 @@ int64_t qmp_guest_file_open(const char *path, const char *mode,
/* set fd non-blocking to avoid common use cases (like reading from a
* named pipe) from hanging the agent
*/
if (!g_unix_set_fd_nonblocking(fileno(fh), true, NULL)) {
if (!qemu_set_blocking(fileno(fh), false, errp)) {
fclose(fh);
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
return -1;
}

View File

@@ -132,7 +132,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);
g_unix_set_fd_nonblocking(sockfds[0], true, NULL);
qemu_set_blocking(sockfds[0], false, &error_abort);
sockfds_initialized = true;
g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ",
sockfds[1]);

View File

@@ -330,7 +330,6 @@ static int chr_can_read(void *opaque)
static void chr_read(void *opaque, const uint8_t *buf, int size)
{
g_autoptr(GError) err = NULL;
TestServer *s = opaque;
CharBackend *chr = &s->chr;
VhostUserMsg msg;
@@ -471,8 +470,7 @@ static void chr_read(void *opaque, const uint8_t *buf, int size)
* The receive function forces it to be blocking,
* so revert it back to non-blocking.
*/
g_unix_set_fd_nonblocking(fd, true, &err);
g_assert_no_error(err);
qemu_set_blocking(fd, false, &error_abort);
break;
case VHOST_USER_SET_LOG_BASE:

View File

@@ -1,4 +1,5 @@
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/iov.h"
#include "qemu/sockets.h"
@@ -186,7 +187,7 @@ static void test_io(void)
close(sv[0]);
FD_SET(sv[1], &fds);
g_unix_set_fd_nonblocking(sv[1], true, NULL);
qemu_set_blocking(sv[1], false, &error_abort);
r = g_test_rand_int_range(sz / 2, sz);
setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &r, sizeof(r));
@@ -222,7 +223,7 @@ static void test_io(void)
close(sv[1]);
FD_SET(sv[0], &fds);
g_unix_set_fd_nonblocking(sv[0], true, NULL);
qemu_set_blocking(sv[0], false, &error_abort);
r = g_test_rand_int_range(sz / 2, sz);
setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &r, sizeof(r));
usleep(500000);

View File

@@ -316,8 +316,7 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
error_setg_file_open(errp, errno, il->evdev);
return;
}
if (!g_unix_set_fd_nonblocking(il->fd, true, NULL)) {
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
if (!qemu_set_blocking(il->fd, false, errp)) {
return;
}

View File

@@ -11,6 +11,7 @@
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/cutils.h"
#include "qemu/event_notifier.h"
#include "qemu/main-loop.h"
@@ -36,6 +37,7 @@ int event_notifier_init(EventNotifier *e, int active)
{
int fds[2];
int ret;
Error *local_err = NULL;
#ifdef CONFIG_EVENTFD
ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
@@ -52,11 +54,11 @@ int event_notifier_init(EventNotifier *e, int active)
if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
return -errno;
}
if (!g_unix_set_fd_nonblocking(fds[0], true, NULL)) {
if (!qemu_set_blocking(fds[0], false, &local_err)) {
ret = -errno;
goto fail;
}
if (!g_unix_set_fd_nonblocking(fds[1], true, NULL)) {
if (!qemu_set_blocking(fds[1], false, &local_err)) {
ret = -errno;
goto fail;
}
@@ -70,6 +72,7 @@ int event_notifier_init(EventNotifier *e, int active)
return 0;
fail:
error_report_err(local_err);
close(fds[0]);
close(fds[1]);
return ret;

View File

@@ -114,7 +114,10 @@ static int qemu_signal_init(Error **errp)
return -errno;
}
g_unix_set_fd_nonblocking(sigfd, true, NULL);
if (!qemu_set_blocking(sigfd, false, errp)) {
close(sigfd);
return -EINVAL;
}
qemu_set_fd_handler(sigfd, sigfd_handler, NULL, (void *)(intptr_t)sigfd);