[RFC,v5,7/9] libcamera: process: Move `closeAllFdsExcept()`
diff mbox series

Message ID 20250424114103.451395-8-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • libcamera: process: Remove `ProcessManager` singleton
Related show

Commit Message

Barnabás Pőcze April 24, 2025, 11:41 a.m. UTC
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 <barnabas.pocze@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/internal/process.h |  1 -
 src/libcamera/process.cpp            | 56 ++++++++++++++--------------
 2 files changed, 28 insertions(+), 29 deletions(-)

Comments

Kieran Bingham April 26, 2025, 1:10 p.m. UTC | #1
Quoting Barnabás Pőcze (2025-04-24 12:41:01)
> Remove `closeAllFdsExcept()` from the `Process` class and have it as a
> local function since it is no needed "outside" and does not depend on

s/it is no/it is not/


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> 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>
> ---
>  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 b8eb7e043..e55d11fa3 100644
> --- a/include/libcamera/internal/process.h
> +++ b/include/libcamera/internal/process.h
> @@ -45,7 +45,6 @@ public:
>  private:
>         LIBCAMERA_DISABLE_COPY_AND_MOVE(Process)
>  
> -       void closeAllFdsExcept(Span<const int> fds);
>         int isolate();
>         void died(int wstatus);
>  
> diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp
> index 99bc3ad36..369fdb12d 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<const int> fds)
> +{
> +       std::vector<int> 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);
> +}
> +
>  } /* namespace */
>  
>  void ProcessManager::sighandler()
> @@ -282,34 +310,6 @@ int Process::start(const std::string &path,
>         }
>  }
>  
> -void Process::closeAllFdsExcept(Span<const int> fds)
> -{
> -       std::vector<int> 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);
> -- 
> 2.49.0
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/process.h b/include/libcamera/internal/process.h
index b8eb7e043..e55d11fa3 100644
--- a/include/libcamera/internal/process.h
+++ b/include/libcamera/internal/process.h
@@ -45,7 +45,6 @@  public:
 private:
 	LIBCAMERA_DISABLE_COPY_AND_MOVE(Process)
 
-	void closeAllFdsExcept(Span<const int> fds);
 	int isolate();
 	void died(int wstatus);
 
diff --git a/src/libcamera/process.cpp b/src/libcamera/process.cpp
index 99bc3ad36..369fdb12d 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<const int> fds)
+{
+	std::vector<int> 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);
+}
+
 } /* namespace */
 
 void ProcessManager::sighandler()
@@ -282,34 +310,6 @@  int Process::start(const std::string &path,
 	}
 }
 
-void Process::closeAllFdsExcept(Span<const int> fds)
-{
-	std::vector<int> 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);