1 /*
2 * Copyright (c) 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 "app_mgr_event.h"
17
18 #include "accesstoken_kit.h"
19 #include "hilog_tag_wrapper.h"
20 #include "ipc_skeleton.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24
GetCallerPid(const std::shared_ptr<AppRunningRecord> & callerAppRecord)25 int32_t AppMgrEventUtil::GetCallerPid(const std::shared_ptr<AppRunningRecord> &callerAppRecord)
26 {
27 if (!callerAppRecord) {
28 TAG_LOGW(AAFwkTag::APPMGR, "callerAppRecord is null");
29 return IPCSkeleton::GetCallingPid();
30 }
31
32 if (!callerAppRecord->GetPriorityObject()) {
33 TAG_LOGW(AAFwkTag::APPMGR, "callerAppRecord's priorityObject is null");
34 return IPCSkeleton::GetCallingPid();
35 }
36
37 return callerAppRecord->GetPriorityObject()->GetPid();
38 }
39
UpdateStartupType(const std::shared_ptr<AbilityInfo> & abilityInfo,int32_t & abilityType,int32_t & extensionType)40 void AppMgrEventUtil::UpdateStartupType(const std::shared_ptr<AbilityInfo> &abilityInfo, int32_t &abilityType,
41 int32_t &extensionType)
42 {
43 if (abilityInfo == nullptr) {
44 return;
45 }
46 TAG_LOGD(AAFwkTag::APPMGR, "bundleName:%{public}s, abilityName:%{public}s", abilityInfo->bundleName.c_str(),
47 abilityInfo->name.c_str());
48 abilityType = static_cast<int32_t>(abilityInfo->type);
49 if (abilityInfo->type != AbilityType::EXTENSION) {
50 return;
51 }
52 extensionType = static_cast<int32_t>(abilityInfo->extensionAbilityType);
53 }
54
SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> & callerAppRecord,const std::shared_ptr<AppRunningRecord> & appRecord,const std::string & moduleName,const std::string & abilityName)55 bool AppMgrEventUtil::SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> &callerAppRecord,
56 const std::shared_ptr<AppRunningRecord> &appRecord, const std::string &moduleName, const std::string &abilityName)
57 {
58 if (!appRecord) {
59 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
60 return false;
61 }
62 AAFwk::EventInfo eventInfo;
63 eventInfo.abilityName = abilityName;
64 eventInfo.moduleName = moduleName;
65 eventInfo.bundleName = appRecord->GetBundleName();
66
67 eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
68 if (callerAppRecord == nullptr) {
69 Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
70 auto token = appRecord->GetCallerTokenId() == -1 ?
71 static_cast<int>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
72 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
73 eventInfo.callerBundleName = "";
74 eventInfo.callerProcessName = nativeTokenInfo.processName;
75 } else {
76 eventInfo.callerBundleName = callerAppRecord->GetName();
77 eventInfo.callerProcessName = callerAppRecord->GetProcessName();
78 }
79 auto eventName = AAFwk::EventName::CREATE_ATOMIC_SERVICE_PROCESS;
80 AAFwk::EventReport::SendAtomicServiceEvent(eventName, HiSysEventType::BEHAVIOR, eventInfo);
81 return true;
82 }
83
SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> & callerAppRecord,const std::shared_ptr<AppRunningRecord> & appRecord,AAFwk::EventInfo & eventInfo)84 bool AppMgrEventUtil::SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> &callerAppRecord,
85 const std::shared_ptr<AppRunningRecord> &appRecord, AAFwk::EventInfo &eventInfo)
86 {
87 if (!appRecord) {
88 TAG_LOGW(AAFwkTag::APPMGR, "appRecord is null");
89 return false;
90 }
91 time_t currentTime = time(nullptr);
92 eventInfo.time = currentTime;
93 eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
94 if (!appRecord->GetAbilities().empty()) {
95 auto abilityinfo = appRecord->GetAbilities().begin()->second->GetAbilityInfo();
96 UpdateStartupType(abilityinfo, eventInfo.abilityType, eventInfo.extensionType);
97 } else {
98 TAG_LOGI(AAFwkTag::APPMGR, "Abilities nullptr!");
99 }
100 if (!callerAppRecord) {
101 Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
102 auto token = appRecord->GetCallerTokenId() == -1 ?
103 static_cast<int>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
104 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
105 eventInfo.callerBundleName = "";
106 eventInfo.callerProcessName = nativeTokenInfo.processName;
107 eventInfo.callerPid = IPCSkeleton::GetCallingPid();
108 } else {
109 if (callerAppRecord->GetBundleName().empty()) {
110 eventInfo.callerBundleName = callerAppRecord->GetName();
111 } else {
112 eventInfo.callerBundleName = callerAppRecord->GetBundleName();
113 }
114 eventInfo.callerProcessName = callerAppRecord->GetProcessName();
115 eventInfo.callerPid = GetCallerPid(callerAppRecord);
116 }
117 if (!appRecord->GetBundleName().empty()) {
118 eventInfo.bundleName = appRecord->GetBundleName();
119 }
120 eventInfo.processName = appRecord->GetProcessName();
121 if (!appRecord->GetPriorityObject()) {
122 TAG_LOGE(AAFwkTag::APPMGR, "appRecord's priorityObject is null");
123 } else {
124 eventInfo.pid = appRecord->GetPriorityObject()->GetPid();
125 }
126 AAFwk::EventReport::SendProcessStartEvent(AAFwk::EventName::PROCESS_START, eventInfo);
127 return true;
128 }
129
SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> callerAppRecord,std::shared_ptr<AppRunningRecord> appRecord,AAFwk::EventInfo & eventInfo)130 bool AppMgrEventUtil::SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> callerAppRecord,
131 std::shared_ptr<AppRunningRecord> appRecord, AAFwk::EventInfo &eventInfo)
132 {
133 if (!appRecord) {
134 TAG_LOGE(AAFwkTag::APPMGR, "appRecord is null");
135 return false;
136 }
137 time_t currentTime = time(nullptr);
138 if (currentTime <= 0) {
139 TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
140 return false;
141 }
142 eventInfo.time = currentTime;
143 eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
144 if (!appRecord->GetAbilities().empty()) {
145 auto abilityinfo = appRecord->GetAbilities().begin()->second->GetAbilityInfo();
146 UpdateStartupType(abilityinfo, eventInfo.abilityType, eventInfo.extensionType);
147 } else {
148 TAG_LOGI(AAFwkTag::APPMGR, "Abilities nullptr!");
149 }
150 if (!callerAppRecord) {
151 Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
152 auto token = appRecord->GetCallerTokenId() == -1 ?
153 static_cast<int32_t>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
154 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
155 eventInfo.callerBundleName = "";
156 eventInfo.callerProcessName = nativeTokenInfo.processName;
157 eventInfo.callerPid = IPCSkeleton::GetCallingPid();
158 } else {
159 if (callerAppRecord->GetBundleName().empty()) {
160 eventInfo.callerBundleName = callerAppRecord->GetName();
161 } else {
162 eventInfo.callerBundleName = callerAppRecord->GetBundleName();
163 }
164 eventInfo.callerProcessName = callerAppRecord->GetProcessName();
165 eventInfo.callerPid = GetCallerPid(callerAppRecord);
166 }
167 if (!appRecord->GetBundleName().empty()) {
168 eventInfo.bundleName = appRecord->GetBundleName();
169 }
170 eventInfo.processName = appRecord->GetProcessName();
171 eventInfo.processType = static_cast<int32_t>(appRecord->GetProcessType());
172 if (!appRecord->GetPriorityObject()) {
173 TAG_LOGE(AAFwkTag::APPMGR, "appRecord's priorityObject is null");
174 } else {
175 eventInfo.pid = appRecord->GetPriorityObject()->GetPid();
176 }
177 AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
178 return true;
179 }
180
SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,ProcessStartFailedReason reason,int32_t subReason)181 bool AppMgrEventUtil::SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,
182 ProcessStartFailedReason reason, int32_t subReason)
183 {
184 if (!childRecord) {
185 TAG_LOGW(AAFwkTag::APPMGR, "appRecord is null");
186 return false;
187 }
188 auto hostRecord = childRecord->GetHostRecord();
189 if (!hostRecord) {
190 TAG_LOGW(AAFwkTag::APPMGR, "hostRecord is null");
191 return false;
192 }
193 AAFwk::EventInfo eventInfo;
194 time_t currentTime = time(nullptr);
195 if (currentTime <= 0) {
196 TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
197 return false;
198 }
199 eventInfo.time = currentTime;
200 eventInfo.callerUid = hostRecord->GetUid();
201 eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPriorityObject()->GetPid() : -1;
202 eventInfo.callerBundleName = hostRecord->GetBundleName();
203 eventInfo.callerProcessName = hostRecord->GetProcessName();
204 eventInfo.bundleName = hostRecord->GetBundleName();
205 eventInfo.processName = childRecord->GetProcessName();
206 eventInfo.processType = static_cast<int32_t>(childRecord->GetProcessType());
207 eventInfo.reason = static_cast<int32_t>(reason);
208 eventInfo.subReason = subReason;
209 AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
210 return true;
211 }
212
SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,ProcessStartFailedReason reason,int32_t subReason)213 bool AppMgrEventUtil::SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,
214 ProcessStartFailedReason reason, int32_t subReason)
215 {
216 if (!renderRecord) {
217 TAG_LOGW(AAFwkTag::APPMGR, "appRecord is null");
218 return false;
219 }
220 auto hostRecord = renderRecord->GetHostRecord();
221 if (!hostRecord) {
222 TAG_LOGW(AAFwkTag::APPMGR, "hostRecord is null");
223 return false;
224 }
225 AAFwk::EventInfo eventInfo;
226 time_t currentTime = time(nullptr);
227 if (currentTime <= 0) {
228 TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
229 return false;
230 }
231 eventInfo.time = currentTime;
232 eventInfo.callerUid = hostRecord->GetUid();
233 eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPriorityObject()->GetPid() : -1;
234 eventInfo.callerBundleName = hostRecord->GetBundleName();
235 eventInfo.callerProcessName = hostRecord->GetProcessName();
236 eventInfo.bundleName = hostRecord->GetBundleName();
237 eventInfo.processName = renderRecord->GetProcessName();
238 eventInfo.processType = static_cast<int32_t>(renderRecord->GetProcessType());
239 eventInfo.reason = static_cast<int32_t>(reason);
240 eventInfo.subReason = subReason;
241 AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
242 return true;
243 }
244
SendReStartProcessEvent(AAFwk::EventInfo & eventInfo,int32_t appUid,int64_t restartTime)245 void AppMgrEventUtil::SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid, int64_t restartTime)
246 {
247 // eventInfo come from SendProcessStartEvent
248 eventInfo.time = restartTime;
249 eventInfo.appUid = appUid;
250 AAFwk::EventReport::SendKeyEvent(AAFwk::EventName::RESTART_PROCESS_BY_SAME_APP,
251 HiSysEventType::BEHAVIOR, eventInfo);
252 }
253 } // namespace AppExecFwk
254 } // namespace OHOS
255