Remove previous files at fork 30/299930/1
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Oct 2023 07:43:37 +0000 (16:43 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Oct 2023 07:43:37 +0000 (16:43 +0900)
This patch uses pthread_atfork() to register a fork handler.
While creating a children process, we should remove previous files
related to the process ID.

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

index de8b624..493a75e 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "launchpad-process-pool/signal_manager.hh"
 
+#include <pthread.h>
 #include <sys/signalfd.h>
 
 #include <filesystem>
@@ -49,6 +50,18 @@ class GarbageCollector : public launchpad::Worker::Job {
     }
   }
 
+ void DoAtFork() {
+   _W("pid: %d", pid_);
+    try {
+      std::string path = "/run/aul/apps/" + std::to_string(getuid()) + "/" +
+          std::to_string(pid_);
+      DeleteSocketPath(fs::path(path));
+      DeleteUnusedFiles();
+    } catch (const std::filesystem::filesystem_error& e) {
+      _E("Exception occurs. error(%s:%d)", e.what(), e.code().value());
+    }
+ }
+
  private:
   void DeleteSocketPath(const fs::path& path) {
     try {
@@ -121,6 +134,7 @@ void SignalManager::Dispose() {
   recycle_bin_.reset();
   hydra_sigchld_event_.reset();
   sigchld_event_.reset();
+  pthread_atfork(nullptr, nullptr, nullptr);
   disposed_ = true;
   _W("END");
 }
@@ -154,6 +168,12 @@ void SignalManager::Init() {
     signal(signo, SIG_DFL);
   }
 
+  pthread_atfork(nullptr, nullptr,
+      []() {
+        GarbageCollector gc(getpid());
+        gc.DoAtFork();
+      });
+
   disposed_ = false;
   _W("END");
 }