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 EventFwk::MatchingSkills matchingSkill;
52 // use COMMON_EVENT_USER_UNLOCKED if only for device with PIN
53 matchingSkill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
54 EventFwk::CommonEventSubscribeInfo eventInfo(matchingSkill);
55 unlockEventSubscriber_ = std::make_shared<UnlockEventSubscriber>(eventInfo);
56 const bool result = EventFwk::CommonEventManager::SubscribeCommonEvent(unlockEventSubscriber_);
57 if (!result) {
58 APP_LOGE_NOFUNC("register screen unlock event for pending sign AOT error");
59 return;
60 }
61 APP_LOGI_NOFUNC("AOT register screen unlock event success");
62 }
63
UnregisterScreenUnlockEvent()64 void AOTSignDataCacheMgr::UnregisterScreenUnlockEvent()
65 {
66 const bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(unlockEventSubscriber_);
67 if (!result) {
68 APP_LOGE_NOFUNC("unregister screen unlock event error");
69 return;
70 }
71 APP_LOGI_NOFUNC("unregister screen unlock event success");
72 }
73
OnReceiveEvent(const EventFwk::CommonEventData & event)74 void AOTSignDataCacheMgr::UnlockEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &event)
75 {
76 const auto want = event.GetWant();
77 const auto action = want.GetAction();
78 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
79 APP_LOGI("receive screen unlock event");
80 auto task = []() {
81 AOTSignDataCacheMgr::GetInstance().HandleUnlockEvent();
82 };
83 std::thread(task).detach();
84 }
85 }
86
HandleUnlockEvent()87 void AOTSignDataCacheMgr::HandleUnlockEvent()
88 {
89 APP_LOGI_NOFUNC("pending sign thread is wake up");
90 UnregisterScreenUnlockEvent();
91
92 sleep(SLEEP_TIME_FOR_WAIT_SIGN_ENABLE);
93 isLocked_ = false;
94 int32_t loopTimes = 0;
95 while (ExecutePendSign() != ERR_OK) {
96 if (++loopTimes > LOOP_TIMES_FOR_WAIT_SIGN_ENABLE) {
97 APP_LOGE_NOFUNC("wait for enforce sign enable time out");
98 return;
99 }
100 sleep(SLEEP_TIME_FOR_WAIT_SIGN_ENABLE);
101 }
102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104 std::unordered_map<std::string, std::unordered_map<std::string, PendingData>>().swap(pendingSignData_);
105 }
106 APP_LOGI_NOFUNC("pending enforce sign success");
107 }
108
ExecutePendSign()109 ErrCode AOTSignDataCacheMgr::ExecutePendSign()
110 {
111 std::lock_guard<std::mutex> lock(mutex_);
112 ErrCode ret = ERR_OK;
113 for (auto itBundle = pendingSignData_.begin(); itBundle != pendingSignData_.end(); ++itBundle) {
114 auto &bundleName = itBundle->first;
115 auto &moduleSignData = itBundle->second;
116 for (auto itModule = moduleSignData.begin(); itModule != moduleSignData.end();) {
117 auto &moduleName = itModule->first;
118 auto &signData = itModule->second.signData;
119 std::string anFileName = ServiceConstants::ARK_CACHE_PATH + bundleName + ServiceConstants::PATH_SEPARATOR
120 + ServiceConstants::ARM64 + ServiceConstants::PATH_SEPARATOR + moduleName + ServiceConstants::AN_SUFFIX;
121
122 ErrCode retCS = InstalldClient::GetInstance()->PendSignAOT(anFileName, signData);
123 if (retCS == ERR_APPEXECFWK_INSTALLD_SIGN_AOT_DISABLE) {
124 APP_LOGE("enforce sign service is disable");
125 ret = ERR_APPEXECFWK_INSTALLD_SIGN_AOT_FAILED;
126 ++itModule;
127 continue;
128 } else if (retCS != ERR_OK) {
129 itModule = moduleSignData.erase(itModule);
130 continue;
131 }
132 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
133 if (!dataMgr) {
134 APP_LOGE("dataMgr is null");
135 ret = ERR_APPEXECFWK_INSTALLD_SIGN_AOT_FAILED;
136 ++itModule;
137 continue;
138 }
139 auto versionCode = itModule->second.versionCode;
140 dataMgr->SetAOTCompileStatus(bundleName, moduleName, AOTCompileStatus::COMPILE_SUCCESS, versionCode);
141 itModule = moduleSignData.erase(itModule);
142 }
143 }
144 return ret;
145 }
146 } // namespace AppExecFwk
147 } // namespace OHOS
148