1 /*
2 * Copyright (c) 2021 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 "system_ability.h"
17
18 #include <cinttypes>
19
20 #include "datetime_ex.h"
21 #include "errors.h"
22 #include "hitrace_meter.h"
23 #include "if_system_ability_manager.h"
24 #include "hisysevent_adapter.h"
25 #include "iservice_registry.h"
26 #include "local_ability_manager.h"
27 #include "nlohmann/json.hpp"
28 #include "safwk_log.h"
29 #include "string_ex.h"
30 #include "samgr_xcollie.h"
31
32 namespace OHOS {
33
SystemAbility(bool runOnCreate)34 SystemAbility::SystemAbility(bool runOnCreate)
35 {
36 isRunning_ = false;
37 abilityState_ = SystemAbilityState::NOT_LOADED;
38 isRunOnCreate_ = runOnCreate;
39 isDistributed_ = false;
40 dumpLevel_ = 0;
41 // timeout for waiting dependency ready, which configed in json, with DEFAULT_DEPENDENCY_TIMEOUT(6s) by default
42 dependTimeout_ = DEFAULT_DEPENDENCY_TIMEOUT;
43 capability_ = u"";
44 }
45
SystemAbility(int32_t systemAbilityId,bool runOnCreate)46 SystemAbility::SystemAbility(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(runOnCreate)
47 {
48 saId_ = systemAbilityId;
49 }
50
~SystemAbility()51 SystemAbility::~SystemAbility()
52 {
53 HILOGI(TAG, "SA:%{public}d destroyed", saId_);
54 }
55
MakeAndRegisterAbility(SystemAbility * systemAbility)56 bool SystemAbility::MakeAndRegisterAbility(SystemAbility* systemAbility)
57 {
58 HILOGD(TAG, "registering system ability...");
59 return LocalAbilityManager::GetInstance().AddAbility(systemAbility);
60 }
61
AddSystemAbilityListener(int32_t systemAbilityId)62 bool SystemAbility::AddSystemAbilityListener(int32_t systemAbilityId)
63 {
64 HILOGD(TAG, "SA:%{public}d, listenerSA:%{public}d", systemAbilityId, saId_);
65 return LocalAbilityManager::GetInstance().AddSystemAbilityListener(systemAbilityId, saId_);
66 }
67
RemoveSystemAbilityListener(int32_t systemAbilityId)68 bool SystemAbility::RemoveSystemAbilityListener(int32_t systemAbilityId)
69 {
70 HILOGD(TAG, "SA:%{public}d, listenerSA:%{public}d", systemAbilityId, saId_);
71 return LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(systemAbilityId, saId_);
72 }
73
Publish(sptr<IRemoteObject> systemAbility)74 bool SystemAbility::Publish(sptr<IRemoteObject> systemAbility)
75 {
76 if (systemAbility == nullptr) {
77 HILOGE(TAG, "systemAbility is nullptr");
78 return false;
79 }
80 LOGD("Publish SA:%{public}d", saId_);
81 // Avoid automatic destruction of system ability caused by failure of publishing ability
82 publishObj_ = systemAbility;
83 int64_t begin = GetTickCount();
84 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85 if (samgrProxy == nullptr) {
86 HILOGE(TAG, "failed to get samgrProxy");
87 return false;
88 }
89
90 ISystemAbilityManager::SAExtraProp saExtra(GetDistributed(), GetDumpLevel(), capability_, permission_);
91 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
92 int32_t result = samgrProxy->AddSystemAbility(saId_, publishObj_, saExtra);
93 KHILOGI(TAG, "SA:%{public}d result:%{public}d,spend:%{public}" PRId64 "ms",
94 saId_, result, (GetTickCount() - begin));
95 if (result == ERR_OK) {
96 abilityState_ = SystemAbilityState::ACTIVE;
97 return true;
98 }
99 return false;
100 }
101
CancelIdle()102 bool SystemAbility::CancelIdle()
103 {
104 {
105 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
106 if (abilityState_ != SystemAbilityState::IDLE) {
107 LOGD("cannot CancelIdle SA:%{public}d,sta is %{public}d", saId_, abilityState_);
108 return true;
109 }
110 }
111 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
112 if (samgrProxy == nullptr) {
113 HILOGE(TAG, "failed to get samgrProxy");
114 return false;
115 }
116 LOGI("CancelIdle-SA:%{public}d", saId_);
117 int32_t result = samgrProxy->CancelUnloadSystemAbility(saId_);
118 return result == ERR_OK;
119 }
120
StopAbility(int32_t systemAbilityId)121 void SystemAbility::StopAbility(int32_t systemAbilityId)
122 {
123 HILOGD(TAG, "SA:%{public}d", systemAbilityId);
124
125 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
126 if (samgrProxy == nullptr) {
127 HILOGE(TAG, "failed to get samgrProxy");
128 return;
129 }
130 int64_t begin = GetTickCount();
131 int32_t ret = samgrProxy->RemoveSystemAbility(systemAbilityId);
132 KHILOGI(TAG, "%{public}s to rm SA:%{public}d, spend:%{public}" PRId64 " ms",
133 (ret == ERR_OK) ? "success" : "failed", systemAbilityId, (GetTickCount() - begin));
134 }
135
GetOnDemandReasonExtraData(SystemAbilityOnDemandReason & onDemandStartReason)136 void SystemAbility::GetOnDemandReasonExtraData(SystemAbilityOnDemandReason& onDemandStartReason)
137 {
138 if (!onDemandStartReason.HasExtraData()) {
139 return;
140 }
141 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
142 if (samgrProxy == nullptr) {
143 HILOGE(TAG, "failed to get samgrProxy");
144 return;
145 }
146 LOGI("get extra id: %{public}d", static_cast<int32_t>(onDemandStartReason.GetExtraDataId()));
147 MessageParcel extraDataParcel;
148 int32_t ret = samgrProxy->GetOnDemandReasonExtraData(onDemandStartReason.GetExtraDataId(), extraDataParcel);
149 if (ret != ERR_OK) {
150 HILOGE(TAG, "get extra data failed");
151 return;
152 }
153 auto extraData = extraDataParcel.ReadParcelable<OnDemandReasonExtraData>();
154 if (extraData == nullptr) {
155 HILOGE(TAG, "read extra data failed");
156 return;
157 }
158 onDemandStartReason.SetExtraData(*extraData);
159 HILOGD(TAG, "get extra data: %{public}d, %{public}s", onDemandStartReason.GetExtraData().GetCode(),
160 onDemandStartReason.GetExtraData().GetData().c_str());
161 delete extraData;
162 }
163
Start()164 void SystemAbility::Start()
165 {
166 // Ensure that the lifecycle is sequentially called by SAMGR
167 HILOGD(TAG, "starting system ability...");
168 {
169 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
170 if (abilityState_ != SystemAbilityState::NOT_LOADED) {
171 LOGW("cannot Start SA:%{public}d,sta is %{public}d", saId_, abilityState_);
172 return;
173 }
174 }
175 nlohmann::json startReason = LocalAbilityManager::GetInstance().GetStartReason(saId_);
176 SystemAbilityOnDemandReason onDemandStartReason =
177 LocalAbilityManager::GetInstance().JsonToOnDemandReason(startReason);
178 GetOnDemandReasonExtraData(onDemandStartReason);
179 LOGI("Start-SA:%{public}d", saId_);
180 HITRACE_METER_NAME(HITRACE_TAG_SAMGR, ToString(saId_) + "_OnStart");
181 int64_t begin = GetTickCount();
182
183 OnStart(onDemandStartReason);
184
185 int64_t duration = GetTickCount() - begin;
186 KHILOGI(TAG, "OnStart-SA:%{public}d finished, spend:%{public}" PRId64 " ms",
187 saId_, duration);
188 ReportSaLoadDuration(saId_, SA_LOAD_ON_START, duration);
189 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
190 isRunning_ = true;
191 }
192
Idle(SystemAbilityOnDemandReason & idleReason,int32_t & delayTime)193 void SystemAbility::Idle(SystemAbilityOnDemandReason& idleReason,
194 int32_t& delayTime)
195 {
196 {
197 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
198 if (abilityState_ != SystemAbilityState::ACTIVE) {
199 LOGW("cannot Idle SA:%{public}d,sta is %{public}d", saId_, abilityState_);
200 return;
201 }
202 }
203 GetOnDemandReasonExtraData(idleReason);
204 LOGI("Idle-SA:%{public}d", saId_);
205 int64_t begin = GetTickCount();
206 {
207 SamgrXCollie samgrXCollie("safwk--onIdle_" + ToString(saId_));
208 delayTime = OnIdle(idleReason);
209 }
210 LOGI("OnIdle-SA:%{public}d end,spend:%{public}" PRId64 "ms",
211 saId_, (GetTickCount() - begin));
212 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
213 if (delayTime == 0) {
214 abilityState_ = SystemAbilityState::IDLE;
215 }
216 }
217
Active(SystemAbilityOnDemandReason & activeReason)218 void SystemAbility::Active(SystemAbilityOnDemandReason& activeReason)
219 {
220 {
221 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
222 if (abilityState_ != SystemAbilityState::IDLE) {
223 LOGW("cannot Active SA:%{public}d,sta is %{public}d", saId_, abilityState_);
224 return;
225 }
226 }
227 GetOnDemandReasonExtraData(activeReason);
228 LOGI("Active-SA:%{public}d", saId_);
229 int64_t begin = GetTickCount();
230 {
231 SamgrXCollie samgrXCollie("safwk--onActive_" + ToString(saId_));
232 OnActive(activeReason);
233 }
234 LOGI("OnActive-SA:%{public}d end,spend:%{public}" PRId64 "ms",
235 saId_, (GetTickCount() - begin));
236 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
237 abilityState_ = SystemAbilityState::ACTIVE;
238 }
239
Stop()240 void SystemAbility::Stop()
241 {
242 HILOGD(TAG, "stopping system ability...");
243 {
244 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
245 if (abilityState_ == SystemAbilityState::NOT_LOADED) {
246 LOGW("cannot Stop SA:%{public}d,sta is %{public}d", saId_, abilityState_);
247 return;
248 }
249 }
250 nlohmann::json stopReason = LocalAbilityManager::GetInstance().GetStopReason(saId_);
251 SystemAbilityOnDemandReason onDemandStopReason =
252 LocalAbilityManager::GetInstance().JsonToOnDemandReason(stopReason);
253 GetOnDemandReasonExtraData(onDemandStopReason);
254
255 LOGI("Stop-SA:%{public}d", saId_);
256 int64_t begin = GetTickCount();
257 {
258 SamgrXCollie samgrXCollie("safwk--onStop_" + ToString(saId_));
259 OnStop(onDemandStopReason);
260 }
261 int64_t duration = GetTickCount() - begin;
262 KHILOGI(TAG, "OnStop-SA:%{public}d finished,spend:%{public}" PRId64 "ms",
263 saId_, duration);
264 ReportSaUnLoadDuration(saId_, SA_UNLOAD_ON_STOP, duration);
265
266 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
267 abilityState_ = SystemAbilityState::NOT_LOADED;
268 isRunning_ = false;
269 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
270 if (samgrProxy == nullptr) {
271 HILOGE(TAG, "failed to get samgrProxy");
272 return;
273 }
274 begin = GetTickCount();
275 int32_t ret = samgrProxy->RemoveSystemAbility(saId_);
276 KHILOGI(TAG, "%{public}s to rm SA:%{public}d,spend:%{public}" PRId64 "ms",
277 (ret == ERR_OK) ? "suc" : "fail", saId_, (GetTickCount() - begin));
278 }
279
SADump()280 void SystemAbility::SADump()
281 {
282 OnDump();
283 }
284
GetSystemAbilitId() const285 int32_t SystemAbility::GetSystemAbilitId() const
286 {
287 return saId_;
288 }
289
SetLibPath(const std::string & libPath)290 void SystemAbility::SetLibPath(const std::string& libPath)
291 {
292 libPath_ = libPath;
293 }
294
GetLibPath() const295 const std::string& SystemAbility::GetLibPath() const
296 {
297 return libPath_;
298 }
299
SetDependSa(const std::vector<std::int32_t> & dependSa)300 void SystemAbility::SetDependSa(const std::vector<std::int32_t>& dependSa)
301 {
302 dependSa_ = dependSa;
303 }
304
GetDependSa() const305 const std::vector<std::int32_t>& SystemAbility::GetDependSa() const
306 {
307 return dependSa_;
308 }
309
SetRunOnCreate(bool isRunOnCreate)310 void SystemAbility::SetRunOnCreate(bool isRunOnCreate)
311 {
312 isRunOnCreate_ = isRunOnCreate;
313 }
314
IsRunOnCreate() const315 bool SystemAbility::IsRunOnCreate() const
316 {
317 return isRunOnCreate_;
318 }
319
SetDistributed(bool isDistributed)320 void SystemAbility::SetDistributed(bool isDistributed)
321 {
322 isDistributed_ = isDistributed;
323 }
324
GetDistributed() const325 bool SystemAbility::GetDistributed() const
326 {
327 return isDistributed_;
328 }
329
GetRunningStatus() const330 bool SystemAbility::GetRunningStatus() const
331 {
332 return isRunning_;
333 }
334
GetAbilityState()335 SystemAbilityState SystemAbility::GetAbilityState()
336 {
337 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
338 return abilityState_;
339 }
340
SetDumpLevel(uint32_t dumpLevel)341 void SystemAbility::SetDumpLevel(uint32_t dumpLevel)
342 {
343 dumpLevel_ = dumpLevel;
344 }
345
GetDumpLevel() const346 uint32_t SystemAbility::GetDumpLevel() const
347 {
348 return dumpLevel_;
349 }
350
SetDependTimeout(int32_t dependTimeout)351 void SystemAbility::SetDependTimeout(int32_t dependTimeout)
352 {
353 if (dependTimeout >= MIN_DEPENDENCY_TIMEOUT && dependTimeout <= MAX_DEPENDENCY_TIMEOUT) {
354 dependTimeout_ = dependTimeout;
355 }
356 LOGD("SetDependTimeout new:%{public}d,old:%{public}d", dependTimeout, dependTimeout_);
357 }
358
GetDependTimeout() const359 int32_t SystemAbility::GetDependTimeout() const
360 {
361 return dependTimeout_;
362 }
363
364 // The details should be implemented by subclass
OnDump()365 void SystemAbility::OnDump()
366 {
367 }
368
369 // The details should be implemented by subclass
OnStart()370 void SystemAbility::OnStart()
371 {
372 }
373
374 // The details should be implemented by subclass
OnStart(const SystemAbilityOnDemandReason & startReason)375 void SystemAbility::OnStart(const SystemAbilityOnDemandReason& startReason)
376 {
377 OnStart();
378 }
379
OnIdle(const SystemAbilityOnDemandReason & idleReason)380 int32_t SystemAbility::OnIdle(const SystemAbilityOnDemandReason& idleReason)
381 {
382 LOGD("OnIdle SA, idle reason %{public}d, %{public}s, %{public}s",
383 idleReason.GetId(), idleReason.GetName().c_str(), idleReason.GetValue().c_str());
384 return 0;
385 }
386
OnActive(const SystemAbilityOnDemandReason & activeReason)387 void SystemAbility::OnActive(const SystemAbilityOnDemandReason& activeReason)
388 {
389 LOGD("OnActive SA, active reason %{public}d, %{public}s, %{public}s",
390 activeReason.GetId(), activeReason.GetName().c_str(), activeReason.GetValue().c_str());
391 }
392
393 // The details should be implemented by subclass
OnStop()394 void SystemAbility::OnStop()
395 {
396 }
397
398 // The details should be implemented by subclass
OnStop(const SystemAbilityOnDemandReason & stopReason)399 void SystemAbility::OnStop(const SystemAbilityOnDemandReason& stopReason)
400 {
401 OnStop();
402 }
403
404 // The details should be implemented by subclass
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)405 void SystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
406 {
407 }
408
409 // The details should be implemented by subclass
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)410 void SystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
411 {
412 }
413
GetSystemAbility(int32_t systemAbilityId)414 sptr<IRemoteObject> SystemAbility::GetSystemAbility(int32_t systemAbilityId)
415 {
416 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
417 if (samgrProxy == nullptr) {
418 HILOGE(TAG, "failed to get samgrProxy");
419 return nullptr;
420 }
421
422 return samgrProxy->GetSystemAbility(systemAbilityId);
423 }
424
SetCapability(const std::u16string & capability)425 void SystemAbility::SetCapability(const std::u16string& capability)
426 {
427 capability_ = capability;
428 }
429
GetCapability() const430 const std::u16string& SystemAbility::GetCapability() const
431 {
432 return capability_;
433 }
434
SetPermission(const std::u16string & permission)435 void SystemAbility::SetPermission(const std::u16string& permission)
436 {
437 permission_ = permission;
438 }
439
440 // The details should be implemented by subclass
OnDeviceLevelChanged(int32_t type,int32_t level,std::string & action)441 void SystemAbility::OnDeviceLevelChanged(int32_t type, int32_t level, std::string& action)
442 {
443 }
444
OnExtension(const std::string & extension,MessageParcel & data,MessageParcel & reply)445 int32_t SystemAbility::OnExtension(const std::string& extension, MessageParcel& data, MessageParcel& reply)
446 {
447 return 0;
448 }
449 }
450