summaryrefslogtreecommitdiff
path: root/runtime/utils.cc
diff options
context:
space:
mode:
author Brian Carlstrom <bdc@google.com> 2014-02-25 04:42:53 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2014-02-25 04:42:54 +0000
commitfffb0b7e23796e5470f4fab4611f2fcc4a16979c (patch)
tree3c2fd045f635a0511149272af9ff55a01a31b83c /runtime/utils.cc
parenta0c9b085d4ecf90ca3aa1252e81e65072b377ca4 (diff)
parent6449c62e40ef3a9bb75f664f922555affb532ee4 (diff)
Merge "Create CompilerOptions"
Diffstat (limited to 'runtime/utils.cc')
-rw-r--r--runtime/utils.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/runtime/utils.cc b/runtime/utils.cc
index aad21bca85..8e6ddaf0c9 100644
--- a/runtime/utils.cc
+++ b/runtime/utils.cc
@@ -24,6 +24,7 @@
#include <unistd.h>
#include "UniquePtr.h"
+#include "base/stl_util.h"
#include "base/unix_file/fd_file.h"
#include "dex_file-inl.h"
#include "mirror/art_field-inl.h"
@@ -1203,4 +1204,56 @@ bool IsOatMagic(uint32_t magic) {
sizeof(OatHeader::kOatMagic)) == 0);
}
+bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg) {
+ const std::string command_line(Join(arg_vector, ' '));
+
+ CHECK_GE(arg_vector.size(), 1U) << command_line;
+
+ // Convert the args to char pointers.
+ const char* program = arg_vector[0].c_str();
+ std::vector<char*> args;
+ for (std::vector<std::string>::const_iterator it = arg_vector.begin(); it != arg_vector.end();
+ ++it) {
+ CHECK(*it != nullptr);
+ args.push_back(const_cast<char*>(it->c_str()));
+ }
+ args.push_back(NULL);
+
+ // fork and exec
+ pid_t pid = fork();
+ if (pid == 0) {
+ // no allocation allowed between fork and exec
+
+ // change process groups, so we don't get reaped by ProcessManager
+ setpgid(0, 0);
+
+ execv(program, &args[0]);
+
+ *error_msg = StringPrintf("Failed to execv(%s): %s", command_line.c_str(), strerror(errno));
+ return false;
+ } else {
+ if (pid == -1) {
+ *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
+ command_line.c_str(), strerror(errno));
+ return false;
+ }
+
+ // wait for subprocess to finish
+ int status;
+ pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
+ if (got_pid != pid) {
+ *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
+ "wanted %d, got %d: %s",
+ command_line.c_str(), pid, got_pid, strerror(errno));
+ return false;
+ }
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
+ command_line.c_str());
+ return false;
+ }
+ }
+ return true;
+}
+
} // namespace art