From patchwork Thu Jun 10 07:50:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12539 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 3AE89BD78E for ; Thu, 10 Jun 2021 07:50:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EAA8768934; Thu, 10 Jun 2021 09:50:45 +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="HOcrrXoZ"; dkim-atps=neutral Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 86E846893C for ; Thu, 10 Jun 2021 09:50:44 +0200 (CEST) Received: by mail-pl1-x635.google.com with SMTP id c13so564215plz.0 for ; Thu, 10 Jun 2021 00:50:44 -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=U2Uz92Hpntcbdb7pyiruOQFKuoRpeTuSvF+dVCQEjvY=; b=HOcrrXoZN+1VkomhnBzhbDt7sUGY6tLcnzvy9GA679T/NrNi7Pi2gGCKiGwdriDeVY uVwhgZYcYz4zkDt0ugwk3i8/ig1ZPceyG6eQA576yP28crq7dekPeOHlXSvY6+n2+6kH YkRW+YGV1HfOhQPtlTg5mJBUnavgHcALtTDCc= 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=U2Uz92Hpntcbdb7pyiruOQFKuoRpeTuSvF+dVCQEjvY=; b=FsHwramElgzMlBet8Md3LOY/L8gOLfvpfTyjNp43BkCtTT5UYUL0BEcVqV7B1ptLlD 2oUjr0G67vshAhYSR60dHJPU1fMngtsnPh9bt3iX56ZOE15dhUMgFdM3bWWpzBQ6OMdY KoLszl8NChkEMDDY3SRNJ56d+uu1re7Wz60mNawVV7N6zlS8JJwFbNFg0JYVMvUNhdbL EqnJQCpUT/a6m9pZEMW0Nf0mqOUC5JNH+JDpoDpCljIX5AuPPnMmXFxCbV2KSg/IabhO XC+++zctNIYSlfyir7e9n0gVlqmwVDPHDB4FXYPMiHL2xwyzZVWwVV5Ja7l9nfbvyfJX sVrw== X-Gm-Message-State: AOAM532iocB9uiA+xqxLSmVdj8Wb0oJjnPlqSoWY8khjtHRwknazEj1C rXs69WqqW0LWEcsErKNY1LBpXzBGrz6lHg== X-Google-Smtp-Source: ABdhPJyrfn3JoJCD5OxlyxWjGnvtmZCY7VFBzQFVt04dzq785EaNqUQGcK/i0d9fym4FxGEiC7/XTA== X-Received: by 2002:a17:90a:9bc4:: with SMTP id b4mr2045550pjw.42.1623311442897; Thu, 10 Jun 2021 00:50:42 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:98e0:b356:1c8a:25d4]) by smtp.gmail.com with ESMTPSA id d66sm1565161pfa.32.2021.06.10.00.50.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:42 -0700 (PDT) From: Hirokazu Honda 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 Subject: [libcamera-devel] [PATCH v2 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" Manages a file descriptor owned by V4L2Device for a v4l2 device node by ScopedFD. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_device.h | 9 ++++---- src/libcamera/v4l2_device.cpp | 26 ++++++++++-------------- src/libcamera/v4l2_videodevice.cpp | 15 ++++++-------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index b82e2a14..1d9bc506 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -27,7 +28,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_; } @@ -49,11 +50,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(); @@ -66,7 +67,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 b39c6266..c6c36359 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -119,7 +119,7 @@ ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl) * 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) { } @@ -147,15 +147,14 @@ int V4L2Device::open(unsigned int flags) return -EBUSY; } - int ret = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags); - if (ret < 0) { - ret = -errno; + ScopedFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags)); + if (!fd.isValid()) { LOG(V4L2, Error) << "Failed to open V4L2 device: " - << strerror(-ret); - return ret; + << strerror(errno); + return errno; } - setFd(ret); + setFd(std::move(fd)); listControls(); @@ -178,14 +177,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); @@ -204,10 +203,7 @@ void V4L2Device::close() delete fdEventNotifier_; - if (::close(fd_) < 0) - LOG(V4L2, Error) << "Failed to close V4L2 device: " - << strerror(errno); - fd_ = -1; + fd_.reset(); } /** @@ -506,7 +502,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..ce6ad8b5 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" @@ -603,22 +604,18 @@ int V4L2VideoDevice::open() */ int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type) { - int ret; - int newFd; - newFd = dup(handle); - if (newFd < 0) { - ret = -errno; + ScopedFD newFd(dup(handle)); + if (!newFd.isValid()) { LOG(V4L2, Error) << "Failed to duplicate file handle: " - << strerror(-ret); - return ret; + << strerror(errno); + return -errno; } - ret = V4L2Device::setFd(newFd); + int ret = V4L2Device::setFd(std::move(newFd)); if (ret < 0) { LOG(V4L2, Error) << "Failed to set file handle: " << strerror(-ret); - ::close(newFd); return ret; }