* [PATCH v4] treewide: use qemu_dup() instead of dup()
@ 2026-11-12 16:17 Filip Hejsek
2026-11-12 19:18 ` Eric Blake
0 siblings, 1 reply; 2+ messages in thread
From: Filip Hejsek @ 2026-11-12 16:17 UTC (permalink / raw)
To: qemu-devel
Cc: Michael S. Tsirkin, Daniel P. Berrangé, Paolo Bonzini,
Marc-André Lureau, Michael Tokarev, Laurent Vivier,
Eric Blake, Vladimir Sementsov-Ogievskiy, qemu-block,
Maciej S. Szmigiero, Peter Xu, Fabiano Rosas, Mark Kanda,
Ben Chaney, Igor Mammedov, Ani Sinha
Move qemu_dup() out of an #ifndef so it can be called from code that
runs on Windows. Then use it instead of dup() to ensure the cloexec
flag is set on dup'd fds.
The only direct users of dup() are now guest syscalls in the user
emulators and the implementation of qemu_dup() itself.
Signed-off-by: Filip Hejsek <filip.hejsek@gmail.com>
---
I fixed the Windows build now, checked with docker-test-mingw. I had
to remove ENABLE_RUST=1 in fedora-win64-cross.docker, because Rust
cross-compilation was broken.
Changes in v4:
- Fix Windows build
- Link to v3: https://lore.kernel.org/qemu-devel/20261015-use-qemu-dup-v3-1-ca02b5927725@gmail.com
Changes in v3:
- Also replace the use in migration/cpr-exec.c and the tests
- Link to v2: https://lore.kernel.org/qemu-devel/20261013-use-qemu-dup-v2-1-12ecd94263b7@gmail.com
Changes in v2:
- Replace the use in qemu-nbd.c too
- Changed commit message
- Link to v1: https://lore.kernel.org/qemu-devel/20261012-use-qemu-dup-v1-1-03d94852a6c8@gmail.com
---
hw/virtio/virtio.c | 2 +-
include/qemu/osdep.h | 2 +-
io/channel-file.c | 2 +-
migration/cpr-exec.c | 2 +-
qemu-nbd.c | 2 +-
system/vl.c | 9 +--------
tests/qtest/bios-tables-test.c | 2 +-
tests/unit/test-util-sockets.c | 6 +++---
tests/unit/test-vmstate.c | 2 +-
ui/dmabuf.c | 2 +-
util/osdep.c | 28 ++++++++++++++--------------
11 files changed, 26 insertions(+), 33 deletions(-)
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index c9f9e5b511..70779fbd56 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -3204,7 +3204,7 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
return NULL;
}
- fd = dup(fd);
+ fd = qemu_dup(fd);
if (fd < 0) {
error_report("Failed to duplicate fd: %s", strerror(errno));
return NULL;
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index b27d159d5b..b5dbfc8a8a 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -653,9 +653,9 @@ int qemu_open(const char *name, int flags, Error **errp);
int qemu_create(const char *name, int flags, mode_t mode, Error **errp);
int qemu_close(int fd);
int qemu_unlink(const char *name);
+int qemu_dup(int fd);
#ifndef _WIN32
int qemu_dup_flags(int fd, int flags);
-int qemu_dup(int fd);
int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
int qemu_unlock_fd(int fd, int64_t start, int64_t len);
int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
diff --git a/io/channel-file.c b/io/channel-file.c
index 4c9da6619b..548a633be8 100644
--- a/io/channel-file.c
+++ b/io/channel-file.c
@@ -54,7 +54,7 @@ qio_channel_file_new_fd(int fd)
QIOChannelFile *
qio_channel_file_new_dupfd(int fd, Error **errp)
{
- int newfd = dup(fd);
+ int newfd = qemu_dup(fd);
if (newfd < 0) {
error_setg_errno(errp, errno, "Could not dup FD %d", fd);
diff --git a/migration/cpr-exec.c b/migration/cpr-exec.c
index daa50916d2..2f38db30bb 100644
--- a/migration/cpr-exec.c
+++ b/migration/cpr-exec.c
@@ -44,7 +44,7 @@ bool cpr_exec_persist_state(QEMUFile *f, Error **errp)
{
QIOChannelFile *fioc = QIO_CHANNEL_FILE(qemu_file_get_ioc(f));
/* coverity[leaked_storage] - mfd intentionally kept open across exec() */
- int mfd = dup(fioc->fd);
+ int mfd = qemu_dup(fioc->fd);
char val[16];
/* Remember mfd in environment for post-exec load */
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 4f7e965edb..b283a1b0e6 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -1027,7 +1027,7 @@ int main(int argc, char **argv)
/* Remember parent's stderr if we will be restoring it. */
if (opts.verbose /* fork_process is set */) {
- opts.old_stderr = dup(STDERR_FILENO);
+ opts.old_stderr = qemu_dup(STDERR_FILENO);
if (opts.old_stderr < 0) {
error_report("Could not dup original stderr: %s",
strerror(errno));
diff --git a/system/vl.c b/system/vl.c
index e7b759d544..14db77dfee 100644
--- a/system/vl.c
+++ b/system/vl.c
@@ -614,14 +614,7 @@ static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
return -1;
}
-#ifdef F_DUPFD_CLOEXEC
- dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
-#else
- dupfd = dup(fd);
- if (dupfd != -1) {
- qemu_set_cloexec(dupfd);
- }
-#endif
+ dupfd = qemu_dup(fd);
if (dupfd == -1) {
error_setg_errno(errp, errno, "error duplicating fd");
return -1;
diff --git a/tests/qtest/bios-tables-test.c b/tests/qtest/bios-tables-test.c
index f6dfa4131b..c4838322fb 100644
--- a/tests/qtest/bios-tables-test.c
+++ b/tests/qtest/bios-tables-test.c
@@ -529,7 +529,7 @@ static void test_acpi_asl(test_data *data)
const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
char *diff = g_strdup_printf("%s %s %s", diff_cmd,
exp_sdt->asl_file, sdt->asl_file);
- int out = dup(STDOUT_FILENO);
+ int out = qemu_dup(STDOUT_FILENO);
int ret G_GNUC_UNUSED;
int dupret;
diff --git a/tests/unit/test-util-sockets.c b/tests/unit/test-util-sockets.c
index 006f5e579c..c47ad6c82d 100644
--- a/tests/unit/test-util-sockets.c
+++ b/tests/unit/test-util-sockets.c
@@ -62,7 +62,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
error_setg(errp, "No fd named %s", fdname);
return -1;
}
- return dup(mon_fd);
+ return qemu_dup(mon_fd);
}
/*
@@ -115,7 +115,7 @@ static void test_socket_fd_pass_name_bad(void)
cur_mon = g_malloc(1); /* Fake a monitor */
mon_fdname = "myfd";
- mon_fd = dup(STDOUT_FILENO);
+ mon_fd = qemu_dup(STDOUT_FILENO);
g_assert_cmpint(mon_fd, >, STDERR_FILENO);
addr.type = SOCKET_ADDRESS_TYPE_FD;
@@ -189,7 +189,7 @@ static void test_socket_fd_pass_num_bad(void)
int fd, sfd;
g_assert(cur_mon == NULL);
- sfd = dup(STDOUT_FILENO);
+ sfd = qemu_dup(STDOUT_FILENO);
addr.type = SOCKET_ADDRESS_TYPE_FD;
addr.u.fd.str = g_strdup_printf("%d", sfd);
diff --git a/tests/unit/test-vmstate.c b/tests/unit/test-vmstate.c
index 23f3173070..57f1e3263f 100644
--- a/tests/unit/test-vmstate.c
+++ b/tests/unit/test-vmstate.c
@@ -42,7 +42,7 @@ static QEMUFile *open_test_file(bool write)
QIOChannel *ioc;
QEMUFile *f;
- fd = dup(temp_fd);
+ fd = qemu_dup(temp_fd);
g_assert(fd >= 0);
lseek(fd, 0, SEEK_SET);
if (write) {
diff --git a/ui/dmabuf.c b/ui/dmabuf.c
index 497f82cc61..6d5dffdf84 100644
--- a/ui/dmabuf.c
+++ b/ui/dmabuf.c
@@ -95,7 +95,7 @@ void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds)
assert(nfds >= dmabuf->num_planes);
for (i = 0; i < dmabuf->num_planes; i++) {
- fds[i] = dmabuf->fd[i] >= 0 ? dup(dmabuf->fd[i]) : -1;
+ fds[i] = dmabuf->fd[i] >= 0 ? qemu_dup(dmabuf->fd[i]) : -1;
}
}
diff --git a/util/osdep.c b/util/osdep.c
index 4a8b8b5a90..efb9dea3f3 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -117,6 +117,20 @@ int qemu_mprotect_none(void *addr, size_t size)
#endif
}
+int qemu_dup(int fd)
+{
+ int ret;
+#ifdef F_DUPFD_CLOEXEC
+ ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);
+#else
+ ret = dup(fd);
+ if (ret != -1) {
+ qemu_set_cloexec(ret);
+ }
+#endif
+ return ret;
+}
+
#ifndef _WIN32
static int fcntl_op_setlk = -1;
@@ -170,20 +184,6 @@ fail:
return -1;
}
-int qemu_dup(int fd)
-{
- int ret;
-#ifdef F_DUPFD_CLOEXEC
- ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);
-#else
- ret = dup(fd);
- if (ret != -1) {
- qemu_set_cloexec(ret);
- }
-#endif
- return ret;
-}
-
static int qemu_parse_fdset(const char *param)
{
return qemu_parse_fd(param);
---
base-commit: 29760efaf96bbe5928f8111881eddbf5ac06439e
change-id: 20261012-use-qemu-dup-38628dbac3ee
Best regards,
--
Filip Hejsek <filip.hejsek@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v4] treewide: use qemu_dup() instead of dup()
2026-11-12 16:17 [PATCH v4] treewide: use qemu_dup() instead of dup() Filip Hejsek
@ 2026-11-12 19:18 ` Eric Blake
0 siblings, 0 replies; 2+ messages in thread
From: Eric Blake @ 2026-11-12 19:18 UTC (permalink / raw)
To: Filip Hejsek
Cc: qemu-devel, Michael S. Tsirkin, Daniel P. Berrangé,
Paolo Bonzini, Marc-André Lureau, Michael Tokarev,
Laurent Vivier, Vladimir Sementsov-Ogievskiy, qemu-block,
Maciej S. Szmigiero, Peter Xu, Fabiano Rosas, Mark Kanda,
Ben Chaney, Igor Mammedov, Ani Sinha
On Thu, Nov 12, 2026 at 05:17:30PM +0100, Filip Hejsek wrote:
> Move qemu_dup() out of an #ifndef so it can be called from code that
> runs on Windows. Then use it instead of dup() to ensure the cloexec
> flag is set on dup'd fds.
>
> The only direct users of dup() are now guest syscalls in the user
> emulators and the implementation of qemu_dup() itself.
>
> Signed-off-by: Filip Hejsek <filip.hejsek@gmail.com>
> ---
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-12 19:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 16:17 [PATCH v4] treewide: use qemu_dup() instead of dup() Filip Hejsek
2026-11-12 19:18 ` Eric Blake
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.