{"id":14822,"url":"https://patchwork.libcamera.org/api/patches/14822/?format=json","web_url":"https://patchwork.libcamera.org/patch/14822/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211128235752.10836-8-laurent.pinchart@ideasonboard.com>","date":"2021-11-28T23:57:42","name":"[libcamera-devel,v3,07/17] libcamera: base: file_descriptor: Return UniqueFD from dup()","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"a2e03fe144a66141e3fe49977c7aef9d88d18232","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14822/mbox/","series":[{"id":2768,"url":"https://patchwork.libcamera.org/api/series/2768/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2768","date":"2021-11-28T23:57:35","name":"libcamera: Introduce UniqueFD","version":3,"mbox":"https://patchwork.libcamera.org/series/2768/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14822/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14822/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 2D472BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 28 Nov 2021 23:58:32 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 336E6605A6;\n\tMon, 29 Nov 2021 00:58:31 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5B88C6057B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 00:58:24 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EC727730;\n\tMon, 29 Nov 2021 00:58:23 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"mDUSoE4P\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638143904;\n\tbh=6VQC/oNlDxzmvYqn6LpHYeUHO8aITJVndH01oUYvOAM=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=mDUSoE4PHpHJdv5X/hD5mKwkBaEGuHn05hwxJv/4gLt1fxodFD/+a7FLhooVvKZB4\n\t3BaHL5keoadSydOlFMCm46rvif1l9JIa4QsGG8nGL8oL5BKmMTuHqwk0JfDHi9oW+x\n\t/DXqW3aaw2wdUbqU6zxEbMX3/WqXFM2p5hhPcM1w=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 29 Nov 2021 01:57:42 +0200","Message-Id":"<20211128235752.10836-8-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>","References":"<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 07/17] libcamera: base:\n\tfile_descriptor: Return UniqueFD from dup()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The dup() function returns a duplicate of the file descriptor. Wrapping\nit in a FileDescriptor isn't wrong as such, but it prevents from using\nit in contexts where a UniqueFD is needed. As the duplicate is\nguaranteed to have a single owner when created, return it as a UniqueFD\ninstead. A FileDescriptor can easily be created from the UniqueFD if\ndesired.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/file_descriptor.h |  2 +-\n src/libcamera/base/file_descriptor.cpp   | 22 ++++++++++++++--------\n 2 files changed, 15 insertions(+), 9 deletions(-)","diff":"diff --git a/include/libcamera/base/file_descriptor.h b/include/libcamera/base/file_descriptor.h\nindex 74292eba04f5..12a43f95d414 100644\n--- a/include/libcamera/base/file_descriptor.h\n+++ b/include/libcamera/base/file_descriptor.h\n@@ -28,7 +28,7 @@ public:\n \n \tbool isValid() const { return fd_ != nullptr; }\n \tint fd() const { return fd_ ? fd_->fd() : -1; }\n-\tFileDescriptor dup() const;\n+\tUniqueFD dup() const;\n \n private:\n \tclass Descriptor\ndiff --git a/src/libcamera/base/file_descriptor.cpp b/src/libcamera/base/file_descriptor.cpp\nindex da696b2501cd..a83bf52c31e6 100644\n--- a/src/libcamera/base/file_descriptor.cpp\n+++ b/src/libcamera/base/file_descriptor.cpp\n@@ -222,17 +222,23 @@ FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other)\n  * \\brief Duplicate a FileDescriptor\n  *\n  * Duplicating a FileDescriptor creates a duplicate of the wrapped file\n- * descriptor and returns a new FileDescriptor instance that wraps the\n- * duplicate. The fd() function of the original and duplicate instances will\n- * return different values. The duplicate instance will not be affected by\n- * destruction of the original instance or its copies.\n+ * descriptor and returns a UniqueFD that owns the duplicate. The fd() function\n+ * of the original and the get() function of the duplicate will return different\n+ * values. The duplicate instance will not be affected by destruction of the\n+ * original instance or its copies.\n  *\n- * \\return A new FileDescriptor instance wrapping a duplicate of the original\n- * file descriptor\n+ * \\return A UniqueFD owning a duplicate of the original file descriptor\n  */\n-FileDescriptor FileDescriptor::dup() const\n+UniqueFD FileDescriptor::dup() const\n {\n-\treturn FileDescriptor(fd());\n+\tint dupFd = ::dup(fd());\n+\tif (dupFd == -1) {\n+\t\tint ret = -errno;\n+\t\tLOG(FileDescriptor, Error)\n+\t\t\t<< \"Failed to dup() fd: \" << strerror(-ret);\n+\t}\n+\n+\treturn UniqueFD(dupFd);\n }\n \n FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate)\n","prefixes":["libcamera-devel","v3","07/17"]}