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