{"id":23995,"url":"https://patchwork.libcamera.org/api/1.1/patches/23995/?format=json","web_url":"https://patchwork.libcamera.org/patch/23995/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250728113641.238256-6-barnabas.pocze@ideasonboard.com>","date":"2025-07-28T11:36:40","name":"[v6,5/6] libcamera: process: Use `close_range()` when available","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"bf73690ebeca93ae5e204b867694bd8ba03ecba2","submitter":{"id":216,"url":"https://patchwork.libcamera.org/api/1.1/people/216/?format=json","name":"Barnabás Pőcze","email":"barnabas.pocze@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/23995/mbox/","series":[{"id":5342,"url":"https://patchwork.libcamera.org/api/1.1/series/5342/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5342","date":"2025-07-28T11:36:35","name":"libcamera: process: Remove `ProcessManager` singleton","version":6,"mbox":"https://patchwork.libcamera.org/series/5342/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/23995/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/23995/checks/","tags":{},"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 A8BC4C332C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 28 Jul 2025 11:37:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B56196917B;\n\tMon, 28 Jul 2025 13:36:58 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 05BF06915B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 28 Jul 2025 13:36:47 +0200 (CEST)","from pb-laptop.local (185.221.140.39.nat.pool.zt.hu\n\t[185.221.140.39])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 081724A4;\n\tMon, 28 Jul 2025 13:36:04 +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=\"P4d0S07j\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1753702565;\n\tbh=zU468YZgi0yylox2Et+YlkCakIhOIDFwylOmeVyQzb4=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=P4d0S07jzrLVhBsrQq6e1i0+/CkCVwjH7XtH4MxOgex7aEEprjL57BwQGKY00EmW+\n\t89ZOkEC0aKRclc1KQ+T041Qc/Bgv00vrxPQu28LUgq57n5DyhD5gz8lpfUuHVRNinX\n\t3tS0s/W2E9l5wAnj4D/9LeHf0Q1Hcqp2JRR1+xlI=","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"[PATCH v6 5/6] libcamera: process: Use `close_range()` when\n\tavailable","Date":"Mon, 28 Jul 2025 13:36:40 +0200","Message-ID":"<20250728113641.238256-6-barnabas.pocze@ideasonboard.com>","X-Mailer":"git-send-email 2.50.1","In-Reply-To":"<20250728113641.238256-1-barnabas.pocze@ideasonboard.com>","References":"<20250728113641.238256-1-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Use the `close_range()` system call when available as it is simpler\nand faster than iterating `/proc/self/fd/`.\n\nSigned-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n meson.build               |  4 ++++\n src/libcamera/process.cpp | 25 +++++++++++++++++++++++++\n 2 files changed, 29 insertions(+)","diff":"diff --git a/meson.build b/meson.build\nindex 4ed8017eb..6fc4504ab 100644\n--- a/meson.build\n+++ b/meson.build\n@@ -74,6 +74,10 @@ cc = meson.get_compiler('c')\n cxx = meson.get_compiler('cpp')\n config_h = configuration_data()\n \n+if cc.has_header_symbol('unistd.h', 'close_range', prefix : '#define _GNU_SOURCE')\n+    config_h.set('HAVE_CLOSE_RANGE', 1)\n+endif\n+\n if cc.has_header_symbol('fcntl.h', 'F_ADD_SEALS', prefix : '#define _GNU_SOURCE')\n     config_h.set('HAVE_FILE_SEALS', 1)\n endif\ndiff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp\nindex 4b87c5591..ce6e4c380 100644\n--- a/src/libcamera/process.cpp\n+++ b/src/libcamera/process.cpp\n@@ -69,6 +69,31 @@ void closeAllFdsExcept(std::vector<int> v)\n \n \tASSERT(v.empty() || v.front() >= 0);\n \n+#if HAVE_CLOSE_RANGE\n+\t/*\n+\t * At the moment libcamera does not require at least Linux 5.9,\n+\t * which introduced the `close_range()` system call, so a runtime\n+\t * check is also needed to make sure that it is supported.\n+\t */\n+\tstatic const bool hasCloseRange = [] {\n+\t\treturn close_range(~0u, 0, 0) < 0 && errno == EINVAL;\n+\t}();\n+\n+\tif (hasCloseRange) {\n+\t\tunsigned int prev = 0;\n+\n+\t\tfor (unsigned int curr : v) {\n+\t\t\tASSERT(prev <= curr + 1);\n+\t\t\tif (prev < curr)\n+\t\t\t\tclose_range(prev, curr - 1, 0);\n+\t\t\tprev = curr + 1;\n+\t\t}\n+\n+\t\tclose_range(prev, ~0u, 0);\n+\t\treturn;\n+\t}\n+#endif\n+\n \tDIR *dir = opendir(\"/proc/self/fd\");\n \tif (!dir)\n \t\treturn;\n","prefixes":["v6","5/6"]}