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 constexpr int32_t UID_SHELL = 2000;
38 constexpr int32_t UID_ROOT = 0;
39 constexpr int32_t UID_HIDUMPER = 1212;
40 constexpr int32_t UID_HIVIEW = 1201;
ClearQueryStub(int32_t uid)41 void FaultloggerServiceOhos::ClearQueryStub(int32_t uid)
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 queries_.erase(uid);
45 }
46
Dump(int32_t fd,const std::vector<std::u16string> & args)47 int32_t FaultloggerServiceOhos::Dump(int32_t fd, const std::vector<std::u16string> &args)
48 {
49 int32_t uid = IPCSkeleton::GetCallingUid();
50 if ((uid != UID_SHELL) && (uid != UID_ROOT) && (uid != UID_HIDUMPER)) {
51 dprintf(fd, "No permission for uid:%d.\n", uid);
52 return -1;
53 }
54
55 std::vector<std::string> cmdList;
56 for (auto arg : args) {
57 for (auto c : arg) {
58 if (!isalnum(c) && c != '-' && c != ' ' && c != '_' && c != '.') {
59 dprintf(fd, "string arg contain invalid char:%c.\n", c);
60 return -1;
61 }
62 }
63 }
64 std::transform(args.begin(), args.end(), std::back_inserter(cmdList), [](const std::u16string &arg) {
65 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
66 return converter.to_bytes(arg);
67 });
68
69 auto service = GetOrSetFaultlogger();
70 if (service == nullptr) {
71 dprintf(fd, "Service is not ready.\n");
72 return -1;
73 }
74
75 service->Dump(fd, cmdList);
76 return 0;
77 }
78
StartService(OHOS::HiviewDFX::Faultlogger * service)79 void FaultloggerServiceOhos::StartService(OHOS::HiviewDFX::Faultlogger *service)
80 {
81 GetOrSetFaultlogger(service);
82 sptr<ISystemAbilityManager> serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
83 if (serviceManager == nullptr) {
84 HIVIEW_LOGE("Failed to find samgr, exit.");
85 return;
86 }
87
88 static sptr<FaultloggerServiceOhos> instance = new FaultloggerServiceOhos();
89 serviceManager->AddSystemAbility(DFX_FAULT_LOGGER_ABILITY_ID, instance);
90 HIVIEW_LOGI("FaultLogger Service started.");
91 }
92
GetOrSetFaultlogger(OHOS::HiviewDFX::Faultlogger * service)93 OHOS::HiviewDFX::Faultlogger *FaultloggerServiceOhos::GetOrSetFaultlogger(
94 OHOS::HiviewDFX::Faultlogger *service)
95 {
96 static OHOS::HiviewDFX::Faultlogger *ref = nullptr;
97 if (service != nullptr) {
98 ref = service;
99 }
100 return ref;
101 }
102
AddFaultLog(const FaultLogInfoOhos & info)103 void FaultloggerServiceOhos::AddFaultLog(const FaultLogInfoOhos& info)
104 {
105 auto service = GetOrSetFaultlogger();
106 if (service == nullptr) {
107 return;
108 }
109
110 int32_t uid = IPCSkeleton::GetCallingUid();
111 HIVIEW_LOGD("info.uid:%{public}d uid:%{public}d info.pid:%{public}d", info.uid, uid, info.pid);
112 if (((uid != static_cast<int32_t>(getuid()))) && (uid != info.uid)) {
113 HIVIEW_LOGW("Fail to add fault log, mismatch uid:%{public}d(%{public}d)", uid, info.uid);
114 return;
115 }
116
117 FaultLogInfo outInfo;
118 outInfo.time = info.time;
119 outInfo.id = info.uid;
120 outInfo.pid = info.pid;
121 auto fdDeleter = [] (int32_t *ptr) {
122 if (*ptr > 0) {
123 close(*ptr);
124 }
125 delete ptr;
126 };
127 outInfo.pipeFd.reset(new int32_t(info.pipeFd), fdDeleter);
128 outInfo.faultLogType = info.faultLogType;
129 outInfo.fd = (info.fd > 0) ? dup(info.fd) : -1;
130 outInfo.module = info.module;
131 outInfo.reason = info.reason;
132 outInfo.summary = info.summary;
133 if (uid == UID_HIVIEW) {
134 outInfo.logPath = info.logPath;
135 }
136 outInfo.registers = info.registers;
137 outInfo.sectionMap = info.sectionMaps;
138 service->AddFaultLog(outInfo);
139 }
140
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)141 sptr<IRemoteObject> FaultloggerServiceOhos::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
142 {
143 auto service = GetOrSetFaultlogger();
144 if (service == nullptr) {
145 return nullptr;
146 }
147
148 int32_t uid = IPCSkeleton::GetCallingUid();
149 int32_t pid = IPCSkeleton::GetCallingPid();
150 std::lock_guard<std::mutex> lock(mutex_);
151 if ((queries_.find(uid) != queries_.end()) &&
152 (queries_[uid]->pid == pid)) {
153 HIVIEW_LOGW("Ongoing query is still alive for uid:%d", uid);
154 return nullptr;
155 }
156
157 auto queryResult = service->QuerySelfFaultLog(uid, pid, faultType, maxNum);
158 if (queryResult == nullptr) {
159 HIVIEW_LOGW("Fail to query fault log for uid:%d", uid);
160 return nullptr;
161 }
162
163 sptr<FaultLogQueryResultOhos> resultRef =
164 new FaultLogQueryResultOhos(std::move(queryResult));
165 auto queryStub = std::make_unique<FaultLogQuery>();
166 queryStub->pid = pid;
167 queryStub->ptr = resultRef;
168 queries_[uid] = std::move(queryStub);
169 return resultRef->AsObject();
170 }
171
Destroy()172 void FaultloggerServiceOhos::Destroy()
173 {
174 auto service = GetOrSetFaultlogger();
175 if (service == nullptr) {
176 return;
177 }
178
179 int32_t uid = IPCSkeleton::GetCallingUid();
180 HIVIEW_LOGI("Destroy Query from uid:%d", uid);
181 ClearQueryStub(uid);
182 }
183 } // namespace HiviewDFX
184 } // namespace OHOS