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 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 19 | #include "EncodedBuffer.h" |
| 20 | #include "FdBuffer.h" |
| 21 | #include "Privacy.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | #include "Section.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include "io_util.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 25 | #include "protobuf.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 26 | #include "section_list.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 27 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 30 | #include <map> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | #include <mutex> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 32 | #include <wait.h> |
| 33 | #include <unistd.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 34 | |
| 35 | using namespace std; |
| 36 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 37 | const int WAIT_MAX = 5; |
| 38 | const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000}; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 39 | const char* INCIDENT_HELPER = "/system/bin/incident_helper"; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 40 | |
| 41 | static pid_t |
| 42 | forkAndExecuteIncidentHelper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe) |
| 43 | { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 44 | const char* ihArgs[] { INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL }; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 45 | |
| 46 | // fork used in multithreaded environment, avoid adding unnecessary code in child process |
| 47 | pid_t pid = fork(); |
| 48 | if (pid == 0) { |
| 49 | // child process executes incident helper as nobody |
| 50 | if (setgid(AID_NOBODY) == -1) { |
| 51 | ALOGW("%s can't change gid: %s", name, strerror(errno)); |
| 52 | _exit(EXIT_FAILURE); |
| 53 | } |
| 54 | if (setuid(AID_NOBODY) == -1) { |
| 55 | ALOGW("%s can't change uid: %s", name, strerror(errno)); |
| 56 | _exit(EXIT_FAILURE); |
| 57 | } |
| 58 | |
| 59 | if (dup2(p2cPipe.readFd(), STDIN_FILENO) != 0 || !p2cPipe.close() || |
| 60 | dup2(c2pPipe.writeFd(), STDOUT_FILENO) != 1 || !c2pPipe.close()) { |
| 61 | ALOGW("%s can't setup stdin and stdout for incident helper", name); |
| 62 | _exit(EXIT_FAILURE); |
| 63 | } |
| 64 | |
| 65 | execv(INCIDENT_HELPER, const_cast<char**>(ihArgs)); |
| 66 | |
| 67 | ALOGW("%s failed in incident helper process: %s", name, strerror(errno)); |
| 68 | _exit(EXIT_FAILURE); // always exits with failure if any |
| 69 | } |
| 70 | // close the fds used in incident helper |
| 71 | close(p2cPipe.readFd()); |
| 72 | close(c2pPipe.writeFd()); |
| 73 | return pid; |
| 74 | } |
| 75 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 76 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 77 | static status_t killChild(pid_t pid) { |
| 78 | int status; |
| 79 | kill(pid, SIGKILL); |
| 80 | if (waitpid(pid, &status, 0) == -1) return -1; |
| 81 | return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status); |
| 82 | } |
| 83 | |
| 84 | static status_t waitForChild(pid_t pid) { |
| 85 | int status; |
| 86 | bool died = false; |
| 87 | // wait for child to report status up to 1 seconds |
| 88 | for(int loop = 0; !died && loop < WAIT_MAX; loop++) { |
| 89 | if (waitpid(pid, &status, WNOHANG) == pid) died = true; |
| 90 | // sleep for 0.2 second |
| 91 | nanosleep(&WAIT_INTERVAL_NS, NULL); |
| 92 | } |
| 93 | if (!died) return killChild(pid); |
| 94 | return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status); |
| 95 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 96 | |
| 97 | // ================================================================================ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 98 | static const Privacy* |
| 99 | GetPrivacyOfSection(int id) |
| 100 | { |
| 101 | if (id < 0) return NULL; |
| 102 | int i=0; |
| 103 | while (PRIVACY_POLICY_LIST[i] != NULL) { |
| 104 | const Privacy* p = PRIVACY_POLICY_LIST[i]; |
| 105 | if (p->field_id == (uint32_t)id) return p; |
| 106 | if (p->field_id > (uint32_t)id) return NULL; |
| 107 | i++; |
| 108 | } |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | static status_t |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 113 | WriteToRequest(const int id, const int fd, EncodedBuffer& buffer) |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 114 | { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 115 | if (buffer.size() == 0) return NO_ERROR; |
| 116 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 117 | status_t err = NO_ERROR; |
| 118 | uint8_t buf[20]; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 119 | uint8_t *p = write_length_delimited_tag_header(buf, id, buffer.size()); |
| 120 | err = write_all(fd, buf, p-buf); |
| 121 | if (err == NO_ERROR) { |
| 122 | err = buffer.flush(fd); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 123 | } |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | static status_t |
| 128 | WriteToReportRequests(const int id, const FdBuffer& buffer, ReportRequestSet* requests) |
| 129 | { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 130 | status_t err = -EBADF; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 131 | EncodedBuffer encodedBuffer(buffer, GetPrivacyOfSection(id)); |
| 132 | int writeable = 0; |
| 133 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 134 | // The streaming ones, group requests by spec in order to save unnecessary strip operations |
| 135 | map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 136 | for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) { |
| 137 | sp<ReportRequest> request = *it; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 138 | if (!request->args.containsSection(id) || request->fd < 0 || request->err != NO_ERROR) { |
| 139 | continue; // skip invalid request |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 140 | } |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 141 | PrivacySpec spec = new_spec_from_args(request->args.dest()); |
| 142 | requestsBySpec[spec].push_back(request); |
| 143 | } |
| 144 | |
| 145 | for (map<PrivacySpec, vector<sp<ReportRequest>>>::iterator mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) { |
| 146 | PrivacySpec spec = mit->first; |
| 147 | err = encodedBuffer.strip(spec); |
| 148 | if (err != NO_ERROR) return err; // it means the encodedBuffer data is corrupted. |
| 149 | if (encodedBuffer.size() == 0) continue; |
| 150 | |
| 151 | for (vector<sp<ReportRequest>>::iterator it = mit->second.begin(); it != mit->second.end(); it++) { |
| 152 | sp<ReportRequest> request = *it; |
| 153 | err = WriteToRequest(id, request->fd, encodedBuffer); |
| 154 | if (err != NO_ERROR) { |
| 155 | request->err = err; |
| 156 | } else { |
| 157 | writeable++; |
| 158 | } |
| 159 | ALOGD("Section %d flushed %zu bytes to fd %d with spec %d", id, encodedBuffer.size(), request->fd, spec.dest); |
| 160 | } |
| 161 | encodedBuffer.clear(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // The dropbox file |
| 165 | if (requests->mainFd() >= 0) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame^] | 166 | err = encodedBuffer.strip(get_default_dropbox_spec()); |
| 167 | if (err != NO_ERROR) return err; // the buffer data is corrupted. |
| 168 | |
| 169 | err = WriteToRequest(id, requests->mainFd(), encodedBuffer); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 170 | if (err != NO_ERROR) { |
| 171 | requests->setMainFd(-1); |
| 172 | } else { |
| 173 | writeable++; |
| 174 | } |
| 175 | } |
| 176 | // only returns error if there is no fd to write to. |
| 177 | return writeable > 0 ? NO_ERROR : err; |
| 178 | } |
| 179 | |
| 180 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 181 | Section::Section(int i, const int64_t timeoutMs) |
| 182 | :id(i), timeoutMs(timeoutMs) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 183 | { |
| 184 | } |
| 185 | |
| 186 | Section::~Section() |
| 187 | { |
| 188 | } |
| 189 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 190 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 191 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
| 192 | : Section(id, timeoutMs), mFilename(filename) { |
| 193 | name = filename; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | FileSection::~FileSection() {} |
| 197 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 198 | status_t |
| 199 | FileSection::Execute(ReportRequestSet* requests) const |
| 200 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 201 | // read from mFilename first, make sure the file is available |
| 202 | // 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] | 203 | int fd = open(mFilename, O_RDONLY | O_CLOEXEC); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 204 | if (fd == -1) { |
| 205 | ALOGW("FileSection '%s' failed to open file", this->name.string()); |
| 206 | return -errno; |
| 207 | } |
| 208 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 209 | FdBuffer buffer; |
| 210 | Fpipe p2cPipe; |
| 211 | Fpipe c2pPipe; |
| 212 | // initiate pipes to pass data to/from incident_helper |
| 213 | if (!p2cPipe.init() || !c2pPipe.init()) { |
| 214 | ALOGW("FileSection '%s' failed to setup pipes", this->name.string()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 215 | return -errno; |
| 216 | } |
| 217 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 218 | pid_t pid = forkAndExecuteIncidentHelper(this->id, this->name.string(), p2cPipe, c2pPipe); |
| 219 | if (pid == -1) { |
| 220 | ALOGW("FileSection '%s' failed to fork", this->name.string()); |
| 221 | return -errno; |
| 222 | } |
| 223 | |
| 224 | // parent process |
| 225 | status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(), |
| 226 | this->timeoutMs); |
| 227 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
| 228 | ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s, kill: %s", |
| 229 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false", |
| 230 | strerror(-killChild(pid))); |
| 231 | return readStatus; |
| 232 | } |
| 233 | |
| 234 | status_t ihStatus = waitForChild(pid); |
| 235 | if (ihStatus != NO_ERROR) { |
| 236 | ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-ihStatus)); |
| 237 | return ihStatus; |
| 238 | } |
| 239 | |
| 240 | 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] | 241 | (int)buffer.durationMs()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 242 | status_t err = WriteToReportRequests(this->id, buffer, requests); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 243 | if (err != NO_ERROR) { |
| 244 | ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 245 | return err; |
| 246 | } |
| 247 | |
| 248 | return NO_ERROR; |
| 249 | } |
| 250 | |
| 251 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 252 | struct WorkerThreadData : public virtual RefBase |
| 253 | { |
| 254 | const WorkerThreadSection* section; |
| 255 | int fds[2]; |
| 256 | |
| 257 | // Lock protects these fields |
| 258 | mutex lock; |
| 259 | bool workerDone; |
| 260 | status_t workerError; |
| 261 | |
| 262 | WorkerThreadData(const WorkerThreadSection* section); |
| 263 | virtual ~WorkerThreadData(); |
| 264 | |
| 265 | int readFd() { return fds[0]; } |
| 266 | int writeFd() { return fds[1]; } |
| 267 | }; |
| 268 | |
| 269 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
| 270 | :section(sec), |
| 271 | workerDone(false), |
| 272 | workerError(NO_ERROR) |
| 273 | { |
| 274 | fds[0] = -1; |
| 275 | fds[1] = -1; |
| 276 | } |
| 277 | |
| 278 | WorkerThreadData::~WorkerThreadData() |
| 279 | { |
| 280 | } |
| 281 | |
| 282 | // ================================================================================ |
| 283 | WorkerThreadSection::WorkerThreadSection(int id) |
| 284 | :Section(id) |
| 285 | { |
| 286 | } |
| 287 | |
| 288 | WorkerThreadSection::~WorkerThreadSection() |
| 289 | { |
| 290 | } |
| 291 | |
| 292 | static void* |
| 293 | worker_thread_func(void* cookie) |
| 294 | { |
| 295 | WorkerThreadData* data = (WorkerThreadData*)cookie; |
| 296 | status_t err = data->section->BlockingCall(data->writeFd()); |
| 297 | |
| 298 | { |
| 299 | unique_lock<mutex> lock(data->lock); |
| 300 | data->workerDone = true; |
| 301 | data->workerError = err; |
| 302 | } |
| 303 | |
| 304 | close(data->writeFd()); |
| 305 | data->decStrong(data->section); |
| 306 | // data might be gone now. don't use it after this point in this thread. |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | status_t |
| 311 | WorkerThreadSection::Execute(ReportRequestSet* requests) const |
| 312 | { |
| 313 | status_t err = NO_ERROR; |
| 314 | pthread_t thread; |
| 315 | pthread_attr_t attr; |
| 316 | bool timedOut = false; |
| 317 | FdBuffer buffer; |
| 318 | |
| 319 | // Data shared between this thread and the worker thread. |
| 320 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 321 | |
| 322 | // Create the pipe |
| 323 | err = pipe(data->fds); |
| 324 | if (err != 0) { |
| 325 | return -errno; |
| 326 | } |
| 327 | |
| 328 | // The worker thread needs a reference and we can't let the count go to zero |
| 329 | // if that thread is slow to start. |
| 330 | data->incStrong(this); |
| 331 | |
| 332 | // Create the thread |
| 333 | err = pthread_attr_init(&attr); |
| 334 | if (err != 0) { |
| 335 | return -err; |
| 336 | } |
| 337 | // TODO: Do we need to tweak thread priority? |
| 338 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 339 | if (err != 0) { |
| 340 | pthread_attr_destroy(&attr); |
| 341 | return -err; |
| 342 | } |
| 343 | err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get()); |
| 344 | if (err != 0) { |
| 345 | pthread_attr_destroy(&attr); |
| 346 | return -err; |
| 347 | } |
| 348 | pthread_attr_destroy(&attr); |
| 349 | |
| 350 | // 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] | 351 | err = buffer.read(data->readFd(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 352 | if (err != NO_ERROR) { |
| 353 | // TODO: Log this error into the incident report. |
| 354 | ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(), |
| 355 | strerror(-err)); |
| 356 | } |
| 357 | |
| 358 | // Done with the read fd. The worker thread closes the write one so |
| 359 | // we never race and get here first. |
| 360 | close(data->readFd()); |
| 361 | |
| 362 | // If the worker side is finished, then return its error (which may overwrite |
| 363 | // our possible error -- but it's more interesting anyway). If not, then we timed out. |
| 364 | { |
| 365 | unique_lock<mutex> lock(data->lock); |
| 366 | if (!data->workerDone) { |
| 367 | // We timed out |
| 368 | timedOut = true; |
| 369 | } else { |
| 370 | if (data->workerError != NO_ERROR) { |
| 371 | err = data->workerError; |
| 372 | // TODO: Log this error into the incident report. |
| 373 | ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(), |
| 374 | strerror(-err)); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (timedOut || buffer.timedOut()) { |
| 380 | ALOGW("WorkerThreadSection '%s' timed out", this->name.string()); |
| 381 | return NO_ERROR; |
| 382 | } |
| 383 | |
| 384 | if (buffer.truncated()) { |
| 385 | // TODO: Log this into the incident report. |
| 386 | } |
| 387 | |
| 388 | // TODO: There was an error with the command or buffering. Report that. For now |
| 389 | // just exit with a log messasge. |
| 390 | if (err != NO_ERROR) { |
| 391 | ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(), |
| 392 | strerror(-err)); |
| 393 | return NO_ERROR; |
| 394 | } |
| 395 | |
| 396 | // Write the data that was collected |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 397 | 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] | 398 | (int)buffer.durationMs()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 399 | err = WriteToReportRequests(this->id, buffer, requests); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 400 | if (err != NO_ERROR) { |
| 401 | ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err)); |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | return NO_ERROR; |
| 406 | } |
| 407 | |
| 408 | // ================================================================================ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 409 | void |
| 410 | CommandSection::init(const char* command, va_list args) |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 411 | { |
| 412 | va_list copied_args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 413 | int numOfArgs = 0; |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 414 | |
| 415 | va_copy(copied_args, args); |
| 416 | while(va_arg(copied_args, const char*) != NULL) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 417 | numOfArgs++; |
| 418 | } |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 419 | va_end(copied_args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 420 | |
| 421 | // allocate extra 1 for command and 1 for NULL terminator |
| 422 | mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2)); |
| 423 | |
| 424 | mCommand[0] = command; |
| 425 | name = command; |
| 426 | for (int i=0; i<numOfArgs; i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 427 | const char* arg = va_arg(args, const char*); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 428 | mCommand[i+1] = arg; |
| 429 | name += " "; |
| 430 | name += arg; |
| 431 | } |
| 432 | mCommand[numOfArgs+1] = NULL; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
| 436 | : Section(id, timeoutMs) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 437 | { |
| 438 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 439 | va_start(args, command); |
| 440 | init(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 441 | va_end(args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 442 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 443 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 444 | CommandSection::CommandSection(int id, const char* command, ...) |
| 445 | : Section(id) |
| 446 | { |
| 447 | va_list args; |
| 448 | va_start(args, command); |
| 449 | init(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 450 | va_end(args); |
| 451 | } |
| 452 | |
| 453 | CommandSection::~CommandSection() |
| 454 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 455 | free(mCommand); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | status_t |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 459 | CommandSection::Execute(ReportRequestSet* requests) const |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 460 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 461 | FdBuffer buffer; |
| 462 | Fpipe cmdPipe; |
| 463 | Fpipe ihPipe; |
| 464 | |
| 465 | if (!cmdPipe.init() || !ihPipe.init()) { |
| 466 | ALOGW("CommandSection '%s' failed to setup pipes", this->name.string()); |
| 467 | return -errno; |
| 468 | } |
| 469 | |
| 470 | pid_t cmdPid = fork(); |
| 471 | if (cmdPid == -1) { |
| 472 | ALOGW("CommandSection '%s' failed to fork", this->name.string()); |
| 473 | return -errno; |
| 474 | } |
| 475 | // child process to execute the command as root |
| 476 | if (cmdPid == 0) { |
| 477 | // replace command's stdout with ihPipe's write Fd |
| 478 | if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) { |
| 479 | ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno)); |
| 480 | _exit(EXIT_FAILURE); |
| 481 | } |
| 482 | execv(this->mCommand[0], (char *const *) this->mCommand); |
| 483 | int err = errno; // record command error code |
| 484 | ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(), strerror(errno)); |
| 485 | _exit(err); // exit with command error code |
| 486 | } |
| 487 | pid_t ihPid = forkAndExecuteIncidentHelper(this->id, this->name.string(), cmdPipe, ihPipe); |
| 488 | if (ihPid == -1) { |
| 489 | ALOGW("CommandSection '%s' failed to fork", this->name.string()); |
| 490 | return -errno; |
| 491 | } |
| 492 | |
| 493 | close(cmdPipe.writeFd()); |
| 494 | status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs); |
| 495 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
| 496 | ALOGW("CommandSection '%s' failed to read data from incident helper: %s, " |
| 497 | "timedout: %s, kill command: %s, kill incident helper: %s", |
| 498 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false", |
| 499 | strerror(-killChild(cmdPid)), strerror(-killChild(ihPid))); |
| 500 | return readStatus; |
| 501 | } |
| 502 | |
| 503 | // TODO: wait for command here has one trade-off: the failed status of command won't be detected until |
| 504 | // buffer timeout, but it has advatage on starting the data stream earlier. |
| 505 | status_t cmdStatus = waitForChild(cmdPid); |
| 506 | status_t ihStatus = waitForChild(ihPid); |
| 507 | if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) { |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 508 | 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] | 509 | this->name.string(), strerror(-cmdStatus), strerror(-ihStatus)); |
| 510 | return cmdStatus != NO_ERROR ? cmdStatus : ihStatus; |
| 511 | } |
| 512 | |
| 513 | ALOGD("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 514 | (int)buffer.durationMs()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 515 | status_t err = WriteToReportRequests(this->id, buffer, requests); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 516 | if (err != NO_ERROR) { |
| 517 | ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 518 | return err; |
| 519 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 520 | return NO_ERROR; |
| 521 | } |
| 522 | |
| 523 | // ================================================================================ |
| 524 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
| 525 | :WorkerThreadSection(id), |
| 526 | mService(service) |
| 527 | { |
| 528 | name = "dumpsys "; |
| 529 | name += service; |
| 530 | |
| 531 | va_list args; |
| 532 | va_start(args, service); |
| 533 | while (true) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 534 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 535 | if (arg == NULL) { |
| 536 | break; |
| 537 | } |
| 538 | mArgs.add(String16(arg)); |
| 539 | name += " "; |
| 540 | name += arg; |
| 541 | } |
| 542 | va_end(args); |
| 543 | } |
| 544 | |
| 545 | DumpsysSection::~DumpsysSection() |
| 546 | { |
| 547 | } |
| 548 | |
| 549 | status_t |
| 550 | DumpsysSection::BlockingCall(int pipeWriteFd) const |
| 551 | { |
| 552 | // checkService won't wait for the service to show up like getService will. |
| 553 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 554 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 555 | if (service == NULL) { |
| 556 | // Returning an error interrupts the entire incident report, so just |
| 557 | // log the failure. |
| 558 | // TODO: have a meta record inside the report that would log this |
| 559 | // failure inside the report, because the fact that we can't find |
| 560 | // the service is good data in and of itself. This is running in |
| 561 | // another thread so lock that carefully... |
| 562 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
| 563 | return NO_ERROR; |
| 564 | } |
| 565 | |
| 566 | service->dump(pipeWriteFd, mArgs); |
| 567 | |
| 568 | return NO_ERROR; |
| 569 | } |