[{"id":17410,"web_url":"https://patchwork.libcamera.org/comment/17410/","msgid":"<YL0O2OV22FWAptM1@pendragon.ideasonboard.com>","date":"2021-06-06T18:07:20","subject":"Re: [libcamera-devel] [RFC PATCH 04/10] libcamera: Process: Manage\n\tpipe fds by ScopedFD","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Thu, Apr 15, 2021 at 05:38:37PM +0900, Hirokazu Honda wrote:\n> Process owns the file descriptors for pipe. They should be\n> managed by ScopedFDs to avoid the leakage.\n> \n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  include/libcamera/internal/process.h |  4 +++-\n>  src/libcamera/process.cpp            | 16 ++++++++++------\n>  2 files changed, 13 insertions(+), 7 deletions(-)\n> \n> diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h\n> index 254cda85..b3bb24df 100644\n> --- a/include/libcamera/internal/process.h\n> +++ b/include/libcamera/internal/process.h\n> @@ -11,6 +11,7 @@\n>  #include <string>\n>  #include <vector>\n>  \n> +#include <libcamera/scoped_file_descriptor.h>\n>  #include <libcamera/signal.h>\n>  \n>  namespace libcamera {\n> @@ -75,8 +76,9 @@ private:\n>  \tstd::list<Process *> processes_;\n>  \n>  \tstruct sigaction oldsa_;\n> +\n>  \tEventNotifier *sigEvent_;\n> -\tint pipe_[2];\n> +\tScopedFD pipe_[2];\n>  };\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\n> index 40a434a6..bb51d73b 100644\n> --- a/src/libcamera/process.cpp\n> +++ b/src/libcamera/process.cpp\n> @@ -69,7 +69,7 @@ void sigact(int signal, siginfo_t *info, void *ucontext)\n>  void ProcessManager::sighandler([[maybe_unused]] EventNotifier *notifier)\n>  {\n>  \tchar data;\n> -\tssize_t ret = read(pipe_[0], &data, sizeof(data));\n> +\tssize_t ret = read(pipe_[0].get(), &data, sizeof(data));\n>  \tif (ret < 0) {\n>  \t\tLOG(Process, Error)\n>  \t\t\t<< \"Failed to read byte from signal handler pipe\";\n> @@ -129,10 +129,15 @@ ProcessManager::ProcessManager()\n>  \n>  \tsigaction(SIGCHLD, &sa, NULL);\n>  \n> -\tif (pipe2(pipe_, O_CLOEXEC | O_DIRECT | O_NONBLOCK))\n> +\tint pipe[2];\n> +\tif (pipe2(pipe, O_CLOEXEC | O_DIRECT | O_NONBLOCK))\n>  \t\tLOG(Process, Fatal)\n>  \t\t\t<< \"Failed to initialize pipe for signal handling\";\n> -\tsigEvent_ = new EventNotifier(pipe_[0], EventNotifier::Read);\n> +\n> +\tpipe_[0] = ScopedFD(pipe[0]);\n> +\tpipe_[1] = ScopedFD(pipe[1]);\n> +\n> +\tsigEvent_ = new EventNotifier(pipe_[0].get(), EventNotifier::Read);\n>  \tsigEvent_->activated.connect(this, &ProcessManager::sighandler);\n>  \n>  \tself_ = this;\n> @@ -141,9 +146,8 @@ ProcessManager::ProcessManager()\n>  ProcessManager::~ProcessManager()\n>  {\n>  \tsigaction(SIGCHLD, &oldsa_, NULL);\n> +\n>  \tdelete sigEvent_;\n> -\tclose(pipe_[0]);\n> -\tclose(pipe_[1]);\n>  \n>  \tself_ = nullptr;\n>  }\n> @@ -170,7 +174,7 @@ ProcessManager *ProcessManager::instance()\n>   */\n>  int ProcessManager::writePipe() const\n>  {\n> -\treturn pipe_[1];\n> +\treturn pipe_[1].get();\n>  }\n>  \n>  /**","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id CF39EC3206\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  6 Jun 2021 18:07:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3F2CB6892B;\n\tSun,  6 Jun 2021 20:07:36 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0A5E2602A7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Jun 2021 20:07:34 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7A9B8EF;\n\tSun,  6 Jun 2021 20:07:33 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"dKV3aP9E\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1623002853;\n\tbh=XNRBj94PZFH7bw22iwzRAobmSDwl4KkyMCHVowbdlkQ=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=dKV3aP9EGNxqvkJtIUykgF5sQQcgJoRFdPIS7FHIzzEwfVth1wxHGDEVRVPR8v1bQ\n\tIepYZW2bqPrCQ4FPZDWENgbI9KDKyLLaSR5QVVi5nwiykHG+MpKbnEcZouP1cEUgfX\n\tX8+AhTI889IR0GBnrH3ORFet68KTvm0mofBUAvrk=","Date":"Sun, 6 Jun 2021 21:07:20 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YL0O2OV22FWAptM1@pendragon.ideasonboard.com>","References":"<20210415083843.3399502-1-hiroh@chromium.org>\n\t<20210415083843.3399502-4-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210415083843.3399502-4-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [RFC PATCH 04/10] libcamera: Process: Manage\n\tpipe fds by ScopedFD","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]