{"id":14871,"url":"https://patchwork.libcamera.org/api/1.1/patches/14871/?format=json","web_url":"https://patchwork.libcamera.org/patch/14871/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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-16-laurent.pinchart@ideasonboard.com>","date":"2021-11-30T03:38:13","name":"[libcamera-devel,v4,15/22] libcamera: v4l2_videodevice: Use fd for a file descriptor","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"e27e5c8d798795b083ac191c232cd485accaec22","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14871/mbox/","series":[{"id":2776,"url":"https://patchwork.libcamera.org/api/1.1/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/14871/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14871/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 68267C3252\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 03:39:13 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1521E605C9;\n\tTue, 30 Nov 2021 04:39:13 +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 867B3605D3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:55 +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 132D88F0;\n\tTue, 30 Nov 2021 04:38:55 +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=\"r20bkne5\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638243535;\n\tbh=xt3Pu3F2X8AkvXsg1TxYcKeyvtD5XDYdaoibapGV62Q=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=r20bkne5n9n3e0l+MGlEMkbCi6JpCYulGTRGVVKpbczFW8qfZ3kQj8fX36e8xR1AK\n\trUEWxyUL6BMd+ypWGk2XLeDQ9Zey/Uj0sY29dJTYyiTFMetW+VtgDphf2+pCTZD69F\n\tpK8Av7gqDt8763MCi/R/Zn5fL0QGO/ZCLlDJuPGo=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 30 Nov 2021 05:38:13 +0200","Message-Id":"<20211130033820.18235-16-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 15/22] libcamera: v4l2_videodevice: Use\n\tfd 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 file descriptors owned by V4L2VideoDevice by UniqueFD.\nThis also changes the return type of exportDmabufFd to UniqueFD\nfrom FileDescriptor in order to represent a caller owns the\nreturned file file descriptor.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Hirokazu Honda <hiroh@chromium.org>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n---\nChanges since v2:\n\n- Use FileDescriptor(UniqueFD) constructor\n- Fix errno handling\n- Return {}\n---\n include/libcamera/internal/v4l2_videodevice.h |  4 +--\n src/libcamera/v4l2_videodevice.cpp            | 30 ++++++++-----------\n 2 files changed, 15 insertions(+), 19 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex 4a44b7fd53b1..80b665771782 100644\n--- a/include/libcamera/internal/v4l2_videodevice.h\n+++ b/include/libcamera/internal/v4l2_videodevice.h\n@@ -19,6 +19,7 @@\n #include <libcamera/base/class.h>\n #include <libcamera/base/log.h>\n #include <libcamera/base/signal.h>\n+#include <libcamera/base/unique_fd.h>\n \n #include <libcamera/framebuffer.h>\n #include <libcamera/geometry.h>\n@@ -31,7 +32,6 @@\n namespace libcamera {\n \n class EventNotifier;\n-class FileDescriptor;\n class MediaDevice;\n class MediaEntity;\n \n@@ -238,7 +238,7 @@ private:\n \tint createBuffers(unsigned int count,\n \t\t\t  std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n \tstd::unique_ptr<FrameBuffer> createBuffer(unsigned int index);\n-\tFileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);\n+\tUniqueFD exportDmabufFd(unsigned int index, unsigned int plane);\n \n \tvoid bufferAvailable();\n \tFrameBuffer *dequeueBuffer();\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex c95626d33cc3..3966483a365f 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -1320,12 +1320,12 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n \n \tstd::vector<FrameBuffer::Plane> planes;\n \tfor (unsigned int nplane = 0; nplane < numPlanes; nplane++) {\n-\t\tFileDescriptor fd = exportDmabufFd(buf.index, nplane);\n+\t\tUniqueFD fd = exportDmabufFd(buf.index, nplane);\n \t\tif (!fd.isValid())\n \t\t\treturn nullptr;\n \n \t\tFrameBuffer::Plane plane;\n-\t\tplane.fd = std::move(fd);\n+\t\tplane.fd = FileDescriptor(std::move(fd));\n \t\t/*\n \t\t * V4L2 API doesn't provide dmabuf offset information of plane.\n \t\t * Set 0 as a placeholder offset.\n@@ -1380,8 +1380,8 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n \treturn std::make_unique<FrameBuffer>(planes);\n }\n \n-FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,\n-\t\t\t\t\t       unsigned int plane)\n+UniqueFD V4L2VideoDevice::exportDmabufFd(unsigned int index,\n+\t\t\t\t\t unsigned int plane)\n {\n \tstruct v4l2_exportbuffer expbuf = {};\n \tint ret;\n@@ -1395,10 +1395,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,\n \tif (ret < 0) {\n \t\tLOG(V4L2, Error)\n \t\t\t<< \"Failed to export buffer: \" << strerror(-ret);\n-\t\treturn FileDescriptor();\n+\t\treturn {};\n \t}\n \n-\treturn FileDescriptor(std::move(expbuf.fd));\n+\treturn UniqueFD(expbuf.fd);\n }\n \n /**\n@@ -1896,7 +1896,6 @@ V4L2M2MDevice::~V4L2M2MDevice()\n  */\n int V4L2M2MDevice::open()\n {\n-\tint fd;\n \tint ret;\n \n \t/*\n@@ -1905,30 +1904,27 @@ int V4L2M2MDevice::open()\n \t * as the V4L2VideoDevice::open() retains a handle by duplicating the\n \t * fd passed in.\n \t */\n-\tfd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(),\n-\t\t     O_RDWR | O_NONBLOCK);\n-\tif (fd < 0) {\n+\tUniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(),\n+\t\t\t    O_RDWR | O_NONBLOCK));\n+\tif (!fd.isValid()) {\n \t\tret = -errno;\n-\t\tLOG(V4L2, Error)\n-\t\t\t<< \"Failed to open V4L2 M2M device: \" << strerror(-ret);\n+\t\tLOG(V4L2, Error) << \"Failed to open V4L2 M2M device: \"\n+\t\t\t\t << strerror(-ret);\n \t\treturn ret;\n \t}\n \n-\tret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT);\n+\tret = output_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT);\n \tif (ret)\n \t\tgoto err;\n \n-\tret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE);\n+\tret = capture_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE);\n \tif (ret)\n \t\tgoto err;\n \n-\t::close(fd);\n-\n \treturn 0;\n \n err:\n \tclose();\n-\t::close(fd);\n \n \treturn ret;\n }\n","prefixes":["libcamera-devel","v4","15/22"]}