• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ipc_utilities.h"
17 
18 #include <atomic>
19 
20 #include "debug_logger.h"
21 #include "hiperf_hilog.h"
22 #include "utilities.h"
23 #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE)
24 #include "application_info.h"
25 #include "bundle_mgr_proxy.h"
26 #endif
27 #if defined(is_ohos) && is_ohos
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 #endif
31 
32 namespace OHOS::Developtools::HiPerf {
33 
34 static std::atomic<bool> g_haveIpc = false;
35 
IsDebugableApp(const std::string & bundleName)36 bool IsDebugableApp(const std::string& bundleName)
37 {
38 #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE)
39     g_haveIpc.store(true);
40     std::string err = "";
41     do {
42         if (bundleName.empty()) {
43             err = "bundleName is empty!";
44             break;
45         }
46 
47         sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48         if (sam == nullptr) {
49             err = "GetSystemAbilityManager failed!";
50             break;
51         }
52 
53         sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
54         if (remoteObject == nullptr) {
55             err = "Get BundleMgr SA failed!";
56             break;
57         }
58 
59         sptr<AppExecFwk::BundleMgrProxy> proxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
60         if (proxy == nullptr) {
61             err = "iface_cast failed!";
62             break;
63         }
64 
65         AppExecFwk::ApplicationInfo appInfo;
66         bool ret = proxy->GetApplicationInfo(bundleName, AppExecFwk::GET_APPLICATION_INFO_WITH_DISABLE,
67                                              AppExecFwk::Constants::ANY_USERID, appInfo);
68         if (!ret) {
69             err = "GetApplicationInfo failed!";
70             break;
71         }
72 
73         if (appInfo.appProvisionType != AppExecFwk::Constants::APP_PROVISION_TYPE_DEBUG) {
74             err = "appProvisionType is " + appInfo.appProvisionType;
75             break;
76         }
77         HIPERF_HILOGI(MODULE_DEFAULT, "bundleName is %{public}s,appProvisionType: %{public}s",
78                       bundleName.c_str(), appInfo.appProvisionType.c_str());
79         return true;
80     } while (0);
81 
82     HIPERF_HILOGE(MODULE_DEFAULT, "IsDebugableApp error, bundleName: [%{public}s] err: [%{public}s]",
83                   bundleName.c_str(), err.c_str());
84     return false;
85 #else
86     return false;
87 #endif
88 }
89 
IsApplicationEncryped(const int pid)90 bool IsApplicationEncryped(const int pid)
91 {
92 #if defined(is_ohos) && is_ohos && defined(BUNDLE_FRAMEWORK_ENABLE)
93     g_haveIpc.store(true);
94     CHECK_TRUE(pid <= 0, false, LOG_TYPE_PRINTF, "Invalid -p value '%d', the pid should be larger than 0\n", pid);
95     std::string bundleName = GetProcessName(pid);
96     CHECK_TRUE(bundleName.empty(), false, 1, "bundleName is empty,pid is %d", pid);
97     auto pos = bundleName.find(":");
98     if (pos != std::string::npos) {
99         bundleName = bundleName.substr(0, pos);
100     }
101     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102     CHECK_TRUE(sam == nullptr, false, LOG_TYPE_PRINTF, "GetSystemAbilityManager failed!\n");
103     sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
104     CHECK_TRUE(remoteObject == nullptr, false, LOG_TYPE_PRINTF, "Get BundleMgr SA failed!\n");
105     sptr<AppExecFwk::BundleMgrProxy> proxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
106     CHECK_TRUE(proxy == nullptr, false, LOG_TYPE_PRINTF, "iface_cast failed!\n");
107 
108     AppExecFwk::ApplicationInfo appInfo;
109     bool ret = proxy->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO,
110                                          AppExecFwk::Constants::ANY_USERID, appInfo);
111     CHECK_TRUE(!ret, false, 1, "%s:%s GetApplicationInfo failed!", __func__, bundleName.c_str());
112     bool isEncrypted = (appInfo.applicationReservedFlag &
113                         static_cast<uint32_t>(AppExecFwk::ApplicationReservedFlag::ENCRYPTED_APPLICATION)) != 0;
114     HLOGD("check application encryped.%d : %s, pid:%d", isEncrypted, bundleName.c_str(), pid);
115     return isEncrypted;
116 #else
117     return false;
118 #endif
119 }
120 
CheckIpcBeforeFork()121 void CheckIpcBeforeFork()
122 {
123     if (g_haveIpc.load()) {
124         HIPERF_HILOGW(MODULE_DEFAULT, "fork after ipc!");
125     }
126 }
127 
128 } // namespace OHOS::Developtools::HiPerf
129