1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "DumpstateDevice.h"
18
19 #include <DumpstateUtil.h>
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23
24 #include <fstream>
25 #include <string>
26
27 using android::base::GetProperty;
28 using android::os::dumpstate::CommandOptions;
29 using android::os::dumpstate::DumpFileToFd;
30 using std::chrono::duration_cast;
31 using std::chrono::seconds;
32 using std::literals::chrono_literals::operator""s;
33
34 static constexpr const char* VENDOR_VERBOSE_LOGGING_ENABLED_PROPERTY =
35 "persist.vendor.verbose_logging_enabled";
36
37 static constexpr const char* VENDOR_HELPER_SYSTEM_LOG_LOC_PROPERTY =
38 "ro.vendor.helpersystem.log_loc";
39
40 static constexpr const char* BOOT_HYPERVISOR_VERSION_PROPERTY = "ro.boot.hypervisor.version";
41
42 namespace android::hardware::dumpstate::V1_1::implementation {
43
getChannelCredentials()44 static std::shared_ptr<::grpc::ChannelCredentials> getChannelCredentials() {
45 // TODO(chenhaosjtuacm): get secured credentials here
46 return ::grpc::InsecureChannelCredentials();
47 }
48
dumpDirAsText(int textFd,const fs::path & dirToDump)49 static void dumpDirAsText(int textFd, const fs::path& dirToDump) {
50 for (const auto& fileEntry : fs::recursive_directory_iterator(dirToDump)) {
51 if (!fileEntry.is_regular_file()) {
52 continue;
53 }
54
55 DumpFileToFd(textFd, "Helper System Log", fileEntry.path());
56 }
57 }
58
tryDumpDirAsTar(int textFd,int binFd,const fs::path & dirToDump)59 static void tryDumpDirAsTar(int textFd, int binFd, const fs::path& dirToDump) {
60 if (!fs::is_directory(dirToDump)) {
61 LOG(ERROR) << "'" << dirToDump << "'"
62 << " is not a valid directory to dump";
63 return;
64 }
65
66 if (binFd < 0) {
67 LOG(WARNING) << "No binary dumped file, fallback to text mode";
68 return dumpDirAsText(textFd, dirToDump);
69 }
70
71 TemporaryFile tempTarFile;
72 constexpr auto kTarTimeout = 20s;
73
74 RunCommandToFd(
75 textFd, "TAR LOG", {"/vendor/bin/tar", "cvf", tempTarFile.path, dirToDump.c_str()},
76 CommandOptions::WithTimeout(duration_cast<seconds>(kTarTimeout).count()).Build());
77
78 std::vector<uint8_t> buffer(65536);
79 while (true) {
80 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(tempTarFile.fd, buffer.data(), buffer.size()));
81
82 if (bytes_read == 0) {
83 break;
84 } else if (bytes_read < 0) {
85 PLOG(DEBUG) << "Error reading temporary tar file(" << tempTarFile.path << ")";
86 break;
87 }
88
89 ssize_t result = TEMP_FAILURE_RETRY(write(binFd, buffer.data(), bytes_read));
90
91 if (result != bytes_read) {
92 LOG(DEBUG) << "Failed to write " << bytes_read
93 << " bytes, actually written: " << result;
94 break;
95 }
96 }
97 }
98
dumpRemoteLogs(::grpc::ClientReaderInterface<dumpstate_proto::DumpstateBuffer> * grpcReader,const fs::path & dumpPath)99 bool DumpstateDevice::dumpRemoteLogs(
100 ::grpc::ClientReaderInterface<dumpstate_proto::DumpstateBuffer>* grpcReader,
101 const fs::path& dumpPath) {
102 dumpstate_proto::DumpstateBuffer logStreamBuffer;
103 std::fstream logFile(dumpPath, std::fstream::out | std::fstream::binary);
104
105 if (!logFile.is_open()) {
106 LOG(ERROR) << "Failed to open file " << dumpPath;
107 return false;
108 }
109
110 while (grpcReader->Read(&logStreamBuffer)) {
111 const auto& writeBuffer = logStreamBuffer.buffer();
112 logFile.write(writeBuffer.c_str(), writeBuffer.size());
113 }
114 auto grpcStatus = grpcReader->Finish();
115 if (!grpcStatus.ok()) {
116 LOG(ERROR) << __func__ << ": GRPC GetCommandOutput Failed: " << grpcStatus.error_message();
117 return false;
118 }
119
120 return true;
121 }
122
dumpString(const std::string & text,const fs::path & dumpPath)123 bool DumpstateDevice::dumpString(const std::string& text, const fs::path& dumpPath) {
124 std::fstream logFile(dumpPath, std::fstream::out | std::fstream::binary);
125
126 if (!logFile.is_open()) {
127 LOG(ERROR) << "Failed to open file " << dumpPath;
128 return false;
129 }
130
131 logFile.write(text.c_str(), text.size());
132
133 return true;
134 }
135
dumpHelperSystem(int textFd,int binFd)136 bool DumpstateDevice::dumpHelperSystem(int textFd, int binFd) {
137 std::string helperSystemLogDir =
138 android::base::GetProperty(VENDOR_HELPER_SYSTEM_LOG_LOC_PROPERTY, "");
139
140 if (helperSystemLogDir.empty()) {
141 LOG(ERROR) << "Helper system log location '" << VENDOR_HELPER_SYSTEM_LOG_LOC_PROPERTY
142 << "' not set";
143 return false;
144 }
145
146 std::error_code error;
147
148 auto helperSysLogPath = fs::path(helperSystemLogDir);
149 if (!fs::create_directories(helperSysLogPath, error)) {
150 LOG(ERROR) << "Failed to create the dumping log directory " << helperSystemLogDir << ": "
151 << error;
152 return false;
153 }
154
155 if (!fs::is_directory(helperSysLogPath)) {
156 LOG(ERROR) << helperSystemLogDir << " is not a directory";
157 return false;
158 }
159
160 if (!isHealthy()) {
161 LOG(ERROR) << "Failed to connect to the dumpstate server";
162 return false;
163 }
164
165 // When start dumping, we always return success to keep dumped logs
166 // even if some of them are failed
167
168 {
169 // Dumping system logs
170 ::grpc::ClientContext context;
171 auto reader = mGrpcStub->GetSystemLogs(&context, ::google::protobuf::Empty());
172 dumpRemoteLogs(reader.get(), helperSysLogPath / "system_log");
173 }
174
175 {
176 // Dumping host system info
177 const std::string hyp =
178 "Host version information: " +
179 GetProperty(BOOT_HYPERVISOR_VERSION_PROPERTY, "missing/unavailable");
180 dumpString(hyp, helperSysLogPath / "host_info");
181 }
182
183 // Request for service list every time to allow the service list to change on the server side.
184 // Also the getAvailableServices() may fail and return an empty list (e.g., failure on the
185 // server side), and it should not affect the future queries
186 const auto availableServices = getAvailableServices();
187
188 // Dumping service logs
189 for (const auto& service : availableServices) {
190 ::grpc::ClientContext context;
191 dumpstate_proto::ServiceLogRequest request;
192 request.set_service_name(service);
193 auto reader = mGrpcStub->GetServiceLogs(&context, request);
194 dumpRemoteLogs(reader.get(), helperSysLogPath / service);
195 }
196
197 tryDumpDirAsTar(textFd, binFd, helperSystemLogDir);
198
199 if (fs::remove_all(helperSysLogPath, error) == static_cast<std::uintmax_t>(-1)) {
200 LOG(ERROR) << "Failed to clear the dumping log directory " << helperSystemLogDir << ": "
201 << error;
202 }
203 return true;
204 }
205
isHealthy()206 bool DumpstateDevice::isHealthy() {
207 // Check that we can get services back from the remote end
208 // This check will not work if the server actually works but is
209 // not exporting any services. This seems like a corner case
210 // but it's worth pointing out.
211 return (getAvailableServices().size() > 0);
212 }
213
getAvailableServices()214 std::vector<std::string> DumpstateDevice::getAvailableServices() {
215 ::grpc::ClientContext context;
216 dumpstate_proto::ServiceNameList servicesProto;
217 auto grpc_status =
218 mGrpcStub->GetAvailableServices(&context, ::google::protobuf::Empty(), &servicesProto);
219 if (!grpc_status.ok()) {
220 LOG(ERROR) << "Failed to get available services from the server: "
221 << grpc_status.error_message();
222 return {};
223 }
224
225 std::vector<std::string> services;
226 for (auto& service : servicesProto.service_names()) {
227 services.emplace_back(service);
228 }
229 return services;
230 }
231
DumpstateDevice(const std::string & addr)232 DumpstateDevice::DumpstateDevice(const std::string& addr)
233 : mServiceAddr(addr),
234 mGrpcChannel(::grpc::CreateChannel(mServiceAddr, getChannelCredentials())),
235 mGrpcStub(dumpstate_proto::DumpstateServer::NewStub(mGrpcChannel)) {}
236
237 // Methods from ::android::hardware::dumpstate::V1_0::IDumpstateDevice follow.
dumpstateBoard(const hidl_handle & handle)238 Return<void> DumpstateDevice::dumpstateBoard(const hidl_handle& handle) {
239 // Ignore return value, just return an empty status.
240 dumpstateBoard_1_1(handle, DumpstateMode::DEFAULT, 30 * 1000 /* timeoutMillis */);
241 return Void();
242 }
243
244 // Methods from ::android::hardware::dumpstate::V1_1::IDumpstateDevice follow.
dumpstateBoard_1_1(const hidl_handle & handle,const DumpstateMode,const uint64_t)245 Return<DumpstateStatus> DumpstateDevice::dumpstateBoard_1_1(const hidl_handle& handle,
246 const DumpstateMode /* mode */,
247 const uint64_t /* timeoutMillis */) {
248 if (handle == nullptr || handle->numFds < 1) {
249 LOG(ERROR) << "No FDs";
250 return DumpstateStatus::ILLEGAL_ARGUMENT;
251 }
252
253 const int textFd = handle->data[0];
254 const int binFd = handle->numFds >= 2 ? handle->data[1] : -1;
255
256 if (!dumpHelperSystem(textFd, binFd)) {
257 return DumpstateStatus::DEVICE_LOGGING_NOT_ENABLED;
258 }
259
260 return DumpstateStatus::OK;
261 }
262
setVerboseLoggingEnabled(const bool enable)263 Return<void> DumpstateDevice::setVerboseLoggingEnabled(const bool enable) {
264 android::base::SetProperty(VENDOR_VERBOSE_LOGGING_ENABLED_PROPERTY, enable ? "true" : "false");
265 return Void();
266 }
267
getVerboseLoggingEnabled()268 Return<bool> DumpstateDevice::getVerboseLoggingEnabled() {
269 return android::base::GetBoolProperty(VENDOR_VERBOSE_LOGGING_ENABLED_PROPERTY, false);
270 }
271
debug(const hidl_handle & h,const hidl_vec<hidl_string> & options)272 Return<void> DumpstateDevice::debug(const hidl_handle& h, const hidl_vec<hidl_string>& options) {
273 if (h.getNativeHandle() == nullptr || h->numFds == 0) {
274 LOG(ERROR) << "Invalid FD passed to debug() function";
275 return Void();
276 }
277
278 const int fd = h->data[0];
279 auto pf = [fd](std::string s) -> void { dprintf(fd, "%s\n", s.c_str()); };
280 debugDumpServices(pf);
281
282 return Void();
283 }
284
debugDumpServices(std::function<void (std::string)> f)285 void DumpstateDevice::debugDumpServices(std::function<void(std::string)> f) {
286 f("Available services for Dumpstate:");
287 for (const auto& svc : getAvailableServices()) {
288 f(" " + svc);
289 }
290 }
291
makeVirtualizationDumpstateDevice(const std::string & addr)292 sp<DumpstateDevice> makeVirtualizationDumpstateDevice(const std::string& addr) {
293 return new DumpstateDevice(addr);
294 }
295
296 } // namespace android::hardware::dumpstate::V1_1::implementation
297