From patchwork Thu Apr 15 08:38:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11943 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id CBDABBD224 for ; Thu, 15 Apr 2021 08:39:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6C01768816; Thu, 15 Apr 2021 10:39:01 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="REJbrhtV"; dkim-atps=neutral Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 129126881F for ; Thu, 15 Apr 2021 10:38:59 +0200 (CEST) Received: by mail-pf1-x42e.google.com with SMTP id a12so15579918pfc.7 for ; Thu, 15 Apr 2021 01:38:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IHmv1/ucE/Y8xRCZdZ4f4MRgI2S4immsBBFqmGQaZYs=; b=REJbrhtVtc5sGUyyhFNqy+BeTgcddcO15fqsnI8go5++X9HyrsMlK+ppCBqGRVUom7 fvKYr0m07BFprKW1NqfQ79GfRKcNkHl++UOl94ZJx0AGucsxfP1G7ZVwsnLrq26KGpHR +ugSC1AyWUPXpVkW7CRXSpvhXEbhmh4ij+RvU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IHmv1/ucE/Y8xRCZdZ4f4MRgI2S4immsBBFqmGQaZYs=; b=AdGZ7tLYQVrB1RXdISp/UfSsZqbZb5jRASPVQ4ylthIimfDXru3nCknkrDpUkhEGVn qYo5s3gieONUpKHXG0XYzrPsvAasXXrhOJ0MmhwiYBRSxioQDU0h5pzEwF/dLzhyqAYV G7OkuoCn8q2DyKS1a+JwQHrZp9q9nObudQ/3hytUWoHzB3iE4zaHM9nDbLHFWLqOCMku v8bMe7vn05JvArmj8XC4K5L4BkquS9q2j3+7u3kIuyNBwY32l3rIiOZV4z4f7LK/p9JO /SCRpBP+4p+S08g3odqugSIdscnFtmFBtdqyolFSDacs2NTMVD/NRqRy9mortbpFpozL 1+8w== X-Gm-Message-State: AOAM5314lEMnwf+7MOxCD7NJsWexasUe9Brgt1pqhaDeZTtj6qgUQuSG vUvEN57pMR8CFmKaA5D7RmE99HTUjV+hOA== X-Google-Smtp-Source: ABdhPJwJQHHe7WbD1gw9oyuU5g7Mp+VlXLSkK95RU8eV+/VQctCswptXXB/bP9nEGj108eO2H+Rw7g== X-Received: by 2002:a63:1b5c:: with SMTP id b28mr2451726pgm.444.1618475937534; Thu, 15 Apr 2021 01:38:57 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:57 -0700 (PDT) From: Hirokazu Honda 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 fd for a file descriptor X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" V4L2VideoDevice deals with file descriptors. This uses ScopedFD for them to avoid the leakage. This also changes the return type of exportDmabufFd to ScopedFD from FileDescriptor in order to represent a caller owns the returned file file descriptor. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_videodevice.h | 4 ++-- src/libcamera/v4l2_videodevice.cpp | 21 ++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 7938343b..925a8e82 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -30,9 +30,9 @@ namespace libcamera { class EventNotifier; -class FileDescriptor; class MediaDevice; class MediaEntity; +class ScopedFD; struct V4L2Capability final : v4l2_capability { const char *driver() const @@ -235,7 +235,7 @@ private: int createBuffers(unsigned int count, std::vector> *buffers); std::unique_ptr createBuffer(unsigned int index); - FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); + ScopedFD exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(EventNotifier *notifier); FrameBuffer *dequeueBuffer(); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 0bf3b5f5..fe311ed9 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1278,12 +1278,12 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) std::vector planes; for (unsigned int nplane = 0; nplane < numPlanes; nplane++) { - FileDescriptor fd = exportDmabufFd(buf.index, nplane); + ScopedFD fd = exportDmabufFd(buf.index, nplane); if (!fd.isValid()) return nullptr; FrameBuffer::Plane plane; - plane.fd = std::move(fd); + plane.fd = FileDescriptor(fd.release()); plane.length = multiPlanar ? buf.m.planes[nplane].length : buf.length; @@ -1293,7 +1293,7 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) return std::make_unique(std::move(planes)); } -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, +ScopedFD V4L2VideoDevice::exportDmabufFd(unsigned int index, unsigned int plane) { struct v4l2_exportbuffer expbuf = {}; @@ -1308,10 +1308,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, if (ret < 0) { LOG(V4L2, Error) << "Failed to export buffer: " << strerror(-ret); - return FileDescriptor(); + return ScopedFD(); } - return FileDescriptor(std::move(expbuf.fd)); + return ScopedFD(expbuf.fd); } /** @@ -1703,7 +1703,6 @@ V4L2M2MDevice::~V4L2M2MDevice() */ int V4L2M2MDevice::open() { - int fd; int ret; /* @@ -1712,7 +1711,7 @@ int V4L2M2MDevice::open() * as the V4L2VideoDevice::open() retains a handle by duplicating the * fd passed in. */ - fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), + int fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), O_RDWR | O_NONBLOCK); if (fd < 0) { ret = -errno; @@ -1720,22 +1719,20 @@ int V4L2M2MDevice::open() << "Failed to open V4L2 M2M device: " << strerror(-ret); return ret; } + ScopedFD scopedFd(fd); - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); + ret = output_->open(scopedFd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); if (ret) goto err; - ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); + ret = capture_->open(scopedFd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); if (ret) goto err; - ::close(fd); - return 0; err: close(); - ::close(fd); return ret; }