1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 #include "system_ability_manager_wrapper.h"
15
16 #include <algorithm>
17 #include <cstdint>
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <vector>
22
23 #include "cxx.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "isystem_ability_status_change.h"
27 #include "isystem_process_status_change.h"
28 #include "refbase.h"
29 #include "status_change_wrapper.h"
30 #include "string_ex.h"
31 #include "wrapper.rs.h"
32
33 namespace OHOS {
34 namespace SamgrRust {
35
36 static constexpr size_t MAX_RUST_STR_LEN = 1024;
37
38 static void* g_selfSoHandle = nullptr;
39
InitSamgrRust()40 extern "C" __attribute__((constructor)) void InitSamgrRust()
41 {
42 if (g_selfSoHandle != nullptr) {
43 return;
44 }
45 Dl_info info;
46 int ret = dladdr(reinterpret_cast<void *>(InitSamgrRust), &info);
47 if (ret == 0) {
48 return;
49 }
50
51 char path[PATH_MAX] = {'\0'};
52 if (realpath(info.dli_fname, path) == nullptr) {
53 return;
54 }
55 std::vector<std::string> strVector;
56 SplitStr(path, "/", strVector);
57 auto vectorSize = strVector.size();
58 if (vectorSize == 0) {
59 return;
60 }
61 auto& fileName = strVector[vectorSize - 1];
62 g_selfSoHandle = dlopen(fileName.c_str(), RTLD_LAZY);
63 if (g_selfSoHandle == nullptr) {
64 return;
65 }
66 }
67
ListSystemAbilities()68 rust::Vec<rust::String> ListSystemAbilities()
69 {
70 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
71 auto res = rust::Vec<rust::String>();
72
73 if (sysm == nullptr) {
74 return res;
75 }
76
77 auto list = sysm->ListSystemAbilities();
78 for (auto s : list) {
79 res.push_back(s.data());
80 }
81 return res;
82 }
83
ListSystemAbilitiesWithDumpFlag(unsigned int dumpFlags)84 rust::Vec<rust::String> ListSystemAbilitiesWithDumpFlag(unsigned int dumpFlags)
85 {
86 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87 auto res = rust::Vec<rust::String>();
88
89 if (sysm == nullptr) {
90 return res;
91 }
92
93 auto list = sysm->ListSystemAbilities(dumpFlags);
94 for (auto s : list) {
95 char16_t *c = s.data();
96 res.push_back(c);
97 }
98 return res;
99 }
100
LoadSystemAbility(int32_t systemAbilityId,int32_t timeout)101 std::unique_ptr<SptrIRemoteObject> LoadSystemAbility(int32_t systemAbilityId, int32_t timeout)
102 {
103 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
104 if (sysm == nullptr) {
105 return nullptr;
106 }
107
108 auto ability = sysm->LoadSystemAbility(systemAbilityId, timeout);
109 if (ability == nullptr) {
110 return nullptr;
111 }
112 return std::make_unique<SptrIRemoteObject>(std::move(ability));
113 }
114
LoadSystemAbilityWithCallback(int32_t systemAbilityId,rust::Fn<void ()> on_success,rust::Fn<void ()> on_fail)115 int32_t LoadSystemAbilityWithCallback(int32_t systemAbilityId, rust::Fn<void()> on_success, rust::Fn<void()> on_fail)
116 {
117 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
118 if (sysm == nullptr) {
119 return -1;
120 }
121 auto callback = sptr<LoadCallbackWrapper>::MakeSptr(on_success, on_fail);
122 return sysm->LoadSystemAbility(systemAbilityId, callback);
123 }
124
GetContextManager()125 std::unique_ptr<SptrIRemoteObject> GetContextManager()
126 {
127 sptr<IRemoteObject> saMgr = IPCSkeleton::GetContextObject();
128 if (saMgr == nullptr) {
129 return nullptr;
130 }
131 return std::make_unique<SptrIRemoteObject>(std::move(saMgr));
132 }
133
GetSystemAbility(int32_t systemAbilityId)134 std::unique_ptr<SptrIRemoteObject> GetSystemAbility(int32_t systemAbilityId)
135 {
136 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
137 if (sysm == nullptr) {
138 return nullptr;
139 }
140
141 auto ability = sysm->GetSystemAbility(systemAbilityId);
142 if (ability == nullptr) {
143 return nullptr;
144 }
145 return std::make_unique<SptrIRemoteObject>(std::move(ability));
146 }
147
CheckSystemAbility(int32_t systemAbilityId)148 std::unique_ptr<SptrIRemoteObject> CheckSystemAbility(int32_t systemAbilityId)
149 {
150 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151 if (sysm == nullptr) {
152 return nullptr;
153 }
154
155 auto ability = sysm->CheckSystemAbility(systemAbilityId);
156 if (ability == nullptr) {
157 return nullptr;
158 }
159 return std::make_unique<SptrIRemoteObject>(std::move(ability));
160 }
161
RemoveSystemAbility(int32_t systemAbilityId)162 int32_t RemoveSystemAbility(int32_t systemAbilityId)
163 {
164 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
165 if (sysm == nullptr) {
166 return -1;
167 }
168 return sysm->RemoveSystemAbility(systemAbilityId);
169 }
170
GetSystemAbilityWithDeviceId(int32_t systemAbilityId,const std::string & deviceId)171 std::unique_ptr<SptrIRemoteObject> GetSystemAbilityWithDeviceId(int32_t systemAbilityId, const std::string &deviceId)
172 {
173 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
174 if (sysm == nullptr) {
175 return nullptr;
176 }
177 auto ability = sysm->GetSystemAbility(systemAbilityId, deviceId);
178 if (ability == nullptr) {
179 return nullptr;
180 }
181 return std::make_unique<SptrIRemoteObject>(std::move(ability));
182 }
183
CheckSystemAbilityWithDeviceId(int32_t systemAbilityId,const std::string & deviceId)184 std::unique_ptr<SptrIRemoteObject> CheckSystemAbilityWithDeviceId(int32_t systemAbilityId, const std::string &deviceId)
185 {
186 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
187 if (sysm == nullptr) {
188 return nullptr;
189 }
190 auto ability = sysm->CheckSystemAbility(systemAbilityId, deviceId);
191 if (ability == nullptr) {
192 return nullptr;
193 }
194 return std::make_unique<SptrIRemoteObject>(std::move(ability));
195 }
196
SubscribeSystemAbility(int32_t systemAbilityId,rust::Fn<void (int32_t systemAbilityId,const rust::str deviceId)> onAdd,rust::Fn<void (int32_t systemAbilityId,const rust::str deviceId)> onRemove)197 std::unique_ptr<UnSubscribeSystemAbilityHandler> SubscribeSystemAbility(int32_t systemAbilityId,
198 rust::Fn<void(int32_t systemAbilityId, const rust::str deviceId)> onAdd,
199 rust::Fn<void(int32_t systemAbilityId, const rust::str deviceId)> onRemove)
200 {
201 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
202 if (sysm == nullptr) {
203 return nullptr;
204 }
205
206 sptr<ISystemAbilityStatusChange> listener = new SystemAbilityStatusChangeWrapper(onAdd, onRemove);
207 sysm->SubscribeSystemAbility(systemAbilityId, listener);
208 return std::make_unique<UnSubscribeSystemAbilityHandler>(systemAbilityId, listener);
209 }
210
AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,const rust::str localAbilityManagerName)211 int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const rust::str localAbilityManagerName)
212 {
213 if (localAbilityManagerName.length() > MAX_RUST_STR_LEN) {
214 return -1;
215 }
216
217 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
218 if (sysm == nullptr) {
219 return -1;
220 }
221 std::u16string s = Str8ToStr16(std::string(localAbilityManagerName));
222 return sysm->AddOnDemandSystemAbilityInfo(systemAbilityId, s);
223 }
224
UnloadSystemAbility(int32_t systemAbilityId)225 int32_t UnloadSystemAbility(int32_t systemAbilityId)
226 {
227 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
228 if (sysm == nullptr) {
229 return -1;
230 }
231 return sysm->UnloadSystemAbility(systemAbilityId);
232 }
233
CancelUnloadSystemAbility(int32_t systemAbilityId)234 int32_t CancelUnloadSystemAbility(int32_t systemAbilityId)
235 {
236 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
237 if (sysm == nullptr) {
238 return -1;
239 }
240 return sysm->CancelUnloadSystemAbility(systemAbilityId);
241 }
242
UnloadAllIdleSystemAbility()243 int32_t UnloadAllIdleSystemAbility()
244 {
245 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
246 if (sysm == nullptr) {
247 return -1;
248 }
249 return sysm->UnloadAllIdleSystemAbility();
250 }
251
AddSystemAbility(int32_t systemAbilityId,rust::Box<AbilityStub> ability,AddSystemAbilityConfig config)252 int32_t AddSystemAbility(int32_t systemAbilityId, rust::Box<AbilityStub> ability, AddSystemAbilityConfig config)
253 {
254 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
255 if (sysm == nullptr) {
256 return -1;
257 }
258 auto capability_u16 = Str8ToStr16(std::string(config.capability));
259 auto permission_u16 = Str8ToStr16(std::string(config.permission));
260
261 ISystemAbilityManager::SAExtraProp extra(config.is_distributed, config.dump_flags, capability_u16, permission_u16);
262 auto stub = sptr<RemoteServiceStub>::MakeSptr(ability.into_raw());
263
264 return sysm->AddSystemAbility(systemAbilityId, stub);
265 }
266
GetSystemProcessInfo(int32_t systemAbilityId)267 SystemProcessInfo GetSystemProcessInfo(int32_t systemAbilityId)
268 {
269 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
270 OHOS::SystemProcessInfo info;
271 if (sysm == nullptr) {
272 return SystemProcessInfo{
273 .processName = info.processName.data(),
274 .pid = info.pid,
275 .uid = info.uid,
276 };
277 }
278 sysm->GetSystemProcessInfo(systemAbilityId, info);
279 return SystemProcessInfo{
280 .processName = info.processName.data(),
281 .pid = info.pid,
282 .uid = info.uid,
283 };
284 }
285
GetRunningSystemProcess()286 rust::Vec<SystemProcessInfo> GetRunningSystemProcess()
287 {
288 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
289
290 auto res = rust::Vec<SystemProcessInfo>();
291 if (sysm == nullptr) {
292 return res;
293 }
294
295 auto infos = std::list<OHOS::SystemProcessInfo>();
296 sysm->GetRunningSystemProcess(infos);
297 for (auto info : infos) {
298 res.push_back(SystemProcessInfo{
299 .processName = info.processName,
300 .pid = info.pid,
301 .uid = info.uid,
302 });
303 }
304 return res;
305 }
306
GetCommonEventExtraDataIdlist(int32_t saId,rust::Vec<int64_t> & extraDataIdList,const std::string & eventName)307 int32_t GetCommonEventExtraDataIdlist(int32_t saId, rust::Vec<int64_t> &extraDataIdList, const std::string &eventName)
308 {
309 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
310 if (sysm == nullptr) {
311 return -1;
312 }
313
314 std::vector<int64_t> idList;
315 auto ret = sysm->GetCommonEventExtraDataIdlist(saId, idList, eventName);
316 if (ret != ERR_OK) {
317 return ret;
318 }
319 for (auto id : idList) {
320 extraDataIdList.push_back(id);
321 }
322 return ret;
323 }
324
SubscribeSystemProcess(rust::Fn<void (const OHOS::SamgrRust::SystemProcessInfo & systemProcessInfo)> onStart_,rust::Fn<void (const OHOS::SamgrRust::SystemProcessInfo & systemProcessInfo)> onStop_)325 std::unique_ptr<UnSubscribeSystemProcessHandler> SubscribeSystemProcess(
326 rust::Fn<void(const OHOS::SamgrRust::SystemProcessInfo &systemProcessInfo)> onStart_,
327 rust::Fn<void(const OHOS::SamgrRust::SystemProcessInfo &systemProcessInfo)> onStop_)
328 {
329 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
330 if (sysm == nullptr) {
331 return nullptr;
332 }
333 sptr<ISystemProcessStatusChange> listener = new SystemProcessStatusChangeWrapper(nullptr, onStart_, onStop_);
334 sysm->SubscribeSystemProcess(listener);
335 return std::make_unique<UnSubscribeSystemProcessHandler>(listener);
336 }
337
GetOnDemandReasonExtraData(int64_t extraDataId,MessageParcel & parcel)338 int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel &parcel)
339 {
340 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
341 if (sysm == nullptr) {
342 return -1;
343 }
344 return sysm->GetOnDemandReasonExtraData(extraDataId, parcel);
345 }
346
SendStrategy(int32_t type,rust::Vec<int32_t> systemAbilityIds,int32_t level,std::string & action)347 int32_t SendStrategy(int32_t type, rust::Vec<int32_t> systemAbilityIds, int32_t level, std::string &action)
348 {
349 auto sysm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
350 if (sysm == nullptr) {
351 return -1;
352 }
353 auto v = std::vector<int32_t>();
354 for (auto id : systemAbilityIds) {
355 v.push_back(id);
356 }
357 return sysm->SendStrategy(type, v, level, action);
358 }
359
RemoteServiceStub(AbilityStub * ability)360 RemoteServiceStub::RemoteServiceStub(AbilityStub *ability)
361 {
362 this->inner_ = ability;
363 }
364
~RemoteServiceStub()365 RemoteServiceStub::~RemoteServiceStub()
366 {
367 auto ability = rust::Box<AbilityStub>::from_raw(this->inner_);
368 }
369
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)370 int RemoteServiceStub ::OnRemoteRequest(
371 uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option)
372 {
373 return inner_->on_remote_request(code, data, reply);
374 }
375
OnRemoteDump(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)376 int RemoteServiceStub ::OnRemoteDump(
377 uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option)
378 {
379 return 0;
380 }
381
LoadCallbackWrapper(rust::Fn<void ()> on_success,rust::Fn<void ()> on_fail)382 LoadCallbackWrapper::LoadCallbackWrapper(rust::Fn<void()> on_success, rust::Fn<void()> on_fail)
383 : on_success_(on_success), on_fail_(on_fail)
384 {
385 }
386
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)387 void LoadCallbackWrapper::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
388 {
389 on_success_();
390 }
391
OnLoadSystemAbilityFail(int32_t systemAbilityId)392 void LoadCallbackWrapper::OnLoadSystemAbilityFail(int32_t systemAbilityId)
393 {
394 on_fail_();
395 }
396
397 } // namespace SamgrRust
398 } // namespace OHOS