From patchwork Thu Jun 10 07:50:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12534 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 97A07BD78E for ; Thu, 10 Jun 2021 07:50:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6991568936; Thu, 10 Jun 2021 09:50:38 +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="IpnhTORn"; dkim-atps=neutral Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A57C6602C5 for ; Thu, 10 Jun 2021 09:50:36 +0200 (CEST) Received: by mail-pf1-x429.google.com with SMTP id u126so904995pfu.13 for ; Thu, 10 Jun 2021 00:50:36 -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=VK/FliDppCrFRUYd6LB558bOEJnuwUUhkrgBuRhSYhg=; b=IpnhTORnWQ7xNYJdxDWxfErZyKzUIklfLh+NBVL4saeC+8WlWLtojYYw5Pg5dL9oqI EvzJ9JnCZh2zqIKHux65bL8sdrfWGBAEooaKigcYRCyhiDjFaWpweyuOpC52mIB+R9Kx EoARLxmMJyA6J7NApH3BOqgqw1/dNgv/g7oiE= 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=VK/FliDppCrFRUYd6LB558bOEJnuwUUhkrgBuRhSYhg=; b=cKCYSJeJbro++cE/6ZB1BzDK44NGtt1Z23k0MpXs4Rra+vcPbw23xepkRiCRjwdO8c x8hOs5bORfdi6RWTiErszNLquqi7/ldi0PJwahElEls/KtvmV2fq1iaLp9xFdcSuFoHG qlwcddaHRcEytvTQjUIbCd8TGel6l3M9OFNlygBzQ0AicqaMshP05H7CNXBdHz6f6L40 QInk4fqZVAZSD9monYqus40x03kI1JYJCf5sgy+0mzsTHohm5pfep2tYXYcZ63bKPaAW pxFK3KcfIzusxe38y4vFsTKB7obf1PsbiCoXSlwJ1kzvlylzmG/DJPyNoxvLHsHMuKrA 4dGA== X-Gm-Message-State: AOAM5317xGBsJTGVIAJOWWqjpndshG24WrVjK/e0EmcacTePMvCI7wW8 Q5R/rGXK8ngHF7gut0jKEkH8ialEnP4iqA== X-Google-Smtp-Source: ABdhPJyBF34RPS7l34siTXSNTbWMAvRSu489JUW1rSoRinLZ0ennB9tQeyEzVCRy4Dxczlrmsm9xhg== X-Received: by 2002:a63:ee11:: with SMTP id e17mr3711858pgi.323.1623311434901; Thu, 10 Jun 2021 00:50:34 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:34 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:18 +0900 Message-Id: <20210610075027.523672-2-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 01/10] libcamera: ScopedFD: Introduce 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" This introduces ScopedFD. It acts like unique_ptr to a file descriptor. Signed-off-by: Hirokazu Honda --- include/libcamera/file_descriptor.h | 3 + include/libcamera/meson.build | 1 + include/libcamera/scoped_fd.h | 36 ++++++++ src/libcamera/file_descriptor.cpp | 20 +++++ src/libcamera/meson.build | 1 + src/libcamera/scoped_fd.cpp | 129 ++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 include/libcamera/scoped_fd.h create mode 100644 src/libcamera/scoped_fd.cpp diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h index d514aac7..1d71d58c 100644 --- a/include/libcamera/file_descriptor.h +++ b/include/libcamera/file_descriptor.h @@ -11,11 +11,14 @@ namespace libcamera { +class ScopedFD; + class FileDescriptor final { public: explicit FileDescriptor(const int &fd = -1); explicit FileDescriptor(int &&fd); + explicit FileDescriptor(ScopedFD &&fd); FileDescriptor(const FileDescriptor &other); FileDescriptor(FileDescriptor &&other); ~FileDescriptor(); diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 086c958b..e1c8b253 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -15,6 +15,7 @@ libcamera_public_headers = files([ 'object.h', 'pixel_format.h', 'request.h', + 'scoped_fd.h', 'signal.h', 'span.h', 'stream.h', diff --git a/include/libcamera/scoped_fd.h b/include/libcamera/scoped_fd.h new file mode 100644 index 00000000..d91b53b4 --- /dev/null +++ b/include/libcamera/scoped_fd.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * scoped_fd.h - File descriptor wrapper that owns a file descriptor. + */ +#ifndef __LIBCAMERA_SCOPED_FD_H__ +#define __LIBCAMERA_SCOPED_FD_H__ + +#include +#include + +namespace libcamera { + +class ScopedFD final +{ +public: + explicit ScopedFD(const int fd = -1); + ~ScopedFD(); + ScopedFD(ScopedFD &&other); + ScopedFD &operator=(ScopedFD &&other); + + bool isValid() const { return fd_ == -1; } + int get() const { return fd_; } + void reset(int fd = -1); + __nodiscard int release(); + +private: + int fd_; + + LIBCAMERA_DISABLE_COPY(ScopedFD) +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_SCOPED_FD_H__ */ diff --git a/src/libcamera/file_descriptor.cpp b/src/libcamera/file_descriptor.cpp index 8b505ed3..4d30b757 100644 --- a/src/libcamera/file_descriptor.cpp +++ b/src/libcamera/file_descriptor.cpp @@ -11,6 +11,8 @@ #include #include +#include + #include "libcamera/internal/log.h" /** @@ -108,6 +110,24 @@ FileDescriptor::FileDescriptor(int &&fd) fd = -1; } +/** + * \brief Create a FileDescriptor taking ownership of a given ScopedFD \a fd + * \param[in] fd ScopedFD + * + * Construct a FileDescriptor from ScopedFD by taking ownership of the \a fd. + * The original \a fd becomes invalid. In particular, the caller shall not close + * the original \a fd manually. The duplicated file descriptor will be closed + * automatically when all FileDescriptor instances that reference it are + * destroyed. + * + * If the \a fd is negative, the FileDescriptor is constructed as invalid and + * the fd() method will return -1. + */ +FileDescriptor::FileDescriptor(ScopedFD &&fd) + : FileDescriptor(fd.release()) +{ +} + /** * \brief Copy constructor, create a FileDescriptor from a copy of \a other * \param[in] other The other FileDescriptor diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 7e19a177..ed311acf 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -44,6 +44,7 @@ libcamera_sources = files([ 'process.cpp', 'pub_key.cpp', 'request.cpp', + 'scoped_fd.cpp', 'semaphore.cpp', 'signal.cpp', 'stream.cpp', diff --git a/src/libcamera/scoped_fd.cpp b/src/libcamera/scoped_fd.cpp new file mode 100644 index 00000000..106386dd --- /dev/null +++ b/src/libcamera/scoped_fd.cpp @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * scoped_fd.cpp - File descriptor wrapper that owns a file descriptor. + */ + +#include + +#include + +#include "libcamera/internal/log.h" + +/** + * \file scoped_fd.h + * \brief File descriptor wrapper that owns a file descriptor + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(ScopedFD) + +/** + * \class ScopedFD + * \brief unique_ptr like wrapper for a file descriptor + * + * The ScopedFD provides RAII-style lifetime management of a file descriptor. + * It doesn't allow the shared ownership unlike FileDescriptor. It is + * constructed from a numerical file descriptor and takes over the ownership of + * the file descriptor. When the ScopedFD is destroyed, the managed file + * descriptor is closed. + */ + +/** + * \brief Create a ScopedFD taking over a given \a fd + * \param[in] fd a numerical file descriptor + * + * Construct a ScopedFD from a numerical file descriptor and take ownership of + * the file descriptor. The given file descriptor is automatically closed when + * the ScopedFD is destructed. + */ +ScopedFD::ScopedFD(const int fd) + : fd_(fd >= 0 ? fd : -1) +{ +} + +/** + * \brief Destroy the ScopedFD instance + * + * The owned file descriptor is automatically closed if it is valid. + */ +ScopedFD::~ScopedFD() +{ + reset(); +} + +/** + * \brief Move constructor, create a ScopedFD by taking over \a other + * \param[in] other The other ScopedFD + * + * Create a ScopedFD that takes the ownership of the file descriptor owned by \a + * other. Upon return, the \a other ScopedFD is invalid. + */ +ScopedFD::ScopedFD(ScopedFD &&other) + : fd_(other.release()) +{ +} + +/** + * \brief Move assignment operator, replace a ScopedFD by taking over \a other + * \param[in] other The other ScopedFD + * + * If this ScopedFD has a valid file descriptor, the file descriptor is closed + * first. The file descriptor is then replaced by the one of \a other. Upon + * return, \a other is invalid. + * + * \return A reference to this ScopedFD + */ +ScopedFD &ScopedFD::operator=(ScopedFD &&other) +{ + reset(other.release()); + + return *this; +} + +/** + * \fn ScopedFD::isValid() + * \brief Check if the ScopedFD has a valid file descriptor + * \return True if the ScopedFD has a valid file descriptor, false otherwise + */ + +/** + * \fn ScopedFD::get() + * \brief Retrieve the numerical file descriptor + * \return The numerical file descriptor + */ + +/** + * \fn ScopedFD::reset() + * \brief Swap the owned file descriptor with \a fd. The originally owned file + * descriptor is closed. + * \param[in] fd a numerical file descriptor + */ +void ScopedFD::reset(int fd) +{ + ASSERT(!isValid() || fd != fd_); + if (isValid()) + close(fd_); + fd_ = fd; +} + +/** + * \fn ScopedFD::release() + * \brief Release ownership of the file descriptor without closing it + * + * This function releases and returns the owned file descriptor without closing + * it. The caller owns the returned value and must take care of handling its + * life time to avoid file descriptor leakages. Upon return the ScopedFD is + * invalid. + * + * \return The numerical file descriptor + */ +int ScopedFD::release() +{ + int fd = fd_; + fd_ = -1; + return fd; +} +} /* namespace libcamera */ From patchwork Thu Jun 10 07:50:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12535 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 A75D4BD78E for ; Thu, 10 Jun 2021 07:50:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5646368938; Thu, 10 Jun 2021 09:50:40 +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="mboGGsJ/"; dkim-atps=neutral Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4FCD36892F for ; Thu, 10 Jun 2021 09:50:38 +0200 (CEST) Received: by mail-pg1-x52f.google.com with SMTP id y12so10083437pgk.6 for ; Thu, 10 Jun 2021 00:50:38 -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=pWxJ4OnA63T98e6zNE9xleVDI6Wpjy17l9FjAPBNIrM=; b=mboGGsJ/NLkz/ZfbnVWWukzzAbsb88fCfwU0/2U+0Uj+VTXJruJC4WljiDDHyVdvLo OOM0c+UbinNyCeK5IyvFbLhtWSczP6YrL+zSWDs8WyeiepfoCqcDtBCQO6jGslxFWP1h fGuCOTLMIa6QpsfEZ0ow9/pR6t2lWD60qrNMc= 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=pWxJ4OnA63T98e6zNE9xleVDI6Wpjy17l9FjAPBNIrM=; b=mg8Z7OwX5KnopH26FtcyYiWkRqazS3owG9s3lfVGnJf/Y6Q3u3jc+jXb4OilDwAlxH em1pSCJaghWUOUIEXZ1iGe0ylswd5QYksCj97u0NjXExv7hInI5pvxsIkIzRBVztCz58 BbkWTiClzsu6YJLvcEQMTh98yQ6M/d883rFCTPp4nn0yWzStJJC9rSDr06kszAm92rnI 5gsLqZ0Gd9NIQPg5fgKd/8ugnzHVONP/KxVv3tkKmip0uQQF7xaO0NqI3fnM92HBNdJz Fu6vV5qU1wN2yei6Y0ceoDxQBMBphgCYvm/KP9iJ0o5VtlZHXJHvk00MyzhQNVJfGPo/ E4+g== X-Gm-Message-State: AOAM533Vy3tJifADXNc12j8lzpAPLPquS2QQj3BtZq2W2yTWyRsFFIIO xcwUOW3UchK6q9/KzEvdQSZFpKk7NMKIRA== X-Google-Smtp-Source: ABdhPJz+IHnvo779Ik9vMJ3M32OPkrG+RoPAaQn+55gsZRWeE3f0j9nGUqVtDFf7GujJ/8VGTQW2GA== X-Received: by 2002:a63:f13:: with SMTP id e19mr3715702pgl.112.1623311436598; Thu, 10 Jun 2021 00:50:36 -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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:36 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:19 +0900 Message-Id: <20210610075027.523672-3-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 02/10] libcamera: EventDispatcherPoll: 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" Manages the event file descriptor owned by EventDispatcherPoll by ScopedFD. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/internal/event_dispatcher_poll.h | 4 +++- src/libcamera/event_dispatcher_poll.cpp | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/libcamera/internal/event_dispatcher_poll.h b/include/libcamera/internal/event_dispatcher_poll.h index 33de051d..a309bb7f 100644 --- a/include/libcamera/internal/event_dispatcher_poll.h +++ b/include/libcamera/internal/event_dispatcher_poll.h @@ -11,6 +11,8 @@ #include #include +#include + #include "libcamera/internal/event_dispatcher.h" struct pollfd; @@ -48,7 +50,7 @@ private: std::map notifiers_; std::list timers_; - int eventfd_; + ScopedFD eventfd_; bool processingEvents_; }; diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp index 456c6def..65e46b92 100644 --- a/src/libcamera/event_dispatcher_poll.cpp +++ b/src/libcamera/event_dispatcher_poll.cpp @@ -54,14 +54,13 @@ EventDispatcherPoll::EventDispatcherPoll() * Create the event fd. Failures are fatal as we can't implement an * interruptible dispatcher without the fd. */ - eventfd_ = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); - if (eventfd_ < 0) + eventfd_ = ScopedFD(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); + if (!eventfd_.isValid()) LOG(Event, Fatal) << "Unable to create eventfd"; } EventDispatcherPoll::~EventDispatcherPoll() { - close(eventfd_); } void EventDispatcherPoll::registerEventNotifier(EventNotifier *notifier) @@ -154,7 +153,7 @@ void EventDispatcherPoll::processEvents() for (auto notifier : notifiers_) pollfds.push_back({ notifier.first, notifier.second.events(), 0 }); - pollfds.push_back({ eventfd_, POLLIN, 0 }); + pollfds.push_back({ eventfd_.get(), POLLIN, 0 }); /* Wait for events and process notifiers and timers. */ do { @@ -176,7 +175,7 @@ void EventDispatcherPoll::processEvents() void EventDispatcherPoll::interrupt() { uint64_t value = 1; - ssize_t ret = write(eventfd_, &value, sizeof(value)); + ssize_t ret = write(eventfd_.get(), &value, sizeof(value)); if (ret != sizeof(value)) { if (ret < 0) ret = -errno; @@ -230,7 +229,7 @@ void EventDispatcherPoll::processInterrupt(const struct pollfd &pfd) return; uint64_t value; - ssize_t ret = read(eventfd_, &value, sizeof(value)); + ssize_t ret = read(eventfd_.get(), &value, sizeof(value)); if (ret != sizeof(value)) { if (ret < 0) ret = -errno; From patchwork Thu Jun 10 07:50:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12536 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 EEAFCBD78E for ; Thu, 10 Jun 2021 07:50:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B134A6892F; Thu, 10 Jun 2021 09:50:42 +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="Zu9rqh6W"; dkim-atps=neutral Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D1A3F6892C for ; Thu, 10 Jun 2021 09:50:39 +0200 (CEST) Received: by mail-pl1-x630.google.com with SMTP id c13so564121plz.0 for ; Thu, 10 Jun 2021 00:50:39 -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=wGjYAnenK+/K4jZT2CK5UkjRDJMREEYxA5ZPmQtkY20=; b=Zu9rqh6WFfme1hAlMKrD+318RVhKT0nP4VwbXpcvmXFGa6SGAs62ZEfz+3Sfajx+7G cUt11G7B+ImEHenbAUdPdVpDbDG41DC9ilJX8UjwWVOXo6zAw13C4wfGvRV6iFWpjJOr +9EhQjAJ3DRzlgSJxbLXc8Mj4wokjpSIUfwa0= 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=wGjYAnenK+/K4jZT2CK5UkjRDJMREEYxA5ZPmQtkY20=; b=agVrhdMb0A9ZvxvvSVSJxZMR8hH65Qu0IZ+JYyOewiDSxBNSj7UtSybyhAghZFvjmX x5DqCq0CWwbkQTqPVWWtcjdns1b/tIaOL7jIMRGyQUcbgXSY03AxyvIW/3oMtaTsOLoG XZFQmv/UfQ71LhAw0Z7CBmgI8iRLUMbmAYpjtZQ6UF8qKPxv8ZvRhpY1CC5kGRMWJ9gV PXCnzDF/JWYaa9gnWG24Vf2tSRuHyY0FpK3xZB9v9xgS5WaEfQgd5FGNRh+4TH0eVksv ch/69lAZJaTV2Q60nN8/Bv7yunBrx+Nmj8vmix3pPY1rtcZpci74ODVxwY9qNJNDI0Nh VEvA== X-Gm-Message-State: AOAM532bi5mmTVGLw6Ic6YcGvIUjKcQtMwuX5LX07ajmOS63cTkUUea/ tuvluELdsCCrY1Qk6vsArpch+LpiPg5wAw== X-Google-Smtp-Source: ABdhPJw61zk/sI7UWmq3rgO1tmNTWUb1X0UPYNgHH2Uh3rfADsGnSmuJcLEJ5zgA2g61kJ2Vg6SNVA== X-Received: by 2002:a17:90a:1141:: with SMTP id d1mr2093636pje.56.1623311438241; Thu, 10 Jun 2021 00:50:38 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:37 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:20 +0900 Message-Id: <20210610075027.523672-4-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 03/10] libcamera: File: 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" Manages the file descriptor owned by File by ScopedFD. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/internal/file.h | 5 +++-- src/libcamera/file.cpp | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h index f0b313a5..eff5da55 100644 --- a/include/libcamera/internal/file.h +++ b/include/libcamera/internal/file.h @@ -12,6 +12,7 @@ #include #include +#include #include namespace libcamera { @@ -40,7 +41,7 @@ public: bool exists() const; bool open(OpenMode mode); - bool isOpen() const { return fd_ != -1; } + bool isOpen() const { return fd_.isValid(); } OpenMode openMode() const { return mode_; } void close(); @@ -65,7 +66,7 @@ private: void unmapAll(); std::string name_; - int fd_; + ScopedFD fd_; OpenMode mode_; int error_; diff --git a/src/libcamera/file.cpp b/src/libcamera/file.cpp index bce2b613..cc9b2a8e 100644 --- a/src/libcamera/file.cpp +++ b/src/libcamera/file.cpp @@ -73,7 +73,7 @@ LOG_DEFINE_CATEGORY(File) * before performing I/O operations. */ File::File(const std::string &name) - : name_(name), fd_(-1), mode_(NotOpen), error_(0) + : name_(name), mode_(NotOpen), error_(0) { } @@ -84,7 +84,7 @@ File::File(const std::string &name) * setFileName(). */ File::File() - : fd_(-1), mode_(NotOpen), error_(0) + : mode_(NotOpen), error_(0) { } @@ -167,8 +167,8 @@ bool File::open(File::OpenMode mode) if (mode & WriteOnly) flags |= O_CREAT; - fd_ = ::open(name_.c_str(), flags, 0666); - if (fd_ < 0) { + fd_ = ScopedFD(::open(name_.c_str(), flags, 0666)); + if (!fd_.isValid()) { error_ = -errno; return false; } @@ -199,11 +199,10 @@ bool File::open(File::OpenMode mode) */ void File::close() { - if (fd_ == -1) + if (!fd_.isValid()) return; - ::close(fd_); - fd_ = -1; + fd_.reset(); mode_ = NotOpen; } @@ -233,7 +232,7 @@ ssize_t File::size() const return -EINVAL; struct stat st; - int ret = fstat(fd_, &st); + int ret = fstat(fd_.get(), &st); if (ret < 0) return -errno; @@ -252,7 +251,7 @@ off_t File::pos() const if (!isOpen()) return 0; - return lseek(fd_, 0, SEEK_CUR); + return lseek(fd_.get(), 0, SEEK_CUR); } /** @@ -266,7 +265,7 @@ off_t File::seek(off_t pos) if (!isOpen()) return -EINVAL; - off_t ret = lseek(fd_, pos, SEEK_SET); + off_t ret = lseek(fd_.get(), pos, SEEK_SET); if (ret < 0) return -errno; @@ -298,7 +297,7 @@ ssize_t File::read(const Span &data) /* Retry in case of interrupted system calls. */ while (readBytes < data.size()) { - ret = ::read(fd_, data.data() + readBytes, + ret = ::read(fd_.get(), data.data() + readBytes, data.size() - readBytes); if (ret <= 0) break; @@ -335,7 +334,7 @@ ssize_t File::write(const Span &data) /* Retry in case of interrupted system calls. */ while (writtenBytes < data.size()) { - ssize_t ret = ::write(fd_, data.data() + writtenBytes, + ssize_t ret = ::write(fd_.get(), data.data() + writtenBytes, data.size() - writtenBytes); if (ret <= 0) break; @@ -398,7 +397,7 @@ Span File::map(off_t offset, ssize_t size, enum File::MapFlag flags) if (flags & MapPrivate) prot |= PROT_WRITE; - void *map = mmap(NULL, size, prot, mmapFlags, fd_, offset); + void *map = mmap(NULL, size, prot, mmapFlags, fd_.get(), offset); if (map == MAP_FAILED) { error_ = -errno; return {}; From patchwork Thu Jun 10 07:50:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12537 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 5F275BD78E for ; Thu, 10 Jun 2021 07:50:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1C61868933; Thu, 10 Jun 2021 09:50:43 +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="ieiBDvEo"; dkim-atps=neutral Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 72B826892C for ; Thu, 10 Jun 2021 09:50:41 +0200 (CEST) Received: by mail-pl1-x62c.google.com with SMTP id 69so544513plc.5 for ; Thu, 10 Jun 2021 00:50:41 -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=6gV/JFiuoSpUYo+CBAqmrodOMqb0XKCJlOoRjDzT+TQ=; b=ieiBDvEobR8/U+OkkNbcKnKcQkg8ZzxPyfGG5vkg1wdkiy8ZYac2qcnBEIisNulBOC Q6ko3/eqfH8038H1i+bksR/6tqc047SvbKb+BlvUsvij/v0Sbh6i5T+cGLAnOcN7y67e oyqzpN79fvzj1+3zwkmmuKtn8jB2ORZOWdBFY= 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=6gV/JFiuoSpUYo+CBAqmrodOMqb0XKCJlOoRjDzT+TQ=; b=aBY9ZgNpbgELSOqafGTERRm8rusSMrbsbBVbtwHogKWeYMUEhGzjkucGAJq4Ma6VII hxKTe0T8AJsNJyCpXuTbDWIC2RYG4hBjiuk2Ru8UIZQOpDaiv+0pqWZ8Qx2mbx98CG9H mXfFzLV4JraI5K/UWpj3Ka1udrEhItnlsogxPvLJkE/CQk2HSya5NiJ9vBS9r/XMz8Ju 6mzF8yWNJiq3TW0j2U5vNeRSP3nLU7KJ9edxFCpRC69YVr/oeaIkj7hsMB+3tZFw9z5p zqsYZMi9xb/idP5TDpqBw6C7mbfV6y9zAmfrO9Nb9tuS1gOiJYmbzEsy+V+BVfhZL2k6 lZ1w== X-Gm-Message-State: AOAM533VGuf5JfN8QW80T/RXR0+15zKAPYyD0byMqM1GJE6oRr6+OWMi /EiUj1nV9CZnpLuL29rRX39pWURMoEVong== X-Google-Smtp-Source: ABdhPJx9T2puFhKU15M3drUUYLTjnfXQWkrcYebYMSmJdk7F9ML7fbQyslMgvnhE+BZtIYU5EvjmZw== X-Received: by 2002:a17:902:d64e:b029:ef:8b85:d299 with SMTP id y14-20020a170902d64eb02900ef8b85d299mr3713709plh.27.1623311439821; Thu, 10 Jun 2021 00:50:39 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:39 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:21 +0900 Message-Id: <20210610075027.523672-5-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 04/10] libcamera: Process: Manage pipe fds 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" Manages the file descriptors owned by Process for pipe by ScopedFDs. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/internal/process.h | 4 +++- src/libcamera/process.cpp | 16 ++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index 254cda85..027bb7da 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -11,6 +11,7 @@ #include #include +#include #include namespace libcamera { @@ -75,8 +76,9 @@ private: std::list processes_; struct sigaction oldsa_; + EventNotifier *sigEvent_; - int pipe_[2]; + ScopedFD pipe_[2]; }; } /* namespace libcamera */ diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 40a434a6..bb51d73b 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -69,7 +69,7 @@ void sigact(int signal, siginfo_t *info, void *ucontext) void ProcessManager::sighandler([[maybe_unused]] EventNotifier *notifier) { char data; - ssize_t ret = read(pipe_[0], &data, sizeof(data)); + ssize_t ret = read(pipe_[0].get(), &data, sizeof(data)); if (ret < 0) { LOG(Process, Error) << "Failed to read byte from signal handler pipe"; @@ -129,10 +129,15 @@ ProcessManager::ProcessManager() sigaction(SIGCHLD, &sa, NULL); - if (pipe2(pipe_, O_CLOEXEC | O_DIRECT | O_NONBLOCK)) + int pipe[2]; + if (pipe2(pipe, O_CLOEXEC | O_DIRECT | O_NONBLOCK)) LOG(Process, Fatal) << "Failed to initialize pipe for signal handling"; - sigEvent_ = new EventNotifier(pipe_[0], EventNotifier::Read); + + pipe_[0] = ScopedFD(pipe[0]); + pipe_[1] = ScopedFD(pipe[1]); + + sigEvent_ = new EventNotifier(pipe_[0].get(), EventNotifier::Read); sigEvent_->activated.connect(this, &ProcessManager::sighandler); self_ = this; @@ -141,9 +146,8 @@ ProcessManager::ProcessManager() ProcessManager::~ProcessManager() { sigaction(SIGCHLD, &oldsa_, NULL); + delete sigEvent_; - close(pipe_[0]); - close(pipe_[1]); self_ = nullptr; } @@ -170,7 +174,7 @@ ProcessManager *ProcessManager::instance() */ int ProcessManager::writePipe() const { - return pipe_[1]; + return pipe_[1].get(); } /** From patchwork Thu Jun 10 07:50:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12538 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 C0A66BD78E for ; Thu, 10 Jun 2021 07:50:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 84F5F6893B; Thu, 10 Jun 2021 09:50:44 +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="Hifbk8Sg"; dkim-atps=neutral Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 137D66892C for ; Thu, 10 Jun 2021 09:50:43 +0200 (CEST) Received: by mail-pl1-x62f.google.com with SMTP id 69so544549plc.5 for ; Thu, 10 Jun 2021 00:50:42 -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=j5NZZ4iXvafGwsvGWf4VPksAYgE7O1U8gS5PYWmf/To=; b=Hifbk8Sg5H13aBfwgVGJbOazl0dO0Q+V8lnXnmbUzVv8w3cuM6lXmzx9aG38gQtp8O K4ykIUVdtEs80ZzgEzv58lsNrO0rbRWcDrU0MwxK9a1vOK2/I6IEkzoUdtd2ap/rctaN HIq+T0UmO7eJP/FIEvvXxCZxO8zdMmMRx974Q= 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=j5NZZ4iXvafGwsvGWf4VPksAYgE7O1U8gS5PYWmf/To=; b=mPQ+ddOfPxV+Oh2nUiaUd96EyYmppW0fu8BngPugWxAYVR4kQZYGzve48CalzDPawi u6Wfmv937cru2thputc/VgfXsoXQBidLwNBvDONhg2SS028c6l++KU8tMh/0NJ9eRY95 /NdnHJROvgwcCQHzIqAvWBDTiOliTAvicZnWVX2l2EUEOkLmQY6knB73H8k6pKGgMcOp NXpySGSIpkxmHRRyMd94wzq3Y97onbikW3/MdG0EHrk77oce/RkCTI2xK8vRkQXPDcdf bG5vznXxwmLceQ3iK3X/aRATpHkPSxhQD+zMP91Waj8ot3EzA3eorr4Rg5eQPuMQmD7v cCFA== X-Gm-Message-State: AOAM533iUKOSpauMBJtEzPA6IL9cl/1hb7njgr9mLb1ncNOlVba+HGNy fSGUpL+kT7P9AlNHDcYNEX3FzUgiBxaBIQ== X-Google-Smtp-Source: ABdhPJz34NJJrhdNYei7z2tfdr5lM82BckRdYpZwC0sSnAXhOeSRY7HnIn7BlPZVpr9JAtUnyRKKBQ== X-Received: by 2002:a17:902:8c91:b029:ef:aa9a:af35 with SMTP id t17-20020a1709028c91b02900efaa9aaf35mr3549104plo.24.1623311441423; Thu, 10 Jun 2021 00:50:41 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:41 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:22 +0900 Message-Id: <20210610075027.523672-6-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 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" Manages a file descriptor owned by MediaDevice for a media device node by ScopedFD. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/media_device.h | 3 +- src/libcamera/media_device.cpp | 39 ++++++++++------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index c3292508..7722df06 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..1050bf6d 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,18 @@ 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; + fd_ = ScopedFD(::open(deviceNode_.c_str(), O_RDWR)); + if (!fd_.isValid()) { LOG(MediaDevice, Error) << "Failed to open media device at " - << deviceNode_ << ": " << strerror(-ret); - return ret; + << deviceNode_ << ": " << strerror(errno); + return -errno; } - fd_ = ret; return 0; } @@ -514,11 +511,7 @@ int MediaDevice::open() */ void MediaDevice::close() { - if (fd_ == -1) - return; - - ::close(fd_); - fd_ = -1; + fd_.reset(); } /** @@ -763,7 +756,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 +799,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) 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; } From patchwork Thu Jun 10 07:50:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12540 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 CF3B9BD78E for ; Thu, 10 Jun 2021 07:50:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 868A46893F; Thu, 10 Jun 2021 09:50:47 +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="jGjthLhx"; dkim-atps=neutral Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D13468937 for ; Thu, 10 Jun 2021 09:50:46 +0200 (CEST) Received: by mail-pl1-x631.google.com with SMTP id h1so552685plt.1 for ; Thu, 10 Jun 2021 00:50:45 -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=oQtmMIORVn3nv2jgxMAC+YN9+BInlyeVUONDYOxbT4c=; b=jGjthLhxxXG+cHZYuL00/PWiCJUvk/WHRTybUVMR4PQ3QWdicZQNGdcVNsJBAmdfB0 KLjIXTXLKRzTpfMstQW9hTayhtc0B3+47FErRpXdpeT8vuKH75uiPUzogo0gNDc535lM +cvoh4tejwz2/PnoQYMtmQo2EPiNWzpY+oWf0= 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=oQtmMIORVn3nv2jgxMAC+YN9+BInlyeVUONDYOxbT4c=; b=dxdFLHN/41o9DnPYSqgc7n+nq9DrQbERthUCiJVaSQYRiAGWDq5PoM6z4vgP1FD+0r J9tnJ08UJ/+nyWSDYI+6+PdX+wSq9NNPDs3awXlgfw2FeERu71ICpgkDpNkYby6c2hf4 rhxiWQkJ4m7RQN+I9QXswWQ0MOtqfZUhvKb34eQ6ftl2UzWzfdnl0is/EPU8wpXB0sTJ jIizIzXLX+6k3CSqMVKsfdXgtqwyFICEF9Uyxbl7mFbNQjBV+xC5Z1FGlISuJABGlFzp ipaBqtUtTqGFj13Zy0o/TrWZSg1w/YoFlOrKn0rqTjReqFPSlfzaUxNOAftq75FPDJrD CJmA== X-Gm-Message-State: AOAM53342/5uYw4S+8xUGijk9dmRlHFBev1jedXtQJrMYdl/9s3fg4VT se3i1KWInyzMEdV4zmq5hWUAp+UJHFZ5Ng== X-Google-Smtp-Source: ABdhPJw89MROU9tHGz+IdgyYP97mkw4gvLX+pQVvGI5hOAqUUUUHi7skxKWAD6U7SDzHUrN6Y479uA== X-Received: by 2002:a17:90a:f48c:: with SMTP id bx12mr2055707pjb.200.1623311444338; Thu, 10 Jun 2021 00:50:44 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:43 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:24 +0900 Message-Id: <20210610075027.523672-8-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 07/10] libcamera: V4L2VideoDevice: Use fd 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 file descriptors owned by V4L2VideoDevice by ScopedFD. This also changes the return type of exportDmabufFd to ScopedFD from FileDescriptor in order to represent a caller owns the returned file file descriptor. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_videodevice.h | 4 +-- src/libcamera/v4l2_videodevice.cpp | 33 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 7938343b..925a8e82 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -30,9 +30,9 @@ namespace libcamera { class EventNotifier; -class FileDescriptor; class MediaDevice; class MediaEntity; +class ScopedFD; struct V4L2Capability final : v4l2_capability { const char *driver() const @@ -235,7 +235,7 @@ private: int createBuffers(unsigned int count, std::vector> *buffers); std::unique_ptr createBuffer(unsigned int index); - FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); + ScopedFD exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(EventNotifier *notifier); FrameBuffer *dequeueBuffer(); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index ce6ad8b5..1dfb011c 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1274,12 +1274,12 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) std::vector planes; for (unsigned int nplane = 0; nplane < numPlanes; nplane++) { - FileDescriptor fd = exportDmabufFd(buf.index, nplane); + ScopedFD fd = exportDmabufFd(buf.index, nplane); if (!fd.isValid()) return nullptr; FrameBuffer::Plane plane; - plane.fd = std::move(fd); + plane.fd = FileDescriptor(fd.release()); plane.length = multiPlanar ? buf.m.planes[nplane].length : buf.length; @@ -1289,7 +1289,7 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) return std::make_unique(std::move(planes)); } -FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, +ScopedFD V4L2VideoDevice::exportDmabufFd(unsigned int index, unsigned int plane) { struct v4l2_exportbuffer expbuf = {}; @@ -1304,10 +1304,10 @@ FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index, if (ret < 0) { LOG(V4L2, Error) << "Failed to export buffer: " << strerror(-ret); - return FileDescriptor(); + return ScopedFD(); } - return FileDescriptor(std::move(expbuf.fd)); + return ScopedFD(expbuf.fd); } /** @@ -1699,39 +1699,32 @@ V4L2M2MDevice::~V4L2M2MDevice() */ int V4L2M2MDevice::open() { - int fd; - int ret; - /* * The output and capture V4L2VideoDevice instances use the same file * handle for the same device node. The local file handle can be closed * as the V4L2VideoDevice::open() retains a handle by duplicating the * fd passed in. */ - fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), - O_RDWR | O_NONBLOCK); - if (fd < 0) { - ret = -errno; - LOG(V4L2, Error) - << "Failed to open V4L2 M2M device: " << strerror(-ret); - return ret; + ScopedFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), + O_RDWR | O_NONBLOCK)); + if (!fd.isValid()) { + LOG(V4L2, Error) << "Failed to open V4L2 M2M device: " + << strerror(errno); + return -errno; } - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); + int ret = output_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); if (ret) goto err; - ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); + ret = capture_->open(fd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); if (ret) goto err; - ::close(fd); - return 0; err: close(); - ::close(fd); return ret; } From patchwork Thu Jun 10 07:50:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12541 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 2CD0EBD78E for ; Thu, 10 Jun 2021 07:50:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DDC9968931; Thu, 10 Jun 2021 09:50:48 +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="TOTW+fMc"; dkim-atps=neutral Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8817968940 for ; Thu, 10 Jun 2021 09:50:47 +0200 (CEST) Received: by mail-pl1-x62e.google.com with SMTP id e1so534519pld.13 for ; Thu, 10 Jun 2021 00:50:47 -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=L/ATuyWRbMv4FyvQrPLRiCOVFfyVOn3wfjrXN4gF/64=; b=TOTW+fMc3aSmxTDzaEmG7gZDduOLmW1Wz2JNRXsa9jTwRqbz94E6ht/Fh2p2T+agEE mH1lbET1qsLX2q2adOs80R3bIv75y9m0wPhgTGAEr6RlEFbHPZOCshZ4Kv89hn571wJe aHqlBhtOnfdJTHFyXSBi5Ucx9ne0cd12uwibo= 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=L/ATuyWRbMv4FyvQrPLRiCOVFfyVOn3wfjrXN4gF/64=; b=a0+tCTftwABmOp4IR1FKXKtJpffQxedwK4htZlIRnjlwUnEmScqxqyl0UQhFSz3uRJ pR4ELDU+1hzTS3Z4bEMZYiii1Q9h3JYzxCZWZnVqi+cM0GsN6krhjNz/4nd69Vt8JRr4 7YsbFpGMawoRa2xNp2uxSy/+6pu083xTFo4jN+79bF/ZReBs1rdVCOkVyilKnSVsd+yA Tl99aFPgHNct/qdMYZaV6YrBZ07yw1vaqZNziuZO8yztmBvBUBUPLec/XkN+TdcNJ0AS P6iGsee5m6PndUP6Vc8JU/IjCKFz/x7bO51YKYww5DaKoFhv9UoEezXuAEppSjiKYZcH 840A== X-Gm-Message-State: AOAM532FbdKQUtWqn9qG5Z9snQXtd0IR0FJCpcjbHi45gvYL1GXADuhQ qn0LiZI7WzO97vHOMQ/BP6HIXkBIQoXlbg== X-Google-Smtp-Source: ABdhPJygdFXsHxtmydDPEFYK1k3XL0ilkOOcQGIG/ghAuWhpsUlLycGetUK3WSk9NFKSRWAsVNJOxw== X-Received: by 2002:a17:90a:fd05:: with SMTP id cv5mr2007108pjb.24.1623311445822; Thu, 10 Jun 2021 00:50:45 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:45 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:25 +0900 Message-Id: <20210610075027.523672-9-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 08/10] libcamera: IPCUnixSocket: 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" IPCUnixSocket::create() creates two file descriptors. One of them is stored in IPCUnixSocket and the other is returned to a caller. This clarifies the ownership using ScopedFD. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/ipc_unixsocket.h | 5 +-- src/libcamera/ipc_pipe_unixsocket.cpp | 8 ++--- src/libcamera/ipc_unixsocket.cpp | 40 ++++++++++----------- test/ipc/unixsocket.cpp | 6 ++-- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/include/libcamera/internal/ipc_unixsocket.h b/include/libcamera/internal/ipc_unixsocket.h index e871b650..c620992b 100644 --- a/include/libcamera/internal/ipc_unixsocket.h +++ b/include/libcamera/internal/ipc_unixsocket.h @@ -12,6 +12,7 @@ #include #include +#include #include namespace libcamera { @@ -29,7 +30,7 @@ public: IPCUnixSocket(); ~IPCUnixSocket(); - int create(); + ScopedFD create(); int bind(int fd); void close(); bool isBound() const; @@ -50,7 +51,7 @@ private: void dataNotifier(EventNotifier *notifier); - int fd_; + ScopedFD fd_; bool headerReceived_; struct Header header_; EventNotifier *notifier_; diff --git a/src/libcamera/ipc_pipe_unixsocket.cpp b/src/libcamera/ipc_pipe_unixsocket.cpp index db0e260f..1997643f 100644 --- a/src/libcamera/ipc_pipe_unixsocket.cpp +++ b/src/libcamera/ipc_pipe_unixsocket.cpp @@ -30,14 +30,14 @@ IPCPipeUnixSocket::IPCPipeUnixSocket(const char *ipaModulePath, args.push_back(ipaModulePath); socket_ = std::make_unique(); - int fd = socket_->create(); - if (fd < 0) { + ScopedFD fd = socket_->create(); + if (!fd.isValid()) { LOG(IPCPipe, Error) << "Failed to create socket"; return; } socket_->readyRead.connect(this, &IPCPipeUnixSocket::readyRead); - args.push_back(std::to_string(fd)); - fds.push_back(fd); + args.push_back(std::to_string(fd.get())); + fds.push_back(fd.release()); proc_ = std::make_unique(); int ret = proc_->start(ipaProxyWorkerPath, args, fds); diff --git a/src/libcamera/ipc_unixsocket.cpp b/src/libcamera/ipc_unixsocket.cpp index fdb359f7..2da43188 100644 --- a/src/libcamera/ipc_unixsocket.cpp +++ b/src/libcamera/ipc_unixsocket.cpp @@ -68,7 +68,7 @@ LOG_DEFINE_CATEGORY(IPCUnixSocket) */ IPCUnixSocket::IPCUnixSocket() - : fd_(-1), headerReceived_(false), notifier_(nullptr) + : headerReceived_(false), notifier_(nullptr) { } @@ -86,9 +86,9 @@ IPCUnixSocket::~IPCUnixSocket() * the remote process, where it can be used with IPCUnixSocket::bind() to bind * the remote side socket. * - * \return A file descriptor on success, negative error code on failure + * \return A file descriptor. It is valid on success or invalid otherwise. */ -int IPCUnixSocket::create() +ScopedFD IPCUnixSocket::create() { int sockets[2]; int ret; @@ -98,14 +98,13 @@ int IPCUnixSocket::create() ret = -errno; LOG(IPCUnixSocket, Error) << "Failed to create socket pair: " << strerror(-ret); - return ret; + return ScopedFD(); } - ret = bind(sockets[0]); - if (ret) - return ret; + if (bind(sockets[0]) < 0) + return ScopedFD(); - return sockets[1]; + return ScopedFD(sockets[1]); } /** @@ -116,15 +115,18 @@ int IPCUnixSocket::create() * by the file descriptor \a fd. The file descriptor is obtained from the * IPCUnixSocket::create() method. * - * \return 0 on success or a negative error code otherwise + * \return 0 on success or a negative error code otherwise. + * + * \todo This argument should be ScopedFD because bind() takes over the + * ownership. */ int IPCUnixSocket::bind(int fd) { if (isBound()) return -EINVAL; - fd_ = fd; - notifier_ = new EventNotifier(fd_, EventNotifier::Read); + fd_ = ScopedFD(fd); + notifier_ = new EventNotifier(fd_.get(), EventNotifier::Read); notifier_->activated.connect(this, &IPCUnixSocket::dataNotifier); return 0; @@ -143,9 +145,7 @@ void IPCUnixSocket::close() delete notifier_; notifier_ = nullptr; - ::close(fd_); - - fd_ = -1; + fd_.reset(); headerReceived_ = false; } @@ -155,7 +155,7 @@ void IPCUnixSocket::close() */ bool IPCUnixSocket::isBound() const { - return fd_ != -1; + return fd_.isValid(); } /** @@ -182,7 +182,7 @@ int IPCUnixSocket::send(const Payload &payload) if (!hdr.data && !hdr.fds) return -EINVAL; - ret = ::send(fd_, &hdr, sizeof(hdr), 0); + ret = ::send(fd_.get(), &hdr, sizeof(hdr), 0); if (ret < 0) { ret = -errno; LOG(IPCUnixSocket, Error) @@ -262,7 +262,7 @@ int IPCUnixSocket::sendData(const void *buffer, size_t length, msg.msg_flags = 0; memcpy(CMSG_DATA(cmsg), fds, num * sizeof(uint32_t)); - if (sendmsg(fd_, &msg, 0) < 0) { + if (sendmsg(fd_.get(), &msg, 0) < 0) { int ret = -errno; LOG(IPCUnixSocket, Error) << "Failed to sendmsg: " << strerror(-ret); @@ -296,7 +296,7 @@ int IPCUnixSocket::recvData(void *buffer, size_t length, msg.msg_controllen = cmsg->cmsg_len; msg.msg_flags = 0; - if (recvmsg(fd_, &msg, 0) < 0) { + if (recvmsg(fd_.get(), &msg, 0) < 0) { int ret = -errno; if (ret != -EAGAIN) LOG(IPCUnixSocket, Error) @@ -315,7 +315,7 @@ void IPCUnixSocket::dataNotifier([[maybe_unused]] EventNotifier *notifier) if (!headerReceived_) { /* Receive the header. */ - ret = ::recv(fd_, &header_, sizeof(header_), 0); + ret = ::recv(fd_.get(), &header_, sizeof(header_), 0); if (ret < 0) { ret = -errno; LOG(IPCUnixSocket, Error) @@ -331,7 +331,7 @@ void IPCUnixSocket::dataNotifier([[maybe_unused]] EventNotifier *notifier) * readyRead signal. The notifier will be reenabled by the receive() * method. */ - struct pollfd fds = { fd_, POLLIN, 0 }; + struct pollfd fds = { fd_.get(), POLLIN, 0 }; ret = poll(&fds, 1, 0); if (ret < 0) return; diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp index 80157b34..9ca0467b 100644 --- a/test/ipc/unixsocket.cpp +++ b/test/ipc/unixsocket.cpp @@ -359,11 +359,11 @@ protected: int run() { - int slavefd = ipc_.create(); - if (slavefd < 0) + ScopedFD slavefd = ipc_.create(); + if (!slavefd.isValid()) return TestFail; - if (slaveStart(slavefd)) { + if (slaveStart(slavefd.release())) { cerr << "Failed to start slave" << endl; return TestFail; } From patchwork Thu Jun 10 07:50:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12542 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 83314BD78E for ; Thu, 10 Jun 2021 07:50:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4090268937; Thu, 10 Jun 2021 09:50:51 +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="MSpbbfKJ"; dkim-atps=neutral Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DEDE268933 for ; Thu, 10 Jun 2021 09:50:48 +0200 (CEST) Received: by mail-pj1-x1030.google.com with SMTP id k22-20020a17090aef16b0290163512accedso5053830pjz.0 for ; Thu, 10 Jun 2021 00:50:48 -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=tRd9oNMvJz1cb4+9G5xPCdGe7j9zeJmXlD0Yo4r3yXg=; b=MSpbbfKJY6zmdfEyj4Wa8Kho+TqhQYSFpfNe3dtvzQDUPyNZO2gwiXqRyGdhtByGr2 kIhYMuGB+4WGBKSmBj6MRpoxFqra619WJUYG62htl2Vj4AVtDf+Klo2Ir15KZPwjQf9d Oid0WJBxWT5hMYPRjpC3wnB5nK0wJmhsA1PKE= 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=tRd9oNMvJz1cb4+9G5xPCdGe7j9zeJmXlD0Yo4r3yXg=; b=Vhbk9cFKn0gSj4EOs0bzn73BbJ+ChYhXOrqQEI2+2AnJUDMq6NULDScqkGeoIjvvqe N2DsojB/tsOjGdmalni9CU4N0jWFveyQSeQo6KzEUV/bwQJxcyx3ng514S+lLBEaHbxt JejrqM45DcXnkC/5hZkOwbKg5pGXUV/HFWUSzOg4iP9XyxZ9ZqK/QID5UXLO6CUfvf9r 8nIy/bB87hV4OWIH7qftaWfSp0N4AaeVnajadc54u017Jt44pHqCT16ky9g+pIXTEjm/ kDWx2pvH1/tywuO8Lmd/2iR/MBpdHTmUvnYdPeI3zgkzh4pCJ29zHdhRBxBxs7psd/aW 6HBA== X-Gm-Message-State: AOAM5305K8MPNDGTdZHqEJTEkKfkG0GgRdwkyEFpVQ3ptZ5/5HuNvg5g FsTs4H41wtisQ2G59GP+74Crwyy7huf4Jw== X-Google-Smtp-Source: ABdhPJwIAnbFLQLhjqQWg94WPvOdJYNx2zU6Mz5toiRZ17BkIDDxHXsddd1IwyU8Eey7QOqPWi3l0Q== X-Received: by 2002:a17:90a:f015:: with SMTP id bt21mr2045899pjb.37.1623311447263; Thu, 10 Jun 2021 00:50:47 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:46 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:26 +0900 Message-Id: <20210610075027.523672-10-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 09/10] libcamera: pipeline: raspberrypi: DmaHeaps: 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 DmaHeaps for a dma handle by ScopedFD. Furthermore, DmaHeaps::alloc() creates a new file descriptor and the returned file descriptor is owned by a caller. This also clarifies it by changing the returned value to ScopedFD. Signed-off-by: Hirokazu Honda --- .../pipeline/raspberrypi/dma_heaps.cpp | 23 ++++++++----------- .../pipeline/raspberrypi/dma_heaps.h | 10 ++++---- .../pipeline/raspberrypi/raspberrypi.cpp | 7 ++++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp index 4d5dd6cb..d5b000b8 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp @@ -35,7 +35,6 @@ LOG_DECLARE_CATEGORY(RPI) namespace RPi { DmaHeap::DmaHeap() - : dmaHeapHandle_(-1) { for (const char *name : heapNames) { int ret = ::open(name, O_RDWR, 0); @@ -46,49 +45,47 @@ DmaHeap::DmaHeap() continue; } - dmaHeapHandle_ = ret; + dmaHeapHandle_ = ScopedFD(ret); break; } - if (dmaHeapHandle_ < 0) + if (!dmaHeapHandle_.isValid()) LOG(RPI, Error) << "Could not open any dmaHeap device"; } DmaHeap::~DmaHeap() { - if (dmaHeapHandle_ > -1) - ::close(dmaHeapHandle_); } -FileDescriptor DmaHeap::alloc(const char *name, std::size_t size) +ScopedFD DmaHeap::alloc(const char *name, std::size_t size) { int ret; if (!name) - return FileDescriptor(); + return ScopedFD(); struct dma_heap_allocation_data alloc = {}; alloc.len = size; alloc.fd_flags = O_CLOEXEC | O_RDWR; - ret = ::ioctl(dmaHeapHandle_, DMA_HEAP_IOCTL_ALLOC, &alloc); + ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); if (ret < 0) { LOG(RPI, Error) << "dmaHeap allocation failure for " << name; - return FileDescriptor(); + return ScopedFD(); } - ret = ::ioctl(alloc.fd, DMA_BUF_SET_NAME, name); + ScopedFD allocFd(alloc.fd); + ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name); if (ret < 0) { LOG(RPI, Error) << "dmaHeap naming failure for " << name; - ::close(alloc.fd); - return FileDescriptor(); + return ScopedFD(); } - return FileDescriptor(std::move(alloc.fd)); + return allocFd; } } /* namespace RPi */ diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h index 79f39c51..3a0765e6 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h +++ b/src/libcamera/pipeline/raspberrypi/dma_heaps.h @@ -7,7 +7,9 @@ #ifndef __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ #define __LIBCAMERA_PIPELINE_RASPBERRYPI_DMA_HEAPS_H__ -#include +#include + +#include namespace libcamera { @@ -18,11 +20,11 @@ class DmaHeap public: DmaHeap(); ~DmaHeap(); - bool isValid() const { return dmaHeapHandle_ > -1; } - FileDescriptor alloc(const char *name, std::size_t size); + bool isValid() const { return dmaHeapHandle_.isValid(); } + ScopedFD alloc(const char *name, std::size_t size); private: - int dmaHeapHandle_; + ScopedFD dmaHeapHandle_; }; } /* namespace RPi */ diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index a65b4568..13faa96e 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -1263,10 +1263,13 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config) /* Allocate the lens shading table via dmaHeap and pass to the IPA. */ if (!lsTable_.isValid()) { - lsTable_ = dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize); - if (!lsTable_.isValid()) + ScopedFD scopedFD = + dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize); + if (!scopedFD.isValid()) return -ENOMEM; + lsTable_ = FileDescriptor(std::move(scopedFD)); + /* Allow the IPA to mmap the LS table via the file descriptor. */ /* * \todo Investigate if mapping the lens shading table buffer From patchwork Thu Jun 10 07:50:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12543 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 E30D0C3214 for ; Thu, 10 Jun 2021 07:50:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 96C8C6892F; Thu, 10 Jun 2021 09:50:51 +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="nQqrcFxf"; dkim-atps=neutral Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6587F6892F for ; Thu, 10 Jun 2021 09:50:50 +0200 (CEST) Received: by mail-pf1-x42e.google.com with SMTP id c12so924385pfl.3 for ; Thu, 10 Jun 2021 00:50:50 -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=dOwdZ1a4Xmkt8I8dXzLynSQEvGuaDTE1Jt4nfltN2pE=; b=nQqrcFxfoOHPHxbM8u8GSBKmZ5SfYC32sYkITLiztcdepDzVXXk2MNb5gTNCFFeUzd HONn2mAEgzMuMkTXgkxwWWsqWXOSppQsQ9PTdsvcF4ln5anjAEBIxu+/kEaIBhcEGZTg pLDbYXDTUYRD14r773u07aFtmvs8EDSSr+qKI= 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=dOwdZ1a4Xmkt8I8dXzLynSQEvGuaDTE1Jt4nfltN2pE=; b=cmQ7w89/1rHDKa4mAIaPpznIWGdO1UshKFeLVNZ8Am3WQsHPbZnsigey2yQCYZfmmX jDhzVpllAjxyGlNciV+OPom2XNf2Leb1pBLHD07D0uhJV4oOO2XjLYHoGFqTBQ4cowXY BefNP85WHc2SrXi5AMlcuLtzEcroiM2y/4p/JhXPdpxYZgpieOm0vC4LKrxsGziuEaiR m/+7vmX4ehaZwd/Q+RmRLhYvyDS9YYB1wzRQmxjgmxjO+8A6Df4uJ16VqZmL7d+wMAUM ewWEZRZlMqUMnuPy5tCmb9dEI/eK7vRfJSeU6V0VUxeNuNfvSuqOjGE9QW6Ow+TzB3NU aJwg== X-Gm-Message-State: AOAM533mEv9fUR+EHlIVR3ZManKiMz7KqmGo8QV8HP+vhwHZFU47r6Jo wCHbXG3J8Xi3aPe42d6ZMfBpHONraIltlQ== X-Google-Smtp-Source: ABdhPJzBG00XUt7lVVSY46TK3mWroMT/loNmweLQsnFkpFxrubc88ehJcJb/EDD9LL7S7EUA2ZkYcA== X-Received: by 2002:a63:bc19:: with SMTP id q25mr3758086pge.211.1623311448772; Thu, 10 Jun 2021 00:50:48 -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.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:48 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:27 +0900 Message-Id: <20210610075027.523672-11-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 10/10] v4l2: V4L2Camera: Return int in getBufferFd() 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" V4L2Camera::getBufferFd() returns FileDescriptor. However, the file descriptor is still owned by V4L2Camera. It should rather return an integer to represent V4L2Camera doesn't have the ownership of the file descriptor. Signed-off-by: Hirokazu Honda --- src/v4l2/v4l2_camera.cpp | 6 +++--- src/v4l2/v4l2_camera.h | 2 +- src/v4l2/v4l2_camera_proxy.cpp | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp index 97825c71..cccc6749 100644 --- a/src/v4l2/v4l2_camera.cpp +++ b/src/v4l2/v4l2_camera.cpp @@ -186,16 +186,16 @@ void V4L2Camera::freeBuffers() bufferAllocator_->free(stream); } -FileDescriptor V4L2Camera::getBufferFd(unsigned int index) +int V4L2Camera::getBufferFd(unsigned int index) { Stream *stream = config_->at(0).stream(); const std::vector> &buffers = bufferAllocator_->buffers(stream); if (buffers.size() <= index) - return FileDescriptor(); + return -1; - return buffers[index]->planes()[0].fd; + return buffers[index]->planes()[0].fd.fd(); } int V4L2Camera::streamOn() diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h index d2380462..8efe1642 100644 --- a/src/v4l2/v4l2_camera.h +++ b/src/v4l2/v4l2_camera.h @@ -53,7 +53,7 @@ public: int allocBuffers(unsigned int count); void freeBuffers(); - FileDescriptor getBufferFd(unsigned int index); + int getBufferFd(unsigned int index); int streamOn(); int streamOff(); diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index f8bfe595..8f417421 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -112,14 +112,14 @@ void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags, return MAP_FAILED; } - FileDescriptor fd = vcam_->getBufferFd(index); - if (!fd.isValid()) { + int fd = vcam_->getBufferFd(index); + if (fd < 0) { errno = EINVAL; return MAP_FAILED; } void *map = V4L2CompatManager::instance()->fops().mmap(addr, length, prot, - flags, fd.fd(), 0); + flags, fd, 0); if (map == MAP_FAILED) return map;