• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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_TAG("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 != ' ') {
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})", 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     outInfo.faultLogType = info.faultLogType;
122     outInfo.fd = (info.fd > 0) ? dup(info.fd) : -1;
123     outInfo.module = info.module;
124     outInfo.reason = info.reason;
125     outInfo.summary = info.summary;
126     if (uid == UID_HIVIEW) {
127         outInfo.logPath = info.logPath;
128     }
129     outInfo.sectionMap = info.sectionMaps;
130     service->AddFaultLog(outInfo);
131 }
132 
QuerySelfFaultLog(int32_t faultType,int32_t maxNum)133 sptr<IRemoteObject> FaultloggerServiceOhos::QuerySelfFaultLog(int32_t faultType, int32_t maxNum)
134 {
135     auto service = GetOrSetFaultlogger();
136     if (service == nullptr) {
137         return nullptr;
138     }
139 
140     int32_t uid = IPCSkeleton::GetCallingUid();
141     int32_t pid = IPCSkeleton::GetCallingPid();
142     std::lock_guard<std::mutex> lock(mutex_);
143     if ((queries_.find(uid) != queries_.end()) &&
144         (queries_[uid]->pid == pid)) {
145         HIVIEW_LOGW("Ongoing query is still alive for uid:%d", uid);
146         return nullptr;
147     }
148 
149     auto queryResult = service->QuerySelfFaultLog(uid, pid, faultType, maxNum);
150     if (queryResult == nullptr) {
151         HIVIEW_LOGW("Fail to query fault log for uid:%d", uid);
152         return nullptr;
153     }
154 
155     sptr<FaultLogQueryResultOhos> resultRef =
156         new FaultLogQueryResultOhos(std::move(queryResult));
157     auto queryStub = std::make_unique<FaultLogQuery>();
158     queryStub->pid = pid;
159     queryStub->ptr = resultRef;
160     queries_[uid] = std::move(queryStub);
161     return resultRef->AsObject();
162 }
163 
Destroy()164 void FaultloggerServiceOhos::Destroy()
165 {
166     auto service = GetOrSetFaultlogger();
167     if (service == nullptr) {
168         return;
169     }
170 
171     int32_t uid = IPCSkeleton::GetCallingUid();
172     HIVIEW_LOGI("Destroy Query from uid:%d", uid);
173     ClearQueryStub(uid);
174 }
175 }  // namespace HiviewDFX
176 }  // namespace OHOS