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 == nullptr) {
74 dprintf(fd, "Service is not ready.\n");
75 return -1;
76 }
77
78 service->Dump(fd, cmdList);
79 return 0;
80 }
81
StartService(OHOS::HiviewDFX::Faultlogger * service)82 void FaultloggerServiceOhos::StartService(OHOS::HiviewDFX::Faultlogger *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(OHOS::HiviewDFX::Faultlogger * service)96 OHOS::HiviewDFX::Faultlogger *FaultloggerServiceOhos::GetOrSetFaultlogger(
97 OHOS::HiviewDFX::Faultlogger *service)
98 {
99 static OHOS::HiviewDFX::Faultlogger *ref = nullptr;
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 == nullptr) {
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.module = info.module;
134 outInfo.reason = info.reason;
135 outInfo.summary = info.summary;
136 if (uid == UID_HIVIEW) {
137 outInfo.logPath = info.logPath;
138 }
139 outInfo.registers = info.registers;
140 outInfo.sectionMap = info.sectionMaps;
141 service->AddFaultLog(outInfo);
142 }
143
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)144 sptr<IRemoteObject> FaultloggerServiceOhos::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
145 {
146 auto service = GetOrSetFaultlogger();
147 if (service == nullptr) {
148 return nullptr;
149 }
150
151 int32_t uid = IPCSkeleton::GetCallingUid();
152 int32_t pid = IPCSkeleton::GetCallingPid();
153 std::lock_guard<std::mutex> lock(mutex_);
154 if ((queries_.find(uid) != queries_.end()) &&
155 (queries_[uid]->pid == pid)) {
156 HIVIEW_LOGW("Ongoing query is still alive for uid:%d", uid);
157 return nullptr;
158 }
159
160 auto queryResult = service->QuerySelfFaultLog(uid, pid, faultType, maxNum);
161 if (queryResult == nullptr) {
162 HIVIEW_LOGW("Fail to query fault log for uid:%d", uid);
163 return nullptr;
164 }
165
166 sptr<FaultLogQueryResultOhos> resultRef =
167 new FaultLogQueryResultOhos(std::move(queryResult));
168 auto queryStub = std::make_unique<FaultLogQuery>();
169 queryStub->pid = pid;
170 queryStub->ptr = resultRef;
171 queries_[uid] = std::move(queryStub);
172 return resultRef->AsObject();
173 }
174
Destroy()175 void FaultloggerServiceOhos::Destroy()
176 {
177 auto service = GetOrSetFaultlogger();
178 if (service == nullptr) {
179 return;
180 }
181
182 int32_t uid = IPCSkeleton::GetCallingUid();
183 HIVIEW_LOGI("Destroy Query from uid:%d", uid);
184 ClearQueryStub(uid);
185 }
186 } // namespace HiviewDFX
187 } // namespace OHOS