ART: Clean up test exec code
Add a helper for fork+exec of another program and collection of
that process' output. Use the helper in other code.
Clean up some tests. Move away from global #ifdef ARCH and
disable tests with the usual-style macros so that it's easier
to see refactoring issues immediately.
Test: mmma
Test: m test-art-host
Change-Id: Ic450e8a3bb24fc6fe423c0e1e007eb0bb34e22b4
diff --git a/dex2oat/dex2oat_test.cc b/dex2oat/dex2oat_test.cc
index ad44624..1196d11 100644
--- a/dex2oat/dex2oat_test.cc
+++ b/dex2oat/dex2oat_test.cc
@@ -230,47 +230,15 @@
LOG(ERROR) << all_args;
}
- int link[2];
-
- if (pipe(link) == -1) {
- return false;
+ // We need dex2oat to actually log things.
+ auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; };
+ ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, &output_);
+ if (res.stage != ForkAndExecResult::kFinished) {
+ *error_msg = strerror(errno);
+ return -1;
}
-
- pid_t pid = fork();
- if (pid == -1) {
- return false;
- }
-
- if (pid == 0) {
- // We need dex2oat to actually log things.
- setenv("ANDROID_LOG_TAGS", "*:d", 1);
- dup2(link[1], STDERR_FILENO);
- close(link[0]);
- close(link[1]);
- std::vector<const char*> c_args;
- for (const std::string& str : argv) {
- c_args.push_back(str.c_str());
- }
- c_args.push_back(nullptr);
- execv(c_args[0], const_cast<char* const*>(c_args.data()));
- exit(1);
- UNREACHABLE();
- } else {
- close(link[1]);
- char buffer[128];
- memset(buffer, 0, 128);
- ssize_t bytes_read = 0;
-
- while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
- output_ += std::string(buffer, bytes_read);
- }
- close(link[0]);
- int status = -1;
- if (waitpid(pid, &status, 0) != -1) {
- success_ = (status == 0);
- }
- return status;
- }
+ success_ = res.StandardSuccess();
+ return res.status_code;
}
std::string output_ = "";