blob: 2e4e980278f9aedeb6fc7f4401aee82d1c45037f [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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 */
Yi Jinb592e3b2018-02-01 15:17:04 -080016#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080017
18#include "Section.h"
Yi Jin99c248f2017-08-25 18:11:58 -070019
Yi Jin3c034c92017-12-22 17:36:47 -080020#include <errno.h>
Yi Jin4bab3a12018-01-10 16:50:59 -080021#include <sys/prctl.h>
Yi Jin3c034c92017-12-22 17:36:47 -080022#include <unistd.h>
23#include <wait.h>
24
25#include <memory>
26#include <mutex>
Joe Onorato1754d742016-11-21 17:51:35 -080027
Yi Jinb592e3b2018-02-01 15:17:04 -080028#include <android-base/file.h>
Yi Jinc23fad22017-09-15 17:24:59 -070029#include <android/util/protobuf.h>
Joe Onorato1754d742016-11-21 17:51:35 -080030#include <binder/IServiceManager.h>
Yi Jin3c034c92017-12-22 17:36:47 -080031#include <log/log_event_list.h>
Yi Jin3c034c92017-12-22 17:36:47 -080032#include <log/log_read.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080033#include <log/logprint.h>
Yi Jin3c034c92017-12-22 17:36:47 -080034#include <private/android_logger.h>
35
36#include "FdBuffer.h"
Yi Jin3c034c92017-12-22 17:36:47 -080037#include "Privacy.h"
38#include "PrivacyBuffer.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080039#include "frameworks/base/core/proto/android/util/log.proto.h"
40#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080041
Yi Jinb592e3b2018-02-01 15:17:04 -080042using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070043using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080044using namespace std;
45
Yi Jinc23fad22017-09-15 17:24:59 -070046// special section ids
47const int FIELD_ID_INCIDENT_HEADER = 1;
Yi Jin329130b2018-02-09 16:47:47 -080048const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070049
50// incident section parameters
Yi Jinb592e3b2018-02-01 15:17:04 -080051const int WAIT_MAX = 5;
Yi Jinb44f7d42017-07-21 12:12:59 -070052const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
Yi Jin3c034c92017-12-22 17:36:47 -080053const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jinb44f7d42017-07-21 12:12:59 -070054
Yi Jinb592e3b2018-02-01 15:17:04 -080055static pid_t fork_execute_incident_helper(const int id, const char* name, Fpipe& p2cPipe,
56 Fpipe& c2pPipe) {
57 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jinb44f7d42017-07-21 12:12:59 -070058 // fork used in multithreaded environment, avoid adding unnecessary code in child process
59 pid_t pid = fork();
60 if (pid == 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080061 if (TEMP_FAILURE_RETRY(dup2(p2cPipe.readFd(), STDIN_FILENO)) != 0 || !p2cPipe.close() ||
62 TEMP_FAILURE_RETRY(dup2(c2pPipe.writeFd(), STDOUT_FILENO)) != 1 || !c2pPipe.close()) {
Yi Jinb44f7d42017-07-21 12:12:59 -070063 ALOGW("%s can't setup stdin and stdout for incident helper", name);
64 _exit(EXIT_FAILURE);
65 }
66
Yi Jin4bab3a12018-01-10 16:50:59 -080067 /* make sure the child dies when incidentd dies */
68 prctl(PR_SET_PDEATHSIG, SIGKILL);
69
Yi Jinb44f7d42017-07-21 12:12:59 -070070 execv(INCIDENT_HELPER, const_cast<char**>(ihArgs));
71
72 ALOGW("%s failed in incident helper process: %s", name, strerror(errno));
Yi Jinb592e3b2018-02-01 15:17:04 -080073 _exit(EXIT_FAILURE); // always exits with failure if any
Yi Jinb44f7d42017-07-21 12:12:59 -070074 }
75 // close the fds used in incident helper
76 close(p2cPipe.readFd());
77 close(c2pPipe.writeFd());
78 return pid;
79}
80
Yi Jin99c248f2017-08-25 18:11:58 -070081// ================================================================================
Yi Jin4bab3a12018-01-10 16:50:59 -080082static status_t statusCode(int status) {
83 if (WIFSIGNALED(status)) {
Yi Jinb592e3b2018-02-01 15:17:04 -080084 VLOG("return by signal: %s", strerror(WTERMSIG(status)));
85 return -WTERMSIG(status);
Yi Jin4bab3a12018-01-10 16:50:59 -080086 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080087 VLOG("return by exit: %s", strerror(WEXITSTATUS(status)));
88 return -WEXITSTATUS(status);
Yi Jin4bab3a12018-01-10 16:50:59 -080089 }
90 return NO_ERROR;
91}
92
Yi Jinedfd5bb2017-09-06 17:09:11 -070093static status_t kill_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070094 int status;
Yi Jinb592e3b2018-02-01 15:17:04 -080095 VLOG("try to kill child process %d", pid);
Yi Jinb44f7d42017-07-21 12:12:59 -070096 kill(pid, SIGKILL);
97 if (waitpid(pid, &status, 0) == -1) return -1;
Yi Jin4bab3a12018-01-10 16:50:59 -080098 return statusCode(status);
Yi Jinb44f7d42017-07-21 12:12:59 -070099}
100
Yi Jinedfd5bb2017-09-06 17:09:11 -0700101static status_t wait_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700102 int status;
103 bool died = false;
104 // wait for child to report status up to 1 seconds
Yi Jinb592e3b2018-02-01 15:17:04 -0800105 for (int loop = 0; !died && loop < WAIT_MAX; loop++) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700106 if (waitpid(pid, &status, WNOHANG) == pid) died = true;
107 // sleep for 0.2 second
108 nanosleep(&WAIT_INTERVAL_NS, NULL);
109 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700110 if (!died) return kill_child(pid);
Yi Jin4bab3a12018-01-10 16:50:59 -0800111 return statusCode(status);
Yi Jinb44f7d42017-07-21 12:12:59 -0700112}
Joe Onorato1754d742016-11-21 17:51:35 -0800113// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800114static status_t write_section_header(int fd, int sectionId, size_t size) {
Yi Jin99c248f2017-08-25 18:11:58 -0700115 uint8_t buf[20];
Yi Jinb592e3b2018-02-01 15:17:04 -0800116 uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size);
117 return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno;
Yi Jin99c248f2017-08-25 18:11:58 -0700118}
119
Yi Jinb592e3b2018-02-01 15:17:04 -0800120static status_t write_report_requests(const int id, const FdBuffer& buffer,
121 ReportRequestSet* requests) {
Yi Jin0f047162017-09-05 13:44:22 -0700122 status_t err = -EBADF;
Yi Jinc23fad22017-09-15 17:24:59 -0700123 EncodedBuffer::iterator data = buffer.data();
124 PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data);
Yi Jin99c248f2017-08-25 18:11:58 -0700125 int writeable = 0;
Yi Jin329130b2018-02-09 16:47:47 -0800126 auto stats = requests->sectionStats(id);
127
128 stats->set_dump_size_bytes(data.size());
129 stats->set_dump_duration_ms(buffer.durationMs());
130 stats->set_timed_out(buffer.timedOut());
131 stats->set_is_truncated(buffer.truncated());
Yi Jin99c248f2017-08-25 18:11:58 -0700132
Yi Jin0f047162017-09-05 13:44:22 -0700133 // The streaming ones, group requests by spec in order to save unnecessary strip operations
134 map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800135 for (auto it = requests->begin(); it != requests->end(); it++) {
Yi Jin99c248f2017-08-25 18:11:58 -0700136 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700137 if (!request->ok() || !request->args.containsSection(id)) {
Yi Jin0f047162017-09-05 13:44:22 -0700138 continue; // skip invalid request
Yi Jin99c248f2017-08-25 18:11:58 -0700139 }
Yi Jin3ec5cc72018-01-26 13:42:43 -0800140 PrivacySpec spec = PrivacySpec::new_spec(request->args.dest());
Yi Jin0f047162017-09-05 13:44:22 -0700141 requestsBySpec[spec].push_back(request);
142 }
143
Yi Jin3ec5cc72018-01-26 13:42:43 -0800144 for (auto mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) {
Yi Jin0f047162017-09-05 13:44:22 -0700145 PrivacySpec spec = mit->first;
Yi Jinc23fad22017-09-15 17:24:59 -0700146 err = privacyBuffer.strip(spec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800147 if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700148 if (privacyBuffer.size() == 0) continue;
Yi Jin0f047162017-09-05 13:44:22 -0700149
Yi Jin3ec5cc72018-01-26 13:42:43 -0800150 for (auto it = mit->second.begin(); it != mit->second.end(); it++) {
Yi Jin0f047162017-09-05 13:44:22 -0700151 sp<ReportRequest> request = *it;
Yi Jinc23fad22017-09-15 17:24:59 -0700152 err = write_section_header(request->fd, id, privacyBuffer.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800153 if (err != NO_ERROR) {
154 request->err = err;
155 continue;
156 }
Yi Jinc23fad22017-09-15 17:24:59 -0700157 err = privacyBuffer.flush(request->fd);
Yi Jinb592e3b2018-02-01 15:17:04 -0800158 if (err != NO_ERROR) {
159 request->err = err;
160 continue;
161 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700162 writeable++;
Yi Jinb592e3b2018-02-01 15:17:04 -0800163 VLOG("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(),
164 request->fd, spec.dest);
Yi Jin0f047162017-09-05 13:44:22 -0700165 }
Yi Jinc23fad22017-09-15 17:24:59 -0700166 privacyBuffer.clear();
Yi Jin99c248f2017-08-25 18:11:58 -0700167 }
168
169 // The dropbox file
170 if (requests->mainFd() >= 0) {
Yi Jin329130b2018-02-09 16:47:47 -0800171 PrivacySpec spec = PrivacySpec::new_spec(requests->mainDest());
Yi Jin3ec5cc72018-01-26 13:42:43 -0800172 err = privacyBuffer.strip(spec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800173 if (err != NO_ERROR) return err; // the buffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700174 if (privacyBuffer.size() == 0) goto DONE;
Yi Jin0f047162017-09-05 13:44:22 -0700175
Yi Jinc23fad22017-09-15 17:24:59 -0700176 err = write_section_header(requests->mainFd(), id, privacyBuffer.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800177 if (err != NO_ERROR) {
178 requests->setMainFd(-1);
179 goto DONE;
180 }
Yi Jinc23fad22017-09-15 17:24:59 -0700181 err = privacyBuffer.flush(requests->mainFd());
Yi Jinb592e3b2018-02-01 15:17:04 -0800182 if (err != NO_ERROR) {
183 requests->setMainFd(-1);
184 goto DONE;
185 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700186 writeable++;
Yi Jinb592e3b2018-02-01 15:17:04 -0800187 VLOG("Section %d flushed %zu bytes to dropbox %d with spec %d", id, privacyBuffer.size(),
188 requests->mainFd(), spec.dest);
Yi Jin329130b2018-02-09 16:47:47 -0800189 stats->set_report_size_bytes(privacyBuffer.size());
Yi Jin99c248f2017-08-25 18:11:58 -0700190 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700191
192DONE:
Yi Jin99c248f2017-08-25 18:11:58 -0700193 // only returns error if there is no fd to write to.
194 return writeable > 0 ? NO_ERROR : err;
195}
196
197// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800198Section::Section(int i, const int64_t timeoutMs) : id(i), timeoutMs(timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800199
Yi Jinb592e3b2018-02-01 15:17:04 -0800200Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800201
Joe Onorato1754d742016-11-21 17:51:35 -0800202// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800203HeaderSection::HeaderSection() : Section(FIELD_ID_INCIDENT_HEADER, 0) {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700204
Yi Jinb592e3b2018-02-01 15:17:04 -0800205HeaderSection::~HeaderSection() {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700206
Yi Jinb592e3b2018-02-01 15:17:04 -0800207status_t HeaderSection::Execute(ReportRequestSet* requests) const {
208 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700209 const sp<ReportRequest> request = *it;
Yi Jinbdf58942017-11-14 17:58:19 -0800210 const vector<vector<uint8_t>>& headers = request->args.headers();
Yi Jinedfd5bb2017-09-06 17:09:11 -0700211
Yi Jinb592e3b2018-02-01 15:17:04 -0800212 for (vector<vector<uint8_t>>::const_iterator buf = headers.begin(); buf != headers.end();
213 buf++) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700214 if (buf->empty()) continue;
215
216 // So the idea is only requests with negative fd are written to dropbox file.
217 int fd = request->fd >= 0 ? request->fd : requests->mainFd();
Yi Jin329130b2018-02-09 16:47:47 -0800218 write_section_header(fd, id, buf->size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800219 WriteFully(fd, (uint8_t const*)buf->data(), buf->size());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700220 // If there was an error now, there will be an error later and we will remove
221 // it from the list then.
222 }
223 }
224 return NO_ERROR;
225}
Yi Jin329130b2018-02-09 16:47:47 -0800226// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800227MetadataSection::MetadataSection() : Section(FIELD_ID_INCIDENT_METADATA, 0) {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700228
Yi Jinb592e3b2018-02-01 15:17:04 -0800229MetadataSection::~MetadataSection() {}
Yi Jin329130b2018-02-09 16:47:47 -0800230
Yi Jinb592e3b2018-02-01 15:17:04 -0800231status_t MetadataSection::Execute(ReportRequestSet* requests) const {
Yi Jin329130b2018-02-09 16:47:47 -0800232 std::string metadataBuf;
233 requests->metadata().SerializeToString(&metadataBuf);
Yi Jinb592e3b2018-02-01 15:17:04 -0800234 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
Yi Jin329130b2018-02-09 16:47:47 -0800235 const sp<ReportRequest> request = *it;
236 if (metadataBuf.empty() || request->fd < 0 || request->err != NO_ERROR) {
237 continue;
238 }
239 write_section_header(request->fd, id, metadataBuf.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800240 if (!WriteFully(request->fd, (uint8_t const*)metadataBuf.data(), metadataBuf.size())) {
241 ALOGW("Failed to write metadata to fd %d", request->fd);
242 // we don't fail if we can't write to a single request's fd.
243 }
Yi Jin329130b2018-02-09 16:47:47 -0800244 }
245 if (requests->mainFd() >= 0 && !metadataBuf.empty()) {
246 write_section_header(requests->mainFd(), id, metadataBuf.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800247 if (!WriteFully(requests->mainFd(), (uint8_t const*)metadataBuf.data(), metadataBuf.size())) {
248 ALOGW("Failed to write metadata to dropbox fd %d", requests->mainFd());
249 return -1;
250 }
Yi Jin329130b2018-02-09 16:47:47 -0800251 }
252 return NO_ERROR;
253}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700254// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700255FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Yi Jinb592e3b2018-02-01 15:17:04 -0800256 : Section(id, timeoutMs), mFilename(filename) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700257 name = filename;
Yi Jin0eb22342017-11-06 17:17:27 -0800258 mIsSysfs = strncmp(filename, "/sys/", 5) == 0;
Yi Jin0a3406f2017-06-22 19:23:11 -0700259}
260
261FileSection::~FileSection() {}
262
Yi Jinb592e3b2018-02-01 15:17:04 -0800263status_t FileSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700264 // read from mFilename first, make sure the file is available
265 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700266 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700267 if (fd == -1) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800268 ALOGW("FileSection '%s' failed to open file", this->name.string());
269 return -errno;
Yi Jin0a3406f2017-06-22 19:23:11 -0700270 }
271
Yi Jinb44f7d42017-07-21 12:12:59 -0700272 FdBuffer buffer;
273 Fpipe p2cPipe;
274 Fpipe c2pPipe;
275 // initiate pipes to pass data to/from incident_helper
276 if (!p2cPipe.init() || !c2pPipe.init()) {
277 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700278 return -errno;
279 }
280
Yi Jinedfd5bb2017-09-06 17:09:11 -0700281 pid_t pid = fork_execute_incident_helper(this->id, this->name.string(), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700282 if (pid == -1) {
283 ALOGW("FileSection '%s' failed to fork", this->name.string());
284 return -errno;
285 }
286
287 // parent process
288 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800289 this->timeoutMs, mIsSysfs);
Yi Jinb44f7d42017-07-21 12:12:59 -0700290 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin4bab3a12018-01-10 16:50:59 -0800291 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800292 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800293 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700294 return readStatus;
295 }
296
Yi Jinedfd5bb2017-09-06 17:09:11 -0700297 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700298 if (ihStatus != NO_ERROR) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800299 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(),
300 strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700301 return ihStatus;
302 }
303
Yi Jinb592e3b2018-02-01 15:17:04 -0800304 VLOG("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
305 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700306 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700307 if (err != NO_ERROR) {
308 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
309 return err;
310 }
311
312 return NO_ERROR;
313}
314
315// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800316struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800317 const WorkerThreadSection* section;
318 int fds[2];
319
320 // Lock protects these fields
321 mutex lock;
322 bool workerDone;
323 status_t workerError;
324
325 WorkerThreadData(const WorkerThreadSection* section);
326 virtual ~WorkerThreadData();
327
328 int readFd() { return fds[0]; }
329 int writeFd() { return fds[1]; }
330};
331
332WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jinb592e3b2018-02-01 15:17:04 -0800333 : section(sec), workerDone(false), workerError(NO_ERROR) {
Joe Onorato1754d742016-11-21 17:51:35 -0800334 fds[0] = -1;
335 fds[1] = -1;
336}
337
Yi Jinb592e3b2018-02-01 15:17:04 -0800338WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800339
340// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800341WorkerThreadSection::WorkerThreadSection(int id) : Section(id) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800342
Yi Jinb592e3b2018-02-01 15:17:04 -0800343WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800344
Yi Jinb592e3b2018-02-01 15:17:04 -0800345static void* worker_thread_func(void* cookie) {
Joe Onorato1754d742016-11-21 17:51:35 -0800346 WorkerThreadData* data = (WorkerThreadData*)cookie;
347 status_t err = data->section->BlockingCall(data->writeFd());
348
349 {
350 unique_lock<mutex> lock(data->lock);
351 data->workerDone = true;
352 data->workerError = err;
353 }
354
355 close(data->writeFd());
356 data->decStrong(data->section);
357 // data might be gone now. don't use it after this point in this thread.
358 return NULL;
359}
360
Yi Jinb592e3b2018-02-01 15:17:04 -0800361status_t WorkerThreadSection::Execute(ReportRequestSet* requests) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800362 status_t err = NO_ERROR;
363 pthread_t thread;
364 pthread_attr_t attr;
365 bool timedOut = false;
366 FdBuffer buffer;
367
368 // Data shared between this thread and the worker thread.
369 sp<WorkerThreadData> data = new WorkerThreadData(this);
370
371 // Create the pipe
372 err = pipe(data->fds);
373 if (err != 0) {
374 return -errno;
375 }
376
377 // The worker thread needs a reference and we can't let the count go to zero
378 // if that thread is slow to start.
379 data->incStrong(this);
380
381 // Create the thread
382 err = pthread_attr_init(&attr);
383 if (err != 0) {
384 return -err;
385 }
386 // TODO: Do we need to tweak thread priority?
387 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
388 if (err != 0) {
389 pthread_attr_destroy(&attr);
390 return -err;
391 }
392 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
393 if (err != 0) {
394 pthread_attr_destroy(&attr);
395 return -err;
396 }
397 pthread_attr_destroy(&attr);
398
399 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700400 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800401 if (err != NO_ERROR) {
402 // TODO: Log this error into the incident report.
403 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800404 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800405 }
406
407 // Done with the read fd. The worker thread closes the write one so
408 // we never race and get here first.
409 close(data->readFd());
410
411 // If the worker side is finished, then return its error (which may overwrite
412 // our possible error -- but it's more interesting anyway). If not, then we timed out.
413 {
414 unique_lock<mutex> lock(data->lock);
415 if (!data->workerDone) {
416 // We timed out
417 timedOut = true;
418 } else {
419 if (data->workerError != NO_ERROR) {
420 err = data->workerError;
421 // TODO: Log this error into the incident report.
422 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800423 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800424 }
425 }
426 }
427
428 if (timedOut || buffer.timedOut()) {
429 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
430 return NO_ERROR;
431 }
432
433 if (buffer.truncated()) {
434 // TODO: Log this into the incident report.
435 }
436
437 // TODO: There was an error with the command or buffering. Report that. For now
438 // just exit with a log messasge.
439 if (err != NO_ERROR) {
440 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800441 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800442 return NO_ERROR;
443 }
444
445 // Write the data that was collected
Yi Jinb592e3b2018-02-01 15:17:04 -0800446 VLOG("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
447 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700448 err = write_report_requests(this->id, buffer, requests);
Joe Onorato1754d742016-11-21 17:51:35 -0800449 if (err != NO_ERROR) {
450 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
451 return err;
452 }
453
454 return NO_ERROR;
455}
456
457// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800458void CommandSection::init(const char* command, va_list args) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700459 va_list copied_args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700460 int numOfArgs = 0;
Yi Jin4ef28b72017-08-14 14:45:28 -0700461
462 va_copy(copied_args, args);
Yi Jinb592e3b2018-02-01 15:17:04 -0800463 while (va_arg(copied_args, const char*) != NULL) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700464 numOfArgs++;
465 }
Yi Jin4ef28b72017-08-14 14:45:28 -0700466 va_end(copied_args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700467
468 // allocate extra 1 for command and 1 for NULL terminator
469 mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2));
470
471 mCommand[0] = command;
472 name = command;
Yi Jinb592e3b2018-02-01 15:17:04 -0800473 for (int i = 0; i < numOfArgs; i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700474 const char* arg = va_arg(args, const char*);
Yi Jinb592e3b2018-02-01 15:17:04 -0800475 mCommand[i + 1] = arg;
Yi Jinb44f7d42017-07-21 12:12:59 -0700476 name += " ";
477 name += arg;
478 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800479 mCommand[numOfArgs + 1] = NULL;
Yi Jinb44f7d42017-07-21 12:12:59 -0700480}
481
482CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800483 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800484 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700485 va_start(args, command);
486 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800487 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700488}
Joe Onorato1754d742016-11-21 17:51:35 -0800489
Yi Jinb592e3b2018-02-01 15:17:04 -0800490CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700491 va_list args;
492 va_start(args, command);
493 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800494 va_end(args);
495}
496
Yi Jinb592e3b2018-02-01 15:17:04 -0800497CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800498
Yi Jinb592e3b2018-02-01 15:17:04 -0800499status_t CommandSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700500 FdBuffer buffer;
501 Fpipe cmdPipe;
502 Fpipe ihPipe;
503
504 if (!cmdPipe.init() || !ihPipe.init()) {
505 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
506 return -errno;
507 }
508
509 pid_t cmdPid = fork();
510 if (cmdPid == -1) {
511 ALOGW("CommandSection '%s' failed to fork", this->name.string());
512 return -errno;
513 }
514 // child process to execute the command as root
515 if (cmdPid == 0) {
516 // replace command's stdout with ihPipe's write Fd
517 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800518 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(),
519 strerror(errno));
Yi Jinb44f7d42017-07-21 12:12:59 -0700520 _exit(EXIT_FAILURE);
521 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800522 execvp(this->mCommand[0], (char* const*)this->mCommand);
523 int err = errno; // record command error code
524 ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(),
525 strerror(errno));
526 _exit(err); // exit with command error code
Yi Jinb44f7d42017-07-21 12:12:59 -0700527 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700528 pid_t ihPid = fork_execute_incident_helper(this->id, this->name.string(), cmdPipe, ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700529 if (ihPid == -1) {
530 ALOGW("CommandSection '%s' failed to fork", this->name.string());
531 return -errno;
532 }
533
534 close(cmdPipe.writeFd());
535 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
536 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin4bab3a12018-01-10 16:50:59 -0800537 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800538 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800539 kill_child(cmdPid);
540 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700541 return readStatus;
542 }
543
Yi Jinb592e3b2018-02-01 15:17:04 -0800544 // TODO: wait for command here has one trade-off: the failed status of command won't be detected
545 // until
Yi Jinb44f7d42017-07-21 12:12:59 -0700546 // buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700547 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800548 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700549 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800550 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident "
551 "helper: %s",
552 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700553 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
554 }
555
Yi Jinb592e3b2018-02-01 15:17:04 -0800556 VLOG("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
557 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700558 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jinb44f7d42017-07-21 12:12:59 -0700559 if (err != NO_ERROR) {
560 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
561 return err;
562 }
Joe Onorato1754d742016-11-21 17:51:35 -0800563 return NO_ERROR;
564}
565
566// ================================================================================
567DumpsysSection::DumpsysSection(int id, const char* service, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800568 : WorkerThreadSection(id), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800569 name = "dumpsys ";
570 name += service;
571
572 va_list args;
573 va_start(args, service);
574 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700575 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800576 if (arg == NULL) {
577 break;
578 }
579 mArgs.add(String16(arg));
580 name += " ";
581 name += arg;
582 }
583 va_end(args);
584}
585
Yi Jinb592e3b2018-02-01 15:17:04 -0800586DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800587
Yi Jinb592e3b2018-02-01 15:17:04 -0800588status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800589 // checkService won't wait for the service to show up like getService will.
590 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700591
Joe Onorato1754d742016-11-21 17:51:35 -0800592 if (service == NULL) {
593 // Returning an error interrupts the entire incident report, so just
594 // log the failure.
595 // TODO: have a meta record inside the report that would log this
596 // failure inside the report, because the fact that we can't find
597 // the service is good data in and of itself. This is running in
598 // another thread so lock that carefully...
599 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
600 return NO_ERROR;
601 }
602
603 service->dump(pipeWriteFd, mArgs);
604
605 return NO_ERROR;
606}
Yi Jin3c034c92017-12-22 17:36:47 -0800607
608// ================================================================================
609// initialization only once in Section.cpp.
610map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
611
Yi Jinb592e3b2018-02-01 15:17:04 -0800612LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
Yi Jin3c034c92017-12-22 17:36:47 -0800613 name += "logcat ";
614 name += android_log_id_to_name(logID);
615 switch (logID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800616 case LOG_ID_EVENTS:
617 case LOG_ID_STATS:
618 case LOG_ID_SECURITY:
619 mBinary = true;
620 break;
621 default:
622 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800623 }
624}
625
Yi Jinb592e3b2018-02-01 15:17:04 -0800626LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800627
Yi Jinb592e3b2018-02-01 15:17:04 -0800628static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800629 while (len > 0) {
630 char c = buf[len - 1];
631 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
632 len--;
633 } else {
634 break;
635 }
636 }
637 return len;
638}
639
640static inline int32_t get4LE(uint8_t const* src) {
641 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
642}
643
Yi Jinb592e3b2018-02-01 15:17:04 -0800644status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800645 status_t err = NO_ERROR;
646 // Open log buffer and getting logs since last retrieved time if any.
647 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800648 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
649 ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0)
650 : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK,
651 gLastLogsRetrieved[mLogID], 0),
652 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800653
654 if (android_logger_open(loggers.get(), mLogID) == NULL) {
655 ALOGW("LogSection %s: Can't get logger.", this->name.string());
656 return err;
657 }
658
659 log_msg msg;
660 log_time lastTimestamp(0);
661
662 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800663 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin3c034c92017-12-22 17:36:47 -0800664 status_t err = android_logger_list_read(loggers.get(), &msg);
665 // err = 0 - no content, unexpected connection drop or EOF.
666 // err = +ive number - size of retrieved data from logger
667 // err = -ive number, OS supplied error _except_ for -EAGAIN
668 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
669 if (err <= 0) {
670 if (err != -EAGAIN) {
671 ALOGE("LogSection %s: fails to read a log_msg.\n", this->name.string());
672 }
673 break;
674 }
675 if (mBinary) {
676 // remove the first uint32 which is tag's index in event log tags
677 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800678 msg.len() - sizeof(uint32_t));
679 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800680 android_log_list_element elem;
681
682 lastTimestamp.tv_sec = msg.entry_v1.sec;
683 lastTimestamp.tv_nsec = msg.entry_v1.nsec;
684
685 // format a BinaryLogEntry
686 long long token = proto.start(LogProto::BINARY_LOGS);
687 proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec);
688 proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800689 proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid);
Yi Jin3c034c92017-12-22 17:36:47 -0800690 proto.write(BinaryLogEntry::PID, msg.entry_v1.pid);
691 proto.write(BinaryLogEntry::TID, msg.entry_v1.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800692 proto.write(BinaryLogEntry::TAG_INDEX,
693 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800694 do {
695 elem = android_log_read_next(context);
696 long long elemToken = proto.start(BinaryLogEntry::ELEMS);
697 switch (elem.type) {
698 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800699 proto.write(BinaryLogEntry::Elem::TYPE,
700 BinaryLogEntry::Elem::EVENT_TYPE_INT);
701 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800702 break;
703 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800704 proto.write(BinaryLogEntry::Elem::TYPE,
705 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
706 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800707 break;
708 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800709 proto.write(BinaryLogEntry::Elem::TYPE,
710 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800711 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
712 break;
713 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800714 proto.write(BinaryLogEntry::Elem::TYPE,
715 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800716 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
717 break;
718 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800719 proto.write(BinaryLogEntry::Elem::TYPE,
720 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800721 break;
722 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800723 proto.write(BinaryLogEntry::Elem::TYPE,
724 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800725 break;
726 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800727 proto.write(BinaryLogEntry::Elem::TYPE,
728 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800729 break;
730 }
731 proto.end(elemToken);
732 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
733 proto.end(token);
734 if (context) {
735 android_log_destroy(&context);
736 }
737 } else {
738 AndroidLogEntry entry;
739 err = android_log_processLogBuffer(&msg.entry_v1, &entry);
740 if (err != NO_ERROR) {
741 ALOGE("LogSection %s: fails to process to an entry.\n", this->name.string());
742 break;
743 }
744 lastTimestamp.tv_sec = entry.tv_sec;
745 lastTimestamp.tv_nsec = entry.tv_nsec;
746
747 // format a TextLogEntry
748 long long token = proto.start(LogProto::TEXT_LOGS);
749 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
750 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
751 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
752 proto.write(TextLogEntry::UID, entry.uid);
753 proto.write(TextLogEntry::PID, entry.pid);
754 proto.write(TextLogEntry::TID, entry.tid);
755 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800756 proto.write(TextLogEntry::LOG, entry.message,
757 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800758 proto.end(token);
759 }
760 }
761 gLastLogsRetrieved[mLogID] = lastTimestamp;
762 proto.flush(pipeWriteFd);
763 return err;
764}