From dc9c08b9001e2b892ccbc7dc2c86a53ab5964b63 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Mon, 25 Mar 2019 18:09:35 -0700 Subject: [PATCH] charger: android_get_control_file on last_kmsg Call android_get_control_file on last_kmsg files if the file descriptor is provided by init. Also, uses base::Read(File|Fd)ToString functions to read the files (because load_file doesn't support fd arguments). Test: charger mode Test: manual kernel panic, then start charger; seen last kmsg. Bug: 129138950 Change-Id: Idd3376e349f29586a1e66faab2c0f1bf73e0eda5 --- healthd/healthd_mode_charger.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp index 5fe58ac5a..bde5fbe4a 100644 --- a/healthd/healthd_mode_charger.cpp +++ b/healthd/healthd_mode_charger.cpp @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -206,10 +207,9 @@ static int64_t curr_time_ms() { #define MAX_KLOG_WRITE_BUF_SZ 256 static void dump_last_kmsg(void) { - char* buf; + std::string buf; char* ptr; - unsigned sz = 0; - int len; + size_t len; LOGW("\n"); LOGW("*************** LAST KMSG ***************\n"); @@ -221,21 +221,25 @@ static void dump_last_kmsg(void) { "/proc/last_kmsg", // clang-format on }; - for (size_t i = 0; i < arraysize(kmsg); ++i) { - buf = (char*)load_file(kmsg[i], &sz); - if (buf && sz) break; + for (size_t i = 0; i < arraysize(kmsg) && buf.empty(); ++i) { + auto fd = android_get_control_file(kmsg[i]); + if (fd >= 0) { + android::base::ReadFdToString(fd, &buf); + } else { + android::base::ReadFileToString(kmsg[i], &buf); + } } - if (!buf || !sz) { + if (buf.empty()) { LOGW("last_kmsg not found. Cold reset?\n"); goto out; } - len = min(sz, LAST_KMSG_MAX_SZ); - ptr = buf + (sz - len); + len = min(buf.size(), LAST_KMSG_MAX_SZ); + ptr = &buf[buf.size() - len]; while (len > 0) { - int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ); + size_t cnt = min(len, MAX_KLOG_WRITE_BUF_SZ); char yoink; char* nl; @@ -251,8 +255,6 @@ static void dump_last_kmsg(void) { ptr += cnt; } - free(buf); - out: LOGW("\n"); LOGW("************* END LAST KMSG *************\n");