[{"id":17413,"web_url":"https://patchwork.libcamera.org/comment/17413/","msgid":"<YL0TFHRyidDzptHv@pendragon.ideasonboard.com>","date":"2021-06-06T18:25:24","subject":"Re: [libcamera-devel] [RFC PATCH 07/10] libcamera: V4L2VideoDevice:\n\tUse fd for a file descriptor","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Thu, Apr 15, 2021 at 05:38:40PM +0900, Hirokazu Honda wrote:\n> V4L2VideoDevice deals with file descriptors. This uses ScopedFD\n> for them to avoid the leakage. This also changes the return type\n> of exportDmabufFd to ScopedFD from FileDescriptor in order to\n> represent a caller owns the returned file file descriptor.\n> \n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  include/libcamera/internal/v4l2_videodevice.h |  4 ++--\n>  src/libcamera/v4l2_videodevice.cpp            | 21 ++++++++-----------\n>  2 files changed, 11 insertions(+), 14 deletions(-)\n> \n> diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\n> index 7938343b..925a8e82 100644\n> --- a/include/libcamera/internal/v4l2_videodevice.h\n> +++ b/include/libcamera/internal/v4l2_videodevice.h\n> @@ -30,9 +30,9 @@\n>  namespace libcamera {\n>  \n>  class EventNotifier;\n> -class FileDescriptor;\n>  class MediaDevice;\n>  class MediaEntity;\n> +class ScopedFD;\n>  \n>  struct V4L2Capability final : v4l2_capability {\n>  \tconst char *driver() const\n> @@ -235,7 +235,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> +\tScopedFD exportDmabufFd(unsigned int index, unsigned int plane);\n>  \n>  \tvoid bufferAvailable(EventNotifier *notifier);\n>  \tFrameBuffer *dequeueBuffer();\n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index 0bf3b5f5..fe311ed9 100644\n> --- a/src/libcamera/v4l2_videodevice.cpp\n> +++ b/src/libcamera/v4l2_videodevice.cpp\n> @@ -1278,12 +1278,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\tScopedFD 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(fd.release());\n>  \t\tplane.length = multiPlanar ?\n>  \t\t\tbuf.m.planes[nplane].length : buf.length;\n>  \n> @@ -1293,7 +1293,7 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n>  \treturn std::make_unique<FrameBuffer>(std::move(planes));\n>  }\n>  \n> -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,\n> +ScopedFD V4L2VideoDevice::exportDmabufFd(unsigned int index,\n>  \t\t\t\t\t       unsigned int plane)\n>  {\n>  \tstruct v4l2_exportbuffer expbuf = {};\n> @@ -1308,10 +1308,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 ScopedFD();\n>  \t}\n>  \n> -\treturn FileDescriptor(std::move(expbuf.fd));\n> +\treturn ScopedFD(expbuf.fd);\n>  }\n>  \n>  /**\n> @@ -1703,7 +1703,6 @@ V4L2M2MDevice::~V4L2M2MDevice()\n>   */\n>  int V4L2M2MDevice::open()\n>  {\n> -\tint fd;\n>  \tint ret;\n>  \n>  \t/*\n> @@ -1712,7 +1711,7 @@ 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> +\tint fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(),\n>  \t\t     O_RDWR | O_NONBLOCK);\n\nWrong indentation.\n\n>  \tif (fd < 0) {\n>  \t\tret = -errno;\n> @@ -1720,22 +1719,20 @@ int V4L2M2MDevice::open()\n>  \t\t\t<< \"Failed to open V4L2 M2M device: \" << strerror(-ret);\n>  \t\treturn ret;\n>  \t}\n\nBlank line.\n\n> +\tScopedFD scopedFd(fd);\n>  \n> -\tret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT);\n> +\tret = output_->open(scopedFd.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(scopedFd.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>  }","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 CBD89C320B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  6 Jun 2021 18:25:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 46CF86892B;\n\tSun,  6 Jun 2021 20:25:39 +0200 (CEST)","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 31D0E602A7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Jun 2021 20:25:38 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 9449DEF;\n\tSun,  6 Jun 2021 20:25:37 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"khPa8sgI\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1623003937;\n\tbh=hCYN4322mUkwwS1Sr6BLDscnlm/DHS3KobvpB8Pu4vM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=khPa8sgI8fbMWGHdU/56mN31IsACErsFTeJPl+HgiUTsEjDpiGAJYqFr+j3Gyl9pg\n\tbpz0DoZHuWf0ae64g+Px3G4wH9MuSWx0MkvI+vmLylB82Cy3/O9PjFsvgifbIjGC8X\n\tohfeu689LD/TooHo0LT0Gff11MgAMpDV2E+jYJG4=","Date":"Sun, 6 Jun 2021 21:25:24 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YL0TFHRyidDzptHv@pendragon.ideasonboard.com>","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-7-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210415083843.3399502-7-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [RFC PATCH 07/10] libcamera: V4L2VideoDevice:\n\tUse fd 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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]