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