• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "distributed_sched_util.h"
17 
18 #include <future>
19 
20 #include "accesstoken_kit.h"
21 #include "bundle_installer_interface.h"
22 #include "dtbschedmgr_log.h"
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "nativetoken_kit.h"
26 #include "nocopyable.h"
27 #include "status_receiver_host.h"
28 #include "status_receiver_interface.h"
29 #include "system_ability_definition.h"
30 #include "token_setproc.h"
31 
32 namespace OHOS {
33 namespace DistributedSchedule {
34 using namespace AppExecFwk;
35 namespace {
36 const std::string TAG = "DistributedSchedUtil";
37 const std::string HAP_FILE_PATH = "/data/test/resource/dmsfwk/resource/bmsThirdBundle.hap";
38 const char* DISTSCHED_PROCESS_NAME = "distributedsched";
39 constexpr int32_t DMS_LOAD_SA_TIMEOUT_MS = 10000;
40 constexpr int32_t USER_ID = 100;
41 constexpr int32_t PERMS_NUM = 2;
42 constexpr int32_t FAILED_RETURN = -1;
43 }
44 
45 class StatusReceiverImpl : public StatusReceiverHost {
46 public:
47     StatusReceiverImpl();
48     ~StatusReceiverImpl() override;
49     void OnStatusNotify(const int progress) override;
50     void OnFinished(const int32_t resultCode, const std::string &resultMsg) override;
51 
52 private:
53     mutable std::promise<std::string> resultMsgSignal_;
54     int iProgress_ = 0;
55 
56     DISALLOW_COPY_AND_MOVE(StatusReceiverImpl);
57 };
58 
StatusReceiverImpl()59 StatusReceiverImpl::StatusReceiverImpl()
60 {
61     HILOGI("create status receiver instance");
62 }
63 
~StatusReceiverImpl()64 StatusReceiverImpl::~StatusReceiverImpl()
65 {
66     HILOGI("destroy status receiver instance");
67 }
68 
OnStatusNotify(const int progress)69 void StatusReceiverImpl::OnStatusNotify(const int progress)
70 {
71     iProgress_ = progress;
72     HILOGI("OnStatusNotify progress:%{public}d", progress);
73 }
74 
OnFinished(const int32_t resultCode,const std::string & resultMsg)75 void StatusReceiverImpl::OnFinished(const int32_t resultCode, const std::string &resultMsg)
76 {
77     HILOGI("on finished result is %{public}d, %{public}s", resultCode, resultMsg.c_str());
78     resultMsgSignal_.set_value(resultMsg);
79 }
80 
81 sptr<IRemoteObject> DistributedSchedUtil::remote_ = nullptr;
82 std::mutex DistributedSchedUtil::remoteMutex_;
83 std::condition_variable DistributedSchedUtil::remoteConVar_;
84 
MockPermission()85 void DistributedSchedUtil::MockPermission()
86 {
87     static const char *PERMS[] = {
88         "ohos.permission.DISTRIBUTED_DATASYNC",
89         "ohos.permission.ACCESS_SERVICE_DM"
90     };
91     MockProcessAndPermission(DISTSCHED_PROCESS_NAME, PERMS, PERMS_NUM);
92 }
93 
MockProcess(const char * processName)94 void DistributedSchedUtil::MockProcess(const char* processName)
95 {
96     MockProcessAndPermission(processName);
97 }
98 
GetBundleManager()99 sptr<AppExecFwk::IBundleMgr> DistributedSchedUtil::GetBundleManager()
100 {
101     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102     if (samgrProxy == nullptr) {
103         return nullptr;
104     }
105     sptr<IRemoteObject> bmsProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
106     if (bmsProxy == nullptr) {
107         HILOGE("failed to get bms from samgr");
108         return nullptr;
109     }
110     return iface_cast<AppExecFwk::IBundleMgr>(bmsProxy);
111 }
112 
InstallThirdPartyHap()113 int32_t DistributedSchedUtil::InstallThirdPartyHap()
114 {
115     auto bms = GetBundleManager();
116     if (bms == nullptr) {
117         HILOGE("bms is null");
118         return FAILED_RETURN;
119     }
120     auto installer = bms->GetBundleInstaller();
121     if (!installer) {
122         HILOGE("installer is null");
123         return FAILED_RETURN;
124     }
125     InstallParam installParam;
126     installParam.installFlag = InstallFlag::NORMAL;
127     installParam.userId = USER_ID;
128     sptr<StatusReceiverImpl> statusReceiver(new (std::nothrow) StatusReceiverImpl());
129     if (!statusReceiver) {
130         return FAILED_RETURN;
131     }
132     bool result = installer->Install(HAP_FILE_PATH, installParam, statusReceiver);
133     if (!result) {
134         return FAILED_RETURN;
135     }
136     return 0;
137 }
138 
MockProcessAndPermission(const char * processName,const char * perms[],int32_t permsNum)139 void DistributedSchedUtil::MockProcessAndPermission(const char* processName, const char *perms[], int32_t permsNum)
140 {
141     uint64_t tokenId;
142     NativeTokenInfoParams infoInstance = {
143         .dcapsNum = 0,
144         .permsNum = permsNum,
145         .aclsNum = 0,
146         .dcaps = nullptr,
147         .perms = perms,
148         .acls = nullptr,
149         .processName = processName,
150         .aplStr = "system_core",
151     };
152     tokenId = GetAccessTokenId(&infoInstance);
153     SetSelfTokenID(tokenId);
154     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
155 }
156 
LoadDistributedSchedService()157 bool DistributedSchedUtil::LoadDistributedSchedService()
158 {
159     std::unique_lock<std::mutex> lock(remoteMutex_);
160     sptr<DistributedSchedLoadCallback> loadCallback = new DistributedSchedLoadCallback();
161     if (loadCallback == nullptr) {
162         HILOGE("loadCallback is nullptr");
163         return false;
164     }
165     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
166     if (samgrProxy == nullptr) {
167         HILOGE("get samgr failed");
168         return false;
169     }
170     int32_t ret = samgrProxy->LoadSystemAbility(DISTRIBUTED_SCHED_SA_ID, loadCallback);
171     if (ret != ERR_OK) {
172         HILOGE("load dms SA failed");
173         return false;
174     }
175     auto waitStatus = remoteConVar_.wait_for(lock, std::chrono::milliseconds(DMS_LOAD_SA_TIMEOUT_MS),
176         []() { return remote_ != nullptr; });
177     if (!waitStatus) {
178         HILOGE("dms SA load timeout");
179         return false;
180     }
181     return true;
182 }
183 
LoadSystemAbilitySuccessNotify(const sptr<IRemoteObject> & remoteObject)184 void DistributedSchedUtil::LoadSystemAbilitySuccessNotify(const sptr<IRemoteObject>& remoteObject)
185 {
186     std::lock_guard<std::mutex> lock(remoteMutex_);
187     remote_ = remoteObject;
188     remoteConVar_.notify_one();
189 }
190 
LoadSystemAbilityFailNotify()191 void DistributedSchedUtil::LoadSystemAbilityFailNotify()
192 {
193     std::lock_guard<std::mutex> lock(remoteMutex_);
194     remote_ = nullptr;
195 }
196 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)197 void DistributedSchedLoadCallback::OnLoadSystemAbilitySuccess(
198     int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
199 {
200     HILOGD("called.");
201     DistributedSchedUtil::LoadSystemAbilitySuccessNotify(remoteObject);
202 }
203 
OnLoadSystemAbilityFail(int32_t systemAbilityId)204 void DistributedSchedLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
205 {
206     HILOGD("called.");
207     DistributedSchedUtil::LoadSystemAbilityFailNotify();
208 }
209 }
210 }