From patchwork Tue Mar 25 18:08:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23014 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 3DB99C3285 for ; Tue, 25 Mar 2025 18:08:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6A40568951; Tue, 25 Mar 2025 19:08:46 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="fN++1HPP"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CE3F8600EB for ; Tue, 25 Mar 2025 19:08:42 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6E71682E for ; Tue, 25 Mar 2025 19:06:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926015; bh=QzqIb/GAXETZPl/ruc6Jqo8QLVvsFIYIbyejuIVMaYQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fN++1HPPfUHpH+tSIkgCFM3IJ2puhds1PK4oAnHQKOrxhGmCzqe34ZJshHBi0jF8+ jvdwTK6dmYNbNWnTvY2LCTCXuIiYyEmNDZTdIYfECTKYcfikPPBvMMM/THyHmJoOBW OX4bwTECvls9XXXGu10uLKD1D8dOReC5RCHdAl5A= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 1/9] libcamera: process: Disable copy/move Date: Tue, 25 Mar 2025 19:08:13 +0100 Message-ID: <20250325180821.1456154-2-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" A `Process` object has address identity because a pointer to it is stored inside the `ProcessManager`. However, copy/move special methods are still generated by the compiler. So disable them to avoid potential issues and confusion. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/internal/process.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index b1d07a5a5..58e3e41ce 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -42,6 +42,8 @@ public: Signal finished; private: + LIBCAMERA_DISABLE_COPY_AND_MOVE(Process) + void closeAllFdsExcept(const std::vector &fds); int isolate(); void died(int wstatus); From patchwork Tue Mar 25 18:08:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23020 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id C4DE9C323E for ; Tue, 25 Mar 2025 19:20:38 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 56ECC353 for ; Tue, 25 Mar 2025 20:18:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742930331; bh=ySDDXkgoPdzwPs6BoIxoTD3UqhqnC2eULU57oLYyKqU=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Resent-From:Resent-To:From; b=n0Qf9gYmazO6F3KspIK06H3QR0KyvJqhBx2X9rUiGH1Uo6mQd49pxrnYqanNHFpLz CqGGct6cpu/jdu0l40EEBQFCOvDLw19opQlgMeRJLOT7Ojw4bijYi99Q3YCpKtr3Un Uh8U40DonFx/5rEedgYpWroCz7qfsT34+5rMvaro= Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id KK2/Bsjw4mfwuS8A4E0KoQ (envelope-from ) for ; Tue, 25 Mar 2025 19:07:04 +0100 Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by perceval.ideasonboard.com (Postfix) with ESMTPS id E6E21353; Tue, 25 Mar 2025 19:07:03 +0100 (CET) Authentication-Results: perceval.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.a=rsa-sha256 header.s=mail header.b=g4VJXIQZ; dkim-atps=neutral Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B5CCE68960; Tue, 25 Mar 2025 19:08:48 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="g4VJXIQZ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DD97568957 for ; Tue, 25 Mar 2025 19:08:43 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 0DC89353 for ; Tue, 25 Mar 2025 19:06:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926016; bh=fBLhKYdPjRhaI/DNIgvU+LareB5sr4B2UxeoLiLrgWI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=g4VJXIQZ2LNhHK/LlpAI4R3gFR3M1ra0k2HJrFhJ5TgBemzftF0L9LU82wSOMwJvm /R+aIydTyQ20mxvDHvEYyclzToiy2quSN30xw7yqwHc849AwWqINbVq2taCrWfS4BB +OTUgUBUOTAg8y+2ckSvrantQE8hKLY/Vnj29Yk8= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 2/9] libcamera: process: Misc. cleanup around `execv()` Date: Tue, 25 Mar 2025 19:08:14 +0100 Message-ID: <20250325180821.1456154-3-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" X-TUID: kdcxRDvFkBCb Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org Firstly, get the number of arguments first, and use that to determine the size of the allocation instead of retrieving it twice. Secondly, use `const_cast` instead of a C-style cast when calling `execv()`. Third, use `size_t` to match the type of `args.size()`. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- src/libcamera/process.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 68fad3270..567b878d4 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -265,14 +265,15 @@ int Process::start(const std::string &path, if (file && strcmp(file, "syslog")) unsetenv("LIBCAMERA_LOG_FILE"); - const char **argv = new const char *[args.size() + 2]; - unsigned int len = args.size(); + size_t len = args.size(); + auto argv = std::make_unique(len + 2); + argv[0] = path.c_str(); - for (unsigned int i = 0; i < len; i++) + for (size_t i = 0; i < len; i++) argv[i + 1] = args[i].c_str(); argv[len + 1] = nullptr; - execv(path.c_str(), (char **)argv); + execv(path.c_str(), const_cast(argv.get())); exit(EXIT_FAILURE); } From patchwork Tue Mar 25 18:08:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23021 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id 23F55C3213 for ; Tue, 25 Mar 2025 19:20:48 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D2A88353 for ; Tue, 25 Mar 2025 20:19:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742930340; bh=Bk0LKNGS3OO3AGvVk9f5L3HGSQ7+H2GMRKWV09sYKy0=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Resent-From:Resent-To:From; b=bL/mrVxa2uJCmh1BvP8qQOQhBh+Sm0ZdVXRS5s9gWpSD/3bSDKCCoGkHIk77t7+KN FeF/WDYoM+ZtKapemQEbqdv8awxQHSLyOTayWOwaUH7pxCtVHkjSTrgdpxOZRf8xjC xLyhrzBZtbzYJxGnDeg457MjPCUcB4evAFmL3XZo= Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id KJMRAcrw4mfsuS8A4E0KoQ (envelope-from ) for ; Tue, 25 Mar 2025 19:07:06 +0100 Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by perceval.ideasonboard.com (Postfix) with ESMTPS id D6784353; Tue, 25 Mar 2025 19:07:05 +0100 (CET) Authentication-Results: perceval.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.a=rsa-sha256 header.s=mail header.b=q0xWf3ZL; dkim-atps=neutral Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0733268964; Tue, 25 Mar 2025 19:08:51 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="q0xWf3ZL"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 661F4600EB for ; Tue, 25 Mar 2025 19:08:44 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id CF98782E for ; Tue, 25 Mar 2025 19:06:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926017; bh=JYVrXzg3/6kpyzn1pqSQm7ylA7r/8ZaMlbV173Qo9PM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=q0xWf3ZLw/fcU4dXsXy/f40AatEmLGGQxaaWGmj0IJXch8X/YGoOBDmuccl6Aytgt tjbAA5KZNVpJv4i32KwUg/que39OxsJlrvTN2olRoJoui7u8hwvTnCTkNWOhmfcjvJ lkHw8CJtwbsh/6jy5VL0PRdB20PrAwOPERvwwZok= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 3/9] libcamera: process: Use `pid_` member to decide if running Date: Tue, 25 Mar 2025 19:08:15 +0100 Message-ID: <20250325180821.1456154-4-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" X-TUID: QYN3u8HQInw6 Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org Instead of using a separate member variable, use `pid_ > 0` to determine if the process is still running. Previously the value of `pid_` was not reset to -1 when the process terminated, but since it is only meaningful while the process is running, reset it to -1 in `Process::died()`. Neither `pid_` nor `running_` are exposed, so this change has no effect on the public interface or observable behaviour. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- include/libcamera/internal/process.h | 1 - src/libcamera/process.cpp | 8 +++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index 58e3e41ce..030a1e66e 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -49,7 +49,6 @@ private: void died(int wstatus); pid_t pid_; - bool running_; enum ExitStatus exitStatus_; int exitCode_; diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 567b878d4..aa9e8f519 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -208,7 +208,7 @@ const struct sigaction &ProcessManager::oldsa() const */ Process::Process() - : pid_(-1), running_(false), exitStatus_(NotExited), exitCode_(0) + : pid_(-1), exitStatus_(NotExited), exitCode_(0) { } @@ -240,7 +240,7 @@ int Process::start(const std::string &path, { int ret; - if (running_) + if (pid_ > 0) return 0; int childPid = fork(); @@ -252,8 +252,6 @@ int Process::start(const std::string &path, pid_ = childPid; ProcessManager::instance()->registerProcess(this); - running_ = true; - return 0; } else { if (isolate()) @@ -327,7 +325,7 @@ int Process::isolate() */ void Process::died(int wstatus) { - running_ = false; + pid_ = -1; exitStatus_ = WIFEXITED(wstatus) ? NormalExit : SignalExit; exitCode_ = exitStatus_ == NormalExit ? WEXITSTATUS(wstatus) : -1; From patchwork Tue Mar 25 18:08:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23017 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 5F66CC3320 for ; Tue, 25 Mar 2025 18:08:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7176E6895C; Tue, 25 Mar 2025 19:08:57 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="odXF0sb4"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 329436895A for ; Tue, 25 Mar 2025 19:08:46 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id AF606353 for ; Tue, 25 Mar 2025 19:06:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926019; bh=LRah4/0T+HxltO41um8++msOU3ZWMbrKOU6OnXDfb7o=; h=From:To:Subject:Date:In-Reply-To:References:From; b=odXF0sb4VeC7nOmUH/u2RxOwi5jQyl49VgURbZUOb1PPLwWZPJPwzwh6YX1AUI6QZ cXIQEZSacFYL0WcHZ3C3eKT04pPasGiAxdxd1WQasRHSiLihyoSqbEHn+tA7xxxb1A XICfzEzkOn5DmWh48FvPKa80F1Kg4rF+yyer0Ap8= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 5/9] libcamera: process: Ensure that file descriptors are nonnegative Date: Tue, 25 Mar 2025 19:08:17 +0100 Message-ID: <20250325180821.1456154-6-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" Return `-EINVAL` from `Process::start()` if any of the file descriptors are negative as those most likely signal some kind of issue such as missed error checking. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/libcamera/process.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 716523acc..62e63255b 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -243,6 +243,11 @@ int Process::start(const std::string &path, if (pid_ > 0) return -EBUSY; + for (int fd : fds) { + if (fd < 0) + return -EINVAL; + } + int childPid = fork(); if (childPid == -1) { ret = -errno; @@ -282,6 +287,8 @@ void Process::closeAllFdsExcept(const std::vector &fds) std::vector v(fds); sort(v.begin(), v.end()); + ASSERT(v.empty() || v.front() >= 0); + DIR *dir = opendir("/proc/self/fd"); if (!dir) return; From patchwork Tue Mar 25 18:08:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23018 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 B9456C3321 for ; Tue, 25 Mar 2025 18:09:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 152636895E; Tue, 25 Mar 2025 19:08:59 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ahIwsZE5"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D72A268955 for ; Tue, 25 Mar 2025 19:08:46 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 506B4E45 for ; Tue, 25 Mar 2025 19:06:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926019; bh=ajDtbhAdnXEqGMrXZuYQpcnu6FTcvIe3UMYT5AkuaHs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ahIwsZE5TJ7q9ZGVjTr/z6lk2JzmgDB4HQ1QQM6ZpkiFVclzlfZYtKaWDNeKbPxtF KPSmAA+Hw6bwNuXFKgxaZwrJKvtM21GbBN4ryHJ9SouNYL/wj0j62YEPrXCB0gAa9m 1TdUuINnfyoTBfGIHQmUVIFx6EjS+qqIdjTQlpw4= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 6/9] libcamera: process: Use span instead of vector Date: Tue, 25 Mar 2025 19:08:18 +0100 Message-ID: <20250325180821.1456154-7-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" Use `libcamera::Span` whenever applicable. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/internal/process.h | 8 ++++---- src/libcamera/ipc_pipe_unixsocket.cpp | 9 +++------ src/libcamera/process.cpp | 8 ++++---- test/process/process_test.cpp | 5 ++--- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index 030a1e66e..e4f5bb979 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -9,9 +9,9 @@ #include #include -#include #include +#include #include namespace libcamera { @@ -31,8 +31,8 @@ public: ~Process(); int start(const std::string &path, - const std::vector &args = std::vector(), - const std::vector &fds = std::vector()); + Span args = {}, + Span fds = {}); ExitStatus exitStatus() const { return exitStatus_; } int exitCode() const { return exitCode_; } @@ -44,7 +44,7 @@ public: private: LIBCAMERA_DISABLE_COPY_AND_MOVE(Process) - void closeAllFdsExcept(const std::vector &fds); + void closeAllFdsExcept(Span fds); int isolate(); void died(int wstatus); diff --git a/src/libcamera/ipc_pipe_unixsocket.cpp b/src/libcamera/ipc_pipe_unixsocket.cpp index 668ec73b9..7ee7cac79 100644 --- a/src/libcamera/ipc_pipe_unixsocket.cpp +++ b/src/libcamera/ipc_pipe_unixsocket.cpp @@ -28,10 +28,6 @@ IPCPipeUnixSocket::IPCPipeUnixSocket(const char *ipaModulePath, const char *ipaProxyWorkerPath) : IPCPipe() { - std::vector fds; - std::vector args; - args.push_back(ipaModulePath); - socket_ = std::make_unique(); UniqueFD fd = socket_->create(); if (!fd.isValid()) { @@ -39,8 +35,9 @@ IPCPipeUnixSocket::IPCPipeUnixSocket(const char *ipaModulePath, return; } socket_->readyRead.connect(this, &IPCPipeUnixSocket::readyRead); - args.push_back(std::to_string(fd.get())); - fds.push_back(fd.get()); + + std::array args{ std::string(ipaModulePath), std::to_string(fd.get()) }; + std::array fds{ fd.get() }; proc_ = std::make_unique(); int ret = proc_->start(ipaProxyWorkerPath, args, fds); diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 62e63255b..5603dc903 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -235,8 +235,8 @@ Process::~Process() * or a negative error code otherwise */ int Process::start(const std::string &path, - const std::vector &args, - const std::vector &fds) + Span args, + Span fds) { int ret; @@ -282,9 +282,9 @@ int Process::start(const std::string &path, } } -void Process::closeAllFdsExcept(const std::vector &fds) +void Process::closeAllFdsExcept(Span fds) { - std::vector v(fds); + std::vector v(fds.begin(), fds.end()); sort(v.begin(), v.end()); ASSERT(v.empty() || v.front() >= 0); diff --git a/test/process/process_test.cpp b/test/process/process_test.cpp index e9f5e7e9b..a88d8fef1 100644 --- a/test/process/process_test.cpp +++ b/test/process/process_test.cpp @@ -5,9 +5,9 @@ * Process test */ +#include #include #include -#include #include #include @@ -48,8 +48,7 @@ protected: Timer timeout; int exitCode = 42; - vector args; - args.push_back(to_string(exitCode)); + std::array args{ to_string(exitCode) }; proc_.finished.connect(this, &ProcessTest::procFinished); /* Test that kill() on an unstarted process is safe. */ From patchwork Tue Mar 25 18:08:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23022 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9635EC3213 for ; Tue, 25 Mar 2025 19:21:14 +0000 (UTC) Received: from pendragon.ideasonboard.com (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 508F9353 for ; Tue, 25 Mar 2025 20:19:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742930367; bh=+j2mYamSF9tXXw08/WL/JPliWJ6Adi1nuMH7jWWk/CQ=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Resent-From:Resent-To:From; b=X+SFezx+Hc80w+WNZU/T4mNS1KmJR1ow1o+kPpFxWgb5eh2SPvxYrg2I5o/O/RVpf Iflm7O+tnShF60RreyYfG+CJWXpgrKO7VXdQQgapmZMTbjEDKYOpkOfppvgTJw8cbs oTLQhAJxIwgjV/+r7ZVR46TrXZlm6FquGWkSF/Yc= Delivered-To: kbingham@ideasonboard.com Received: from perceval.ideasonboard.com by perceval.ideasonboard.com with LMTP id QN/8JNPw4mcIui8A4E0KoQ (envelope-from ) for ; Tue, 25 Mar 2025 19:07:15 +0100 Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by perceval.ideasonboard.com (Postfix) with ESMTPS id 736B4353; Tue, 25 Mar 2025 19:07:15 +0100 (CET) Authentication-Results: perceval.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.a=rsa-sha256 header.s=mail header.b=coBrJMS2; dkim-atps=neutral Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F3C826895D; Tue, 25 Mar 2025 19:09:00 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="coBrJMS2"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A390468952 for ; Tue, 25 Mar 2025 19:08:47 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 11223353 for ; Tue, 25 Mar 2025 19:06:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926020; bh=hkTcXpSyWx7kGcQfaYLCqweQCjn31N2EjC6G98vUfGM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=coBrJMS29P3vVByeAzCOQCjifZ3KPsClA0eUbEZhLV28qyHX2JXUODyLdKkZX+3w0 uXdSOPicCzFYl0hhW25Ut2ksQHBmBSaAp8N3OGJhFfas2E1/6scCxkvcNzf0XBXV5n SNjnWv79wh+liHwisrn6HAGpBsVjm0rSd6X8xlWM= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 7/9] libcamera: process: Move `closeAllFdsExcept()` Date: Tue, 25 Mar 2025 19:08:19 +0100 Message-ID: <20250325180821.1456154-8-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" X-TUID: LfNPeoJK49Vh Resent-From: Kieran Bingham Resent-To: parsemail@patchwork.libcamera.org Remove `closeAllFdsExcept()` from the `Process` class and have it as a local function since it is no needed "outside" and does not depend on any part of the `Process` class. Signed-off-by: Barnabás Pőcze --- include/libcamera/internal/process.h | 1 - src/libcamera/process.cpp | 56 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h index e4f5bb979..14391d60a 100644 --- a/include/libcamera/internal/process.h +++ b/include/libcamera/internal/process.h @@ -44,7 +44,6 @@ public: private: LIBCAMERA_DISABLE_COPY_AND_MOVE(Process) - void closeAllFdsExcept(Span fds); int isolate(); void died(int wstatus); diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 5603dc903..01503e485 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -63,6 +63,34 @@ void sigact(int signal, siginfo_t *info, void *ucontext) } } +void closeAllFdsExcept(Span fds) +{ + std::vector v(fds.begin(), fds.end()); + std::sort(v.begin(), v.end()); + + ASSERT(v.empty() || v.front() >= 0); + + DIR *dir = opendir("/proc/self/fd"); + if (!dir) + return; + + int dfd = dirfd(dir); + + struct dirent *ent; + while ((ent = readdir(dir)) != nullptr) { + char *endp; + int fd = strtoul(ent->d_name, &endp, 10); + if (*endp) + continue; + + if (fd >= 0 && fd != dfd && + !std::binary_search(v.begin(), v.end(), fd)) + close(fd); + } + + closedir(dir); +} + } /* namespace */ void ProcessManager::sighandler() @@ -282,34 +310,6 @@ int Process::start(const std::string &path, } } -void Process::closeAllFdsExcept(Span fds) -{ - std::vector v(fds.begin(), fds.end()); - sort(v.begin(), v.end()); - - ASSERT(v.empty() || v.front() >= 0); - - DIR *dir = opendir("/proc/self/fd"); - if (!dir) - return; - - int dfd = dirfd(dir); - - struct dirent *ent; - while ((ent = readdir(dir)) != nullptr) { - char *endp; - int fd = strtoul(ent->d_name, &endp, 10); - if (*endp) - continue; - - if (fd >= 0 && fd != dfd && - !std::binary_search(v.begin(), v.end(), fd)) - close(fd); - } - - closedir(dir); -} - int Process::isolate() { int ret = unshare(CLONE_NEWUSER | CLONE_NEWNET); From patchwork Tue Mar 25 18:08:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23019 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 7696CC3323 for ; Tue, 25 Mar 2025 18:09:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 457E96895F; Tue, 25 Mar 2025 19:09:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="tgJt/Ixy"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 757686895F for ; Tue, 25 Mar 2025 19:08:48 +0100 (CET) Received: from pb-laptop.local (185.221.143.221.nat.pool.zt.hu [185.221.143.221]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id CBBCA353 for ; Tue, 25 Mar 2025 19:07:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742926021; bh=qRGu2Bth2fPbi9yFuKL/gq8+YmejefhAxhWGC8edVHY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=tgJt/IxyH+HoYo8B9gSiBwdT33Wg1OCoGywhJIkdtifW6Sdszs9+gpJbCEZtbEy85 YmogVyQEd8d+noYk8+tfRdQ+hD38KGYOhvGb39sv51XPZ2wAdUECFlM9XkSqxO2i4C tRYllR+LBJFYAtl5Dv9zrpuBe4XJp3zQDUF3keWA= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v3 8/9] libcamera: process: Use `close_range()` when available Date: Tue, 25 Mar 2025 19:08:20 +0100 Message-ID: <20250325180821.1456154-9-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> References: <20250325180821.1456154-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" Use the `close_range()` system call when available as it is simpler and faster than iterating `/proc/self/fd/`. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- meson.build | 4 ++++ src/libcamera/process.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/meson.build b/meson.build index 668ec3969..00291d628 100644 --- a/meson.build +++ b/meson.build @@ -103,6 +103,10 @@ if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix : '#define _GNU_SOUR config_h.set('HAVE_SECURE_GETENV', 1) endif +if cc.has_header_symbol('unistd.h', 'close_range', prefix : '#define _GNU_SOURCE') + config_h.set('HAVE_CLOSE_RANGE', 1) +endif + common_arguments = [ '-Wmissing-declarations', '-Wshadow', diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 01503e485..5fa813300 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -70,6 +70,26 @@ void closeAllFdsExcept(Span fds) ASSERT(v.empty() || v.front() >= 0); +#if HAVE_CLOSE_RANGE + static const bool hasCloseRange = [] { + return close_range(~0u, 0, 0) < 0 && errno == EINVAL; + }(); + + if (hasCloseRange) { + unsigned int prev = 0; + + for (unsigned int curr : v) { + ASSERT(prev <= curr); + if (prev < curr) + close_range(prev, curr - 1, 0); + prev = curr + 1; + } + + close_range(prev, ~0u, 0); + return; + } +#endif + DIR *dir = opendir("/proc/self/fd"); if (!dir) return;