• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "system_ability_manager_stub.h"
17 
18 #include <unistd.h>
19 #include <cinttypes>
20 
21 #include "accesstoken_kit.h"
22 #include "datetime_ex.h"
23 #include "errors.h"
24 #include "hitrace_meter.h"
25 #include "ipc_skeleton.h"
26 #include "ipc_types.h"
27 #include "memory_guard.h"
28 #include "sam_log.h"
29 #include "string_ex.h"
30 #include "system_ability_manager.h"
31 #include "system_ability_on_demand_event.h"
32 #include "tools.h"
33 
34 #ifdef WITH_SELINUX
35 #include "service_checker.h"
36 #endif
37 
38 namespace {
39 #ifdef WITH_SELINUX
40     using namespace OHOS::HiviewDFX;
41     OHOS::HiviewDFX::HiLogLabel label_ = { LOG_CORE, 0xD001800, "SA_SELINUX" };
42     std::unique_ptr<ServiceChecker> selinuxChecker_ = std::make_unique<ServiceChecker>(false);
43 #endif
44 
CheckGetSAPermission(const int32_t said)45     bool CheckGetSAPermission(const int32_t said)
46     {
47 #ifdef WITH_SELINUX
48         int64_t begin = OHOS::GetTickCount();
49         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
50         auto ret = selinuxChecker_->GetServiceCheck(callingPid, std::to_string(said)) == 0;
51         HiLog::Debug(label_, "[Performance] GetServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
52             said, OHOS::GetTickCount() - begin);
53         return  ret;
54 #else
55         return true; // if not support selinux, not check selinux permission
56 #endif
57     }
58 
CheckAddOrRemovePermission(const int32_t said)59     bool CheckAddOrRemovePermission(const int32_t said)
60     {
61 #ifdef WITH_SELINUX
62         int64_t begin = OHOS::GetTickCount();
63         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
64         auto ret = selinuxChecker_->AddServiceCheck(callingPid, std::to_string(said)) == 0;
65         HiLog::Debug(label_, "[Performance] AddServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
66             said, OHOS::GetTickCount() - begin);
67         return ret;
68 #else
69         return true; // if not support selinux, not check selinux permission
70 #endif
71     }
72 
CheckGetRemoteSAPermission(const int32_t said)73     bool CheckGetRemoteSAPermission(const int32_t said)
74     {
75 #ifdef WITH_SELINUX
76         int64_t begin = OHOS::GetTickCount();
77         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
78         auto ret = selinuxChecker_->GetRemoteServiceCheck(callingPid, std::to_string(said)) == 0;
79         HiLog::Debug(label_, "[Performance] GetRemoteServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
80             said, OHOS::GetTickCount() - begin);
81         return ret;
82 #else
83         return true; // if not support selinux, not check selinux permission
84 #endif
85     }
86 
CheckListSAPermission()87     bool CheckListSAPermission()
88     {
89 #ifdef WITH_SELINUX
90         int64_t begin = OHOS::GetTickCount();
91         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
92         auto ret = selinuxChecker_->ListServiceCheck(callingPid) == 0;
93         HiLog::Debug(label_, "[Performance] ListServiceCheck spend %{public}" PRId64 " ms",
94             OHOS::GetTickCount() - begin);
95         return ret;
96 #else
97         return true; // if not support selinux, not check selinux permission
98 #endif
99     }
100 }
101 
102 using namespace OHOS::Security;
103 namespace OHOS {
SetAbilityFuncMap()104 void SystemAbilityManagerStub::SetAbilityFuncMap()
105 {
106     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_SYSTEM_ABILITY_TRANSACTION)] =
107         &SystemAbilityManagerStub::GetSystemAbilityInner;
108     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_TRANSACTION)] =
109         &SystemAbilityManagerStub::CheckSystemAbilityInner;
110     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION)] =
111         &SystemAbilityManagerStub::AddSystemAbilityInner;
112     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::REMOVE_SYSTEM_ABILITY_TRANSACTION)] =
113         &SystemAbilityManagerStub::RemoveSystemAbilityInner;
114     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::LIST_SYSTEM_ABILITY_TRANSACTION)] =
115         &SystemAbilityManagerStub::ListSystemAbilityInner;
116     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION)] =
117         &SystemAbilityManagerStub::SubsSystemAbilityInner;
118     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION)] =
119         &SystemAbilityManagerStub::CheckRemtSystemAbilityInner;
120     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION)] =
121         &SystemAbilityManagerStub::AddOndemandSystemAbilityInner;
122     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION)] =
123         &SystemAbilityManagerStub::CheckSystemAbilityImmeInner;
124     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION)] =
125         &SystemAbilityManagerStub::UnSubsSystemAbilityInner;
126     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::LOAD_SYSTEM_ABILITY_TRANSACTION)] =
127         &SystemAbilityManagerStub::LoadSystemAbilityInner;
128     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::LOAD_REMOTE_SYSTEM_ABILITY_TRANSACTION)] =
129         &SystemAbilityManagerStub::LoadRemoteSystemAbilityInner;
130     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNLOAD_SYSTEM_ABILITY_TRANSACTION)] =
131         &SystemAbilityManagerStub::UnloadSystemAbilityInner;
132     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CANCEL_UNLOAD_SYSTEM_ABILITY_TRANSACTION)] =
133         &SystemAbilityManagerStub::CancelUnloadSystemAbilityInner;
134 }
135 
SetProcessFuncMap()136 void SystemAbilityManagerStub::SetProcessFuncMap()
137 {
138     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_PROCESS_TRANSACTION)] =
139         &SystemAbilityManagerStub::AddSystemProcessInner;
140     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_SYSTEM_PROCESS_INFO_TRANSACTION)] =
141         &SystemAbilityManagerStub::GetSystemProcessInfoInner;
142     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_RUNNING_SYSTEM_PROCESS_TRANSACTION)] =
143         &SystemAbilityManagerStub::GetRunningSystemProcessInner;
144     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_PROCESS_TRANSACTION)] =
145         &SystemAbilityManagerStub::SubscribeSystemProcessInner;
146     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_PROCESS_TRANSACTION)] =
147         &SystemAbilityManagerStub::UnSubscribeSystemProcessInner;
148 }
149 
SystemAbilityManagerStub()150 SystemAbilityManagerStub::SystemAbilityManagerStub()
151 {
152     SetAbilityFuncMap();
153     SetProcessFuncMap();
154     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_REASON_EXTRA_DATA_TRANSACTION)] =
155         &SystemAbilityManagerStub::GetOnDemandReasonExtraDataInner;
156     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEAMND_POLICY_TRANSACTION)] =
157         &SystemAbilityManagerStub::GetOnDemandPolicyInner;
158     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UPDATE_ONDEAMND_POLICY_TRANSACTION)] =
159         &SystemAbilityManagerStub::UpdateOnDemandPolicyInner;
160     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_SYSTEM_ABILITY_IDS_TRANSACTION)] =
161         &SystemAbilityManagerStub::GetOnDemandSystemAbilityIdsInner;
162     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::SEND_STRATEGY_TRANASACTION)] =
163         &SystemAbilityManagerStub::SendStrategyInner;
164     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNLOAD_ALL_IDLE_SYSTEM_ABILITY_TRANSACTION)] =
165         &SystemAbilityManagerStub::UnloadAllIdleSystemAbilityInner;
166 }
167 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)168 int32_t SystemAbilityManagerStub::OnRemoteRequest(uint32_t code,
169     MessageParcel& data, MessageParcel& reply, MessageOption &option)
170 {
171     HILOGD("SAMStub::OnReceived, code = %{public}u, callerPid = %{public}d",
172         code, IPCSkeleton::GetCallingPid());
173     Samgr::MemoryGuard cacheGuard;
174     if (!EnforceInterceToken(data)) {
175         HILOGE("SAMStub::OnReceived, code = %{public}u, check interfaceToken failed", code);
176         return ERR_PERMISSION_DENIED;
177     }
178     auto itFunc = memberFuncMap_.find(code);
179     if (itFunc != memberFuncMap_.end()) {
180         auto memberFunc = itFunc->second;
181         if (memberFunc != nullptr) {
182             return (this->*memberFunc)(data, reply);
183         }
184     }
185     HILOGW("SAMStub: default case, need check.");
186     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
187 }
188 
EnforceInterceToken(MessageParcel & data)189 bool SystemAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
190 {
191     std::u16string interfaceToken = data.ReadInterfaceToken();
192     return interfaceToken == SAMANAGER_INTERFACE_TOKEN;
193 }
194 
ListSystemAbilityInner(MessageParcel & data,MessageParcel & reply)195 int32_t SystemAbilityManagerStub::ListSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
196 {
197     if (!CanRequest()) {
198         HILOGE("ListSystemAbilityInner PERMISSION DENIED!");
199         return ERR_PERMISSION_DENIED;
200     }
201 
202     if (!CheckListSAPermission()) {
203         HILOGE("ListSystemAbilityInner selinux permission denied!");
204         return ERR_PERMISSION_DENIED;
205     }
206 
207     int32_t dumpFlag = 0;
208     bool ret = data.ReadInt32(dumpFlag);
209     if (!ret) {
210         HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner read dumpflag failed!");
211         return ERR_FLATTEN_OBJECT;
212     }
213 
214     std::vector<std::u16string> saNameVector = ListSystemAbilities(dumpFlag);
215     if (saNameVector.empty()) {
216         HILOGI("List System Abilities list errors");
217         ret = reply.WriteInt32(ERR_INVALID_VALUE);
218     } else {
219         HILOGI("SystemAbilityManagerStub::ListSystemAbilityInner list success");
220         ret = reply.WriteInt32(ERR_NONE);
221         if (ret) {
222             ret = reply.WriteString16Vector(saNameVector);
223         }
224     }
225 
226     if (!ret) {
227         HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner write reply failed.");
228         return ERR_FLATTEN_OBJECT;
229     }
230 
231     return ERR_NONE;
232 }
233 
SubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)234 int32_t SystemAbilityManagerStub::SubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
235 {
236     int32_t systemAbilityId = -1;
237     bool ret = data.ReadInt32(systemAbilityId);
238     if (!ret) {
239         return ERR_NULL_OBJECT;
240     }
241     if (!CheckInputSysAbilityId(systemAbilityId)) {
242         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read systemAbilityId failed!");
243         return ERR_NULL_OBJECT;
244     }
245     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
246     if (remoteObject == nullptr) {
247         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read listener failed!");
248         return ERR_NULL_OBJECT;
249     }
250     sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
251     if (listener == nullptr) {
252         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner iface_cast failed!");
253         return ERR_NULL_OBJECT;
254     }
255     int32_t result = SubscribeSystemAbility(systemAbilityId, listener);
256     HILOGD("SystemAbilityManagerStub::SubsSystemAbilityInner result is %d", result);
257     ret = reply.WriteInt32(result);
258     if (!ret) {
259         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner write reply failed.");
260         return ERR_FLATTEN_OBJECT;
261     }
262 
263     return result;
264 }
265 
UnSubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)266 int32_t SystemAbilityManagerStub::UnSubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
267 {
268     int32_t systemAbilityId = -1;
269     bool ret = data.ReadInt32(systemAbilityId);
270     if (!ret) {
271         return ERR_NULL_OBJECT;
272     }
273     if (!CheckInputSysAbilityId(systemAbilityId)) {
274         HILOGW("SystemAbilityManagerStub::UnSubsSystemAbilityInner read systemAbilityId failed!");
275         return ERR_NULL_OBJECT;
276     }
277     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
278     if (remoteObject == nullptr) {
279         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility read listener failed!");
280         return ERR_NULL_OBJECT;
281     }
282     sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
283     if (listener == nullptr) {
284         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility iface_cast failed!");
285         return ERR_NULL_OBJECT;
286     }
287     int32_t result = UnSubscribeSystemAbility(systemAbilityId, listener);
288     HILOGD("SystemAbilityManagerStub::UnSubscribeSystemAbility result is %d", result);
289     ret = reply.WriteInt32(result);
290     if (!ret) {
291         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility write reply failed.");
292         return ERR_FLATTEN_OBJECT;
293     }
294 
295     return result;
296 }
297 
CheckRemtSystemAbilityInner(MessageParcel & data,MessageParcel & reply)298 int32_t SystemAbilityManagerStub::CheckRemtSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
299 {
300     if (!CanRequest()) {
301         HILOGE("CheckRemoteSystemAbilityInner PERMISSION DENIED!");
302         return ERR_PERMISSION_DENIED;
303     }
304     int32_t systemAbilityId = -1;
305     bool ret = data.ReadInt32(systemAbilityId);
306     if (!ret) {
307         return ERR_NULL_OBJECT;
308     }
309     if (!CheckInputSysAbilityId(systemAbilityId)) {
310         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read systemAbilityId failed!");
311         return ERR_NULL_OBJECT;
312     }
313 
314     if (!CheckGetRemoteSAPermission(systemAbilityId)) {
315         HILOGE("CheckRemtSystemAbilityInner selinux permission denied!, SA:%{public}d", systemAbilityId);
316         return ERR_PERMISSION_DENIED;
317     }
318 
319     std::string deviceId;
320     ret = data.ReadString(deviceId);
321     if (!ret) {
322         HILOGW("CheckRemtSystemAbilityInner read deviceId failed!");
323         return ERR_FLATTEN_OBJECT;
324     }
325     std::string uuid = SystemAbilityManager::GetInstance()->TransformDeviceId(deviceId, UUID, false);
326     ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId, uuid));
327     if (!ret) {
328         HILOGW("CheckRemtSystemAbilityInner SA:%{public}d write reply failed.", systemAbilityId);
329         return ERR_FLATTEN_OBJECT;
330     }
331 
332     return ERR_NONE;
333 }
334 
AddOndemandSystemAbilityInner(MessageParcel & data,MessageParcel & reply)335 int32_t SystemAbilityManagerStub::AddOndemandSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
336 {
337     if (!CanRequest()) {
338         HILOGE("AddOndemandSystemAbilityInner PERMISSION DENIED!");
339         return ERR_PERMISSION_DENIED;
340     }
341     int32_t systemAbilityId = -1;
342     bool ret = data.ReadInt32(systemAbilityId);
343     if (!ret) {
344         return ERR_NULL_OBJECT;
345     }
346     if (!CheckInputSysAbilityId(systemAbilityId)) {
347         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read systemAbilityId failed!");
348         return ERR_NULL_OBJECT;
349     }
350 
351     if (!CheckAddOrRemovePermission(systemAbilityId)) {
352         HILOGE("AddOndemandSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
353         return ERR_PERMISSION_DENIED;
354     }
355 
356     std::u16string localManagerName = data.ReadString16();
357     if (localManagerName.empty()) {
358         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read localName failed!");
359         return ERR_NULL_OBJECT;
360     }
361 
362     int32_t result = AddOnDemandSystemAbilityInfo(systemAbilityId, localManagerName);
363     HILOGD("SystemAbilityManagerStub::AddOndemandSystemAbilityInner result is %d", result);
364     ret = reply.WriteInt32(result);
365     if (!ret) {
366         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner write reply failed.");
367         return ERR_FLATTEN_OBJECT;
368     }
369 
370     return result;
371 }
372 
CheckSystemAbilityImmeInner(MessageParcel & data,MessageParcel & reply)373 int32_t SystemAbilityManagerStub::CheckSystemAbilityImmeInner(MessageParcel& data, MessageParcel& reply)
374 {
375     int64_t begin = OHOS::GetTickCount();
376     int32_t systemAbilityId = -1;
377     bool ret = data.ReadInt32(systemAbilityId);
378     if (!ret) {
379         return ERR_NULL_OBJECT;
380     }
381     if (!CheckInputSysAbilityId(systemAbilityId)) {
382         HILOGW("CheckSystemAbilityImmeInner read systemAbilityId failed!");
383         return ERR_NULL_OBJECT;
384     }
385 
386     if (!CheckGetSAPermission(systemAbilityId)) {
387         HILOGD("CheckSystemAbilityImmeInner selinux permission denied! SA : %{public}d", systemAbilityId);
388         return ERR_PERMISSION_DENIED;
389     }
390 
391     bool isExist = false;
392     ret = data.ReadBool(isExist);
393     if (!ret) {
394         HILOGW("CheckSystemAbilityImmeInner read isExist failed!");
395         return ERR_FLATTEN_OBJECT;
396     }
397     ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId, isExist));
398     if (!ret) {
399         HILOGD("CheckSystemAbilityImmeInner SA:%{public}d, callpid:%{public}d, write obj fail, spend %{public}"
400             PRId64 " ms", systemAbilityId, OHOS::IPCSkeleton::GetCallingPid(), OHOS::GetTickCount() - begin);
401         return ERR_FLATTEN_OBJECT;
402     }
403 
404     ret = reply.WriteBool(isExist);
405     if (!ret) {
406         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner write reply failed.");
407         return ERR_FLATTEN_OBJECT;
408     }
409 
410     return ERR_NONE;
411 }
412 
UnmarshalingSaExtraProp(MessageParcel & data,SAExtraProp & extraProp)413 int32_t SystemAbilityManagerStub::UnmarshalingSaExtraProp(MessageParcel& data, SAExtraProp& extraProp)
414 {
415     bool isDistributed = false;
416     bool ret = data.ReadBool(isDistributed);
417     if (!ret) {
418         HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp read isDistributed failed!");
419         return ERR_FLATTEN_OBJECT;
420     }
421 
422     int32_t dumpFlags = 0;
423     ret = data.ReadInt32(dumpFlags);
424     if (!ret || dumpFlags < 0) {
425         HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp dumpFlags failed!");
426         return ERR_FLATTEN_OBJECT;
427     }
428     std::u16string capability = data.ReadString16();
429     std::u16string permission = data.ReadString16();
430     extraProp.isDistributed = isDistributed;
431     extraProp.dumpFlags = static_cast<uint32_t>(dumpFlags);
432     extraProp.capability = capability;
433     extraProp.permission = permission;
434     return ERR_OK;
435 }
436 
AddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)437 int32_t SystemAbilityManagerStub::AddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
438 {
439     if (!CanRequest()) {
440         HILOGE("AddSystemAbilityInner PERMISSION DENIED!");
441         return ERR_PERMISSION_DENIED;
442     }
443     int32_t systemAbilityId = -1;
444     bool ret = data.ReadInt32(systemAbilityId);
445     if (!ret) {
446         return ERR_NULL_OBJECT;
447     }
448     if (!CheckInputSysAbilityId(systemAbilityId)) {
449         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner read systemAbilityId failed!");
450         return ERR_NULL_OBJECT;
451     }
452 
453     if (!CheckAddOrRemovePermission(systemAbilityId)) {
454         HILOGE("AddSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
455         return ERR_PERMISSION_DENIED;
456     }
457 
458     auto object = data.ReadRemoteObject();
459     if (object == nullptr) {
460         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner readParcelable failed!");
461         return ERR_NULL_OBJECT;
462     }
463     SAExtraProp extraProp;
464     int32_t result = UnmarshalingSaExtraProp(data, extraProp);
465     if (result != ERR_OK) {
466         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner UnmarshalingSaExtraProp failed!");
467         return result;
468     }
469     result = AddSystemAbility(systemAbilityId, object, extraProp);
470     ret = reply.WriteInt32(result);
471     if (!ret) {
472         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner write reply failed.");
473         return ERR_FLATTEN_OBJECT;
474     }
475     return result;
476 }
477 
GetSystemAbilityInner(MessageParcel & data,MessageParcel & reply)478 int32_t SystemAbilityManagerStub::GetSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
479 {
480     int32_t systemAbilityId = -1;
481     bool ret = data.ReadInt32(systemAbilityId);
482     if (!ret) {
483         return ERR_NULL_OBJECT;
484     }
485     if (!CheckInputSysAbilityId(systemAbilityId)) {
486         HILOGW("SystemAbilityManagerStub::GetSystemAbilityInner read systemAbilityId failed!");
487         return ERR_NULL_OBJECT;
488     }
489 
490     if (!CheckGetSAPermission(systemAbilityId)) {
491         HILOGE("GetSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
492         return ERR_PERMISSION_DENIED;
493     }
494 
495     ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId));
496     if (!ret) {
497         HILOGW("GetSystemAbilityInner SA:%{public}d write reply failed.", systemAbilityId);
498         return ERR_FLATTEN_OBJECT;
499     }
500     return ERR_NONE;
501 }
502 
CheckSystemAbilityInner(MessageParcel & data,MessageParcel & reply)503 int32_t SystemAbilityManagerStub::CheckSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
504 {
505     int32_t systemAbilityId = -1;
506     bool ret = data.ReadInt32(systemAbilityId);
507     if (!ret) {
508         return ERR_NULL_OBJECT;
509     }
510     if (!CheckInputSysAbilityId(systemAbilityId)) {
511         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityInner read systemAbilityId failed!");
512         return ERR_NULL_OBJECT;
513     }
514 
515     if (!CheckGetSAPermission(systemAbilityId)) {
516         HILOGD("CheckSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
517         return ERR_PERMISSION_DENIED;
518     }
519 
520     ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId));
521     if (!ret) {
522         return ERR_FLATTEN_OBJECT;
523     }
524     return ERR_NONE;
525 }
526 
RemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)527 int32_t SystemAbilityManagerStub::RemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
528 {
529     if (!CanRequest()) {
530         HILOGE("RemoveSystemAbilityInner PERMISSION DENIED!");
531         return ERR_PERMISSION_DENIED;
532     }
533     int32_t systemAbilityId = -1;
534     bool ret = data.ReadInt32(systemAbilityId);
535     if (!ret) {
536         return ERR_NULL_OBJECT;
537     }
538     if (!CheckInputSysAbilityId(systemAbilityId)) {
539         HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner read systemAbilityId failed!");
540         return ERR_NULL_OBJECT;
541     }
542 
543     if (!CheckAddOrRemovePermission(systemAbilityId)) {
544         HILOGE("RemoveSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
545         return ERR_PERMISSION_DENIED;
546     }
547 
548     int32_t result = RemoveSystemAbility(systemAbilityId);
549     ret = reply.WriteInt32(result);
550     if (!ret) {
551         HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner write reply failed.");
552         return ERR_FLATTEN_OBJECT;
553     }
554     return result;
555 }
556 
557 
AddSystemProcessInner(MessageParcel & data,MessageParcel & reply)558 int32_t SystemAbilityManagerStub::AddSystemProcessInner(MessageParcel& data, MessageParcel& reply)
559 {
560     if (!CanRequest()) {
561         HILOGE("AddSystemProcessInner PERMISSION DENIED!");
562         return ERR_PERMISSION_DENIED;
563     }
564     std::u16string procName = data.ReadString16();
565     if (procName.empty()) {
566         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner read process name failed!");
567         return ERR_NULL_OBJECT;
568     }
569 
570     sptr<IRemoteObject> procObject = data.ReadRemoteObject();
571     if (procObject == nullptr) {
572         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner readParcelable failed!");
573         return ERR_NULL_OBJECT;
574     }
575 
576     int32_t result = AddSystemProcess(procName, procObject);
577     bool ret = reply.WriteInt32(result);
578     if (!ret) {
579         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner write reply failed.");
580         return ERR_FLATTEN_OBJECT;
581     }
582     return result;
583 }
584 
LoadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)585 int32_t SystemAbilityManagerStub::LoadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
586 {
587     int32_t systemAbilityId = -1;
588     bool ret = data.ReadInt32(systemAbilityId);
589     if (!ret) {
590         return ERR_INVALID_VALUE;
591     }
592     std::string loadSystemAbilityTag = ToString(systemAbilityId) + "_LoadSystemAbility";
593     HITRACE_METER_NAME(HITRACE_TAG_SAMGR, loadSystemAbilityTag);
594     if (!CheckInputSysAbilityId(systemAbilityId)) {
595         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read systemAbilityId failed!");
596         return ERR_INVALID_VALUE;
597     }
598 
599     if (!CheckGetSAPermission(systemAbilityId)) {
600         HILOGE("LoadSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
601         return ERR_PERMISSION_DENIED;
602     }
603 
604     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
605     if (remoteObject == nullptr) {
606         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read callback failed!");
607         return ERR_INVALID_VALUE;
608     }
609     sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
610     if (callback == nullptr) {
611         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner iface_cast failed!");
612         return ERR_INVALID_VALUE;
613     }
614     int32_t result = LoadSystemAbility(systemAbilityId, callback);
615     HILOGD("SystemAbilityManagerStub::LoadSystemAbilityInner result is %{public}d", result);
616     ret = reply.WriteInt32(result);
617     if (!ret) {
618         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner write reply failed.");
619         return ERR_FLATTEN_OBJECT;
620     }
621     return result;
622 }
623 
LoadRemoteSystemAbilityInner(MessageParcel & data,MessageParcel & reply)624 int32_t SystemAbilityManagerStub::LoadRemoteSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
625 {
626     int32_t systemAbilityId = -1;
627     bool ret = data.ReadInt32(systemAbilityId);
628     if (!ret) {
629         return ERR_INVALID_VALUE;
630     }
631     if (!CheckInputSysAbilityId(systemAbilityId)) {
632         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner systemAbilityId invalid");
633         return ERR_INVALID_VALUE;
634     }
635 
636     if (!CheckGetRemoteSAPermission(systemAbilityId)) {
637         HILOGE("LoadRemoteSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
638         return ERR_PERMISSION_DENIED;
639     }
640 
641     std::string deviceId = data.ReadString();
642     if (deviceId.empty()) {
643         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner read deviceId failed");
644         return ERR_INVALID_VALUE;
645     }
646     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
647     if (remoteObject == nullptr) {
648         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner read callback failed!");
649         return ERR_INVALID_VALUE;
650     }
651     sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
652     if (callback == nullptr) {
653         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner iface_cast failed!");
654         return ERR_INVALID_VALUE;
655     }
656     int32_t result = LoadSystemAbility(systemAbilityId, deviceId, callback);
657     HILOGD("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner result is %{public}d", result);
658     ret = reply.WriteInt32(result);
659     if (!ret) {
660         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner write reply failed.");
661         return ERR_FLATTEN_OBJECT;
662     }
663     return result;
664 }
665 
UnloadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)666 int32_t SystemAbilityManagerStub::UnloadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
667 {
668     int32_t systemAbilityId = -1;
669     bool ret = data.ReadInt32(systemAbilityId);
670     if (!ret) {
671         return ERR_INVALID_VALUE;
672     }
673     if (!CheckInputSysAbilityId(systemAbilityId)) {
674         HILOGW("SystemAbilityManagerStub::UnloadSystemAbilityInner systemAbilityId invalid");
675         return ERR_INVALID_VALUE;
676     }
677     int32_t result = UnloadSystemAbility(systemAbilityId);
678     HILOGD("SystemAbilityManagerStub::UnloadSystemAbilityInner result is %{public}d", result);
679     ret = reply.WriteInt32(result);
680     if (!ret) {
681         HILOGW("SystemAbilityManagerStub::UnloadSystemAbilityInner write reply failed.");
682         return ERR_FLATTEN_OBJECT;
683     }
684     return result;
685 }
686 
UnloadAllIdleSystemAbilityInner(MessageParcel & data,MessageParcel & reply)687 int32_t SystemAbilityManagerStub::UnloadAllIdleSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
688 {
689     int32_t result = UnloadAllIdleSystemAbility();
690     HILOGI("SystemAbilityManagerStub::UnloadAllIdleSystemAbilityInner result is %{public}d", result);
691     return result;
692 }
693 
GetSystemProcessInfoInner(MessageParcel & data,MessageParcel & reply)694 int32_t SystemAbilityManagerStub::GetSystemProcessInfoInner(MessageParcel& data, MessageParcel& reply)
695 {
696     HILOGI("GetSystemProcessInfoInner called");
697     if (!CanRequest()) {
698         HILOGE("GetSystemProcessInfoInner PERMISSION DENIED!");
699         return ERR_PERMISSION_DENIED;
700     }
701     int32_t systemAbilityId = -1;
702     bool ret = data.ReadInt32(systemAbilityId);
703     if (!ret) {
704         return ERR_NULL_OBJECT;
705     }
706     if (!CheckInputSysAbilityId(systemAbilityId)) {
707         HILOGW("SystemAbilityManagerStub::GetSystemProcessInfoInner read systemAbilityId failed!");
708         return ERR_NULL_OBJECT;
709     }
710     SystemProcessInfo processInfo;
711     int32_t result = GetSystemProcessInfo(systemAbilityId, processInfo);
712     ret = reply.WriteInt32(result);
713     if (!ret) {
714         HILOGW("GetSystemProcessInfoInner write reply failed.");
715         return ERR_FLATTEN_OBJECT;
716     }
717     if (result != ERR_OK) {
718         return ERR_OK;
719     }
720 
721     ret = reply.WriteString(processInfo.processName);
722     if (!ret) {
723         HILOGW("GetSystemProcessInfoInner write processName failed.");
724         return ERR_FLATTEN_OBJECT;
725     }
726     ret = reply.WriteInt32(processInfo.pid);
727     if (!ret) {
728         HILOGW("GetSystemProcessInfoInner write pid failed.");
729         return ERR_FLATTEN_OBJECT;
730     }
731     ret = reply.WriteInt32(processInfo.uid);
732     if (!ret) {
733         HILOGW("GetSystemProcessInfoInner write uid failed.");
734             return ERR_FLATTEN_OBJECT;
735     }
736     return ERR_OK;
737 }
738 
GetRunningSystemProcessInner(MessageParcel & data,MessageParcel & reply)739 int32_t SystemAbilityManagerStub::GetRunningSystemProcessInner(MessageParcel& data, MessageParcel& reply)
740 {
741     HILOGI("GetRunningSystemProcessInner called");
742     if (!CanRequest()) {
743         HILOGE("GetRunningSystemProcessInner PERMISSION DENIED!");
744         return ERR_PERMISSION_DENIED;
745     }
746     std::list<SystemProcessInfo> systemProcessInfos;
747     int32_t result = GetRunningSystemProcess(systemProcessInfos);
748     bool ret = reply.WriteInt32(result);
749     if (!ret) {
750         HILOGW("GetRunningSystemProcessInner write reply failed.");
751         return ERR_FLATTEN_OBJECT;
752     }
753     if (result != ERR_OK) {
754         return ERR_OK;
755     }
756 
757     size_t size = systemProcessInfos.size();
758     ret = reply.WriteInt32(size);
759     if (!ret) {
760         HILOGW("GetRunningSystemProcessInner write systemProcessInfos size failed.");
761         return ERR_FLATTEN_OBJECT;
762     }
763     for (auto& systemProcessInfo : systemProcessInfos) {
764         ret = reply.WriteString(systemProcessInfo.processName);
765         if (!ret) {
766             HILOGW("GetRunningSystemProcessInner write processName failed.");
767             return ERR_FLATTEN_OBJECT;
768         }
769         ret = reply.WriteInt32(systemProcessInfo.pid);
770         if (!ret) {
771             HILOGW("GetRunningSystemProcessInner write pid failed.");
772             return ERR_FLATTEN_OBJECT;
773         }
774         ret = reply.WriteInt32(systemProcessInfo.uid);
775         if (!ret) {
776             HILOGW("GetRunningSystemProcessInner write uid failed.");
777             return ERR_FLATTEN_OBJECT;
778         }
779     }
780     return ERR_OK;
781 }
782 
SubscribeSystemProcessInner(MessageParcel & data,MessageParcel & reply)783 int32_t SystemAbilityManagerStub::SubscribeSystemProcessInner(MessageParcel& data, MessageParcel& reply)
784 {
785     if (!CanRequest()) {
786         HILOGE("SubscribeSystemProcessInner PERMISSION DENIED!");
787         return ERR_PERMISSION_DENIED;
788     }
789     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
790     if (remoteObject == nullptr) {
791         HILOGW("SubscribeSystemProcessInner read listener failed!");
792         return ERR_NULL_OBJECT;
793     }
794     sptr<ISystemProcessStatusChange> listener = iface_cast<ISystemProcessStatusChange>(remoteObject);
795     if (listener == nullptr) {
796         HILOGW("SubscribeSystemProcessInner iface_cast failed!");
797         return ERR_NULL_OBJECT;
798     }
799     int32_t result = SubscribeSystemProcess(listener);
800     HILOGD("SubscribeSystemProcess result is %{public}d", result);
801     bool ret = reply.WriteInt32(result);
802     if (!ret) {
803         HILOGW("SubscribeSystemProcessInner write reply failed.");
804         return ERR_FLATTEN_OBJECT;
805     }
806     return result;
807 }
808 
UnSubscribeSystemProcessInner(MessageParcel & data,MessageParcel & reply)809 int32_t SystemAbilityManagerStub::UnSubscribeSystemProcessInner(MessageParcel& data, MessageParcel& reply)
810 {
811     if (!CanRequest()) {
812         HILOGE("UnSubscribeSystemProcessInner PERMISSION DENIED!");
813         return ERR_PERMISSION_DENIED;
814     }
815     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
816     if (remoteObject == nullptr) {
817         HILOGW("UnSubscribeSystemProcessInner read listener failed!");
818         return ERR_NULL_OBJECT;
819     }
820     sptr<ISystemProcessStatusChange> listener = iface_cast<ISystemProcessStatusChange>(remoteObject);
821     if (listener == nullptr) {
822         HILOGW("UnSubscribeSystemProcessInner iface_cast failed!");
823         return ERR_NULL_OBJECT;
824     }
825     int32_t result = UnSubscribeSystemProcess(listener);
826     HILOGD("UnSubscribeSystemProcessInner result is %{public}d", result);
827     bool ret = reply.WriteInt32(result);
828     if (!ret) {
829         HILOGW("UnSubscribeSystemProcessInner write reply failed.");
830         return ERR_FLATTEN_OBJECT;
831     }
832     return result;
833 }
834 
CancelUnloadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)835 int32_t SystemAbilityManagerStub::CancelUnloadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
836 {
837     int32_t systemAbilityId = -1;
838     bool ret = data.ReadInt32(systemAbilityId);
839     if (!ret) {
840         return ERR_INVALID_VALUE;
841     }
842     if (!CheckInputSysAbilityId(systemAbilityId)) {
843         HILOGW("SystemAbilityManagerStub::CancelUnloadSystemAbilityInner systemAbilityId invalid");
844         return ERR_INVALID_VALUE;
845     }
846     int32_t result = CancelUnloadSystemAbility(systemAbilityId);
847     HILOGD("SystemAbilityManagerStub::CancelUnloadSystemAbilityInner result is %{public}d", result);
848     ret = reply.WriteInt32(result);
849     if (!ret) {
850         HILOGW("SystemAbilityManagerStub::CancelUnloadSystemAbilityInner write reply failed.");
851         return ERR_FLATTEN_OBJECT;
852     }
853     return result;
854 }
855 
GetOnDemandReasonExtraDataInner(MessageParcel & data,MessageParcel & reply)856 int32_t SystemAbilityManagerStub::GetOnDemandReasonExtraDataInner(MessageParcel& data, MessageParcel& reply)
857 {
858     if (!CanRequest()) {
859         HILOGE("GetOnDemandReasonExtraData PERMISSION DENIED!");
860         return ERR_PERMISSION_DENIED;
861     }
862     int64_t extraDataId = -1;
863     if (!data.ReadInt64(extraDataId)) {
864         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData read extraDataId failed.");
865         return ERR_FLATTEN_OBJECT;
866     }
867     MessageParcel extraDataParcel;
868     int32_t result = GetOnDemandReasonExtraData(extraDataId, extraDataParcel);
869     HILOGD("SystemAbilityManagerStub::GetOnDemandReasonExtraData result is %{public}d", result);
870     if (!reply.WriteInt32(result)) {
871         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData write reply failed.");
872         return ERR_FLATTEN_OBJECT;
873     }
874     sptr<OnDemandReasonExtraData> extraData;
875     extraData = extraDataParcel.ReadParcelable<OnDemandReasonExtraData>();
876     if (extraData == nullptr) {
877         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData read extraData failed.");
878         return ERR_FLATTEN_OBJECT;
879     }
880     if (!reply.WriteParcelable(extraData)) {
881         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData write extraData failed.");
882         return ERR_FLATTEN_OBJECT;
883     }
884     return ERR_OK;
885 }
886 
GetOnDemandPolicyInner(MessageParcel & data,MessageParcel & reply)887 int32_t SystemAbilityManagerStub::GetOnDemandPolicyInner(MessageParcel& data, MessageParcel& reply)
888 {
889     if (!CanRequest()) {
890         HILOGE("GetOnDemandPolicyInner PERMISSION DENIED!");
891         return ERR_PERMISSION_DENIED;
892     }
893     int32_t systemAbilityId = -1;
894     if (!data.ReadInt32(systemAbilityId)) {
895         HILOGW("GetOnDemandPolicyInner read saId failed.");
896         return ERR_FLATTEN_OBJECT;
897     }
898     int32_t type = 0;
899     if (!data.ReadInt32(type)) {
900         HILOGW("GetOnDemandPolicyInner read type failed.");
901         return ERR_FLATTEN_OBJECT;
902     }
903     OnDemandPolicyType typeEnum = static_cast<OnDemandPolicyType>(type);
904     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
905     int32_t result = GetOnDemandPolicy(systemAbilityId, typeEnum, abilityOnDemandEvents);
906     if (!reply.WriteInt32(result)) {
907         HILOGW("GetOnDemandPolicyInner write result failed.");
908         return ERR_FLATTEN_OBJECT;
909     }
910     if (!OnDemandEventToParcel::WriteOnDemandEventsToParcel(abilityOnDemandEvents, reply)) {
911         HILOGW("GetOnDemandPolicyInner write on demand event failed.");
912         return ERR_FLATTEN_OBJECT;
913     }
914     return ERR_OK;
915 }
916 
UpdateOnDemandPolicyInner(MessageParcel & data,MessageParcel & reply)917 int32_t SystemAbilityManagerStub::UpdateOnDemandPolicyInner(MessageParcel& data, MessageParcel& reply)
918 {
919     if (!CanRequest()) {
920         HILOGE("UpdateOnDemandPolicyInner PERMISSION DENIED!");
921         return ERR_PERMISSION_DENIED;
922     }
923     int32_t systemAbilityId = -1;
924     if (!data.ReadInt32(systemAbilityId)) {
925         HILOGW("UpdateOnDemandPolicyInner read saId failed.");
926         return ERR_FLATTEN_OBJECT;
927     }
928     int32_t type = 0;
929     if (!data.ReadInt32(type)) {
930         HILOGW("GetOnDemandPolicyInner read type failed.");
931         return ERR_FLATTEN_OBJECT;
932     }
933     OnDemandPolicyType typeEnum = static_cast<OnDemandPolicyType>(type);
934     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
935     if (!OnDemandEventToParcel::ReadOnDemandEventsFromParcel(abilityOnDemandEvents, data)) {
936         HILOGW("UpdateOnDemandPolicyInner read on demand event failed.");
937         return ERR_FLATTEN_OBJECT;
938     }
939     int32_t result = UpdateOnDemandPolicy(systemAbilityId, typeEnum, abilityOnDemandEvents);
940     if (!reply.WriteInt32(result)) {
941         HILOGW("UpdateOnDemandPolicyInner write result failed.");
942         return ERR_FLATTEN_OBJECT;
943     }
944     return ERR_OK;
945 }
946 
SendStrategyInner(MessageParcel & data,MessageParcel & reply)947 int32_t SystemAbilityManagerStub::SendStrategyInner(MessageParcel& data, MessageParcel& reply)
948 {
949     if (!CanRequest()) {
950         HILOGE("SendStrategy PERMISSION DENIED!");
951         return ERR_PERMISSION_DENIED;
952     }
953     int32_t type = 0;
954     if (!data.ReadInt32(type)) {
955         HILOGW("SendStrategy read type failed.");
956         return ERR_FLATTEN_OBJECT;
957     }
958     std::vector<int32_t> systemAbilityIds;
959     if (!data.ReadInt32Vector(&systemAbilityIds)) {
960         HILOGW("SendStrategy read saId failed.");
961         return ERR_FLATTEN_OBJECT;
962     }
963     int32_t level = -1;
964     if (!data.ReadInt32(level)) {
965         HILOGW("SendStrategy read level failed.");
966         return ERR_FLATTEN_OBJECT;
967     }
968     std::string action;
969     if (!data.ReadString(action)) {
970         HILOGW("SendStrategy read action failed!");
971         return ERR_FLATTEN_OBJECT;
972     }
973     int32_t result = SendStrategy(type, systemAbilityIds, level, action);
974     if (!reply.WriteInt32(result)) {
975         HILOGW("SendStrategy write result failed.");
976         return ERR_FLATTEN_OBJECT;
977     }
978     return ERR_OK;
979 }
980 
GetOnDemandSystemAbilityIdsInner(MessageParcel & data,MessageParcel & reply)981 int32_t SystemAbilityManagerStub::GetOnDemandSystemAbilityIdsInner(MessageParcel& data, MessageParcel& reply)
982 {
983     std::vector<int32_t> systemAbilityIds;
984     int32_t result = GetOnDemandSystemAbilityIds(systemAbilityIds);
985     if (!reply.WriteInt32(result)) {
986         HILOGE("GetOnDemandSystemAbilityIdsInner write result failed.");
987         return ERR_FLATTEN_OBJECT;
988     }
989     if (!reply.WriteInt32Vector(systemAbilityIds)) {
990         HILOGE("GetOnDemandSystemAbilityIdsInner write result failed.");
991         return ERR_FLATTEN_OBJECT;
992     }
993     return ERR_OK;
994 }
995 
CanRequest()996 bool SystemAbilityManagerStub::CanRequest()
997 {
998     auto accessTokenId = IPCSkeleton::GetCallingTokenID();
999     AccessToken::ATokenTypeEnum tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(accessTokenId);
1000     HILOGD("SystemAbilityManagerStub::CanRequest tokenId:%{private}u, tokenType:%{public}d",
1001         accessTokenId, tokenType);
1002     return (tokenType == AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
1003 }
1004 } // namespace OHOS
1005