From patchwork Tue Nov 30 03:38:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 14863 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 7A0D8C3250 for ; Tue, 30 Nov 2021 03:39:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E876B606FD; Tue, 30 Nov 2021 04:39:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="s+F9Ximf"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 60319605C2 for ; Tue, 30 Nov 2021 04:38:51 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id ECEC711C5 for ; Tue, 30 Nov 2021 04:38:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1638243531; bh=6VQC/oNlDxzmvYqn6LpHYeUHO8aITJVndH01oUYvOAM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=s+F9Ximf9tCdS7PI8HCrZiAf80zlVL11SFlx/rnbMVyOx8oigGVH1ELuRtlyV3qox UwKT/yAWGrySlnswXN8j3lQdc/G/eG2VVVvwL3ig2m+Els7RbWx3AjOEOzwaOyuyaZ 9xoT4K0LYa+YGNyt6ayKqDN6xcGE4hrM5CNe3RZo= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 30 Nov 2021 05:38:05 +0200 Message-Id: <20211130033820.18235-8-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211130033820.18235-1-laurent.pinchart@ideasonboard.com> References: <20211130033820.18235-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 07/22] libcamera: base: file_descriptor: Return UniqueFD from dup() 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 Reviewed-by: Hirokazu Honda Reviewed-by: Jacopo Mondi --- include/libcamera/base/file_descriptor.h | 2 +- src/libcamera/base/file_descriptor.cpp | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/libcamera/base/file_descriptor.h b/include/libcamera/base/file_descriptor.h index 74292eba04f5..12a43f95d414 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 da696b2501cd..a83bf52c31e6 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()); + int dupFd = ::dup(fd()); + if (dupFd == -1) { + int ret = -errno; + LOG(FileDescriptor, Error) + << "Failed to dup() fd: " << strerror(-ret); + } + + return UniqueFD(dupFd); } FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate)