libcamera: process: Move closeAllFdsExcept()

Remove `closeAllFdsExcept()` from the `Process` class and have it as a
local function since it is not needed "outside" and does not depend on
any part of the `Process` class.

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-01-22 12:40:29 +01:00
parent b43691af94
commit d44ec357d2
2 changed files with 27 additions and 28 deletions

View File

@@ -63,6 +63,33 @@ void sigact(int signal, siginfo_t *info, void *ucontext)
}
}
void closeAllFdsExcept(std::vector<int> v)
{
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()
@@ -296,33 +323,6 @@ int Process::start(const std::string &path,
}
}
void Process::closeAllFdsExcept(std::vector<int> v)
{
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);