Exclude dlog fds from closing fds 02/299902/1
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Oct 2023 01:03:39 +0000 (10:03 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Oct 2023 01:03:39 +0000 (10:03 +0900)
To print dlogs, the process pool checks whether the file descriptor is
dlog fd or not. And then, the dlog fds will be excluded from closing fds.

Change-Id: I86aee6554ec3ba7ad8cfbac395a84e75b629b52b
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/launchpad-process-pool/process_pool.cc

index c7bb40f..d91bc58 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "launchpad-process-pool/process_pool.hh"
 
+#include <dlog-redirect-stdout.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <filesystem>
 #include <utility>
+#include <vector>
 
 #include <util.hh>
 
 #include "launchpad-process-pool/launchpad_args.hh"
 #include "launchpad-process-pool/log_private.hh"
 
+namespace fs = std::filesystem;
+
 namespace launchpad {
 namespace {
 
 constexpr const char kProcessPool[] = "process-pool";
 
+std::vector<int> GetDlogFds() {
+  std::vector<int> fds;
+  try {
+    fs::path proc_path("/proc/self/fd");
+    for (const auto& entry : fs::directory_iterator(proc_path)) {
+      if (!isdigit(entry.path().filename().string()[0]))
+        continue;
+
+      int fd = std::stoi(entry.path().filename().string());
+      if (dlog_is_log_fd(fd))
+        fds.push_back(fd);
+    }
+  } catch (const fs::filesystem_error& e) {
+    _E("Exception occurs. error(%s)", e.what());
+  }
+
+  return fds;
+}
+
 }  // namespace
 
 ProcessPool::ProcessPool(std::string name, int num_processes,
@@ -112,7 +136,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] };
+  std::vector<int> except_fds = GetDlogFds();
+  except_fds.push_back(pipe_fd_[0]);
   Util::CloseAllFds(except_fds);
   int ret = WaitForRequest(std::make_unique<Socket>(pipe_fd_[0]));
   exit(ret);