summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-11-28 19:50:43 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-12-03 19:20:47 +0200
commitfcf98514cb4eb603a05329deec508fe790ba1c17 (patch)
tree74bb2ab6ac1f961bae91c6428b9e615f1555c96c
parent1d0dbc0da83968d9b2f21b9552151ca28fd392e5 (diff)
libcamera: base: file_descriptor: Return UniqueFD from dup()
The dup() function returns a duplicate of the file descriptor. Wrapping it in a FileDescriptor isn't wrong as such, but it prevents from using it in contexts where a UniqueFD is needed. As the duplicate is guaranteed to have a single owner when created, return it as a UniqueFD instead. A FileDescriptor can easily be created from the UniqueFD if desired. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Hirokazu Honda <hiroh@chromium.org> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
-rw-r--r--include/libcamera/base/file_descriptor.h2
-rw-r--r--src/libcamera/base/file_descriptor.cpp22
2 files changed, 15 insertions, 9 deletions
diff --git a/include/libcamera/base/file_descriptor.h b/include/libcamera/base/file_descriptor.h
index 74292eba..12a43f95 100644
--- a/include/libcamera/base/file_descriptor.h
+++ b/include/libcamera/base/file_descriptor.h
@@ -28,7 +28,7 @@ public:
bool isValid() const { return fd_ != nullptr; }
int fd() const { return fd_ ? fd_->fd() : -1; }
- FileDescriptor dup() const;
+ UniqueFD dup() const;
private:
class Descriptor
diff --git a/src/libcamera/base/file_descriptor.cpp b/src/libcamera/base/file_descriptor.cpp
index da696b25..8e181abc 100644
--- a/src/libcamera/base/file_descriptor.cpp
+++ b/src/libcamera/base/file_descriptor.cpp
@@ -222,17 +222,23 @@ FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other)
* \brief Duplicate a FileDescriptor
*
* Duplicating a FileDescriptor creates a duplicate of the wrapped file
- * descriptor and returns a new FileDescriptor instance that wraps the
- * duplicate. The fd() function of the original and duplicate instances will
- * return different values. The duplicate instance will not be affected by
- * destruction of the original instance or its copies.
+ * descriptor and returns a UniqueFD that owns the duplicate. The fd() function
+ * of the original and the get() function of the duplicate will return different
+ * values. The duplicate instance will not be affected by destruction of the
+ * original instance or its copies.
*
- * \return A new FileDescriptor instance wrapping a duplicate of the original
- * file descriptor
+ * \return A UniqueFD owning a duplicate of the original file descriptor
*/
-FileDescriptor FileDescriptor::dup() const
+UniqueFD FileDescriptor::dup() const
{
- return FileDescriptor(fd());
+ UniqueFD dupFd(::dup(fd()));
+ if (!dupFd.isValid()) {
+ int ret = -errno;
+ LOG(FileDescriptor, Error)
+ << "Failed to dup() fd: " << strerror(-ret);
+ }
+
+ return dupFd;
}
FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate)