From 72f8ca9f6ff4ee53e382c73bac1eaba6e2c01022 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 11 Aug 2023 15:26:58 +0900 Subject: [PATCH] Close all fds while creating process pool After calling fork(), the child process should close all unused file descriptors. Change-Id: I5c0e714dff3bff182fdafbeb1153da5b4e38d0b7 Signed-off-by: Hwankyu Jhun --- src/launchpad-process-pool/launchpad.cc | 1 + src/launchpad-process-pool/loader_manager.cc | 7 +++++++ src/launchpad-process-pool/process_pool.cc | 4 ++++ src/lib/launchpad-glib/util.cc | 10 +++++++++- src/lib/launchpad-glib/util.hh | 3 ++- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/launchpad-process-pool/launchpad.cc b/src/launchpad-process-pool/launchpad.cc index 69ddf1a..bc51ef5 100644 --- a/src/launchpad-process-pool/launchpad.cc +++ b/src/launchpad-process-pool/launchpad.cc @@ -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()); } diff --git a/src/launchpad-process-pool/loader_manager.cc b/src/launchpad-process-pool/loader_manager.cc index 8e4fc3e..4ddee1c 100644 --- a/src/launchpad-process-pool/loader_manager.cc +++ b/src/launchpad-process-pool/loader_manager.cc @@ -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(); } diff --git a/src/launchpad-process-pool/process_pool.cc b/src/launchpad-process-pool/process_pool.cc index f8c010f..c7bb40f 100644 --- a/src/launchpad-process-pool/process_pool.cc +++ b/src/launchpad-process-pool/process_pool.cc @@ -25,6 +25,8 @@ #include +#include + #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 except_fds { pipe_fd_[0] }; + Util::CloseAllFds(except_fds); int ret = WaitForRequest(std::make_unique(pipe_fd_[0])); exit(ret); } diff --git a/src/lib/launchpad-glib/util.cc b/src/lib/launchpad-glib/util.cc index 9ab6d9e..f3ac15a 100644 --- a/src/lib/launchpad-glib/util.cc +++ b/src/lib/launchpad-glib/util.cc @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -469,7 +470,7 @@ std::string Util::GetLibDirectory(const std::string& app_path) { return ""; } -void Util::CloseAllFds() { +void Util::CloseAllFds(const std::vector& 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) { diff --git a/src/lib/launchpad-glib/util.hh b/src/lib/launchpad-glib/util.hh index 9dfeaff..013581c 100644 --- a/src/lib/launchpad-glib/util.hh +++ b/src/lib/launchpad-glib/util.hh @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -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& except_fds = {}); static int PrepareAppSocket(); static int PrepareAppIdFile(const AppInfo* app_info); static int SendCmdToAmd(enum AmdCmd cmd); -- 2.7.4