blob: 6f052deaecf2c1ac9807bc12e5ce7af68a1a2342 [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 */
16
17#define LOG_TAG "incidentd"
18
Yi Jin99c248f2017-08-25 18:11:58 -070019#include "EncodedBuffer.h"
20#include "FdBuffer.h"
21#include "Privacy.h"
Joe Onorato1754d742016-11-21 17:51:35 -080022#include "Section.h"
Yi Jin99c248f2017-08-25 18:11:58 -070023
24#include "io_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080025#include "protobuf.h"
Yi Jin99c248f2017-08-25 18:11:58 -070026#include "section_list.h"
Joe Onorato1754d742016-11-21 17:51:35 -080027
Yi Jinb44f7d42017-07-21 12:12:59 -070028#include <private/android_filesystem_config.h>
Joe Onorato1754d742016-11-21 17:51:35 -080029#include <binder/IServiceManager.h>
Yi Jin0f047162017-09-05 13:44:22 -070030#include <map>
Joe Onorato1754d742016-11-21 17:51:35 -080031#include <mutex>
Yi Jin0a3406f2017-06-22 19:23:11 -070032#include <wait.h>
33#include <unistd.h>
Joe Onorato1754d742016-11-21 17:51:35 -080034
35using namespace std;
36
Yi Jinb44f7d42017-07-21 12:12:59 -070037const int WAIT_MAX = 5;
38const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
Yi Jin0a3406f2017-06-22 19:23:11 -070039const char* INCIDENT_HELPER = "/system/bin/incident_helper";
Yi Jinb44f7d42017-07-21 12:12:59 -070040
41static pid_t
Yi Jinedfd5bb2017-09-06 17:09:11 -070042fork_execute_incident_helper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe)
Yi Jinb44f7d42017-07-21 12:12:59 -070043{
Yi Jin0ed9b682017-08-18 14:51:20 -070044 const char* ihArgs[] { INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL };
Yi Jinb44f7d42017-07-21 12:12:59 -070045
46 // fork used in multithreaded environment, avoid adding unnecessary code in child process
47 pid_t pid = fork();
48 if (pid == 0) {
49 // child process executes incident helper as nobody
50 if (setgid(AID_NOBODY) == -1) {
51 ALOGW("%s can't change gid: %s", name, strerror(errno));
52 _exit(EXIT_FAILURE);
53 }
54 if (setuid(AID_NOBODY) == -1) {
55 ALOGW("%s can't change uid: %s", name, strerror(errno));
56 _exit(EXIT_FAILURE);
57 }
58
59 if (dup2(p2cPipe.readFd(), STDIN_FILENO) != 0 || !p2cPipe.close() ||
60 dup2(c2pPipe.writeFd(), STDOUT_FILENO) != 1 || !c2pPipe.close()) {
61 ALOGW("%s can't setup stdin and stdout for incident helper", name);
62 _exit(EXIT_FAILURE);
63 }
64
65 execv(INCIDENT_HELPER, const_cast<char**>(ihArgs));
66
67 ALOGW("%s failed in incident helper process: %s", name, strerror(errno));
68 _exit(EXIT_FAILURE); // always exits with failure if any
69 }
70 // close the fds used in incident helper
71 close(p2cPipe.readFd());
72 close(c2pPipe.writeFd());
73 return pid;
74}
75
Yi Jin99c248f2017-08-25 18:11:58 -070076// ================================================================================
Yi Jinedfd5bb2017-09-06 17:09:11 -070077static status_t kill_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070078 int status;
79 kill(pid, SIGKILL);
80 if (waitpid(pid, &status, 0) == -1) return -1;
81 return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status);
82}
83
Yi Jinedfd5bb2017-09-06 17:09:11 -070084static status_t wait_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070085 int status;
86 bool died = false;
87 // wait for child to report status up to 1 seconds
88 for(int loop = 0; !died && loop < WAIT_MAX; loop++) {
89 if (waitpid(pid, &status, WNOHANG) == pid) died = true;
90 // sleep for 0.2 second
91 nanosleep(&WAIT_INTERVAL_NS, NULL);
92 }
Yi Jinedfd5bb2017-09-06 17:09:11 -070093 if (!died) return kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -070094 return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status);
95}
Joe Onorato1754d742016-11-21 17:51:35 -080096// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -070097static const Privacy*
Yi Jinedfd5bb2017-09-06 17:09:11 -070098get_privacy_of_section(int id)
Yi Jin99c248f2017-08-25 18:11:58 -070099{
100 if (id < 0) return NULL;
101 int i=0;
102 while (PRIVACY_POLICY_LIST[i] != NULL) {
103 const Privacy* p = PRIVACY_POLICY_LIST[i];
104 if (p->field_id == (uint32_t)id) return p;
105 if (p->field_id > (uint32_t)id) return NULL;
106 i++;
107 }
108 return NULL;
109}
110
Yi Jinedfd5bb2017-09-06 17:09:11 -0700111// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700112static status_t
Yi Jinedfd5bb2017-09-06 17:09:11 -0700113write_section_header(int fd, int sectionId, size_t size)
Yi Jin99c248f2017-08-25 18:11:58 -0700114{
Yi Jin99c248f2017-08-25 18:11:58 -0700115 uint8_t buf[20];
Yi Jinedfd5bb2017-09-06 17:09:11 -0700116 uint8_t *p = write_length_delimited_tag_header(buf, sectionId, size);
117 return write_all(fd, buf, p-buf);
Yi Jin99c248f2017-08-25 18:11:58 -0700118}
119
120static status_t
Yi Jinedfd5bb2017-09-06 17:09:11 -0700121write_report_requests(const int id, const FdBuffer& buffer, ReportRequestSet* requests)
Yi Jin99c248f2017-08-25 18:11:58 -0700122{
Yi Jin0f047162017-09-05 13:44:22 -0700123 status_t err = -EBADF;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700124 EncodedBuffer encodedBuffer(buffer, get_privacy_of_section(id));
Yi Jin99c248f2017-08-25 18:11:58 -0700125 int writeable = 0;
126
Yi Jin0f047162017-09-05 13:44:22 -0700127 // The streaming ones, group requests by spec in order to save unnecessary strip operations
128 map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec;
Yi Jin99c248f2017-08-25 18:11:58 -0700129 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
130 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700131 if (!request->ok() || !request->args.containsSection(id)) {
Yi Jin0f047162017-09-05 13:44:22 -0700132 continue; // skip invalid request
Yi Jin99c248f2017-08-25 18:11:58 -0700133 }
Yi Jin0f047162017-09-05 13:44:22 -0700134 PrivacySpec spec = new_spec_from_args(request->args.dest());
135 requestsBySpec[spec].push_back(request);
136 }
137
138 for (map<PrivacySpec, vector<sp<ReportRequest>>>::iterator mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) {
139 PrivacySpec spec = mit->first;
140 err = encodedBuffer.strip(spec);
141 if (err != NO_ERROR) return err; // it means the encodedBuffer data is corrupted.
142 if (encodedBuffer.size() == 0) continue;
143
144 for (vector<sp<ReportRequest>>::iterator it = mit->second.begin(); it != mit->second.end(); it++) {
145 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700146 err = write_section_header(request->fd, id, encodedBuffer.size());
147 if (err != NO_ERROR) { request->err = err; continue; }
148 err = encodedBuffer.flush(request->fd);
149 if (err != NO_ERROR) { request->err = err; continue; }
150 writeable++;
Yi Jin0f047162017-09-05 13:44:22 -0700151 ALOGD("Section %d flushed %zu bytes to fd %d with spec %d", id, encodedBuffer.size(), request->fd, spec.dest);
152 }
153 encodedBuffer.clear();
Yi Jin99c248f2017-08-25 18:11:58 -0700154 }
155
156 // The dropbox file
157 if (requests->mainFd() >= 0) {
Yi Jin0f047162017-09-05 13:44:22 -0700158 err = encodedBuffer.strip(get_default_dropbox_spec());
159 if (err != NO_ERROR) return err; // the buffer data is corrupted.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700160 if (encodedBuffer.size() == 0) goto DONE;
Yi Jin0f047162017-09-05 13:44:22 -0700161
Yi Jinedfd5bb2017-09-06 17:09:11 -0700162 err = write_section_header(requests->mainFd(), id, encodedBuffer.size());
163 if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; }
164 err = encodedBuffer.flush(requests->mainFd());
165 if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; }
166 writeable++;
167 ALOGD("Section %d flushed %zu bytes to dropbox %d", id, encodedBuffer.size(), requests->mainFd());
Yi Jin99c248f2017-08-25 18:11:58 -0700168 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700169
170DONE:
Yi Jin99c248f2017-08-25 18:11:58 -0700171 // only returns error if there is no fd to write to.
172 return writeable > 0 ? NO_ERROR : err;
173}
174
175// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700176Section::Section(int i, const int64_t timeoutMs)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700177 :id(i),
178 timeoutMs(timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800179{
180}
181
182Section::~Section()
183{
184}
185
Joe Onorato1754d742016-11-21 17:51:35 -0800186// ================================================================================
Yi Jinedfd5bb2017-09-06 17:09:11 -0700187HeaderSection::HeaderSection()
188 :Section(FIELD_ID_INCIDENT_HEADER, 0)
189{
190}
191
192HeaderSection::~HeaderSection()
193{
194}
195
196status_t
197HeaderSection::Execute(ReportRequestSet* requests) const
198{
199 for (ReportRequestSet::iterator it=requests->begin(); it!=requests->end(); it++) {
200 const sp<ReportRequest> request = *it;
201 const vector<vector<int8_t>>& headers = request->args.headers();
202
203 for (vector<vector<int8_t>>::const_iterator buf=headers.begin(); buf!=headers.end(); buf++) {
204 if (buf->empty()) continue;
205
206 // So the idea is only requests with negative fd are written to dropbox file.
207 int fd = request->fd >= 0 ? request->fd : requests->mainFd();
208 write_section_header(fd, FIELD_ID_INCIDENT_HEADER, buf->size());
209 write_all(fd, (uint8_t const*)buf->data(), buf->size());
210 // If there was an error now, there will be an error later and we will remove
211 // it from the list then.
212 }
213 }
214 return NO_ERROR;
215}
216
217// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700218FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700219 :Section(id, timeoutMs),
220 mFilename(filename)
221{
Yi Jinb44f7d42017-07-21 12:12:59 -0700222 name = filename;
Yi Jin0a3406f2017-06-22 19:23:11 -0700223}
224
225FileSection::~FileSection() {}
226
Yi Jin99c248f2017-08-25 18:11:58 -0700227status_t
228FileSection::Execute(ReportRequestSet* requests) const
229{
Yi Jinb44f7d42017-07-21 12:12:59 -0700230 // read from mFilename first, make sure the file is available
231 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700232 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700233 if (fd == -1) {
234 ALOGW("FileSection '%s' failed to open file", this->name.string());
235 return -errno;
236 }
237
Yi Jinb44f7d42017-07-21 12:12:59 -0700238 FdBuffer buffer;
239 Fpipe p2cPipe;
240 Fpipe c2pPipe;
241 // initiate pipes to pass data to/from incident_helper
242 if (!p2cPipe.init() || !c2pPipe.init()) {
243 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700244 return -errno;
245 }
246
Yi Jinedfd5bb2017-09-06 17:09:11 -0700247 pid_t pid = fork_execute_incident_helper(this->id, this->name.string(), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700248 if (pid == -1) {
249 ALOGW("FileSection '%s' failed to fork", this->name.string());
250 return -errno;
251 }
252
253 // parent process
254 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
255 this->timeoutMs);
256 if (readStatus != NO_ERROR || buffer.timedOut()) {
257 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s, kill: %s",
258 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
Yi Jinedfd5bb2017-09-06 17:09:11 -0700259 strerror(-kill_child(pid)));
Yi Jinb44f7d42017-07-21 12:12:59 -0700260 return readStatus;
261 }
262
Yi Jinedfd5bb2017-09-06 17:09:11 -0700263 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700264 if (ihStatus != NO_ERROR) {
265 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-ihStatus));
266 return ihStatus;
267 }
268
269 ALOGD("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
Yi Jin0a3406f2017-06-22 19:23:11 -0700270 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700271 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700272 if (err != NO_ERROR) {
273 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
274 return err;
275 }
276
277 return NO_ERROR;
278}
279
280// ================================================================================
Joe Onorato1754d742016-11-21 17:51:35 -0800281struct WorkerThreadData : public virtual RefBase
282{
283 const WorkerThreadSection* section;
284 int fds[2];
285
286 // Lock protects these fields
287 mutex lock;
288 bool workerDone;
289 status_t workerError;
290
291 WorkerThreadData(const WorkerThreadSection* section);
292 virtual ~WorkerThreadData();
293
294 int readFd() { return fds[0]; }
295 int writeFd() { return fds[1]; }
296};
297
298WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
299 :section(sec),
300 workerDone(false),
301 workerError(NO_ERROR)
302{
303 fds[0] = -1;
304 fds[1] = -1;
305}
306
307WorkerThreadData::~WorkerThreadData()
308{
309}
310
311// ================================================================================
312WorkerThreadSection::WorkerThreadSection(int id)
313 :Section(id)
314{
315}
316
317WorkerThreadSection::~WorkerThreadSection()
318{
319}
320
321static void*
322worker_thread_func(void* cookie)
323{
324 WorkerThreadData* data = (WorkerThreadData*)cookie;
325 status_t err = data->section->BlockingCall(data->writeFd());
326
327 {
328 unique_lock<mutex> lock(data->lock);
329 data->workerDone = true;
330 data->workerError = err;
331 }
332
333 close(data->writeFd());
334 data->decStrong(data->section);
335 // data might be gone now. don't use it after this point in this thread.
336 return NULL;
337}
338
339status_t
340WorkerThreadSection::Execute(ReportRequestSet* requests) const
341{
342 status_t err = NO_ERROR;
343 pthread_t thread;
344 pthread_attr_t attr;
345 bool timedOut = false;
346 FdBuffer buffer;
347
348 // Data shared between this thread and the worker thread.
349 sp<WorkerThreadData> data = new WorkerThreadData(this);
350
351 // Create the pipe
352 err = pipe(data->fds);
353 if (err != 0) {
354 return -errno;
355 }
356
357 // The worker thread needs a reference and we can't let the count go to zero
358 // if that thread is slow to start.
359 data->incStrong(this);
360
361 // Create the thread
362 err = pthread_attr_init(&attr);
363 if (err != 0) {
364 return -err;
365 }
366 // TODO: Do we need to tweak thread priority?
367 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
368 if (err != 0) {
369 pthread_attr_destroy(&attr);
370 return -err;
371 }
372 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
373 if (err != 0) {
374 pthread_attr_destroy(&attr);
375 return -err;
376 }
377 pthread_attr_destroy(&attr);
378
379 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700380 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800381 if (err != NO_ERROR) {
382 // TODO: Log this error into the incident report.
383 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
384 strerror(-err));
385 }
386
387 // Done with the read fd. The worker thread closes the write one so
388 // we never race and get here first.
389 close(data->readFd());
390
391 // If the worker side is finished, then return its error (which may overwrite
392 // our possible error -- but it's more interesting anyway). If not, then we timed out.
393 {
394 unique_lock<mutex> lock(data->lock);
395 if (!data->workerDone) {
396 // We timed out
397 timedOut = true;
398 } else {
399 if (data->workerError != NO_ERROR) {
400 err = data->workerError;
401 // TODO: Log this error into the incident report.
402 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
403 strerror(-err));
404 }
405 }
406 }
407
408 if (timedOut || buffer.timedOut()) {
409 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
410 return NO_ERROR;
411 }
412
413 if (buffer.truncated()) {
414 // TODO: Log this into the incident report.
415 }
416
417 // TODO: There was an error with the command or buffering. Report that. For now
418 // just exit with a log messasge.
419 if (err != NO_ERROR) {
420 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
421 strerror(-err));
422 return NO_ERROR;
423 }
424
425 // Write the data that was collected
Yi Jinb44f7d42017-07-21 12:12:59 -0700426 ALOGD("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
Joe Onorato1754d742016-11-21 17:51:35 -0800427 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700428 err = write_report_requests(this->id, buffer, requests);
Joe Onorato1754d742016-11-21 17:51:35 -0800429 if (err != NO_ERROR) {
430 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
431 return err;
432 }
433
434 return NO_ERROR;
435}
436
437// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700438void
439CommandSection::init(const char* command, va_list args)
Yi Jinb44f7d42017-07-21 12:12:59 -0700440{
441 va_list copied_args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700442 int numOfArgs = 0;
Yi Jin4ef28b72017-08-14 14:45:28 -0700443
444 va_copy(copied_args, args);
445 while(va_arg(copied_args, const char*) != NULL) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700446 numOfArgs++;
447 }
Yi Jin4ef28b72017-08-14 14:45:28 -0700448 va_end(copied_args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700449
450 // allocate extra 1 for command and 1 for NULL terminator
451 mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2));
452
453 mCommand[0] = command;
454 name = command;
455 for (int i=0; i<numOfArgs; i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700456 const char* arg = va_arg(args, const char*);
Yi Jinb44f7d42017-07-21 12:12:59 -0700457 mCommand[i+1] = arg;
458 name += " ";
459 name += arg;
460 }
461 mCommand[numOfArgs+1] = NULL;
Yi Jinb44f7d42017-07-21 12:12:59 -0700462}
463
464CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700465 :Section(id, timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800466{
467 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700468 va_start(args, command);
469 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800470 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700471}
Joe Onorato1754d742016-11-21 17:51:35 -0800472
Yi Jinb44f7d42017-07-21 12:12:59 -0700473CommandSection::CommandSection(int id, const char* command, ...)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700474 :Section(id)
Yi Jinb44f7d42017-07-21 12:12:59 -0700475{
476 va_list args;
477 va_start(args, command);
478 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800479 va_end(args);
480}
481
482CommandSection::~CommandSection()
483{
Yi Jinb44f7d42017-07-21 12:12:59 -0700484 free(mCommand);
Joe Onorato1754d742016-11-21 17:51:35 -0800485}
486
487status_t
Yi Jinb44f7d42017-07-21 12:12:59 -0700488CommandSection::Execute(ReportRequestSet* requests) const
Joe Onorato1754d742016-11-21 17:51:35 -0800489{
Yi Jinb44f7d42017-07-21 12:12:59 -0700490 FdBuffer buffer;
491 Fpipe cmdPipe;
492 Fpipe ihPipe;
493
494 if (!cmdPipe.init() || !ihPipe.init()) {
495 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
496 return -errno;
497 }
498
499 pid_t cmdPid = fork();
500 if (cmdPid == -1) {
501 ALOGW("CommandSection '%s' failed to fork", this->name.string());
502 return -errno;
503 }
504 // child process to execute the command as root
505 if (cmdPid == 0) {
506 // replace command's stdout with ihPipe's write Fd
507 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
508 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno));
509 _exit(EXIT_FAILURE);
510 }
511 execv(this->mCommand[0], (char *const *) this->mCommand);
512 int err = errno; // record command error code
513 ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(), strerror(errno));
514 _exit(err); // exit with command error code
515 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700516 pid_t ihPid = fork_execute_incident_helper(this->id, this->name.string(), cmdPipe, ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700517 if (ihPid == -1) {
518 ALOGW("CommandSection '%s' failed to fork", this->name.string());
519 return -errno;
520 }
521
522 close(cmdPipe.writeFd());
523 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
524 if (readStatus != NO_ERROR || buffer.timedOut()) {
525 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, "
526 "timedout: %s, kill command: %s, kill incident helper: %s",
527 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
Yi Jinedfd5bb2017-09-06 17:09:11 -0700528 strerror(-kill_child(cmdPid)), strerror(-kill_child(ihPid)));
Yi Jinb44f7d42017-07-21 12:12:59 -0700529 return readStatus;
530 }
531
532 // TODO: wait for command here has one trade-off: the failed status of command won't be detected until
533 // buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700534 status_t cmdStatus = wait_child(cmdPid);
535 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700536 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinadd11e92017-07-30 16:10:07 -0700537 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb44f7d42017-07-21 12:12:59 -0700538 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
539 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
540 }
541
542 ALOGD("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
543 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700544 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jinb44f7d42017-07-21 12:12:59 -0700545 if (err != NO_ERROR) {
546 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
547 return err;
548 }
Joe Onorato1754d742016-11-21 17:51:35 -0800549 return NO_ERROR;
550}
551
552// ================================================================================
553DumpsysSection::DumpsysSection(int id, const char* service, ...)
554 :WorkerThreadSection(id),
555 mService(service)
556{
557 name = "dumpsys ";
558 name += service;
559
560 va_list args;
561 va_start(args, service);
562 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700563 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800564 if (arg == NULL) {
565 break;
566 }
567 mArgs.add(String16(arg));
568 name += " ";
569 name += arg;
570 }
571 va_end(args);
572}
573
574DumpsysSection::~DumpsysSection()
575{
576}
577
578status_t
579DumpsysSection::BlockingCall(int pipeWriteFd) const
580{
581 // checkService won't wait for the service to show up like getService will.
582 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700583
Joe Onorato1754d742016-11-21 17:51:35 -0800584 if (service == NULL) {
585 // Returning an error interrupts the entire incident report, so just
586 // log the failure.
587 // TODO: have a meta record inside the report that would log this
588 // failure inside the report, because the fact that we can't find
589 // the service is good data in and of itself. This is running in
590 // another thread so lock that carefully...
591 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
592 return NO_ERROR;
593 }
594
595 service->dump(pipeWriteFd, mArgs);
596
597 return NO_ERROR;
598}