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(); } /**