diff options
author | 2022-06-29 17:09:51 +0100 | |
---|---|---|
committer | 2022-07-04 16:25:06 +0000 | |
commit | f0061f62fbd5e4766220c154d914e7b9d07a86eb (patch) | |
tree | 28aa2f340a2436172d4995451d945eb7703488f8 /runtime/exec_utils_test.cc | |
parent | f5970c3ffc4aebc8e33f91370d55586ee446f501 (diff) |
Update ExecUtils to support artd use cases.
- Use pidfd_open/poll instaed of sigprocmask/sigtimedwait to implement
executing a command with a timeout because sigprocmask/sigtimedwait
does not support multithreaded program, while artd is multithreaded.
The same approach is used in installd in
https://r.android.com/1993670.
- Add "const" to the `std::vector<std::string>& arg_vector` parameter.
- Use "int" instead of "time_t" for the "timeout_sec" parameter because
"time_t" represents the time since epoch rather than a duration.
- Set an error message with the signal number when the subprocess is
terminated by a signal.
- Crash the subprocess instead of exiting it with an exit code when
execv fails.
The behavior does not change. ExecUtilsTest is still passing.
Bug: 229268202
Test: m test-art-host-gtest-art_runtime_tests
Test: atest ArtGtestsTargetChroot:ExecUtilsTest
Change-Id: I375034162cded6fdf09ee4d4cd783a0d3987af49
Merged-In: I375034162cded6fdf09ee4d4cd783a0d3987af49
(cherry picked from commit b48b732c535894bbae8f3c19eceeda67c40e9d61)
Diffstat (limited to 'runtime/exec_utils_test.cc')
-rw-r--r-- | runtime/exec_utils_test.cc | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/runtime/exec_utils_test.cc b/runtime/exec_utils_test.cc index dc789aa292..aa53739cfa 100644 --- a/runtime/exec_utils_test.cc +++ b/runtime/exec_utils_test.cc @@ -16,16 +16,34 @@ #include "exec_utils.h" +#include <unistd.h> + #include "android-base/stringprintf.h" #include "base/file_utils.h" #include "base/memory_tool.h" #include "common_runtime_test.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" namespace art { std::string PrettyArguments(const char* signature); std::string PrettyReturnType(const char* signature); +bool IsPidfdSupported() { +#ifdef __BIONIC__ + return true; +#else + constexpr int SYS_pidfd_open = 434; + int pidfd = syscall(SYS_pidfd_open, getpid(), /*flags=*/0); + if (pidfd < 0) { + return false; + } + close(pidfd); + return true; +#endif +} + class ExecUtilsTest : public CommonRuntimeTest {}; TEST_F(ExecUtilsTest, ExecSuccess) { @@ -44,9 +62,6 @@ TEST_F(ExecUtilsTest, ExecSuccess) { } TEST_F(ExecUtilsTest, ExecError) { - // This will lead to error messages in the log. - ScopedLogSeverity sls(LogSeverity::FATAL); - std::vector<std::string> command; command.push_back("bogus"); std::string error_msg; @@ -115,6 +130,10 @@ static std::vector<std::string> SleepCommand(int sleep_seconds) { } TEST_F(ExecUtilsTest, ExecTimeout) { + if (!IsPidfdSupported()) { + GTEST_SKIP() << "pidfd not supported"; + } + static constexpr int kSleepSeconds = 5; static constexpr int kWaitSeconds = 1; std::vector<std::string> command = SleepCommand(kSleepSeconds); @@ -125,6 +144,10 @@ TEST_F(ExecUtilsTest, ExecTimeout) { } TEST_F(ExecUtilsTest, ExecNoTimeout) { + if (!IsPidfdSupported()) { + GTEST_SKIP() << "pidfd not supported"; + } + static constexpr int kSleepSeconds = 1; static constexpr int kWaitSeconds = 5; std::vector<std::string> command = SleepCommand(kSleepSeconds); @@ -134,4 +157,15 @@ TEST_F(ExecUtilsTest, ExecNoTimeout) { EXPECT_FALSE(timed_out); } +TEST_F(ExecUtilsTest, ExecTimeoutNotSupported) { + if (IsPidfdSupported()) { + GTEST_SKIP() << "pidfd supported"; + } + + std::string error_msg; + bool timed_out; + ASSERT_EQ(ExecAndReturnCode({"command"}, /*timeout_sec=*/0, &timed_out, &error_msg), -1); + EXPECT_THAT(error_msg, testing::HasSubstr("pidfd_open failed for pid")); +} + } // namespace art |