From f2ead0ca1a2f5f432a51028f886f4e7d55a62d55 Mon Sep 17 00:00:00 2001 From: Martin Liu Date: Wed, 18 Dec 2019 13:05:25 +0800 Subject: [PATCH] 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 --- tests/lmkd_test.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/lmkd_test.cpp b/tests/lmkd_test.cpp index f54b25c..5dbf6db 100644 --- a/tests/lmkd_test.cpp +++ b/tests/lmkd_test.cpp @@ -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(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) {