{"id":11943,"url":"https://patchwork.libcamera.org/api/patches/11943/?format=json","web_url":"https://patchwork.libcamera.org/patch/11943/","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":"<20210415083843.3399502-7-hiroh@chromium.org>","date":"2021-04-15T08:38:40","name":"[libcamera-devel,RFC,07/10] libcamera: V4L2VideoDevice: Use fd for a file descriptor","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"3defae76dc49ba4b60f9360bb434d9e817dbde66","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11943/mbox/","series":[{"id":1933,"url":"https://patchwork.libcamera.org/api/series/1933/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1933","date":"2021-04-15T08:38:35","name":null,"version":1,"mbox":"https://patchwork.libcamera.org/series/1933/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11943/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11943/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 CBDABBD224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 15 Apr 2021 08:39:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6C01768816;\n\tThu, 15 Apr 2021 10:39:01 +0200 (CEST)","from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com\n\t[IPv6:2607:f8b0:4864:20::42e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 129126881F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 10:38:59 +0200 (CEST)","by mail-pf1-x42e.google.com with SMTP id a12so15579918pfc.7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 01:38:59 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:2:84f5:7981:dfbe:8f02])\n\tby smtp.gmail.com with ESMTPSA id\n\t205sm1520258pfc.201.2021.04.15.01.38.56\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 15 Apr 2021 01:38:57 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"REJbrhtV\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=IHmv1/ucE/Y8xRCZdZ4f4MRgI2S4immsBBFqmGQaZYs=;\n\tb=REJbrhtVtc5sGUyyhFNqy+BeTgcddcO15fqsnI8go5++X9HyrsMlK+ppCBqGRVUom7\n\tfvKYr0m07BFprKW1NqfQ79GfRKcNkHl++UOl94ZJx0AGucsxfP1G7ZVwsnLrq26KGpHR\n\t+ugSC1AyWUPXpVkW7CRXSpvhXEbhmh4ij+RvU=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=IHmv1/ucE/Y8xRCZdZ4f4MRgI2S4immsBBFqmGQaZYs=;\n\tb=AdGZ7tLYQVrB1RXdISp/UfSsZqbZb5jRASPVQ4ylthIimfDXru3nCknkrDpUkhEGVn\n\tqYo5s3gieONUpKHXG0XYzrPsvAasXXrhOJ0MmhwiYBRSxioQDU0h5pzEwF/dLzhyqAYV\n\tG7OkuoCn8q2DyKS1a+JwQHrZp9q9nObudQ/3hytUWoHzB3iE4zaHM9nDbLHFWLqOCMku\n\tv8bMe7vn05JvArmj8XC4K5L4BkquS9q2j3+7u3kIuyNBwY32l3rIiOZV4z4f7LK/p9JO\n\t/SCRpBP+4p+S08g3odqugSIdscnFtmFBtdqyolFSDacs2NTMVD/NRqRy9mortbpFpozL\n\t1+8w==","X-Gm-Message-State":"AOAM5314lEMnwf+7MOxCD7NJsWexasUe9Brgt1pqhaDeZTtj6qgUQuSG\n\tvUvEN57pMR8CFmKaA5D7RmE99HTUjV+hOA==","X-Google-Smtp-Source":"ABdhPJwJQHHe7WbD1gw9oyuU5g7Mp+VlXLSkK95RU8eV+/VQctCswptXXB/bP9nEGj108eO2H+Rw7g==","X-Received":"by 2002:a63:1b5c:: with SMTP id\n\tb28mr2451726pgm.444.1618475937534; \n\tThu, 15 Apr 2021 01:38:57 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 15 Apr 2021 17:38:40 +0900","Message-Id":"<20210415083843.3399502-7-hiroh@chromium.org>","X-Mailer":"git-send-email 2.31.1.368.gbe11c130af-goog","In-Reply-To":"<20210415083843.3399502-1-hiroh@chromium.org>","References":"<20210415083843.3399502-1-hiroh@chromium.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH 07/10] libcamera: V4L2VideoDevice: 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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"V4L2VideoDevice deals with file descriptors. This uses ScopedFD\nfor them to avoid the leakage. This also changes the return type\nof exportDmabufFd to ScopedFD from FileDescriptor in order to\nrepresent a caller owns the returned file file descriptor.\n\nSigned-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(-)","diff":"diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex 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();\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 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 \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+\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 }\n","prefixes":["libcamera-devel","RFC","07/10"]}