From patchwork Tue Nov 26 23:35:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2352 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 27AC860C3D for ; Wed, 27 Nov 2019 00:39:28 +0100 (CET) X-Halon-ID: fb9c075c-10a5-11ea-a0b9-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac5865.dip0.t-ipconnect.de [84.172.88.101]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id fb9c075c-10a5-11ea-a0b9-005056917f90; Wed, 27 Nov 2019 00:39:26 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Wed, 27 Nov 2019 00:35:54 +0100 Message-Id: <20191126233620.1695316-5-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191126233620.1695316-1-niklas.soderlund@ragnatech.se> References: <20191126233620.1695316-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 04/30] libcamera: buffer: Add FileDecriptor to help deal with file descriptors X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Nov 2019 23:39:29 -0000 Add a helper to make it easier to pass file descriptors around. The helper class duplicates the fd which decouples it from the original fd which could be closed by its owner while the new FileDescriptor remains valid. Signed-off-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- include/libcamera/buffer.h | 12 ++++++++++++ src/libcamera/buffer.cpp | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index 7302b9f32ce8c50d..33793b4ccf881eda 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -46,6 +46,18 @@ private: std::vector planes_; }; +class FileDescriptor final +{ +public: + FileDescriptor(int fd); + ~FileDescriptor(); + + int fd() const { return fd_; } + +private: + int fd_; +}; + class Plane final { public: diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 4acff216cafba484..0676586ae3be2a61 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -115,6 +115,46 @@ void BufferInfo::update(Status status, unsigned int sequence, * \return A array holding all plane information for the buffer */ +/** + * \class FileDescriptor + * \brief Life time management of a file descriptor + * + * The managed file descriptor (fd) is duplicated from the original one and + * is opened for the life time of the FileDescriptor object disregarding + * if the original one is closed. + */ + +/** + * \brief Create a life time managed file descriptor + * \param[in] fd File descriptor + */ +FileDescriptor::FileDescriptor(int fd) + : fd_(-1) +{ + if (fd < 0) + return; + + /* Failing to dup() a fd should not happen and is fatal. */ + fd_ = dup(fd); + if (fd_ == -1) { + int ret = -errno; + LOG(Buffer, Fatal) + << "Failed to dup() fd: " << strerror(-ret); + } +} + +FileDescriptor::~FileDescriptor() +{ + if (fd_ != -1) + close(fd_); +} + +/** + * \fn FileDescriptor::fd() + * \brief Retrieve the file descriptor or + * \return A valid file descriptor or a negative error code + */ + /** * \class Plane * \brief A memory region to store a single plane of a frame