1 /* 2 * Copyright (c) 2021-2023 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 #ifndef SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H 17 #define SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H 18 19 #include <map> 20 #include <set> 21 #include <string> 22 #include <utility> 23 24 #include "dbinder_service.h" 25 #include "dbinder_service_stub.h" 26 #include "device_status_collect_manager.h" 27 #include "dynamic_cache.h" 28 #include "ffrt_handler.h" 29 #include "rpc_callback_imp.h" 30 #include "thread_pool.h" 31 #include "timer.h" 32 #include "sa_profiles.h" 33 #include "system_ability_manager_stub.h" 34 #include "schedule/system_ability_state_scheduler.h" 35 36 namespace OHOS { 37 struct SAInfo { 38 sptr<IRemoteObject> remoteObj; 39 bool isDistributed = false; 40 std::u16string capability; 41 std::string permission; 42 }; 43 44 enum { 45 UUID = 0, 46 NODE_ID, 47 UNKNOWN, 48 }; 49 50 enum ListenerState { 51 INIT = 0, 52 NOTIFIED, 53 }; 54 55 struct SAListener { 56 sptr<ISystemAbilityStatusChange> listener; 57 int32_t callingPid; 58 ListenerState state = ListenerState::INIT; 59 SAListener(sptr<ISystemAbilityStatusChange> lst, int32_t cpid, ListenerState sta = ListenerState::INIT) listenerSAListener60 :listener(lst), callingPid(cpid), state(sta) {} 61 }; 62 63 class SystemAbilityManager : public DynamicCache, public SystemAbilityManagerStub { 64 public: ~SystemAbilityManager()65 virtual ~SystemAbilityManager() 66 { 67 if (reportEventTimer_ != nullptr) { 68 reportEventTimer_->Shutdown(); 69 } 70 } GetInstance()71 static sptr<SystemAbilityManager> GetInstance() 72 { 73 std::lock_guard<samgr::mutex> autoLock(instanceLock); 74 if (instance == nullptr) { 75 instance = new SystemAbilityManager; 76 } 77 return instance; 78 } 79 80 int32_t RemoveSystemAbility(const sptr<IRemoteObject>& ability); 81 std::vector<std::u16string> ListSystemAbilities(uint32_t dumpFlags) override; 82 SetDeviceName(const std::u16string & name)83 void SetDeviceName(const std::u16string &name) 84 { 85 deviceName_ = name; 86 } 87 GetDeviceName()88 const std::u16string& GetDeviceName() const 89 { 90 return deviceName_; 91 } 92 GetDBinder()93 const sptr<DBinderService> GetDBinder() const 94 { 95 return dBinderService_; 96 } 97 98 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) override; 99 100 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) override; 101 102 int32_t RemoveSystemAbility(int32_t systemAbilityId) override; 103 104 int32_t SubscribeSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener) override; 105 int32_t UnSubscribeSystemAbility(int32_t systemAbilityId, 106 const sptr<ISystemAbilityStatusChange>& listener) override; 107 void UnSubscribeSystemAbility(const sptr<IRemoteObject>& remoteObject); 108 109 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 110 111 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 112 113 int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const std::u16string& procName) override; 114 115 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override; 116 bool DoLoadOnDemandAbility(int32_t systemAbilityId, bool& isExist); 117 118 int32_t RemoveDiedSystemAbility(int32_t systemAbilityId); 119 120 void NotifyRemoteSaDied(const std::u16string& name); 121 void NotifyRemoteDeviceOffline(const std::string& deviceId); 122 int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability, 123 const SAExtraProp& extraProp) override; GetLocalNodeId()124 std::string GetLocalNodeId() 125 { 126 return std::string(); 127 } 128 void Init(); CleanFfrt()129 void CleanFfrt() 130 { 131 if (workHandler_ != nullptr) { 132 workHandler_->CleanFfrt(); 133 } 134 if (collectManager_ != nullptr) { 135 collectManager_->CleanFfrt(); 136 } 137 if (abilityStateScheduler_ != nullptr) { 138 abilityStateScheduler_->CleanFfrt(); 139 } 140 } 141 SetFfrt()142 void SetFfrt() 143 { 144 if (workHandler_ != nullptr) { 145 workHandler_->SetFfrt("workHandler"); 146 } 147 if (collectManager_ != nullptr) { 148 collectManager_->SetFfrt(); 149 } 150 if (abilityStateScheduler_ != nullptr) { 151 abilityStateScheduler_->SetFfrt(); 152 } 153 } 154 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 155 void AddSamgrToAbilityMap(); 156 157 int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) override; 158 int32_t RemoveSystemProcess(const sptr<IRemoteObject>& procObject); 159 int32_t GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo) override; 160 int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos) override; 161 int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override; 162 int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override; 163 int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel) override; 164 LoadSystemAbility(int32_t systemAbilityId,int32_t timeout)165 sptr<IRemoteObject> LoadSystemAbility(int32_t systemAbilityId, int32_t timeout) override 166 { 167 return nullptr; 168 } 169 170 int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) override; 171 int32_t DoLoadSystemAbility(int32_t systemAbilityId, const std::u16string& procName, 172 const sptr<ISystemAbilityLoadCallback>& callback, int32_t callingPid, const OnDemandEvent& event); 173 int32_t LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId, 174 const sptr<ISystemAbilityLoadCallback>& callback) override; 175 int32_t UnloadSystemAbility(int32_t systemAbilityId) override; 176 int32_t DoUnloadSystemAbility(int32_t systemAbilityId, const std::u16string& procName, const OnDemandEvent& event); 177 int32_t CancelUnloadSystemAbility(int32_t systemAbilityId) override; 178 int32_t DoUnloadSystemAbility(int32_t systemAbilityId, const std::u16string& procName); 179 int32_t UnloadAllIdleSystemAbility() override; 180 bool IdleSystemAbility(int32_t systemAbilityId, const std::u16string& procName, 181 const nlohmann::json& idleReason, int32_t& delayTime); 182 bool ActiveSystemAbility(int32_t systemAbilityId, const std::u16string& procName, 183 const nlohmann::json& activeReason); 184 int32_t UnloadProcess(const std::vector<std::u16string>& processList) override; 185 int32_t GetLruIdleSystemAbilityProc(std::vector<IdleProcessInfo>& processInfos) override; 186 void OnAbilityCallbackDied(const sptr<IRemoteObject>& remoteObject); 187 void OnRemoteCallbackDied(const sptr<IRemoteObject>& remoteObject); 188 sptr<IRemoteObject> GetSystemAbilityFromRemote(int32_t systemAbilityId); 189 bool LoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId, 190 const sptr<ISystemAbilityLoadCallback>& callback); 191 int32_t DoLoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId, 192 const std::u16string& procName, const sptr<ISystemAbilityLoadCallback>& callback, const OnDemandEvent& event); 193 void NotifyRpcLoadCompleted(const std::string& srcDeviceId, int32_t systemAbilityId, 194 const sptr<IRemoteObject>& remoteObject); 195 void StartDfxTimer(); 196 void DoLoadForPerf(); 197 void ProcessOnDemandEvent(const OnDemandEvent& event, const std::list<SaControlInfo>& saControlList); 198 int32_t GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type, 199 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override; 200 int32_t UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type, 201 const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override; 202 int32_t GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds) override; 203 int32_t SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds, 204 int32_t level, std::string& action) override; CheckSaIsImmediatelyRecycle(int32_t systemAbilityId)205 bool CheckSaIsImmediatelyRecycle(int32_t systemAbilityId) 206 { 207 CommonSaProfile saProfile; 208 bool ret = GetSaProfile(systemAbilityId, saProfile); 209 if (!ret) { 210 HILOGE("UnloadSystemAbility SA:%{public}d not supported!", systemAbilityId); 211 return ERR_INVALID_VALUE; 212 } 213 return saProfile.recycleStrategy == IMMEDIATELY; 214 } IsDistributedSystemAbility(int32_t systemAbilityId)215 bool IsDistributedSystemAbility(int32_t systemAbilityId) 216 { 217 CommonSaProfile saProfile; 218 bool ret = GetSaProfile(systemAbilityId, saProfile); 219 if (!ret) { 220 HILOGE("IsDistributedSa SA:%{public}d no Profile!", systemAbilityId); 221 return false; 222 } 223 return saProfile.distributed; 224 } 225 int32_t GetRunningSaExtensionInfoList(const std::string& extension, 226 std::vector<SaExtensionInfo>& infoList) override; 227 int32_t GetExtensionSaIds(const std::string& extension, std::vector<int32_t>& saIds) override; 228 int32_t GetExtensionRunningSaList(const std::string& extension, std::vector<sptr<IRemoteObject>>& saList) override; 229 int32_t GetCommonEventExtraDataIdlist(int32_t saId, std::vector<int64_t>& extraDataIdList, 230 const std::string& eventName = "") override; 231 sptr<IRemoteObject> GetSystemProcess(const std::u16string& procName); GetLocalAbilityManagerProxy(int32_t systemAbilityId)232 sptr<IRemoteObject> GetLocalAbilityManagerProxy(int32_t systemAbilityId) override 233 { 234 CommonSaProfile saProfile; 235 if (!GetSaProfile(systemAbilityId, saProfile)) { 236 HILOGD("SA:%{public}d no profile!", systemAbilityId); 237 return nullptr; 238 } 239 return GetSystemProcess(saProfile.process); 240 } IsModuleUpdate(int32_t systemAbilityId)241 bool IsModuleUpdate(int32_t systemAbilityId) 242 { 243 CommonSaProfile saProfile; 244 bool ret = GetSaProfile(systemAbilityId, saProfile); 245 if (!ret) { 246 HILOGE("IsModuleUpdate SA:%{public}d not exist!", systemAbilityId); 247 return false; 248 } 249 return saProfile.moduleUpdate; 250 } RemoveWhiteCommonEvent()251 void RemoveWhiteCommonEvent() 252 { 253 if (collectManager_ != nullptr) { 254 collectManager_->RemoveWhiteCommonEvent(); 255 } 256 } 257 void RemoveOnDemandSaInDiedProc(std::shared_ptr<SystemProcessContext>& processContext); 258 #ifdef SAMGR_ENABLE_DELAY_DBINDER 259 void InitDbinderService(); 260 #endif 261 private: 262 enum class AbilityState { 263 INIT, 264 STARTING, 265 STARTED, 266 }; 267 268 using CallbackList = std::list<std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>>; 269 270 struct AbilityItem { 271 AbilityState state = AbilityState::INIT; 272 std::map<std::string, CallbackList> callbackMap; // key : networkid 273 OnDemandEvent event; 274 }; 275 SystemAbilityManager()276 SystemAbilityManager() 277 { 278 #ifndef SAMGR_ENABLE_DELAY_DBINDER 279 dBinderService_ = DBinderService::GetInstance(); 280 #endif 281 } 282 std::string EventToJson(const OnDemandEvent& event); 283 void DoInsertSaData(const std::u16string& name, const sptr<IRemoteObject>& ability, const SAExtraProp& extraProp); StartOnDemandAbility(int32_t systemAbilityId,bool & isExist)284 int32_t StartOnDemandAbility(int32_t systemAbilityId, bool& isExist) 285 { 286 std::lock_guard<samgr::mutex> onDemandAbilityLock(onDemandLock_); 287 return StartOnDemandAbilityLocked(systemAbilityId, isExist); 288 } 289 int32_t StartOnDemandAbilityLocked(int32_t systemAbilityId, bool& isExist); 290 void RefreshListenerState(int32_t systemAbilityId); 291 int32_t AddSystemAbility(const std::u16string& name, const sptr<IRemoteObject>& ability, 292 const SAExtraProp& extraProp); 293 int32_t FindSystemAbilityNotify(int32_t systemAbilityId, int32_t code); 294 int32_t FindSystemAbilityNotify(int32_t systemAbilityId, const std::string& deviceId, int32_t code); 295 296 void InitSaProfile(); GetSaProfile(int32_t saId,CommonSaProfile & saProfile)297 bool GetSaProfile(int32_t saId, CommonSaProfile& saProfile) 298 { 299 std::lock_guard<samgr::mutex> autoLock(saProfileMapLock_); 300 auto iter = saProfileMap_.find(saId); 301 if (iter == saProfileMap_.end()) { 302 return false; 303 } else { 304 saProfile = iter->second; 305 } 306 return true; 307 } 308 void CheckListenerNotify(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener); 309 void NotifySystemAbilityChanged(int32_t systemAbilityId, const std::string& deviceId, int32_t code, 310 const sptr<ISystemAbilityStatusChange>& listener); 311 void NotifySystemAbilityAddedByAsync(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener); 312 void UnSubscribeSystemAbilityLocked(std::list<SAListener>& listenerList, 313 const sptr<IRemoteObject>& listener); 314 315 void SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject); 316 void SendSystemAbilityRemovedMsg(int32_t systemAbilityId); 317 318 void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject); 319 void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject, 320 const sptr<ISystemAbilityLoadCallback>& callback); 321 void NotifySystemAbilityLoadFail(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback); 322 int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); 323 int32_t StartingSystemProcessLocked(const std::u16string& name, int32_t systemAbilityId, 324 const OnDemandEvent& event); StartOnDemandAbility(const std::u16string & name,int32_t systemAbilityId)325 void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId) 326 { 327 std::lock_guard<samgr::mutex> autoLock(onDemandLock_); 328 StartOnDemandAbilityLocked(name, systemAbilityId); 329 } 330 void StartOnDemandAbilityLocked(const std::u16string& name, int32_t systemAbilityId); 331 int32_t StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem); 332 bool IsInitBootFinished(); 333 int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); StopOnDemandAbility(const std::u16string & name,int32_t systemAbilityId,const OnDemandEvent & event)334 bool StopOnDemandAbility(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event) 335 { 336 std::lock_guard<samgr::mutex> autoLock(onDemandLock_); 337 return StopOnDemandAbilityInner(name, systemAbilityId, event); 338 } 339 bool StopOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); 340 void RemoveStartingAbilityCallback(CallbackList& callbackList, const sptr<IRemoteObject>& remoteObject); 341 void RemoveStartingAbilityCallbackForDevice(AbilityItem& abilityItem, const sptr<IRemoteObject>& remoteObject); 342 void RemoveStartingAbilityCallbackLocked(std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>& itemPair); IsCacheCommonEvent(int32_t systemAbilityId)343 bool IsCacheCommonEvent(int32_t systemAbilityId) 344 { 345 CommonSaProfile saProfile; 346 if (!GetSaProfile(systemAbilityId, saProfile)) { 347 HILOGD("SA:%{public}d no profile!", systemAbilityId); 348 return false; 349 } 350 return saProfile.cacheCommonEvent; 351 } 352 void SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, const std::string& srcDeviceId, 353 const sptr<ISystemAbilityLoadCallback>& callback); 354 void RemoveCheckLoadedMsg(int32_t systemAbilityId); 355 void SendLoadedSystemAbilityMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject, 356 const sptr<ISystemAbilityLoadCallback>& callback); 357 void DoLoadRemoteSystemAbility(int32_t systemAbilityId, int32_t callingPid, 358 int32_t callingUid, const std::string& deviceId, const sptr<ISystemAbilityLoadCallback>& callback); 359 sptr<DBinderServiceStub> DoMakeRemoteBinder(int32_t systemAbilityId, int32_t callingPid, int32_t callingUid, 360 const std::string& deviceId); 361 void RemoveRemoteCallbackLocked(std::list<sptr<ISystemAbilityLoadCallback>>& callbacks, 362 const sptr<IRemoteObject>& remoteObject); 363 void CleanCallbackForLoadFailed(int32_t systemAbilityId, const std::u16string& name, 364 const std::string& srcDeviceId, const sptr<ISystemAbilityLoadCallback>& callback); 365 int32_t UpdateSaFreMap(int32_t uid, int32_t saId); 366 void ReportGetSAPeriodically(); 367 void OndemandLoad(); 368 void OndemandLoadForPerf(); 369 std::list<int32_t> GetAllOndemandSa(); 370 void SystemAbilityInvalidateCache(int32_t systemAbilityId); 371 #ifdef SUPPORT_DEVICE_MANAGER 372 void DeviceIdToNetworkId(std::string& networkId); 373 #endif 374 bool IpcStatSamgrProc(int32_t fd, int32_t cmd); 375 void IpcDumpAllProcess(int32_t fd, int32_t cmd); 376 void IpcDumpSamgrProcess(int32_t fd, int32_t cmd); 377 void IpcDumpSingleProcess(int32_t fd, int32_t cmd, const std::string processName); 378 int32_t IpcDumpProc(int32_t fd, const std::vector<std::string>& args); 379 void RegisterDistribute(int32_t said, bool isDistributed); 380 void ConvertDumpListener(std::vector<std::pair<int32_t, std::list<int32_t>>>& dumpListeners); 381 382 std::u16string deviceName_; 383 static sptr<SystemAbilityManager> instance; 384 static samgr::mutex instanceLock; 385 sptr<IRemoteObject::DeathRecipient> abilityDeath_; 386 sptr<IRemoteObject::DeathRecipient> systemProcessDeath_; 387 sptr<IRemoteObject::DeathRecipient> abilityStatusDeath_; 388 sptr<IRemoteObject::DeathRecipient> abilityCallbackDeath_; 389 sptr<IRemoteObject::DeathRecipient> remoteCallbackDeath_; 390 sptr<DBinderService> dBinderService_; 391 sptr<DeviceStatusCollectManager> collectManager_; 392 std::shared_ptr<RpcSystemAbilityCallback> rpcCallbackImp_; 393 394 #ifdef SAMGR_ENABLE_DELAY_DBINDER 395 samgr::shared_mutex dBinderServiceLock_; 396 std::list<int32_t> distributedSaList_; 397 bool isDbinderServiceInit_ = false; 398 #endif 399 400 // must hold abilityMapLock_ never access other locks 401 samgr::shared_mutex abilityMapLock_; 402 std::map<int32_t, SAInfo> abilityMap_; 403 404 // maybe hold listenerMapLock_ and then access onDemandLock_ 405 samgr::mutex listenerMapLock_; 406 std::map<int32_t, std::list<SAListener>> listenerMap_; 407 std::map<int32_t, int32_t> subscribeCountMap_; 408 409 samgr::mutex onDemandLock_; 410 std::map<int32_t, std::u16string> onDemandAbilityMap_; 411 std::map<int32_t, AbilityItem> startingAbilityMap_; 412 samgr::mutex systemProcessMapLock_; 413 std::map<std::u16string, sptr<IRemoteObject>> systemProcessMap_; 414 samgr::mutex startingProcessMapLock_; 415 std::map<std::u16string, int64_t> startingProcessMap_; 416 std::map<int32_t, int32_t> callbackCountMap_; 417 418 std::shared_ptr<FFRTHandler> workHandler_; 419 420 std::map<int32_t, CommonSaProfile> saProfileMap_; 421 std::set<int32_t> onDemandSaIdsSet_; 422 samgr::mutex saProfileMapLock_; 423 samgr::mutex loadRemoteLock_; 424 std::map<std::string, std::list<sptr<ISystemAbilityLoadCallback>>> remoteCallbacks_; // key : said_deviceId 425 426 samgr::mutex saFrequencyLock_; 427 std::map<uint64_t, int32_t> saFrequencyMap_; // {pid_said, count} 428 429 std::unique_ptr<Utils::Timer> reportEventTimer_; 430 std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler_; 431 }; 432 } // namespace OHOS 433 434 #endif // !defined(SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H) 435