From 3cd2c9ae0d97765cf89c39c47db65f884566ad77 Mon Sep 17 00:00:00 2001 From: Adam Shih Date: Fri, 17 May 2024 03:14:50 +0000 Subject: [PATCH] Create an isolated copy of vendor dumpstate from all devices Bug: 326153087 Test: make android.hardware.dumpstate-service on husky and cheetah target without breaking Change-Id: I9053d813e4be75ac279beeec77fdcefed08396f9 --- gear/dumpstate_v3/Android.bp | 29 +++ gear/dumpstate_v3/Dumpstate.cpp | 230 ++++++++++++++++++ gear/dumpstate_v3/Dumpstate.h | 53 ++++ gear/dumpstate_v3/aidl.mk | 4 + .../android.hardware.dumpstate.3-service.rc | 5 + .../android.hardware.dumpstate.3-service.xml | 9 + gear/dumpstate_v3/sepolicy/file.te | 6 + gear/dumpstate_v3/sepolicy/file_contexts | 5 + .../sepolicy/hal_dumpstate_default.te | 7 + gear/dumpstate_v3/sepolicy/property.te | 3 + gear/dumpstate_v3/sepolicy/property_contexts | 3 + gear/dumpstate_v3/service.cpp | 37 +++ 12 files changed, 391 insertions(+) create mode 100644 gear/dumpstate_v3/Android.bp create mode 100644 gear/dumpstate_v3/Dumpstate.cpp create mode 100644 gear/dumpstate_v3/Dumpstate.h create mode 100644 gear/dumpstate_v3/aidl.mk create mode 100644 gear/dumpstate_v3/android.hardware.dumpstate.3-service.rc create mode 100644 gear/dumpstate_v3/android.hardware.dumpstate.3-service.xml create mode 100644 gear/dumpstate_v3/sepolicy/file.te create mode 100644 gear/dumpstate_v3/sepolicy/file_contexts create mode 100644 gear/dumpstate_v3/sepolicy/hal_dumpstate_default.te create mode 100644 gear/dumpstate_v3/sepolicy/property.te create mode 100644 gear/dumpstate_v3/sepolicy/property_contexts create mode 100644 gear/dumpstate_v3/service.cpp diff --git a/gear/dumpstate_v3/Android.bp b/gear/dumpstate_v3/Android.bp new file mode 100644 index 0000000..d1ab29c --- /dev/null +++ b/gear/dumpstate_v3/Android.bp @@ -0,0 +1,29 @@ +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +cc_binary { + name: "android.hardware.dumpstate.3-service", + srcs: [ + "Dumpstate.cpp", + "service.cpp", + ], + init_rc: ["android.hardware.dumpstate.3-service.rc"], + vintf_fragments: ["android.hardware.dumpstate.3-service.xml"], + cflags: [ + "-Wall", + "-Werror", + ], + shared_libs: [ + "libbase", + "libbinder_ndk", + "libcutils", + "libdumpstateutil", + "liblog", + "libutils", + "libdump", + "android.hardware.dumpstate-V1-ndk", + ], + vendor: true, + relative_install_path: "hw", +} diff --git a/gear/dumpstate_v3/Dumpstate.cpp b/gear/dumpstate_v3/Dumpstate.cpp new file mode 100644 index 0000000..31df2c7 --- /dev/null +++ b/gear/dumpstate_v3/Dumpstate.cpp @@ -0,0 +1,230 @@ +/* + * Copyright 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "dumpstate_device" +#define ATRACE_TAG ATRACE_TAG_ALWAYS + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Dumpstate.h" + +#include "DumpstateUtil.h" + +#define HW_REVISION "ro.boot.hardware.revision" + +using android::os::dumpstate::CommandOptions; +using android::os::dumpstate::DumpFileToFd; +using android::os::dumpstate::PropertiesHelper; +using android::os::dumpstate::RunCommandToFd; + +namespace aidl { +namespace android { +namespace hardware { +namespace dumpstate { + +typedef std::chrono::time_point timepoint_t; + +const char kVerboseLoggingProperty[] = "persist.vendor.verbose_logging_enabled"; + +timepoint_t startSection(int fd, const std::string §ionName) { + ATRACE_BEGIN(sectionName.c_str()); + ::android::base::WriteStringToFd( + "\n" + "------ Section start: " + sectionName + " ------\n" + "\n", fd); + return std::chrono::steady_clock::now(); +} + +void endSection(int fd, const std::string §ionName, timepoint_t startTime) { + ATRACE_END(); + auto endTime = std::chrono::steady_clock::now(); + auto elapsedMsec = std::chrono::duration_cast + (endTime - startTime).count(); + + ::android::base::WriteStringToFd( + "\n" + "------ Section end: " + sectionName + " ------\n" + "Elapsed msec: " + std::to_string(elapsedMsec) + "\n" + "\n", fd); +} + +// Dump data requested by an argument to the "dump" interface, or help info +// if the specified section is not supported. +void Dumpstate::dumpTextSection(int fd, const std::string §ionName) { + bool dumpAll = (sectionName == kAllSections); + std::string dumpFiles; + struct dirent **dirent_list = NULL; + int num_entries = scandir("/vendor/bin/dump", &dirent_list, 0, (int (*)(const struct dirent **, const struct dirent **)) alphasort); + if (!dirent_list) { + ALOGE("Unable to scan dir: /vendor/bin/dump\n"); + return; + } else if (num_entries <= 0) { + ALOGE("No file is found.\n"); + return; + } + // Execute all or designated programs under vendor/bin/dump/ + for (int i = 0; i < num_entries; i++) { + if (dirent_list[i]->d_name[0] == '.') { + continue; + } + std::string bin(dirent_list[i]->d_name); + dumpFiles = dumpFiles + " " + bin; + if (dumpAll || sectionName == bin) { + auto startTime = startSection(fd, bin); + RunCommandToFd(fd, "/vendor/bin/dump/"+bin, {"/vendor/bin/dump/"+bin}, CommandOptions::WithTimeout(15).Build()); + endSection(fd, bin, startTime); + if (!dumpAll) { + return; + } + } + } + + if (dumpAll) { + RunCommandToFd(fd, "VENDOR PROPERTIES", {"/vendor/bin/getprop"}); + return; + } + + // An unsupported section was requested on the command line + ::android::base::WriteStringToFd("Unrecognized text section: " + sectionName + "\n", fd); + ::android::base::WriteStringToFd("Try \"" + kAllSections + "\" or one of the following:", fd); + ::android::base::WriteStringToFd(dumpFiles, fd); + ::android::base::WriteStringToFd("\nNote: sections with attachments (e.g. dump_soc) are" + "not available from the command line.\n", fd); + while (num_entries--) { + free(dirent_list[num_entries]); + } + free(dirent_list); +} + +void Dumpstate::dumpLogSection(int fd, int fd_bin) +{ + std::string logDir = MODEM_LOG_DIRECTORY; + const std::string logCombined = logDir + "/combined_logs.tar"; + const std::string logAllDir = logDir + "/all_logs"; + + RunCommandToFd(fd, "MKDIR LOG", {"/vendor/bin/mkdir", "-p", logAllDir.c_str()}, CommandOptions::WithTimeout(2).Build()); + + dumpTextSection(fd, kAllSections); + + RunCommandToFd(fd, "TAR LOG", {"/vendor/bin/tar", "cvf", logCombined.c_str(), "-C", logAllDir.c_str(), "."}, CommandOptions::WithTimeout(20).Build()); + RunCommandToFd(fd, "CHG PERM", {"/vendor/bin/chmod", "a+w", logCombined.c_str()}, CommandOptions::WithTimeout(2).Build()); + + std::vector buffer(65536); + ::android::base::unique_fd fdLog(TEMP_FAILURE_RETRY(open(logCombined.c_str(), O_RDONLY | O_CLOEXEC | O_NONBLOCK))); + + if (fdLog >= 0) { + while (1) { + ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fdLog, buffer.data(), buffer.size())); + + if (bytes_read == 0) { + break; + } else if (bytes_read < 0) { + ALOGD("read(%s): %s\n", logCombined.c_str(), strerror(errno)); + break; + } + + ssize_t result = TEMP_FAILURE_RETRY(write(fd_bin, buffer.data(), bytes_read)); + + if (result != bytes_read) { + ALOGD("Failed to write %zd bytes, actually written: %zd", bytes_read, result); + break; + } + } + } + + RunCommandToFd(fd, "RM LOG DIR", { "/vendor/bin/rm", "-r", logAllDir.c_str()}, CommandOptions::WithTimeout(2).Build()); + RunCommandToFd(fd, "RM LOG", { "/vendor/bin/rm", logCombined.c_str()}, CommandOptions::WithTimeout(2).Build()); +} + +ndk::ScopedAStatus Dumpstate::dumpstateBoard(const std::vector<::ndk::ScopedFileDescriptor>& in_fds, + IDumpstateDevice::DumpstateMode in_mode, + int64_t in_timeoutMillis) { + ATRACE_BEGIN("dumpstateBoard"); + // Unused arguments. + (void) in_timeoutMillis; + + if (in_mode < IDumpstateDevice::DumpstateMode::FULL || in_mode > IDumpstateDevice::DumpstateMode::PROTO) { + ALOGE("Invalid mode: %d\n", in_mode); + return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, "Invalid mode"); + } + + if (in_fds.size() < 1) { + ALOGE("no FDs\n"); + return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, + "No file descriptor"); + } + + int fd = in_fds[0].get(); + if (fd < 0) { + ALOGE("invalid FD: %d\n", fd); + return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, + "Invalid file descriptor"); + } + + if (in_fds.size() < 2) { + ALOGE("no FD for dumpstate_board binary\n"); + dumpTextSection(fd, ""); + } else { + int fd_bin = in_fds[1].get(); + dumpLogSection(fd, fd_bin); + } + + ATRACE_END(); + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus Dumpstate::setVerboseLoggingEnabled(bool in_enable) { + ::android::base::SetProperty(kVerboseLoggingProperty, in_enable ? "true" : "false"); + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus Dumpstate::getVerboseLoggingEnabled(bool* _aidl_return) { + *_aidl_return = ::android::base::GetBoolProperty(kVerboseLoggingProperty, false); + return ndk::ScopedAStatus::ok(); +} + +// Since AIDLs that support the dump() interface are automatically invoked during +// bugreport generation and we don't want to generate a second copy of the same +// data that will go into dumpstate_board.txt, this function will only do +// something if it is called with an option, e.g. +// dumpsys android.hardware.dumpstate.IDumpstateDevice/default all +// +// Also, note that sections which generate attachments and/or binary data when +// included in a bugreport are not available through the dump() interface. +binder_status_t Dumpstate::dump(int fd, const char** args, uint32_t numArgs) { + + if (numArgs != 1) { + return STATUS_OK; + } + + dumpTextSection(fd, static_cast(args[0])); + + fsync(fd); + return STATUS_OK; +} + +} // namespace dumpstate +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/gear/dumpstate_v3/Dumpstate.h b/gear/dumpstate_v3/Dumpstate.h new file mode 100644 index 0000000..787b774 --- /dev/null +++ b/gear/dumpstate_v3/Dumpstate.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +namespace aidl { +namespace android { +namespace hardware { +namespace dumpstate { + +class Dumpstate : public BnDumpstateDevice { + public: + ::ndk::ScopedAStatus dumpstateBoard(const std::vector<::ndk::ScopedFileDescriptor>& in_fds, + IDumpstateDevice::DumpstateMode in_mode, + int64_t in_timeoutMillis) override; + + ::ndk::ScopedAStatus getVerboseLoggingEnabled(bool* _aidl_return) override; + + ::ndk::ScopedAStatus setVerboseLoggingEnabled(bool in_enable) override; + + binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; + + private: + const std::string kAllSections = "all"; + + void dumpTextSection(int fd, std::string const& sectionName); + void dumpLogSection(int fd, int fdModem); + + //bool getVerboseLoggingEnabledImpl(); + //::ndk::ScopedAStatus dumpstateBoardImpl(const int fd, const bool full); +}; + +} // namespace dumpstate +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/gear/dumpstate_v3/aidl.mk b/gear/dumpstate_v3/aidl.mk new file mode 100644 index 0000000..bcecb78 --- /dev/null +++ b/gear/dumpstate_v3/aidl.mk @@ -0,0 +1,4 @@ +PRODUCT_SOONG_NAMESPACES += device/google/gs-common/gear/dumpstate_v3 +PRODUCT_PACKAGES += android.hardware.dumpstate.3-service +BOARD_VENDOR_SEPOLICY_DIRS += device/google/gs-common/gear/dumpstate_v3/sepolicy + diff --git a/gear/dumpstate_v3/android.hardware.dumpstate.3-service.rc b/gear/dumpstate_v3/android.hardware.dumpstate.3-service.rc new file mode 100644 index 0000000..0a80e0c --- /dev/null +++ b/gear/dumpstate_v3/android.hardware.dumpstate.3-service.rc @@ -0,0 +1,5 @@ +service vendor.dumpstate-default /vendor/bin/hw/android.hardware.dumpstate.3-service + class hal + user system + group system shell + interface aidl android.hardware.dumpstate.IDumpstateDevice/default diff --git a/gear/dumpstate_v3/android.hardware.dumpstate.3-service.xml b/gear/dumpstate_v3/android.hardware.dumpstate.3-service.xml new file mode 100644 index 0000000..5e51b28 --- /dev/null +++ b/gear/dumpstate_v3/android.hardware.dumpstate.3-service.xml @@ -0,0 +1,9 @@ + + + android.hardware.dumpstate + + IDumpstateDevice + default + + + diff --git a/gear/dumpstate_v3/sepolicy/file.te b/gear/dumpstate_v3/sepolicy/file.te new file mode 100644 index 0000000..1777353 --- /dev/null +++ b/gear/dumpstate_v3/sepolicy/file.te @@ -0,0 +1,6 @@ +# dumpstate packing directory +type radio_vendor_data_file, file_type, data_file_type; +userdebug_or_eng(` + typeattribute radio_vendor_data_file mlstrustedobject; +') + diff --git a/gear/dumpstate_v3/sepolicy/file_contexts b/gear/dumpstate_v3/sepolicy/file_contexts new file mode 100644 index 0000000..da10e76 --- /dev/null +++ b/gear/dumpstate_v3/sepolicy/file_contexts @@ -0,0 +1,5 @@ +# generic dumpstate for pixel +/vendor/bin/hw/android\.hardware\.dumpstate\.3-service u:object_r:hal_dumpstate_default_exec:s0 + +/data/vendor/radio(/.*)? u:object_r:radio_vendor_data_file:s0 + diff --git a/gear/dumpstate_v3/sepolicy/hal_dumpstate_default.te b/gear/dumpstate_v3/sepolicy/hal_dumpstate_default.te new file mode 100644 index 0000000..06ebb75 --- /dev/null +++ b/gear/dumpstate_v3/sepolicy/hal_dumpstate_default.te @@ -0,0 +1,7 @@ +# required permission to use tar to pack dumpsate_board.bin +allow hal_dumpstate_default vendor_toolbox_exec:file execute_no_trans; +allow hal_dumpstate_default radio_vendor_data_file:dir create_dir_perms; +allow hal_dumpstate_default radio_vendor_data_file:file create_file_perms; +allow hal_dumpstate_default shell_data_file:file getattr; +set_prop(hal_dumpstate_default, vendor_logger_prop) + diff --git a/gear/dumpstate_v3/sepolicy/property.te b/gear/dumpstate_v3/sepolicy/property.te new file mode 100644 index 0000000..c6f5a4f --- /dev/null +++ b/gear/dumpstate_v3/sepolicy/property.te @@ -0,0 +1,3 @@ +# verbose property +vendor_internal_prop(vendor_logger_prop) + diff --git a/gear/dumpstate_v3/sepolicy/property_contexts b/gear/dumpstate_v3/sepolicy/property_contexts new file mode 100644 index 0000000..5ead16f --- /dev/null +++ b/gear/dumpstate_v3/sepolicy/property_contexts @@ -0,0 +1,3 @@ +# verbose property name +persist.vendor.verbose_logging_enabled u:object_r:vendor_logger_prop:s0 + diff --git a/gear/dumpstate_v3/service.cpp b/gear/dumpstate_v3/service.cpp new file mode 100644 index 0000000..53dc8ac --- /dev/null +++ b/gear/dumpstate_v3/service.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "android.hardware.dumpstate.3-service" + +#include "Dumpstate.h" + +#include +#include +#include + +using aidl::android::hardware::dumpstate::Dumpstate; + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + std::shared_ptr dumpstate = ndk::SharedRefBase::make(); + + const std::string instance = std::string() + Dumpstate::descriptor + "/default"; + binder_status_t status = + AServiceManager_addService(dumpstate->asBinder().get(), instance.c_str()); + CHECK_EQ(status, STATUS_OK); + + ABinderProcess_joinThreadPool(); + return EXIT_FAILURE; // Unreachable +}