From patchwork Thu Apr 15 08:38:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11942 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 77FF7BD224 for ; Thu, 15 Apr 2021 08:38:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0C3BB6881C; Thu, 15 Apr 2021 10:38:59 +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="MPhvuPdb"; dkim-atps=neutral Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C6BB968820 for ; Thu, 15 Apr 2021 10:38:57 +0200 (CEST) Received: by mail-pj1-x1032.google.com with SMTP id u14-20020a17090a1f0eb029014e38011b09so7463149pja.5 for ; Thu, 15 Apr 2021 01:38:57 -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=EwK6TRKf8yCqOMBzpdZgZjAZpzEc6NbEHDy7mgMWlig=; b=MPhvuPdbJ+CARch9WDCy5Gg6zT6uHEfjaLohextw32D2Zh6gOQl46VDc4dfODSOZcE QRDm9tpXSxriaIQXlGJ2NmtD1x/ciGLnIB7Hs21B9fNd4yj5HmtNcxKlSCHaAGkv669w BA1WlbE4yFJebI37bRXiPKwhW+4x6oNwmq80Y= 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=EwK6TRKf8yCqOMBzpdZgZjAZpzEc6NbEHDy7mgMWlig=; b=t9qXRnLhifp0UIGWTe/nFjpir3rTBzH/sF21/YOxu6CcAKUyvIX3qH2u1ze/uZvItG afOKsUYY5S8HpIUt0yLUEbdZ+3KEeHg7R7MfdKkzS/s18bGsmDuQOahWFJIYdJn1MdGf mRafE1cHFy1v+giukVNG5TsQ8W8zFy4B6HnzltVdzZ4Wgwvph0fXjPGbkd4H7NDi0aRE CaQy1IA4PGJSLeogRLKlAT0pC7qk3IMW3iwlh84k7yIpceSbzzXiDs2Ziedpy1IbFJHs SwRlVMTVs9pZcbXG+9NODHUwNXfE40H9tR46GSXqyrXRq7z+FlO5vzI1wT3TmfaYgfzJ mJ/g== X-Gm-Message-State: AOAM533iX1517frj2vwOR9Uo16GSp4lmYUXAlQ0/3wynIq/t/Ws03VkM mPGZvtzK2PAdwQdlcEXx3wrEeo8bqm9mcg== X-Google-Smtp-Source: ABdhPJwvwUlZ2/SHBRiYiLoVvFkLv7egxrBLWiS1wJ0s/KrYFgrjBnLvVZLqdFjZozeSh+qM5neKnA== X-Received: by 2002:a17:902:760b:b029:e9:8bda:ae44 with SMTP id k11-20020a170902760bb02900e98bdaae44mr2747202pll.26.1618475936125; Thu, 15 Apr 2021 01:38:56 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:55 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:39 +0900 Message-Id: <20210415083843.3399502-6-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 06/10] libcamera: V4L2Device: Use ScopedFD 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" V4L2Device owns a file descriptor for a v4l2 device node. It should be managed by ScopedFD avoid the leakage. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_device.h | 9 +++++---- src/libcamera/v4l2_device.cpp | 17 +++++++---------- src/libcamera/v4l2_videodevice.cpp | 3 ++- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index d006bf68..e0262de0 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -13,6 +13,7 @@ #include +#include #include #include "libcamera/internal/log.h" @@ -26,7 +27,7 @@ class V4L2Device : protected Loggable { public: void close(); - bool isOpen() const { return fd_ != -1; } + bool isOpen() const { return fd_.isValid(); } const ControlInfoMap &controls() const { return controls_; } @@ -46,11 +47,11 @@ protected: ~V4L2Device(); int open(unsigned int flags); - int setFd(int fd); + int setFd(ScopedFD fd); int ioctl(unsigned long request, void *argp); - int fd() const { return fd_; } + int fd() const { return fd_.get(); } private: void listControls(); @@ -64,7 +65,7 @@ private: std::vector> controlIds_; ControlInfoMap controls_; std::string deviceNode_; - int fd_; + ScopedFD fd_; EventNotifier *fdEventNotifier_; bool frameStartEnabled_; diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index decd19ef..4fbb2d60 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -53,7 +53,7 @@ LOG_DEFINE_CATEGORY(V4L2) * at open() time, and the \a logTag to prefix log messages with. */ V4L2Device::V4L2Device(const std::string &deviceNode) - : deviceNode_(deviceNode), fd_(-1), fdEventNotifier_(nullptr), + : deviceNode_(deviceNode), fdEventNotifier_(nullptr), frameStartEnabled_(false) { } @@ -89,7 +89,7 @@ int V4L2Device::open(unsigned int flags) return ret; } - setFd(ret); + setFd(ScopedFD(ret)); listControls(); @@ -112,14 +112,14 @@ int V4L2Device::open(unsigned int flags) * * \return 0 on success or a negative error code otherwise */ -int V4L2Device::setFd(int fd) +int V4L2Device::setFd(ScopedFD fd) { if (isOpen()) return -EBUSY; - fd_ = fd; + fd_ = std::move(fd); - fdEventNotifier_ = new EventNotifier(fd_, EventNotifier::Exception); + fdEventNotifier_ = new EventNotifier(fd_.get(), EventNotifier::Exception); fdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable); fdEventNotifier_->setEnabled(false); @@ -138,10 +138,7 @@ void V4L2Device::close() delete fdEventNotifier_; - if (::close(fd_) < 0) - LOG(V4L2, Error) << "Failed to close V4L2 device: " - << strerror(errno); - fd_ = -1; + fd_.reset(); } /** @@ -449,7 +446,7 @@ int V4L2Device::ioctl(unsigned long request, void *argp) * Printing out an error message is usually better performed * in the caller, which can provide more context. */ - if (::ioctl(fd_, request, argp) < 0) + if (::ioctl(fd_.get(), request, argp) < 0) return -errno; return 0; diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 12c09dc7..0bf3b5f5 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -22,6 +22,7 @@ #include #include +#include #include "libcamera/internal/event_notifier.h" #include "libcamera/internal/log.h" @@ -614,7 +615,7 @@ int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type) return ret; } - ret = V4L2Device::setFd(newFd); + ret = V4L2Device::setFd(ScopedFD(newFd)); if (ret < 0) { LOG(V4L2, Error) << "Failed to set file handle: " << strerror(-ret);