From patchwork Thu Apr 15 08:38:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11941 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 E39B5BD224 for ; Thu, 15 Apr 2021 08:38:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8F09B68819; Thu, 15 Apr 2021 10:38:58 +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="QL8iEkZI"; dkim-atps=neutral Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4704868814 for ; Thu, 15 Apr 2021 10:38:56 +0200 (CEST) Received: by mail-pf1-x430.google.com with SMTP id w6so1060042pfc.8 for ; Thu, 15 Apr 2021 01:38:56 -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=LKkKX4GMwLU2Dxbhn7PTkVZ+gEoDG/ROJiWihqhB7b4=; b=QL8iEkZI2IYGN2h3YWKByp6+FutPNjlM3pA2losM496fu/XOA2SqRl4LL6oHNuJzHP PlwqYQjKBxuG5kXJoN8690c6R5YGjJYnJXGLkPwvN/2NndPvUU7K+8qRLMgqgI4HweAt O1tuR+nQdryDRZxEztYMhez3a/OYRn1Lgg/JM= 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=LKkKX4GMwLU2Dxbhn7PTkVZ+gEoDG/ROJiWihqhB7b4=; b=M9Mt1Ik9BdBjkvHQtoeGRiQiBgZC/sI6EoGjbHI2Zm+x4d89P0pcAOQmaUFI7S4C7Y na9inwDU1yeBQFXdO0A+F86kCJontopXrrQ73HwNsMVFWBC1AHbAORGlh4gX9i4HkEVm NcnLYrBnXrtI2I3/ke5C9OLwOk7jCeLaRmEGaGLa5mZNnEoj6Pz4+IxVMuAe1yRe9MIV RjkyANaWF3q0HdztAGpGWlWcyQJhNPFXh87M6xs6etybRcge5galiJ96DDsPd7unM4hp WnR6JyvbMo92wh4ldDuvf07t/ccRfx7/k3a9FFNYvx0MsLpSDyfQd//hpw6XYXzEi8OS 1EFw== X-Gm-Message-State: AOAM532I0/sz0RkVYpy0l8NShOUILLKiPUDI5qllg6Go65F2Zqr308zp So33IWZZmxnzGJxcpIsff569L18fcAzrCw== X-Google-Smtp-Source: ABdhPJy4sW+NyxoFOm0ZosWnLiOrjRa8sTucBrbP9Q2mwe+OXZeecLFlbPQKzhQNHvhDm6lduqzYkw== X-Received: by 2002:a63:1f4d:: with SMTP id q13mr2441681pgm.453.1618475934731; Thu, 15 Apr 2021 01:38:54 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:54 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:38 +0900 Message-Id: <20210415083843.3399502-5-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 05/10] libcamera: MediaDevice: Manage fd by ScopedFD 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" MediaDevice owns a file descriptor for a media device node. It should be managed by ScopedFD to avoid the leakage. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/media_device.h | 3 ++- src/libcamera/media_device.cpp | 33 ++++++++++------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index c3292508..efdfbf36 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -14,6 +14,7 @@ #include +#include #include #include "libcamera/internal/log.h" @@ -82,7 +83,7 @@ private: unsigned int version_; unsigned int hwRevision_; - int fd_; + ScopedFD fd_; bool valid_; bool acquired_; bool lockOwner_; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 9ec84e56..ad5efbf6 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -63,15 +63,14 @@ LOG_DEFINE_CATEGORY(MediaDevice) * populate() before the media graph can be queried. */ MediaDevice::MediaDevice(const std::string &deviceNode) - : deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false), + : deviceNode_(deviceNode), valid_(false), acquired_(false), lockOwner_(false) { } MediaDevice::~MediaDevice() { - if (fd_ != -1) - ::close(fd_); + fd_.reset(); clear(); } @@ -143,14 +142,14 @@ void MediaDevice::release() */ bool MediaDevice::lock() { - if (fd_ == -1) + if (!fd_.isValid()) return false; /* Do not allow nested locking in the same libcamera instance. */ if (lockOwner_) return false; - if (lockf(fd_, F_TLOCK, 0)) + if (lockf(fd_.get(), F_TLOCK, 0)) return false; lockOwner_ = true; @@ -169,7 +168,7 @@ bool MediaDevice::lock() */ void MediaDevice::unlock() { - if (fd_ == -1) + if (!fd_.isValid()) return; if (!lockOwner_) @@ -177,7 +176,7 @@ void MediaDevice::unlock() lockOwner_ = false; - lockf(fd_, F_ULOCK, 0); + lockf(fd_.get(), F_ULOCK, 0); } /** @@ -220,7 +219,7 @@ int MediaDevice::populate() return ret; struct media_device_info info = {}; - ret = ioctl(fd_, MEDIA_IOC_DEVICE_INFO, &info); + ret = ioctl(fd_.get(), MEDIA_IOC_DEVICE_INFO, &info); if (ret) { ret = -errno; LOG(MediaDevice, Error) @@ -243,7 +242,7 @@ int MediaDevice::populate() topology.ptr_links = reinterpret_cast(links); topology.ptr_pads = reinterpret_cast(pads); - ret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology); + ret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &topology); if (ret < 0) { ret = -errno; LOG(MediaDevice, Error) @@ -481,20 +480,20 @@ int MediaDevice::disableLinks() */ int MediaDevice::open() { - if (fd_ != -1) { + if (!fd_.isValid()) { LOG(MediaDevice, Error) << "MediaDevice already open"; return -EBUSY; } int ret = ::open(deviceNode_.c_str(), O_RDWR); if (ret < 0) { - ret = -errno; LOG(MediaDevice, Error) << "Failed to open media device at " << deviceNode_ << ": " << strerror(-ret); return ret; } - fd_ = ret; + + fd_ = ScopedFD(ret); return 0; } @@ -514,11 +513,7 @@ int MediaDevice::open() */ void MediaDevice::close() { - if (fd_ == -1) - return; - - ::close(fd_); - fd_ = -1; + fd_.reset(); } /** @@ -763,7 +758,7 @@ void MediaDevice::fixupEntityFlags(struct media_v2_entity *entity) struct media_entity_desc desc = {}; desc.id = entity->id; - int ret = ioctl(fd_, MEDIA_IOC_ENUM_ENTITIES, &desc); + int ret = ioctl(fd_.get(), MEDIA_IOC_ENUM_ENTITIES, &desc); if (ret < 0) { ret = -errno; LOG(MediaDevice, Debug) @@ -806,7 +801,7 @@ int MediaDevice::setupLink(const MediaLink *link, unsigned int flags) linkDesc.flags = flags; - int ret = ioctl(fd_, MEDIA_IOC_SETUP_LINK, &linkDesc); + int ret = ioctl(fd_.get(), MEDIA_IOC_SETUP_LINK, &linkDesc); if (ret) { ret = -errno; LOG(MediaDevice, Error)