!2200 添加超时操作

Merge pull request !2200 from swg/timer
This commit is contained in:
openharmony_ci 2024-10-28 07:10:39 +00:00 committed by Gitee
commit 92973e0be6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 102 additions and 6 deletions

View File

@ -46,6 +46,7 @@ ohos_shared_library("libaccount_common") {
"log/src/account_log_wrapper.cpp",
"perf_stat/src/perf_stat.cpp",
"utils/src/account_permission_manager.cpp",
"utils/src/account_timeout_task.cpp",
"utils/src/memory_guard.cpp",
]

View File

@ -19,6 +19,7 @@
#define TOKEN_ID_LOWMASK 0xffffffff
namespace OHOS {
namespace AccountSA {
constexpr int32_t WAIT_TIME = 60;
// for watchdog func
#ifdef HICOLLIE_ENABLE
const uint32_t TIMEOUT = 30; // 30s

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* 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.
*/
#ifndef OS_ACCOUNT_FRAMEWORKS_ACCOUNT_TIMEOUT_TASK_H
#define OS_ACCOUNT_FRAMEWORKS_ACCOUNT_TIMEOUT_TASK_H
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include "account_constants.h"
namespace OHOS {
namespace AccountSA {
class AccountTimeoutTask : public std::enable_shared_from_this<AccountTimeoutTask> {
public:
bool RunTask(std::string taskName, std::function<void()> callback, int32_t timeout = WAIT_TIME);
bool isCalled_ = false;
std::mutex mutex_;
std::condition_variable cv_;
};
} // AccountSA
} // OHOS
#endif // OS_ACCOUNT_FRAMEWORKS_ACCOUNT_TIMEOUT_TASK_H

View File

@ -17,6 +17,7 @@
#define OS_ACCOUNT_FRAMEWORKS_ACCOUNT_TIMER_H
#include <stdint.h>
#include "account_constants.h"
namespace OHOS {
namespace AccountSA {
@ -24,7 +25,7 @@ class AccountTimer {
public:
AccountTimer(bool needInit = true);
~AccountTimer();
void Init();
void Init(int32_t timeout = TIMEOUT);
private:
int64_t timerId_;

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* 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.
*/
#include "account_timeout_task.h"
#include <thread>
namespace OHOS {
namespace AccountSA {
bool AccountTimeoutTask::RunTask(std::string taskName, std::function<void()> callback, int32_t timeout)
{
auto task = [callback, weakPtr = weak_from_this()] {
callback();
auto ptr = weakPtr.lock();
if (ptr) {
std::unique_lock<std::mutex> lock(ptr->mutex_);
ptr->isCalled_ = true;
ptr->cv_.notify_one();
}
};
std::thread taskThread(task);
pthread_setname_np(taskThread.native_handle(), taskName.c_str());
taskThread.detach();
std::unique_lock<std::mutex> lock(mutex_);
return cv_.wait_for(lock, std::chrono::seconds(timeout), [this] { return isCalled_; });
}
} // namespace AccountSA
} // namespace OHOS

View File

@ -14,7 +14,6 @@
*/
#include "account_timer.h"
#include "account_constants.h"
#include "xcollie/xcollie.h"
namespace OHOS {
@ -31,10 +30,10 @@ AccountTimer::~AccountTimer()
HiviewDFX::XCollie::GetInstance().CancelTimer(timerId_);
}
void AccountTimer::Init()
void AccountTimer::Init(int32_t timeout)
{
timerId_ = HiviewDFX::XCollie::GetInstance().SetTimer(
TIMER_NAME, TIMEOUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
TIMER_NAME, timeout, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
}
} // namespace AccountSA
} // namespace OHOS

View File

@ -19,7 +19,9 @@
#include <pthread.h>
#include <securec.h>
#include <thread>
#include "account_hisysevent_adapter.h"
#include "account_log_wrapper.h"
#include "account_timeout_task.h"
#ifdef HAS_HUKS_PART
#include "hks_api.h"
#include "hks_param.h"
@ -224,9 +226,15 @@ int32_t GenerateAccountInfoDigest(const std::string &inData, uint8_t* outData, u
AccountFileWatcherMgr::AccountFileWatcherMgr()
{
std::shared_ptr<AccountTimeoutTask> task = std::make_shared<AccountTimeoutTask>();
bool state = task->RunTask("InitEncryptionKey", [] {
#ifdef HAS_HUKS_PART
InitEncryptionKey();
InitEncryptionKey();
#endif // HAS_HUKS_PART
});
if (!state) {
ReportServiceStartFail(ERR_ACCOUNT_COMMON_OPERATION_TIMEOUT, "InitEncryptionKey timeout");
}
inotifyFd_ = inotify_init();
if (inotifyFd_ < 0) {
ACCOUNT_LOGE("failed to init notify, errCode:%{public}d", errno);

View File

@ -38,10 +38,16 @@
#ifdef HAS_USER_AUTH_PART
#include "account_iam_service.h"
#endif
#ifdef HICOLLIE_ENABLE
#include "account_timer.h"
#endif // HICOLLIE_ENABLE
namespace OHOS {
namespace AccountSA {
namespace {
#ifdef HICOLLIE_ENABLE
constexpr int32_t MAX_INIT_TIME = 120;
#endif // HICOLLIE_ENABLE
const bool REGISTER_RESULT =
SystemAbility::MakeAndRegisterAbility(&DelayedRefSingleton<AccountMgrService>::GetInstance());
const std::string DEVICE_OWNER_DIR = "/data/service/el1/public/account/0/";
@ -305,7 +311,10 @@ bool AccountMgrService::Init()
ACCOUNT_LOGW("Service is already running!");
return false;
}
#ifdef HICOLLIE_ENABLE
AccountTimer timer(false);
timer.Init(MAX_INIT_TIME);
#endif // HICOLLIE_ENABLE
CreateDeviceDir();
IAccountContext::SetInstance(this);
if ((!CreateOsAccountService()) || (!CreateAppAccountService()) || (!CreateIAMService()) ||