libcamera: process: Use close_range() when available

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 <barnabas.pocze@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Barnabás Pőcze
2025-02-27 13:05:16 +01:00
parent d44ec357d2
commit 4db96b6e66
2 changed files with 29 additions and 0 deletions

View File

@@ -69,6 +69,31 @@ void closeAllFdsExcept(std::vector<int> v)
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;