aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabiano Rosas <farosas@suse.de>2024-03-11 20:33:34 -0300
committerPeter Xu <peterx@redhat.com>2024-03-12 15:22:23 -0400
commit4760cedc61328e47bf7f1fabceb9937facfa4cdd (patch)
tree6eef4197a0c83d12afca866005569787b03106f4
parent35ac6831d98e18e2c78c85c93e3a6ca1f1ae3e58 (diff)
downloadqemu-4760cedc61328e47bf7f1fabceb9937facfa4cdd.zip
qemu-4760cedc61328e47bf7f1fabceb9937facfa4cdd.tar.gz
qemu-4760cedc61328e47bf7f1fabceb9937facfa4cdd.tar.bz2
io: Introduce qio_channel_file_new_dupfd
Add a new helper function for creating a QIOChannelFile channel with a duplicated file descriptor. This saves the calling code from having to do error checking on the dup() call. Suggested-by: "Daniel P. Berrangé" <berrange@redhat.com> Signed-off-by: Fabiano Rosas <farosas@suse.de> Reviewed-by: "Daniel P. Berrangé" <berrange@redhat.com> Link: https://lore.kernel.org/r/20240311233335.17299-2-farosas@suse.de Signed-off-by: Peter Xu <peterx@redhat.com>
-rw-r--r--include/io/channel-file.h18
-rw-r--r--io/channel-file.c12
2 files changed, 30 insertions, 0 deletions
diff --git a/include/io/channel-file.h b/include/io/channel-file.h
index 50e8eb1..d373a4e 100644
--- a/include/io/channel-file.h
+++ b/include/io/channel-file.h
@@ -69,6 +69,24 @@ QIOChannelFile *
qio_channel_file_new_fd(int fd);
/**
+ * qio_channel_file_new_dupfd:
+ * @fd: the file descriptor
+ * @errp: pointer to initialized error object
+ *
+ * Create a new IO channel object for a file represented by the @fd
+ * parameter. Like qio_channel_file_new_fd(), but the @fd is first
+ * duplicated with dup().
+ *
+ * The channel will own the duplicated file descriptor and will take
+ * responsibility for closing it, the original FD is owned by the
+ * caller.
+ *
+ * Returns: the new channel object
+ */
+QIOChannelFile *
+qio_channel_file_new_dupfd(int fd, Error **errp);
+
+/**
* qio_channel_file_new_path:
* @path: the file path
* @flags: the open flags (O_RDONLY|O_WRONLY|O_RDWR, etc)
diff --git a/io/channel-file.c b/io/channel-file.c
index a6ad777..6436cfb 100644
--- a/io/channel-file.c
+++ b/io/channel-file.c
@@ -45,6 +45,18 @@ qio_channel_file_new_fd(int fd)
return ioc;
}
+QIOChannelFile *
+qio_channel_file_new_dupfd(int fd, Error **errp)
+{
+ int newfd = dup(fd);
+
+ if (newfd < 0) {
+ error_setg_errno(errp, errno, "Could not dup FD %d", fd);
+ return NULL;
+ }
+
+ return qio_channel_file_new_fd(newfd);
+}
QIOChannelFile *
qio_channel_file_new_path(const char *path,