{"id":11941,"url":"https://patchwork.libcamera.org/api/patches/11941/?format=json","web_url":"https://patchwork.libcamera.org/patch/11941/","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-5-hiroh@chromium.org>","date":"2021-04-15T08:38:38","name":"[libcamera-devel,RFC,05/10] libcamera: MediaDevice: Manage fd by ScopedFD","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"a8c3b414cf139873aae4f45af618c98138d8dafa","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/11941/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/11941/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11941/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 E39B5BD224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 15 Apr 2021 08:38:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8F09B68819;\n\tThu, 15 Apr 2021 10:38:58 +0200 (CEST)","from mail-pf1-x430.google.com (mail-pf1-x430.google.com\n\t[IPv6:2607:f8b0:4864:20::430])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4704868814\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 10:38:56 +0200 (CEST)","by mail-pf1-x430.google.com with SMTP id w6so1060042pfc.8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 01:38:56 -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.53\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 15 Apr 2021 01:38:54 -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=\"QL8iEkZI\"; 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=LKkKX4GMwLU2Dxbhn7PTkVZ+gEoDG/ROJiWihqhB7b4=;\n\tb=QL8iEkZI2IYGN2h3YWKByp6+FutPNjlM3pA2losM496fu/XOA2SqRl4LL6oHNuJzHP\n\tPlwqYQjKBxuG5kXJoN8690c6R5YGjJYnJXGLkPwvN/2NndPvUU7K+8qRLMgqgI4HweAt\n\tO1tuR+nQdryDRZxEztYMhez3a/OYRn1Lgg/JM=","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=LKkKX4GMwLU2Dxbhn7PTkVZ+gEoDG/ROJiWihqhB7b4=;\n\tb=M9Mt1Ik9BdBjkvHQtoeGRiQiBgZC/sI6EoGjbHI2Zm+x4d89P0pcAOQmaUFI7S4C7Y\n\tna9inwDU1yeBQFXdO0A+F86kCJontopXrrQ73HwNsMVFWBC1AHbAORGlh4gX9i4HkEVm\n\tNcnLYrBnXrtI2I3/ke5C9OLwOk7jCeLaRmEGaGLa5mZNnEoj6Pz4+IxVMuAe1yRe9MIV\n\tRjkyANaWF3q0HdztAGpGWlWcyQJhNPFXh87M6xs6etybRcge5galiJ96DDsPd7unM4hp\n\tWnR6JyvbMo92wh4ldDuvf07t/ccRfx7/k3a9FFNYvx0MsLpSDyfQd//hpw6XYXzEi8OS\n\t1EFw==","X-Gm-Message-State":"AOAM532I0/sz0RkVYpy0l8NShOUILLKiPUDI5qllg6Go65F2Zqr308zp\n\tSo33IWZZmxnzGJxcpIsff569L18fcAzrCw==","X-Google-Smtp-Source":"ABdhPJy4sW+NyxoFOm0ZosWnLiOrjRa8sTucBrbP9Q2mwe+OXZeecLFlbPQKzhQNHvhDm6lduqzYkw==","X-Received":"by 2002:a63:1f4d:: with SMTP id\n\tq13mr2441681pgm.453.1618475934731; \n\tThu, 15 Apr 2021 01:38:54 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","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\n\tfd by ScopedFD","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":"MediaDevice owns a file descriptor for a media device node. It\nshould be managed by ScopedFD to avoid the leakage.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n include/libcamera/internal/media_device.h |  3 ++-\n src/libcamera/media_device.cpp            | 33 ++++++++++-------------\n 2 files changed, 16 insertions(+), 20 deletions(-)","diff":"diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h\nindex c3292508..efdfbf36 100644\n--- a/include/libcamera/internal/media_device.h\n+++ b/include/libcamera/internal/media_device.h\n@@ -14,6 +14,7 @@\n \n #include <linux/media.h>\n \n+#include <libcamera/scoped_file_descriptor.h>\n #include <libcamera/signal.h>\n \n #include \"libcamera/internal/log.h\"\n@@ -82,7 +83,7 @@ private:\n \tunsigned int version_;\n \tunsigned int hwRevision_;\n \n-\tint fd_;\n+\tScopedFD fd_;\n \tbool valid_;\n \tbool acquired_;\n \tbool lockOwner_;\ndiff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp\nindex 9ec84e56..ad5efbf6 100644\n--- a/src/libcamera/media_device.cpp\n+++ b/src/libcamera/media_device.cpp\n@@ -63,15 +63,14 @@ LOG_DEFINE_CATEGORY(MediaDevice)\n  * populate() before the media graph can be queried.\n  */\n MediaDevice::MediaDevice(const std::string &deviceNode)\n-\t: deviceNode_(deviceNode), fd_(-1), valid_(false), acquired_(false),\n+\t: deviceNode_(deviceNode), valid_(false), acquired_(false),\n \t  lockOwner_(false)\n {\n }\n \n MediaDevice::~MediaDevice()\n {\n-\tif (fd_ != -1)\n-\t\t::close(fd_);\n+\tfd_.reset();\n \tclear();\n }\n \n@@ -143,14 +142,14 @@ void MediaDevice::release()\n  */\n bool MediaDevice::lock()\n {\n-\tif (fd_ == -1)\n+\tif (!fd_.isValid())\n \t\treturn false;\n \n \t/* Do not allow nested locking in the same libcamera instance. */\n \tif (lockOwner_)\n \t\treturn false;\n \n-\tif (lockf(fd_, F_TLOCK, 0))\n+\tif (lockf(fd_.get(), F_TLOCK, 0))\n \t\treturn false;\n \n \tlockOwner_ = true;\n@@ -169,7 +168,7 @@ bool MediaDevice::lock()\n  */\n void MediaDevice::unlock()\n {\n-\tif (fd_ == -1)\n+\tif (!fd_.isValid())\n \t\treturn;\n \n \tif (!lockOwner_)\n@@ -177,7 +176,7 @@ void MediaDevice::unlock()\n \n \tlockOwner_ = false;\n \n-\tlockf(fd_, F_ULOCK, 0);\n+\tlockf(fd_.get(), F_ULOCK, 0);\n }\n \n /**\n@@ -220,7 +219,7 @@ int MediaDevice::populate()\n \t\treturn ret;\n \n \tstruct media_device_info info = {};\n-\tret = ioctl(fd_, MEDIA_IOC_DEVICE_INFO, &info);\n+\tret = ioctl(fd_.get(), MEDIA_IOC_DEVICE_INFO, &info);\n \tif (ret) {\n \t\tret = -errno;\n \t\tLOG(MediaDevice, Error)\n@@ -243,7 +242,7 @@ int MediaDevice::populate()\n \t\ttopology.ptr_links = reinterpret_cast<uintptr_t>(links);\n \t\ttopology.ptr_pads = reinterpret_cast<uintptr_t>(pads);\n \n-\t\tret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology);\n+\t\tret = ioctl(fd_.get(), MEDIA_IOC_G_TOPOLOGY, &topology);\n \t\tif (ret < 0) {\n \t\t\tret = -errno;\n \t\t\tLOG(MediaDevice, Error)\n@@ -481,20 +480,20 @@ int MediaDevice::disableLinks()\n  */\n int MediaDevice::open()\n {\n-\tif (fd_ != -1) {\n+\tif (!fd_.isValid()) {\n \t\tLOG(MediaDevice, Error) << \"MediaDevice already open\";\n \t\treturn -EBUSY;\n \t}\n \n \tint ret = ::open(deviceNode_.c_str(), O_RDWR);\n \tif (ret < 0) {\n-\t\tret = -errno;\n \t\tLOG(MediaDevice, Error)\n \t\t\t<< \"Failed to open media device at \"\n \t\t\t<< deviceNode_ << \": \" << strerror(-ret);\n \t\treturn ret;\n \t}\n-\tfd_ = ret;\n+\n+\tfd_ = ScopedFD(ret);\n \n \treturn 0;\n }\n@@ -514,11 +513,7 @@ int MediaDevice::open()\n  */\n void MediaDevice::close()\n {\n-\tif (fd_ == -1)\n-\t\treturn;\n-\n-\t::close(fd_);\n-\tfd_ = -1;\n+\tfd_.reset();\n }\n \n /**\n@@ -763,7 +758,7 @@ void MediaDevice::fixupEntityFlags(struct media_v2_entity *entity)\n \tstruct media_entity_desc desc = {};\n \tdesc.id = entity->id;\n \n-\tint ret = ioctl(fd_, MEDIA_IOC_ENUM_ENTITIES, &desc);\n+\tint ret = ioctl(fd_.get(), MEDIA_IOC_ENUM_ENTITIES, &desc);\n \tif (ret < 0) {\n \t\tret = -errno;\n \t\tLOG(MediaDevice, Debug)\n@@ -806,7 +801,7 @@ int MediaDevice::setupLink(const MediaLink *link, unsigned int flags)\n \n \tlinkDesc.flags = flags;\n \n-\tint ret = ioctl(fd_, MEDIA_IOC_SETUP_LINK, &linkDesc);\n+\tint ret = ioctl(fd_.get(), MEDIA_IOC_SETUP_LINK, &linkDesc);\n \tif (ret) {\n \t\tret = -errno;\n \t\tLOG(MediaDevice, Error)\n","prefixes":["libcamera-devel","RFC","05/10"]}