From 3aa38628082d8e835324724fbdb831e6d08ded73 Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Fri, 8 Jan 2016 13:58:32 +0100 Subject: [PATCH] Subprocess utility Change-Id: I6efadead6c4eed68e38746b3dcbb615fbde5a921 --- src/common/CMakeLists.txt | 1 + src/common/utils/subprocess.cc | 58 ++++++++++++++++++++++++++++++++++++++++++ src/common/utils/subprocess.h | 34 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/common/utils/subprocess.cc create mode 100644 src/common/utils/subprocess.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 94cac15..55e1e15 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -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 index 0000000..a400c4c --- /dev/null +++ b/src/common/utils/subprocess.cc @@ -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 + +#include +#include +#include + +#include + +namespace common_installer { + +Subprocess::Subprocess(const std::string& program) + : program_(program), + pid_(0), + started_(false) { +} + +bool Subprocess::RunWithArgs(const std::vector& args) { + if (started_) { + LOG(WARNING) << "Process already started"; + return false; + } + pid_ = fork(); + if (pid_ == 0) { + std::unique_ptr 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(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 index 0000000..9347c81 --- /dev/null +++ b/src/common/utils/subprocess.h @@ -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 +#include + +namespace common_installer { + +class Subprocess { + public: + explicit Subprocess(const std::string& program); + + template bool Run(Args&&... args) { + std::vector argv{std::forward(args)...}; + return RunWithArgs(argv); + } + + bool RunWithArgs( + const std::vector& args = std::vector()); + int Wait(); + + private: + std::string program_; + int pid_; + bool started_; +}; + +} // namespace common_installer + +#endif // COMMON_UTILS_SUBPROCESS_H_ -- 2.7.4