init_test: Simplify the gentle_kill test

Remove the ConvertToArgv() function. Do not cast the std::string::find()
result. Make the strace path absolute.

Change-Id: If329e91275c834ba375863e40901f6be07a5f347
Signed-off-by: Bart Van Assche <bvanassche@google.com>
This commit is contained in:
Bart Van Assche 2023-02-14 07:44:54 -08:00
parent 368a908334
commit 947d75f0b8

View file

@ -643,34 +643,19 @@ TEST(init, MemLockLimit) {
ASSERT_LE(curr_limit.rlim_max, max_limit);
}
static std::vector<const char*> ConvertToArgv(const std::vector<std::string>& args) {
std::vector<const char*> argv;
argv.reserve(args.size() + 1);
for (const auto& arg : args) {
if (argv.empty()) {
LOG(DEBUG) << arg;
} else {
LOG(DEBUG) << " " << arg;
}
argv.emplace_back(arg.data());
}
argv.emplace_back(nullptr);
return argv;
}
pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
auto argv = ConvertToArgv(args);
pid_t ForkExecvpAsync(const char* argv[]) {
pid_t pid = fork();
if (pid == 0) {
// Child process.
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
execvp(argv[0], const_cast<char**>(argv.data()));
execvp(argv[0], const_cast<char**>(argv));
PLOG(ERROR) << "exec in ForkExecvpAsync init test";
_exit(EXIT_FAILURE);
}
// Parent process.
if (pid == -1) {
PLOG(ERROR) << "fork in ForkExecvpAsync init test";
return -1;
@ -709,15 +694,10 @@ service test_gentle_kill /system/bin/sleep 1000
logfile.DoNotRemove();
ASSERT_TRUE(logfile.fd != -1);
std::vector<std::string> cmd;
cmd.push_back("system/bin/strace");
cmd.push_back("-o");
cmd.push_back(logfile.path);
cmd.push_back("-e");
cmd.push_back("signal");
cmd.push_back("-p");
cmd.push_back(std::to_string(pid));
pid_t strace_pid = ForkExecvpAsync(cmd);
std::string pid_str = std::to_string(pid);
const char* argv[] = {"/system/bin/strace", "-o", logfile.path, "-e", "signal", "-p",
pid_str.c_str(), nullptr};
pid_t strace_pid = ForkExecvpAsync(argv);
// Give strace a moment to connect
std::this_thread::sleep_for(1s);
@ -728,8 +708,7 @@ service test_gentle_kill /system/bin/sleep 1000
std::string logs;
android::base::ReadFdToString(logfile.fd, &logs);
int pos = logs.find("killed by SIGTERM");
ASSERT_NE(pos, (int)std::string::npos);
ASSERT_NE(logs.find("killed by SIGTERM"), std::string::npos);
}
class TestCaseLogger : public ::testing::EmptyTestEventListener {