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 | |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <mutex> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <wait.h> |
| 28 | #include <unistd.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 33 | const int64_t INCIDENT_HELPER_TIMEOUT_MS = 5 * 1000; // 5 seconds |
| 34 | const char* INCIDENT_HELPER = "/system/bin/incident_helper"; |
| 35 | const uid_t IH_UID = 9999; // run incident_helper as nobody |
| 36 | const gid_t IH_GID = 9999; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 37 | |
| 38 | // ================================================================================ |
| 39 | Section::Section(int i) |
| 40 | :id(i) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Section::~Section() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | status_t |
| 49 | Section::WriteHeader(ReportRequestSet* requests, size_t size) const |
| 50 | { |
| 51 | ssize_t amt; |
| 52 | uint8_t buf[20]; |
| 53 | uint8_t* p = write_length_delimited_tag_header(buf, this->id, size); |
| 54 | return requests->write(buf, p-buf); |
| 55 | } |
| 56 | |
| 57 | // ================================================================================ |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 58 | FileSection::FileSection(int id, const char* filename) |
| 59 | : Section(id), mFilename(filename) { |
| 60 | name = "cat "; |
| 61 | name += filename; |
| 62 | } |
| 63 | |
| 64 | FileSection::~FileSection() {} |
| 65 | |
| 66 | status_t FileSection::Execute(ReportRequestSet* requests) const { |
| 67 | Fpipe p2cPipe; |
| 68 | Fpipe c2pPipe; |
| 69 | FdBuffer buffer; |
| 70 | |
| 71 | // initiate pipes to pass data to/from incident_helper |
| 72 | if (p2cPipe.init() == -1) { |
| 73 | return -errno; |
| 74 | } |
| 75 | if (c2pPipe.init() == -1) { |
| 76 | return -errno; |
| 77 | } |
| 78 | |
| 79 | // fork a child process |
| 80 | pid_t pid = fork(); |
| 81 | |
| 82 | if (pid == -1) { |
| 83 | ALOGW("FileSection '%s' failed to fork", this->name.string()); |
| 84 | return -errno; |
| 85 | } |
| 86 | |
| 87 | // child process |
| 88 | if (pid == 0) { |
| 89 | if (setgid(IH_GID) == -1) { |
| 90 | ALOGW("FileSection '%s' can't change gid: %s", this->name.string(), strerror(errno)); |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | if (setuid(IH_UID) == -1) { |
| 94 | ALOGW("FileSection '%s' can't change uid: %s", this->name.string(), strerror(errno)); |
| 95 | exit(EXIT_FAILURE); |
| 96 | } |
| 97 | |
| 98 | if (dup2(p2cPipe.readFd(), STDIN_FILENO) != 0 || !p2cPipe.close()) { |
| 99 | ALOGW("FileSection '%s' failed to set up stdin: %s", this->name.string(), strerror(errno)); |
| 100 | exit(EXIT_FAILURE); |
| 101 | } |
| 102 | if (dup2(c2pPipe.writeFd(), STDOUT_FILENO) != 1 || !c2pPipe.close()) { |
| 103 | ALOGW("FileSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno)); |
| 104 | exit(EXIT_FAILURE); |
| 105 | } |
| 106 | |
| 107 | // execute incident_helper to parse raw file data and generate protobuf |
| 108 | char sectionID[8]; // section id is expected to be smaller than 8 digits |
| 109 | sprintf(sectionID, "%d", this->id); |
| 110 | const char* args[]{INCIDENT_HELPER, "-s", sectionID, NULL}; |
| 111 | execv(INCIDENT_HELPER, const_cast<char**>(args)); |
| 112 | |
| 113 | ALOGW("FileSection '%s' failed in child process: %s", this->name.string(), strerror(errno)); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | // parent process |
| 118 | |
| 119 | // close fds used in child process |
| 120 | close(p2cPipe.readFd()); |
| 121 | close(c2pPipe.writeFd()); |
| 122 | |
| 123 | // read from mFilename and pump buffer to incident_helper |
| 124 | status_t err = NO_ERROR; |
| 125 | int fd = open(mFilename, O_RDONLY, 0444); |
| 126 | if (fd == -1) { |
| 127 | ALOGW("FileSection '%s' failed to open file", this->name.string()); |
| 128 | return -errno; |
| 129 | } |
| 130 | |
| 131 | err = buffer.readProcessedDataInStream(fd, |
| 132 | p2cPipe.writeFd(), c2pPipe.readFd(), INCIDENT_HELPER_TIMEOUT_MS); |
| 133 | if (err != NO_ERROR) { |
| 134 | ALOGW("FileSection '%s' failed to read data from incident helper: %s", |
| 135 | this->name.string(), strerror(-err)); |
| 136 | kill(pid, SIGKILL); // kill child process if meets error |
| 137 | return err; |
| 138 | } |
| 139 | |
| 140 | if (buffer.timedOut()) { |
| 141 | ALOGW("FileSection '%s' timed out reading from incident helper!", this->name.string()); |
| 142 | kill(pid, SIGKILL); // kill the child process if timed out |
| 143 | } |
| 144 | |
| 145 | // has to block here to reap child process |
| 146 | int status; |
| 147 | int w = waitpid(pid, &status, 0); |
| 148 | if (w < 0 || status == -1) { |
| 149 | ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-err)); |
| 150 | return -errno; |
| 151 | } |
| 152 | |
| 153 | // write parsed data to reporter |
| 154 | ALOGD("section '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 155 | (int)buffer.durationMs()); |
| 156 | WriteHeader(requests, buffer.size()); |
| 157 | err = buffer.write(requests); |
| 158 | if (err != NO_ERROR) { |
| 159 | ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | return NO_ERROR; |
| 164 | } |
| 165 | |
| 166 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 167 | struct WorkerThreadData : public virtual RefBase |
| 168 | { |
| 169 | const WorkerThreadSection* section; |
| 170 | int fds[2]; |
| 171 | |
| 172 | // Lock protects these fields |
| 173 | mutex lock; |
| 174 | bool workerDone; |
| 175 | status_t workerError; |
| 176 | |
| 177 | WorkerThreadData(const WorkerThreadSection* section); |
| 178 | virtual ~WorkerThreadData(); |
| 179 | |
| 180 | int readFd() { return fds[0]; } |
| 181 | int writeFd() { return fds[1]; } |
| 182 | }; |
| 183 | |
| 184 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
| 185 | :section(sec), |
| 186 | workerDone(false), |
| 187 | workerError(NO_ERROR) |
| 188 | { |
| 189 | fds[0] = -1; |
| 190 | fds[1] = -1; |
| 191 | } |
| 192 | |
| 193 | WorkerThreadData::~WorkerThreadData() |
| 194 | { |
| 195 | } |
| 196 | |
| 197 | // ================================================================================ |
| 198 | WorkerThreadSection::WorkerThreadSection(int id) |
| 199 | :Section(id) |
| 200 | { |
| 201 | } |
| 202 | |
| 203 | WorkerThreadSection::~WorkerThreadSection() |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | static void* |
| 208 | worker_thread_func(void* cookie) |
| 209 | { |
| 210 | WorkerThreadData* data = (WorkerThreadData*)cookie; |
| 211 | status_t err = data->section->BlockingCall(data->writeFd()); |
| 212 | |
| 213 | { |
| 214 | unique_lock<mutex> lock(data->lock); |
| 215 | data->workerDone = true; |
| 216 | data->workerError = err; |
| 217 | } |
| 218 | |
| 219 | close(data->writeFd()); |
| 220 | data->decStrong(data->section); |
| 221 | // data might be gone now. don't use it after this point in this thread. |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | status_t |
| 226 | WorkerThreadSection::Execute(ReportRequestSet* requests) const |
| 227 | { |
| 228 | status_t err = NO_ERROR; |
| 229 | pthread_t thread; |
| 230 | pthread_attr_t attr; |
| 231 | bool timedOut = false; |
| 232 | FdBuffer buffer; |
| 233 | |
| 234 | // Data shared between this thread and the worker thread. |
| 235 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 236 | |
| 237 | // Create the pipe |
| 238 | err = pipe(data->fds); |
| 239 | if (err != 0) { |
| 240 | return -errno; |
| 241 | } |
| 242 | |
| 243 | // The worker thread needs a reference and we can't let the count go to zero |
| 244 | // if that thread is slow to start. |
| 245 | data->incStrong(this); |
| 246 | |
| 247 | // Create the thread |
| 248 | err = pthread_attr_init(&attr); |
| 249 | if (err != 0) { |
| 250 | return -err; |
| 251 | } |
| 252 | // TODO: Do we need to tweak thread priority? |
| 253 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 254 | if (err != 0) { |
| 255 | pthread_attr_destroy(&attr); |
| 256 | return -err; |
| 257 | } |
| 258 | err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get()); |
| 259 | if (err != 0) { |
| 260 | pthread_attr_destroy(&attr); |
| 261 | return -err; |
| 262 | } |
| 263 | pthread_attr_destroy(&attr); |
| 264 | |
| 265 | // Loop reading until either the timeout or the worker side is done (i.e. eof). |
| 266 | err = buffer.read(data->readFd(), REMOTE_CALL_TIMEOUT_MS); |
| 267 | if (err != NO_ERROR) { |
| 268 | // TODO: Log this error into the incident report. |
| 269 | ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(), |
| 270 | strerror(-err)); |
| 271 | } |
| 272 | |
| 273 | // Done with the read fd. The worker thread closes the write one so |
| 274 | // we never race and get here first. |
| 275 | close(data->readFd()); |
| 276 | |
| 277 | // If the worker side is finished, then return its error (which may overwrite |
| 278 | // our possible error -- but it's more interesting anyway). If not, then we timed out. |
| 279 | { |
| 280 | unique_lock<mutex> lock(data->lock); |
| 281 | if (!data->workerDone) { |
| 282 | // We timed out |
| 283 | timedOut = true; |
| 284 | } else { |
| 285 | if (data->workerError != NO_ERROR) { |
| 286 | err = data->workerError; |
| 287 | // TODO: Log this error into the incident report. |
| 288 | ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(), |
| 289 | strerror(-err)); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (timedOut || buffer.timedOut()) { |
| 295 | ALOGW("WorkerThreadSection '%s' timed out", this->name.string()); |
| 296 | return NO_ERROR; |
| 297 | } |
| 298 | |
| 299 | if (buffer.truncated()) { |
| 300 | // TODO: Log this into the incident report. |
| 301 | } |
| 302 | |
| 303 | // TODO: There was an error with the command or buffering. Report that. For now |
| 304 | // just exit with a log messasge. |
| 305 | if (err != NO_ERROR) { |
| 306 | ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(), |
| 307 | strerror(-err)); |
| 308 | return NO_ERROR; |
| 309 | } |
| 310 | |
| 311 | // Write the data that was collected |
| 312 | ALOGD("section '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(), |
| 313 | (int)buffer.durationMs()); |
| 314 | WriteHeader(requests, buffer.size()); |
| 315 | err = buffer.write(requests); |
| 316 | if (err != NO_ERROR) { |
| 317 | ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err)); |
| 318 | return err; |
| 319 | } |
| 320 | |
| 321 | return NO_ERROR; |
| 322 | } |
| 323 | |
| 324 | // ================================================================================ |
| 325 | CommandSection::CommandSection(int id, const char* first, ...) |
| 326 | :Section(id) |
| 327 | { |
| 328 | va_list args; |
| 329 | int count = 0; |
| 330 | |
| 331 | va_start(args, first); |
| 332 | while (va_arg(args, const char*) != NULL) { |
| 333 | count++; |
| 334 | } |
| 335 | va_end(args); |
| 336 | |
| 337 | mCommand = (const char**)malloc(sizeof(const char*) * count); |
| 338 | |
| 339 | mCommand[0] = first; |
| 340 | name = first; |
| 341 | name += " "; |
| 342 | va_start(args, first); |
| 343 | for (int i=0; i<count; i++) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 344 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 345 | mCommand[i+1] = arg; |
| 346 | if (arg != NULL) { |
| 347 | name += va_arg(args, const char*); |
| 348 | name += " "; |
| 349 | } |
| 350 | } |
| 351 | va_end(args); |
| 352 | } |
| 353 | |
| 354 | CommandSection::~CommandSection() |
| 355 | { |
| 356 | } |
| 357 | |
| 358 | status_t |
| 359 | CommandSection::Execute(ReportRequestSet* /*requests*/) const |
| 360 | { |
| 361 | return NO_ERROR; |
| 362 | } |
| 363 | |
| 364 | // ================================================================================ |
| 365 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
| 366 | :WorkerThreadSection(id), |
| 367 | mService(service) |
| 368 | { |
| 369 | name = "dumpsys "; |
| 370 | name += service; |
| 371 | |
| 372 | va_list args; |
| 373 | va_start(args, service); |
| 374 | while (true) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 375 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 376 | if (arg == NULL) { |
| 377 | break; |
| 378 | } |
| 379 | mArgs.add(String16(arg)); |
| 380 | name += " "; |
| 381 | name += arg; |
| 382 | } |
| 383 | va_end(args); |
| 384 | } |
| 385 | |
| 386 | DumpsysSection::~DumpsysSection() |
| 387 | { |
| 388 | } |
| 389 | |
| 390 | status_t |
| 391 | DumpsysSection::BlockingCall(int pipeWriteFd) const |
| 392 | { |
| 393 | // checkService won't wait for the service to show up like getService will. |
| 394 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 395 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 396 | if (service == NULL) { |
| 397 | // Returning an error interrupts the entire incident report, so just |
| 398 | // log the failure. |
| 399 | // TODO: have a meta record inside the report that would log this |
| 400 | // failure inside the report, because the fact that we can't find |
| 401 | // the service is good data in and of itself. This is running in |
| 402 | // another thread so lock that carefully... |
| 403 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
| 404 | return NO_ERROR; |
| 405 | } |
| 406 | |
| 407 | service->dump(pipeWriteFd, mArgs); |
| 408 | |
| 409 | return NO_ERROR; |
| 410 | } |