{"id":14828,"url":"https://patchwork.libcamera.org/api/patches/14828/?format=json","web_url":"https://patchwork.libcamera.org/patch/14828/","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-14-laurent.pinchart@ideasonboard.com>","date":"2021-11-28T23:57:48","name":"[libcamera-devel,v3,13/17] libcamera: v4l2_device: Use UniqueFD for a file descriptor","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"98c860b291944620b4d9f3ee8798060f263cbacc","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/14828/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/14828/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14828/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 A4D8DC324F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 28 Nov 2021 23:58:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5F457605A1;\n\tMon, 29 Nov 2021 00:58:39 +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 16CB560592\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 00:58:27 +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 971AE730;\n\tMon, 29 Nov 2021 00:58:26 +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=\"KCVKIXue\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638143906;\n\tbh=jYtZ9x8E5aewctI3eJJsAkDRc86PPPTFhuLcwExvZw4=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=KCVKIXuevwhuBX6sI8oshhz4F8RbXiMavxreQg75r0zhFF6n4xP+4TSd8gZbTDUGT\n\t8zxbbpgk7xc3RMtfYY12n9HCEK5vS5+CfUTv08chVnPkPsZtxxo9M5FbuCTDePADT1\n\tmy4OP7G5UUmJyj/QoN/fDiOwY6tjJShT9XOIph70=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 29 Nov 2021 01:57:48 +0200","Message-Id":"<20211128235752.10836-14-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 13/17] libcamera: v4l2_device: Use\n\tUniqueFD for a file descriptor","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":"From: Hirokazu Honda <hiroh@chromium.org>\n\nManages a file descriptor owned by V4L2Device for a v4l2 device node\nby UniqueFD.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v2:\n\n- Fix errno handling\n---\n include/libcamera/internal/v4l2_device.h |  9 +++++----\n src/libcamera/v4l2_device.cpp            | 23 ++++++++++-------------\n src/libcamera/v4l2_videodevice.cpp       | 16 ++++++----------\n 3 files changed, 21 insertions(+), 27 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\nindex 7816a290141d..8886b750ae29 100644\n--- a/include/libcamera/internal/v4l2_device.h\n+++ b/include/libcamera/internal/v4l2_device.h\n@@ -16,6 +16,7 @@\n #include <libcamera/base/log.h>\n #include <libcamera/base/signal.h>\n #include <libcamera/base/span.h>\n+#include <libcamera/base/unique_fd.h>\n \n #include <libcamera/controls.h>\n \n@@ -27,7 +28,7 @@ class V4L2Device : protected Loggable\n {\n public:\n \tvoid close();\n-\tbool isOpen() const { return fd_ != -1; }\n+\tbool isOpen() const { return fd_.isValid(); }\n \n \tconst ControlInfoMap &controls() const { return controls_; }\n \n@@ -49,11 +50,11 @@ protected:\n \t~V4L2Device();\n \n \tint open(unsigned int flags);\n-\tint setFd(int fd);\n+\tint setFd(UniqueFD fd);\n \n \tint ioctl(unsigned long request, void *argp);\n \n-\tint fd() const { return fd_; }\n+\tint fd() const { return fd_.get(); }\n \n private:\n \tstatic ControlType v4l2CtrlType(uint32_t ctrlType);\n@@ -72,7 +73,7 @@ private:\n \tControlIdMap controlIdMap_;\n \tControlInfoMap controls_;\n \tstd::string deviceNode_;\n-\tint fd_;\n+\tUniqueFD fd_;\n \n \tEventNotifier *fdEventNotifier_;\n \tbool frameStartEnabled_;\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 9c783c9cbed1..39f360091f64 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -53,7 +53,7 @@ LOG_DEFINE_CATEGORY(V4L2)\n  * at open() time, and the \\a logTag to prefix log messages with.\n  */\n V4L2Device::V4L2Device(const std::string &deviceNode)\n-\t: deviceNode_(deviceNode), fd_(-1), fdEventNotifier_(nullptr),\n+\t: deviceNode_(deviceNode), fdEventNotifier_(nullptr),\n \t  frameStartEnabled_(false)\n {\n }\n@@ -81,15 +81,15 @@ int V4L2Device::open(unsigned int flags)\n \t\treturn -EBUSY;\n \t}\n \n-\tint ret = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags);\n-\tif (ret < 0) {\n-\t\tret = -errno;\n+\tUniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags));\n+\tif (!fd.isValid()) {\n+\t\tint ret = -errno;\n \t\tLOG(V4L2, Error) << \"Failed to open V4L2 device: \"\n \t\t\t\t << strerror(-ret);\n \t\treturn ret;\n \t}\n \n-\tsetFd(ret);\n+\tsetFd(std::move(fd));\n \n \tlistControls();\n \n@@ -112,14 +112,14 @@ int V4L2Device::open(unsigned int flags)\n  *\n  * \\return 0 on success or a negative error code otherwise\n  */\n-int V4L2Device::setFd(int fd)\n+int V4L2Device::setFd(UniqueFD fd)\n {\n \tif (isOpen())\n \t\treturn -EBUSY;\n \n-\tfd_ = fd;\n+\tfd_ = std::move(fd);\n \n-\tfdEventNotifier_ = new EventNotifier(fd_, EventNotifier::Exception);\n+\tfdEventNotifier_ = new EventNotifier(fd_.get(), EventNotifier::Exception);\n \tfdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable);\n \tfdEventNotifier_->setEnabled(false);\n \n@@ -138,10 +138,7 @@ void V4L2Device::close()\n \n \tdelete fdEventNotifier_;\n \n-\tif (::close(fd_) < 0)\n-\t\tLOG(V4L2, Error) << \"Failed to close V4L2 device: \"\n-\t\t\t\t << strerror(errno);\n-\tfd_ = -1;\n+\tfd_.reset();\n }\n \n /**\n@@ -440,7 +437,7 @@ int V4L2Device::ioctl(unsigned long request, void *argp)\n \t * Printing out an error message is usually better performed\n \t * in the caller, which can provide more context.\n \t */\n-\tif (::ioctl(fd_, request, argp) < 0)\n+\tif (::ioctl(fd_.get(), request, argp) < 0)\n \t\treturn -errno;\n \n \treturn 0;\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 0a85bcf6b3ff..c95626d33cc3 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -24,6 +24,7 @@\n #include <libcamera/base/event_notifier.h>\n #include <libcamera/base/file_descriptor.h>\n #include <libcamera/base/log.h>\n+#include <libcamera/base/unique_fd.h>\n #include <libcamera/base/utils.h>\n \n #include \"libcamera/internal/formats.h\"\n@@ -620,22 +621,17 @@ int V4L2VideoDevice::open()\n  */\n int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type)\n {\n-\tint ret;\n-\tint newFd;\n-\n-\tnewFd = dup(handle);\n-\tif (newFd < 0) {\n-\t\tret = -errno;\n+\tUniqueFD newFd(dup(handle));\n+\tif (!newFd.isValid()) {\n \t\tLOG(V4L2, Error) << \"Failed to duplicate file handle: \"\n-\t\t\t\t << strerror(-ret);\n-\t\treturn ret;\n+\t\t\t\t << strerror(errno);\n+\t\treturn -errno;\n \t}\n \n-\tret = V4L2Device::setFd(newFd);\n+\tint ret = V4L2Device::setFd(std::move(newFd));\n \tif (ret < 0) {\n \t\tLOG(V4L2, Error) << \"Failed to set file handle: \"\n \t\t\t\t << strerror(-ret);\n-\t\t::close(newFd);\n \t\treturn ret;\n \t}\n \n","prefixes":["libcamera-devel","v3","13/17"]}