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