From patchwork Tue Nov 30 03:38:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 14870 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com 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 BC66EC3251 for ; Tue, 30 Nov 2021 03:39:12 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6397D605C2; Tue, 30 Nov 2021 04:39:12 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="bHsiojhX"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 21AB5605BA for ; Tue, 30 Nov 2021 04:38:56 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B05C211FB for ; Tue, 30 Nov 2021 04:38:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1638243535; bh=SeyzkNKxBvNqGt6W5rdjec51yqCbeLJdUCINRKHg0p4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=bHsiojhXi2dqgNmbHtzO1WAatEXC7o48XxScwkT80UP1piDQoQFc/o9So9BiN0gXY CkVh7kJl/3WVHmHngkHKOpph8IUrvvkqLKAT0HoK4GKh/0NsczJ8bDJnY8CexnyUj0 ylyD3pFotNpzWG1HmtL6jQ9kgr7/OltEap9USmfY= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 30 Nov 2021 05:38:14 +0200 Message-Id: <20211130033820.18235-17-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 Subject: [libcamera-devel] [PATCH v4 16/22] libcamera: v4l2_videodevice: Pass SharedFD to open() 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" The V4L2VideoDevice::open() function that takes an open file handle duplicates it internally, and leaves the original handle untouched. This is documented but not enforced through language constructs. Fix it by passing a SharedFD to the function. Signed-off-by: Laurent Pinchart Reviewed-by: Hirokazu Honda Reviewed-by: Jacopo Mondi --- include/libcamera/internal/v4l2_videodevice.h | 2 +- src/libcamera/v4l2_videodevice.cpp | 22 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 80b665771782..9f556f99587c 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -184,7 +184,7 @@ public: ~V4L2VideoDevice(); int open(); - int open(int handle, enum v4l2_buf_type type); + int open(FileDescriptor handle, enum v4l2_buf_type type); void close(); const char *driverName() const { return caps_.driver(); } diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 3966483a365f..5f1cc6e2f47f 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -612,16 +612,14 @@ int V4L2VideoDevice::open() * its type from the capabilities. This can be used to force a given device type * for memory-to-memory devices. * - * The file descriptor \a handle is duplicated, and the caller is responsible - * for closing the \a handle when it has no further use for it. The close() - * function will close the duplicated file descriptor, leaving \a handle - * untouched. + * The file descriptor \a handle is duplicated, no reference to the original + * handle is kept. * * \return 0 on success or a negative error code otherwise */ -int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type) +int V4L2VideoDevice::open(FileDescriptor handle, enum v4l2_buf_type type) { - UniqueFD newFd(dup(handle)); + UniqueFD newFd = handle.dup(); if (!newFd.isValid()) { LOG(V4L2, Error) << "Failed to duplicate file handle: " << strerror(errno); @@ -1900,12 +1898,10 @@ int V4L2M2MDevice::open() /* * The output and capture V4L2VideoDevice instances use the same file - * handle for the same device node. The local file handle can be closed - * as the V4L2VideoDevice::open() retains a handle by duplicating the - * fd passed in. + * handle for the same device node. */ - UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), - O_RDWR | O_NONBLOCK)); + FileDescriptor fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), + O_RDWR | O_NONBLOCK)); if (!fd.isValid()) { ret = -errno; LOG(V4L2, Error) << "Failed to open V4L2 M2M device: " @@ -1913,11 +1909,11 @@ int V4L2M2MDevice::open() return ret; } - ret = output_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); + ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); if (ret) goto err; - ret = capture_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); + ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); if (ret) goto err;