• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, "null callerAppRecord");
29         return IPCSkeleton::GetCallingPid();
30     }
31 
32     if (!callerAppRecord->GetPriorityObject()) {
33         TAG_LOGW(AAFwkTag::APPMGR, "null priorityObject");
34         return IPCSkeleton::GetCallingPid();
35     }
36 
37     return callerAppRecord->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, "null appRecord");
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 failed");
99     }
100 #define START_UP_ABILITY_TYPE_PREMAKE 100
101     if (eventInfo.isPreload) {
102         eventInfo.abilityType = START_UP_ABILITY_TYPE_PREMAKE + eventInfo.preloadMode;
103     }
104     if (!callerAppRecord) {
105         Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
106         auto token = appRecord->GetCallerTokenId() == -1 ?
107             static_cast<int>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
108         Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
109         eventInfo.callerBundleName = "";
110         eventInfo.callerProcessName = nativeTokenInfo.processName;
111         eventInfo.callerPid = IPCSkeleton::GetCallingPid();
112     } else {
113         if (callerAppRecord->GetBundleName().empty()) {
114             eventInfo.callerBundleName = callerAppRecord->GetName();
115         } else {
116             eventInfo.callerBundleName = callerAppRecord->GetBundleName();
117         }
118         eventInfo.callerProcessName = callerAppRecord->GetProcessName();
119         eventInfo.callerPid = GetCallerPid(callerAppRecord);
120     }
121     if (!appRecord->GetBundleName().empty()) {
122         eventInfo.bundleName = appRecord->GetBundleName();
123     }
124     eventInfo.processName = appRecord->GetProcessName();
125     if (!appRecord->GetPriorityObject()) {
126         TAG_LOGE(AAFwkTag::APPMGR, "null priorityObject");
127     } else {
128         eventInfo.pid = appRecord->GetPid();
129     }
130     AAFwk::EventReport::SendProcessStartEvent(AAFwk::EventName::PROCESS_START, eventInfo);
131     return true;
132 }
133 
SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> callerAppRecord,std::shared_ptr<AppRunningRecord> appRecord,AAFwk::EventInfo & eventInfo)134 bool AppMgrEventUtil::SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> callerAppRecord,
135     std::shared_ptr<AppRunningRecord> appRecord, AAFwk::EventInfo &eventInfo)
136 {
137     if (!appRecord) {
138         TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
139         return false;
140     }
141     time_t currentTime = time(nullptr);
142     if (currentTime <= 0) {
143         TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
144         return false;
145     }
146     eventInfo.time = currentTime;
147     eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
148     if (!appRecord->GetAbilities().empty()) {
149         auto abilityRecord = appRecord->GetAbilities().begin()->second;
150         if (!abilityRecord) {
151             TAG_LOGE(AAFwkTag::APPMGR, "abilityRecord null");
152             return false;
153         }
154         auto abilityinfo = abilityRecord->GetAbilityInfo();
155         UpdateStartupType(abilityinfo, eventInfo.abilityType, eventInfo.extensionType);
156     } else {
157         TAG_LOGI(AAFwkTag::APPMGR, "abilities failed");
158     }
159     UpdateCallerInfo(eventInfo, callerAppRecord, appRecord);
160     if (!appRecord->GetBundleName().empty()) {
161         eventInfo.bundleName = appRecord->GetBundleName();
162     }
163     eventInfo.processName = appRecord->GetProcessName();
164     eventInfo.processType = static_cast<int32_t>(appRecord->GetProcessType());
165     if (!appRecord->GetPriorityObject()) {
166         TAG_LOGE(AAFwkTag::APPMGR, "null priorityObject");
167     } else {
168         eventInfo.pid = appRecord->GetPid();
169     }
170     AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
171     return true;
172 }
173 
UpdateCallerInfo(AAFwk::EventInfo & eventInfo,std::shared_ptr<AppRunningRecord> callerAppRecord,std::shared_ptr<AppRunningRecord> appRecord)174 void AppMgrEventUtil::UpdateCallerInfo(AAFwk::EventInfo &eventInfo, std::shared_ptr<AppRunningRecord> callerAppRecord,
175     std::shared_ptr<AppRunningRecord> appRecord)
176 {
177     if (!callerAppRecord) {
178         Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
179         auto token = appRecord->GetCallerTokenId() == -1 ?
180             static_cast<int32_t>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
181         Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
182         eventInfo.callerBundleName = "";
183         eventInfo.callerProcessName = nativeTokenInfo.processName;
184         eventInfo.callerPid = IPCSkeleton::GetCallingPid();
185     } else {
186         if (callerAppRecord->GetBundleName().empty()) {
187             eventInfo.callerBundleName = callerAppRecord->GetName();
188         } else {
189             eventInfo.callerBundleName = callerAppRecord->GetBundleName();
190         }
191         eventInfo.callerProcessName = callerAppRecord->GetProcessName();
192         eventInfo.callerPid = GetCallerPid(callerAppRecord);
193     }
194 }
195 
196 #ifdef SUPPORT_CHILD_PROCESS
SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,ProcessStartFailedReason reason,int32_t subReason)197 bool AppMgrEventUtil::SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,
198     ProcessStartFailedReason reason, int32_t subReason)
199 {
200     if (!childRecord) {
201         TAG_LOGW(AAFwkTag::APPMGR, "null appRecord");
202         return false;
203     }
204     auto hostRecord = childRecord->GetHostRecord();
205     if (!hostRecord) {
206         TAG_LOGW(AAFwkTag::APPMGR, "null hostRecord");
207         return false;
208     }
209     AAFwk::EventInfo eventInfo;
210     time_t currentTime = time(nullptr);
211     if (currentTime <= 0) {
212         TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
213         return false;
214     }
215     eventInfo.time = currentTime;
216     eventInfo.callerUid = hostRecord->GetUid();
217     eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPid() : -1;
218     eventInfo.callerBundleName = hostRecord->GetBundleName();
219     eventInfo.callerProcessName = hostRecord->GetProcessName();
220     eventInfo.bundleName = hostRecord->GetBundleName();
221     eventInfo.processName = childRecord->GetProcessName();
222     eventInfo.processType = static_cast<int32_t>(childRecord->GetProcessType());
223     eventInfo.reason = static_cast<int32_t>(reason);
224     eventInfo.subReason = subReason;
225     AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
226     return true;
227 }
228 #endif  // SUPPORT_CHILD_PROCESS
229 
SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,ProcessStartFailedReason reason,int32_t subReason)230 bool AppMgrEventUtil::SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,
231     ProcessStartFailedReason reason, int32_t subReason)
232 {
233     if (!renderRecord) {
234         TAG_LOGW(AAFwkTag::APPMGR, "null appRecord");
235         return false;
236     }
237     auto hostRecord = renderRecord->GetHostRecord();
238     if (!hostRecord) {
239         TAG_LOGW(AAFwkTag::APPMGR, "null hostRecord");
240         return false;
241     }
242     AAFwk::EventInfo eventInfo;
243     time_t currentTime = time(nullptr);
244     if (currentTime <= 0) {
245         TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
246         return false;
247     }
248     eventInfo.time = currentTime;
249     eventInfo.callerUid = hostRecord->GetUid();
250     eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPid() : -1;
251     eventInfo.callerBundleName = hostRecord->GetBundleName();
252     eventInfo.callerProcessName = hostRecord->GetProcessName();
253     eventInfo.bundleName = hostRecord->GetBundleName();
254     eventInfo.processName = renderRecord->GetProcessName();
255     eventInfo.processType = static_cast<int32_t>(renderRecord->GetProcessType());
256     eventInfo.reason = static_cast<int32_t>(reason);
257     eventInfo.subReason = subReason;
258     AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
259     return true;
260 }
261 
SendReStartProcessEvent(AAFwk::EventInfo & eventInfo,int32_t appUid,int64_t restartTime)262 void AppMgrEventUtil::SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid, int64_t restartTime)
263 {
264     // eventInfo come from SendProcessStartEvent
265     eventInfo.time = restartTime;
266     eventInfo.appUid = appUid;
267     AAFwk::EventReport::SendKeyEvent(AAFwk::EventName::RESTART_PROCESS_BY_SAME_APP,
268         HiSysEventType::BEHAVIOR, eventInfo);
269 }
270 }  // namespace AppExecFwk
271 }  // namespace OHOS
272