{"id":11942,"url":"https://patchwork.libcamera.org/api/patches/11942/?format=json","web_url":"https://patchwork.libcamera.org/patch/11942/","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-6-hiroh@chromium.org>","date":"2021-04-15T08:38:39","name":"[libcamera-devel,RFC,06/10] libcamera: V4L2Device: Use ScopedFD for a file descriptor","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"940815ba7549fff3923c6af172e16347a674fe06","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/11942/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/11942/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11942/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 77FF7BD224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 15 Apr 2021 08:38:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0C3BB6881C;\n\tThu, 15 Apr 2021 10:38:59 +0200 (CEST)","from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com\n\t[IPv6:2607:f8b0:4864:20::1032])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C6BB968820\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 10:38:57 +0200 (CEST)","by mail-pj1-x1032.google.com with SMTP id\n\tu14-20020a17090a1f0eb029014e38011b09so7463149pja.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 01:38:57 -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.54\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 15 Apr 2021 01:38:55 -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=\"MPhvuPdb\"; 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=EwK6TRKf8yCqOMBzpdZgZjAZpzEc6NbEHDy7mgMWlig=;\n\tb=MPhvuPdbJ+CARch9WDCy5Gg6zT6uHEfjaLohextw32D2Zh6gOQl46VDc4dfODSOZcE\n\tQRDm9tpXSxriaIQXlGJ2NmtD1x/ciGLnIB7Hs21B9fNd4yj5HmtNcxKlSCHaAGkv669w\n\tBA1WlbE4yFJebI37bRXiPKwhW+4x6oNwmq80Y=","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=EwK6TRKf8yCqOMBzpdZgZjAZpzEc6NbEHDy7mgMWlig=;\n\tb=t9qXRnLhifp0UIGWTe/nFjpir3rTBzH/sF21/YOxu6CcAKUyvIX3qH2u1ze/uZvItG\n\tafOKsUYY5S8HpIUt0yLUEbdZ+3KEeHg7R7MfdKkzS/s18bGsmDuQOahWFJIYdJn1MdGf\n\tmRafE1cHFy1v+giukVNG5TsQ8W8zFy4B6HnzltVdzZ4Wgwvph0fXjPGbkd4H7NDi0aRE\n\tCaQy1IA4PGJSLeogRLKlAT0pC7qk3IMW3iwlh84k7yIpceSbzzXiDs2Ziedpy1IbFJHs\n\tSwRlVMTVs9pZcbXG+9NODHUwNXfE40H9tR46GSXqyrXRq7z+FlO5vzI1wT3TmfaYgfzJ\n\tmJ/g==","X-Gm-Message-State":"AOAM533iX1517frj2vwOR9Uo16GSp4lmYUXAlQ0/3wynIq/t/Ws03VkM\n\tmPGZvtzK2PAdwQdlcEXx3wrEeo8bqm9mcg==","X-Google-Smtp-Source":"ABdhPJwvwUlZ2/SHBRiYiLoVvFkLv7egxrBLWiS1wJ0s/KrYFgrjBnLvVZLqdFjZozeSh+qM5neKnA==","X-Received":"by 2002:a17:902:760b:b029:e9:8bda:ae44 with SMTP id\n\tk11-20020a170902760bb02900e98bdaae44mr2747202pll.26.1618475936125; \n\tThu, 15 Apr 2021 01:38:56 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","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\n\tScopedFD for a file descriptor","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":"V4L2Device owns a file descriptor for a v4l2 device node. It\nshould be managed by ScopedFD avoid the leakage.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n include/libcamera/internal/v4l2_device.h |  9 +++++----\n src/libcamera/v4l2_device.cpp            | 17 +++++++----------\n src/libcamera/v4l2_videodevice.cpp       |  3 ++-\n 3 files changed, 14 insertions(+), 15 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\nindex d006bf68..e0262de0 100644\n--- a/include/libcamera/internal/v4l2_device.h\n+++ b/include/libcamera/internal/v4l2_device.h\n@@ -13,6 +13,7 @@\n \n #include <linux/videodev2.h>\n \n+#include <libcamera/scoped_file_descriptor.h>\n #include <libcamera/signal.h>\n \n #include \"libcamera/internal/log.h\"\n@@ -26,7 +27,7 @@ class V4L2Device : protected Loggable\n {\n public:\n \tvoid close();\n-\tbool isOpen() const { return fd_ != -1; }\n+\tbool isOpen() const { return fd_.isValid(); }\n \n \tconst ControlInfoMap &controls() const { return controls_; }\n \n@@ -46,11 +47,11 @@ protected:\n \t~V4L2Device();\n \n \tint open(unsigned int flags);\n-\tint setFd(int fd);\n+\tint setFd(ScopedFD fd);\n \n \tint ioctl(unsigned long request, void *argp);\n \n-\tint fd() const { return fd_; }\n+\tint fd() const { return fd_.get(); }\n \n private:\n \tvoid listControls();\n@@ -64,7 +65,7 @@ private:\n \tstd::vector<std::unique_ptr<V4L2ControlId>> controlIds_;\n \tControlInfoMap controls_;\n \tstd::string deviceNode_;\n-\tint fd_;\n+\tScopedFD fd_;\n \n \tEventNotifier *fdEventNotifier_;\n \tbool frameStartEnabled_;\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex decd19ef..4fbb2d60 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -53,7 +53,7 @@ LOG_DEFINE_CATEGORY(V4L2)\n  * at open() time, and the \\a logTag to prefix log messages with.\n  */\n V4L2Device::V4L2Device(const std::string &deviceNode)\n-\t: deviceNode_(deviceNode), fd_(-1), fdEventNotifier_(nullptr),\n+\t: deviceNode_(deviceNode), fdEventNotifier_(nullptr),\n \t  frameStartEnabled_(false)\n {\n }\n@@ -89,7 +89,7 @@ int V4L2Device::open(unsigned int flags)\n \t\treturn ret;\n \t}\n \n-\tsetFd(ret);\n+\tsetFd(ScopedFD(ret));\n \n \tlistControls();\n \n@@ -112,14 +112,14 @@ int V4L2Device::open(unsigned int flags)\n  *\n  * \\return 0 on success or a negative error code otherwise\n  */\n-int V4L2Device::setFd(int fd)\n+int V4L2Device::setFd(ScopedFD fd)\n {\n \tif (isOpen())\n \t\treturn -EBUSY;\n \n-\tfd_ = fd;\n+\tfd_ = std::move(fd);\n \n-\tfdEventNotifier_ = new EventNotifier(fd_, EventNotifier::Exception);\n+\tfdEventNotifier_ = new EventNotifier(fd_.get(), EventNotifier::Exception);\n \tfdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable);\n \tfdEventNotifier_->setEnabled(false);\n \n@@ -138,10 +138,7 @@ void V4L2Device::close()\n \n \tdelete fdEventNotifier_;\n \n-\tif (::close(fd_) < 0)\n-\t\tLOG(V4L2, Error) << \"Failed to close V4L2 device: \"\n-\t\t\t\t << strerror(errno);\n-\tfd_ = -1;\n+\tfd_.reset();\n }\n \n /**\n@@ -449,7 +446,7 @@ int V4L2Device::ioctl(unsigned long request, void *argp)\n \t * Printing out an error message is usually better performed\n \t * in the caller, which can provide more context.\n \t */\n-\tif (::ioctl(fd_, request, argp) < 0)\n+\tif (::ioctl(fd_.get(), request, argp) < 0)\n \t\treturn -errno;\n \n \treturn 0;\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 12c09dc7..0bf3b5f5 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -22,6 +22,7 @@\n #include <linux/version.h>\n \n #include <libcamera/file_descriptor.h>\n+#include <libcamera/scoped_file_descriptor.h>\n \n #include \"libcamera/internal/event_notifier.h\"\n #include \"libcamera/internal/log.h\"\n@@ -614,7 +615,7 @@ int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type)\n \t\treturn ret;\n \t}\n \n-\tret = V4L2Device::setFd(newFd);\n+\tret = V4L2Device::setFd(ScopedFD(newFd));\n \tif (ret < 0) {\n \t\tLOG(V4L2, Error) << \"Failed to set file handle: \"\n \t\t\t\t << strerror(-ret);\n","prefixes":["libcamera-devel","RFC","06/10"]}