1 /*
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "aot/aot_sign_data_cache_mgr.h"
17
18 #include "installd_client.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 constexpr int32_t SLEEP_TIME_FOR_WAIT_SIGN_ENABLE = 1; // 1 s
24 constexpr int32_t LOOP_TIMES_FOR_WAIT_SIGN_ENABLE = 5;
25 }
26
GetInstance()27 AOTSignDataCacheMgr& AOTSignDataCacheMgr::GetInstance()
28 {
29 static AOTSignDataCacheMgr signDataCacheMgr;
30 return signDataCacheMgr;
31 }
32
AddPendSignData(const AOTArgs & aotArgs,const uint32_t versionCode,const std::vector<uint8_t> & pendSignData,const ErrCode ret)33 void AOTSignDataCacheMgr::AddPendSignData(const AOTArgs &aotArgs, const uint32_t versionCode,
34 const std::vector<uint8_t> &pendSignData, const ErrCode ret)
35 {
36 if (isLocked_ && (ret == ERR_APPEXECFWK_INSTALLD_SIGN_AOT_DISABLE) && !pendSignData.empty()) {
37 if (aotArgs.bundleName.empty() || aotArgs.moduleName.empty()) {
38 APP_LOGE("empty bundle or/and module name error");
39 return;
40 }
41 PendingData pendingData = {versionCode, pendSignData};
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 pendingSignData_[aotArgs.bundleName][aotArgs.moduleName] = pendingData;
45 }
46 }
47 }
48
RegisterScreenUnlockListener()49 void AOTSignDataCacheMgr::RegisterScreenUnlockListener()
50 {
51 APP_LOGI("register screen unlock event start");
52 EventFwk::MatchingSkills matchingSkill;
53 // use COMMON_EVENT_USER_UNLOCKED if only for device with PIN
54 matchingSkill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
55 EventFwk::CommonEventSubscribeInfo eventInfo(matchingSkill);
56 unlockEventSubscriber_ = std::make_shared<UnlockEventSubscriber>(eventInfo);
57 const bool result = EventFwk::CommonEventManager::SubscribeCommonEvent(unlockEventSubscriber_);
58 if (!result) {
59 APP_LOGE("register screen unlock event for pending sign AOT error");
60 return;
61 }
62 APP_LOGI("register screen unlock event for pending sign AOT success");
63 }
64
UnregisterScreenUnlockEvent()65 void AOTSignDataCacheMgr::UnregisterScreenUnlockEvent()
66 {
67 APP_LOGI("unregister screen unlock event start");
68 const bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(unlockEventSubscriber_);
69 if (!result) {
70 APP_LOGE("unregister screen unlock event error");
71 return;
72 }
73 APP_LOGI("unregister screen unlock event success");
74 }
75
OnReceiveEvent(const EventFwk::CommonEventData & event)76 void AOTSignDataCacheMgr::UnlockEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &event)
77 {
78 const auto want = event.GetWant();
79 const auto action = want.GetAction();
80 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
81 APP_LOGI("receive screen unlock event");
82 auto task = []() {
83 AOTSignDataCacheMgr::GetInstance().HandleUnlockEvent();
84 };
85 std::thread(task).detach();
86 }
87 }
88
HandleUnlockEvent()89 void AOTSignDataCacheMgr::HandleUnlockEvent()
90 {
91 APP_LOGI("pending sign thread is wake up");
92 UnregisterScreenUnlockEvent();
93
94 sleep(SLEEP_TIME_FOR_WAIT_SIGN_ENABLE);
95 isLocked_ = false;
96 int32_t loopTimes = 0;
97 while (ExecutePendSign() != ERR_OK) {
98 if (++loopTimes > LOOP_TIMES_FOR_WAIT_SIGN_ENABLE) {
99 APP_LOGE("wait for enforce sign enable time out");
100 return;
101 }
102 sleep(SLEEP_TIME_FOR_WAIT_SIGN_ENABLE);
103 }
104 {
105 std::lock_guard<std::mutex> lock(mutex_);
106 std::unordered_map<std::string, std::unordered_map<std::string, PendingData>>().swap(pendingSignData_);
107 }
108 APP_LOGI("pending enforce sign success");
109 }
110
ExecutePendSign()111 ErrCode AOTSignDataCacheMgr::ExecutePendSign()
112 {
113 std::lock_guard<std::mutex> lock(mutex_);
114 ErrCode ret = ERR_OK;
115 for (auto itBundle = pendingSignData_.begin(); itBundle != pendingSignData_.end(); ++itBundle) {
116 auto &bundleName = itBundle->first;
117 auto &moduleSignData = itBundle->second;
118 for (auto itModule = moduleSignData.begin(); itModule != moduleSignData.end();) {
119 auto &moduleName = itModule->first;
120 auto &signData = itModule->second.signData;
121 std::string anFileName = ServiceConstants::ARK_CACHE_PATH + bundleName + ServiceConstants::PATH_SEPARATOR
122 + ServiceConstants::ARM64 + ServiceConstants::PATH_SEPARATOR + moduleName + ServiceConstants::AN_SUFFIX;
123
124 ErrCode retCS = InstalldClient::GetInstance()->PendSignAOT(anFileName, signData);
125 if (retCS == ERR_APPEXECFWK_INSTALLD_SIGN_AOT_DISABLE) {
126 APP_LOGE("enforce sign service is disable");
127 ret = ERR_APPEXECFWK_INSTALLD_SIGN_AOT_FAILED;
128 ++itModule;
129 continue;
130 } else if (retCS != ERR_OK) {
131 itModule = moduleSignData.erase(itModule);
132 continue;
133 }
134 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
135 if (!dataMgr) {
136 APP_LOGE("dataMgr is null");
137 ret = ERR_APPEXECFWK_INSTALLD_SIGN_AOT_FAILED;
138 ++itModule;
139 continue;
140 }
141 auto versionCode = itModule->second.versionCode;
142 dataMgr->SetAOTCompileStatus(bundleName, moduleName, AOTCompileStatus::COMPILE_SUCCESS, versionCode);
143 itModule = moduleSignData.erase(itModule);
144 }
145 }
146 return ret;
147 }
148 } // namespace AppExecFwk
149 } // namespace OHOS
150