1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/utils/subprocess.h"
7 #include <manifest_parser/utils/logging.h>
10 #include <sys/types.h>
16 namespace common_installer {
18 Subprocess::Subprocess(const std::string& program)
25 bool Subprocess::RunWithArgs(const std::vector<std::string>& args) {
27 LOG(WARNING) << "Process already started";
32 std::unique_ptr<const char*[]> argv(new const char*[2 + args.size()]);
33 argv[0] = program_.c_str();
34 for (size_t i = 1; i <= args.size(); ++i) {
35 argv[i] = args[i - 1].c_str();
37 argv[args.size() + 1] = nullptr;
40 LOG(ERROR) << "Failed to setuid";
44 execvp(argv[0], const_cast<char* const*>(argv.get()));
45 LOG(ERROR) << "Failed to execv";
47 } else if (pid_ == -1) {
48 LOG(ERROR) << "Failed to fork";
56 bool Subprocess::Wait() {
58 LOG(WARNING) << "Process is not started. Cannot wait";
62 waitpid(pid_, &status, 0);
63 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
69 void Subprocess::Kill() {
70 int ret = kill(pid_, SIGKILL);
72 LOG(ERROR) << "kill failed";
75 } // namespace common_installer