libpower: Check if suspend service is null

Before requesting wakelock ensure that we have a valid suspend service.
Use waitForService instead of checkService.

Test: libpower_test
Test: block_suspend
Test: atest NexusLauncherOutOfProcTests:MemoryTests#testAppLaunchFromWorkspace --rerun-until-failure 10
Bug: 195319784
Change-Id: If13e233360a7b5f372dd2991c1891ee0bee8544b
This commit is contained in:
Kalesh Singh 2021-08-04 15:14:34 +00:00
parent fc7f7499c2
commit 06c0d048b1

View file

@ -40,7 +40,7 @@ static std::unordered_map<std::string, std::shared_ptr<IWakeLock>> gWakeLockMap;
static const std::shared_ptr<ISystemSuspend> getSystemSuspendServiceOnce() { static const std::shared_ptr<ISystemSuspend> getSystemSuspendServiceOnce() {
static std::shared_ptr<ISystemSuspend> suspendService = static std::shared_ptr<ISystemSuspend> suspendService =
ISystemSuspend::fromBinder(ndk::SpAIBinder(AServiceManager_checkService( ISystemSuspend::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(
(ISystemSuspend::descriptor + std::string("/default")).c_str()))); (ISystemSuspend::descriptor + std::string("/default")).c_str())));
return suspendService; return suspendService;
} }
@ -49,7 +49,7 @@ int acquire_wake_lock(int, const char* id) {
ATRACE_CALL(); ATRACE_CALL();
const auto suspendService = getSystemSuspendServiceOnce(); const auto suspendService = getSystemSuspendServiceOnce();
if (!suspendService) { if (!suspendService) {
LOG(ERROR) << "ISystemSuspend::getService() failed."; LOG(ERROR) << "Failed to get SystemSuspend service";
return -1; return -1;
} }
@ -115,6 +115,11 @@ WakeLock::~WakeLock() = default;
WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) { WakeLock::WakeLockImpl::WakeLockImpl(const std::string& name) : mWakeLock(nullptr) {
const auto suspendService = getSystemSuspendServiceOnce(); const auto suspendService = getSystemSuspendServiceOnce();
if (!suspendService) {
LOG(ERROR) << "Failed to get SystemSuspend service";
return;
}
std::shared_ptr<IWakeLock> wl = nullptr; std::shared_ptr<IWakeLock> wl = nullptr;
auto status = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name, &wl); auto status = suspendService->acquireWakeLock(WakeLockType::PARTIAL, name, &wl);
// It's possible that during device shutdown SystemSuspend service has already exited. // It's possible that during device shutdown SystemSuspend service has already exited.