Subprocess utility 03/56503/3
authorTomasz Iwanek <t.iwanek@samsung.com>
Fri, 8 Jan 2016 12:58:32 +0000 (13:58 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Mon, 11 Jan 2016 15:10:45 +0000 (07:10 -0800)
Change-Id: I6efadead6c4eed68e38746b3dcbb615fbde5a921

src/common/CMakeLists.txt
src/common/utils/subprocess.cc [new file with mode: 0644]
src/common/utils/subprocess.h [new file with mode: 0644]

index 94cac15..55e1e15 100644 (file)
@@ -49,6 +49,7 @@ SET(SRCS
   step/step_update_security.cc
   step/step_update_tep.cc
   utils/file_util.cc
+  utils/subprocess.cc
 )
 # Target - definition
 ADD_LIBRARY(${TARGET_LIBNAME_COMMON} SHARED ${SRCS})
diff --git a/src/common/utils/subprocess.cc b/src/common/utils/subprocess.cc
new file mode 100644 (file)
index 0000000..a400c4c
--- /dev/null
@@ -0,0 +1,58 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#include "common/utils/subprocess.h"
+
+#include <manifest_parser/utils/logging.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include <memory>
+
+namespace common_installer {
+
+Subprocess::Subprocess(const std::string& program)
+    : program_(program),
+      pid_(0),
+      started_(false) {
+}
+
+bool Subprocess::RunWithArgs(const std::vector<std::string>& args) {
+  if (started_) {
+    LOG(WARNING) << "Process already started";
+    return false;
+  }
+  pid_ = fork();
+  if (pid_ == 0) {
+    std::unique_ptr<const char*[]> argv(new const char*[2 + args.size()]);
+    argv[0] = program_.c_str();
+    for (size_t i = 1; i <= args.size(); ++i) {
+      argv[i] = args[i - 1].c_str();
+    }
+    argv[args.size() + 1] = nullptr;
+    execvp(argv[0], const_cast<char* const*>(argv.get()));
+    LOG(ERROR) << "Failed to execv";
+    return false;
+  } else if (pid_ == -1) {
+    LOG(ERROR) << "Failed to fork";
+    return false;
+  } else {
+    started_ = true;
+    return true;
+  }
+}
+
+int Subprocess::Wait() {
+  if (!started_) {
+    LOG(WARNING) << "Process is not started. Cannot wait";
+    return -1;
+  }
+  int status;
+  waitpid(pid_, &status, 0);
+  return status;
+}
+
+}  // namespace common_installer
diff --git a/src/common/utils/subprocess.h b/src/common/utils/subprocess.h
new file mode 100644 (file)
index 0000000..9347c81
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef COMMON_UTILS_SUBPROCESS_H_
+#define COMMON_UTILS_SUBPROCESS_H_
+
+#include <string>
+#include <vector>
+
+namespace common_installer {
+
+class Subprocess {
+ public:
+  explicit Subprocess(const std::string& program);
+
+  template<typename ...Args> bool Run(Args&&... args) {
+    std::vector<std::string> argv{std::forward<Args>(args)...};
+    return RunWithArgs(argv);
+  }
+
+  bool RunWithArgs(
+      const std::vector<std::string>& args = std::vector<std::string>());
+  int Wait();
+
+ private:
+  std::string program_;
+  int pid_;
+  bool started_;
+};
+
+}  // namespace common_installer
+
+#endif  // COMMON_UTILS_SUBPROCESS_H_