{"id":14877,"url":"https://patchwork.libcamera.org/api/patches/14877/?format=json","web_url":"https://patchwork.libcamera.org/patch/14877/","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-23-laurent.pinchart@ideasonboard.com>","date":"2021-11-30T03:38:20","name":"[libcamera-devel,v4,22/22] libcamera: base: unique_fd: Pass rvalue reference to constructor and reset()","commit_ref":null,"pull_url":null,"state":"rejected","archived":false,"hash":"8926481a92c69542a1be046a6156838278b83e5f","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":{"id":14,"url":"https://patchwork.libcamera.org/api/users/14/?format=json","username":"pinchartl","first_name":"Laurent","last_name":"Pinchart","email":"laurent.pinchart@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/14877/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/14877/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14877/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 07BDEC3255\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 03:39:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A479760727;\n\tTue, 30 Nov 2021 04:39:16 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 082AC605D2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:59 +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 A346B11C5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:58 +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=\"YANpMEkU\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638243538;\n\tbh=SqUv1cjL1iMmgnUdTeHgH5AAculkGF2lt3yu8foY4iY=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=YANpMEkU2RhdJwT7T9CyL+da0WhSF2ltglF3I9ot3rXBb+1NvEtN9tVtSbO1P+VSp\n\tGRWuqsXKo4X/MzTP0263sW68jdAYjI7s9Zbj0E//crSQWRBJ40a64mgPRc2PLkNWnw\n\tavLphdxlUxVX/9XKgZDFWrhytHY9iYenWex5+iw8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 30 Nov 2021 05:38:20 +0200","Message-Id":"<20211130033820.18235-23-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 22/22] libcamera: base: unique_fd: Pass\n\trvalue reference to constructor and reset()","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":"To avoid the risk of using a file descriptor whose ownership has been\ntransferred to a UniqueFD instance, pass an rvalue reference to the\nconstructor taking an int, and to the reset() function. The fd argument\nis set to -1 upon return, making incorrect usage of the file descriptor\nimpossible.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nI'm not entirely sure about this patch. If desired, it will get squashed\nwith the one introducing UniqueFD.\n---\n include/libcamera/base/unique_fd.h               |  5 +++--\n src/libcamera/base/shared_fd.cpp                 |  2 +-\n src/libcamera/base/unique_fd.cpp                 | 10 ++++++++--\n src/libcamera/ipc_unixsocket.cpp                 |  4 ++--\n src/libcamera/pipeline/raspberrypi/dma_heaps.cpp | 15 ++++++---------\n src/libcamera/process.cpp                        |  4 ++--\n src/libcamera/v4l2_videodevice.cpp               |  2 +-\n test/unique-fd.cpp                               | 10 ++++++----\n 8 files changed, 29 insertions(+), 23 deletions(-)","diff":"diff --git a/include/libcamera/base/unique_fd.h b/include/libcamera/base/unique_fd.h\nindex ae4d96b75797..a5ffb1a435dc 100644\n--- a/include/libcamera/base/unique_fd.h\n+++ b/include/libcamera/base/unique_fd.h\n@@ -22,9 +22,10 @@ public:\n \t{\n \t}\n \n-\texplicit UniqueFD(int fd)\n+\texplicit UniqueFD(int &&fd)\n \t\t: fd_(fd)\n \t{\n+\t\tfd = -1;\n \t}\n \n \tUniqueFD(UniqueFD &&other)\n@@ -50,7 +51,7 @@ public:\n \t\treturn fd;\n \t}\n \n-\tvoid reset(int fd = -1);\n+\tvoid reset(int &&fd = -1);\n \n \tvoid swap(UniqueFD &other)\n \t{\ndiff --git a/src/libcamera/base/shared_fd.cpp b/src/libcamera/base/shared_fd.cpp\nindex 0c8b93a47f85..8aecd34038bd 100644\n--- a/src/libcamera/base/shared_fd.cpp\n+++ b/src/libcamera/base/shared_fd.cpp\n@@ -260,7 +260,7 @@ UniqueFD SharedFD::dup() const\n \t\t\t<< \"Failed to dup() fd: \" << strerror(-ret);\n \t}\n \n-\treturn UniqueFD(dupFd);\n+\treturn UniqueFD(std::move(dupFd));\n }\n \n SharedFD::Descriptor::Descriptor(int fd, bool duplicate)\ndiff --git a/src/libcamera/base/unique_fd.cpp b/src/libcamera/base/unique_fd.cpp\nindex 83d6919cf623..7a961dbc01d0 100644\n--- a/src/libcamera/base/unique_fd.cpp\n+++ b/src/libcamera/base/unique_fd.cpp\n@@ -38,9 +38,12 @@ LOG_DEFINE_CATEGORY(UniqueFD)\n  */\n \n /**\n- * \\fn UniqueFD::UniqueFD(int fd)\n+ * \\fn UniqueFD::UniqueFD(int &&fd)\n  * \\brief Construct a UniqueFD that owns \\a fd\n  * \\param[in] fd A file descriptor to manage\n+ *\n+ * Ownership of the file descriptor is transferred to the UniqueFD instance.\n+ * Upon return \\a fd is set to -1.\n  */\n \n /**\n@@ -88,11 +91,12 @@ LOG_DEFINE_CATEGORY(UniqueFD)\n  * \\param[in] fd The new file descriptor to manage\n  *\n  * Close the managed file descriptor, if any, and replace it with the new \\a fd.\n+ * Upon return \\a fd is set to -1.\n  *\n  * Self-resetting (passing an \\a fd already managed by this instance) is invalid\n  * and results in undefined behaviour.\n  */\n-void UniqueFD::reset(int fd)\n+void UniqueFD::reset(int &&fd)\n {\n \tASSERT(!isValid() || fd != fd_);\n \n@@ -100,6 +104,8 @@ void UniqueFD::reset(int fd)\n \n \tif (fd >= 0)\n \t\tclose(fd);\n+\n+\tfd = -1;\n }\n \n /**\ndiff --git a/src/libcamera/ipc_unixsocket.cpp b/src/libcamera/ipc_unixsocket.cpp\nindex 1980d374cea8..32bd6533d192 100644\n--- a/src/libcamera/ipc_unixsocket.cpp\n+++ b/src/libcamera/ipc_unixsocket.cpp\n@@ -103,8 +103,8 @@ UniqueFD IPCUnixSocket::create()\n \t}\n \n \tstd::array<UniqueFD, 2> socketFds{\n-\t\tUniqueFD(sockets[0]),\n-\t\tUniqueFD(sockets[1]),\n+\t\tUniqueFD(std::move(sockets[0])),\n+\t\tUniqueFD(std::move(sockets[1])),\n \t};\n \n \tif (bind(std::move(socketFds[0])) < 0)\ndiff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\nindex 69831dabbe75..f2a95300d187 100644\n--- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp\n@@ -37,16 +37,13 @@ namespace RPi {\n DmaHeap::DmaHeap()\n {\n \tfor (const char *name : heapNames) {\n-\t\tint ret = ::open(name, O_RDWR, 0);\n-\t\tif (ret < 0) {\n-\t\t\tret = errno;\n-\t\t\tLOG(RPI, Debug) << \"Failed to open \" << name << \": \"\n-\t\t\t\t\t<< strerror(ret);\n-\t\t\tcontinue;\n-\t\t}\n+\t\tdmaHeapHandle_ = UniqueFD(open(name, O_RDWR, 0));\n+\t\tif (dmaHeapHandle_.isValid())\n+\t\t\tbreak;\n \n-\t\tdmaHeapHandle_ = UniqueFD(ret);\n-\t\tbreak;\n+\t\tint err = errno;\n+\t\tLOG(RPI, Debug) << \"Failed to open \" << name << \": \"\n+\t\t\t\t<< strerror(err);\n \t}\n \n \tif (!dmaHeapHandle_.isValid())\ndiff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\nindex 0e6b4e1dde58..dc8576d934b6 100644\n--- a/src/libcamera/process.cpp\n+++ b/src/libcamera/process.cpp\n@@ -134,8 +134,8 @@ ProcessManager::ProcessManager()\n \t\tLOG(Process, Fatal)\n \t\t\t<< \"Failed to initialize pipe for signal handling\";\n \n-\tpipe_[0] = UniqueFD(pipe[0]);\n-\tpipe_[1] = UniqueFD(pipe[1]);\n+\tpipe_[0] = UniqueFD(std::move(pipe[0]));\n+\tpipe_[1] = UniqueFD(std::move(pipe[1]));\n \n \tsigEvent_ = new EventNotifier(pipe_[0].get(), EventNotifier::Read);\n \tsigEvent_->activated.connect(this, &ProcessManager::sighandler);\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex b4b89e2759b9..9d0d0077f4e4 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -1396,7 +1396,7 @@ UniqueFD V4L2VideoDevice::exportDmabufFd(unsigned int index,\n \t\treturn {};\n \t}\n \n-\treturn UniqueFD(expbuf.fd);\n+\treturn UniqueFD(std::move(expbuf.fd));\n }\n \n /**\ndiff --git a/test/unique-fd.cpp b/test/unique-fd.cpp\nindex eb3b591fec28..7e94e3ca450e 100644\n--- a/test/unique-fd.cpp\n+++ b/test/unique-fd.cpp\n@@ -39,7 +39,8 @@ protected:\n \t\t}\n \n \t\t/* Test creating UniqueFD from numerical file descriptor. */\n-\t\tUniqueFD fd2(fd_);\n+\t\tint numFd = fd_;\n+\t\tUniqueFD fd2(std::move(numFd));\n \t\tif (!fd2.isValid() || fd2.get() != fd_) {\n \t\t\tstd::cout << \"Failed fd check (fd constructor)\"\n \t\t\t\t  << std::endl;\n@@ -113,7 +114,7 @@ protected:\n \t\t}\n \n \t\t/* Test release. */\n-\t\tint numFd = fd2.release();\n+\t\tnumFd = fd2.release();\n \t\tif (fd2.isValid() || fd2.get() != -1) {\n \t\t\tstd::cout << \"Failed fd check (release)\"\n \t\t\t\t  << std::endl;\n@@ -133,7 +134,7 @@ protected:\n \t\t}\n \n \t\t/* Test reset assignment. */\n-\t\tfd.reset(numFd);\n+\t\tfd.reset(std::move(numFd));\n \t\tif (!fd.isValid() || fd.get() != fd_) {\n \t\t\tstd::cout << \"Failed fd check (reset assignment)\"\n \t\t\t\t  << std::endl;\n@@ -168,7 +169,8 @@ protected:\n \t\t}\n \n \t\t{\n-\t\t\tUniqueFD fd4(fd_);\n+\t\t\tnumFd = fd_;\n+\t\t\tUniqueFD fd4(std::move(numFd));\n \t\t}\n \n \t\tif (isValidFd(fd_)) {\n","prefixes":["libcamera-devel","v4","22/22"]}