From patchwork Tue Nov 30 10:27:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 14883 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 A0E2ABF415 for ; Tue, 30 Nov 2021 10:28:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5783E605B4; Tue, 30 Nov 2021 11:28:05 +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="d65vqPik"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D1A9160230 for ; Tue, 30 Nov 2021 11:28:03 +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 6F7732A5 for ; Tue, 30 Nov 2021 11:28:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1638268083; bh=YSFQHGBQvSj8ibJm0RrAidBpqucwCNeGQQw1uH3Hscc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=d65vqPik1HZiYxEuesc3WkHlDDrjfKHazKwiOyCls4MLsjg6a3VOj+OEVV93gRi4s c2MLhusngs4LwZunDVN1dEtwo+IBW66FciKwZkynsE8/96xiVYdAX5HeG04pycCae/ PyuEZwBKy6yEimcsXANkHd1WlV3MaffiCZM9H+EU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 30 Nov 2021 12:27:36 +0200 Message-Id: <20211130102736.1768-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211130033820.18235-8-laurent.pinchart@ideasonboard.com> References: <20211130033820.18235-8-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4.1 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 --- Changes since v4: - Store ::dup() return value directly in UniqueFD --- 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..8e181abc44bb 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)