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 #include "faultlogger_service_ohos.h"
16
17 #include <functional>
18 #include <string>
19 #include <vector>
20
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 #include "hiview_logger.h"
27
28 #include "faultlog_info.h"
29 #include "faultlog_info_ohos.h"
30 #include "faultlog_query_result_inner.h"
31 #include "faultlog_query_result_ohos.h"
32 #include "faultlogger.h"
33
34 DEFINE_LOG_LABEL(0xD002D11, "FaultloggerServiceOhos");
35 namespace OHOS {
36 namespace HiviewDFX {
37 namespace {
38 constexpr int32_t UID_SHELL = 2000;
39 constexpr int32_t UID_ROOT = 0;
40 constexpr int32_t UID_HIDUMPER = 1212;
41 constexpr int32_t UID_HIVIEW = 1201;
42 constexpr int32_t UID_FAULTLOGGERD = 1202;
43 }
ClearQueryStub(int32_t uid)44 void FaultloggerServiceOhos::ClearQueryStub(int32_t uid)
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 queries_.erase(uid);
48 }
49
Dump(int32_t fd,const std::vector<std::u16string> & args)50 int32_t FaultloggerServiceOhos::Dump(int32_t fd, const std::vector<std::u16string> &args)
51 {
52 int32_t uid = IPCSkeleton::GetCallingUid();
53 if ((uid != UID_SHELL) && (uid != UID_ROOT) && (uid != UID_HIDUMPER)) {
54 dprintf(fd, "No permission for uid:%d.\n", uid);
55 return -1;
56 }
57
58 std::vector<std::string> cmdList;
59 for (auto arg : args) {
60 for (auto c : arg) {
61 if (!isalnum(c) && c != '-' && c != ' ' && c != '_' && c != '.') {
62 dprintf(fd, "string arg contain invalid char:%c.\n", c);
63 return -1;
64 }
65 }
66 }
67 std::transform(args.begin(), args.end(), std::back_inserter(cmdList), [](const std::u16string &arg) {
68 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
69 return converter.to_bytes(arg);
70 });
71
72 auto service = GetOrSetFaultlogger();
73 if (!service) {
74 dprintf(fd, "Service is not ready.\n");
75 return -1;
76 }
77
78 service->Dump(fd, cmdList);
79 return 0;
80 }
81
StartService(std::shared_ptr<IFaultLogManagerService> service)82 void FaultloggerServiceOhos::StartService(std::shared_ptr<IFaultLogManagerService> service)
83 {
84 GetOrSetFaultlogger(service);
85 sptr<ISystemAbilityManager> serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86 if (serviceManager == nullptr) {
87 HIVIEW_LOGE("Failed to find samgr, exit.");
88 return;
89 }
90
91 static sptr<FaultloggerServiceOhos> instance = new FaultloggerServiceOhos();
92 serviceManager->AddSystemAbility(DFX_FAULT_LOGGER_ABILITY_ID, instance);
93 HIVIEW_LOGI("FaultLogger Service started.");
94 }
95
GetOrSetFaultlogger(std::shared_ptr<IFaultLogManagerService> service)96 std::shared_ptr<IFaultLogManagerService> FaultloggerServiceOhos::GetOrSetFaultlogger(
97 std::shared_ptr<IFaultLogManagerService> service)
98 {
99 static std::shared_ptr<IFaultLogManagerService> ref;
100 if (service != nullptr) {
101 ref = service;
102 }
103 return ref;
104 }
105
AddFaultLog(const FaultLogInfoOhos & info)106 void FaultloggerServiceOhos::AddFaultLog(const FaultLogInfoOhos& info)
107 {
108 auto service = GetOrSetFaultlogger();
109 if (!service) {
110 return;
111 }
112
113 int32_t uid = IPCSkeleton::GetCallingUid();
114 HIVIEW_LOGD("info.uid:%{public}d uid:%{public}d info.pid:%{public}d", info.uid, uid, info.pid);
115 if ((uid != static_cast<int32_t>(getuid())) && uid != info.uid && uid != UID_FAULTLOGGERD) {
116 HIVIEW_LOGW("Fail to add fault log, mismatch uid:%{public}d(%{public}d)", uid, info.uid);
117 return;
118 }
119
120 FaultLogInfo outInfo;
121 outInfo.time = info.time;
122 outInfo.id = info.uid;
123 outInfo.pid = info.pid;
124 auto fdDeleter = [] (int32_t *ptr) {
125 if (*ptr > 0) {
126 close(*ptr);
127 }
128 delete ptr;
129 };
130 outInfo.pipeFd.reset(new int32_t(info.pipeFd), fdDeleter);
131 outInfo.faultLogType = info.faultLogType;
132 outInfo.fd = (info.fd > 0) ? dup(info.fd) : -1;
133 outInfo.logFileCutoffSizeBytes = info.logFileCutoffSizeBytes;
134 outInfo.module = info.module;
135 outInfo.reason = info.reason;
136 outInfo.summary = info.summary;
137 if (uid == UID_HIVIEW) {
138 outInfo.logPath = info.logPath;
139 }
140 outInfo.registers = info.registers;
141 outInfo.sectionMap = info.sectionMaps;
142 service->AddFaultLog(outInfo);
143 }
144
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)145 sptr<IRemoteObject> FaultloggerServiceOhos::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
146 {
147 auto service = GetOrSetFaultlogger();
148 if (!service) {
149 return nullptr;
150 }
151
152 int32_t uid = IPCSkeleton::GetCallingUid();
153 int32_t pid = IPCSkeleton::GetCallingPid();
154 std::lock_guard<std::mutex> lock(mutex_);
155 if ((queries_.find(uid) != queries_.end()) &&
156 (queries_[uid]->pid == pid)) {
157 HIVIEW_LOGW("Ongoing query is still alive for uid:%d", uid);
158 return nullptr;
159 }
160
161 auto queryResult = service->QuerySelfFaultLog(uid, pid, faultType, maxNum);
162 if (queryResult == nullptr) {
163 HIVIEW_LOGW("Fail to query fault log for uid:%d", uid);
164 return nullptr;
165 }
166
167 sptr<FaultLogQueryResultOhos> resultRef =
168 new FaultLogQueryResultOhos(std::move(queryResult));
169 auto queryStub = std::make_unique<FaultLogQuery>();
170 queryStub->pid = pid;
171 queryStub->ptr = resultRef;
172 queries_[uid] = std::move(queryStub);
173 return resultRef->AsObject();
174 }
175
EnableGwpAsanGrayscale(bool alwaysEnabled,double sampleRate,double maxSimutaneousAllocations,int32_t duration)176 bool FaultloggerServiceOhos::EnableGwpAsanGrayscale(bool alwaysEnabled, double sampleRate,
177 double maxSimutaneousAllocations, int32_t duration)
178 {
179 auto service = GetOrSetFaultlogger();
180 if (!service) {
181 return false;
182 }
183 if (sampleRate <= 0 || maxSimutaneousAllocations <= 0 || duration <= 0) {
184 HIVIEW_LOGE("failed to enable gwp asan grayscale, sampleRate: %{public}f"
185 ", maxSimutaneousAllocations: %{public}f, duration: %{public}d.",
186 sampleRate, maxSimutaneousAllocations, duration);
187 return false;
188 }
189 int32_t uid = IPCSkeleton::GetCallingUid();
190 bool result = service->EnableGwpAsanGrayscale(alwaysEnabled, sampleRate,
191 maxSimutaneousAllocations, duration, uid);
192 return result;
193 }
194
DisableGwpAsanGrayscale()195 void FaultloggerServiceOhos::DisableGwpAsanGrayscale()
196 {
197 auto service = GetOrSetFaultlogger();
198 if (!service) {
199 return;
200 }
201
202 int32_t uid = IPCSkeleton::GetCallingUid();
203 service->DisableGwpAsanGrayscale(uid);
204 }
205
GetGwpAsanGrayscaleState()206 uint32_t FaultloggerServiceOhos::GetGwpAsanGrayscaleState()
207 {
208 auto service = GetOrSetFaultlogger();
209 if (!service) {
210 return 0;
211 }
212
213 int32_t uid = IPCSkeleton::GetCallingUid();
214 return service->GetGwpAsanGrayscaleState(uid);
215 }
216
Destroy()217 void FaultloggerServiceOhos::Destroy()
218 {
219 auto service = GetOrSetFaultlogger();
220 if (!service) {
221 return;
222 }
223
224 int32_t uid = IPCSkeleton::GetCallingUid();
225 HIVIEW_LOGI("Destroy Query from uid:%d", uid);
226 ClearQueryStub(uid);
227 }
228 } // namespace HiviewDFX
229 } // namespace OHOS