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 #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, "appRecord's priorityObject is null");
127     } else {
128         eventInfo.pid = appRecord->GetPriorityObject()->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, "appRecord is null");
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 abilityinfo = appRecord->GetAbilities().begin()->second->GetAbilityInfo();
150         UpdateStartupType(abilityinfo, eventInfo.abilityType, eventInfo.extensionType);
151     } else {
152         TAG_LOGI(AAFwkTag::APPMGR, "Abilities nullptr!");
153     }
154     if (!callerAppRecord) {
155         Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
156         auto token = appRecord->GetCallerTokenId() == -1 ?
157             static_cast<int32_t>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
158         Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
159         eventInfo.callerBundleName = "";
160         eventInfo.callerProcessName = nativeTokenInfo.processName;
161         eventInfo.callerPid = IPCSkeleton::GetCallingPid();
162     } else {
163         if (callerAppRecord->GetBundleName().empty()) {
164             eventInfo.callerBundleName = callerAppRecord->GetName();
165         } else {
166             eventInfo.callerBundleName = callerAppRecord->GetBundleName();
167         }
168         eventInfo.callerProcessName = callerAppRecord->GetProcessName();
169         eventInfo.callerPid = GetCallerPid(callerAppRecord);
170     }
171     if (!appRecord->GetBundleName().empty()) {
172         eventInfo.bundleName = appRecord->GetBundleName();
173     }
174     eventInfo.processName = appRecord->GetProcessName();
175     eventInfo.processType = static_cast<int32_t>(appRecord->GetProcessType());
176     if (!appRecord->GetPriorityObject()) {
177         TAG_LOGE(AAFwkTag::APPMGR, "appRecord's priorityObject is null");
178     } else {
179         eventInfo.pid = appRecord->GetPriorityObject()->GetPid();
180     }
181     AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
182     return true;
183 }
184 
SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,ProcessStartFailedReason reason,int32_t subReason)185 bool AppMgrEventUtil::SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,
186     ProcessStartFailedReason reason, int32_t subReason)
187 {
188     if (!childRecord) {
189         TAG_LOGW(AAFwkTag::APPMGR, "appRecord is null");
190         return false;
191     }
192     auto hostRecord = childRecord->GetHostRecord();
193     if (!hostRecord) {
194         TAG_LOGW(AAFwkTag::APPMGR, "hostRecord is null");
195         return false;
196     }
197     AAFwk::EventInfo eventInfo;
198     time_t currentTime = time(nullptr);
199     if (currentTime <= 0) {
200         TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
201         return false;
202     }
203     eventInfo.time = currentTime;
204     eventInfo.callerUid = hostRecord->GetUid();
205     eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPriorityObject()->GetPid() : -1;
206     eventInfo.callerBundleName = hostRecord->GetBundleName();
207     eventInfo.callerProcessName = hostRecord->GetProcessName();
208     eventInfo.bundleName = hostRecord->GetBundleName();
209     eventInfo.processName = childRecord->GetProcessName();
210     eventInfo.processType = static_cast<int32_t>(childRecord->GetProcessType());
211     eventInfo.reason = static_cast<int32_t>(reason);
212     eventInfo.subReason = subReason;
213     AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
214     return true;
215 }
216 
SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,ProcessStartFailedReason reason,int32_t subReason)217 bool AppMgrEventUtil::SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,
218     ProcessStartFailedReason reason, int32_t subReason)
219 {
220     if (!renderRecord) {
221         TAG_LOGW(AAFwkTag::APPMGR, "appRecord is null");
222         return false;
223     }
224     auto hostRecord = renderRecord->GetHostRecord();
225     if (!hostRecord) {
226         TAG_LOGW(AAFwkTag::APPMGR, "hostRecord is null");
227         return false;
228     }
229     AAFwk::EventInfo eventInfo;
230     time_t currentTime = time(nullptr);
231     if (currentTime <= 0) {
232         TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
233         return false;
234     }
235     eventInfo.time = currentTime;
236     eventInfo.callerUid = hostRecord->GetUid();
237     eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPriorityObject()->GetPid() : -1;
238     eventInfo.callerBundleName = hostRecord->GetBundleName();
239     eventInfo.callerProcessName = hostRecord->GetProcessName();
240     eventInfo.bundleName = hostRecord->GetBundleName();
241     eventInfo.processName = renderRecord->GetProcessName();
242     eventInfo.processType = static_cast<int32_t>(renderRecord->GetProcessType());
243     eventInfo.reason = static_cast<int32_t>(reason);
244     eventInfo.subReason = subReason;
245     AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
246     return true;
247 }
248 
SendReStartProcessEvent(AAFwk::EventInfo & eventInfo,int32_t appUid,int64_t restartTime)249 void AppMgrEventUtil::SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid, int64_t restartTime)
250 {
251     // eventInfo come from SendProcessStartEvent
252     eventInfo.time = restartTime;
253     eventInfo.appUid = appUid;
254     AAFwk::EventReport::SendKeyEvent(AAFwk::EventName::RESTART_PROCESS_BY_SAME_APP,
255         HiSysEventType::BEHAVIOR, eventInfo);
256 }
257 }  // namespace AppExecFwk
258 }  // namespace OHOS
259