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