Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 22 | #include <private/android_filesystem_config.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
| 24 | #include <mutex> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 25 | #include <wait.h> |
| 26 | #include <unistd.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace std; |
| 29 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 30 | const int WAIT_MAX = 5; |
| 31 | const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000}; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 32 | const char* INCIDENT_HELPER = "/system/bin/incident_helper"; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 33 | |
| 34 | static pid_t |
| 35 | forkAndExecuteIncidentHelper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe) |
| 36 | { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame^] | 37 | const char* ihArgs[] { INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL }; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 38 | |
| 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 | |
| 69 | static 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 | |
| 76 | static 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 88 | |
| 89 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 90 | Section::Section(int i, const int64_t timeoutMs) |
| 91 | :id(i), timeoutMs(timeoutMs) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 92 | { |
| 93 | } |
| 94 | |
| 95 | Section::~Section() |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | status_t |
| 100 | Section::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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 109 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
| 110 | : Section(id, timeoutMs), mFilename(filename) { |
| 111 | name = filename; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | FileSection::~FileSection() {} |
| 115 | |
| 116 | status_t FileSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 117 | // 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 IV | 6f9735b | 2017-08-03 16:08:29 -0700 | [diff] [blame] | 119 | int fd = open(mFilename, O_RDONLY | O_CLOEXEC); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 120 | if (fd == -1) { |
| 121 | ALOGW("FileSection '%s' failed to open file", this->name.string()); |
| 122 | return -errno; |
| 123 | } |
| 124 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 125 | 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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 131 | return -errno; |
| 132 | } |
| 133 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 134 | 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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 157 | (int)buffer.durationMs()); |
| 158 | WriteHeader(requests, buffer.size()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 159 | status_t err = buffer.write(requests); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 160 | 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 169 | struct 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 | |
| 186 | WorkerThreadData::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 | |
| 195 | WorkerThreadData::~WorkerThreadData() |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | // ================================================================================ |
| 200 | WorkerThreadSection::WorkerThreadSection(int id) |
| 201 | :Section(id) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | WorkerThreadSection::~WorkerThreadSection() |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | static void* |
| 210 | worker_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 | |
| 227 | status_t |
| 228 | WorkerThreadSection::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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 268 | err = buffer.read(data->readFd(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 269 | 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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 314 | ALOGD("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(), |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 315 | (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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 327 | void CommandSection::init(const char* command, va_list args) |
| 328 | { |
| 329 | va_list copied_args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 330 | int numOfArgs = 0; |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 331 | |
| 332 | va_copy(copied_args, args); |
| 333 | while(va_arg(copied_args, const char*) != NULL) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 334 | numOfArgs++; |
| 335 | } |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 336 | va_end(copied_args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 337 | |
| 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 Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 344 | const char* arg = va_arg(args, const char*); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 345 | mCommand[i+1] = arg; |
| 346 | name += " "; |
| 347 | name += arg; |
| 348 | } |
| 349 | mCommand[numOfArgs+1] = NULL; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
| 353 | : Section(id, timeoutMs) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 354 | { |
| 355 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 356 | va_start(args, command); |
| 357 | init(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 358 | va_end(args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 359 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 360 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 361 | CommandSection::CommandSection(int id, const char* command, ...) |
| 362 | : Section(id) |
| 363 | { |
| 364 | va_list args; |
| 365 | va_start(args, command); |
| 366 | init(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 367 | va_end(args); |
| 368 | } |
| 369 | |
| 370 | CommandSection::~CommandSection() |
| 371 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 372 | free(mCommand); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | status_t |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 376 | CommandSection::Execute(ReportRequestSet* requests) const |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 377 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 378 | 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 Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 425 | ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident helper: %s", |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 426 | 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 438 | return NO_ERROR; |
| 439 | } |
| 440 | |
| 441 | // ================================================================================ |
| 442 | DumpsysSection::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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 452 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 453 | if (arg == NULL) { |
| 454 | break; |
| 455 | } |
| 456 | mArgs.add(String16(arg)); |
| 457 | name += " "; |
| 458 | name += arg; |
| 459 | } |
| 460 | va_end(args); |
| 461 | } |
| 462 | |
| 463 | DumpsysSection::~DumpsysSection() |
| 464 | { |
| 465 | } |
| 466 | |
| 467 | status_t |
| 468 | DumpsysSection::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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 472 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 473 | 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 | } |