From patchwork Tue Dec 17 01:51:04 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: 2432 X-Patchwork-Delegate: niklas.soderlund@ragnatech.se Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1B484601ED for ; Tue, 17 Dec 2019 02:51:17 +0100 (CET) X-Halon-ID: b6548959-206f-11ea-a00b-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac5865.dip0.t-ipconnect.de [84.172.88.101]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id b6548959-206f-11ea-a00b-005056917a89; Tue, 17 Dec 2019 02:51:16 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Tue, 17 Dec 2019 02:51:04 +0100 Message-Id: <20191217015106.1175515-2-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191217015106.1175515-1-niklas.soderlund@ragnatech.se> References: <20191217015106.1175515-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/3] libcamera: utils: Add exchange() 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, 17 Dec 2019 01:51:17 -0000 C++11 does not support std::exchange(), add a custom implementation in utils. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/libcamera/include/utils.h | 9 +++++++++ src/libcamera/utils.cpp | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index a80f7d096bf725a1..32e3b009040d9efc 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -66,6 +66,15 @@ const T& clamp(const T& v, const T& lo, const T& hi) return std::max(lo, std::min(v, hi)); } +/* C++11 doesn't provide std::exchange */ +template +T exchange(T &obj, U &&new_value) +{ + T old_value = std::move(obj); + obj = std::forward(new_value); + return old_value; +} + using clock = std::chrono::steady_clock; using duration = std::chrono::steady_clock::duration; using time_point = std::chrono::steady_clock::time_point; diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index d632f6e66638038d..6ebb4e8ce39010c0 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -95,6 +95,14 @@ char *secure_getenv(const char *name) * \return lo if v is less than lo, hi if v is greater than hi, otherwise v */ +/** + * \fn libcamera::utils::exchange(T &obj, U &&new_value) + * \brief Replace the value of \a obj with \a new_value and return the old value + * \param[inout] obj Object whose value to replace + * \param[in] new_value The value to assign to obj + * \return The old value of \a obj + */ + /** * \typedef clock * \brief The libcamera clock (monotonic) From patchwork Tue Dec 17 01:51:05 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: 2433 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EBAAF6047B for ; Tue, 17 Dec 2019 02:51:17 +0100 (CET) X-Halon-ID: b6aaace7-206f-11ea-a00b-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac5865.dip0.t-ipconnect.de [84.172.88.101]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id b6aaace7-206f-11ea-a00b-005056917a89; Tue, 17 Dec 2019 02:51:16 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Tue, 17 Dec 2019 02:51:05 +0100 Message-Id: <20191217015106.1175515-3-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191217015106.1175515-1-niklas.soderlund@ragnatech.se> References: <20191217015106.1175515-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/3] libcamera: Add FileDescriptor to help pass numerical fds around 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, 17 Dec 2019 01:51:18 -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: Laurent Pinchart --- include/libcamera/file_descriptor.h | 33 ++++++ include/libcamera/meson.build | 1 + src/libcamera/file_descriptor.cpp | 164 ++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 199 insertions(+) create mode 100644 include/libcamera/file_descriptor.h create mode 100644 src/libcamera/file_descriptor.cpp diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h new file mode 100644 index 0000000000000000..e05f111d128e0ab1 --- /dev/null +++ b/include/libcamera/file_descriptor.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * file_descriptor.h - File descriptor wrapper + */ +#ifndef __LIBCAMERA_FILE_DESCRIPTOR_H__ +#define __LIBCAMERA_FILE_DESCRIPTOR_H__ + +namespace libcamera { + +class FileDescriptor final +{ +public: + explicit FileDescriptor(int fd = -1); + explicit FileDescriptor(const FileDescriptor &other); + explicit FileDescriptor(FileDescriptor &&other); + ~FileDescriptor(); + + FileDescriptor &operator=(const FileDescriptor &other); + FileDescriptor &operator=(FileDescriptor &&other); + + int fd() const { return fd_; } + +private: + void duplicate(int fd); + + int fd_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_FILE_DESCRIPTOR_H__ */ diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 99abf06099407c1f..543e6773cc5158a0 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -6,6 +6,7 @@ libcamera_api = files([ 'controls.h', 'event_dispatcher.h', 'event_notifier.h', + 'file_descriptor.h', 'geometry.h', 'logging.h', 'object.h', diff --git a/src/libcamera/file_descriptor.cpp b/src/libcamera/file_descriptor.cpp new file mode 100644 index 0000000000000000..71d1cf27be79b288 --- /dev/null +++ b/src/libcamera/file_descriptor.cpp @@ -0,0 +1,164 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * file_descriptor.cpp - File descriptor wrapper + */ + +#include + +#include +#include +#include + +#include "log.h" + +/** + * \file file_descriptor.h + * \brief File descriptor wrapper + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(FileDescriptor) + +/** + * \class FileDescriptor + * \brief RAII-style wrapper for file descriptors + * + * The FileDescriptor class wraps a file descriptor (expressed as a signed + * integer) to provide an RAII-style mechanism for owning the file descriptor. + * When constructed, the FileDescriptor instance duplicates a given file + * descriptor and takes ownership of the duplicate as a resource. The copy + * constructor and assignment operator duplicate the file descriptor, while the + * move versions of those methods move the resource and make the original + * FileDescriptor invalid. When the FileDescriptor is deleted, it closes the + * file descriptor it owns, if any. + */ + +/** + * \brief Create a FileDescriptor wrapping a copy of a given \a fd + * \param[in] fd File descriptor + * + * Construct a FileDescriptor from a numerical file descriptor duplicates the + * \a fd and takes ownership of the copy. The original \a fd is left untouched, + * and the caller is responsible for closing it when appropriate. The duplicated + * file descriptor will be closed automatically when the FileDescriptor instance + * is destroyed. + */ +FileDescriptor::FileDescriptor(int fd) + : fd_(-1) +{ + duplicate(fd); +} + +/** + * \brief Copy constructor, create a FileDescriptor from a copy of \a other + * \param[in] other The other FileDescriptor + * + * Construct a FileDescriptor from another FileDescriptor duplicates the + * wrapped numerical file descriptor and takes ownership of the copy. The + * original FileDescriptor is left untouched, and the caller is responsible for + * closing it when appropriate. The duplicated file descriptor will be closed + * automatically when this FileDescriptor instance is destroyed. + */ +FileDescriptor::FileDescriptor(const FileDescriptor &other) + : FileDescriptor(other.fd_) +{ +} + +/** + * \brief Move constructor, create a FileDescriptor by taking over \a other + * \param[in] other The other FileDescriptor + * + * Construct a FileDescriptor by taking over a wrapped numerical file + * descriptor and taking ownership of it. The \a other FileDescriptor is + * invalidated and set to -1. The taken over file descriptor will be closed + * automatically when this FileDescriptor instance is destroyed. + */ +FileDescriptor::FileDescriptor(FileDescriptor &&other) + : fd_(utils::exchange(other.fd_, -1)) +{ +} + +/** + * \brief Destroy the managed file descriptor + * + * If the managed file descriptor, as returned by fd(), is not equal to -1, the + * file descriptor is closed. + */ +FileDescriptor::~FileDescriptor() +{ + if (fd_ != -1) + close(fd_); +} + +/** + * \brief Copy assignment operator, replace the wrapped file descriptor with a + * duplicate from \a other + * \param[in] other The other FileDescriptor + * + * Close the wrapped file descriptor (if any) and duplicate the file descriptor + * from \a other. The \a other FileDescriptor is left untouched, and the caller + * is responsible for destroying it when appropriate. The duplicated file + * descriptor will be closed automatically when the FileDescriptor instance is + * destroyed. + * + * \return A reference to this FileDescriptor + */ +FileDescriptor &FileDescriptor::operator=(const FileDescriptor &other) +{ + duplicate(other.fd_); + + return *this; +} + +/** + * \brief Move assignment operator, replace the wrapped file descriptor by + * taking over \a other + * \param[in] other The other FileDescriptor + * + * Close the wrapped file descriptor (if any) and take over the file descriptor + * from \a other. The \a other FileDescriptor is invalidated and set to -1. The + * taken over file descriptor will be closed automatically when the + * FileDescriptor instance is destroyed. + * + * \return A reference to this FileDescriptor + */ +FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other) +{ + if (fd_ != -1) + close(fd_); + + fd_ = utils::exchange(other.fd_, -1); + + return *this; +} + +/** + * \fn FileDescriptor::fd() + * \brief Retrieve the numerical file descriptor + * \return The numerical file descriptor, which may be -1 if the FileDescriptor + * instance doesn't own a file descriptor + */ + +void FileDescriptor::duplicate(int fd) +{ + if (fd_ != -1) + close(fd_); + + if (fd < 0) { + fd_ = -1; + return; + } + + /* Failing to dup() a fd should not happen and is fatal. */ + fd_ = dup(fd); + if (fd_ == -1) { + int ret = -errno; + LOG(FileDescriptor, Fatal) + << "Failed to dup() fd: " << strerror(-ret); + } +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index c4f965bd7413b37e..722c5bc15afe52ef 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -14,6 +14,7 @@ libcamera_sources = files([ 'event_dispatcher.cpp', 'event_dispatcher_poll.cpp', 'event_notifier.cpp', + 'file_descriptor.cpp', 'formats.cpp', 'geometry.cpp', 'ipa_context_wrapper.cpp', From patchwork Tue Dec 17 01:51:06 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: 2434 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 98B4C6047E for ; Tue, 17 Dec 2019 02:51:18 +0100 (CET) X-Halon-ID: b72fedea-206f-11ea-a00b-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac5865.dip0.t-ipconnect.de [84.172.88.101]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id b72fedea-206f-11ea-a00b-005056917a89; Tue, 17 Dec 2019 02:51:17 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Tue, 17 Dec 2019 02:51:06 +0100 Message-Id: <20191217015106.1175515-4-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191217015106.1175515-1-niklas.soderlund@ragnatech.se> References: <20191217015106.1175515-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/3] test: file_descriptor: Add test 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, 17 Dec 2019 01:51:19 -0000 Add a test which exercises the whole FileDescriptor interface. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- test/file_descriptor.cpp | 165 +++++++++++++++++++++++++++++++++++++++ test/meson.build | 1 + 2 files changed, 166 insertions(+) create mode 100644 test/file_descriptor.cpp diff --git a/test/file_descriptor.cpp b/test/file_descriptor.cpp new file mode 100644 index 0000000000000000..177d8bcf16591185 --- /dev/null +++ b/test/file_descriptor.cpp @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * file_descriptor.cpp - FileDescriptor test + */ + +#include +#include +#include +#include + +#include + +#include "test.h" + +using namespace libcamera; +using namespace std; + +class FileDescriptorTest : public Test +{ +protected: + int init() + { + desc1_ = nullptr; + desc2_ = nullptr; + + fd_ = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); + if (fd_ < 0) + return TestFail; + + /* Cache inode number of temp file. */ + struct stat s; + if (fstat(fd_, &s)) + return TestFail; + + inodeNr_ = s.st_ino; + + return 0; + } + + int run() + { + /* Test creating FileDescriptor from numerical file descriptor. */ + desc1_ = new FileDescriptor(fd_); + if (desc1_->fd() == fd_) + return TestFail; + + if (!isValidFd(fd_) || !isValidFd(desc1_->fd())) + return TestFail; + + int fd = desc1_->fd(); + + delete desc1_; + desc1_ = nullptr; + + if (!isValidFd(fd_) || isValidFd(fd)) + return TestFail; + + /* Test creating FileDescriptor from other FileDescriptor. */ + desc1_ = new FileDescriptor(fd_); + desc2_ = new FileDescriptor(*desc1_); + + if (desc1_->fd() == fd_ || desc2_->fd() == fd_ || desc1_->fd() == desc2_->fd()) + return TestFail; + + if (!isValidFd(desc1_->fd()) || !isValidFd(desc2_->fd())) + return TestFail; + + delete desc1_; + desc1_ = nullptr; + + if (!isValidFd(desc2_->fd())) + return TestFail; + + delete desc2_; + desc2_ = nullptr; + + /* Test creating FileDescriptor by taking over other FileDescriptor. */ + desc1_ = new FileDescriptor(fd_); + fd = desc1_->fd(); + desc2_ = new FileDescriptor(std::move(*desc1_)); + + if (desc1_->fd() != -1 || desc2_->fd() != fd) + return TestFail; + + if (!isValidFd(desc2_->fd())) + return TestFail; + + delete desc1_; + desc1_ = nullptr; + delete desc2_; + desc2_ = nullptr; + + /* Test creating FileDescriptor by copy assignment. */ + desc1_ = new FileDescriptor(); + desc2_ = new FileDescriptor(fd_); + + if (desc1_->fd() != -1) + return TestFail; + + fd = desc2_->fd(); + *desc1_ = *desc2_; + + if (desc1_->fd() == fd || desc2_->fd() != fd) + return TestFail; + + if (!isValidFd(desc1_->fd()) || !isValidFd(desc2_->fd())) + return TestFail; + + delete desc1_; + desc1_ = nullptr; + delete desc2_; + desc2_ = nullptr; + + if (!isValidFd(fd_)) + return TestFail; + + /* Test creating FileDescriptor by move assignment. */ + desc1_ = new FileDescriptor(); + desc2_ = new FileDescriptor(fd_); + + fd = desc2_->fd(); + *desc1_ = std::move(*desc2_); + + if (desc1_->fd() != fd || desc2_->fd() != -1) + return TestFail; + + if (!isValidFd(desc1_->fd())) + return TestFail; + + delete desc1_; + desc1_ = nullptr; + delete desc2_; + desc2_ = nullptr; + + return TestPass; + } + + void cleanup() + { + delete desc2_; + delete desc1_; + + if (fd_ > 0) + close(fd_); + } + +private: + bool isValidFd(int fd) + { + struct stat s; + if (fstat(fd, &s)) + return false; + + /* Check that inode number matches cached temp file. */ + return s.st_ino == inodeNr_; + } + + int fd_; + ino_t inodeNr_; + FileDescriptor *desc1_, *desc2_; +}; + +TEST_REGISTER(FileDescriptorTest) diff --git a/test/meson.build b/test/meson.build index 1bb2161dc05a7b1d..9dc392df30efd956 100644 --- a/test/meson.build +++ b/test/meson.build @@ -25,6 +25,7 @@ internal_tests = [ ['event', 'event.cpp'], ['event-dispatcher', 'event-dispatcher.cpp'], ['event-thread', 'event-thread.cpp'], + ['file_descriptor', 'file_descriptor.cpp'], ['message', 'message.cpp'], ['object', 'object.cpp'], ['object-invoke', 'object-invoke.cpp'],