Add RunFunc to Subprocess for just executing function
[platform/core/appfw/app-installers.git] / src / common / utils / subprocess.h
index 42527ee..79d7e3b 100644 (file)
@@ -8,6 +8,8 @@
 #include <string>
 #include <utility>
 #include <vector>
+#include <functional>
+#include <unistd.h>
 
 namespace common_installer {
 
@@ -20,6 +22,23 @@ class Subprocess {
     return RunWithArgs(argv);
   }
 
+  template <typename... Args>
+  bool RunFunc(std::function<int(Args...)> func, Args&&... args) {
+    if (started_) {
+      return false;
+    }
+    pid_ = fork();
+    if (pid_ == 0) {
+      exit(func(std::forward<Args>(args)...));
+      return false;
+    } else if (pid_ == -1) {
+      return false;
+    } else {
+      started_ = true;
+      return true;
+    }
+  }
+
   bool RunWithArgs(
       const std::vector<std::string>& args = std::vector<std::string>());
   int Wait();