blob: 64eae3acd34c1ae69827377c127c2ae45b7f890e [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 Jin4e843102018-02-14 15:36:18 -080016#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080017#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080018
19#include "Section.h"
Yi Jin99c248f2017-08-25 18:11:58 -070020
Yi Jin3c034c92017-12-22 17:36:47 -080021#include <errno.h>
Yi Jin4bab3a12018-01-10 16:50:59 -080022#include <sys/prctl.h>
Yi Jin3c034c92017-12-22 17:36:47 -080023#include <unistd.h>
24#include <wait.h>
25
26#include <memory>
27#include <mutex>
Joe Onorato1754d742016-11-21 17:51:35 -080028
Yi Jinb592e3b2018-02-01 15:17:04 -080029#include <android-base/file.h>
Yi Jinc23fad22017-09-15 17:24:59 -070030#include <android/util/protobuf.h>
Joe Onorato1754d742016-11-21 17:51:35 -080031#include <binder/IServiceManager.h>
Yi Jin3c034c92017-12-22 17:36:47 -080032#include <log/log_event_list.h>
Yi Jin3c034c92017-12-22 17:36:47 -080033#include <log/log_read.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080034#include <log/logprint.h>
Yi Jin3c034c92017-12-22 17:36:47 -080035#include <private/android_logger.h>
36
37#include "FdBuffer.h"
Yi Jin3c034c92017-12-22 17:36:47 -080038#include "Privacy.h"
39#include "PrivacyBuffer.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080040#include "frameworks/base/core/proto/android/util/log.proto.h"
41#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080042
Yi Jinb592e3b2018-02-01 15:17:04 -080043using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070044using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080045using namespace std;
46
Yi Jinc23fad22017-09-15 17:24:59 -070047// special section ids
48const int FIELD_ID_INCIDENT_HEADER = 1;
Yi Jin329130b2018-02-09 16:47:47 -080049const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070050
51// incident section parameters
Yi Jinb592e3b2018-02-01 15:17:04 -080052const int WAIT_MAX = 5;
Yi Jinb44f7d42017-07-21 12:12:59 -070053const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
Yi Jin3c034c92017-12-22 17:36:47 -080054const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jinb44f7d42017-07-21 12:12:59 -070055
Yi Jinb592e3b2018-02-01 15:17:04 -080056static pid_t fork_execute_incident_helper(const int id, const char* name, Fpipe& p2cPipe,
57 Fpipe& c2pPipe) {
58 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jinb44f7d42017-07-21 12:12:59 -070059 // fork used in multithreaded environment, avoid adding unnecessary code in child process
60 pid_t pid = fork();
61 if (pid == 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080062 if (TEMP_FAILURE_RETRY(dup2(p2cPipe.readFd(), STDIN_FILENO)) != 0 || !p2cPipe.close() ||
63 TEMP_FAILURE_RETRY(dup2(c2pPipe.writeFd(), STDOUT_FILENO)) != 1 || !c2pPipe.close()) {
Yi Jinb44f7d42017-07-21 12:12:59 -070064 ALOGW("%s can't setup stdin and stdout for incident helper", name);
65 _exit(EXIT_FAILURE);
66 }
67
Yi Jin4bab3a12018-01-10 16:50:59 -080068 /* make sure the child dies when incidentd dies */
69 prctl(PR_SET_PDEATHSIG, SIGKILL);
70
Yi Jinb44f7d42017-07-21 12:12:59 -070071 execv(INCIDENT_HELPER, const_cast<char**>(ihArgs));
72
73 ALOGW("%s failed in incident helper process: %s", name, strerror(errno));
Yi Jinb592e3b2018-02-01 15:17:04 -080074 _exit(EXIT_FAILURE); // always exits with failure if any
Yi Jinb44f7d42017-07-21 12:12:59 -070075 }
76 // close the fds used in incident helper
77 close(p2cPipe.readFd());
78 close(c2pPipe.writeFd());
79 return pid;
80}
81
Yi Jin99c248f2017-08-25 18:11:58 -070082// ================================================================================
Yi Jin4bab3a12018-01-10 16:50:59 -080083static status_t statusCode(int status) {
84 if (WIFSIGNALED(status)) {
Yi Jinb592e3b2018-02-01 15:17:04 -080085 VLOG("return by signal: %s", strerror(WTERMSIG(status)));
86 return -WTERMSIG(status);
Yi Jin4bab3a12018-01-10 16:50:59 -080087 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080088 VLOG("return by exit: %s", strerror(WEXITSTATUS(status)));
89 return -WEXITSTATUS(status);
Yi Jin4bab3a12018-01-10 16:50:59 -080090 }
91 return NO_ERROR;
92}
93
Yi Jinedfd5bb2017-09-06 17:09:11 -070094static status_t kill_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070095 int status;
Yi Jinb592e3b2018-02-01 15:17:04 -080096 VLOG("try to kill child process %d", pid);
Yi Jinb44f7d42017-07-21 12:12:59 -070097 kill(pid, SIGKILL);
98 if (waitpid(pid, &status, 0) == -1) return -1;
Yi Jin4bab3a12018-01-10 16:50:59 -080099 return statusCode(status);
Yi Jinb44f7d42017-07-21 12:12:59 -0700100}
101
Yi Jinedfd5bb2017-09-06 17:09:11 -0700102static status_t wait_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700103 int status;
104 bool died = false;
105 // wait for child to report status up to 1 seconds
Yi Jinb592e3b2018-02-01 15:17:04 -0800106 for (int loop = 0; !died && loop < WAIT_MAX; loop++) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700107 if (waitpid(pid, &status, WNOHANG) == pid) died = true;
108 // sleep for 0.2 second
109 nanosleep(&WAIT_INTERVAL_NS, NULL);
110 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700111 if (!died) return kill_child(pid);
Yi Jin4bab3a12018-01-10 16:50:59 -0800112 return statusCode(status);
Yi Jinb44f7d42017-07-21 12:12:59 -0700113}
Joe Onorato1754d742016-11-21 17:51:35 -0800114// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800115static status_t write_section_header(int fd, int sectionId, size_t size) {
Yi Jin99c248f2017-08-25 18:11:58 -0700116 uint8_t buf[20];
Yi Jinb592e3b2018-02-01 15:17:04 -0800117 uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size);
118 return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno;
Yi Jin99c248f2017-08-25 18:11:58 -0700119}
120
Yi Jinb592e3b2018-02-01 15:17:04 -0800121static status_t write_report_requests(const int id, const FdBuffer& buffer,
122 ReportRequestSet* requests) {
Yi Jin0f047162017-09-05 13:44:22 -0700123 status_t err = -EBADF;
Yi Jinc23fad22017-09-15 17:24:59 -0700124 EncodedBuffer::iterator data = buffer.data();
125 PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data);
Yi Jin99c248f2017-08-25 18:11:58 -0700126 int writeable = 0;
Yi Jin329130b2018-02-09 16:47:47 -0800127 auto stats = requests->sectionStats(id);
128
129 stats->set_dump_size_bytes(data.size());
130 stats->set_dump_duration_ms(buffer.durationMs());
131 stats->set_timed_out(buffer.timedOut());
132 stats->set_is_truncated(buffer.truncated());
Yi Jin99c248f2017-08-25 18:11:58 -0700133
Yi Jin0f047162017-09-05 13:44:22 -0700134 // The streaming ones, group requests by spec in order to save unnecessary strip operations
135 map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800136 for (auto it = requests->begin(); it != requests->end(); it++) {
Yi Jin99c248f2017-08-25 18:11:58 -0700137 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700138 if (!request->ok() || !request->args.containsSection(id)) {
Yi Jin0f047162017-09-05 13:44:22 -0700139 continue; // skip invalid request
Yi Jin99c248f2017-08-25 18:11:58 -0700140 }
Yi Jin3ec5cc72018-01-26 13:42:43 -0800141 PrivacySpec spec = PrivacySpec::new_spec(request->args.dest());
Yi Jin0f047162017-09-05 13:44:22 -0700142 requestsBySpec[spec].push_back(request);
143 }
144
Yi Jin3ec5cc72018-01-26 13:42:43 -0800145 for (auto mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) {
Yi Jin0f047162017-09-05 13:44:22 -0700146 PrivacySpec spec = mit->first;
Yi Jinc23fad22017-09-15 17:24:59 -0700147 err = privacyBuffer.strip(spec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800148 if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700149 if (privacyBuffer.size() == 0) continue;
Yi Jin0f047162017-09-05 13:44:22 -0700150
Yi Jin3ec5cc72018-01-26 13:42:43 -0800151 for (auto it = mit->second.begin(); it != mit->second.end(); it++) {
Yi Jin0f047162017-09-05 13:44:22 -0700152 sp<ReportRequest> request = *it;
Yi Jinc23fad22017-09-15 17:24:59 -0700153 err = write_section_header(request->fd, id, privacyBuffer.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800154 if (err != NO_ERROR) {
155 request->err = err;
156 continue;
157 }
Yi Jinc23fad22017-09-15 17:24:59 -0700158 err = privacyBuffer.flush(request->fd);
Yi Jinb592e3b2018-02-01 15:17:04 -0800159 if (err != NO_ERROR) {
160 request->err = err;
161 continue;
162 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700163 writeable++;
Yi Jinb592e3b2018-02-01 15:17:04 -0800164 VLOG("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(),
165 request->fd, spec.dest);
Yi Jin0f047162017-09-05 13:44:22 -0700166 }
Yi Jinc23fad22017-09-15 17:24:59 -0700167 privacyBuffer.clear();
Yi Jin99c248f2017-08-25 18:11:58 -0700168 }
169
170 // The dropbox file
171 if (requests->mainFd() >= 0) {
Yi Jin329130b2018-02-09 16:47:47 -0800172 PrivacySpec spec = PrivacySpec::new_spec(requests->mainDest());
Yi Jin3ec5cc72018-01-26 13:42:43 -0800173 err = privacyBuffer.strip(spec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800174 if (err != NO_ERROR) return err; // the buffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700175 if (privacyBuffer.size() == 0) goto DONE;
Yi Jin0f047162017-09-05 13:44:22 -0700176
Yi Jinc23fad22017-09-15 17:24:59 -0700177 err = write_section_header(requests->mainFd(), id, privacyBuffer.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800178 if (err != NO_ERROR) {
179 requests->setMainFd(-1);
180 goto DONE;
181 }
Yi Jinc23fad22017-09-15 17:24:59 -0700182 err = privacyBuffer.flush(requests->mainFd());
Yi Jinb592e3b2018-02-01 15:17:04 -0800183 if (err != NO_ERROR) {
184 requests->setMainFd(-1);
185 goto DONE;
186 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700187 writeable++;
Yi Jinb592e3b2018-02-01 15:17:04 -0800188 VLOG("Section %d flushed %zu bytes to dropbox %d with spec %d", id, privacyBuffer.size(),
189 requests->mainFd(), spec.dest);
Yi Jin329130b2018-02-09 16:47:47 -0800190 stats->set_report_size_bytes(privacyBuffer.size());
Yi Jin99c248f2017-08-25 18:11:58 -0700191 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700192
193DONE:
Yi Jin99c248f2017-08-25 18:11:58 -0700194 // only returns error if there is no fd to write to.
195 return writeable > 0 ? NO_ERROR : err;
196}
197
198// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800199Section::Section(int i, const int64_t timeoutMs) : id(i), timeoutMs(timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800200
Yi Jinb592e3b2018-02-01 15:17:04 -0800201Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800202
Joe Onorato1754d742016-11-21 17:51:35 -0800203// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800204HeaderSection::HeaderSection() : Section(FIELD_ID_INCIDENT_HEADER, 0) {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700205
Yi Jinb592e3b2018-02-01 15:17:04 -0800206HeaderSection::~HeaderSection() {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700207
Yi Jinb592e3b2018-02-01 15:17:04 -0800208status_t HeaderSection::Execute(ReportRequestSet* requests) const {
209 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700210 const sp<ReportRequest> request = *it;
Yi Jinbdf58942017-11-14 17:58:19 -0800211 const vector<vector<uint8_t>>& headers = request->args.headers();
Yi Jinedfd5bb2017-09-06 17:09:11 -0700212
Yi Jinb592e3b2018-02-01 15:17:04 -0800213 for (vector<vector<uint8_t>>::const_iterator buf = headers.begin(); buf != headers.end();
214 buf++) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700215 if (buf->empty()) continue;
216
217 // So the idea is only requests with negative fd are written to dropbox file.
218 int fd = request->fd >= 0 ? request->fd : requests->mainFd();
Yi Jin329130b2018-02-09 16:47:47 -0800219 write_section_header(fd, id, buf->size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800220 WriteFully(fd, (uint8_t const*)buf->data(), buf->size());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700221 // If there was an error now, there will be an error later and we will remove
222 // it from the list then.
223 }
224 }
225 return NO_ERROR;
226}
Yi Jin329130b2018-02-09 16:47:47 -0800227// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800228MetadataSection::MetadataSection() : Section(FIELD_ID_INCIDENT_METADATA, 0) {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700229
Yi Jinb592e3b2018-02-01 15:17:04 -0800230MetadataSection::~MetadataSection() {}
Yi Jin329130b2018-02-09 16:47:47 -0800231
Yi Jinb592e3b2018-02-01 15:17:04 -0800232status_t MetadataSection::Execute(ReportRequestSet* requests) const {
Yi Jin329130b2018-02-09 16:47:47 -0800233 std::string metadataBuf;
234 requests->metadata().SerializeToString(&metadataBuf);
Yi Jinb592e3b2018-02-01 15:17:04 -0800235 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
Yi Jin329130b2018-02-09 16:47:47 -0800236 const sp<ReportRequest> request = *it;
237 if (metadataBuf.empty() || request->fd < 0 || request->err != NO_ERROR) {
238 continue;
239 }
240 write_section_header(request->fd, id, metadataBuf.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800241 if (!WriteFully(request->fd, (uint8_t const*)metadataBuf.data(), metadataBuf.size())) {
242 ALOGW("Failed to write metadata to fd %d", request->fd);
243 // we don't fail if we can't write to a single request's fd.
244 }
Yi Jin329130b2018-02-09 16:47:47 -0800245 }
246 if (requests->mainFd() >= 0 && !metadataBuf.empty()) {
247 write_section_header(requests->mainFd(), id, metadataBuf.size());
Yi Jin4e843102018-02-14 15:36:18 -0800248 if (!WriteFully(requests->mainFd(), (uint8_t const*)metadataBuf.data(),
249 metadataBuf.size())) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800250 ALOGW("Failed to write metadata to dropbox fd %d", requests->mainFd());
251 return -1;
252 }
Yi Jin329130b2018-02-09 16:47:47 -0800253 }
254 return NO_ERROR;
255}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700256// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700257FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Yi Jinb592e3b2018-02-01 15:17:04 -0800258 : Section(id, timeoutMs), mFilename(filename) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700259 name = filename;
Yi Jin0eb22342017-11-06 17:17:27 -0800260 mIsSysfs = strncmp(filename, "/sys/", 5) == 0;
Yi Jin0a3406f2017-06-22 19:23:11 -0700261}
262
263FileSection::~FileSection() {}
264
Yi Jinb592e3b2018-02-01 15:17:04 -0800265status_t FileSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700266 // read from mFilename first, make sure the file is available
267 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700268 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700269 if (fd == -1) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800270 ALOGW("FileSection '%s' failed to open file", this->name.string());
271 return -errno;
Yi Jin0a3406f2017-06-22 19:23:11 -0700272 }
273
Yi Jinb44f7d42017-07-21 12:12:59 -0700274 FdBuffer buffer;
275 Fpipe p2cPipe;
276 Fpipe c2pPipe;
277 // initiate pipes to pass data to/from incident_helper
278 if (!p2cPipe.init() || !c2pPipe.init()) {
279 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700280 return -errno;
281 }
282
Yi Jinedfd5bb2017-09-06 17:09:11 -0700283 pid_t pid = fork_execute_incident_helper(this->id, this->name.string(), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700284 if (pid == -1) {
285 ALOGW("FileSection '%s' failed to fork", this->name.string());
286 return -errno;
287 }
288
289 // parent process
290 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800291 this->timeoutMs, mIsSysfs);
Yi Jinb44f7d42017-07-21 12:12:59 -0700292 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin4bab3a12018-01-10 16:50:59 -0800293 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800294 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800295 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700296 return readStatus;
297 }
298
Yi Jinedfd5bb2017-09-06 17:09:11 -0700299 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700300 if (ihStatus != NO_ERROR) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800301 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(),
302 strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700303 return ihStatus;
304 }
305
Yi Jinb592e3b2018-02-01 15:17:04 -0800306 VLOG("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
307 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700308 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700309 if (err != NO_ERROR) {
310 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
311 return err;
312 }
313
314 return NO_ERROR;
315}
316
317// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800318struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800319 const WorkerThreadSection* section;
320 int fds[2];
321
322 // Lock protects these fields
323 mutex lock;
324 bool workerDone;
325 status_t workerError;
326
327 WorkerThreadData(const WorkerThreadSection* section);
328 virtual ~WorkerThreadData();
329
330 int readFd() { return fds[0]; }
331 int writeFd() { return fds[1]; }
332};
333
334WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jinb592e3b2018-02-01 15:17:04 -0800335 : section(sec), workerDone(false), workerError(NO_ERROR) {
Joe Onorato1754d742016-11-21 17:51:35 -0800336 fds[0] = -1;
337 fds[1] = -1;
338}
339
Yi Jinb592e3b2018-02-01 15:17:04 -0800340WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800341
342// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800343WorkerThreadSection::WorkerThreadSection(int id) : Section(id) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800344
Yi Jinb592e3b2018-02-01 15:17:04 -0800345WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800346
Yi Jinb592e3b2018-02-01 15:17:04 -0800347static void* worker_thread_func(void* cookie) {
Joe Onorato1754d742016-11-21 17:51:35 -0800348 WorkerThreadData* data = (WorkerThreadData*)cookie;
349 status_t err = data->section->BlockingCall(data->writeFd());
350
351 {
352 unique_lock<mutex> lock(data->lock);
353 data->workerDone = true;
354 data->workerError = err;
355 }
356
357 close(data->writeFd());
358 data->decStrong(data->section);
359 // data might be gone now. don't use it after this point in this thread.
360 return NULL;
361}
362
Yi Jinb592e3b2018-02-01 15:17:04 -0800363status_t WorkerThreadSection::Execute(ReportRequestSet* requests) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800364 status_t err = NO_ERROR;
365 pthread_t thread;
366 pthread_attr_t attr;
367 bool timedOut = false;
368 FdBuffer buffer;
369
370 // Data shared between this thread and the worker thread.
371 sp<WorkerThreadData> data = new WorkerThreadData(this);
372
373 // Create the pipe
374 err = pipe(data->fds);
375 if (err != 0) {
376 return -errno;
377 }
378
379 // The worker thread needs a reference and we can't let the count go to zero
380 // if that thread is slow to start.
381 data->incStrong(this);
382
383 // Create the thread
384 err = pthread_attr_init(&attr);
385 if (err != 0) {
386 return -err;
387 }
388 // TODO: Do we need to tweak thread priority?
389 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
390 if (err != 0) {
391 pthread_attr_destroy(&attr);
392 return -err;
393 }
394 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
395 if (err != 0) {
396 pthread_attr_destroy(&attr);
397 return -err;
398 }
399 pthread_attr_destroy(&attr);
400
401 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700402 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800403 if (err != NO_ERROR) {
404 // TODO: Log this error into the incident report.
405 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800406 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800407 }
408
409 // Done with the read fd. The worker thread closes the write one so
410 // we never race and get here first.
411 close(data->readFd());
412
413 // If the worker side is finished, then return its error (which may overwrite
414 // our possible error -- but it's more interesting anyway). If not, then we timed out.
415 {
416 unique_lock<mutex> lock(data->lock);
417 if (!data->workerDone) {
418 // We timed out
419 timedOut = true;
420 } else {
421 if (data->workerError != NO_ERROR) {
422 err = data->workerError;
423 // TODO: Log this error into the incident report.
424 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800425 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800426 }
427 }
428 }
429
430 if (timedOut || buffer.timedOut()) {
431 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
432 return NO_ERROR;
433 }
434
435 if (buffer.truncated()) {
436 // TODO: Log this into the incident report.
437 }
438
439 // TODO: There was an error with the command or buffering. Report that. For now
440 // just exit with a log messasge.
441 if (err != NO_ERROR) {
442 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800443 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800444 return NO_ERROR;
445 }
446
447 // Write the data that was collected
Yi Jinb592e3b2018-02-01 15:17:04 -0800448 VLOG("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
449 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700450 err = write_report_requests(this->id, buffer, requests);
Joe Onorato1754d742016-11-21 17:51:35 -0800451 if (err != NO_ERROR) {
452 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
453 return err;
454 }
455
456 return NO_ERROR;
457}
458
459// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800460void CommandSection::init(const char* command, va_list args) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700461 va_list copied_args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700462 int numOfArgs = 0;
Yi Jin4ef28b72017-08-14 14:45:28 -0700463
464 va_copy(copied_args, args);
Yi Jinb592e3b2018-02-01 15:17:04 -0800465 while (va_arg(copied_args, const char*) != NULL) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700466 numOfArgs++;
467 }
Yi Jin4ef28b72017-08-14 14:45:28 -0700468 va_end(copied_args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700469
470 // allocate extra 1 for command and 1 for NULL terminator
471 mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2));
472
473 mCommand[0] = command;
474 name = command;
Yi Jinb592e3b2018-02-01 15:17:04 -0800475 for (int i = 0; i < numOfArgs; i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700476 const char* arg = va_arg(args, const char*);
Yi Jinb592e3b2018-02-01 15:17:04 -0800477 mCommand[i + 1] = arg;
Yi Jinb44f7d42017-07-21 12:12:59 -0700478 name += " ";
479 name += arg;
480 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800481 mCommand[numOfArgs + 1] = NULL;
Yi Jinb44f7d42017-07-21 12:12:59 -0700482}
483
484CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800485 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800486 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700487 va_start(args, command);
488 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800489 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700490}
Joe Onorato1754d742016-11-21 17:51:35 -0800491
Yi Jinb592e3b2018-02-01 15:17:04 -0800492CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700493 va_list args;
494 va_start(args, command);
495 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800496 va_end(args);
497}
498
Yi Jinb592e3b2018-02-01 15:17:04 -0800499CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800500
Yi Jinb592e3b2018-02-01 15:17:04 -0800501status_t CommandSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700502 FdBuffer buffer;
503 Fpipe cmdPipe;
504 Fpipe ihPipe;
505
506 if (!cmdPipe.init() || !ihPipe.init()) {
507 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
508 return -errno;
509 }
510
511 pid_t cmdPid = fork();
512 if (cmdPid == -1) {
513 ALOGW("CommandSection '%s' failed to fork", this->name.string());
514 return -errno;
515 }
516 // child process to execute the command as root
517 if (cmdPid == 0) {
518 // replace command's stdout with ihPipe's write Fd
519 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800520 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(),
521 strerror(errno));
Yi Jinb44f7d42017-07-21 12:12:59 -0700522 _exit(EXIT_FAILURE);
523 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800524 execvp(this->mCommand[0], (char* const*)this->mCommand);
525 int err = errno; // record command error code
526 ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(),
527 strerror(errno));
528 _exit(err); // exit with command error code
Yi Jinb44f7d42017-07-21 12:12:59 -0700529 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700530 pid_t ihPid = fork_execute_incident_helper(this->id, this->name.string(), cmdPipe, ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700531 if (ihPid == -1) {
532 ALOGW("CommandSection '%s' failed to fork", this->name.string());
533 return -errno;
534 }
535
536 close(cmdPipe.writeFd());
537 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
538 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin4bab3a12018-01-10 16:50:59 -0800539 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800540 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800541 kill_child(cmdPid);
542 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700543 return readStatus;
544 }
545
Yi Jinb592e3b2018-02-01 15:17:04 -0800546 // TODO: wait for command here has one trade-off: the failed status of command won't be detected
547 // until
Yi Jinb44f7d42017-07-21 12:12:59 -0700548 // buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700549 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800550 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700551 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800552 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident "
553 "helper: %s",
554 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700555 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
556 }
557
Yi Jinb592e3b2018-02-01 15:17:04 -0800558 VLOG("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
559 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700560 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jinb44f7d42017-07-21 12:12:59 -0700561 if (err != NO_ERROR) {
562 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
563 return err;
564 }
Joe Onorato1754d742016-11-21 17:51:35 -0800565 return NO_ERROR;
566}
567
568// ================================================================================
569DumpsysSection::DumpsysSection(int id, const char* service, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800570 : WorkerThreadSection(id), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800571 name = "dumpsys ";
572 name += service;
573
574 va_list args;
575 va_start(args, service);
576 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700577 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800578 if (arg == NULL) {
579 break;
580 }
581 mArgs.add(String16(arg));
582 name += " ";
583 name += arg;
584 }
585 va_end(args);
586}
587
Yi Jinb592e3b2018-02-01 15:17:04 -0800588DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800589
Yi Jinb592e3b2018-02-01 15:17:04 -0800590status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800591 // checkService won't wait for the service to show up like getService will.
592 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700593
Joe Onorato1754d742016-11-21 17:51:35 -0800594 if (service == NULL) {
595 // Returning an error interrupts the entire incident report, so just
596 // log the failure.
597 // TODO: have a meta record inside the report that would log this
598 // failure inside the report, because the fact that we can't find
599 // the service is good data in and of itself. This is running in
600 // another thread so lock that carefully...
601 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
602 return NO_ERROR;
603 }
604
605 service->dump(pipeWriteFd, mArgs);
606
607 return NO_ERROR;
608}
Yi Jin3c034c92017-12-22 17:36:47 -0800609
610// ================================================================================
611// initialization only once in Section.cpp.
612map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
613
Yi Jinb592e3b2018-02-01 15:17:04 -0800614LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
Yi Jin3c034c92017-12-22 17:36:47 -0800615 name += "logcat ";
616 name += android_log_id_to_name(logID);
617 switch (logID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800618 case LOG_ID_EVENTS:
619 case LOG_ID_STATS:
620 case LOG_ID_SECURITY:
621 mBinary = true;
622 break;
623 default:
624 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800625 }
626}
627
Yi Jinb592e3b2018-02-01 15:17:04 -0800628LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800629
Yi Jinb592e3b2018-02-01 15:17:04 -0800630static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800631 while (len > 0) {
632 char c = buf[len - 1];
633 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
634 len--;
635 } else {
636 break;
637 }
638 }
639 return len;
640}
641
642static inline int32_t get4LE(uint8_t const* src) {
643 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
644}
645
Yi Jinb592e3b2018-02-01 15:17:04 -0800646status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800647 status_t err = NO_ERROR;
648 // Open log buffer and getting logs since last retrieved time if any.
649 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800650 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
651 ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0)
652 : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK,
653 gLastLogsRetrieved[mLogID], 0),
654 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800655
656 if (android_logger_open(loggers.get(), mLogID) == NULL) {
657 ALOGW("LogSection %s: Can't get logger.", this->name.string());
658 return err;
659 }
660
661 log_msg msg;
662 log_time lastTimestamp(0);
663
664 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800665 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin3c034c92017-12-22 17:36:47 -0800666 status_t err = android_logger_list_read(loggers.get(), &msg);
667 // err = 0 - no content, unexpected connection drop or EOF.
668 // err = +ive number - size of retrieved data from logger
669 // err = -ive number, OS supplied error _except_ for -EAGAIN
670 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
671 if (err <= 0) {
672 if (err != -EAGAIN) {
673 ALOGE("LogSection %s: fails to read a log_msg.\n", this->name.string());
674 }
675 break;
676 }
677 if (mBinary) {
678 // remove the first uint32 which is tag's index in event log tags
679 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800680 msg.len() - sizeof(uint32_t));
681 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800682 android_log_list_element elem;
683
684 lastTimestamp.tv_sec = msg.entry_v1.sec;
685 lastTimestamp.tv_nsec = msg.entry_v1.nsec;
686
687 // format a BinaryLogEntry
688 long long token = proto.start(LogProto::BINARY_LOGS);
689 proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec);
690 proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800691 proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid);
Yi Jin3c034c92017-12-22 17:36:47 -0800692 proto.write(BinaryLogEntry::PID, msg.entry_v1.pid);
693 proto.write(BinaryLogEntry::TID, msg.entry_v1.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800694 proto.write(BinaryLogEntry::TAG_INDEX,
695 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800696 do {
697 elem = android_log_read_next(context);
698 long long elemToken = proto.start(BinaryLogEntry::ELEMS);
699 switch (elem.type) {
700 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800701 proto.write(BinaryLogEntry::Elem::TYPE,
702 BinaryLogEntry::Elem::EVENT_TYPE_INT);
703 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800704 break;
705 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800706 proto.write(BinaryLogEntry::Elem::TYPE,
707 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
708 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800709 break;
710 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800711 proto.write(BinaryLogEntry::Elem::TYPE,
712 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800713 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
714 break;
715 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800716 proto.write(BinaryLogEntry::Elem::TYPE,
717 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800718 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
719 break;
720 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800721 proto.write(BinaryLogEntry::Elem::TYPE,
722 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800723 break;
724 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800725 proto.write(BinaryLogEntry::Elem::TYPE,
726 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800727 break;
728 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800729 proto.write(BinaryLogEntry::Elem::TYPE,
730 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800731 break;
732 }
733 proto.end(elemToken);
734 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
735 proto.end(token);
736 if (context) {
737 android_log_destroy(&context);
738 }
739 } else {
740 AndroidLogEntry entry;
741 err = android_log_processLogBuffer(&msg.entry_v1, &entry);
742 if (err != NO_ERROR) {
743 ALOGE("LogSection %s: fails to process to an entry.\n", this->name.string());
744 break;
745 }
746 lastTimestamp.tv_sec = entry.tv_sec;
747 lastTimestamp.tv_nsec = entry.tv_nsec;
748
749 // format a TextLogEntry
750 long long token = proto.start(LogProto::TEXT_LOGS);
751 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
752 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
753 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
754 proto.write(TextLogEntry::UID, entry.uid);
755 proto.write(TextLogEntry::PID, entry.pid);
756 proto.write(TextLogEntry::TID, entry.tid);
757 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800758 proto.write(TextLogEntry::LOG, entry.message,
759 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800760 proto.end(token);
761 }
762 }
763 gLastLogsRetrieved[mLogID] = lastTimestamp;
764 proto.flush(pipeWriteFd);
765 return err;
766}