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;