blob: 166fef08441acbe1f8bbf77cd5fff4d433f9cfac [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{
Yi Jin7e0b4e52017-09-12 20:00:25 -0700100 int l = 0;
101 int r = PRIVACY_POLICY_COUNT - 1;
102 while (l <= r) {
103 int mid = (l + r) >> 1;
104 const Privacy* p = PRIVACY_POLICY_LIST[mid];
105
106 if (p->field_id < (uint32_t)id) {
107 l = mid + 1;
108 } else if (p->field_id > (uint32_t)id) {
109 r = mid - 1;
110 } else {
111 return p;
112 }
Yi Jin99c248f2017-08-25 18:11:58 -0700113 }
114 return NULL;
115}
116
Yi Jinedfd5bb2017-09-06 17:09:11 -0700117// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700118static status_t
Yi Jinedfd5bb2017-09-06 17:09:11 -0700119write_section_header(int fd, int sectionId, size_t size)
Yi Jin99c248f2017-08-25 18:11:58 -0700120{
Yi Jin99c248f2017-08-25 18:11:58 -0700121 uint8_t buf[20];
Yi Jinedfd5bb2017-09-06 17:09:11 -0700122 uint8_t *p = write_length_delimited_tag_header(buf, sectionId, size);
123 return write_all(fd, buf, p-buf);
Yi Jin99c248f2017-08-25 18:11:58 -0700124}
125
126static status_t
Yi Jinedfd5bb2017-09-06 17:09:11 -0700127write_report_requests(const int id, const FdBuffer& buffer, ReportRequestSet* requests)
Yi Jin99c248f2017-08-25 18:11:58 -0700128{
Yi Jin0f047162017-09-05 13:44:22 -0700129 status_t err = -EBADF;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700130 EncodedBuffer encodedBuffer(buffer, get_privacy_of_section(id));
Yi Jin99c248f2017-08-25 18:11:58 -0700131 int writeable = 0;
132
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 Jin99c248f2017-08-25 18:11:58 -0700135 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
136 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 Jin0f047162017-09-05 13:44:22 -0700140 PrivacySpec spec = new_spec_from_args(request->args.dest());
141 requestsBySpec[spec].push_back(request);
142 }
143
144 for (map<PrivacySpec, vector<sp<ReportRequest>>>::iterator mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) {
145 PrivacySpec spec = mit->first;
146 err = encodedBuffer.strip(spec);
147 if (err != NO_ERROR) return err; // it means the encodedBuffer data is corrupted.
148 if (encodedBuffer.size() == 0) continue;
149
150 for (vector<sp<ReportRequest>>::iterator it = mit->second.begin(); it != mit->second.end(); it++) {
151 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700152 err = write_section_header(request->fd, id, encodedBuffer.size());
153 if (err != NO_ERROR) { request->err = err; continue; }
154 err = encodedBuffer.flush(request->fd);
155 if (err != NO_ERROR) { request->err = err; continue; }
156 writeable++;
Yi Jin0f047162017-09-05 13:44:22 -0700157 ALOGD("Section %d flushed %zu bytes to fd %d with spec %d", id, encodedBuffer.size(), request->fd, spec.dest);
158 }
159 encodedBuffer.clear();
Yi Jin99c248f2017-08-25 18:11:58 -0700160 }
161
162 // The dropbox file
163 if (requests->mainFd() >= 0) {
Yi Jin0f047162017-09-05 13:44:22 -0700164 err = encodedBuffer.strip(get_default_dropbox_spec());
165 if (err != NO_ERROR) return err; // the buffer data is corrupted.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700166 if (encodedBuffer.size() == 0) goto DONE;
Yi Jin0f047162017-09-05 13:44:22 -0700167
Yi Jinedfd5bb2017-09-06 17:09:11 -0700168 err = write_section_header(requests->mainFd(), id, encodedBuffer.size());
169 if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; }
170 err = encodedBuffer.flush(requests->mainFd());
171 if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; }
172 writeable++;
173 ALOGD("Section %d flushed %zu bytes to dropbox %d", id, encodedBuffer.size(), requests->mainFd());
Yi Jin99c248f2017-08-25 18:11:58 -0700174 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700175
176DONE:
Yi Jin99c248f2017-08-25 18:11:58 -0700177 // only returns error if there is no fd to write to.
178 return writeable > 0 ? NO_ERROR : err;
179}
180
181// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700182Section::Section(int i, const int64_t timeoutMs)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700183 :id(i),
184 timeoutMs(timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800185{
186}
187
188Section::~Section()
189{
190}
191
Joe Onorato1754d742016-11-21 17:51:35 -0800192// ================================================================================
Yi Jinedfd5bb2017-09-06 17:09:11 -0700193HeaderSection::HeaderSection()
194 :Section(FIELD_ID_INCIDENT_HEADER, 0)
195{
196}
197
198HeaderSection::~HeaderSection()
199{
200}
201
202status_t
203HeaderSection::Execute(ReportRequestSet* requests) const
204{
205 for (ReportRequestSet::iterator it=requests->begin(); it!=requests->end(); it++) {
206 const sp<ReportRequest> request = *it;
207 const vector<vector<int8_t>>& headers = request->args.headers();
208
209 for (vector<vector<int8_t>>::const_iterator buf=headers.begin(); buf!=headers.end(); buf++) {
210 if (buf->empty()) continue;
211
212 // So the idea is only requests with negative fd are written to dropbox file.
213 int fd = request->fd >= 0 ? request->fd : requests->mainFd();
214 write_section_header(fd, FIELD_ID_INCIDENT_HEADER, buf->size());
215 write_all(fd, (uint8_t const*)buf->data(), buf->size());
216 // If there was an error now, there will be an error later and we will remove
217 // it from the list then.
218 }
219 }
220 return NO_ERROR;
221}
222
223// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700224FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700225 :Section(id, timeoutMs),
226 mFilename(filename)
227{
Yi Jinb44f7d42017-07-21 12:12:59 -0700228 name = filename;
Yi Jin0a3406f2017-06-22 19:23:11 -0700229}
230
231FileSection::~FileSection() {}
232
Yi Jin99c248f2017-08-25 18:11:58 -0700233status_t
234FileSection::Execute(ReportRequestSet* requests) const
235{
Yi Jinb44f7d42017-07-21 12:12:59 -0700236 // read from mFilename first, make sure the file is available
237 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700238 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700239 if (fd == -1) {
240 ALOGW("FileSection '%s' failed to open file", this->name.string());
241 return -errno;
242 }
243
Yi Jinb44f7d42017-07-21 12:12:59 -0700244 FdBuffer buffer;
245 Fpipe p2cPipe;
246 Fpipe c2pPipe;
247 // initiate pipes to pass data to/from incident_helper
248 if (!p2cPipe.init() || !c2pPipe.init()) {
249 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700250 return -errno;
251 }
252
Yi Jinedfd5bb2017-09-06 17:09:11 -0700253 pid_t pid = fork_execute_incident_helper(this->id, this->name.string(), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700254 if (pid == -1) {
255 ALOGW("FileSection '%s' failed to fork", this->name.string());
256 return -errno;
257 }
258
259 // parent process
260 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
261 this->timeoutMs);
262 if (readStatus != NO_ERROR || buffer.timedOut()) {
263 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s, kill: %s",
264 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
Yi Jinedfd5bb2017-09-06 17:09:11 -0700265 strerror(-kill_child(pid)));
Yi Jinb44f7d42017-07-21 12:12:59 -0700266 return readStatus;
267 }
268
Yi Jinedfd5bb2017-09-06 17:09:11 -0700269 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700270 if (ihStatus != NO_ERROR) {
271 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-ihStatus));
272 return ihStatus;
273 }
274
275 ALOGD("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
Yi Jin0a3406f2017-06-22 19:23:11 -0700276 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700277 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700278 if (err != NO_ERROR) {
279 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
280 return err;
281 }
282
283 return NO_ERROR;
284}
285
286// ================================================================================
Joe Onorato1754d742016-11-21 17:51:35 -0800287struct WorkerThreadData : public virtual RefBase
288{
289 const WorkerThreadSection* section;
290 int fds[2];
291
292 // Lock protects these fields
293 mutex lock;
294 bool workerDone;
295 status_t workerError;
296
297 WorkerThreadData(const WorkerThreadSection* section);
298 virtual ~WorkerThreadData();
299
300 int readFd() { return fds[0]; }
301 int writeFd() { return fds[1]; }
302};
303
304WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
305 :section(sec),
306 workerDone(false),
307 workerError(NO_ERROR)
308{
309 fds[0] = -1;
310 fds[1] = -1;
311}
312
313WorkerThreadData::~WorkerThreadData()
314{
315}
316
317// ================================================================================
318WorkerThreadSection::WorkerThreadSection(int id)
319 :Section(id)
320{
321}
322
323WorkerThreadSection::~WorkerThreadSection()
324{
325}
326
327static void*
328worker_thread_func(void* cookie)
329{
330 WorkerThreadData* data = (WorkerThreadData*)cookie;
331 status_t err = data->section->BlockingCall(data->writeFd());
332
333 {
334 unique_lock<mutex> lock(data->lock);
335 data->workerDone = true;
336 data->workerError = err;
337 }
338
339 close(data->writeFd());
340 data->decStrong(data->section);
341 // data might be gone now. don't use it after this point in this thread.
342 return NULL;
343}
344
345status_t
346WorkerThreadSection::Execute(ReportRequestSet* requests) const
347{
348 status_t err = NO_ERROR;
349 pthread_t thread;
350 pthread_attr_t attr;
351 bool timedOut = false;
352 FdBuffer buffer;
353
354 // Data shared between this thread and the worker thread.
355 sp<WorkerThreadData> data = new WorkerThreadData(this);
356
357 // Create the pipe
358 err = pipe(data->fds);
359 if (err != 0) {
360 return -errno;
361 }
362
363 // The worker thread needs a reference and we can't let the count go to zero
364 // if that thread is slow to start.
365 data->incStrong(this);
366
367 // Create the thread
368 err = pthread_attr_init(&attr);
369 if (err != 0) {
370 return -err;
371 }
372 // TODO: Do we need to tweak thread priority?
373 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
374 if (err != 0) {
375 pthread_attr_destroy(&attr);
376 return -err;
377 }
378 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
379 if (err != 0) {
380 pthread_attr_destroy(&attr);
381 return -err;
382 }
383 pthread_attr_destroy(&attr);
384
385 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700386 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800387 if (err != NO_ERROR) {
388 // TODO: Log this error into the incident report.
389 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
390 strerror(-err));
391 }
392
393 // Done with the read fd. The worker thread closes the write one so
394 // we never race and get here first.
395 close(data->readFd());
396
397 // If the worker side is finished, then return its error (which may overwrite
398 // our possible error -- but it's more interesting anyway). If not, then we timed out.
399 {
400 unique_lock<mutex> lock(data->lock);
401 if (!data->workerDone) {
402 // We timed out
403 timedOut = true;
404 } else {
405 if (data->workerError != NO_ERROR) {
406 err = data->workerError;
407 // TODO: Log this error into the incident report.
408 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
409 strerror(-err));
410 }
411 }
412 }
413
414 if (timedOut || buffer.timedOut()) {
415 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
416 return NO_ERROR;
417 }
418
419 if (buffer.truncated()) {
420 // TODO: Log this into the incident report.
421 }
422
423 // TODO: There was an error with the command or buffering. Report that. For now
424 // just exit with a log messasge.
425 if (err != NO_ERROR) {
426 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
427 strerror(-err));
428 return NO_ERROR;
429 }
430
431 // Write the data that was collected
Yi Jinb44f7d42017-07-21 12:12:59 -0700432 ALOGD("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
Joe Onorato1754d742016-11-21 17:51:35 -0800433 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700434 err = write_report_requests(this->id, buffer, requests);
Joe Onorato1754d742016-11-21 17:51:35 -0800435 if (err != NO_ERROR) {
436 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
437 return err;
438 }
439
440 return NO_ERROR;
441}
442
443// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700444void
445CommandSection::init(const char* command, va_list args)
Yi Jinb44f7d42017-07-21 12:12:59 -0700446{
447 va_list copied_args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700448 int numOfArgs = 0;
Yi Jin4ef28b72017-08-14 14:45:28 -0700449
450 va_copy(copied_args, args);
451 while(va_arg(copied_args, const char*) != NULL) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700452 numOfArgs++;
453 }
Yi Jin4ef28b72017-08-14 14:45:28 -0700454 va_end(copied_args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700455
456 // allocate extra 1 for command and 1 for NULL terminator
457 mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2));
458
459 mCommand[0] = command;
460 name = command;
461 for (int i=0; i<numOfArgs; i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700462 const char* arg = va_arg(args, const char*);
Yi Jinb44f7d42017-07-21 12:12:59 -0700463 mCommand[i+1] = arg;
464 name += " ";
465 name += arg;
466 }
467 mCommand[numOfArgs+1] = NULL;
Yi Jinb44f7d42017-07-21 12:12:59 -0700468}
469
470CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700471 :Section(id, timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800472{
473 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700474 va_start(args, command);
475 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800476 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700477}
Joe Onorato1754d742016-11-21 17:51:35 -0800478
Yi Jinb44f7d42017-07-21 12:12:59 -0700479CommandSection::CommandSection(int id, const char* command, ...)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700480 :Section(id)
Yi Jinb44f7d42017-07-21 12:12:59 -0700481{
482 va_list args;
483 va_start(args, command);
484 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800485 va_end(args);
486}
487
488CommandSection::~CommandSection()
489{
Yi Jinb44f7d42017-07-21 12:12:59 -0700490 free(mCommand);
Joe Onorato1754d742016-11-21 17:51:35 -0800491}
492
493status_t
Yi Jinb44f7d42017-07-21 12:12:59 -0700494CommandSection::Execute(ReportRequestSet* requests) const
Joe Onorato1754d742016-11-21 17:51:35 -0800495{
Yi Jinb44f7d42017-07-21 12:12:59 -0700496 FdBuffer buffer;
497 Fpipe cmdPipe;
498 Fpipe ihPipe;
499
500 if (!cmdPipe.init() || !ihPipe.init()) {
501 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
502 return -errno;
503 }
504
505 pid_t cmdPid = fork();
506 if (cmdPid == -1) {
507 ALOGW("CommandSection '%s' failed to fork", this->name.string());
508 return -errno;
509 }
510 // child process to execute the command as root
511 if (cmdPid == 0) {
512 // replace command's stdout with ihPipe's write Fd
513 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
514 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno));
515 _exit(EXIT_FAILURE);
516 }
517 execv(this->mCommand[0], (char *const *) this->mCommand);
518 int err = errno; // record command error code
519 ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(), strerror(errno));
520 _exit(err); // exit with command error code
521 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700522 pid_t ihPid = fork_execute_incident_helper(this->id, this->name.string(), cmdPipe, ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700523 if (ihPid == -1) {
524 ALOGW("CommandSection '%s' failed to fork", this->name.string());
525 return -errno;
526 }
527
528 close(cmdPipe.writeFd());
529 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
530 if (readStatus != NO_ERROR || buffer.timedOut()) {
531 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, "
532 "timedout: %s, kill command: %s, kill incident helper: %s",
533 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
Yi Jinedfd5bb2017-09-06 17:09:11 -0700534 strerror(-kill_child(cmdPid)), strerror(-kill_child(ihPid)));
Yi Jinb44f7d42017-07-21 12:12:59 -0700535 return readStatus;
536 }
537
538 // TODO: wait for command here has one trade-off: the failed status of command won't be detected until
539 // buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700540 status_t cmdStatus = wait_child(cmdPid);
541 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700542 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinadd11e92017-07-30 16:10:07 -0700543 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb44f7d42017-07-21 12:12:59 -0700544 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
545 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
546 }
547
548 ALOGD("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
549 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700550 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jinb44f7d42017-07-21 12:12:59 -0700551 if (err != NO_ERROR) {
552 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
553 return err;
554 }
Joe Onorato1754d742016-11-21 17:51:35 -0800555 return NO_ERROR;
556}
557
558// ================================================================================
559DumpsysSection::DumpsysSection(int id, const char* service, ...)
560 :WorkerThreadSection(id),
561 mService(service)
562{
563 name = "dumpsys ";
564 name += service;
565
566 va_list args;
567 va_start(args, service);
568 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700569 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800570 if (arg == NULL) {
571 break;
572 }
573 mArgs.add(String16(arg));
574 name += " ";
575 name += arg;
576 }
577 va_end(args);
578}
579
580DumpsysSection::~DumpsysSection()
581{
582}
583
584status_t
585DumpsysSection::BlockingCall(int pipeWriteFd) const
586{
587 // checkService won't wait for the service to show up like getService will.
588 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700589
Joe Onorato1754d742016-11-21 17:51:35 -0800590 if (service == NULL) {
591 // Returning an error interrupts the entire incident report, so just
592 // log the failure.
593 // TODO: have a meta record inside the report that would log this
594 // failure inside the report, because the fact that we can't find
595 // the service is good data in and of itself. This is running in
596 // another thread so lock that carefully...
597 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
598 return NO_ERROR;
599 }
600
601 service->dump(pipeWriteFd, mArgs);
602
603 return NO_ERROR;
604}