1 /*
2 * Copyright (C) 2022 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 "ipc_skeleton.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "bundle_mgr_interface.h"
22
23 namespace OHOS {
24 #ifdef CONFIG_IPC_SINGLE
25 using namespace IPC_SINGLE;
26 #endif
27 namespace {
28 constexpr int32_t DEFAULT_UID = 100;
29 }
DebugGetUidByBundleName(const std::string & bundleName,const int32_t userId)30 int32_t DebugGetUidByBundleName(const std::string &bundleName, const int32_t userId)
31 {
32 sptr<ISystemAbilityManager> systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33 if (systemMgr == nullptr) {
34 return -1;
35 }
36
37 sptr<IRemoteObject> remoteObject = systemMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
38 if (remoteObject == nullptr) {
39 return -1;
40 }
41
42 sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgrProxy = iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
43 if (bundleMgrProxy == nullptr) {
44 return -1;
45 }
46
47 return bundleMgrProxy->GetUidByBundleName(bundleName, userId);
48 }
49
GetCallingPid()50 pid_t IPCSkeleton::GetCallingPid()
51 {
52 return 1;
53 }
54
GetCallingUid()55 pid_t IPCSkeleton::GetCallingUid()
56 {
57 int32_t uid = DebugGetUidByBundleName("com.ohos.launcher", DEFAULT_UID);
58 if (uid == -1) {
59 uid = DebugGetUidByBundleName("com.ohos.sceneboard", DEFAULT_UID);
60 }
61 return uid;
62 }
63
ResetCallingIdentity()64 std::string IPCSkeleton::ResetCallingIdentity()
65 {
66 return "";
67 }
68
SetCallingIdentity(std::string & identity,bool flag)69 bool IPCSkeleton::SetCallingIdentity(std::string &identity, bool flag)
70 {
71 return true;
72 }
73 } // namespace OHOS
74