From patchwork Thu Apr 24 11:41:02 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: 23260 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 7054EC32A2 for ; Thu, 24 Apr 2025 11:41:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 558D268B32; Thu, 24 Apr 2025 13:41:18 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ELi6HAki"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AB9E668AD1 for ; Thu, 24 Apr 2025 13:41:10 +0200 (CEST) Received: from pb-laptop.local (185.221.143.16.nat.pool.zt.hu [185.221.143.16]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 7434D1A99; Thu, 24 Apr 2025 13:41:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1745494868; bh=aOOx0AFCP9uaRXubSVrh4PGb0/Mp1a/g1L8iuB7sCOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ELi6HAkiGz6wuReCFOKyFaKqcwXEbtrC5pLXUktODmEmzMkzWRWBKyAph5xddByjs A2kuLLK8Hw0Ry48DBsT7GSzHm7GDVgJr72RQbPZ1bQhVT1p2cW0ARUXVN4l/5Pr2CC cm00kZBiRPtLRsMywfZR0lMMXSu7FcD+21Xvco8U= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Laurent Pinchart Subject: [RFC PATCH v5 8/9] libcamera: process: Use `close_range()` when available Date: Thu, 24 Apr 2025 13:41:02 +0200 Message-ID: <20250424114103.451395-9-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250424114103.451395-1-barnabas.pocze@ideasonboard.com> References: <20250424114103.451395-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 | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/meson.build b/meson.build index 9ba5e2ca9..ae13727eb 100644 --- a/meson.build +++ b/meson.build @@ -74,6 +74,10 @@ cc = meson.get_compiler('c') cxx = meson.get_compiler('cpp') config_h = configuration_data() +if cc.has_header_symbol('unistd.h', 'close_range', prefix : '#define _GNU_SOURCE') + config_h.set('HAVE_CLOSE_RANGE', 1) +endif + if cc.has_header_symbol('fcntl.h', 'F_ADD_SEALS', prefix : '#define _GNU_SOURCE') config_h.set('HAVE_FILE_SEALS', 1) endif diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp index 369fdb12d..42b56374d 100644 --- a/src/libcamera/process.cpp +++ b/src/libcamera/process.cpp @@ -70,6 +70,31 @@ void closeAllFdsExcept(Span fds) ASSERT(v.empty() || v.front() >= 0); +#if HAVE_CLOSE_RANGE + /* + * At the moment libcamera does not require at least Linux 5.9, + * which introduced the `close_range()` system call, so a runtime + * check is also needed to make sure that it is supported. + */ + 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 + 1); + 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;