| /* | |
| * Copyright (C) 2021 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 DEBUG false // STOPSHIP if true | |
| #include "Log.h" | |
| #include "StatsAidl.h" | |
| #include <Counter.h> | |
| #include <aidl/android/frameworks/stats/BnVendorPullAtomResultReceiver.h> | |
| #include <android/permission_manager.h> | |
| #include <com_android_server_stats_hal.h> | |
| #include <log/log.h> | |
| #include <stats_annotations.h> | |
| #include <stats_event.h> | |
| #include <stats_pull_atom_callback.h> | |
| #include <chrono> | |
| #include <condition_variable> | |
| #include <map> | |
| #include <mutex> | |
| #include "PullCallbackImpl.h" | |
| #include "PullCallbackRegistry.h" | |
| #include "PullResultReceiver.h" | |
| #include "Utils.h" | |
| namespace { | |
| static const char* kRegisterStatsPullAtomPermission = "android.permission.REGISTER_STATS_PULL_ATOM"; | |
| } // namespace | |
| namespace aidl { | |
| namespace android { | |
| namespace frameworks { | |
| namespace stats { | |
| using ::aidl::android::frameworks::stats::IVendorPullAtomResultReceiver; | |
| using ::android::expresslog::Counter; | |
| using ndk::ScopedAStatus; | |
| ScopedAStatus StatsHal::reportVendorAtom(const VendorAtom& vendorAtom) { | |
| if (!isPushedVendorAtom(vendorAtom.atomId) && !isPulledVendorAtom(vendorAtom.atomId)) { | |
| ALOGE("Atom ID %ld is not a valid pushed vendor atom ID", (long)vendorAtom.atomId); | |
| Counter::logIncrement(kAtomErrorMetricName); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| -1, "Not a valid pushed vendor atom ID"); | |
| } | |
| if (vendorAtom.reverseDomainName.length() > 50) { | |
| ALOGE("Vendor atom reverse domain name %s is too long.", | |
| vendorAtom.reverseDomainName.c_str()); | |
| Counter::logIncrement(kAtomErrorMetricName); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| -1, "Vendor atom reverse domain name is too long"); | |
| } | |
| using ScopedAStatsEvent = std::unique_ptr<AStatsEvent, void (*)(AStatsEvent*)>; | |
| ScopedAStatsEvent event = ScopedAStatsEvent(AStatsEvent_obtain(), AStatsEvent_release); | |
| ScopedAStatus conversionResult = toAStatsEvent(vendorAtom, event.get()); | |
| if (!conversionResult.isOk()) { | |
| return conversionResult; | |
| } | |
| AStatsEvent_build(event.get()); | |
| const int ret = AStatsEvent_write(event.get()); | |
| if (ret <= 0) { | |
| ALOGE("Error writing Atom ID %ld. Result: %d", (long)vendorAtom.atomId, ret); | |
| Counter::logIncrement(kAtomErrorMetricName); | |
| } | |
| return ret <= 0 ? ScopedAStatus::fromServiceSpecificErrorWithMessage(ret, "report atom failed") | |
| : ScopedAStatus::ok(); | |
| } | |
| static bool toAStatsEventList(const std::vector<VendorAtom>& vendorAtoms, AStatsEventList* output) { | |
| VLOG("pullAtomCallback received %d atoms", (int)vendorAtoms.size()); | |
| for (const VendorAtom& vendorAtom : vendorAtoms) { | |
| AStatsEvent* event = AStatsEventList_addStatsEvent(output); | |
| ScopedAStatus conversionResult = toAStatsEvent(vendorAtom, event); | |
| if (!conversionResult.isOk()) { | |
| return false; | |
| } | |
| AStatsEvent_build(event); | |
| } | |
| return true; | |
| } | |
| // global callback to be used as a trampoline function to the callbacks from vendor processes | |
| AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag, AStatsEventList* data, | |
| void* /*cookie*/) { | |
| VLOG("pullAtomCallback for atom tag %d", atomTag); | |
| if (data == nullptr) { | |
| ALOGE("pullAtomCallback data is null"); | |
| return AStatsManager_PULL_SKIP; | |
| } | |
| std::optional<PullCallbackRegistry::CallbackData> callbackData = | |
| PullCallbackRegistry::getInstance().getCallback(atomTag); | |
| if (!callbackData) { | |
| VLOG("pullAtomCallback for atom tag %d not found", atomTag); | |
| return AStatsManager_PULL_SKIP; | |
| } | |
| if (!callbackData->callback) { | |
| VLOG("pullAtomCallback for atom tag %d died", atomTag); | |
| return AStatsManager_PULL_SKIP; | |
| } | |
| struct PullState { | |
| std::condition_variable cv; | |
| bool callbackInvoked = false; | |
| bool timedOut = false; | |
| AStatsManager_PullAtomCallbackReturn pullResultCode = AStatsManager_PULL_SKIP; | |
| std::mutex mtx; | |
| }; | |
| auto state = std::make_shared<PullState>(); | |
| auto resultReceiver = ndk::SharedRefBase::make< | |
| PullResultReceiver>([state, data](int32_t atomTag, VendorPullAtomReturnCode returnCode, | |
| const std::vector<VendorAtom>& result) { | |
| VLOG("pullAtomCallback::pullFinished for atom tag %d with code %d and result size %d", | |
| atomTag, (int)returnCode, (int)result.size()); | |
| { | |
| std::lock_guard<std::mutex> lck(state->mtx); | |
| // if callback is already timed out - skip processing the result | |
| // data pointer is potentially already freed by the caller | |
| if (state->timedOut || state->callbackInvoked) { | |
| VLOG("pullFinished called after timeout or repeatedly called for atom tag %d", | |
| atomTag); | |
| return; | |
| } | |
| if (returnCode == VendorPullAtomReturnCode::PULL_SUCCESS && | |
| toAStatsEventList(result, data)) { | |
| state->pullResultCode = AStatsManager_PULL_SUCCESS; | |
| } | |
| state->callbackInvoked = true; | |
| } | |
| state->cv.notify_one(); | |
| }); | |
| VLOG("Trigger callback in vendor process to pull data in async way for atom tag %d", atomTag); | |
| ScopedAStatus status = (*callbackData).callback->onPullAtom(atomTag, resultReceiver); | |
| if (!status.isOk()) { | |
| // Binder died? | |
| if (status.getExceptionCode() == EX_TRANSACTION_FAILED && | |
| status.getStatus() == STATUS_DEAD_OBJECT) { | |
| ALOGE("pullAtomCallback for atom tag %d failed: remote binder is dead", atomTag); | |
| } else { | |
| ALOGE("pullAtomCallback for atom tag %d failed", atomTag); | |
| } | |
| return AStatsManager_PULL_SKIP; | |
| } | |
| VLOG("blocking wait for pullAtomCallback result for atom tag %d", atomTag); | |
| std::unique_lock<std::mutex> lck(state->mtx); | |
| state->cv.wait_for(lck, std::chrono::milliseconds((*callbackData).timeoutMillis), | |
| [state] { return state->callbackInvoked; }); | |
| if (!state->callbackInvoked) { | |
| state->timedOut = true; | |
| VLOG("pullAtomCallback for atom tag %d timed out", atomTag); | |
| return AStatsManager_PULL_SKIP; | |
| } | |
| VLOG("pullAtomCallback for atom tag %d done with result %d", atomTag, state->pullResultCode); | |
| return state->pullResultCode; | |
| } | |
| static bool enforcePermission(const char* permission, int32_t uid) { | |
| int32_t result = PERMISSION_MANAGER_PERMISSION_DENIED; | |
| int32_t permissionCheckResult = | |
| APermissionManager_checkPermission(permission, -1, uid, &result); | |
| if (permissionCheckResult != PERMISSION_MANAGER_STATUS_OK || | |
| result != PERMISSION_MANAGER_PERMISSION_GRANTED) { | |
| ALOGE("enforcePermission %s failed for uid(%d). CheckResult %d Error: %d", permission, uid, | |
| permissionCheckResult, result); | |
| return false; | |
| } | |
| return true; | |
| } | |
| // TODO (b/458758750): add vts to validate uid differentiation | |
| ScopedAStatus StatsHal::setPullAtomCallback( | |
| int32_t atomTag, const std::optional<VendorPullAtomMetadata>& metadata, | |
| const std::shared_ptr<IVendorPullAtomCallback>& pullerCallback) { | |
| if (com_android_server_stats_hal_vendor_pulled_atoms_enabled()) { | |
| // check atom range | |
| if (!isPulledVendorAtom(atomTag)) { | |
| ALOGE("Atom ID %d is not a valid pulled vendor atom ID", atomTag); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| 1, "Not a valid pulled vendor atom ID"); | |
| } | |
| // check permission | |
| int32_t uid = AIBinder_getCallingUid(); | |
| if (!enforcePermission(kRegisterStatsPullAtomPermission, uid)) { | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| 1, "Missing permission: android.permission.REGISTER_STATS_PULL_ATOM"); | |
| } | |
| VLOG("setPullAtomCallback for atom tag %d from uid(%d)", atomTag, uid); | |
| // in case of missing permission or other issues, pullerCallback can be empty | |
| if (!pullerCallback) { | |
| ALOGE("setPullAtomCallback failed: callback is null for atom tag %d", atomTag); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| 1, "Callback cannot be null"); | |
| } | |
| using ScopedAStatsManagerPullAtomMetadata = | |
| std::unique_ptr<AStatsManager_PullAtomMetadata, | |
| void (*)(AStatsManager_PullAtomMetadata*)>; | |
| ScopedAStatsManagerPullAtomMetadata stats_metadata(nullptr, | |
| AStatsManager_PullAtomMetadata_release); | |
| if (metadata) { | |
| stats_metadata = | |
| ScopedAStatsManagerPullAtomMetadata(AStatsManager_PullAtomMetadata_obtain(), | |
| AStatsManager_PullAtomMetadata_release); | |
| AStatsManager_PullAtomMetadata_setTimeoutMillis(stats_metadata.get(), | |
| metadata->timeoutMillis); | |
| AStatsManager_PullAtomMetadata_setCoolDownMillis(stats_metadata.get(), | |
| metadata->coolDownMillis); | |
| if (metadata->additiveFields) { | |
| auto additiveFields = *metadata->additiveFields; | |
| AStatsManager_PullAtomMetadata_setAdditiveFields( | |
| stats_metadata.get(), additiveFields.data(), | |
| static_cast<int32_t>(additiveFields.size())); | |
| } | |
| } | |
| if (!PullCallbackRegistry::getInstance().setCallback(atomTag, pullerCallback, metadata, | |
| uid)) { | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| 1, "UID mismatch, cannot update callback"); | |
| } | |
| AStatsManager_setPullAtomCallback(atomTag, stats_metadata.get(), &pullAtomCallback, | |
| nullptr); | |
| VLOG("setPullAtomCallback for atom tag %d done", atomTag); | |
| return ScopedAStatus::ok(); | |
| } | |
| ALOGW("setPullAtomCallback for atom tag %d failed - feature disabled", atomTag); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage(1, "Feature is disabled"); | |
| } | |
| ScopedAStatus StatsHal::clearPullAtomCallback(int32_t atomTag) { | |
| if (com_android_server_stats_hal_vendor_pulled_atoms_enabled()) { | |
| // check atom range | |
| if (!isPulledVendorAtom(atomTag)) { | |
| ALOGE("Atom ID %d is not a valid pulled vendor atom ID", atomTag); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| 1, "Not a valid pulled vendor atom ID"); | |
| } | |
| // check permission | |
| int32_t uid = AIBinder_getCallingUid(); | |
| if (!enforcePermission(kRegisterStatsPullAtomPermission, uid)) { | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage( | |
| 1, "Missing permission: android.permission.REGISTER_STATS_PULL_ATOM"); | |
| } | |
| VLOG("clearPullAtomCallback for atom tag %d", atomTag); | |
| if (!PullCallbackRegistry::getInstance().removeCallback(atomTag, uid)) { | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage(1, "Cannot clear callback"); | |
| } | |
| AStatsManager_clearPullAtomCallback(atomTag); | |
| return ScopedAStatus::ok(); | |
| } | |
| VLOG("clearPullAtomCallback for atom tag %d failed", atomTag); | |
| return ScopedAStatus::fromServiceSpecificErrorWithMessage(1, "Feature is disabled"); | |
| } | |
| } // namespace stats | |
| } // namespace frameworks | |
| } // namespace android | |
| } // namespace aidl |