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 #include "hiview_service_ability.h"
16
17 #include <mutex>
18 #include <cstdio>
19 #include <unistd.h>
20
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "ipc_skeleton.h"
24
25 #include "string_util.h"
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("HiViewSA-HiViewServiceAbility");
29 namespace {
30 constexpr int MAXRETRYTIMEOUT = 10;
31 }
Dump(int32_t fd,const std::vector<std::u16string> & args)32 int HiviewServiceAbility::Dump(int32_t fd, const std::vector<std::u16string> &args)
33 {
34 auto service = GetOrSetHiviewService(nullptr);
35 if (service != nullptr) {
36 std::vector<std::string> cmds;
37 for (const auto &arg : args) {
38 cmds.push_back(StringUtil::ConvertToUTF8(arg));
39 }
40 service->DumpRequestDispatcher(fd, cmds);
41 }
42 return 0;
43 }
44
HiviewServiceAbility()45 HiviewServiceAbility::HiviewServiceAbility() : SystemAbility(DFX_SYS_HIVIEW_ABILITY_ID, true)
46 {
47 HIVIEW_LOGI("begin, cmd : %d", DFX_SYS_HIVIEW_ABILITY_ID);
48 }
49
~HiviewServiceAbility()50 HiviewServiceAbility::~HiviewServiceAbility()
51 {
52 HIVIEW_LOGI("begin, cmd : %d", DFX_SYS_HIVIEW_ABILITY_ID);
53 }
54
StartServiceAbility(int sleepS)55 void HiviewServiceAbility::StartServiceAbility(int sleepS)
56 {
57 HIVIEW_LOGI("called");
58
59 sptr<ISystemAbilityManager> serviceManager;
60
61 int retryTimeout = MAXRETRYTIMEOUT;
62 while (retryTimeout > 0) {
63 --retryTimeout;
64 if (sleepS > 0) {
65 sleep(sleepS);
66 }
67
68 SystemAbilityManagerClient::GetInstance().DestroySystemAbilityManagerObject();
69 serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
70 if (serviceManager == nullptr) {
71 continue;
72 }
73
74 int result = serviceManager->AddSystemAbility(DFX_SYS_HIVIEW_ABILITY_ID, new HiviewServiceAbility());
75 if (result != 0) {
76 HIVIEW_LOGE("AddSystemAbility error %d", result);
77 continue;
78 }
79 break;
80 }
81
82 if (serviceManager == nullptr) {
83 HIVIEW_LOGE("serviceManager == nullptr");
84 return;
85 }
86
87 auto abilityObjext = serviceManager->AsObject();
88 if (abilityObjext == nullptr) {
89 HIVIEW_LOGE("AsObject() == nullptr");
90 return;
91 }
92
93 bool ret = abilityObjext->AddDeathRecipient(new HiviewServiceAbilityDeathRecipient());
94 if (ret == false) {
95 HIVIEW_LOGE("AddDeathRecipient == false");
96 }
97 }
98
StartService(HiviewService * service)99 void HiviewServiceAbility::StartService(HiviewService *service)
100 {
101 GetOrSetHiviewService(service);
102 StartServiceAbility(0);
103 IPCSkeleton::JoinWorkThread();
104 }
105
GetOrSetHiviewService(HiviewService * service)106 HiviewService *HiviewServiceAbility::GetOrSetHiviewService(HiviewService *service)
107 {
108 static HiviewService *ref = nullptr;
109 if (service != nullptr) {
110 ref = service;
111 }
112 return ref;
113 }
114
OnDump()115 void HiviewServiceAbility::OnDump()
116 {
117 HIVIEW_LOGI("called");
118 }
119
OnStart()120 void HiviewServiceAbility::OnStart()
121 {
122 HIVIEW_LOGI("called");
123 }
124
OnStop()125 void HiviewServiceAbility::OnStop()
126 {
127 HIVIEW_LOGI("called");
128 }
129
HiviewServiceAbilityDeathRecipient()130 HiviewServiceAbilityDeathRecipient::HiviewServiceAbilityDeathRecipient()
131 {
132 HIVIEW_LOGI("called");
133 }
134
~HiviewServiceAbilityDeathRecipient()135 HiviewServiceAbilityDeathRecipient::~HiviewServiceAbilityDeathRecipient()
136 {
137 HIVIEW_LOGI("called");
138 }
139
OnRemoteDied(const wptr<IRemoteObject> & object)140 void HiviewServiceAbilityDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
141 {
142 HIVIEW_LOGI("called");
143 if (object == nullptr) {
144 return;
145 }
146 HiviewServiceAbility::StartServiceAbility(1);
147 }
148 } // namespace HiviewDFX
149 } // namespace OHOS
150