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 <mutex> 21 #include <set> 22 #include <shared_mutex> 23 #include <string> 24 #include <utility> 25 26 #include "dbinder_service.h" 27 #include "dbinder_service_stub.h" 28 #include "device_status_collect_manager.h" 29 #include "ffrt_handler.h" 30 #include "dynamic_cache.h" 31 #include "rpc_callback_imp.h" 32 #include "thread_pool.h" 33 #include "timer.h" 34 #include "sa_profiles.h" 35 #include "system_ability_manager_stub.h" 36 #include "schedule/system_ability_state_scheduler.h" 37 38 namespace OHOS { 39 struct SAInfo { 40 sptr<IRemoteObject> remoteObj; 41 bool isDistributed = false; 42 std::u16string capability; 43 std::string permission; 44 }; 45 46 enum { 47 UUID = 0, 48 NODE_ID, 49 UNKNOWN, 50 }; 51 52 class SystemAbilityManager : public DynamicCache<int32_t, sptr<IRemoteObject>>, public SystemAbilityManagerStub { 53 public: 54 virtual ~SystemAbilityManager(); 55 static sptr<SystemAbilityManager> GetInstance(); 56 57 int32_t RemoveSystemAbility(const sptr<IRemoteObject>& ability); 58 std::vector<std::u16string> ListSystemAbilities(uint32_t dumpFlags) override; 59 60 void SetDeviceName(const std::u16string &name); 61 62 const std::u16string& GetDeviceName() const; 63 64 const sptr<DBinderService> GetDBinder() const; 65 66 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) override; 67 68 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) override; 69 70 int32_t RemoveSystemAbility(int32_t systemAbilityId) override; 71 72 int32_t SubscribeSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener) override; 73 int32_t UnSubscribeSystemAbility(int32_t systemAbilityId, 74 const sptr<ISystemAbilityStatusChange>& listener) override; 75 void UnSubscribeSystemAbility(const sptr<IRemoteObject>& remoteObject); 76 77 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 78 79 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 80 81 int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const std::u16string& procName) override; 82 83 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override; 84 bool DoLoadOnDemandAbility(int32_t systemAbilityId, bool& isExist); 85 86 int32_t RemoveDiedSystemAbility(int32_t systemAbilityId); 87 88 void NotifyRemoteSaDied(const std::u16string& name); 89 void NotifyRemoteDeviceOffline(const std::string& deviceId); 90 int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability, 91 const SAExtraProp& extraProp) override; 92 std::string TransformDeviceId(const std::string& deviceId, int32_t type, bool isPrivate); 93 std::string GetLocalNodeId(); 94 void Init(); 95 void CleanFfrt(); 96 void SetFfrt(); 97 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 98 void AddSamgrToAbilityMap(); 99 100 int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) override; 101 int32_t RemoveSystemProcess(const sptr<IRemoteObject>& procObject); 102 int32_t GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo) override; 103 int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos) override; 104 int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override; 105 int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override; 106 int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel) override; 107 LoadSystemAbility(int32_t systemAbilityId,int32_t timeout)108 sptr<IRemoteObject> LoadSystemAbility(int32_t systemAbilityId, int32_t timeout) override 109 { 110 return nullptr; 111 } 112 113 int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) override; 114 int32_t DoLoadSystemAbility(int32_t systemAbilityId, const std::u16string& procName, 115 const sptr<ISystemAbilityLoadCallback>& callback, int32_t callingPid, const OnDemandEvent& event); 116 int32_t LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId, 117 const sptr<ISystemAbilityLoadCallback>& callback) override; 118 int32_t UnloadSystemAbility(int32_t systemAbilityId) override; 119 int32_t DoUnloadSystemAbility(int32_t systemAbilityId, const std::u16string& procName, const OnDemandEvent& event); 120 int32_t CancelUnloadSystemAbility(int32_t systemAbilityId) override; 121 int32_t DoUnloadSystemAbility(int32_t systemAbilityId, const std::u16string& procName); 122 int32_t UnloadAllIdleSystemAbility() override; 123 bool IdleSystemAbility(int32_t systemAbilityId, const std::u16string& procName, 124 const nlohmann::json& idleReason, int32_t& delayTime); 125 bool ActiveSystemAbility(int32_t systemAbilityId, const std::u16string& procName, 126 const nlohmann::json& activeReason); 127 void OnAbilityCallbackDied(const sptr<IRemoteObject>& remoteObject); 128 void OnRemoteCallbackDied(const sptr<IRemoteObject>& remoteObject); 129 sptr<IRemoteObject> GetSystemAbilityFromRemote(int32_t systemAbilityId); 130 bool LoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId, 131 const sptr<ISystemAbilityLoadCallback>& callback); 132 int32_t DoLoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId, 133 const std::u16string& procName, const sptr<ISystemAbilityLoadCallback>& callback, const OnDemandEvent& event); 134 void NotifyRpcLoadCompleted(const std::string& srcDeviceId, int32_t systemAbilityId, 135 const sptr<IRemoteObject>& remoteObject); 136 void StartDfxTimer(); 137 void DoLoadForPerf(); 138 void ProcessOnDemandEvent(const OnDemandEvent& event, const std::list<SaControlInfo>& saControlList); 139 int32_t GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type, 140 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override; 141 int32_t UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type, 142 const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override; 143 int32_t GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds) override; 144 int32_t SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds, 145 int32_t level, std::string& action) override; 146 bool CheckSaIsImmediatelyRecycle(int32_t systemAbilityId); 147 private: 148 enum class AbilityState { 149 INIT, 150 STARTING, 151 STARTED, 152 }; 153 154 using CallbackList = std::list<std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>>; 155 156 struct AbilityItem { 157 AbilityState state = AbilityState::INIT; 158 std::map<std::string, CallbackList> callbackMap; // key : networkid 159 OnDemandEvent event; 160 }; 161 162 SystemAbilityManager(); 163 std::string EventToJson(const OnDemandEvent& event); 164 void DoInsertSaData(const std::u16string& name, const sptr<IRemoteObject>& ability, const SAExtraProp& extraProp); 165 bool IsNameInValid(const std::u16string& name); 166 int32_t StartOnDemandAbility(int32_t systemAbilityId, bool& isExist); 167 int32_t StartOnDemandAbilityLocked(int32_t systemAbilityId, bool& isExist); 168 void ParseRemoteSaName(const std::u16string& name, std::string& deviceId, std::u16string& saName); 169 bool IsLocalDeviceId(const std::string& deviceId); 170 bool CheckDistributedPermission(); 171 int32_t AddSystemAbility(const std::u16string& name, const sptr<IRemoteObject>& ability, 172 const SAExtraProp& extraProp); 173 int32_t FindSystemAbilityNotify(int32_t systemAbilityId, int32_t code); 174 int32_t FindSystemAbilityNotify(int32_t systemAbilityId, const std::string& deviceId, int32_t code); 175 176 sptr<IRemoteObject> GetSystemProcess(const std::u16string& procName); 177 178 void InitSaProfile(); 179 bool GetSaProfile(int32_t saId, SaProfile& saProfile); 180 void NotifySystemAbilityChanged(int32_t systemAbilityId, const std::string& deviceId, int32_t code, 181 const sptr<ISystemAbilityStatusChange>& listener); 182 void UnSubscribeSystemAbilityLocked(std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>& listenerList, 183 const sptr<IRemoteObject>& listener); 184 185 void SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject); 186 void SendSystemAbilityRemovedMsg(int32_t systemAbilityId); 187 188 void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject); 189 void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject, 190 const sptr<ISystemAbilityLoadCallback>& callback); 191 void NotifySystemAbilityLoadFail(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback); 192 int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); 193 int32_t StartingSystemProcessLocked(const std::u16string& name, int32_t systemAbilityId, 194 const OnDemandEvent& event); 195 void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId); 196 void StartOnDemandAbilityLocked(const std::u16string& name, int32_t systemAbilityId); 197 int32_t StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem); 198 int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); 199 bool StopOnDemandAbility(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); 200 bool StopOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event); 201 void RemoveStartingAbilityCallback(CallbackList& callbackList, const sptr<IRemoteObject>& remoteObject); 202 void RemoveStartingAbilityCallbackForDevice(AbilityItem& abilityItem, const sptr<IRemoteObject>& remoteObject); 203 void RemoveStartingAbilityCallbackLocked(std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>& itemPair); 204 void SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, const std::string& srcDeviceId, 205 const sptr<ISystemAbilityLoadCallback>& callback); 206 void RemoveCheckLoadedMsg(int32_t systemAbilityId); 207 void SendLoadedSystemAbilityMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject, 208 const sptr<ISystemAbilityLoadCallback>& callback); 209 void DoLoadRemoteSystemAbility(int32_t systemAbilityId, int32_t callingPid, 210 int32_t callingUid, const std::string& deviceId, const sptr<ISystemAbilityLoadCallback>& callback); 211 sptr<DBinderServiceStub> DoMakeRemoteBinder(int32_t systemAbilityId, int32_t callingPid, int32_t callingUid, 212 const std::string& deviceId); 213 void RemoveRemoteCallbackLocked(std::list<sptr<ISystemAbilityLoadCallback>>& callbacks, 214 const sptr<IRemoteObject>& remoteObject); 215 void CleanCallbackForLoadFailed(int32_t systemAbilityId, const std::u16string& name, 216 const std::string& srcDeviceId, const sptr<ISystemAbilityLoadCallback>& callback); 217 int32_t CheckStartEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl, 218 sptr<ISystemAbilityLoadCallback> callback); 219 int32_t CheckStopEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl); 220 bool IsSameEvent(const OnDemandEvent& event, std::list<OnDemandEvent>& enableOnceList); 221 int32_t UpdateSaFreMap(int32_t uid, int32_t saId); 222 uint64_t GenerateFreKey(int32_t uid, int32_t saId) const; 223 void ReportGetSAPeriodically(); 224 void OndemandLoad(); 225 void OndemandLoadForPerf(); 226 std::list<int32_t> GetAllOndemandSa(); 227 std::string EventToStr(const OnDemandEvent& event); 228 bool CheckCallerProcess(SaProfile& saProfile); 229 bool CheckCallerProcess(const std::string& callProcess); 230 bool CheckAllowUpdate(OnDemandPolicyType type, SaProfile& saProfile); 231 void ConvertToOnDemandEvent(const SystemAbilityOnDemandEvent& from, OnDemandEvent& to); 232 void ConvertToSystemAbilityOnDemandEvent(const OnDemandEvent& from, SystemAbilityOnDemandEvent& to); 233 void SystemAbilityInvalidateCache(int32_t systemAbilityId); 234 #ifdef SUPPORT_DEVICE_MANAGER 235 void DeviceIdToNetworkId(std::string& networkId); 236 #endif 237 238 std::u16string deviceName_; 239 static sptr<SystemAbilityManager> instance; 240 static std::mutex instanceLock; 241 sptr<IRemoteObject::DeathRecipient> abilityDeath_; 242 sptr<IRemoteObject::DeathRecipient> systemProcessDeath_; 243 sptr<IRemoteObject::DeathRecipient> abilityStatusDeath_; 244 sptr<IRemoteObject::DeathRecipient> abilityCallbackDeath_; 245 sptr<IRemoteObject::DeathRecipient> remoteCallbackDeath_; 246 sptr<DBinderService> dBinderService_; 247 sptr<DeviceStatusCollectManager> collectManager_; 248 std::shared_ptr<RpcSystemAbilityCallback> rpcCallbackImp_; 249 250 // must hold abilityMapLock_ never access other locks 251 std::shared_mutex abilityMapLock_; 252 std::map<int32_t, SAInfo> abilityMap_; 253 254 // maybe hold listenerMapLock_ and then access onDemandLock_ 255 std::mutex listenerMapLock_; 256 std::map<int32_t, std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>> listenerMap_; 257 std::map<int32_t, int32_t> subscribeCountMap_; 258 259 std::mutex onDemandLock_; 260 std::map<int32_t, std::u16string> onDemandAbilityMap_; 261 std::map<int32_t, AbilityItem> startingAbilityMap_; 262 std::mutex systemProcessMapLock_; 263 std::map<std::u16string, sptr<IRemoteObject>> systemProcessMap_; 264 std::mutex startingProcessMapLock_; 265 std::map<std::u16string, int64_t> startingProcessMap_; 266 std::map<int32_t, int32_t> callbackCountMap_; 267 std::mutex startEnableOnceLock_; 268 std::map<int32_t, std::list<OnDemandEvent>> startEnableOnceMap_; 269 std::mutex stopEnableOnceLock_; 270 std::map<int32_t, std::list<OnDemandEvent>> stopEnableOnceMap_; 271 272 std::shared_ptr<FFRTHandler> workHandler_; 273 274 std::map<int32_t, SaProfile> saProfileMap_; 275 std::set<int32_t> onDemandSaIdsSet_; 276 std::mutex saProfileMapLock_; 277 std::mutex loadRemoteLock_; 278 std::map<std::string, std::list<sptr<ISystemAbilityLoadCallback>>> remoteCallbacks_; // key : said_deviceId 279 280 std::mutex saFrequencyLock_; 281 std::map<uint64_t, int32_t> saFrequencyMap_; // {pid_said, count} 282 283 std::unique_ptr<Utils::Timer> reportEventTimer_; 284 std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler_; 285 }; 286 } // namespace OHOS 287 288 #endif // !defined(SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H) 289