blob: 0f6f38edf3c49c6e9810f03ab13619bb5f4582ba [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
19#include "Section.h"
20#include "protobuf.h"
21
Yi Jinb44f7d42017-07-21 12:12:59 -070022#include <private/android_filesystem_config.h>
Joe Onorato1754d742016-11-21 17:51:35 -080023#include <binder/IServiceManager.h>
24#include <mutex>
Yi Jin0a3406f2017-06-22 19:23:11 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <wait.h>
29#include <unistd.h>
Joe Onorato1754d742016-11-21 17:51:35 -080030
31using namespace std;
32
Yi Jinb44f7d42017-07-21 12:12:59 -070033const int WAIT_MAX = 5;
34const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
Yi Jin0a3406f2017-06-22 19:23:11 -070035const char* INCIDENT_HELPER = "/system/bin/incident_helper";
Yi Jinb44f7d42017-07-21 12:12:59 -070036
37static pid_t
38forkAndExecuteIncidentHelper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe)
39{
40 const char* ihArgs[] { INCIDENT_HELPER, "-s", to_string(id).c_str(), NULL };
41
42 // fork used in multithreaded environment, avoid adding unnecessary code in child process
43 pid_t pid = fork();
44 if (pid == 0) {
45 // child process executes incident helper as nobody
46 if (setgid(AID_NOBODY) == -1) {
47 ALOGW("%s can't change gid: %s", name, strerror(errno));
48 _exit(EXIT_FAILURE);
49 }
50 if (setuid(AID_NOBODY) == -1) {
51 ALOGW("%s can't change uid: %s", name, strerror(errno));
52 _exit(EXIT_FAILURE);
53 }
54
55 if (dup2(p2cPipe.readFd(), STDIN_FILENO) != 0 || !p2cPipe.close() ||
56 dup2(c2pPipe.writeFd(), STDOUT_FILENO) != 1 || !c2pPipe.close()) {
57 ALOGW("%s can't setup stdin and stdout for incident helper", name);
58 _exit(EXIT_FAILURE);
59 }
60
61 execv(INCIDENT_HELPER, const_cast<char**>(ihArgs));
62
63 ALOGW("%s failed in incident helper process: %s", name, strerror(errno));
64 _exit(EXIT_FAILURE); // always exits with failure if any
65 }
66 // close the fds used in incident helper
67 close(p2cPipe.readFd());
68 close(c2pPipe.writeFd());
69 return pid;
70}
71
72static status_t killChild(pid_t pid) {
73 int status;
74 kill(pid, SIGKILL);
75 if (waitpid(pid, &status, 0) == -1) return -1;
76 return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status);
77}
78
79static status_t waitForChild(pid_t pid) {
80 int status;
81 bool died = false;
82 // wait for child to report status up to 1 seconds
83 for(int loop = 0; !died && loop < WAIT_MAX; loop++) {
84 if (waitpid(pid, &status, WNOHANG) == pid) died = true;
85 // sleep for 0.2 second
86 nanosleep(&WAIT_INTERVAL_NS, NULL);
87 }
88 if (!died) return killChild(pid);
89 return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status);
90}
Joe Onorato1754d742016-11-21 17:51:35 -080091
92// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -070093Section::Section(int i, const int64_t timeoutMs)
94 :id(i), timeoutMs(timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -080095{
96}
97
98Section::~Section()
99{
100}
101
102status_t
103Section::WriteHeader(ReportRequestSet* requests, size_t size) const
104{
105 ssize_t amt;
106 uint8_t buf[20];
107 uint8_t* p = write_length_delimited_tag_header(buf, this->id, size);
108 return requests->write(buf, p-buf);
109}
110
111// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700112FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
113 : Section(id, timeoutMs), mFilename(filename) {
114 name = filename;
Yi Jin0a3406f2017-06-22 19:23:11 -0700115}
116
117FileSection::~FileSection() {}
118
119status_t FileSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700120 // read from mFilename first, make sure the file is available
121 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700122 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700123 if (fd == -1) {
124 ALOGW("FileSection '%s' failed to open file", this->name.string());
125 return -errno;
126 }
127
Yi Jinb44f7d42017-07-21 12:12:59 -0700128 FdBuffer buffer;
129 Fpipe p2cPipe;
130 Fpipe c2pPipe;
131 // initiate pipes to pass data to/from incident_helper
132 if (!p2cPipe.init() || !c2pPipe.init()) {
133 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700134 return -errno;
135 }
136
Yi Jinb44f7d42017-07-21 12:12:59 -0700137 pid_t pid = forkAndExecuteIncidentHelper(this->id, this->name.string(), p2cPipe, c2pPipe);
138 if (pid == -1) {
139 ALOGW("FileSection '%s' failed to fork", this->name.string());
140 return -errno;
141 }
142
143 // parent process
144 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
145 this->timeoutMs);
146 if (readStatus != NO_ERROR || buffer.timedOut()) {
147 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s, kill: %s",
148 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
149 strerror(-killChild(pid)));
150 return readStatus;
151 }
152
153 status_t ihStatus = waitForChild(pid);
154 if (ihStatus != NO_ERROR) {
155 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-ihStatus));
156 return ihStatus;
157 }
158
159 ALOGD("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
Yi Jin0a3406f2017-06-22 19:23:11 -0700160 (int)buffer.durationMs());
161 WriteHeader(requests, buffer.size());
Yi Jinb44f7d42017-07-21 12:12:59 -0700162 status_t err = buffer.write(requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700163 if (err != NO_ERROR) {
164 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
165 return err;
166 }
167
168 return NO_ERROR;
169}
170
171// ================================================================================
Joe Onorato1754d742016-11-21 17:51:35 -0800172struct WorkerThreadData : public virtual RefBase
173{
174 const WorkerThreadSection* section;
175 int fds[2];
176
177 // Lock protects these fields
178 mutex lock;
179 bool workerDone;
180 status_t workerError;
181
182 WorkerThreadData(const WorkerThreadSection* section);
183 virtual ~WorkerThreadData();
184
185 int readFd() { return fds[0]; }
186 int writeFd() { return fds[1]; }
187};
188
189WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
190 :section(sec),
191 workerDone(false),
192 workerError(NO_ERROR)
193{
194 fds[0] = -1;
195 fds[1] = -1;
196}
197
198WorkerThreadData::~WorkerThreadData()
199{
200}
201
202// ================================================================================
203WorkerThreadSection::WorkerThreadSection(int id)
204 :Section(id)
205{
206}
207
208WorkerThreadSection::~WorkerThreadSection()
209{
210}
211
212static void*
213worker_thread_func(void* cookie)
214{
215 WorkerThreadData* data = (WorkerThreadData*)cookie;
216 status_t err = data->section->BlockingCall(data->writeFd());
217
218 {
219 unique_lock<mutex> lock(data->lock);
220 data->workerDone = true;
221 data->workerError = err;
222 }
223
224 close(data->writeFd());
225 data->decStrong(data->section);
226 // data might be gone now. don't use it after this point in this thread.
227 return NULL;
228}
229
230status_t
231WorkerThreadSection::Execute(ReportRequestSet* requests) const
232{
233 status_t err = NO_ERROR;
234 pthread_t thread;
235 pthread_attr_t attr;
236 bool timedOut = false;
237 FdBuffer buffer;
238
239 // Data shared between this thread and the worker thread.
240 sp<WorkerThreadData> data = new WorkerThreadData(this);
241
242 // Create the pipe
243 err = pipe(data->fds);
244 if (err != 0) {
245 return -errno;
246 }
247
248 // The worker thread needs a reference and we can't let the count go to zero
249 // if that thread is slow to start.
250 data->incStrong(this);
251
252 // Create the thread
253 err = pthread_attr_init(&attr);
254 if (err != 0) {
255 return -err;
256 }
257 // TODO: Do we need to tweak thread priority?
258 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
259 if (err != 0) {
260 pthread_attr_destroy(&attr);
261 return -err;
262 }
263 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
264 if (err != 0) {
265 pthread_attr_destroy(&attr);
266 return -err;
267 }
268 pthread_attr_destroy(&attr);
269
270 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700271 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800272 if (err != NO_ERROR) {
273 // TODO: Log this error into the incident report.
274 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
275 strerror(-err));
276 }
277
278 // Done with the read fd. The worker thread closes the write one so
279 // we never race and get here first.
280 close(data->readFd());
281
282 // If the worker side is finished, then return its error (which may overwrite
283 // our possible error -- but it's more interesting anyway). If not, then we timed out.
284 {
285 unique_lock<mutex> lock(data->lock);
286 if (!data->workerDone) {
287 // We timed out
288 timedOut = true;
289 } else {
290 if (data->workerError != NO_ERROR) {
291 err = data->workerError;
292 // TODO: Log this error into the incident report.
293 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
294 strerror(-err));
295 }
296 }
297 }
298
299 if (timedOut || buffer.timedOut()) {
300 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
301 return NO_ERROR;
302 }
303
304 if (buffer.truncated()) {
305 // TODO: Log this into the incident report.
306 }
307
308 // TODO: There was an error with the command or buffering. Report that. For now
309 // just exit with a log messasge.
310 if (err != NO_ERROR) {
311 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
312 strerror(-err));
313 return NO_ERROR;
314 }
315
316 // Write the data that was collected
Yi Jinb44f7d42017-07-21 12:12:59 -0700317 ALOGD("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
Joe Onorato1754d742016-11-21 17:51:35 -0800318 (int)buffer.durationMs());
319 WriteHeader(requests, buffer.size());
320 err = buffer.write(requests);
321 if (err != NO_ERROR) {
322 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
323 return err;
324 }
325
326 return NO_ERROR;
327}
328
329// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700330void CommandSection::init(const char* command, va_list args)
331{
332 va_list copied_args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700333 int numOfArgs = 0;
Yi Jin4ef28b72017-08-14 14:45:28 -0700334
335 va_copy(copied_args, args);
336 while(va_arg(copied_args, const char*) != NULL) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700337 numOfArgs++;
338 }
Yi Jin4ef28b72017-08-14 14:45:28 -0700339 va_end(copied_args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700340
341 // allocate extra 1 for command and 1 for NULL terminator
342 mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2));
343
344 mCommand[0] = command;
345 name = command;
346 for (int i=0; i<numOfArgs; i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700347 const char* arg = va_arg(args, const char*);
Yi Jinb44f7d42017-07-21 12:12:59 -0700348 mCommand[i+1] = arg;
349 name += " ";
350 name += arg;
351 }
352 mCommand[numOfArgs+1] = NULL;
Yi Jinb44f7d42017-07-21 12:12:59 -0700353}
354
355CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
356 : Section(id, timeoutMs)
Joe Onorato1754d742016-11-21 17:51:35 -0800357{
358 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700359 va_start(args, command);
360 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800361 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700362}
Joe Onorato1754d742016-11-21 17:51:35 -0800363
Yi Jinb44f7d42017-07-21 12:12:59 -0700364CommandSection::CommandSection(int id, const char* command, ...)
365 : Section(id)
366{
367 va_list args;
368 va_start(args, command);
369 init(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800370 va_end(args);
371}
372
373CommandSection::~CommandSection()
374{
Yi Jinb44f7d42017-07-21 12:12:59 -0700375 free(mCommand);
Joe Onorato1754d742016-11-21 17:51:35 -0800376}
377
378status_t
Yi Jinb44f7d42017-07-21 12:12:59 -0700379CommandSection::Execute(ReportRequestSet* requests) const
Joe Onorato1754d742016-11-21 17:51:35 -0800380{
Yi Jinb44f7d42017-07-21 12:12:59 -0700381 FdBuffer buffer;
382 Fpipe cmdPipe;
383 Fpipe ihPipe;
384
385 if (!cmdPipe.init() || !ihPipe.init()) {
386 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
387 return -errno;
388 }
389
390 pid_t cmdPid = fork();
391 if (cmdPid == -1) {
392 ALOGW("CommandSection '%s' failed to fork", this->name.string());
393 return -errno;
394 }
395 // child process to execute the command as root
396 if (cmdPid == 0) {
397 // replace command's stdout with ihPipe's write Fd
398 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
399 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno));
400 _exit(EXIT_FAILURE);
401 }
402 execv(this->mCommand[0], (char *const *) this->mCommand);
403 int err = errno; // record command error code
404 ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(), strerror(errno));
405 _exit(err); // exit with command error code
406 }
407 pid_t ihPid = forkAndExecuteIncidentHelper(this->id, this->name.string(), cmdPipe, ihPipe);
408 if (ihPid == -1) {
409 ALOGW("CommandSection '%s' failed to fork", this->name.string());
410 return -errno;
411 }
412
413 close(cmdPipe.writeFd());
414 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
415 if (readStatus != NO_ERROR || buffer.timedOut()) {
416 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, "
417 "timedout: %s, kill command: %s, kill incident helper: %s",
418 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false",
419 strerror(-killChild(cmdPid)), strerror(-killChild(ihPid)));
420 return readStatus;
421 }
422
423 // TODO: wait for command here has one trade-off: the failed status of command won't be detected until
424 // buffer timeout, but it has advatage on starting the data stream earlier.
425 status_t cmdStatus = waitForChild(cmdPid);
426 status_t ihStatus = waitForChild(ihPid);
427 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinadd11e92017-07-30 16:10:07 -0700428 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb44f7d42017-07-21 12:12:59 -0700429 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
430 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
431 }
432
433 ALOGD("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
434 (int)buffer.durationMs());
435 WriteHeader(requests, buffer.size());
436 status_t err = buffer.write(requests);
437 if (err != NO_ERROR) {
438 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
439 return err;
440 }
Joe Onorato1754d742016-11-21 17:51:35 -0800441 return NO_ERROR;
442}
443
444// ================================================================================
445DumpsysSection::DumpsysSection(int id, const char* service, ...)
446 :WorkerThreadSection(id),
447 mService(service)
448{
449 name = "dumpsys ";
450 name += service;
451
452 va_list args;
453 va_start(args, service);
454 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700455 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800456 if (arg == NULL) {
457 break;
458 }
459 mArgs.add(String16(arg));
460 name += " ";
461 name += arg;
462 }
463 va_end(args);
464}
465
466DumpsysSection::~DumpsysSection()
467{
468}
469
470status_t
471DumpsysSection::BlockingCall(int pipeWriteFd) const
472{
473 // checkService won't wait for the service to show up like getService will.
474 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700475
Joe Onorato1754d742016-11-21 17:51:35 -0800476 if (service == NULL) {
477 // Returning an error interrupts the entire incident report, so just
478 // log the failure.
479 // TODO: have a meta record inside the report that would log this
480 // failure inside the report, because the fact that we can't find
481 // the service is good data in and of itself. This is running in
482 // another thread so lock that carefully...
483 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
484 return NO_ERROR;
485 }
486
487 service->dump(pipeWriteFd, mArgs);
488
489 return NO_ERROR;
490}