diff --git a/services/accountmgr/include/account_iam/account_iam_callback.h b/services/accountmgr/include/account_iam/account_iam_callback.h index b6426f8ad..1583af970 100644 --- a/services/accountmgr/include/account_iam/account_iam_callback.h +++ b/services/accountmgr/include/account_iam/account_iam_callback.h @@ -222,6 +222,7 @@ public: public: int32_t userId_; uint64_t secureUid_ = 0; + bool isCalled_ = false; std::mutex secureMtx_; std::condition_variable secureCv_; }; diff --git a/services/accountmgr/include/osaccount/os_account_delete_user_idm_callback.h b/services/accountmgr/include/osaccount/os_account_delete_user_idm_callback.h index c0de9a8e6..b8390c07b 100644 --- a/services/accountmgr/include/osaccount/os_account_delete_user_idm_callback.h +++ b/services/accountmgr/include/osaccount/os_account_delete_user_idm_callback.h @@ -45,7 +45,7 @@ public: void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const UserIam::UserAuth::Attributes &extraInfo) override; public: - bool isIdmOnResultCallBack_ = false; + bool isCalled_ = false; int32_t resultCode_ = -1; std::mutex mutex_; std::condition_variable onResultCondition_; diff --git a/services/accountmgr/include/osaccount/os_account_user_callback.h b/services/accountmgr/include/osaccount/os_account_user_callback.h index 5bc6fa593..b5de0a865 100644 --- a/services/accountmgr/include/osaccount/os_account_user_callback.h +++ b/services/accountmgr/include/osaccount/os_account_user_callback.h @@ -33,7 +33,8 @@ public: void OnStartUserDone(int userId, int errcode) override; public: - bool isReturnOk_ = false; + bool isCalled_ = false; + int32_t resultCode_ = -1; std::mutex mutex_; std::condition_variable onStartCondition_; std::condition_variable onStopCondition_; diff --git a/services/accountmgr/src/account_iam/account_iam_callback.cpp b/services/accountmgr/src/account_iam/account_iam_callback.cpp index 9fe42639a..bc9de2df7 100644 --- a/services/accountmgr/src/account_iam/account_iam_callback.cpp +++ b/services/accountmgr/src/account_iam/account_iam_callback.cpp @@ -402,16 +402,19 @@ void DelUserCallback::OnResult(int32_t result, const Attributes &extraInfo) } auto eraseUserCallback = std::make_shared(); - std::unique_lock lock(eraseUserCallback->mutex_); errCode = UserIam::UserAuth::UserIdmClient::GetInstance().EraseUser(userId_, eraseUserCallback); if (errCode != ERR_OK) { ACCOUNT_LOGE("Fail to erase user, userId=%{public}d, errcode=%{public}d", userId_, errCode); return innerCallback_->OnResult(errCode, extraInfo); } - eraseUserCallback->onResultCondition_.wait(lock); + std::unique_lock lock(eraseUserCallback->mutex_); + eraseUserCallback->onResultCondition_.wait(lock, [eraseUserCallback] { + return eraseUserCallback->isCalled_; + }); if (eraseUserCallback->resultCode_ != ERR_OK) { - ACCOUNT_LOGE("Fail to erase user, userId=%{public}d, errcode=%{public}d", userId_, errCode); - return innerCallback_->OnResult(errCode, extraInfo); + ACCOUNT_LOGE("Fail to erase user, userId=%{public}d, errcode=%{public}d", + userId_, eraseUserCallback->resultCode_); + return innerCallback_->OnResult(eraseUserCallback->resultCode_, extraInfo); } (void)IInnerOsAccountManager::GetInstance().SetOsAccountCredentialId(userId_, 0); errCode = innerIamMgr_.UpdateStorageKeyContext(userId_); @@ -588,6 +591,7 @@ void GetSecureUidCallback::OnSecUserInfo(const SecUserInfo &info) ACCOUNT_LOGI("SecUserInfo call back userId=%{public}d", userId_); std::unique_lock lck(secureMtx_); this->secureUid_ = info.secureUid; + isCalled_ = true; secureCv_.notify_all(); } diff --git a/services/accountmgr/src/account_iam/inner_account_iam_manager.cpp b/services/accountmgr/src/account_iam/inner_account_iam_manager.cpp index 64a58e677..9d0fad90f 100644 --- a/services/accountmgr/src/account_iam/inner_account_iam_manager.cpp +++ b/services/accountmgr/src/account_iam/inner_account_iam_manager.cpp @@ -435,13 +435,15 @@ void InnerAccountIAMManager::HandleFileKeyException(int32_t userId, const std::v return; } auto callback = std::make_shared(userId); - std::unique_lock lck(callback->secureMtx_); ErrCode code = UserIDMClient::GetInstance().GetSecUserInfo(userId, callback); if (code != ERR_OK) { return; } - auto status = callback->secureCv_.wait_for(lck, std::chrono::seconds(TIME_WAIT_TIME_OUT)); - if (status != std::cv_status::no_timeout) { + std::unique_lock lck(callback->secureMtx_); + auto status = callback->secureCv_.wait_for(lck, std::chrono::seconds(TIME_WAIT_TIME_OUT), [callback] { + return callback->isCalled_; + }); + if (!status) { ACCOUNT_LOGE("GetSecureUidCallback time out"); return; } diff --git a/services/accountmgr/src/appaccount/app_account_common_event_observer.cpp b/services/accountmgr/src/appaccount/app_account_common_event_observer.cpp index 3e7e35444..c097411e8 100644 --- a/services/accountmgr/src/appaccount/app_account_common_event_observer.cpp +++ b/services/accountmgr/src/appaccount/app_account_common_event_observer.cpp @@ -81,7 +81,6 @@ void AppAccountCommonEventObserver::OnReceiveEvent(const CommonEventData &data) { auto want = data.GetWant(); std::string action = want.GetAction(); - ACCOUNT_LOGI("Receive event: %{public}s", action.c_str()); if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { DealWithRemoveEvent(want, action); diff --git a/services/accountmgr/src/osaccount/os_account_delete_user_idm_callback.cpp b/services/accountmgr/src/osaccount/os_account_delete_user_idm_callback.cpp index f54c68248..389778ef6 100644 --- a/services/accountmgr/src/osaccount/os_account_delete_user_idm_callback.cpp +++ b/services/accountmgr/src/osaccount/os_account_delete_user_idm_callback.cpp @@ -24,7 +24,7 @@ void OsAccountDeleteUserIdmCallback::OnResult(int32_t result, const UserIam::Use { std::unique_lock lock(mutex_); ACCOUNT_LOGI("IAM OnResult callback! result %{public}d", result); - isIdmOnResultCallBack_ = true; + isCalled_ = true; resultCode_ = result; onResultCondition_.notify_one(); } diff --git a/services/accountmgr/src/osaccount/os_account_interface.cpp b/services/accountmgr/src/osaccount/os_account_interface.cpp index 10cc186c3..080ca0ffe 100644 --- a/services/accountmgr/src/osaccount/os_account_interface.cpp +++ b/services/accountmgr/src/osaccount/os_account_interface.cpp @@ -80,7 +80,6 @@ ErrCode OsAccountInterface::SendToAMSAccountStart(OsAccountInfo &osAccountInfo) } StartTraceAdapter("AbilityManagerAdapter StartUser"); - std::unique_lock lock(osAccountStartUserCallback->mutex_); ErrCode code = AbilityManagerAdapter::GetInstance()->StartUser(osAccountInfo.GetLocalId(), osAccountStartUserCallback); if (code != ERR_OK) { @@ -90,9 +89,12 @@ ErrCode OsAccountInterface::SendToAMSAccountStart(OsAccountInfo &osAccountInfo) FinishTraceAdapter(); return code; } - osAccountStartUserCallback->onStartCondition_.wait(lock); + std::unique_lock lock(osAccountStartUserCallback->mutex_); + osAccountStartUserCallback->onStartCondition_.wait(lock, [osAccountStartUserCallback] { + return osAccountStartUserCallback->isCalled_; + }); FinishTraceAdapter(); - if (!osAccountStartUserCallback->isReturnOk_) { + if (osAccountStartUserCallback->resultCode_ != ERR_OK) { ACCOUNT_LOGE("failed to AbilityManagerService in call back"); ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_START, -1, "AbilityManagerService failed!"); @@ -114,7 +116,6 @@ ErrCode OsAccountInterface::SendToAMSAccountStop(OsAccountInfo &osAccountInfo) } StartTraceAdapter("AbilityManagerAdapter StopUser"); - std::unique_lock lock(osAccountStopUserCallback->mutex_); ErrCode code = AbilityManagerAdapter::GetInstance()->StopUser(osAccountInfo.GetLocalId(), osAccountStopUserCallback); if (code != ERR_OK) { @@ -124,9 +125,12 @@ ErrCode OsAccountInterface::SendToAMSAccountStop(OsAccountInfo &osAccountInfo) FinishTraceAdapter(); return code; } - osAccountStopUserCallback->onStopCondition_.wait(lock); + std::unique_lock lock(osAccountStopUserCallback->mutex_); + osAccountStopUserCallback->onStopCondition_.wait(lock, [osAccountStopUserCallback] { + return osAccountStopUserCallback->isCalled_; + }); FinishTraceAdapter(); - if (!osAccountStopUserCallback->isReturnOk_) { + if (osAccountStopUserCallback->resultCode_ != ERR_OK) { ACCOUNT_LOGE("failed to AbilityManagerService in call back"); ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_STOP, -1, "AbilityManagerService failed!"); @@ -214,11 +218,11 @@ ErrCode OsAccountInterface::SendToIDMAccountDelete(OsAccountInfo &osAccountInfo) OHOS::GetSystemCurrentTime(&startTime); OHOS::GetSystemCurrentTime(&nowTime); while (OHOS::GetSecondsBetween(startTime, nowTime) < Constants::TIME_WAIT_TIME_OUT && - !callback->isIdmOnResultCallBack_) { + !callback->isCalled_) { std::this_thread::sleep_for(std::chrono::milliseconds(Constants::WAIT_ONE_TIME)); OHOS::GetSystemCurrentTime(&nowTime); } - if (!callback->isIdmOnResultCallBack_) { + if (!callback->isCalled_) { ACCOUNT_LOGE("idm did not call back! timeout!"); ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE, -1, "UserIDMClient EnforceDelUser timeout!"); diff --git a/services/accountmgr/src/osaccount/os_account_user_callback.cpp b/services/accountmgr/src/osaccount/os_account_user_callback.cpp index ae565c92e..7d69f5145 100644 --- a/services/accountmgr/src/osaccount/os_account_user_callback.cpp +++ b/services/accountmgr/src/osaccount/os_account_user_callback.cpp @@ -68,7 +68,8 @@ void OsAccountUserCallback::OnStopUserDone(int userId, int errcode) std::unique_lock lock(mutex_); ACCOUNT_LOGI("in call back account, OnStopUserDone id is %{public}d, errcode is %{public}d.", userId, errcode); - isReturnOk_ = (errcode == 0); + isCalled_ = true; + resultCode_ = errcode; onStopCondition_.notify_one(); } @@ -77,7 +78,8 @@ void OsAccountUserCallback::OnStartUserDone(int userId, int errcode) std::unique_lock lock(mutex_); ACCOUNT_LOGI("in call back account, OnStartUserDone id is %{public}d, errcode is %{public}d.", userId, errcode); - isReturnOk_ = (errcode == 0); + isCalled_ = true; + resultCode_ = errcode; onStartCondition_.notify_one(); } } // namespace AccountSA diff --git a/services/accountmgr/test/unittest/os_account/os_account_service_test.cpp b/services/accountmgr/test/unittest/os_account/os_account_service_test.cpp index b726f44ac..2c32363cd 100644 --- a/services/accountmgr/test/unittest/os_account/os_account_service_test.cpp +++ b/services/accountmgr/test/unittest/os_account/os_account_service_test.cpp @@ -72,7 +72,7 @@ HWTEST_F(OsAccountServiceTest, OnStopUserDone001, TestSize.Level1) ASSERT_NE(nullptr, osAccountStopUserCallback); int errCode = 0; osAccountStopUserCallback->OnStopUserDone(TEST_USER_ID, errCode); - EXPECT_TRUE(osAccountStopUserCallback->isReturnOk_); + EXPECT_EQ(osAccountStopUserCallback->resultCode_, ERR_OK); } /** @@ -87,7 +87,7 @@ HWTEST_F(OsAccountServiceTest, OnStartUserDone001, TestSize.Level1) ASSERT_NE(nullptr, osAccountStartUserCallback); int errCode = 0; osAccountStartUserCallback->OnStartUserDone(TEST_USER_ID, errCode); - EXPECT_TRUE(osAccountStartUserCallback->isReturnOk_); + EXPECT_EQ(osAccountStartUserCallback->resultCode_, ERR_OK); } } // namespace AccountSA } // namespace OHOS \ No newline at end of file