Close all fds while creating process pool 57/297157/4
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 11 Aug 2023 06:26:58 +0000 (15:26 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 11 Aug 2023 07:43:30 +0000 (16:43 +0900)
After calling fork(), the child process should close all unused file
descriptors.

Change-Id: I5c0e714dff3bff182fdafbeb1153da5b4e38d0b7
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/launchpad.cc
src/launchpad-process-pool/loader_manager.cc
src/launchpad-process-pool/process_pool.cc
src/lib/launchpad-glib/util.cc
src/lib/launchpad-glib/util.hh

index 69ddf1a..bc51ef5 100644 (file)
@@ -497,6 +497,7 @@ Launchpad::LaunchResult Launchpad::LaunchRequestPend(
   if (loader_context->GetPid() > 0) {
     CPUBoostController::DoBoost(loader_context->GetPid(),
         CPUBoostController::Level::Strong, 10000);
+    _W("Send result: %d", loader_context->GetPid());
     request->SendResult(loader_context->GetPid());
   }
 
index 8e4fc3e..4ddee1c 100644 (file)
@@ -458,6 +458,13 @@ void LoaderManager::OnAppLabelsChanged() {
         hydra_context->Prepare();
       }
     } else if (context->GetPid() > 0) {
+      if (context->RefCount() != 0) {
+        _W("Except. type(%d), loader_name(%s), pid(%d)",
+            context->GetType(), context->GetLoaderName().c_str(),
+            context->GetPid());
+        continue;
+      }
+
       context->Dispose();
       context->Prepare();
     }
index f8c010f..c7bb40f 100644 (file)
@@ -25,6 +25,8 @@
 
 #include <utility>
 
+#include <util.hh>
+
 #include "launchpad-process-pool/launchpad_args.hh"
 #include "launchpad-process-pool/log_private.hh"
 
@@ -110,6 +112,8 @@ void ProcessPool::OnExecution() {
   snprintf(args[0], length, "/usr/bin/%s <%s>", kProcessPool, name_.c_str());
 
   close(pipe_fd_[1]);
+  std::vector<int> except_fds { pipe_fd_[0] };
+  Util::CloseAllFds(except_fds);
   int ret = WaitForRequest(std::make_unique<Socket>(pipe_fd_[0]));
   exit(ret);
 }
index 9ab6d9e..f3ac15a 100644 (file)
@@ -26,6 +26,7 @@
 #include <tzplatform_config.h>
 #include <unistd.h>
 
+#include <algorithm>
 #include <filesystem>
 #include <fstream>
 #include <memory>
@@ -469,7 +470,7 @@ std::string Util::GetLibDirectory(const std::string& app_path) {
   return "";
 }
 
-void Util::CloseAllFds() {
+void Util::CloseAllFds(const std::vector<int>& except_fds) {
   int aul_fd = -1;
   const char* aul_listen_fd = getenv("AUL_LISTEN_FD");
   if (aul_listen_fd != nullptr)
@@ -486,6 +487,13 @@ void Util::CloseAllFds() {
       if (fd < 3 || fd == aul_fd)
         continue;
 
+      auto found = std::find_if(except_fds.begin(), except_fds.end(),
+          [&](int except_fd) {
+            return except_fd == fd;
+          });
+      if (found != except_fds.end())
+        continue;
+
       fds.push_back(fd);
     }
   } catch (const fs::filesystem_error& e) {
index 9dfeaff..013581c 100644 (file)
@@ -21,6 +21,7 @@
 #include <tzplatform_config.h>
 
 #include <string>
+#include <vector>
 
 #include <app_info.hh>
 #include <types.hh>
@@ -41,7 +42,7 @@ class EXPORT_API Util {
   static int MountResourceDirectories(const AppInfo* app_info);
   static int WaitTepMount(const AppInfo* app_info);
   static std::string GetLibDirectory(const std::string& app_path);
-  static void CloseAllFds();
+  static void CloseAllFds(const std::vector<int>& except_fds = {});
   static int PrepareAppSocket();
   static int PrepareAppIdFile(const AppInfo* app_info);
   static int SendCmdToAmd(enum AmdCmd cmd);