From patchwork Thu Apr 15 08:38:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11938 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 8DA9EBD224 for ; Thu, 15 Apr 2021 08:38:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 508556880B; Thu, 15 Apr 2021 10:38:54 +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="M1reJcFd"; dkim-atps=neutral Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4319168813 for ; Thu, 15 Apr 2021 10:38:52 +0200 (CEST) Received: by mail-pj1-x1029.google.com with SMTP id q14-20020a17090a430eb02901503aaee02bso1237278pjg.3 for ; Thu, 15 Apr 2021 01:38:52 -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=ixH+lzGrZPeeae5KvkgM+e04VdvZ3YsSbidPulfu7Hk=; b=M1reJcFd52JDdieFjSELynpJ2JxOibozZ051HJz/M4DbtMWrAKxC2xMq+Sf6Kn1kF3 1wdCuGRIyXxmkGoaSQFJuWKSWfiqAr7Cdtz/IQvH6JUfDidskI/a1smLgqzke2QyabIV YPB3pTUhrjvvVIs6Jd/jlpYZhlnrbNFMZ8VR0= 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=ixH+lzGrZPeeae5KvkgM+e04VdvZ3YsSbidPulfu7Hk=; b=COxSuYmyuxumNOJ4cVXl+fJTxsCwYPVGaoPDVcpU2UYq2xIlAXNI764KPSPfbp4xqX WEJCqa/tQ6CdalnUCiv9Sh0vk47E85Lalyx3zmhRADg9LCUCNeuipY9i4BRvniEUVtBd 75sKK0dGJYWeyg9Bf6+b9zEiOMhPbhlqwAoc41GD96ytQ3HhHpauOrZ+fHFAoOpjb0Qm ZcwdcZwJZ/58FCd6o4057DrENZb5gaZYzsnzvyMsHLFqE5UzNtfABPVOMueV+i9vRfvl 4TAa7Al24DGrGALgO4ktbQq7rkfUyuhQv701TkvPMHhZwvZciq/XOJyXjuEQdADK13ue J0HA== X-Gm-Message-State: AOAM530HEaFr7FixOeebDoX67YFpquHNe1zfKznLfVpG6tan0DTndDmM Tqu5TDDS7WkjPuxH6rtU/LniLDRERzWENw== X-Google-Smtp-Source: ABdhPJz3YWDD642xAC1Scbh6MDH4SMYbkxTpaEzR1krdW3HWWwU0p3+CthVSSdEALLQxRJBndF5h4Q== X-Received: by 2002:a17:902:f687:b029:e8:da63:6195 with SMTP id l7-20020a170902f687b02900e8da636195mr2537060plg.75.1618475930435; Thu, 15 Apr 2021 01:38:50 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:50 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:35 +0900 Message-Id: <20210415083843.3399502-2-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 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" EventDispatcherPoll owns the event file descriptor. This manages the fd with ScopedFD to avoid leakage. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart Reviewed-by: Laurent Pinchart --- include/libcamera/internal/event_dispatcher_poll.h | 4 +++- src/libcamera/event_dispatcher_poll.cpp | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/libcamera/internal/event_dispatcher_poll.h b/include/libcamera/internal/event_dispatcher_poll.h index 33de051d..b686183d 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..6b22a8a3 100644 --- a/src/libcamera/event_dispatcher_poll.cpp +++ b/src/libcamera/event_dispatcher_poll.cpp @@ -54,14 +54,14 @@ 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) + int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); + if (fd < 0) LOG(Event, Fatal) << "Unable to create eventfd"; + eventfd_ = ScopedFD(fd); } EventDispatcherPoll::~EventDispatcherPoll() { - close(eventfd_); } void EventDispatcherPoll::registerEventNotifier(EventNotifier *notifier) @@ -154,7 +154,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 +176,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 +230,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 Apr 15 08:38:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11939 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 8D850BD224 for ; Thu, 15 Apr 2021 08:38:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C41A568813; Thu, 15 Apr 2021 10:38:54 +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="FHptjf/b"; dkim-atps=neutral Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 742766880B for ; Thu, 15 Apr 2021 10:38:53 +0200 (CEST) Received: by mail-pf1-x42b.google.com with SMTP id w6so1059942pfc.8 for ; Thu, 15 Apr 2021 01:38:53 -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=DYcKT7iH73omu+2Qdx1U5wAzjkExITB+CoFUzylVeWM=; b=FHptjf/b49SClU9j3PfvlIF5TnGtF2l1MvAf+HjTgUbTDmC3CtomMFljyxzTNKTSrg EXfbAmQR9i+O/ji6mh61P3tzA3XcgUNUVIHOcGLKuC3yLYETipVJ42jAjjJw5TzQNWkS KwUiG2ouG7fDUZjisMX9DETuhRwcx2XeXBnT0= 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=DYcKT7iH73omu+2Qdx1U5wAzjkExITB+CoFUzylVeWM=; b=XtUJ5b50E3Q0qreMe3HhIMD+tupf1uTIEYOrVr8w9wYPDtSdhjpuzy1eL3U7Ffv4qJ ZNH3YB3mTNoiXVCpzqbCX6qL9IUtDk65MEJa7Fqiu8MGrXAlAjoPHoT0v01Tx8rRvkTW z9L587Lp5o2AkSgVLPhicFC4fhoM+0I2db6cBtELE+aDUION9ELJYFTzrtQ3bwS3nref 70Ze4PSMVyMa80MjNwuHfzWuEo6JxJQUmrbrosxem6YKSXyQc/An/bF8GYOyXVgIHYIx P835nNhz1OpSSXqLkdUSQ2iv15+u4LlgL+ZBHSjJDx5CPcQOAo60b9lrRwy+y5PeQ0WR jTcA== X-Gm-Message-State: AOAM531jzYiypXRiAy+peCsPZblnhHRD6SFiouE0D/s9BIlEpe/XU6Wk x6g+WC/A665EBm2dQCTdWtWESAxFPSUSdw== X-Google-Smtp-Source: ABdhPJz5F5JNTLAXeaYyluRk3HFp2mRvEyRSLLWKVLbWZ2naAYUHnFu/vMuJQ7ewqNXGtu1RgB7Z7g== X-Received: by 2002:a63:9dc5:: with SMTP id i188mr2501704pgd.191.1618475931862; Thu, 15 Apr 2021 01:38:51 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:51 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:36 +0900 Message-Id: <20210415083843.3399502-3-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 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" File owns the file descriptor for a file. It should be managed by ScopedFD to avoid the leakage. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- include/libcamera/internal/file.h | 5 +++-- src/libcamera/file.cpp | 26 +++++++++++++------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/file.h b/include/libcamera/internal/file.h index f0b313a5..e6eb7f04 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..4db9a8d3 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,12 +167,13 @@ bool File::open(File::OpenMode mode) if (mode & WriteOnly) flags |= O_CREAT; - fd_ = ::open(name_.c_str(), flags, 0666); - if (fd_ < 0) { + int fd = ::open(name_.c_str(), flags, 0666); + if (fd < 0) { error_ = -errno; return false; } + fd_ = ScopedFD(fd); mode_ = mode; error_ = 0; return true; @@ -199,11 +200,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 +233,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 +252,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 +266,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 +298,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 +335,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 +398,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 Apr 15 08:38:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11940 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 E1844BD224 for ; Thu, 15 Apr 2021 08:38:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A09D568817; Thu, 15 Apr 2021 10:38:57 +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="bZ7DyiSv"; dkim-atps=neutral Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E89DD68818 for ; Thu, 15 Apr 2021 10:38:54 +0200 (CEST) Received: by mail-pg1-x534.google.com with SMTP id p2so955693pgh.4 for ; Thu, 15 Apr 2021 01:38:54 -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=GwHyTdrZ7Hgaje7MCYsALSU4WjeRmENQdCEzaaNFQZs=; b=bZ7DyiSvPw8wr4pSwBP073Wj/UXM62nDUZWebkwRzQaNKSAQxrlJRexlw5H9UOlWsF SK5M4aiS56CgGzr1Dg0NZvxjqzBkadehZp5iHEl1zcc5iNKE0z2a7xzkqDmGYeZ5mpiO XdvblQrRQV2Shixg1KD0Bb1zhZCIuNhw8fJtQ= 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=GwHyTdrZ7Hgaje7MCYsALSU4WjeRmENQdCEzaaNFQZs=; b=l5J/vq5Sb1rBRZRaQ9sU8bf8IP18ycJPRjXvmLdB3EZuvcF7Mb5Kez2yGgpbFt6+cG 3j4ef2edsrUFB+7yvioVmJTTi0npWGyaLL7KI2emkvHnSRXIvElYb0Fwfdn6Ih2Nfej4 ZsZwQdD7EkDDu0X8pd/oG3FGQk/WHbdDGdBhmRXbY3ptvkM6eUii4i4rRODzseLFNRci UTIDmfHHKcCj8g6veUNkf40hN7RdPIF3t3ozaPZlGMrEGk5Ga5ncIRjN0fe+6e2yVCmg MPpeNu+qEiTEMvxLBg9HEHYJXYUp1wkk2Uug+qF4DwGW6GbZiRfq7hmzTroBP326cwhh 0z+w== X-Gm-Message-State: AOAM531a8Wv+Oq/zUhjtlqcFKInOJ5XGvNnDhS1X8SAxQ7CC10ptoL8G ncUNIhIiJmL1zP89KOfiSjT+ftJfgailQw== X-Google-Smtp-Source: ABdhPJy0k2INyRxVvAwkU0ng+5HBLInxvF3ZbU26EqH/wK36ckcabggQvQYTbE+oowlX7mqvZEs0Sg== X-Received: by 2002:a62:d417:0:b029:258:4e8a:ad06 with SMTP id a23-20020a62d4170000b02902584e8aad06mr721066pfh.68.1618475933243; Thu, 15 Apr 2021 01:38:53 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:52 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:37 +0900 Message-Id: <20210415083843.3399502-4-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 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" Process owns the file descriptors for pipe. They should be managed by ScopedFDs to avoid the leakage. 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..b3bb24df 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 Apr 15 08:38:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11941 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 E39B5BD224 for ; Thu, 15 Apr 2021 08:38:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8F09B68819; Thu, 15 Apr 2021 10:38:58 +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="QL8iEkZI"; dkim-atps=neutral Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4704868814 for ; Thu, 15 Apr 2021 10:38:56 +0200 (CEST) Received: by mail-pf1-x430.google.com with SMTP id w6so1060042pfc.8 for ; Thu, 15 Apr 2021 01:38:56 -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=LKkKX4GMwLU2Dxbhn7PTkVZ+gEoDG/ROJiWihqhB7b4=; b=QL8iEkZI2IYGN2h3YWKByp6+FutPNjlM3pA2losM496fu/XOA2SqRl4LL6oHNuJzHP PlwqYQjKBxuG5kXJoN8690c6R5YGjJYnJXGLkPwvN/2NndPvUU7K+8qRLMgqgI4HweAt O1tuR+nQdryDRZxEztYMhez3a/OYRn1Lgg/JM= 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=LKkKX4GMwLU2Dxbhn7PTkVZ+gEoDG/ROJiWihqhB7b4=; b=M9Mt1Ik9BdBjkvHQtoeGRiQiBgZC/sI6EoGjbHI2Zm+x4d89P0pcAOQmaUFI7S4C7Y na9inwDU1yeBQFXdO0A+F86kCJontopXrrQ73HwNsMVFWBC1AHbAORGlh4gX9i4HkEVm NcnLYrBnXrtI2I3/ke5C9OLwOk7jCeLaRmEGaGLa5mZNnEoj6Pz4+IxVMuAe1yRe9MIV RjkyANaWF3q0HdztAGpGWlWcyQJhNPFXh87M6xs6etybRcge5galiJ96DDsPd7unM4hp WnR6JyvbMo92wh4ldDuvf07t/ccRfx7/k3a9FFNYvx0MsLpSDyfQd//hpw6XYXzEi8OS 1EFw== X-Gm-Message-State: AOAM532I0/sz0RkVYpy0l8NShOUILLKiPUDI5qllg6Go65F2Zqr308zp So33IWZZmxnzGJxcpIsff569L18fcAzrCw== X-Google-Smtp-Source: ABdhPJy4sW+NyxoFOm0ZosWnLiOrjRa8sTucBrbP9Q2mwe+OXZeecLFlbPQKzhQNHvhDm6lduqzYkw== X-Received: by 2002:a63:1f4d:: with SMTP id q13mr2441681pgm.453.1618475934731; Thu, 15 Apr 2021 01:38:54 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:54 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:38 +0900 Message-Id: <20210415083843.3399502-5-hiroh@chromium.org> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog In-Reply-To: <20210415083843.3399502-1-hiroh@chromium.org> References: <20210415083843.3399502-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 05/10] libcamera: MediaDevice: Manage 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" MediaDevice owns a file descriptor for a media device node. It should be managed by ScopedFD to avoid the leakage. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/media_device.h | 3 ++- src/libcamera/media_device.cpp | 33 ++++++++++------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index c3292508..efdfbf36 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..ad5efbf6 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,20 @@ 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; LOG(MediaDevice, Error) << "Failed to open media device at " << deviceNode_ << ": " << strerror(-ret); return ret; } - fd_ = ret; + + fd_ = ScopedFD(ret); return 0; } @@ -514,11 +513,7 @@ int MediaDevice::open() */ void MediaDevice::close() { - if (fd_ == -1) - return; - - ::close(fd_); - fd_ = -1; + fd_.reset(); } /** @@ -763,7 +758,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 +801,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 Apr 15 08:38:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11942 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 77FF7BD224 for ; Thu, 15 Apr 2021 08:38:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0C3BB6881C; Thu, 15 Apr 2021 10:38:59 +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="MPhvuPdb"; dkim-atps=neutral Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C6BB968820 for ; Thu, 15 Apr 2021 10:38:57 +0200 (CEST) Received: by mail-pj1-x1032.google.com with SMTP id u14-20020a17090a1f0eb029014e38011b09so7463149pja.5 for ; Thu, 15 Apr 2021 01:38:57 -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=EwK6TRKf8yCqOMBzpdZgZjAZpzEc6NbEHDy7mgMWlig=; b=MPhvuPdbJ+CARch9WDCy5Gg6zT6uHEfjaLohextw32D2Zh6gOQl46VDc4dfODSOZcE QRDm9tpXSxriaIQXlGJ2NmtD1x/ciGLnIB7Hs21B9fNd4yj5HmtNcxKlSCHaAGkv669w BA1WlbE4yFJebI37bRXiPKwhW+4x6oNwmq80Y= 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=EwK6TRKf8yCqOMBzpdZgZjAZpzEc6NbEHDy7mgMWlig=; b=t9qXRnLhifp0UIGWTe/nFjpir3rTBzH/sF21/YOxu6CcAKUyvIX3qH2u1ze/uZvItG afOKsUYY5S8HpIUt0yLUEbdZ+3KEeHg7R7MfdKkzS/s18bGsmDuQOahWFJIYdJn1MdGf mRafE1cHFy1v+giukVNG5TsQ8W8zFy4B6HnzltVdzZ4Wgwvph0fXjPGbkd4H7NDi0aRE CaQy1IA4PGJSLeogRLKlAT0pC7qk3IMW3iwlh84k7yIpceSbzzXiDs2Ziedpy1IbFJHs SwRlVMTVs9pZcbXG+9NODHUwNXfE40H9tR46GSXqyrXRq7z+FlO5vzI1wT3TmfaYgfzJ mJ/g== X-Gm-Message-State: AOAM533iX1517frj2vwOR9Uo16GSp4lmYUXAlQ0/3wynIq/t/Ws03VkM mPGZvtzK2PAdwQdlcEXx3wrEeo8bqm9mcg== X-Google-Smtp-Source: ABdhPJwvwUlZ2/SHBRiYiLoVvFkLv7egxrBLWiS1wJ0s/KrYFgrjBnLvVZLqdFjZozeSh+qM5neKnA== X-Received: by 2002:a17:902:760b:b029:e9:8bda:ae44 with SMTP id k11-20020a170902760bb02900e98bdaae44mr2747202pll.26.1618475936125; Thu, 15 Apr 2021 01:38:56 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:55 -0700 (PDT) From: Hirokazu Honda 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 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" V4L2Device owns a file descriptor for a v4l2 device node. It should be managed by ScopedFD avoid the leakage. Signed-off-by: Hirokazu Honda --- include/libcamera/internal/v4l2_device.h | 9 +++++---- src/libcamera/v4l2_device.cpp | 17 +++++++---------- src/libcamera/v4l2_videodevice.cpp | 3 ++- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index d006bf68..e0262de0 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -13,6 +13,7 @@ #include +#include #include #include "libcamera/internal/log.h" @@ -26,7 +27,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_; } @@ -46,11 +47,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(); @@ -64,7 +65,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 decd19ef..4fbb2d60 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -53,7 +53,7 @@ LOG_DEFINE_CATEGORY(V4L2) * 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) { } @@ -89,7 +89,7 @@ int V4L2Device::open(unsigned int flags) return ret; } - setFd(ret); + setFd(ScopedFD(ret)); listControls(); @@ -112,14 +112,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); @@ -138,10 +138,7 @@ void V4L2Device::close() delete fdEventNotifier_; - if (::close(fd_) < 0) - LOG(V4L2, Error) << "Failed to close V4L2 device: " - << strerror(errno); - fd_ = -1; + fd_.reset(); } /** @@ -449,7 +446,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..0bf3b5f5 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" @@ -614,7 +615,7 @@ int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type) return ret; } - ret = V4L2Device::setFd(newFd); + ret = V4L2Device::setFd(ScopedFD(newFd)); if (ret < 0) { LOG(V4L2, Error) << "Failed to set file handle: " << strerror(-ret); From patchwork Thu Apr 15 08:38:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11943 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 CBDABBD224 for ; Thu, 15 Apr 2021 08:39:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6C01768816; Thu, 15 Apr 2021 10:39:01 +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="REJbrhtV"; 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 129126881F for ; Thu, 15 Apr 2021 10:38:59 +0200 (CEST) Received: by mail-pf1-x42e.google.com with SMTP id a12so15579918pfc.7 for ; Thu, 15 Apr 2021 01:38:59 -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=IHmv1/ucE/Y8xRCZdZ4f4MRgI2S4immsBBFqmGQaZYs=; b=REJbrhtVtc5sGUyyhFNqy+BeTgcddcO15fqsnI8go5++X9HyrsMlK+ppCBqGRVUom7 fvKYr0m07BFprKW1NqfQ79GfRKcNkHl++UOl94ZJx0AGucsxfP1G7ZVwsnLrq26KGpHR +ugSC1AyWUPXpVkW7CRXSpvhXEbhmh4ij+RvU= 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=IHmv1/ucE/Y8xRCZdZ4f4MRgI2S4immsBBFqmGQaZYs=; b=AdGZ7tLYQVrB1RXdISp/UfSsZqbZb5jRASPVQ4ylthIimfDXru3nCknkrDpUkhEGVn qYo5s3gieONUpKHXG0XYzrPsvAasXXrhOJ0MmhwiYBRSxioQDU0h5pzEwF/dLzhyqAYV G7OkuoCn8q2DyKS1a+JwQHrZp9q9nObudQ/3hytUWoHzB3iE4zaHM9nDbLHFWLqOCMku v8bMe7vn05JvArmj8XC4K5L4BkquS9q2j3+7u3kIuyNBwY32l3rIiOZV4z4f7LK/p9JO /SCRpBP+4p+S08g3odqugSIdscnFtmFBtdqyolFSDacs2NTMVD/NRqRy9mortbpFpozL 1+8w== X-Gm-Message-State: AOAM5314lEMnwf+7MOxCD7NJsWexasUe9Brgt1pqhaDeZTtj6qgUQuSG vUvEN57pMR8CFmKaA5D7RmE99HTUjV+hOA== X-Google-Smtp-Source: ABdhPJwJQHHe7WbD1gw9oyuU5g7Mp+VlXLSkK95RU8eV+/VQctCswptXXB/bP9nEGj108eO2H+Rw7g== X-Received: by 2002:a63:1b5c:: with SMTP id b28mr2451726pgm.444.1618475937534; Thu, 15 Apr 2021 01:38:57 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:57 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:40 +0900 Message-Id: <20210415083843.3399502-7-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 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" V4L2VideoDevice deals with file descriptors. This uses ScopedFD for them to avoid the leakage. 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 | 21 ++++++++----------- 2 files changed, 11 insertions(+), 14 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 0bf3b5f5..fe311ed9 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1278,12 +1278,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; @@ -1293,7 +1293,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 = {}; @@ -1308,10 +1308,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); } /** @@ -1703,7 +1703,6 @@ V4L2M2MDevice::~V4L2M2MDevice() */ int V4L2M2MDevice::open() { - int fd; int ret; /* @@ -1712,7 +1711,7 @@ int V4L2M2MDevice::open() * as the V4L2VideoDevice::open() retains a handle by duplicating the * fd passed in. */ - fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), + int fd = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), O_RDWR | O_NONBLOCK); if (fd < 0) { ret = -errno; @@ -1720,22 +1719,20 @@ int V4L2M2MDevice::open() << "Failed to open V4L2 M2M device: " << strerror(-ret); return ret; } + ScopedFD scopedFd(fd); - ret = output_->open(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT); + ret = output_->open(scopedFd.get(), V4L2_BUF_TYPE_VIDEO_OUTPUT); if (ret) goto err; - ret = capture_->open(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE); + ret = capture_->open(scopedFd.get(), V4L2_BUF_TYPE_VIDEO_CAPTURE); if (ret) goto err; - ::close(fd); - return 0; err: close(); - ::close(fd); return ret; } From patchwork Thu Apr 15 08:38:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11944 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 78089BD224 for ; Thu, 15 Apr 2021 08:39:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 24F1468821; Thu, 15 Apr 2021 10:39:02 +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="QhCyrM8/"; 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 95D706880B for ; Thu, 15 Apr 2021 10:39:00 +0200 (CEST) Received: by mail-pf1-x42e.google.com with SMTP id m11so15592314pfc.11 for ; Thu, 15 Apr 2021 01:39:00 -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=L2GJ5FdpixTlkZnE5WQQJ3Cr2SvhnXOuRKhPG46hJfw=; b=QhCyrM8/c+4VbnwAvyuohqHLc8ATnk7MJ2H5s1GWQEOY56DGWSfdYLFx5YqJ/k43am ubSlIHgJvAcotvcOwcxu3BocDhSJ+aRv/qsWLQkiJbQL/qEYp3UZD3LWYgDW9xghRvbI q0sXZS5VEErP/m1KBKBVurn0yEcuZbrts271Q= 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=L2GJ5FdpixTlkZnE5WQQJ3Cr2SvhnXOuRKhPG46hJfw=; b=REfpOkyA5h6m+43NcPGUgkAWCnoXdvdyzLffKu8tXIR4cRzSopdJPCiB6bnsg39+DS MLdpx3YWZi0shUNyskaUcLZ8KKUt6mHYUBZMH+6Z4KxG+FXRg7ByYDYKEMMrQGqnCGNy AcJuTpK/GK0L5iE9c+lS0Sf5rK3yP+oxQhwnl7MxWhTmiX+11ZXcV6DqWp+gPXJYbTYU B6H6o9igVWOjhGiD0xesB4Dnd9nbc8jro6EPE6GCgO6Pzi5VG3JbNYQKafUtc2wTJp9Z ZguKSGVwGWEiQ4fGeevDUZ30CgPl4WSMccE364tpSQRRf3TXSN2K3EdhifgUSsuN4s6O doww== X-Gm-Message-State: AOAM532jrBwpGFBE7lb04tyk4AawSwSymuTx8aQBBUiylR3wg3DzJJYQ 0unNMZr9EzJUBBG9F+GI7mdWV+xIOOK11Q== X-Google-Smtp-Source: ABdhPJzlNSHIBAGITEaV+xsYWQHirm6xq22xFsXPyRZkKdH/C+w3qpdeAX+lHnx1JlAZ4V0uFTIG7Q== X-Received: by 2002:a63:fb15:: with SMTP id o21mr2461179pgh.337.1618475938994; Thu, 15 Apr 2021 01:38:58 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:38:58 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:41 +0900 Message-Id: <20210415083843.3399502-8-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 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..c06fee47 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 Apr 15 08:38:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11945 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 4442EBD224 for ; Thu, 15 Apr 2021 08:39:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 092A968816; Thu, 15 Apr 2021 10:39:04 +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="SWEq/gKh"; dkim-atps=neutral Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2992A68822 for ; Thu, 15 Apr 2021 10:39:02 +0200 (CEST) Received: by mail-pf1-x435.google.com with SMTP id c17so15595224pfn.6 for ; Thu, 15 Apr 2021 01:39:02 -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=0oMmXz+UiOFrr5cvYdkpEx8rCkMr9w7tm7pVdtG8/HY=; b=SWEq/gKhWDOUu5hqas/KXb8Xiu/m674JXbscSlZLvbip+BZuqVqW81Tqa2wwXqYQe4 9McwoJemahnr8p2sW5ddrSQG+i/P397skIzb8qIfQlEVbm7wpnnr9nyu6rNxEiEHXpkf DtLifPxWEPVcGatmAh1nMSRSjYH/+lnJrkaME= 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=0oMmXz+UiOFrr5cvYdkpEx8rCkMr9w7tm7pVdtG8/HY=; b=RFjp7h/9mwaJuEt/cDWk/09fB9Fw9d+y6K+slDam7METVOP2xof/FB1AnKdNmB5y0C qlYn1xCUZm8aL5+rotSuTBXoNiKdkZCV1JzpJbAfue4uBXLkFgz/0BtLV9hp4LM3xmPd UoULDx47ysiKXsSf8ZnDU29Nwz4X8pOtrYYZD+KXu+Bq5hG78VtXqyWBNSSdWu774EhV qKzbYWGsXLI4Z9cW4IFh8NSOMmTGgoF8Lfo1XVLihSwwRLA+ttekrv2V5u83OQ2qBgoj 5ccbHqmrUeMkOfdiv4PG9vTSePx5Fdt/PJDf2jkcbbuzAfjs0KSxLLU7iGzluqghmi8G E3gQ== X-Gm-Message-State: AOAM532QroW2sLCUIB3dHZ2GjkTx7qlFQ00VCmWmoebNQ7WT9vOWkTA2 YhxwWiEQmWjLkK5Bpls6xsZeYzVn/X1Ytg== X-Google-Smtp-Source: ABdhPJy4F41uN95O4hH67DqTBL/dAYHLd6Ie+dJCfVvwX+UKH040TV8yi7EZSL1pPEG5BkIfnmNjdA== X-Received: by 2002:a62:7592:0:b029:254:ebcd:802b with SMTP id q140-20020a6275920000b0290254ebcd802bmr2128456pfc.41.1618475940417; Thu, 15 Apr 2021 01:39:00 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:39:00 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:42 +0900 Message-Id: <20210415083843.3399502-9-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 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" DmaHeaps owns a file descriptor for a dma handle. It should be managed by ScopedFD to avoid the leakage. 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..c49772df 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 f22e286e..0075fdb1 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -1256,10 +1256,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(scopedFD.release()); + /* Allow the IPA to mmap the LS table via the file descriptor. */ /* * \todo Investigate if mapping the lens shading table buffer From patchwork Thu Apr 15 08:38:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 11946 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 B013CBD224 for ; Thu, 15 Apr 2021 08:39:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 67FD568820; Thu, 15 Apr 2021 10:39:04 +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="UszF879V"; dkim-atps=neutral Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6C1046881C for ; Thu, 15 Apr 2021 10:39:03 +0200 (CEST) Received: by mail-pg1-x52d.google.com with SMTP id w10so16430180pgh.5 for ; Thu, 15 Apr 2021 01:39:03 -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=DSsxcSmsNI9tDWl/QmgciTOfqlHw1LrlygyIrWs5bYw=; b=UszF879VhFcbEnqHkvQJ34hIscVtI9CHlD0kqhgOmzJoUUMQwUlmuyPDS8HYA1VciJ KCYxTabv5/fYUlg0il08GzYJ+RQIJvxW0iWTrycYpgAAcDHmIRPO5hML1oc5y2hyOhUs k/f89NPo4Ng9DfHK3dwq/7PySNr0ufP6dUeqU= 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=DSsxcSmsNI9tDWl/QmgciTOfqlHw1LrlygyIrWs5bYw=; b=C+zXhR4DLRK8W2whysUJtXVd7iYgo/SzIBzM75rbpZYlmaUxo/Fd4kCuQpKe5t2rzH vcHOFQ6BfZahfMM5bMyNxN/18LQU1rwKztXMJokiSIj4QB4o7e4dgUZtUaogXfCJUrOQ cuwqGkB2UwMRqibSBHJM07iRRQPxUzSqRqOTqegALNUWKxBFmSLM6G0mojdEp6uX1d7C ZHsGUZkyFsj+GwQ5UB9bJZdOLXPOuSC+JY3G+sQVyz86XOUNHBXlghKcpLJSRWly8WQ1 3mkg1ZMzyrz4LfS6y6MM63hvy4Lskiu/TpJUAn9BszPsdSPsSzSMMxJTZAyNio4REWg2 dioA== X-Gm-Message-State: AOAM531j8QXYkLSw/aFNcC3XIYdLXKOqEQSIedXG3O1Kgta8uInvm5I9 GRTeqY79s58wnxIOdvAnjxa2cfF1jXtODA== X-Google-Smtp-Source: ABdhPJwYB8ZCYkI41AySZLE23mkwwfUC5gud5+oOglGczMt33plvLktIeG9I1mjK3eUFup/EdC5H9Q== X-Received: by 2002:a63:dc03:: with SMTP id s3mr2409558pgg.426.1618475941835; Thu, 15 Apr 2021 01:39:01 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:2:84f5:7981:dfbe:8f02]) by smtp.gmail.com with ESMTPSA id 205sm1520258pfc.201.2021.04.15.01.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Apr 2021 01:39:01 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 15 Apr 2021 17:38:43 +0900 Message-Id: <20210415083843.3399502-10-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 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;