blob: c08b9ea80c0407141a39c5bc81a596a8ef6e6ebc [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 "FdBuffer.h"
20#include "Privacy.h"
Yi Jinc23fad22017-09-15 17:24:59 -070021#include "PrivacyBuffer.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"
Yi Jin99c248f2017-08-25 18:11:58 -070025#include "section_list.h"
Joe Onorato1754d742016-11-21 17:51:35 -080026
Yi Jinc23fad22017-09-15 17:24:59 -070027#include <android/util/protobuf.h>
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
Yi Jinc23fad22017-09-15 17:24:59 -070035using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080036using namespace std;
37
Yi Jinc23fad22017-09-15 17:24:59 -070038// special section ids
39const int FIELD_ID_INCIDENT_HEADER = 1;
40
41// incident section parameters
Yi Jinb44f7d42017-07-21 12:12:59 -070042const int WAIT_MAX = 5;
43const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
Yi Jin0a3406f2017-06-22 19:23:11 -070044const char* INCIDENT_HELPER = "/system/bin/incident_helper";
Yi Jinb44f7d42017-07-21 12:12:59 -070045
46static pid_t
Yi Jinedfd5bb2017-09-06 17:09:11 -070047fork_execute_incident_helper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe)
Yi Jinb44f7d42017-07-21 12:12:59 -070048{
Yi Jin0ed9b682017-08-18 14:51:20 -070049 const char* ihArgs[] { INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL };
Yi Jinb44f7d42017-07-21 12:12:59 -070050
51 // fork used in multithreaded environment, avoid adding unnecessary code in child process
52 pid_t pid = fork();
53 if (pid == 0) {
54 // child process executes incident helper as nobody
55 if (setgid(AID_NOBODY) == -1) {
56 ALOGW("%s can't change gid: %s", name, strerror(errno));
57 _exit(EXIT_FAILURE);
58 }
59 if (setuid(AID_NOBODY) == -1) {
60 ALOGW("%s can't change uid: %s", name, strerror(errno));
61 _exit(EXIT_FAILURE);
62 }
63
64 if (dup2(p2cPipe.readFd(), STDIN_FILENO) != 0 || !p2cPipe.close() ||
65 dup2(c2pPipe.writeFd(), STDOUT_FILENO) != 1 || !c2pPipe.close()) {
66 ALOGW("%s can't setup stdin and stdout for incident helper", name);
67 _exit(EXIT_FAILURE);
68 }
69
70 execv(INCIDENT_HELPER, const_cast<char**>(ihArgs));
71
72 ALOGW("%s failed in incident helper process: %s", name, strerror(errno));
73 _exit(EXIT_FAILURE); // always exits with failure if any
74 }
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 Jinedfd5bb2017-09-06 17:09:11 -070082static status_t kill_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070083 int status;
84 kill(pid, SIGKILL);
85 if (waitpid(pid, &status, 0) == -1) return -1;
86 return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status);
87}
88
Yi Jinedfd5bb2017-09-06 17:09:11 -070089static status_t wait_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070090 int status;
91 bool died = false;
92 // wait for child to report status up to 1 seconds
93 for(int loop = 0; !died && loop < WAIT_MAX; loop++) {
94 if (waitpid(pid, &status, WNOHANG) == pid) died = true;
95 // sleep for 0.2 second
96 nanosleep(&WAIT_INTERVAL_NS, NULL);
97 }
Yi Jinedfd5bb2017-09-06 17:09:11 -070098 if (!died) return kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -070099 return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status);
100}
Joe Onorato1754d742016-11-21 17:51:35 -0800101// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700102static const Privacy*
Yi Jinedfd5bb2017-09-06 17:09:11 -0700103get_privacy_of_section(int id)
Yi Jin99c248f2017-08-25 18:11:58 -0700104{
Yi Jin7e0b4e52017-09-12 20:00:25 -0700105 int l = 0;
106 int r = PRIVACY_POLICY_COUNT - 1;
107 while (l <= r) {
108 int mid = (l + r) >> 1;
109 const Privacy* p = PRIVACY_POLICY_LIST[mid];
110
111 if (p->field_id < (uint32_t)id) {
112 l = mid + 1;
113 } else if (p->field_id > (uint32_t)id) {
114 r = mid - 1;
115 } else {
116 return p;
117 }
Yi Jin99c248f2017-08-25 18:11:58 -0700118 }
119 return NULL;
120}
121
Yi Jinedfd5bb2017-09-06 17:09:11 -0700122// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700123static status_t
Yi Jinedfd5bb2017-09-06 17:09:11 -0700124write_section_header(int fd, int sectionId, size_t size)
Yi Jin99c248f2017-08-25 18:11:58 -0700125{
Yi Jin99c248f2017-08-25 18:11:58 -0700126 uint8_t buf[20];
Yi Jinedfd5bb2017-09-06 17:09:11 -0700127 uint8_t *p = write_length_delimited_tag_header(buf, sectionId, size);
128 return write_all(fd, buf, p-buf);
Yi Jin99c248f2017-08-25 18:11:58 -0700129}
130
131static status_t
Yi Jinedfd5bb2017-09-06 17:09:11 -0700132write_report_requests(const int id, const FdBuffer& buffer, ReportRequestSet* requests)
Yi Jin99c248f2017-08-25 18:11:58 -0700133{
Yi Jin0f047162017-09-05 13:44:22 -0700134 status_t err = -EBADF;
Yi Jinc23fad22017-09-15 17:24:59 -0700135 EncodedBuffer::iterator data = buffer.data();
136 PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data);
Yi Jin99c248f2017-08-25 18:11:58 -0700137 int writeable = 0;
138
Yi Jin0f047162017-09-05 13:44:22 -0700139 // The streaming ones, group requests by spec in order to save unnecessary strip operations
140 map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec;
Yi Jin99c248f2017-08-25 18:11:58 -0700141 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
142 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700143 if (!request->ok() || !request->args.containsSection(id)) {
Yi Jin0f047162017-09-05 13:44:22 -0700144 continue; // skip invalid request
Yi Jin99c248f2017-08-25 18:11:58 -0700145 }
Yi Jin0f047162017-09-05 13:44:22 -0700146 PrivacySpec spec = new_spec_from_args(request->args.dest());
147 requestsBySpec[spec].push_back(request);
148 }
149
150 for (map<PrivacySpec, vector<sp<ReportRequest>>>::iterator mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) {
151 PrivacySpec spec = mit->first;
Yi Jinc23fad22017-09-15 17:24:59 -0700152 err = privacyBuffer.strip(spec);
153 if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted.
154 if (privacyBuffer.size() == 0) continue;
Yi Jin0f047162017-09-05 13:44:22 -0700155
156 for (vector<sp<ReportRequest>>::iterator it = mit->second.begin(); it != mit->second.end(); it++) {
157 sp<ReportRequest> request = *it;
Yi Jinc23fad22017-09-15 17:24:59 -0700158 err = write_section_header(request->fd, id, privacyBuffer.size());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700159 if (err != NO_ERROR) { request->err = err; continue; }
Yi Jinc23fad22017-09-15 17:24:59 -0700160 err = privacyBuffer.flush(request->fd);
Yi Jinedfd5bb2017-09-06 17:09:11 -0700161 if (err != NO_ERROR) { request->err = err; continue; }
162 writeable++;
Yi Jinc23fad22017-09-15 17:24:59 -0700163 ALOGD("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(), request->fd, spec.dest);
Yi Jin0f047162017-09-05 13:44:22 -0700164 }
Yi Jinc23fad22017-09-15 17:24:59 -0700165 privacyBuffer.clear();
Yi Jin99c248f2017-08-25 18:11:58 -0700166 }
167
168 // The dropbox file
169 if (requests->mainFd() >= 0) {
Yi Jinc23fad22017-09-15 17:24:59 -0700170 err = privacyBuffer.strip(get_default_dropbox_spec());
Yi Jin0f047162017-09-05 13:44:22 -0700171 if (err != NO_ERROR) return err; // the buffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700172 if (privacyBuffer.size() == 0) goto DONE;
Yi Jin0f047162017-09-05 13:44:22 -0700173
Yi Jinc23fad22017-09-15 17:24:59 -0700174 err = write_section_header(requests->mainFd(), id, privacyBuffer.size());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700175 if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; }
Yi Jinc23fad22017-09-15 17:24:59 -0700176 err = privacyBuffer.flush(requests->mainFd());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700177 if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; }
178 writeable++;
Yi Jinc23fad22017-09-15 17:24:59 -0700179 ALOGD("Section %d flushed %zu bytes to dropbox %d", id, privacyBuffer.size(), requests->mainFd());
Yi Jin99c248f2017-08-25 18:11:58 -0700180 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700181
182DONE:
Yi Jin99c248f2017-08-25 18:11:58 -0700183 // only returns error if there is no fd to write to.
184 return writeable > 0 ? NO_ERROR : err;
185}
186
187// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700188Section::Section(int i, const int64_t timeoutMs)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700189 :id(i),
190 timeoutMs(timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800191{
192}
193
194Section::~Section()
195{
196}
197
Joe Onorato1754d742016-11-21 17:51:35 -0800198// ================================================================================
Yi Jinedfd5bb2017-09-06 17:09:11 -0700199HeaderSection::HeaderSection()
200 :Section(FIELD_ID_INCIDENT_HEADER, 0)
201{
202}
203
204HeaderSection::~HeaderSection()
205{
206}
207
208status_t
209HeaderSection::Execute(ReportRequestSet* requests) const
210{
211 for (ReportRequestSet::iterator it=requests->begin(); it!=requests->end(); it++) {
212 const sp<ReportRequest> request = *it;
213 const vector<vector<int8_t>>& headers = request->args.headers();
214
215 for (vector<vector<int8_t>>::const_iterator buf=headers.begin(); buf!=headers.end(); buf++) {
216 if (buf->empty()) continue;
217
218 // So the idea is only requests with negative fd are written to dropbox file.
219 int fd = request->fd >= 0 ? request->fd : requests->mainFd();
220 write_section_header(fd, FIELD_ID_INCIDENT_HEADER, buf->size());
221 write_all(fd, (uint8_t const*)buf->data(), buf->size());
222 // If there was an error now, there will be an error later and we will remove
223 // it from the list then.
224 }
225 }
226 return NO_ERROR;
227}
228
229// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700230FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700231 :Section(id, timeoutMs),
232 mFilename(filename)
233{
Yi Jinb44f7d42017-07-21 12:12:59 -0700234 name = filename;
Yi Jin0eb22342017-11-06 17:17:27 -0800235 mIsSysfs = strncmp(filename, "/sys/", 5) == 0;
Yi Jin0a3406f2017-06-22 19:23:11 -0700236}
237
238FileSection::~FileSection() {}
239
Yi Jin99c248f2017-08-25 18:11:58 -0700240status_t
241FileSection::Execute(ReportRequestSet* requests) const
242{
Yi Jinb44f7d42017-07-21 12:12:59 -0700243 // read from mFilename first, make sure the file is available
244 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700245 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700246 if (fd == -1) {
247 ALOGW("FileSection '%s' failed to open file", this->name.string());
248 return -errno;
249 }
250
Yi Jinb44f7d42017-07-21 12:12:59 -0700251 FdBuffer buffer;
252 Fpipe p2cPipe;
253 Fpipe c2pPipe;
254 // initiate pipes to pass data to/from incident_helper
255 if (!p2cPipe.init() || !c2pPipe.init()) {
256 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700257 return -errno;
258 }
259
Yi Jinedfd5bb2017-09-06 17:09:11 -0700260 pid_t pid = fork_execute_incident_helper(this->id, this->name.string(), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700261 if (pid == -1) {
262 ALOGW("FileSection '%s' failed to fork", this->name.string());
263 return -errno;
264 }
265
266 // parent process
267 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
Yi Jin0eb22342017-11-06 17:17:27 -0800268 this->timeoutMs, mIsSysfs);
Yi Jinb44f7d42017-07-21 12:12:59 -0700269 if (readStatus != NO_ERROR || buffer.timedOut()) {
270 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s, kill: %s",
271 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
Yi Jinedfd5bb2017-09-06 17:09:11 -0700272 strerror(-kill_child(pid)));
Yi Jinb44f7d42017-07-21 12:12:59 -0700273 return readStatus;
274 }
275
Yi Jinedfd5bb2017-09-06 17:09:11 -0700276 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700277 if (ihStatus != NO_ERROR) {
278 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-ihStatus));
279 return ihStatus;
280 }
281
282 ALOGD("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
Yi Jin0a3406f2017-06-22 19:23:11 -0700283 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700284 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700285 if (err != NO_ERROR) {
286 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
287 return err;
288 }
289
290 return NO_ERROR;
291}
292
293// ================================================================================
Joe Onorato1754d742016-11-21 17:51:35 -0800294struct WorkerThreadData : public virtual RefBase
295{
296 const WorkerThreadSection* section;
297 int fds[2];
298
299 // Lock protects these fields
300 mutex lock;
301 bool workerDone;
302 status_t workerError;
303
304 WorkerThreadData(const WorkerThreadSection* section);
305 virtual ~WorkerThreadData();
306
307 int readFd() { return fds[0]; }
308 int writeFd() { return fds[1]; }
309};
310
311WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
312 :section(sec),
313 workerDone(false),
314 workerError(NO_ERROR)
315{
316 fds[0] = -1;
317 fds[1] = -1;
318}
319
320WorkerThreadData::~WorkerThreadData()
321{
322}
323
324// ================================================================================
325WorkerThreadSection::WorkerThreadSection(int id)
326 :Section(id)
327{
328}
329
330WorkerThreadSection::~WorkerThreadSection()
331{
332}
333
334static void*
335worker_thread_func(void* cookie)
336{
337 WorkerThreadData* data = (WorkerThreadData*)cookie;
338 status_t err = data->section->BlockingCall(data->writeFd());
339
340 {
341 unique_lock<mutex> lock(data->lock);
342 data->workerDone = true;
343 data->workerError = err;
344 }
345
346 close(data->writeFd());
347 data->decStrong(data->section);
348 // data might be gone now. don't use it after this point in this thread.
349 return NULL;
350}
351
352status_t
353WorkerThreadSection::Execute(ReportRequestSet* requests) const
354{
355 status_t err = NO_ERROR;
356 pthread_t thread;
357 pthread_attr_t attr;
358 bool timedOut = false;
359 FdBuffer buffer;
360
361 // Data shared between this thread and the worker thread.
362 sp<WorkerThreadData> data = new WorkerThreadData(this);
363
364 // Create the pipe
365 err = pipe(data->fds);
366 if (err != 0) {
367 return -errno;
368 }
369
370 // The worker thread needs a reference and we can't let the count go to zero
371 // if that thread is slow to start.
372 data->incStrong(this);
373
374 // Create the thread
375 err = pthread_attr_init(&attr);
376 if (err != 0) {
377 return -err;
378 }
379 // TODO: Do we need to tweak thread priority?
380 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
381 if (err != 0) {
382 pthread_attr_destroy(&attr);
383 return -err;
384 }
385 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
386 if (err != 0) {
387 pthread_attr_destroy(&attr);
388 return -err;
389 }
390 pthread_attr_destroy(&attr);
391
392 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700393 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800394 if (err != NO_ERROR) {
395 // TODO: Log this error into the incident report.
396 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
397 strerror(-err));
398 }
399
400 // Done with the read fd. The worker thread closes the write one so
401 // we never race and get here first.
402 close(data->readFd());
403
404 // If the worker side is finished, then return its error (which may overwrite
405 // our possible error -- but it's more interesting anyway). If not, then we timed out.
406 {
407 unique_lock<mutex> lock(data->lock);
408 if (!data->workerDone) {
409 // We timed out
410 timedOut = true;
411 } else {
412 if (data->workerError != NO_ERROR) {
413 err = data->workerError;
414 // TODO: Log this error into the incident report.
415 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
416 strerror(-err));
417 }
418 }
419 }
420
421 if (timedOut || buffer.timedOut()) {
422 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
423 return NO_ERROR;
424 }
425
426 if (buffer.truncated()) {
427 // TODO: Log this into the incident report.
428 }
429
430 // TODO: There was an error with the command or buffering. Report that. For now
431 // just exit with a log messasge.
432 if (err != NO_ERROR) {
433 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
434 strerror(-err));
435 return NO_ERROR;
436 }
437
438 // Write the data that was collected
Yi Jinb44f7d42017-07-21 12:12:59 -0700439 ALOGD("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
Joe Onorato1754d742016-11-21 17:51:35 -0800440 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700441 err = write_report_requests(this->id, buffer, requests);
Joe Onorato1754d742016-11-21 17:51:35 -0800442 if (err != NO_ERROR) {
443 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
444 return err;
445 }
446
447 return NO_ERROR;
448}
449
450// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -0700451void
452CommandSection::init(const char* command, va_list args)
Yi Jinb44f7d42017-07-21 12:12:59 -0700453{
454 va_list copied_args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700455 int numOfArgs = 0;
Yi Jin4ef28b72017-08-14 14:45:28 -0700456
457 va_copy(copied_args, args);
458 while(va_arg(copied_args, const char*) != NULL) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700459 numOfArgs++;
460 }
Yi Jin4ef28b72017-08-14 14:45:28 -0700461 va_end(copied_args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700462
463 // allocate extra 1 for command and 1 for NULL terminator
464 mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2));
465
466 mCommand[0] = command;
467 name = command;
468 for (int i=0; i<numOfArgs; i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700469 const char* arg = va_arg(args, const char*);
Yi Jinb44f7d42017-07-21 12:12:59 -0700470 mCommand[i+1] = arg;
471 name += " ";
472 name += arg;
473 }
474 mCommand[numOfArgs+1] = NULL;
Yi Jinb44f7d42017-07-21 12:12:59 -0700475}
476
477CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700478 :Section(id, timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800479{
480 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700481 va_start(args, command);
482 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800483 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700484}
Joe Onorato1754d742016-11-21 17:51:35 -0800485
Yi Jinb44f7d42017-07-21 12:12:59 -0700486CommandSection::CommandSection(int id, const char* command, ...)
Yi Jinedfd5bb2017-09-06 17:09:11 -0700487 :Section(id)
Yi Jinb44f7d42017-07-21 12:12:59 -0700488{
489 va_list args;
490 va_start(args, command);
491 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800492 va_end(args);
493}
494
495CommandSection::~CommandSection()
496{
Yi Jinb44f7d42017-07-21 12:12:59 -0700497 free(mCommand);
Joe Onorato1754d742016-11-21 17:51:35 -0800498}
499
500status_t
Yi Jinb44f7d42017-07-21 12:12:59 -0700501CommandSection::Execute(ReportRequestSet* requests) const
Joe Onorato1754d742016-11-21 17:51:35 -0800502{
Yi Jinb44f7d42017-07-21 12:12:59 -0700503 FdBuffer buffer;
504 Fpipe cmdPipe;
505 Fpipe ihPipe;
506
507 if (!cmdPipe.init() || !ihPipe.init()) {
508 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
509 return -errno;
510 }
511
512 pid_t cmdPid = fork();
513 if (cmdPid == -1) {
514 ALOGW("CommandSection '%s' failed to fork", this->name.string());
515 return -errno;
516 }
517 // child process to execute the command as root
518 if (cmdPid == 0) {
519 // replace command's stdout with ihPipe's write Fd
520 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
521 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno));
522 _exit(EXIT_FAILURE);
523 }
524 execv(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(), strerror(errno));
527 _exit(err); // exit with command error code
528 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700529 pid_t ihPid = fork_execute_incident_helper(this->id, this->name.string(), cmdPipe, ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700530 if (ihPid == -1) {
531 ALOGW("CommandSection '%s' failed to fork", this->name.string());
532 return -errno;
533 }
534
535 close(cmdPipe.writeFd());
536 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
537 if (readStatus != NO_ERROR || buffer.timedOut()) {
538 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, "
539 "timedout: %s, kill command: %s, kill incident helper: %s",
540 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
Yi Jinedfd5bb2017-09-06 17:09:11 -0700541 strerror(-kill_child(cmdPid)), strerror(-kill_child(ihPid)));
Yi Jinb44f7d42017-07-21 12:12:59 -0700542 return readStatus;
543 }
544
545 // TODO: wait for command here has one trade-off: the failed status of command won't be detected until
546 // 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);
548 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700549 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinadd11e92017-07-30 16:10:07 -0700550 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb44f7d42017-07-21 12:12:59 -0700551 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
552 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
553 }
554
555 ALOGD("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
556 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700557 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jinb44f7d42017-07-21 12:12:59 -0700558 if (err != NO_ERROR) {
559 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
560 return err;
561 }
Joe Onorato1754d742016-11-21 17:51:35 -0800562 return NO_ERROR;
563}
564
565// ================================================================================
566DumpsysSection::DumpsysSection(int id, const char* service, ...)
567 :WorkerThreadSection(id),
568 mService(service)
569{
570 name = "dumpsys ";
571 name += service;
572
573 va_list args;
574 va_start(args, service);
575 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700576 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800577 if (arg == NULL) {
578 break;
579 }
580 mArgs.add(String16(arg));
581 name += " ";
582 name += arg;
583 }
584 va_end(args);
585}
586
587DumpsysSection::~DumpsysSection()
588{
589}
590
591status_t
592DumpsysSection::BlockingCall(int pipeWriteFd) const
593{
594 // checkService won't wait for the service to show up like getService will.
595 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700596
Joe Onorato1754d742016-11-21 17:51:35 -0800597 if (service == NULL) {
598 // Returning an error interrupts the entire incident report, so just
599 // log the failure.
600 // TODO: have a meta record inside the report that would log this
601 // failure inside the report, because the fact that we can't find
602 // the service is good data in and of itself. This is running in
603 // another thread so lock that carefully...
604 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
605 return NO_ERROR;
606 }
607
608 service->dump(pipeWriteFd, mArgs);
609
610 return NO_ERROR;
611}