From cf419a7786245cf1a31ed039f4785b3d2cebf749 Mon Sep 17 00:00:00 2001 From: Nandana Dutt Date: Thu, 14 Mar 2019 10:40:17 +0000 Subject: Check for user consent denial when dumping traces. VM traces is one of the longer running operations. Check consent periodically and return early if necessary. BUG:128270426 Test: manual; by canceling when traces was running. Test: verified "VM TRACES JUST NOW" shows up as expected after refactoring in the normal flow as well. Change-Id: Ic5282b948929857850730d6b06ec0cae8c39a99f --- cmds/dumpstate/dumpstate.cpp | 18 +++++++++--------- cmds/dumpstate/dumpstate.h | 7 +++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp index a32fffdcbb..daee7e5597 100644 --- a/cmds/dumpstate/dumpstate.cpp +++ b/cmds/dumpstate/dumpstate.cpp @@ -1495,14 +1495,12 @@ static Dumpstate::RunStatus DumpstateDefault() { // Try to dump anrd trace if the daemon is running. dump_anrd_trace(); - // Invoking the following dumpsys calls before dump_traces() to try and + // Invoking the following dumpsys calls before DumpTraces() to try and // keep the system stats as close to its initial state as possible. RUN_SLOW_FUNCTION_WITH_CONSENT_CHECK(RunDumpsysCritical); /* collect stack traces from Dalvik and native processes (needs root) */ - // TODO(128270426): Refactor to take output argument and wrap in - // RUN_SLOW_FUNCTION_WITH_CONSENT_CHECK. - dump_traces_path = ds.DumpTraces(); + RUN_SLOW_FUNCTION_WITH_CONSENT_CHECK(ds.DumpTraces, &dump_traces_path); /* Run some operations that require root. */ ds.tombstone_data_ = GetDumpFds(TOMBSTONE_DIR, TOMBSTONE_FILE_PREFIX, !ds.IsZipping()); @@ -1632,7 +1630,7 @@ static void DumpstateWifiOnly() { printf("========================================================\n"); } -const char* Dumpstate::DumpTraces() { +Dumpstate::RunStatus Dumpstate::DumpTraces(const char** path) { DurationReporter duration_reporter("DUMP TRACES"); const std::string temp_file_pattern = "/data/anr/dumptrace_XXXXXX"; @@ -1648,7 +1646,7 @@ const char* Dumpstate::DumpTraces() { android::base::unique_fd fd(mkostemp(file_name_buf.get(), O_APPEND | O_CLOEXEC)); if (fd < 0) { MYLOGE("mkostemp on pattern %s: %s\n", file_name_buf.get(), strerror(errno)); - return nullptr; + return RunStatus::OK; } // Nobody should have access to this temporary file except dumpstate, but we @@ -1658,13 +1656,13 @@ const char* Dumpstate::DumpTraces() { const int chmod_ret = fchmod(fd, 0666); if (chmod_ret < 0) { MYLOGE("fchmod on %s failed: %s\n", file_name_buf.get(), strerror(errno)); - return nullptr; + return RunStatus::OK; } std::unique_ptr proc(opendir("/proc"), closedir); if (proc.get() == nullptr) { MYLOGE("opendir /proc failed: %s\n", strerror(errno)); - return nullptr; + return RunStatus::OK; } // Number of times process dumping has timed out. If we encounter too many @@ -1676,6 +1674,7 @@ const char* Dumpstate::DumpTraces() { struct dirent* d; while ((d = readdir(proc.get()))) { + RETURN_IF_USER_DENIED_CONSENT(); int pid = atoi(d->d_name); if (pid <= 0) { continue; @@ -1737,7 +1736,8 @@ const char* Dumpstate::DumpTraces() { MYLOGE("Warning: no Dalvik processes found to dump stacks\n"); } - return file_name_buf.release(); + *path = file_name_buf.release(); + return RunStatus::OK; } void Dumpstate::DumpstateBoard() { diff --git a/cmds/dumpstate/dumpstate.h b/cmds/dumpstate/dumpstate.h index 1463b8b9ba..d02ec759a7 100644 --- a/cmds/dumpstate/dumpstate.h +++ b/cmds/dumpstate/dumpstate.h @@ -291,8 +291,11 @@ class Dumpstate { // TODO: temporary method until Dumpstate object is properly set void SetProgress(std::unique_ptr progress); - // Dumps Dalvik and native stack traces, return the trace file location (nullptr if none). - const char* DumpTraces(); + // Dumps Dalvik and native stack traces, sets the trace file location to path + // if it succeeded. + // Note that it returns early if user consent is denied with status USER_CONSENT_DENIED. + // Returns OK in all other cases. + RunStatus DumpTraces(const char** path); void DumpstateBoard(); -- cgit v1.2.3-59-g8ed1b