{"id":12539,"url":"https://patchwork.libcamera.org/api/patches/12539/?format=json","web_url":"https://patchwork.libcamera.org/patch/12539/","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":"<20210610075027.523672-7-hiroh@chromium.org>","date":"2021-06-10T07:50:23","name":"[libcamera-devel,v2,06/10] libcamera: V4L2Device: Use ScopedFD for a file descriptor","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"c04bdbd7b04c6dc1abd2d1bddabc3c383dc19c5e","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/12539/mbox/","series":[{"id":2115,"url":"https://patchwork.libcamera.org/api/series/2115/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2115","date":"2021-06-10T07:50:17","name":"Introduce ScopedFD","version":2,"mbox":"https://patchwork.libcamera.org/series/2115/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12539/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12539/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 3AE89BD78E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Jun 2021 07:50:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EAA8768934;\n\tThu, 10 Jun 2021 09:50:45 +0200 (CEST)","from mail-pl1-x635.google.com (mail-pl1-x635.google.com\n\t[IPv6:2607:f8b0:4864:20::635])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 86E846893C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 09:50:44 +0200 (CEST)","by mail-pl1-x635.google.com with SMTP id c13so564215plz.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 00:50:44 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:203:98e0:b356:1c8a:25d4])\n\tby smtp.gmail.com with ESMTPSA id\n\td66sm1565161pfa.32.2021.06.10.00.50.41\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 10 Jun 2021 00:50:42 -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=\"HOcrrXoZ\"; 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=U2Uz92Hpntcbdb7pyiruOQFKuoRpeTuSvF+dVCQEjvY=;\n\tb=HOcrrXoZN+1VkomhnBzhbDt7sUGY6tLcnzvy9GA679T/NrNi7Pi2gGCKiGwdriDeVY\n\tuVwhgZYcYz4zkDt0ugwk3i8/ig1ZPceyG6eQA576yP28crq7dekPeOHlXSvY6+n2+6kH\n\tYkRW+YGV1HfOhQPtlTg5mJBUnavgHcALtTDCc=","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=U2Uz92Hpntcbdb7pyiruOQFKuoRpeTuSvF+dVCQEjvY=;\n\tb=FsHwramElgzMlBet8Md3LOY/L8gOLfvpfTyjNp43BkCtTT5UYUL0BEcVqV7B1ptLlD\n\t2oUjr0G67vshAhYSR60dHJPU1fMngtsnPh9bt3iX56ZOE15dhUMgFdM3bWWpzBQ6OMdY\n\tKoLszl8NChkEMDDY3SRNJ56d+uu1re7Wz60mNawVV7N6zlS8JJwFbNFg0JYVMvUNhdbL\n\tEqnJQCpUT/a6m9pZEMW0Nf0mqOUC5JNH+JDpoDpCljIX5AuPPnMmXFxCbV2KSg/IabhO\n\tXC+++zctNIYSlfyir7e9n0gVlqmwVDPHDB4FXYPMiHL2xwyzZVWwVV5Ja7l9nfbvyfJX\n\tsVrw==","X-Gm-Message-State":"AOAM532iocB9uiA+xqxLSmVdj8Wb0oJjnPlqSoWY8khjtHRwknazEj1C\n\trXs69WqqW0LWEcsErKNY1LBpXzBGrz6lHg==","X-Google-Smtp-Source":"ABdhPJyrfn3JoJCD5OxlyxWjGnvtmZCY7VFBzQFVt04dzq785EaNqUQGcK/i0d9fym4FxGEiC7/XTA==","X-Received":"by 2002:a17:90a:9bc4:: with SMTP id\n\tb4mr2045550pjw.42.1623311442897; \n\tThu, 10 Jun 2021 00:50:42 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 10 Jun 2021 16:50:23 +0900","Message-Id":"<20210610075027.523672-7-hiroh@chromium.org>","X-Mailer":"git-send-email 2.32.0.rc1.229.g3e70b5a671-goog","In-Reply-To":"<20210610075027.523672-1-hiroh@chromium.org>","References":"<20210610075027.523672-1-hiroh@chromium.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Manages a file descriptor owned by V4L2Device for a v4l2 device node\nby ScopedFD.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n include/libcamera/internal/v4l2_device.h |  9 ++++----\n src/libcamera/v4l2_device.cpp            | 26 ++++++++++--------------\n src/libcamera/v4l2_videodevice.cpp       | 15 ++++++--------\n 3 files changed, 22 insertions(+), 28 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\nindex b82e2a14..1d9bc506 100644\n--- a/include/libcamera/internal/v4l2_device.h\n+++ b/include/libcamera/internal/v4l2_device.h\n@@ -14,6 +14,7 @@\n #include <linux/videodev2.h>\n \n #include <libcamera/controls.h>\n+#include <libcamera/scoped_fd.h>\n #include <libcamera/signal.h>\n #include <libcamera/span.h>\n \n@@ -27,7 +28,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@@ -49,11 +50,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@@ -66,7 +67,7 @@ private:\n \tstd::vector<std::unique_ptr<ControlId>> 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 b39c6266..c6c36359 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -119,7 +119,7 @@ ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl)\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@@ -147,15 +147,14 @@ int V4L2Device::open(unsigned int flags)\n \t\treturn -EBUSY;\n \t}\n \n-\tint ret = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags);\n-\tif (ret < 0) {\n-\t\tret = -errno;\n+\tScopedFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags));\n+\tif (!fd.isValid()) {\n \t\tLOG(V4L2, Error) << \"Failed to open V4L2 device: \"\n-\t\t\t\t << strerror(-ret);\n-\t\treturn ret;\n+\t\t\t\t << strerror(errno);\n+\t\treturn errno;\n \t}\n \n-\tsetFd(ret);\n+\tsetFd(std::move(fd));\n \n \tlistControls();\n \n@@ -178,14 +177,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@@ -204,10 +203,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@@ -506,7 +502,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..ce6ad8b5 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_fd.h>\n \n #include \"libcamera/internal/event_notifier.h\"\n #include \"libcamera/internal/log.h\"\n@@ -603,22 +604,18 @@ int V4L2VideoDevice::open()\n  */\n int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type)\n {\n-\tint ret;\n-\tint newFd;\n \n-\tnewFd = dup(handle);\n-\tif (newFd < 0) {\n-\t\tret = -errno;\n+\tScopedFD newFd(dup(handle));\n+\tif (!newFd.isValid()) {\n \t\tLOG(V4L2, Error) << \"Failed to duplicate file handle: \"\n-\t\t\t\t << strerror(-ret);\n-\t\treturn ret;\n+\t\t\t\t << strerror(errno);\n+\t\treturn -errno;\n \t}\n \n-\tret = V4L2Device::setFd(newFd);\n+\tint ret = V4L2Device::setFd(std::move(newFd));\n \tif (ret < 0) {\n \t\tLOG(V4L2, Error) << \"Failed to set file handle: \"\n \t\t\t\t << strerror(-ret);\n-\t\t::close(newFd);\n \t\treturn ret;\n \t}\n \n","prefixes":["libcamera-devel","v2","06/10"]}