lmkd_test: align proc_name with lmkd

This change is to align how we get proc_name
between lmkd and lmkd_unit_test or it will always
fail when we run command such as ./lmkd_unit_test
under the device shell.

Bug: 146415484
Test: Verified using lmkd_unit_test
Change-Id: I3a6ebf0747a1054c5758b91fbacf3179b66b56c3
This commit is contained in:
Martin Liu 2019-12-18 13:05:25 +08:00
parent 43f3d2b190
commit f2ead0ca1a
1 changed files with 11 additions and 10 deletions

View File

@ -109,14 +109,15 @@ std::string getTextAround(const std::string& text, size_t pos,
std::string::npos : pos - start_pos);
}
bool getExecPath(std::string &path) {
char buf[PATH_MAX + 1];
int ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
if (ret < 0) {
bool getTaskName(std::string &name) {
std::string cmdline;
if (!ReadFileToString("/proc/self/cmdline", &cmdline)) {
GTEST_LOG_(INFO) << "Failed to read /proc/self/cmdline";
return false;
}
buf[ret] = '\0';
path = buf;
//filter out paramters as cmdline use null bytes to separate
name = cmdline.c_str();
return true;
}
@ -308,15 +309,15 @@ TEST(lmkd, check_for_oom) {
ASSERT_TRUE(writeKmsg(marker));
// get executable complete path
std::string test_path;
ASSERT_TRUE(getExecPath(test_path));
std::string task_name;
ASSERT_TRUE(getTaskName(task_name));
std::string test_output;
if (getuid() != static_cast<unsigned>(AID_ROOT)) {
// if not root respawn itself as root and capture output
std::string command = StringPrintf(
"%s=true su root %s 2>&1", LMKDTEST_RESPAWN_FLAG,
test_path.c_str());
task_name.c_str());
std::string test_output = readCommand(command);
GTEST_LOG_(INFO) << test_output;
} else {
@ -332,7 +333,7 @@ TEST(lmkd, check_for_oom) {
std::stringstream kill_logs;
int hit_count = 0;
size_t pos = 0;
marker = StringPrintf(LMKD_KILL_MARKER_TEMPLATE, test_path.c_str());
marker = StringPrintf(LMKD_KILL_MARKER_TEMPLATE, task_name.c_str());
while (true) {
if ((pos = logcat_out.find(marker, pos)) != std::string::npos) {