{"id":14863,"url":"https://patchwork.libcamera.org/api/patches/14863/?format=json","web_url":"https://patchwork.libcamera.org/patch/14863/","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":"<20211130033820.18235-8-laurent.pinchart@ideasonboard.com>","date":"2021-11-30T03:38:05","name":"[libcamera-devel,v4,07/22] 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/14863/mbox/","series":[{"id":2776,"url":"https://patchwork.libcamera.org/api/series/2776/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2776","date":"2021-11-30T03:37:58","name":"libcamera: Introduce UniqueFD","version":4,"mbox":"https://patchwork.libcamera.org/series/2776/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14863/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14863/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 7A0D8C3250\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 03:39:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E876B606FD;\n\tTue, 30 Nov 2021 04:39:02 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 60319605C2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:51 +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 ECEC711C5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:50 +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=\"s+F9Ximf\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638243531;\n\tbh=6VQC/oNlDxzmvYqn6LpHYeUHO8aITJVndH01oUYvOAM=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=s+F9Ximf9tCdS7PI8HCrZiAf80zlVL11SFlx/rnbMVyOx8oigGVH1ELuRtlyV3qox\n\tUwKT/yAWGrySlnswXN8j3lQdc/G/eG2VVVvwL3ig2m+Els7RbWx3AjOEOzwaOyuyaZ\n\t9xoT4K0LYa+YGNyt6ayKqDN6xcGE4hrM5CNe3RZo=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 07/22] 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","v4","07/22"]}